From 59b72f2b382de8dbcc782712b42a832fe2c91e27 Mon Sep 17 00:00:00 2001 From: Torbjörn Andersson Date: Wed, 22 Oct 2003 06:51:57 +0000 Subject: Moved a few remaining pieces of the script interpreter into the Logic class svn-id: r10939 --- sword2/interpreter.cpp | 23 ++++----- sword2/logic.cpp | 124 +++++++++++++++++++++++++++---------------------- sword2/logic.h | 23 +++++++-- sword2/sword2.cpp | 3 +- 4 files changed, 97 insertions(+), 76 deletions(-) (limited to 'sword2') diff --git a/sword2/interpreter.cpp b/sword2/interpreter.cpp index bf1161180d..e1305b662c 100644 --- a/sword2/interpreter.cpp +++ b/sword2/interpreter.cpp @@ -36,11 +36,6 @@ namespace Sword2 { #define MAX_FN_NUMBER 117 -// Point to the global variable data -int32 *globalInterpreterVariables2 = NULL; - -int g_debugFlag = 0; // Set this to turn debugging on - #define OPCODE(x, y) { x, &Logic::y, #y } void Logic::setupOpcodes(void) { @@ -216,7 +211,7 @@ int32 Logic::executeOpcode(int i, int32 *params) { #define DOOPERATION(x) { stack2[stackPointer2 - 2] = (x); stackPointer2--; CHECKSTACKPOINTER2 } void Logic::setGlobalInterpreterVariables(int32 *vars) { - globalInterpreterVariables2 = vars; + _globals = vars; } int Logic::runScript(char *scriptData, char *objectData, uint32 *offset) { @@ -310,9 +305,9 @@ int Logic::runScript(char *scriptData, char *objectData, uint32 *offset) { case CP_PUSH_GLOBAL_VAR32: // Push a global variable Read16ip(parameter); - debug(5, "Push global var %d (%d)", parameter, globalInterpreterVariables2[parameter]); - assert(globalInterpreterVariables2); - PUSHONSTACK(globalInterpreterVariables2[parameter]); + assert(_globals); + debug(5, "Push global var %d (%d)", parameter, _globals[parameter]); + PUSHONSTACK(_globals[parameter]); break; case CP_POP_LOCAL_VAR32: // Pop a value into a local word variable @@ -444,7 +439,7 @@ int Logic::runScript(char *scriptData, char *objectData, uint32 *offset) { TRACEGLOBALVARIABLESET(parameter, value); #endif - globalInterpreterVariables2[parameter] = value; + _globals[parameter] = value; break; case CP_ADDNPOP_GLOBAL_VAR32: // Add and pop a global variable @@ -452,23 +447,23 @@ int Logic::runScript(char *scriptData, char *objectData, uint32 *offset) { // parameter = *((int16_TYPE *) (code + ip)); // ip += 2; POPOFFSTACK(value); - globalInterpreterVariables2[parameter] += value; + _globals[parameter] += value; debug(5, "+= %d into global var %d->%d", value, parameter, *(int32 *) (variables + parameter)); break; case CP_SUBNPOP_GLOBAL_VAR32: // Sub and pop a global variable Read16ip(parameter); POPOFFSTACK(value); - globalInterpreterVariables2[parameter] -= value; + _globals[parameter] -= value; debug(5, "-= %d into global var %d->%d", value, parameter, *(int32 *) (variables + parameter)); break; case CP_DEBUGON: // Turn debugging on - g_debugFlag = 1; + _debugFlag = true; break; case CP_DEBUGOFF: // Turn debugging on - g_debugFlag = 0; + _debugFlag = false; break; case CP_QUIT: // Quit out for a cycle diff --git a/sword2/logic.cpp b/sword2/logic.cpp index 1a7c0e79de..166fd53b73 100644 --- a/sword2/logic.cpp +++ b/sword2/logic.cpp @@ -19,6 +19,7 @@ #include "stdafx.h" #include "bs2/sword2.h" +#include "bs2/defs.h" #include "bs2/build_display.h" #include "bs2/console.h" #include "bs2/debug.h" @@ -34,17 +35,11 @@ Logic g_logic; #define LEVEL (_curObjectHub->logic_level) -// this must allow for the largest number of objects in a screen -#define OBJECT_KILL_LIST_SIZE 50 - -uint32 object_kill_list[OBJECT_KILL_LIST_SIZE]; - -// keeps note of no. of objects in the kill list -uint32 kills = 0; +/** + * Do one cycle of the current session. + */ int Logic::processSession(void) { - // do one cycle of the current session - uint32 run_list; uint32 ret, script; uint32 *game_object_list; @@ -218,10 +213,12 @@ int Logic::processSession(void) { return 1; } -void Logic::expressChangeSession(uint32 sesh_id) { - // a game-object can bring an immediate halt to the session and cause - // a new one to start without a screen update +/** + * Bring an immediate halt to the session and cause a new one to start without + * a screen update. + */ +void Logic::expressChangeSession(uint32 sesh_id) { //set to new _currentRunList = sesh_id; @@ -247,17 +244,22 @@ void Logic::expressChangeSession(uint32 sesh_id) { router.freeAllRouteMem(); } -void Logic::naturalChangeSession(uint32 sesh_id) { - // FIXME: This function doesn't seem to be used anywhere - - // A new session will begin next game cycle. The current cycle will - // conclude and build the screen and flip into view as normal +/** + * A new session will begin next game cycle. The current cycle will conclude + * and build the screen and flip into view as normal. + * + * @note This functino doesn't seem to be used anywhere. + */ +void Logic::naturalChangeSession(uint32 sesh_id) { _currentRunList = sesh_id; } +/** + * @return The private _currentRunList variable. + */ + uint32 Logic::getRunList(void) { - // pass back the private _currentRunList variable return _currentRunList; } @@ -270,11 +272,13 @@ int32 Logic::fnSetSession(int32 *params) { return IR_CONT; } -int32 Logic::fnEndSession(int32 *params) { - // causes no more objects in this logic loop to be processed - // the logic engine will restart at the beginning of the new list - // !!the current screen will not be drawn!! +/** + * Causes no more objects in this logic loop to be processed. The logic engine + * will restart at the beginning of the new list. The current screen will not + * be drawn! + */ +int32 Logic::fnEndSession(int32 *params) { // params: 0 id of new run-list // terminate current and change to next run-list @@ -285,11 +289,12 @@ int32 Logic::fnEndSession(int32 *params) { return IR_STOP; } -void Logic::logicUp(uint32 new_script) { - // move the current object up a level - // called by fnGosub command - remember, only the logic object has - // access to _curObjectHub +/** + * Move the current object up a level. Called by fnGosub command. Remember: + * only the logic object has access to _curObjectHub. + */ +void Logic::logicUp(uint32 new_script) { // going up a level - and we'll keeping going this cycle LEVEL++; @@ -305,9 +310,11 @@ void Logic::logicUp(uint32 new_script) { _curObjectHub->script_pc[LEVEL] = new_script & 0xffff; } -void Logic::logicOne(uint32 new_script) { - // force to level one +/** + * Force the level to one. + */ +void Logic::logicOne(uint32 new_script) { LEVEL = 1; // setup new script on level 1 @@ -315,10 +322,12 @@ void Logic::logicOne(uint32 new_script) { _curObjectHub->script_pc[1] = new_script & 0xffff; } -void Logic::logicReplace(uint32 new_script) { - // change current logic - script must quit with a TERMINATE directive - // - which does not write to &pc +/** + * Change current logic. Script must quit with a TERMINATE directive, which + * does not write to &pc + */ +void Logic::logicReplace(uint32 new_script) { // setup new script on this level _curObjectHub->script_id[LEVEL] = new_script; _curObjectHub->script_pc[LEVEL] = new_script & 0xffff; @@ -336,10 +345,10 @@ uint32 Logic::examineRunList(void) { Print_to_console("runlist number %d", _currentRunList); - while(*(game_object_list)) { - file_header = (_standardHeader*) res_man.open(*(game_object_list)); - Print_to_console(" %d %s", *(game_object_list), file_header->name); - res_man.close(*(game_object_list++)); + for (int i = 0; game_object_list[i]; i++) { + file_header = (_standardHeader *) res_man.open(game_object_list[i]); + Print_to_console(" %d %s", game_object_list[i], file_header->name); + res_man.close(game_object_list[i]); scrolls++; Build_display(); @@ -373,11 +382,12 @@ uint32 Logic::examineRunList(void) { return 1; } -void Logic::totalRestart(void) { - // reset the object restart script 1 on level 0 +/** + * Reset the object and restart script 1 on level 0 + */ +void Logic::totalRestart(void) { LEVEL = 0; - // _curObjectHub->script_id[0] = 1; // reset to rerun _curObjectHub->script_pc[0] = 1; @@ -395,15 +405,17 @@ int32 Logic::fnTotalRestart(int32 *params) { return IR_TERMINATE; } -int32 Logic::fnAddToKillList(int32 *params) { - // call *once* from object's logic script - ie. in startup code - // - so not re-called every time script drops off & restarts! - - // Mark this object for killing - to be killed when player leaves - // this screen. Object reloads & script restarts upon re-entry to - // screen, which causes this object's startup logic to be re-run - // every time we enter the screen. "Which is nice" +/** + * Mark this object for killing - to be killed when player leaves this screen. + * Object reloads and script restarts upon re-entry to screen, which causes + * this object's startup logic to be re-run every time we enter the screen. + * "Which is nice." + * + * @note Call ONCE from object's logic script, i.e. in startup code, so not + * re-called every time script frops off and restarts! + */ +int32 Logic::fnAddToKillList(int32 *params) { // params: none uint32 entry; @@ -413,28 +425,28 @@ int32 Logic::fnAddToKillList(int32 *params) { // first, scan list to see if this object is already included entry = 0; - while (entry < kills && object_kill_list[entry] != ID) + while (entry < _kills && _objectKillList[entry] != ID) entry++; // if this ID isn't already in the list, then add it, // (otherwise finish) - if (entry == kills) { + if (entry == _kills) { #ifdef _SWORD2_DEBUG // no room at the inn - if (kills == OBJECT_KILL_LIST_SIZE) + if (_kills == OBJECT_KILL_LIST_SIZE) Con_fatal_error("List full in fnAddToKillList(%u)", ID); #endif // add this 'ID' to the kill list - object_kill_list[kills] = ID; - kills++; + _objectKillList[_kills] = ID; + _kills++; // "another one bites the dust" // when we leave the screen, all these object // resources are to be cleaned out of memory and the - // kill list emptied by doing 'kills = 0', ensuring + // kill list emptied by doing '_kills = 0', ensuring // that all resources are in fact still in memory & // more importantly closed before killing! } @@ -445,14 +457,14 @@ int32 Logic::fnAddToKillList(int32 *params) { } void Logic::processKillList(void) { - for (uint32 j = 0; j < kills; j++) - res_man.remove(object_kill_list[j]); + for (uint32 i = 0; i < _kills; i++) + res_man.remove(_objectKillList[i]); - kills = 0; + _kills = 0; } void Logic::resetKillList(void) { - kills = 0; + _kills = 0; } } // End of namespace Sword2 diff --git a/sword2/logic.h b/sword2/logic.h index a1c2e4d718..e54d520794 100644 --- a/sword2/logic.h +++ b/sword2/logic.h @@ -22,16 +22,28 @@ #ifndef _LOGIC #define _LOGIC -#include "bs2/defs.h" +// #include "bs2/defs.h" #include "bs2/header.h" namespace Sword2 { #define TREE_SIZE 3 +// This must allow for the largest number of objects in a screen +#define OBJECT_KILL_LIST_SIZE 50 + class Logic { private: - void setupOpcodes(void); + // Point to the global variable data + int32 *_globals; + + uint32 _objectKillList[OBJECT_KILL_LIST_SIZE]; + + // keeps note of no. of objects in the kill list + uint32 _kills; + + // Set this to turn debugging on + bool _debugFlag; // FIXME: Some opcodes pass pointers in integer variables. I don't // think that's entirely portable. @@ -48,16 +60,17 @@ private: // denotes the res id of the game-object-list in current use uint32 _currentRunList; - void processKillList(void); - //pc during logic loop uint32 _pc; // each object has one of these tacked onto the beginning _object_hub *_curObjectHub; + void setupOpcodes(void); + void processKillList(void); + public: - Logic() { + Logic() : _globals(NULL), _kills(0), _debugFlag(false) { setupOpcodes(); } diff --git a/sword2/sword2.cpp b/sword2/sword2.cpp index f6925d39c6..c6bee075c7 100644 --- a/sword2/sword2.cpp +++ b/sword2/sword2.cpp @@ -22,6 +22,8 @@ #include "base/gameDetector.h" #include "base/plugins.h" #include "common/config-manager.h" +#include "bs2/sword2.h" +#include "bs2/defs.h" #include "bs2/build_display.h" #include "bs2/console.h" #include "bs2/controls.h" @@ -42,7 +44,6 @@ #include "bs2/sound.h" #include "bs2/speech.h" #include "bs2/startup.h" -#include "bs2/sword2.h" #include "bs2/sync.h" #include "bs2/driver/driver96.h" #include "bs2/driver/palette.h" -- cgit v1.2.3