Add global scripts (#294)

This commit is contained in:
Alexander Batalov 2023-05-31 21:48:44 +03:00 committed by GitHub
parent ec722475b6
commit 89839be3af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 323 additions and 9 deletions

View File

@ -267,6 +267,8 @@ target_sources(${EXECUTABLE_NAME} PUBLIC
"src/sfall_config.h" "src/sfall_config.h"
"src/sfall_global_vars.cc" "src/sfall_global_vars.cc"
"src/sfall_global_vars.h" "src/sfall_global_vars.h"
"src/sfall_global_scripts.cc"
"src/sfall_global_scripts.h"
"src/sfall_ini.cc" "src/sfall_ini.cc"
"src/sfall_ini.h" "src/sfall_ini.h"
"src/sfall_lists.cc" "src/sfall_lists.cc"

View File

@ -37,6 +37,7 @@
#include "scripts.h" #include "scripts.h"
#include "settings.h" #include "settings.h"
#include "sfall_config.h" #include "sfall_config.h"
#include "sfall_global_scripts.h"
#include "skill.h" #include "skill.h"
#include "stat.h" #include "stat.h"
#include "svga.h" #include "svga.h"
@ -3144,6 +3145,10 @@ static int _combat_input()
} }
int keyCode = inputGetInput(); int keyCode = inputGetInput();
// SFALL: CombatLoopHook.
sfall_gl_scr_process_main();
if (_action_explode_running()) { if (_action_explode_running()) {
// NOTE: Uninline. // NOTE: Uninline.
_combat_turn_run(); _combat_turn_run();

View File

@ -397,7 +397,7 @@ static bool configParseLine(Config* config, char* string)
// keys there. // keys there.
// Skip leading whitespace. // Skip leading whitespace.
while (isspace(*string)) { while (isspace(static_cast<unsigned char>(*string))) {
string++; string++;
} }
@ -500,7 +500,7 @@ static bool configTrimString(char* string)
// Starting from the end of the string, loop while it's a whitespace and // Starting from the end of the string, loop while it's a whitespace and
// decrement string length. // decrement string length.
char* pch = string + length - 1; char* pch = string + length - 1;
while (length != 0 && isspace(*pch)) { while (length != 0 && isspace(static_cast<unsigned char>(*pch))) {
length--; length--;
pch--; pch--;
} }
@ -511,7 +511,7 @@ static bool configTrimString(char* string)
// Starting from the beginning of the string loop while it's a whitespace // Starting from the beginning of the string loop while it's a whitespace
// and decrement string length. // and decrement string length.
pch = string; pch = string;
while (isspace(*pch)) { while (isspace(static_cast<unsigned char>(*pch))) {
pch++; pch++;
length--; length--;
} }

View File

@ -52,6 +52,7 @@
#include "settings.h" #include "settings.h"
#include "sfall_arrays.h" #include "sfall_arrays.h"
#include "sfall_config.h" #include "sfall_config.h"
#include "sfall_global_scripts.h"
#include "sfall_global_vars.h" #include "sfall_global_vars.h"
#include "sfall_ini.h" #include "sfall_ini.h"
#include "sfall_lists.h" #include "sfall_lists.h"
@ -355,6 +356,11 @@ int gameInitWithOptions(const char* windowTitle, bool isMapper, int font, int a4
return -1; return -1;
} }
if (!sfall_gl_scr_init()) {
debugPrint("Failed on sfall_gl_scr_init");
return -1;
}
char* customConfigBasePath; char* customConfigBasePath;
configGetString(&gSfallConfig, SFALL_CONFIG_SCRIPTS_KEY, SFALL_CONFIG_INI_CONFIG_FOLDER, &customConfigBasePath); configGetString(&gSfallConfig, SFALL_CONFIG_SCRIPTS_KEY, SFALL_CONFIG_INI_CONFIG_FOLDER, &customConfigBasePath);
sfall_ini_set_base_path(customConfigBasePath); sfall_ini_set_base_path(customConfigBasePath);
@ -407,6 +413,7 @@ void gameReset()
sfallListsReset(); sfallListsReset();
messageListRepositoryReset(); messageListRepositoryReset();
sfallArraysReset(); sfallArraysReset();
sfall_gl_scr_reset();
} }
// 0x442C34 // 0x442C34
@ -415,6 +422,7 @@ void gameExit()
debugPrint("\nGame Exit\n"); debugPrint("\nGame Exit\n");
// SFALL // SFALL
sfall_gl_scr_exit();
sfallArraysExit(); sfallArraysExit();
sfallListsExit(); sfallListsExit();
sfallGlobalVarsExit(); sfallGlobalVarsExit();

View File

@ -43,7 +43,6 @@ static opcode_t programReturnStackPopInt16(Program* program);
static int programReturnStackPopInt32(Program* program); static int programReturnStackPopInt32(Program* program);
static void _detachProgram(Program* program); static void _detachProgram(Program* program);
static void _purgeProgram(Program* program); static void _purgeProgram(Program* program);
static void programFree(Program* program);
static opcode_t _getOp(Program* program); static opcode_t _getOp(Program* program);
static void programMarkHeap(Program* program); static void programMarkHeap(Program* program);
static void opNoop(Program* program); static void opNoop(Program* program);
@ -421,7 +420,7 @@ static void _purgeProgram(Program* program)
} }
// 0x467614 // 0x467614
static void programFree(Program* program) void programFree(Program* program)
{ {
// NOTE: Uninline. // NOTE: Uninline.
_detachProgram(program); _detachProgram(program);

View File

@ -204,6 +204,7 @@ void _interpretOutputFunc(int (*func)(char*));
int _interpretOutput(const char* format, ...); int _interpretOutput(const char* format, ...);
[[noreturn]] void programFatalError(const char* str, ...); [[noreturn]] void programFatalError(const char* str, ...);
void _interpretDecStringRef(Program* program, opcode_t a2, int a3); void _interpretDecStringRef(Program* program, opcode_t a2, int a3);
void programFree(Program* program);
Program* programCreateByPath(const char* path); Program* programCreateByPath(const char* path);
char* programGetString(Program* program, opcode_t opcode, int offset); char* programGetString(Program* program, opcode_t opcode, int offset);
char* programGetIdentifier(Program* program, int offset); char* programGetIdentifier(Program* program, int offset);

View File

@ -45,6 +45,7 @@
#include "random.h" #include "random.h"
#include "scripts.h" #include "scripts.h"
#include "settings.h" #include "settings.h"
#include "sfall_global_scripts.h"
#include "skill.h" #include "skill.h"
#include "stat.h" #include "stat.h"
#include "svga.h" #include "svga.h"
@ -1676,6 +1677,9 @@ static int lsgLoadGameInSlot(int slot)
_loadingGame = 0; _loadingGame = 0;
// SFALL: Start global scripts.
sfall_gl_scr_exec_start_proc();
return 0; return 0;
} }

View File

@ -33,6 +33,7 @@
#include "selfrun.h" #include "selfrun.h"
#include "settings.h" #include "settings.h"
#include "sfall_config.h" #include "sfall_config.h"
#include "sfall_global_scripts.h"
#include "svga.h" #include "svga.h"
#include "text_font.h" #include "text_font.h"
#include "window.h" #include "window.h"
@ -148,6 +149,9 @@ int falloutMain(int argc, char** argv)
_main_load_new(mapNameCopy); _main_load_new(mapNameCopy);
free(mapNameCopy); free(mapNameCopy);
// SFALL: AfterNewGameStartHook.
sfall_gl_scr_exec_start_proc();
mainLoop(); mainLoop();
paletteFadeTo(gPaletteWhite); paletteFadeTo(gPaletteWhite);
@ -357,6 +361,10 @@ static void mainLoop()
sharedFpsLimiter.mark(); sharedFpsLimiter.mark();
int keyCode = inputGetInput(); int keyCode = inputGetInput();
// SFALL: MainLoopHook.
sfall_gl_scr_process_main();
gameHandleKey(keyCode, false); gameHandleKey(keyCode, false);
scriptsHandleRequests(); scriptsHandleRequests();

View File

@ -31,6 +31,7 @@
#include "queue.h" #include "queue.h"
#include "sfall_arrays.h" #include "sfall_arrays.h"
#include "sfall_config.h" #include "sfall_config.h"
#include "sfall_global_scripts.h"
#include "stat.h" #include "stat.h"
#include "svga.h" #include "svga.h"
#include "tile.h" #include "tile.h"
@ -145,7 +146,7 @@ static const int gGameTimeDaysPerMonth[12] = {
}; };
// 0x51C758 // 0x51C758
static const char* gScriptProcNames[28] = { const char* gScriptProcNames[SCRIPT_PROC_COUNT] = {
"no_p_proc", "no_p_proc",
"start", "start",
"spatial_p_proc", "spatial_p_proc",
@ -2589,6 +2590,9 @@ void scriptsExecMapUpdateProc()
// 0x4A67EC // 0x4A67EC
void scriptsExecMapUpdateScripts(int proc) void scriptsExecMapUpdateScripts(int proc)
{ {
// SFALL: Run global scripts.
sfall_gl_scr_exec_map_update_scripts(proc);
_scr_SpatialsEnabled = false; _scr_SpatialsEnabled = false;
int fixedParam = 0; int fixedParam = 0;

View File

@ -145,6 +145,8 @@ typedef struct Script {
int field_DC; int field_DC;
} Script; } Script;
extern const char* gScriptProcNames[SCRIPT_PROC_COUNT];
int gameTimeGetTime(); int gameTimeGetTime();
void gameTimeGetDate(int* monthPtr, int* dayPtr, int* yearPtr); void gameTimeGetDate(int* monthPtr, int* dayPtr, int* yearPtr);
int gameTimeGetHour(); int gameTimeGetHour();

View File

@ -59,6 +59,7 @@ bool sfallConfigInit(int argc, char** argv)
configSetInt(&gSfallConfig, SFALL_CONFIG_MISC_KEY, SFALL_CONFIG_MOVIE_TIMER_ARTIMER4, 360); configSetInt(&gSfallConfig, SFALL_CONFIG_MISC_KEY, SFALL_CONFIG_MOVIE_TIMER_ARTIMER4, 360);
configSetString(&gSfallConfig, SFALL_CONFIG_SCRIPTS_KEY, SFALL_CONFIG_INI_CONFIG_FOLDER, ""); configSetString(&gSfallConfig, SFALL_CONFIG_SCRIPTS_KEY, SFALL_CONFIG_INI_CONFIG_FOLDER, "");
configSetString(&gSfallConfig, SFALL_CONFIG_SCRIPTS_KEY, SFALL_CONFIG_GLOBAL_SCRIPT_PATHS, "");
char path[COMPAT_MAX_PATH]; char path[COMPAT_MAX_PATH];
char* executable = argv[0]; char* executable = argv[0];

View File

@ -70,6 +70,7 @@ namespace fallout {
#define SFALL_CONFIG_EXTRA_MESSAGE_LISTS_KEY "ExtraGameMsgFileList" #define SFALL_CONFIG_EXTRA_MESSAGE_LISTS_KEY "ExtraGameMsgFileList"
#define SFALL_CONFIG_NUMBERS_IS_DIALOG_KEY "NumbersInDialogue" #define SFALL_CONFIG_NUMBERS_IS_DIALOG_KEY "NumbersInDialogue"
#define SFALL_CONFIG_INI_CONFIG_FOLDER "IniConfigFolder" #define SFALL_CONFIG_INI_CONFIG_FOLDER "IniConfigFolder"
#define SFALL_CONFIG_GLOBAL_SCRIPT_PATHS "GlobalScriptPaths"
#define SFALL_CONFIG_BURST_MOD_DEFAULT_CENTER_MULTIPLIER 1 #define SFALL_CONFIG_BURST_MOD_DEFAULT_CENTER_MULTIPLIER 1
#define SFALL_CONFIG_BURST_MOD_DEFAULT_CENTER_DIVISOR 3 #define SFALL_CONFIG_BURST_MOD_DEFAULT_CENTER_DIVISOR 3

213
src/sfall_global_scripts.cc Normal file
View File

@ -0,0 +1,213 @@
#include "sfall_global_scripts.h"
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include "db.h"
#include "input.h"
#include "platform_compat.h"
#include "scripts.h"
#include "sfall_config.h"
namespace fallout {
struct GlobalScript {
Program* program = nullptr;
int procs[SCRIPT_PROC_COUNT] = { 0 };
int repeat = 0;
int count = 0;
int mode = 0;
bool once = true;
};
struct GlobalScriptsState {
std::vector<std::string> paths;
std::vector<GlobalScript> globalScripts;
};
static GlobalScriptsState* state = nullptr;
bool sfall_gl_scr_init()
{
state = new (std::nothrow) GlobalScriptsState();
if (state == nullptr) {
return false;
}
char* paths;
configGetString(&gSfallConfig, SFALL_CONFIG_SCRIPTS_KEY, SFALL_CONFIG_GLOBAL_SCRIPT_PATHS, &paths);
char* curr = paths;
while (curr != nullptr && *curr != '\0') {
char* end = strchr(curr, ',');
if (end != nullptr) {
*end = '\0';
}
char drive[COMPAT_MAX_DRIVE];
char dir[COMPAT_MAX_DIR];
compat_splitpath(curr, drive, dir, nullptr, nullptr);
char** files;
int filesLength = fileNameListInit(curr, &files, 0, 0);
if (filesLength != 0) {
for (int index = 0; index < filesLength; index++) {
char path[COMPAT_MAX_PATH];
compat_makepath(path, drive, dir, files[index], nullptr);
state->paths.push_back(std::string { path });
}
fileNameListFree(&files, 0);
}
if (end != nullptr) {
*end = ',';
curr = end + 1;
} else {
curr = nullptr;
}
}
std::sort(state->paths.begin(), state->paths.end());
return true;
}
void sfall_gl_scr_reset()
{
if (state != nullptr) {
sfall_gl_scr_remove_all();
}
}
void sfall_gl_scr_exit()
{
if (state != nullptr) {
sfall_gl_scr_remove_all();
delete state;
state = nullptr;
}
}
void sfall_gl_scr_exec_start_proc()
{
for (auto& path : state->paths) {
Program* program = programCreateByPath(path.c_str());
if (program != nullptr) {
GlobalScript scr;
scr.program = program;
for (int action = 0; action < SCRIPT_PROC_COUNT; action++) {
scr.procs[action] = programFindProcedure(program, gScriptProcNames[action]);
}
state->globalScripts.push_back(std::move(scr));
_interpret(program, -1);
}
}
tickersAdd(sfall_gl_scr_process_input);
}
void sfall_gl_scr_remove_all()
{
tickersRemove(sfall_gl_scr_process_input);
for (auto& scr : state->globalScripts) {
programFree(scr.program);
}
state->globalScripts.clear();
}
void sfall_gl_scr_exec_map_update_scripts(int action)
{
for (auto& scr : state->globalScripts) {
if (scr.mode == 0 || scr.mode == 3) {
if (scr.procs[action] != -1) {
_executeProcedure(scr.program, scr.procs[action]);
}
}
}
}
static void sfall_gl_scr_process_simple(int mode1, int mode2)
{
for (auto& scr : state->globalScripts) {
if (scr.repeat != 0 && (scr.mode == mode1 || scr.mode == mode2)) {
scr.count++;
if (scr.count >= scr.repeat) {
_executeProcedure(scr.program, scr.procs[SCRIPT_PROC_START]);
scr.count = 0;
}
}
}
}
void sfall_gl_scr_process_main()
{
sfall_gl_scr_process_simple(0, 3);
}
void sfall_gl_scr_process_input()
{
sfall_gl_scr_process_simple(1, 1);
}
void sfall_gl_scr_process_worldmap()
{
sfall_gl_scr_process_simple(2, 3);
}
static GlobalScript* sfall_gl_scr_map_program_to_scr(Program* program)
{
auto it = std::find_if(state->globalScripts.begin(),
state->globalScripts.end(),
[&program](const GlobalScript& scr) {
return scr.program == program;
});
return it != state->globalScripts.end() ? &(*it) : nullptr;
}
void sfall_gl_scr_set_repeat(Program* program, int frames)
{
GlobalScript* scr = sfall_gl_scr_map_program_to_scr(program);
if (scr != nullptr) {
scr->repeat = frames;
}
}
void sfall_gl_scr_set_type(Program* program, int type)
{
if (type < 0 || type > 3) {
return;
}
GlobalScript* scr = sfall_gl_scr_map_program_to_scr(program);
if (scr != nullptr) {
scr->mode = type;
}
}
bool sfall_gl_scr_is_loaded(Program* program)
{
GlobalScript* scr = sfall_gl_scr_map_program_to_scr(program);
if (scr != nullptr) {
if (scr->once) {
scr->once = false;
return true;
}
return false;
}
// Not a global script.
return true;
}
} // namespace fallout

View File

@ -0,0 +1,23 @@
#ifndef FALLOUT_SFALL_GLOBAL_SCRIPTS_H_
#define FALLOUT_SFALL_GLOBAL_SCRIPTS_H_
#include "interpreter.h"
namespace fallout {
bool sfall_gl_scr_init();
void sfall_gl_scr_reset();
void sfall_gl_scr_exit();
void sfall_gl_scr_exec_start_proc();
void sfall_gl_scr_remove_all();
void sfall_gl_scr_exec_map_update_scripts(int action);
void sfall_gl_scr_process_main();
void sfall_gl_scr_process_input();
void sfall_gl_scr_process_worldmap();
void sfall_gl_scr_set_repeat(Program* program, int frames);
void sfall_gl_scr_set_type(Program* program, int type);
bool sfall_gl_scr_is_loaded(Program* program);
} // namespace fallout
#endif /* FALLOUT_SFALL_GLOBAL_SCRIPTS_H_ */

View File

@ -128,19 +128,21 @@ bool sfall_ini_get_string(const char* triplet, char* value, size_t size)
loaded = configRead(&config, path, false); loaded = configRead(&config, path, false);
} }
bool ok = false; // NOTE: Sfall's `GetIniSetting` returns error code (-1) only when it cannot
// parse triplet. Otherwise the default for string settings is empty string.
value[0] = '\0';
if (loaded) { if (loaded) {
char* stringValue; char* stringValue;
if (configGetString(&config, section, key, &stringValue)) { if (configGetString(&config, section, key, &stringValue)) {
strncpy(value, stringValue, size - 1); strncpy(value, stringValue, size - 1);
value[size - 1] = '\0'; value[size - 1] = '\0';
ok = true;
} }
} }
configFree(&config); configFree(&config);
return ok; return true;
} }
} // namespace fallout } // namespace fallout

View File

@ -21,6 +21,7 @@
#include "proto.h" #include "proto.h"
#include "scripts.h" #include "scripts.h"
#include "sfall_arrays.h" #include "sfall_arrays.h"
#include "sfall_global_scripts.h"
#include "sfall_global_vars.h" #include "sfall_global_vars.h"
#include "sfall_ini.h" #include "sfall_ini.h"
#include "sfall_lists.h" #include "sfall_lists.h"
@ -101,6 +102,30 @@ static void op_get_year(Program* program)
programStackPushInteger(program, year); programStackPushInteger(program, year);
} }
// game_loaded
static void op_game_loaded(Program* program)
{
bool loaded = sfall_gl_scr_is_loaded(program);
programStackPushInteger(program, loaded ? 1 : 0);
}
// set_global_script_repeat
static void op_set_global_script_repeat(Program* program)
{
int frames = programStackPopInteger(program);
sfall_gl_scr_set_repeat(program, frames);
}
// key_pressed
static void op_key_pressed(Program* program)
{
int key = programStackPopInteger(program);
// TODO: Incomplete.
programStackPushInteger(program, 0);
}
// in_world_map // in_world_map
static void op_in_world_map(Program* program) static void op_in_world_map(Program* program)
{ {
@ -121,6 +146,13 @@ static void opGetCurrentHand(Program* program)
programStackPushInteger(program, interfaceGetCurrentHand()); programStackPushInteger(program, interfaceGetCurrentHand());
} }
// set_global_script_type
static void op_set_global_script_type(Program* program)
{
int type = programStackPopInteger(program);
sfall_gl_scr_set_type(program, type);
}
// set_sfall_global // set_sfall_global
static void opSetGlobalVar(Program* program) static void opSetGlobalVar(Program* program)
{ {
@ -851,9 +883,13 @@ void sfallOpcodesInit()
interpreterRegisterOpcode(0x815C, op_get_pc_base_stat); interpreterRegisterOpcode(0x815C, op_get_pc_base_stat);
interpreterRegisterOpcode(0x815D, opGetPcBonusStat); interpreterRegisterOpcode(0x815D, opGetPcBonusStat);
interpreterRegisterOpcode(0x8163, op_get_year); interpreterRegisterOpcode(0x8163, op_get_year);
interpreterRegisterOpcode(0x8164, op_game_loaded);
interpreterRegisterOpcode(0x816A, op_set_global_script_repeat);
interpreterRegisterOpcode(0x816C, op_key_pressed);
interpreterRegisterOpcode(0x8170, op_in_world_map); interpreterRegisterOpcode(0x8170, op_in_world_map);
interpreterRegisterOpcode(0x8172, op_set_world_map_pos); interpreterRegisterOpcode(0x8172, op_set_world_map_pos);
interpreterRegisterOpcode(0x8193, opGetCurrentHand); interpreterRegisterOpcode(0x8193, opGetCurrentHand);
interpreterRegisterOpcode(0x819B, op_set_global_script_type);
interpreterRegisterOpcode(0x819D, opSetGlobalVar); interpreterRegisterOpcode(0x819D, opSetGlobalVar);
interpreterRegisterOpcode(0x819E, opGetGlobalInt); interpreterRegisterOpcode(0x819E, opGetGlobalInt);
interpreterRegisterOpcode(0x81AC, op_get_ini_setting); interpreterRegisterOpcode(0x81AC, op_get_ini_setting);

View File

@ -41,6 +41,7 @@
#include "scripts.h" #include "scripts.h"
#include "settings.h" #include "settings.h"
#include "sfall_config.h" #include "sfall_config.h"
#include "sfall_global_scripts.h"
#include "skill.h" #include "skill.h"
#include "stat.h" #include "stat.h"
#include "string_parsers.h" #include "string_parsers.h"
@ -2982,6 +2983,10 @@ static int wmWorldMapFunc(int a1)
sharedFpsLimiter.mark(); sharedFpsLimiter.mark();
int keyCode = inputGetInput(); int keyCode = inputGetInput();
// SFALL: WorldmapLoopHook.
sfall_gl_scr_process_worldmap();
unsigned int now = getTicks(); unsigned int now = getTicks();
int mouseX; int mouseX;