Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sfall: SpeedPatch, LoadGameHook (LoopFlag) #174

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
88 changes: 84 additions & 4 deletions src/input.cc
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#include "input.h"

#include <SDL.h>
#include <time.h>

#include "audio_engine.h"
#include "color.h"
#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"
#include "svga.h"
#include "text_font.h"
#include "vcr.h"
Expand Down Expand Up @@ -116,6 +120,28 @@ static TickerListNode* gTickerListHead;
// 0x6AC788
static unsigned int gTickerLastTimestamp;

static bool gSpeedPatchEnabled;
static int gSpeedMultiInitial;
static float gSpeedMulti;
static unsigned int gLastTickCount;
static unsigned int gStoredTickCount;
static float gTickCountFraction;
static time_t gStartTime;
static bool gDefaultDelay;

static void SetKeyboardDefaultDelay() {
if (gDefaultDelay) return;
gDefaultDelay = true;
gKeyboardKeyRepeatRate = INPUT_DEFAULT_KEYBOARD_KEY_REPEAT_RATE;
gKeyboardKeyRepeatDelay = INPUT_DEFAULT_KEYBOARD_KEY_REPEAT_DELAY;
}

static void SetKeyboardDelay() {
gKeyboardKeyRepeatRate = static_cast<int>(INPUT_DEFAULT_KEYBOARD_KEY_REPEAT_RATE * gSpeedMulti);
gKeyboardKeyRepeatDelay = static_cast<int>(INPUT_DEFAULT_KEYBOARD_KEY_REPEAT_DELAY * gSpeedMulti);
gDefaultDelay = false;
}

// 0x4C8A70
int inputInit(int a1)
{
Expand Down Expand Up @@ -155,6 +181,18 @@ int inputInit(int a1)
// CE: Prevents frying CPU when window is not focused.
inputSetIdleFunc(idleImpl);

// SFALL: Speed patch
gSpeedPatchEnabled = false;
configGetBool(&gSfallConfig, SFALL_CONFIG_SPEED_KEY, SFALL_CONFIG_SPEED_ENABLE_KEY, &gSpeedPatchEnabled);
gSpeedMultiInitial = 100;
configGetInt(&gSfallConfig, SFALL_CONFIG_SPEED_KEY, SFALL_CONFIG_SPEED_MULTI_INITIAL_KEY, &gSpeedMultiInitial);
gSpeedMulti = gSpeedMultiInitial / 100.0f;
gLastTickCount = SDL_GetTicks();
gStoredTickCount = 0;
gTickCountFraction = 0.0f;
gStartTime = time(NULL);
gDefaultDelay = true;

return 0;
}

Expand Down Expand Up @@ -304,7 +342,7 @@ void tickersExecute()
return;
}

gTickerLastTimestamp = SDL_GetTicks();
gTickerLastTimestamp = getTicks();

TickerListNode* curr = gTickerListHead;
TickerListNode** currPtr = &(gTickerListHead);
Expand Down Expand Up @@ -606,9 +644,51 @@ void screenshotHandlerConfigure(int keyCode, ScreenshotHandler* handler)
}

// 0x4C9370
// Since implementing sfall SpeedPatch, this returns a potentially sped up
// (multiplied) tick count. For the original tick count, call SDL_GetTicks();
unsigned int getTicks()
{
return SDL_GetTicks();
unsigned int newTickCount = SDL_GetTicks();
if (newTickCount == gLastTickCount) return gStoredTickCount;

// Just in case someone's been running their computer for 49 days straight
if (gLastTickCount > newTickCount) {
gLastTickCount = newTickCount;
return gStoredTickCount;
}

float elapsed = static_cast<float>(newTickCount - gLastTickCount);
gLastTickCount = newTickCount;

// Multiply the tick count difference by the multiplier
if (mainIsGameLoaded()
&& gSpeedPatchEnabled
&& !mainIsInEndgameSlideshow()
&& (loopIsInWorldMap()
|| loopIsInCombatEnemyTurn()
|| loopIsInCombatWaitingForPlayerAction()
|| loopIsOutsideCombatWaitingForPlayerAction()
)
) {
elapsed *= gSpeedMulti;
elapsed += gTickCountFraction;
gTickCountFraction = modff(gTickCountFraction, &gTickCountFraction);
if (gDefaultDelay) SetKeyboardDelay();
} else {
SetKeyboardDefaultDelay();
}

gStoredTickCount += static_cast<unsigned int>(elapsed);

return gStoredTickCount;
}

// Done by sfall (presumably) because this time gets converted to game time.
// If you've played the game at 8x speed for a while, you'll want the time in
// the savefile to agree with the time in the game at the time you saved it.
time_t getLocalTimeAfterSpeedup()
{
return gStartTime + gStoredTickCount / 1000;
}

// 0x4C937C
Expand All @@ -633,7 +713,7 @@ void inputPauseForTocks(unsigned int delay)
// 0x4C93B8
void inputBlockForTocks(unsigned int ms)
{
unsigned int start = SDL_GetTicks();
unsigned int start = getTicks();
unsigned int diff;
do {
// NOTE: Uninline
Expand All @@ -644,7 +724,7 @@ void inputBlockForTocks(unsigned int ms)
// 0x4C93E0
unsigned int getTicksSince(unsigned int start)
{
unsigned int end = SDL_GetTicks();
unsigned int end = getTicks();

// NOTE: Uninline.
return getTicksBetween(end, start);
Expand Down
6 changes: 6 additions & 0 deletions src/input.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#ifndef FALLOUT_INPUT_H_
#define FALLOUT_INPUT_H_

#include <time.h>

namespace fallout {

#define INPUT_DEFAULT_KEYBOARD_KEY_REPEAT_RATE 80
#define INPUT_DEFAULT_KEYBOARD_KEY_REPEAT_DELAY 500

typedef void(IdleFunc)();
typedef void(FocusFunc)(bool focus);
typedef void(TickerProc)();
Expand All @@ -26,6 +31,7 @@ void takeScreenshot();
int screenshotHandlerDefaultImpl(int width, int height, unsigned char* data, unsigned char* palette);
void screenshotHandlerConfigure(int keyCode, ScreenshotHandler* handler);
unsigned int getTicks();
time_t getLocalTimeAfterSpeedup();
void inputPauseForTocks(unsigned int ms);
void inputBlockForTocks(unsigned int ms);
unsigned int getTicksSince(unsigned int a1);
Expand Down
Loading