Skip to content

Commit

Permalink
Implement LoadGameHook (LoopFlag)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Oct 18, 2022
1 parent a330eb9 commit 220c30a
Show file tree
Hide file tree
Showing 19 changed files with 286 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ target_sources(${EXECUTABLE_NAME} PUBLIC
"src/lips.h"
"src/loadsave.cc"
"src/loadsave.h"
"src/loop.cc"
"src/loop.h"
"src/main.cc"
"src/main.h"
"src/map_defs.h"
Expand Down
5 changes: 5 additions & 0 deletions src/automap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "input.h"
#include "item.h"
#include "kb.h"
#include "loop.h"
#include "map.h"
#include "memory.h"
#include "object.h"
Expand Down Expand Up @@ -395,6 +396,8 @@ void automapShow(bool isInGame, bool isUsingScanner)
bool isoWasEnabled = isoDisable();
gameMouseSetCursor(MOUSE_CURSOR_ARROW);

loopSetFlag(LoopFlag::AUTOMAP);

bool done = false;
while (!done) {
sharedFpsLimiter.mark();
Expand Down Expand Up @@ -488,6 +491,8 @@ void automapShow(bool isInGame, bool isUsingScanner)
isoEnable();
}

loopClearFlag(LoopFlag::AUTOMAP);

windowDestroy(window);
fontSetCurrent(oldFont);
}
Expand Down
14 changes: 13 additions & 1 deletion src/character_editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "interface.h"
#include "item.h"
#include "kb.h"
#include "loop.h"
#include "map.h"
#include "memory.h"
#include "message.h"
Expand Down Expand Up @@ -787,8 +788,19 @@ struct CustomKarmaFolderDescription {
static std::vector<CustomKarmaFolderDescription> gCustomKarmaFolderDescriptions;
static std::vector<TownReputationEntry> gCustomTownReputationEntries;

int characterEditorShowInner(bool isCreationMode);

// Wrapper for editor_design_, setting LoopFlag::CHARSCREEN
// (see sfall: CharacterHook)
int characterEditorShow(bool isCreationMode) {
loopSetFlag(LoopFlag::CHARSCREEN);
int result = characterEditorShowInner(isCreationMode);
loopClearFlag(LoopFlag::CHARSCREEN);
return result;
}

// 0x431DF8
int characterEditorShow(bool isCreationMode)
int characterEditorShowInner(bool isCreationMode)
{
char* messageListItemText;
char line1[128];
Expand Down
21 changes: 18 additions & 3 deletions src/combat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "item.h"
#include "kb.h"
#include "loadsave.h"
#include "loop.h"
#include "map.h"
#include "memory.h"
#include "message.h"
Expand Down Expand Up @@ -102,7 +103,8 @@ static int _compare_faster(const void* a1, const void* a2);
static void _combat_sequence_init(Object* a1, Object* a2);
static void _combat_sequence();
static void combatAttemptEnd();
static int _combat_input();
static int combatInput();
static int combatInputInner();
static void _combat_set_move_all();
static int _combat_turn(Object* a1, bool a2);
static bool _combat_should_end();
Expand Down Expand Up @@ -3125,8 +3127,17 @@ void _combat_turn_run()
}
}

// Wrapper for combatInput, setting LoopFlag::COMBAT_PLAYER_TURN
// (see sfall: PlayerCombatHook)
static int combatInput() {
loopSetFlag(LoopFlag::COMBAT_PLAYER_TURN);
int result = combatInputInner();
loopClearFlag(LoopFlag::COMBAT_PLAYER_TURN);
return result;
}

// 0x4227F4
static int _combat_input()
static int combatInputInner()
{
while ((gCombatState & COMBAT_STATE_0x02) != 0) {
sharedFpsLimiter.mark();
Expand Down Expand Up @@ -3271,7 +3282,7 @@ static int _combat_turn(Object* a1, bool a2)
_combat_outline_on();
}

if (_combat_input() == -1) {
if (combatInput() == -1) {
gameUiDisable(1);
gameMouseSetCursor(MOUSE_CURSOR_WAIT_WATCH);
a1->data.critter.combat.damageLastTurn = 0;
Expand Down Expand Up @@ -3366,6 +3377,8 @@ static bool _combat_should_end()
// 0x422D2C
void _combat(STRUCT_664980* attack)
{
loopSetFlag(LoopFlag::COMBAT);

if (attack == NULL
|| (attack->attacker == NULL || attack->attacker->elevation == gElevation)
|| (attack->defender == NULL || attack->defender->elevation == gElevation)) {
Expand Down Expand Up @@ -3449,6 +3462,8 @@ void _combat(STRUCT_664980* attack)
_game_user_wants_to_quit = 0;
}
}

loopClearFlag(LoopFlag::COMBAT);
}

// 0x422EC4
Expand Down
5 changes: 5 additions & 0 deletions src/game.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include "item.h"
#include "kb.h"
#include "loadsave.h"
#include "loop.h"
#include "main.h"
#include "map.h"
#include "memory.h"
#include "mouse.h"
Expand Down Expand Up @@ -352,6 +354,7 @@ int gameInitWithOptions(const char* windowTitle, bool isMapper, int font, int a4
// 0x442B84
void gameReset()
{
mainSetIsGameLoaded(false);
tileDisable();
paletteReset();
randomReset();
Expand Down Expand Up @@ -819,7 +822,9 @@ int gameHandleKey(int eventCode, bool isInCombatMode)
break;
case KEY_F1:
soundPlayFile("ib1p1xx1");
loopSetFlag(LoopFlag::HELP);
showHelp();
loopClearFlag(LoopFlag::HELP);
break;
case KEY_F2:
gameSoundSetMasterVolume(gameSoundGetMasterVolume() - 2047);
Expand Down
9 changes: 9 additions & 0 deletions src/game_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "item.h"
#include "kb.h"
#include "lips.h"
#include "loop.h"
#include "memory.h"
#include "mouse.h"
#include "object.h"
Expand Down Expand Up @@ -920,6 +921,8 @@ int _gdialogInitFromScript(int headFid, int reaction)
backgroundSoundDelete();
}

loopSetFlag(LoopFlag::DIALOG);

_gdDialogWentOff = true;

return 0;
Expand Down Expand Up @@ -947,6 +950,8 @@ int _gdialogExitFromScript()
_tile_scroll_to(gGameDialogOldCenterTile, 2);
}

loopClearFlag(LoopFlag::DIALOG);

_gdDestroyHeadWindow();

// CE: Fix Barter button.
Expand Down Expand Up @@ -1437,6 +1442,8 @@ int gameDialogShowReview()
return -1;
}

loopSetFlag(LoopFlag::DIALOG_REVIEW);

// probably current top line or something like this, which is used to scroll
int v1 = 0;
gameDialogReviewWindowUpdate(win, v1);
Expand Down Expand Up @@ -1474,6 +1481,8 @@ int gameDialogShowReview()
sharedFpsLimiter.throttle();
}

loopClearFlag(LoopFlag::DIALOG_REVIEW);

if (gameDialogReviewWindowFree(&win) == -1) {
return -1;
}
Expand Down
22 changes: 13 additions & 9 deletions src/input.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "dinput.h"
#include "draw.h"
#include "kb.h"
#include "loop.h"
#include "main.h"
#include "memory.h"
#include "mouse.h"
#include "sfall_config.h"
Expand Down Expand Up @@ -634,7 +636,7 @@ unsigned int getTicks()
return SDL_GetTicks();
}

// Inspired by sfall SpeedPatch.cpp.
// sfall SpeedPatch.cpp
// Returns the potentially sped up (multiplied) tick count.
unsigned int getMultipliedTicks()
{
Expand All @@ -651,10 +653,15 @@ unsigned int getMultipliedTicks()
gLastTickCount = newTickCount;

// Multiply the tick count difference by the multiplier
/* TODO janiczek: Original condition was:
if (IsGameLoaded() && enabled && (!(mode = GetLoopFlags()) || mode == LoopFlag::COMBAT || mode == (LoopFlag::COMBAT | LoopFlag::PCOMBAT) || (mode & LoopFlag::WORLDMAP)) && !slideShow)
*/
if (gSpeedPatchEnabled) {
if (mainIsGameLoaded()
&& gSpeedPatchEnabled
&& !mainIsInEndgameSlideshow()
&& (loopIsInWorldMap()
|| loopIsInCombatEnemyTurn()
|| loopIsInCombatWaitingForPlayerAction()
|| loopIsOutsideCombatWaitingForPlayerAction()
)
) {
elapsed *= gSpeedMulti;
elapsed += gTickCountFraction;
gTickCountFraction = modff(gTickCountFraction, &gTickCountFraction);
Expand Down Expand Up @@ -706,10 +713,7 @@ void inputBlockForTocks(unsigned int ms)
// 0x4C93E0
unsigned int getTicksSince(unsigned int start)
{
// TODO janiczek: this one was supposed to be patched, but the game seems to work better without that.
// We can retry after implementing the big condition, it will likely fix
// all the issues, eg. with inventory animation spinning too fast etc.
unsigned int end = getTicks();
unsigned int end = getMultipliedTicks();

// NOTE: Uninline.
return getTicksBetween(end, start);
Expand Down
24 changes: 24 additions & 0 deletions src/inventory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "item.h"
#include "kb.h"
#include "light.h"
#include "loop.h"
#include "map.h"
#include "message.h"
#include "mouse.h"
Expand Down Expand Up @@ -585,6 +586,9 @@ void inventoryOpen()
reg_anim_clear(_inven_dude);
inventoryRenderSummary();
_display_inventory(_stack_offset[_curr_stack], -1, INVENTORY_WINDOW_TYPE_NORMAL);

loopSetFlag(LoopFlag::INVENTORY);

inventorySetCursor(INVENTORY_WINDOW_CURSOR_HAND);

for (;;) {
Expand Down Expand Up @@ -706,6 +710,8 @@ void inventoryOpen()
}
}

loopClearFlag(LoopFlag::INVENTORY);

_exit_inventory(isoWasEnabled);

// NOTE: Uninline.
Expand Down Expand Up @@ -2624,6 +2630,9 @@ void inventoryOpenUseItemOn(Object* a1)

bool isoWasEnabled = _setup_inventory(INVENTORY_WINDOW_TYPE_USE_ITEM_ON);
_display_inventory(_stack_offset[_curr_stack], -1, INVENTORY_WINDOW_TYPE_USE_ITEM_ON);

loopSetFlag(LoopFlag::USE_INTERFACE);

inventorySetCursor(INVENTORY_WINDOW_CURSOR_HAND);
for (;;) {
sharedFpsLimiter.mark();
Expand Down Expand Up @@ -2743,6 +2752,8 @@ void inventoryOpenUseItemOn(Object* a1)
sharedFpsLimiter.throttle();
}

loopClearFlag(LoopFlag::USE_INTERFACE);

_exit_inventory(isoWasEnabled);

// NOTE: Uninline.
Expand Down Expand Up @@ -4218,6 +4229,9 @@ int inventoryOpenLooting(Object* a1, Object* a2)
_display_target_inventory(_target_stack_offset[_target_curr_stack], -1, _target_pud, INVENTORY_WINDOW_TYPE_LOOT);
_display_inventory(_stack_offset[_curr_stack], -1, INVENTORY_WINDOW_TYPE_LOOT);
_display_body(a2->fid, INVENTORY_WINDOW_TYPE_LOOT);

loopSetFlag(LoopFlag::LOOT_INTERFACE);

inventorySetCursor(INVENTORY_WINDOW_CURSOR_HAND);

bool isCaughtStealing = false;
Expand Down Expand Up @@ -4466,6 +4480,8 @@ int inventoryOpenLooting(Object* a1, Object* a2)
}
}

loopClearFlag(LoopFlag::LOOT_INTERFACE);

_exit_inventory(isoWasEnabled);

// NOTE: Uninline.
Expand Down Expand Up @@ -5024,6 +5040,8 @@ void inventoryOpenTrade(int win, Object* a2, Object* a3, Object* a4, int a5)
return;
}

loopSetFlag(LoopFlag::BARTER);

Object* armor = critterGetArmor(a2);
if (armor != NULL) {
itemRemove(a2, armor, 1);
Expand Down Expand Up @@ -5338,6 +5356,8 @@ void inventoryOpenTrade(int win, Object* a2, Object* a3, Object* a4, int a5)
itemAdd(a2, item1, 1);
}

loopClearFlag(LoopFlag::BARTER);

_exit_inventory(isoWasEnabled);

// NOTE: Uninline.
Expand Down Expand Up @@ -5725,6 +5745,8 @@ static int inventoryQuantitySelect(int inventoryWindowType, Object* item, int ma
// 0x476AB8
static int inventoryQuantityWindowInit(int inventoryWindowType, Object* item)
{
loopSetFlag(LoopFlag::COUNTER_WINDOW);

const int oldFont = fontGetCurrent();
fontSetCurrent(103);

Expand Down Expand Up @@ -5945,6 +5967,8 @@ static int inventoryQuantityWindowFree(int inventoryWindowType)
_moveFrmImages[index].unlock();
}

loopClearFlag(LoopFlag::COUNTER_WINDOW);

windowDestroy(_mt_wid);

return 0;
Expand Down
Loading

0 comments on commit 220c30a

Please sign in to comment.