aboutsummaryrefslogtreecommitdiff
path: root/sword2/logic.cpp
diff options
context:
space:
mode:
authorTorbjörn Andersson2005-10-29 21:24:54 +0000
committerTorbjörn Andersson2005-10-29 21:24:54 +0000
commit7f4cda6622990b7aa26e433ecdc8f17b8a3b237d (patch)
tree5a7e7f9613ff2601a7bf70477b34b30b8c0a21dc /sword2/logic.cpp
parentbcccd3f558bbbdab07d355dabfb072298e4f5cf2 (diff)
downloadscummvm-rg350-7f4cda6622990b7aa26e433ecdc8f17b8a3b237d.tar.gz
scummvm-rg350-7f4cda6622990b7aa26e433ecdc8f17b8a3b237d.tar.bz2
scummvm-rg350-7f4cda6622990b7aa26e433ecdc8f17b8a3b237d.zip
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith. To elaborate a bit, the engine no longer accesses resource data through packed structs. Instead it uses memory streams and the READ/WRITE functions. If data is mainly read, not written, I have replaced the old struct with a new one with a read() function to read the whole thing from memory into the struct's variables, and a write() function to dump the struct's variables to memory. In fact, most of these write() functions remain unused. If data is both read and written, I have replaced the struct with a class with individual get/set functions to replace the old variables. This manipulates memory directly. Since I'm fairly sure that these structs are frequently stored as local variables for a script, all script variables (both local and global) are stored as little-endian and accessed through the READ/WRITE functions, rather than being treated as arrays of 32-bit integers. On a positive note, the functions for doing endian conversion of resources and save games have been removed, and some general cleanups have been made to assist in the rewrite. Initial reports indicate that this patch indeed fixes alignment issues, and that I have not - surprisingly - broken the game on big-endian platforms. At least not in any immediately obvious way. And there's still plenty of time to fix regressions before 0.9.0, too. svn-id: r19366
Diffstat (limited to 'sword2/logic.cpp')
-rw-r--r--sword2/logic.cpp86
1 files changed, 46 insertions, 40 deletions
diff --git a/sword2/logic.cpp b/sword2/logic.cpp
index 51185a5373..c25ef8270f 100644
--- a/sword2/logic.cpp
+++ b/sword2/logic.cpp
@@ -26,8 +26,6 @@
#include "sword2/router.h"
#include "sword2/sound.h"
-#define LEVEL (_curObjectHub->logic_level)
-
namespace Sword2 {
Logic::Logic(Sword2Engine *vm) :
@@ -60,34 +58,37 @@ int Logic::processSession() {
// processing on the current list
while (_pc != 0xffffffff) {
- uint32 ret, script;
- char *raw_script_ad;
+ byte *game_object_list, *head, *raw_script_ad, *raw_data_ad;
+ uint32 level, ret, script, id;
- StandardHeader *head = (StandardHeader *)_vm->_resman->openResource(run_list);
- assert(head->fileType == RUN_LIST);
+ game_object_list = _vm->_resman->openResource(run_list) + ResHeader::size();
- uint32 *game_object_list = (uint32 *)(head + 1);
+ assert(_vm->_resman->fetchType(run_list) == RUN_LIST);
// read the next id
- uint id = game_object_list[_pc++];
- _scriptVars[ID] = id;
+ id = READ_LE_UINT32(game_object_list + 4 * _pc);
+ _pc++;
+
+ writeVar(ID, id);
_vm->_resman->closeResource(run_list);
- if (!_scriptVars[ID]) {
+ if (!id) {
// End of list - end the session naturally
return 0;
}
- head = (StandardHeader *)_vm->_resman->openResource(_scriptVars[ID]);
- assert(head->fileType == GAME_OBJECT);
+ assert(_vm->_resman->fetchType(id) == GAME_OBJECT);
+
+ head = _vm->_resman->openResource(id);
+ _curObjectHub.setAddress(head + ResHeader::size());
- _curObjectHub = (ObjectHub *)(head + 1);
+ level = _curObjectHub.getLogicLevel();
debug(5, "Level %d id(%d) pc(%d)",
- LEVEL,
- _curObjectHub->script_id[LEVEL],
- _curObjectHub->script_pc[LEVEL]);
+ level,
+ _curObjectHub.getScriptId(level),
+ _curObjectHub.getScriptPc(level));
// Do the logic for this object. We keep going until a function
// says to stop - remember, system operations are run via
@@ -97,32 +98,34 @@ int Logic::processSession() {
// There is a distinction between running one of our
// own scripts and that of another object.
- script = _curObjectHub->script_id[LEVEL];
+ level = _curObjectHub.getLogicLevel();
+ script = _curObjectHub.getScriptId(level);
- if (script / SIZE == _scriptVars[ID]) {
+ if (script / SIZE == readVar(ID)) {
// It's our own script
debug(5, "Run script %d pc=%d",
script / SIZE,
- _curObjectHub->script_pc[LEVEL]);
+ _curObjectHub.getScriptPc(level));
// This is the script data. Script and data
// object are the same.
- raw_script_ad = (char *)head;
+ raw_script_ad = head;
- ret = runScript(raw_script_ad, raw_script_ad, &_curObjectHub->script_pc[LEVEL]);
+ ret = runScript2(raw_script_ad, raw_script_ad, _curObjectHub.getScriptPcPtr(level));
} else {
// We're running the script of another game
// object - get our data object address.
- StandardHeader *far_head = (StandardHeader *)_vm->_resman->openResource(script / SIZE);
- assert(far_head->fileType == GAME_OBJECT || far_head->fileType == SCREEN_MANAGER);
+ uint8 type = _vm->_resman->fetchType(script / SIZE);
- raw_script_ad = (char *)far_head;
- char *raw_data_ad = (char *)head;
+ assert(type == GAME_OBJECT || type == SCREEN_MANAGER);
- ret = runScript(raw_script_ad, raw_data_ad, &_curObjectHub->script_pc[LEVEL]);
+ raw_script_ad = _vm->_resman->openResource(script / SIZE);
+ raw_data_ad = head;
+
+ ret = runScript2(raw_script_ad, raw_data_ad, _curObjectHub.getScriptPcPtr(level));
_vm->_resman->closeResource(script / SIZE);
@@ -131,10 +134,12 @@ int Logic::processSession() {
}
if (ret == 1) {
+ level = _curObjectHub.getLogicLevel();
+
// The script finished - drop down a level
- if (LEVEL)
- LEVEL--;
- else {
+ if (level) {
+ _curObjectHub.setLogicLevel(level - 1);
+ } else {
// Hmmm, level 0 terminated :-| Let's
// be different this time and simply
// let it restart next go :-)
@@ -145,7 +150,7 @@ int Logic::processSession() {
debug(5, "object %d script 0 terminated!", id);
// reset to rerun, drop out for a cycle
- _curObjectHub->script_pc[LEVEL] = _curObjectHub->script_id[LEVEL] & 0xffff;
+ _curObjectHub.setScriptPc(level, _curObjectHub.getScriptId(level) & 0xffff);
ret = 0;
}
} else if (ret > 2) {
@@ -163,18 +168,17 @@ int Logic::processSession() {
// Clear any syncs that were waiting for this character - it
// has used them or now looses them
- clearSyncs(_scriptVars[ID]);
+ clearSyncs(readVar(ID));
if (_pc != 0xffffffff) {
// The session is still valid so run the graphics/mouse
// service script
- uint32 null_pc = 0;
- runScript(raw_script_ad, raw_script_ad, &null_pc);
+ runScript(raw_script_ad, raw_script_ad, 0);
}
// and that's it so close the object resource
- _vm->_resman->closeResource(_scriptVars[ID]);
+ _vm->_resman->closeResource(readVar(ID));
}
// Leaving a room so remove all ids that must reboot correctly. Then
@@ -198,7 +202,7 @@ void Logic::expressChangeSession(uint32 sesh_id) {
_pc = 0xffffffff;
// Reset now in case we double-clicked an exit prior to changing screen
- _scriptVars[EXIT_FADING] = 0;
+ writeVar(EXIT_FADING, 0);
// We're trashing the list - presumably to change room. In theory,
// sync waiting in the list could be left behind and never removed -
@@ -228,9 +232,9 @@ void Logic::logicUp(uint32 new_script) {
debug(5, "new pc = %d", new_script & 0xffff);
// going up a level - and we'll keep going this cycle
- LEVEL++;
+ _curObjectHub.setLogicLevel(_curObjectHub.getLogicLevel() + 1);
- assert(LEVEL < 3); // Can be 0, 1, 2
+ assert(_curObjectHub.getLogicLevel() < 3); // Can be 0, 1, 2
logicReplace(new_script);
}
@@ -239,7 +243,7 @@ void Logic::logicUp(uint32 new_script) {
*/
void Logic::logicOne(uint32 new_script) {
- LEVEL = 1;
+ _curObjectHub.setLogicLevel(1);
logicReplace(new_script);
}
@@ -249,8 +253,10 @@ void Logic::logicOne(uint32 new_script) {
*/
void Logic::logicReplace(uint32 new_script) {
- _curObjectHub->script_id[LEVEL] = new_script;
- _curObjectHub->script_pc[LEVEL] = new_script & 0xffff;
+ uint32 level = _curObjectHub.getLogicLevel();
+
+ _curObjectHub.setScriptId(level, new_script);
+ _curObjectHub.setScriptPc(level, new_script & 0xffff);
}
void Logic::resetKillList() {