diff options
-rw-r--r-- | saga/saga.cpp | 8 | ||||
-rw-r--r-- | saga/saga.h | 2 | ||||
-rw-r--r-- | saga/scene.cpp | 5 | ||||
-rw-r--r-- | saga/script.cpp | 179 | ||||
-rw-r--r-- | saga/script.h | 55 | ||||
-rw-r--r-- | saga/script_mod.h | 5 | ||||
-rw-r--r-- | saga/sdata.cpp | 26 | ||||
-rw-r--r-- | saga/sdebug.cpp | 14 | ||||
-rw-r--r-- | saga/sthread.cpp | 38 |
9 files changed, 171 insertions, 161 deletions
diff --git a/saga/saga.cpp b/saga/saga.cpp index 2c9d60c90d..b6c75cfd95 100644 --- a/saga/saga.cpp +++ b/saga/saga.cpp @@ -47,6 +47,7 @@ #include "game.h" #include "interface_mod.h" #include "isomap_mod.h" +#include "script.h" #include "script_mod.h" #include "scene_mod.h" #include "sdata.h" @@ -117,9 +118,7 @@ void SagaEngine::go() { CON_Register(); // Register console cvars first GAME_Register(); - OBJECTMAP_Register(); - SCRIPT_Register(); ACTOR_Register(); SCENE_Register(); @@ -159,7 +158,7 @@ void SagaEngine::go() { _actionMap = new ActionMap(this); OBJECTMAP_Init(); ISOMAP_Init(); - SCRIPT_Init(); + _script = new Script(); _sdata = new SData(); INTERFACE_Init(); // requires script module ACTOR_Init(); @@ -208,6 +207,7 @@ void SagaEngine::go() { debug(0, "Sound disabled."); } + _script->reg(); _render->reg(); _anim->reg(); _actionMap->reg(); @@ -240,7 +240,7 @@ void SagaEngine::go() { void SagaEngine::shutdown() { SCENE_Shutdown(); ACTOR_Shutdown(); - SCRIPT_Shutdown(); + delete _script; SPRITE_Shutdown(); OBJECTMAP_Shutdown(); FONT_Shutdown(); diff --git a/saga/saga.h b/saga/saga.h index 71315360a8..d57ea91425 100644 --- a/saga/saga.h +++ b/saga/saga.h @@ -45,6 +45,7 @@ class Render; class ActionMap; class Gfx; class SData; +class Script; using Common::MemoryReadStream; @@ -95,6 +96,7 @@ public: ActionMap *_actionMap; Gfx *_gfx; SData *_sdata; + Script *_script; private: int decodeBGImageRLE(const byte *inbuf, size_t inbuf_len, byte *outbuf, size_t outbuf_len); diff --git a/saga/scene.cpp b/saga/scene.cpp index 55a40a0028..f36d5cf5a8 100644 --- a/saga/scene.cpp +++ b/saga/scene.cpp @@ -38,6 +38,7 @@ #include "palanim_mod.h" #include "render.h" #include "rscfile_mod.h" +#include "script.h" #include "text_mod.h" #include "sound.h" #include "music.h" @@ -469,7 +470,7 @@ int SCENE_Load(int scene_num, int load_flag, R_SCENE_PROC scene_proc, R_SCENE_DE // Load scene script data if (SceneModule.desc.script_num > 0) { - if (SCRIPT_Load(SceneModule.desc.script_num) != R_SUCCESS) { + if (_vm->_script->loadScript(SceneModule.desc.script_num) != R_SUCCESS) { warning("Error loading scene script"); return R_FAILURE; } @@ -763,7 +764,7 @@ int SCENE_End() { SceneModule.scene_proc(SCENE_END, &scene_info); if (SceneModule.desc.script_num > 0) { - SCRIPT_Free(); + _vm->_script->freeScript(); } // Free scene background diff --git a/saga/script.cpp b/saga/script.cpp index 23fee26124..de692d0485 100644 --- a/saga/script.cpp +++ b/saga/script.cpp @@ -39,19 +39,21 @@ namespace Saga { -R_SCRIPT_MODULE ScriptModule; +static void CF_script_info(int argc, char *argv[], void *refCon); +static void CF_script_exec(int argc, char *argv[], void *refCon); +static void CF_script_togglestep(int argc, char *argv[], void *refCon); -int SCRIPT_Register() { - CVAR_RegisterFunc(CF_script_info, "script_info", NULL, R_CVAR_NONE, 0, 0, NULL); - CVAR_RegisterFunc(CF_script_exec, "script_exec", "<Script number>", R_CVAR_NONE, 1, 1, NULL); - CVAR_RegisterFunc(CF_script_togglestep, "script_togglestep", NULL, R_CVAR_NONE, 0, 0, NULL); +int Script::reg() { + CVAR_RegisterFunc(CF_script_info, "script_info", NULL, R_CVAR_NONE, 0, 0, this); + CVAR_RegisterFunc(CF_script_exec, "script_exec", "<Script number>", R_CVAR_NONE, 1, 1, this); + CVAR_RegisterFunc(CF_script_togglestep, "script_togglestep", NULL, R_CVAR_NONE, 0, 0, this); return R_SUCCESS; } // Initializes the scripting module. // Loads script resource look-up table, initializes script data system -int SCRIPT_Init() { +Script::Script() { R_RSCFILE_CONTEXT *s_lut_ctxt; byte *rsc_ptr; size_t rsc_len; @@ -59,105 +61,108 @@ int SCRIPT_Init() { int result; int i, j; - debug(0, "Initializing scripting subsystem."); + //initialize member variables + _initialized = 0; + _script_ctxt = 0; + _voice_lut_present = 0; + _script_lut = 0; + _script_lut_max = 0; + _script_lut_entrylen = 0; + _current_script = 0; + _thread_list = 0; + memset(_data_buf, 0, sizeof(_data_buf)); + + debug(0, "Initializing scripting subsystem"); // Load script resource file context - result = GAME_GetFileContext(&ScriptModule.script_ctxt, R_GAME_SCRIPTFILE, 0); + result = GAME_GetFileContext(&_script_ctxt, R_GAME_SCRIPTFILE, 0); if (result != R_SUCCESS) { - warning("Couldn't get script file context"); - return R_FAILURE; + error("Couldn't get script file context"); } // Load script LUT resource result = GAME_GetFileContext(&s_lut_ctxt, R_GAME_RESOURCEFILE, 0); if (result != R_SUCCESS) { - warning("Couldn't get resource file context"); - return R_FAILURE; + error("Couldn't get resource file context"); } result = RSC_LoadResource(s_lut_ctxt, ITE_SCRIPT_LUT, &rsc_ptr, &rsc_len); if (result != R_SUCCESS) { - warning("Error: Couldn't load script resource look-up table"); - return R_FAILURE; + error("Error: Couldn't load script resource look-up table"); } // Create logical script LUT from resource if (rsc_len % R_S_LUT_ENTRYLEN_ITECD == 0) { - ScriptModule.script_lut_entrylen = R_S_LUT_ENTRYLEN_ITECD; + _script_lut_entrylen = R_S_LUT_ENTRYLEN_ITECD; } else if (rsc_len % R_S_LUT_ENTRYLEN_ITEDISK == 0) { - ScriptModule.script_lut_entrylen = R_S_LUT_ENTRYLEN_ITEDISK; + _script_lut_entrylen = R_S_LUT_ENTRYLEN_ITEDISK; } else { - warning("Error: Invalid script lookup table length"); - return R_FAILURE; + error("Error: Invalid script lookup table length"); } // Calculate number of entries - ScriptModule.script_lut_max = rsc_len / ScriptModule.script_lut_entrylen; + _script_lut_max = rsc_len / _script_lut_entrylen; // Allocate space for logical LUT - ScriptModule.script_lut = (R_SCRIPT_LUT_ENTRY *)malloc(ScriptModule.script_lut_max * sizeof(R_SCRIPT_LUT_ENTRY)); - if (ScriptModule.script_lut == NULL) { - warning("Error: Couldn't allocate memory for script resource look-up table"); - return R_MEM; + _script_lut = (R_SCRIPT_LUT_ENTRY *)malloc(_script_lut_max * sizeof(R_SCRIPT_LUT_ENTRY)); + if (_script_lut == NULL) { + error("Error: Couldn't allocate memory for script resource look-up table"); } // Convert LUT resource to logical LUT MemoryReadStream *readS = new MemoryReadStream(rsc_ptr, rsc_len); - for (i = 0; i < ScriptModule.script_lut_max; i++) { + for (i = 0; i < _script_lut_max; i++) { prevTell = readS->pos(); - ScriptModule.script_lut[i].script_rn = readS->readUint16LE(); - ScriptModule.script_lut[i].diag_list_rn = readS->readUint16LE(); - ScriptModule.script_lut[i].voice_lut_rn = readS->readUint16LE(); + _script_lut[i].script_rn = readS->readUint16LE(); + _script_lut[i].diag_list_rn = readS->readUint16LE(); + _script_lut[i].voice_lut_rn = readS->readUint16LE(); // Skip the unused portion of the structure - for (j = readS->pos(); j < prevTell + ScriptModule.script_lut_entrylen; j++) + for (j = readS->pos(); j < prevTell + _script_lut_entrylen; j++) readS->readByte(); } RSC_FreeResource(rsc_ptr); // Any voice lookup table resources present? - for (i = 0; i < ScriptModule.script_lut_max; i++) { - if (ScriptModule.script_lut[i].voice_lut_rn) { - ScriptModule.voice_lut_present = 1; + for (i = 0; i < _script_lut_max; i++) { + if (_script_lut[i].voice_lut_rn) { + _voice_lut_present = 1; break; } } // Initialize script submodules - ScriptModule.thread_list = ys_dll_create(); + _thread_list = ys_dll_create(); - ScriptModule.initialized = 1; - return R_SUCCESS; + _initialized = 1; } // Shut down script module gracefully; free all allocated module resources -int SCRIPT_Shutdown() { +Script::~Script() { YS_DL_NODE *thread_node; R_SCRIPT_THREAD *thread; - if (!ScriptModule.initialized) { - return R_FAILURE; + if (!_initialized) { + error("Script not initialized"); } debug(0, "Shutting down scripting subsystem."); // Free script lookup table - free(ScriptModule.script_lut); + free(_script_lut); // Stop all threads and destroy them - for (thread_node = ys_dll_head(ScriptModule.thread_list); thread_node != NULL; + for (thread_node = ys_dll_head(_thread_list); thread_node != NULL; thread_node = ys_dll_next(thread_node)) { thread = (R_SCRIPT_THREAD *)ys_dll_get_data(thread_node); STHREAD_Destroy(thread); } - ScriptModule.initialized = 0; - - return R_SUCCESS; + _initialized = 0; } // Loads a script; including script bytecode and dialogue list -int SCRIPT_Load(int script_num) { +int Script::loadScript(int script_num) { R_SCRIPTDATA *script_data; byte *bytecode_p; size_t bytecode_len; @@ -175,13 +180,13 @@ int SCRIPT_Load(int script_num) { } // Validate script number - if ((script_num < 0) || (script_num > ScriptModule.script_lut_max)) { - warning("SCRIPT_Load(): Invalid script number"); + if ((script_num < 0) || (script_num > _script_lut_max)) { + warning("Script::loadScript(): Invalid script number"); return R_FAILURE; } // Release old script data if present - SCRIPT_Free(); + freeScript(); // Initialize script data structure debug(0, "Loading script data for script #%d", script_num); @@ -200,16 +205,16 @@ int SCRIPT_Load(int script_num) { script_data->voice = NULL; // Load script bytecode - scriptl_rn = ScriptModule.script_lut[script_num].script_rn; + scriptl_rn = _script_lut[script_num].script_rn; - result = RSC_LoadResource(ScriptModule.script_ctxt, scriptl_rn, &bytecode_p, &bytecode_len); + result = RSC_LoadResource(_script_ctxt, scriptl_rn, &bytecode_p, &bytecode_len); if (result != R_SUCCESS) { warning("Error loading script bytecode resource"); free(script_data); return R_FAILURE; } - script_data->bytecode = SCRIPT_LoadBytecode(bytecode_p, bytecode_len); + script_data->bytecode = loadBytecode(bytecode_p, bytecode_len); if (script_data->bytecode == NULL) { warning("Error interpreting script bytecode resource"); @@ -219,10 +224,10 @@ int SCRIPT_Load(int script_num) { } // Load script dialogue list - diagl_rn = ScriptModule.script_lut[script_num].diag_list_rn; + diagl_rn = _script_lut[script_num].diag_list_rn; // Load dialogue list resource - result = RSC_LoadResource(ScriptModule.script_ctxt, diagl_rn, &diagl_p, &diagl_len); + result = RSC_LoadResource(_script_ctxt, diagl_rn, &diagl_p, &diagl_len); if (result != R_SUCCESS) { warning("Error loading dialogue list resource"); free(script_data); @@ -231,7 +236,7 @@ int SCRIPT_Load(int script_num) { } // Convert dialogue list resource to logical dialogue list - script_data->diag = SCRIPT_LoadDialogue(diagl_p, diagl_len); + script_data->diag = loadDialogue(diagl_p, diagl_len); if (script_data->diag == NULL) { warning("Error interpreting dialogue list resource"); free(script_data); @@ -241,11 +246,11 @@ int SCRIPT_Load(int script_num) { } // Load voice resource lookup table - if (ScriptModule.voice_lut_present) { - voicelut_rn = ScriptModule.script_lut[script_num].voice_lut_rn; + if (_voice_lut_present) { + voicelut_rn = _script_lut[script_num].voice_lut_rn; // Load voice LUT resource - result = RSC_LoadResource(ScriptModule.script_ctxt, voicelut_rn, &voicelut_p, &voicelut_len); + result = RSC_LoadResource(_script_ctxt, voicelut_rn, &voicelut_p, &voicelut_len); if (result != R_SUCCESS) { warning("Error loading voice LUT resource"); free(script_data); @@ -255,7 +260,7 @@ int SCRIPT_Load(int script_num) { } // Convert voice LUT resource to logical voice LUT - script_data->voice = SCRIPT_LoadVoiceLUT(voicelut_p, voicelut_len, script_data); + script_data->voice = loadVoiceLUT(voicelut_p, voicelut_len, script_data); if (script_data->voice == NULL) { warning("Error interpreting voice LUT resource"); free(script_data); @@ -268,52 +273,52 @@ int SCRIPT_Load(int script_num) { // Finish initialization script_data->loaded = 1; - ScriptModule.current_script = script_data; + _current_script = script_data; return R_SUCCESS; } // Frees all resources associated with current script. -int SCRIPT_Free() { - if (ScriptModule.current_script == NULL) { +int Script::freeScript() { + if (_current_script == NULL) { return R_FAILURE; } - if (!ScriptModule.current_script->loaded) { + if (!_current_script->loaded) { return R_FAILURE; } debug(0, "Releasing script data."); // Finish initialization - if (ScriptModule.current_script->diag != NULL) { - free(ScriptModule.current_script->diag->str); - free(ScriptModule.current_script->diag->str_off); + if (_current_script->diag != NULL) { + free(_current_script->diag->str); + free(_current_script->diag->str_off); } - free(ScriptModule.current_script->diag); + free(_current_script->diag); - if (ScriptModule.current_script->bytecode != NULL) { - free(ScriptModule.current_script->bytecode->entrypoints); - RSC_FreeResource(ScriptModule.current_script->bytecode->bytecode_p); + if (_current_script->bytecode != NULL) { + free(_current_script->bytecode->entrypoints); + RSC_FreeResource(_current_script->bytecode->bytecode_p); } - free(ScriptModule.current_script->bytecode); + free(_current_script->bytecode); - if (ScriptModule.voice_lut_present) { - free(ScriptModule.current_script->voice->voices); - free(ScriptModule.current_script->voice); + if (_voice_lut_present) { + free(_current_script->voice->voices); + free(_current_script->voice); } - free(ScriptModule.current_script); + free(_current_script); - ScriptModule.current_script = NULL; + _current_script = NULL; return R_SUCCESS; } // Reads the entrypoint table from a script bytecode resource in memory. // Returns NULL on failure. -R_SCRIPT_BYTECODE *SCRIPT_LoadBytecode(byte *bytecode_p, size_t bytecode_len) { +R_SCRIPT_BYTECODE *Script::loadBytecode(byte *bytecode_p, size_t bytecode_len) { R_PROC_TBLENTRY *bc_ep_tbl = NULL; R_SCRIPT_BYTECODE *bc_new_data = NULL; @@ -390,7 +395,7 @@ R_SCRIPT_BYTECODE *SCRIPT_LoadBytecode(byte *bytecode_p, size_t bytecode_len) { // Reads a logical dialogue list from a dialogue list resource in memory. // Returns NULL on failure. -R_DIALOGUE_LIST *SCRIPT_LoadDialogue(const byte *dialogue_p, size_t dialogue_len) { +R_DIALOGUE_LIST *Script::loadDialogue(const byte *dialogue_p, size_t dialogue_len) { R_DIALOGUE_LIST *dialogue_list; uint16 n_dialogue; uint16 i; @@ -452,7 +457,7 @@ R_DIALOGUE_LIST *SCRIPT_LoadDialogue(const byte *dialogue_p, size_t dialogue_len // Reads a logical voice LUT from a voice LUT resource in memory. // Returns NULL on failure. -R_VOICE_LUT *SCRIPT_LoadVoiceLUT(const byte *voicelut_p, size_t voicelut_len, R_SCRIPTDATA *script) { +R_VOICE_LUT *Script::loadVoiceLUT(const byte *voicelut_p, size_t voicelut_len, R_SCRIPTDATA *script) { R_VOICE_LUT *voice_lut; uint16 n_voices; @@ -489,21 +494,21 @@ void CF_script_info(int argc, char *argv[], void *refCon) { uint32 i; char *name_ptr; - if (ScriptModule.current_script == NULL) { + if (((Script *)refCon)->_current_script == NULL) { return; } - if (!ScriptModule.current_script->loaded) { + if (!((Script *)refCon)->_current_script->loaded) { return; } - n_entrypoints = ScriptModule.current_script->bytecode->n_entrypoints; + n_entrypoints = ((Script *)refCon)->_current_script->bytecode->n_entrypoints; CON_Print("Current script contains %d entrypoints:", n_entrypoints); for (i = 0; i < n_entrypoints; i++) { - name_ptr = (char *)ScriptModule.current_script->bytecode->bytecode_p + - ScriptModule.current_script->bytecode->entrypoints[i].name_offset; + name_ptr = (char *)((Script *)refCon)->_current_script->bytecode->bytecode_p + + ((Script *)refCon)->_current_script->bytecode->entrypoints[i].name_offset; CON_Print("%lu: %s", i, name_ptr); } } @@ -517,25 +522,25 @@ void CF_script_exec(int argc, char *argv[], void *refCon) { ep_num = atoi(argv[0]); - if (ScriptModule.dbg_thread == NULL) { + if (((Script *)refCon)->_dbg_thread == NULL) { CON_Print("Creating debug thread..."); - ScriptModule.dbg_thread = STHREAD_Create(); - if (ScriptModule.dbg_thread == NULL) { + ((Script *)refCon)->_dbg_thread = STHREAD_Create(); + if (((Script *)refCon)->_dbg_thread == NULL) { CON_Print("Thread creation failed."); return; } } - if (ep_num >= ScriptModule.current_script->bytecode->n_entrypoints) { + if (ep_num >= ((Script *)refCon)->_current_script->bytecode->n_entrypoints) { CON_Print("Invalid entrypoint."); return; } - STHREAD_Execute(ScriptModule.dbg_thread, ep_num); + STHREAD_Execute(((Script *)refCon)->_dbg_thread, ep_num); } void CF_script_togglestep(int argc, char *argv[], void *refCon) { - ScriptModule.dbg_singlestep = !ScriptModule.dbg_singlestep; + ((Script *)refCon)->_dbg_singlestep = !((Script *)refCon)->_dbg_singlestep; } } // End of namespace Saga diff --git a/saga/script.h b/saga/script.h index 285db6bedc..d318c2e08f 100644 --- a/saga/script.h +++ b/saga/script.h @@ -28,6 +28,7 @@ #include "sstack.h" #include "sdata.h" +#include "text_mod.h" #include "yslib.h" namespace Saga { @@ -89,31 +90,37 @@ struct R_SCRIPT_DATABUF { int len; }; -struct R_SCRIPT_MODULE { - int initialized; - R_RSCFILE_CONTEXT *script_ctxt; - int voice_lut_present; - R_SCRIPT_LUT_ENTRY *script_lut; - int script_lut_max; - uint16 script_lut_entrylen; - R_SCRIPTDATA *current_script; - YS_DL_LIST *thread_list; - R_SCRIPT_DATABUF *data_buf[R_SCRIPT_DATABUF_NUM]; - int dbg_singlestep; - int dbg_dostep; - R_SCRIPT_THREAD *dbg_thread; - R_TEXTLIST_ENTRY *dbg_txtentry; -}; - -extern R_SCRIPT_MODULE ScriptModule; -R_SCRIPT_BYTECODE *SCRIPT_LoadBytecode(byte *bytecode_p, size_t bytecode_len); -R_DIALOGUE_LIST *SCRIPT_LoadDialogue(const byte *dialogue_p, size_t dialogue_len); -R_VOICE_LUT *SCRIPT_LoadVoiceLUT(const byte *voicelut_p, size_t voicelut_len, R_SCRIPTDATA *script); -int SCRIPT_Disassemble(R_SCRIPT_BYTECODE *script_list, R_DIALOGUE_LIST *diag_list); -void CF_script_info(int argc, char *argv[], void *refCon); -void CF_script_exec(int argc, char *argv[], void *refCon); -void CF_script_togglestep(int argc, char *argv[], void *refCon); +class Script { +public: + Script(); + ~Script(); + + int reg(void); + int loadScript(int scriptNum); + int freeScript(); + R_SCRIPT_BYTECODE *loadBytecode(byte *bytecode_p, size_t bytecode_len); + R_DIALOGUE_LIST *loadDialogue(const byte *dialogue_p, size_t dialogue_len); + R_VOICE_LUT *loadVoiceLUT(const byte *voicelut_p, size_t voicelut_len, R_SCRIPTDATA *script); + int disassemble(R_SCRIPT_BYTECODE *script_list, R_DIALOGUE_LIST *diag_list); + + +//protected: +//these are temporarily public, eventually get/set methods should be created for them + int _initialized; + R_RSCFILE_CONTEXT *_script_ctxt; + int _voice_lut_present; + R_SCRIPT_LUT_ENTRY *_script_lut; + int _script_lut_max; + uint16 _script_lut_entrylen; + R_SCRIPTDATA *_current_script; + YS_DL_LIST *_thread_list; + R_SCRIPT_DATABUF *_data_buf[R_SCRIPT_DATABUF_NUM]; + int _dbg_singlestep; + int _dbg_dostep; + R_SCRIPT_THREAD *_dbg_thread; + R_TEXTLIST_ENTRY *_dbg_txtentry; +}; } // End of namespace Saga diff --git a/saga/script_mod.h b/saga/script_mod.h index ee08bcbd2a..6dfb53116a 100644 --- a/saga/script_mod.h +++ b/saga/script_mod.h @@ -43,11 +43,6 @@ enum R_SCRIPT_VERBS { S_VERB_GIVE }; -int SCRIPT_Register(); -int SCRIPT_Init(); -int SCRIPT_Shutdown(); -int SCRIPT_Load(int script_num); -int SCRIPT_Free(); R_SCRIPT_THREAD *STHREAD_Create(); int STHREAD_Execute(R_SCRIPT_THREAD *thread, int ep_num); int STHREAD_ExecThreads(int msec); diff --git a/saga/sdata.cpp b/saga/sdata.cpp index 35c87e26cc..f88510cad1 100644 --- a/saga/sdata.cpp +++ b/saga/sdata.cpp @@ -37,20 +37,20 @@ SData::SData() { debug(0, "Initializing script data buffers"); for (i = 0; i < R_SCRIPT_DATABUF_NUM; i++) { - alloc_ptr = malloc(sizeof *ScriptModule.data_buf[0]); + alloc_ptr = malloc(sizeof *_vm->_script->_data_buf[0]); if (alloc_ptr == NULL) { error("Couldn't allocate memory for script data buffer %d", i); } - ScriptModule.data_buf[i] = (R_SCRIPT_DATABUF *)alloc_ptr; + _vm->_script->_data_buf[i] = (R_SCRIPT_DATABUF *)alloc_ptr; alloc_ptr = calloc(R_SCRIPT_DATABUF_LEN, sizeof(SDataWord_T)); if (alloc_ptr == NULL) { error("Couldn't allocate memory for script data buffer %d", i); } - ScriptModule.data_buf[i]->len = R_SCRIPT_DATABUF_LEN; - ScriptModule.data_buf[i]->data = (SDataWord_T *)alloc_ptr; + _vm->_script->_data_buf[i]->len = R_SCRIPT_DATABUF_LEN; + _vm->_script->_data_buf[i]->data = (SDataWord_T *)alloc_ptr; } } @@ -62,7 +62,7 @@ int SData::getWord(int n_buf, int n_word, SDataWord_T *data) { return R_FAILURE; } - if ((n_word < 0) || (n_word >= ScriptModule.data_buf[n_buf]->len)) { + if ((n_word < 0) || (n_word >= _vm->_script->_data_buf[n_buf]->len)) { return R_FAILURE; } @@ -70,7 +70,7 @@ int SData::getWord(int n_buf, int n_word, SDataWord_T *data) { return R_FAILURE; } - *data = ScriptModule.data_buf[n_buf]->data[n_word]; + *data = _vm->_script->_data_buf[n_buf]->data[n_word]; return R_SUCCESS; } @@ -80,11 +80,11 @@ int SData::putWord(int n_buf, int n_word, SDataWord_T data) { return R_FAILURE; } - if ((n_word < 0) || (n_word >= ScriptModule.data_buf[n_buf]->len)) { + if ((n_word < 0) || (n_word >= _vm->_script->_data_buf[n_buf]->len)) { return R_FAILURE; } - ScriptModule.data_buf[n_buf]->data[n_word] = data; + _vm->_script->_data_buf[n_buf]->data[n_word] = data; return R_SUCCESS; } @@ -99,7 +99,7 @@ int SData::setBit(int n_buf, SDataWord_T n_bit, int bitstate) { return R_FAILURE; } - if (n_bit >= (unsigned long)ScriptModule.data_buf[n_buf]->len * (sizeof(SDataWord_T) * CHAR_BIT)) { + if (n_bit >= (unsigned long)_vm->_script->_data_buf[n_buf]->len * (sizeof(SDataWord_T) * CHAR_BIT)) { return R_FAILURE; } @@ -109,9 +109,9 @@ int SData::setBit(int n_buf, SDataWord_T n_bit, int bitstate) { bit_pattern <<= ((sizeof(SDataWord_T) * CHAR_BIT) - (n_bitpos + 1)); if (bitstate) { - ScriptModule.data_buf[n_buf]->data[n_word] |= bit_pattern; + _vm->_script->_data_buf[n_buf]->data[n_word] |= bit_pattern; } else { - ScriptModule.data_buf[n_buf]->data[n_word] &= ~bit_pattern; + _vm->_script->_data_buf[n_buf]->data[n_word] &= ~bit_pattern; } return R_SUCCESS; @@ -127,7 +127,7 @@ int SData::getBit(int n_buf, SDataWord_T n_bit, int *bitstate) { return R_FAILURE; } - if (n_bit >= (SDataWord_T) ScriptModule.data_buf[n_buf]->len * (sizeof(SDataWord_T) * CHAR_BIT)) { + if (n_bit >= (SDataWord_T) _vm->_script->_data_buf[n_buf]->len * (sizeof(SDataWord_T) * CHAR_BIT)) { return R_FAILURE; } @@ -136,7 +136,7 @@ int SData::getBit(int n_buf, SDataWord_T n_bit, int *bitstate) { bit_pattern <<= ((sizeof(SDataWord_T) * CHAR_BIT) - (n_bitpos + 1)); - *bitstate = (ScriptModule.data_buf[n_buf]->data[n_word] & bit_pattern) ? 1 : 0; + *bitstate = (_vm->_script->_data_buf[n_buf]->data[n_word] & bit_pattern) ? 1 : 0; return R_SUCCESS; } diff --git a/saga/sdebug.cpp b/saga/sdebug.cpp index c7d09fc786..8e88fb5afd 100644 --- a/saga/sdebug.cpp +++ b/saga/sdebug.cpp @@ -53,9 +53,9 @@ int SDEBUG_PrintInstr(R_SCRIPT_THREAD *thread) { disp_buf[0] = 0; - if (ScriptModule.dbg_txtentry != NULL) { - TEXT_DeleteEntry(si.text_list, ScriptModule.dbg_txtentry); - ScriptModule.dbg_txtentry = NULL; + if (_vm->_script->_dbg_txtentry != NULL) { + TEXT_DeleteEntry(si.text_list, _vm->_script->_dbg_txtentry); + _vm->_script->_dbg_txtentry = NULL; } tl_e.color = 1; @@ -69,9 +69,9 @@ int SDEBUG_PrintInstr(R_SCRIPT_THREAD *thread) { // XXX MemoryReadStream *readS = - new MemoryReadStream(ScriptModule.current_script->bytecode->bytecode_p + new MemoryReadStream(_vm->_script->_current_script->bytecode->bytecode_p + thread->i_offset, - ScriptModule.current_script->bytecode->bytecode_len + _vm->_script->_current_script->bytecode->bytecode_len - thread->i_offset); in_char = readS->readByte(); sprintf(tmp_buf, "%04lX | %02X | ", thread->i_offset, in_char); @@ -517,8 +517,8 @@ int SDEBUG_PrintInstr(R_SCRIPT_THREAD *thread) { break; } - ScriptModule.dbg_txtentry = TEXT_AddEntry(si.text_list, &tl_e); - TEXT_SetDisplay(ScriptModule.dbg_txtentry, 1); + _vm->_script->_dbg_txtentry = TEXT_AddEntry(si.text_list, &tl_e); + TEXT_SetDisplay(_vm->_script->_dbg_txtentry, 1); return R_SUCCESS; } diff --git a/saga/sthread.cpp b/saga/sthread.cpp index 6fbca7302a..7263b8e653 100644 --- a/saga/sthread.cpp +++ b/saga/sthread.cpp @@ -47,7 +47,7 @@ R_SCRIPT_THREAD *STHREAD_Create() { int result; - if (!ScriptModule.initialized) { + if (!_vm->_script->_initialized) { return NULL; } @@ -62,7 +62,7 @@ R_SCRIPT_THREAD *STHREAD_Create() { return NULL; } - new_node = ys_dll_add_head(ScriptModule.thread_list, new_thread, sizeof *new_thread); + new_node = ys_dll_add_head(_vm->_script->_thread_list, new_thread, sizeof *new_thread); free(new_thread); @@ -83,11 +83,11 @@ int STHREAD_ExecThreads(int msec) { YS_DL_NODE *walk_p; R_SCRIPT_THREAD *thread; - if (!ScriptModule.initialized) { + if (!_vm->_script->_initialized) { return R_FAILURE; } - for (walk_p = ys_dll_head(ScriptModule.thread_list); walk_p != NULL; walk_p = ys_dll_next(walk_p)) { + for (walk_p = ys_dll_head(_vm->_script->_thread_list); walk_p != NULL; walk_p = ys_dll_next(walk_p)) { thread = (R_SCRIPT_THREAD *)ys_dll_get_data(walk_p); if (thread->executing) { STHREAD_Run(thread, STHREAD_DEF_INSTR_COUNT, msec); @@ -101,9 +101,9 @@ int STHREAD_SetEntrypoint(R_SCRIPT_THREAD *thread, int ep_num) { R_SCRIPT_BYTECODE *bytecode; int max_entrypoint; - assert(ScriptModule.initialized); + assert(_vm->_script->_initialized); - bytecode = ScriptModule.current_script->bytecode; + bytecode = _vm->_script->_current_script->bytecode; max_entrypoint = bytecode->n_entrypoints; if ((ep_num < 0) || (ep_num >= max_entrypoint)) { @@ -117,9 +117,9 @@ int STHREAD_SetEntrypoint(R_SCRIPT_THREAD *thread, int ep_num) { } int STHREAD_Execute(R_SCRIPT_THREAD *thread, int ep_num) { - assert(ScriptModule.initialized); + assert(_vm->_script->_initialized); - if ((ScriptModule.current_script == NULL) || (!ScriptModule.current_script->loaded)) { + if ((_vm->_script->_current_script == NULL) || (!_vm->_script->_current_script->loaded)) { return R_FAILURE; } @@ -132,15 +132,15 @@ int STHREAD_Execute(R_SCRIPT_THREAD *thread, int ep_num) { } unsigned char *GetReadPtr(R_SCRIPT_THREAD *thread) { - return ScriptModule.current_script->bytecode->bytecode_p + thread->i_offset; + return _vm->_script->_current_script->bytecode->bytecode_p + thread->i_offset; } unsigned long GetReadOffset(const byte *read_p) { - return (unsigned long)(read_p - (unsigned char *)ScriptModule.current_script->bytecode->bytecode_p); + return (unsigned long)(read_p - (unsigned char *)_vm->_script->_current_script->bytecode->bytecode_p); } size_t GetReadLen(R_SCRIPT_THREAD *thread) { - return ScriptModule.current_script->bytecode->bytecode_len - thread->i_offset; + return _vm->_script->_current_script->bytecode->bytecode_len - thread->i_offset; } @@ -168,8 +168,8 @@ int STHREAD_ReleaseSem(R_SEMAPHORE *sem) { } int STHREAD_DebugStep() { - if (ScriptModule.dbg_singlestep) { - ScriptModule.dbg_dostep = 1; + if (_vm->_script->_dbg_singlestep) { + _vm->_script->_dbg_dostep = 1; } return R_SUCCESS; @@ -194,12 +194,12 @@ int STHREAD_Run(R_SCRIPT_THREAD *thread, int instr_limit, int msec) { int unhandled = 0; // Handle debug single-stepping - if ((thread == ScriptModule.dbg_thread) && ScriptModule.dbg_singlestep) { - if (ScriptModule.dbg_dostep) { + if ((thread == _vm->_script->_dbg_thread) && _vm->_script->_dbg_singlestep) { + if (_vm->_script->_dbg_dostep) { debug_print = 1; thread->sleep_time = 0; instr_limit = 1; - ScriptModule.dbg_dostep = 0; + _vm->_script->_dbg_dostep = 0; } else { return R_SUCCESS; } @@ -730,12 +730,12 @@ int STHREAD_Run(R_SCRIPT_THREAD *thread, int instr_limit, int msec) { SSTACK_Pop(thread->stack, &data); if (a_index < 0) continue; - if (!ScriptModule.voice_lut_present) { + if (!_vm->_script->_voice_lut_present) { voice_rn = -1; } else { - voice_rn = ScriptModule.current_script->voice->voices[data]; + voice_rn = _vm->_script->_current_script->voice->voices[data]; } - ACTOR_Speak(a_index, ScriptModule.current_script->diag-> str[data], voice_rn, &thread->sem); + ACTOR_Speak(a_index, _vm->_script->_current_script->diag-> str[data], voice_rn, &thread->sem); } } break; |