From 0bcfbd3cbdc4910e40635846844b1d356b1ae82e Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Sun, 15 May 2011 09:23:02 +1000 Subject: M4: Replaced the trouble variable data map code with direct statements. Since there aren't expected to be all that many engine variables needed, this seems a cleaner overall implementation. --- engines/m4/globals.h | 59 +------------------------------ engines/m4/mads_logic.cpp | 88 ++++++++++++++++++++++++++++++++++++++--------- engines/m4/mads_logic.h | 4 +-- engines/m4/mads_scene.h | 1 - 4 files changed, 75 insertions(+), 77 deletions(-) (limited to 'engines') diff --git a/engines/m4/globals.h b/engines/m4/globals.h index a1339632f3..a95e5169be 100644 --- a/engines/m4/globals.h +++ b/engines/m4/globals.h @@ -240,63 +240,6 @@ union DataMapEntry { typedef Common::HashMap DataMapHash; -enum DataMapType {BOOL, UINT16, INT, INT_FN}; - -class DataMapWrapper { - friend class DataMap; -private: - DataMapEntry _value; - DataMapType _type; -public: - DataMapWrapper(bool *v) { _value.boolValue = v; _type = BOOL; } - DataMapWrapper(uint16 *v) { _value.uint16Value = v; _type = UINT16; } - DataMapWrapper(int16 *v) { _value.uint16Value = (uint16 *)v; _type = UINT16; } - DataMapWrapper(int *v) { _value.intValue = v; _type = INT; } - DataMapWrapper(IntFunctionPtr v) { _value.fnPtr = v; _type = INT_FN; } - - uint16 getIntValue() { - if (_type == BOOL) return *_value.boolValue ? 0xffff : 0; - else if (_type == UINT16) return *_value.uint16Value; - else if (_type == INT) return *_value.intValue; - else return _value.fnPtr(); - } - void setIntValue(uint16 v) { - if (_type == BOOL) *_value.boolValue = v != 0; - else if (_type == UINT16) *_value.uint16Value = v; - else if (_type == INT) *_value.intValue = v; - } -}; - -#define MAP_DATA(V) _madsVm->globals()->_dataMap.addMapping(new DataMapWrapper(V)) - -class DataMap { -private: - DataMapHash _data; - Common::Array _mapList; -public: - DataMap() { - _mapList.push_back(NULL); - } - ~DataMap() { - for (uint i = 1; i < _mapList.size(); ++i) - delete _mapList[i]; - } - - void addMapping(DataMapWrapper *v) { _mapList.push_back(v); } - uint16 get(uint16 index) { - if (index < _mapList.size()) - return _mapList[index]->getIntValue(); - - return _data[index]; - } - void set(uint16 index, uint16 v) { - if (index < _mapList.size()) - _mapList[index]->setIntValue(v); - else - _data[index] = v; - } -}; - class MadsGlobals : public Globals { private: struct MessageItem { @@ -325,7 +268,7 @@ public: int previousScene; int16 _nextSceneId; uint16 actionNouns[3]; - DataMap _dataMap; + DataMapHash _dataMap; int _difficultyLevel; void loadMadsVocab(); diff --git a/engines/m4/mads_logic.cpp b/engines/m4/mads_logic.cpp index 3af9f657aa..a28d38080b 100644 --- a/engines/m4/mads_logic.cpp +++ b/engines/m4/mads_logic.cpp @@ -169,25 +169,81 @@ const char *MadsSceneLogic::_opcodeStrings[] = { * This method sets up the data map with pointers to all the common game objects. This allows the script engine to * convert game specific offsets for various fields in the original game's data segment into a generic data index * that will be common across all the MADS games - */ + void MadsSceneLogic::initialiseDataMap() { // The unique order of these items must be maintained - MAP_DATA((uint16 *)&_madsVm->scene()->_abortTimersMode2); - MAP_DATA(&_madsVm->scene()->_abortTimers); - MAP_DATA(&_madsVm->_player._stepEnabled); - MAP_DATA(&_madsVm->scene()->_nextScene); - MAP_DATA(&_madsVm->scene()->_previousScene); - MAP_DATA(&_madsVm->_player._playerPos.x); - MAP_DATA(&_madsVm->_player._playerPos.y); - MAP_DATA(&_madsVm->_player._direction); - MAP_DATA(&_madsVm->_player._visible); - MAP_DATA(&getActiveAnimationBool); - MAP_DATA(&getAnimationCurrentFrame); +} +*/ +uint32 MadsSceneLogic::getDataValue(int dataId) { + switch (dataId) { + case 1: + return _madsVm->scene()->_abortTimersMode2; + case 2: + return _madsVm->scene()->_abortTimers; + case 3: + return _madsVm->_player._stepEnabled ? 0xffff : 0; + case 4: + return _madsVm->scene()->_nextScene; + case 5: + return _madsVm->scene()->_previousScene; + case 6: + return _madsVm->_player._playerPos.x; + case 7: + return _madsVm->_player._playerPos.y; + case 8: + return _madsVm->_player._direction; + case 9: + return _madsVm->_player._visible ? 0xffff : 0; + case 10: + return getActiveAnimationBool(); + case 11: + return getAnimationCurrentFrame(); + default: + // All other data variables get stored in the hash table + return _madsVm->globals()->_dataMap[dataId]; + break; + } } -DataMap &MadsSceneLogic::dataMap() { - return _madsVm->globals()->_dataMap; +void MadsSceneLogic::setDataValue(int dataId, uint16 dataValue) { + switch (dataId) { + case 1: + _madsVm->scene()->_abortTimersMode2 = (AbortTimerMode)dataValue; + break; + case 2: + _madsVm->scene()->_abortTimers = dataValue; + break; + case 3: + _madsVm->_player._stepEnabled = dataValue != 0; + break; + case 4: + _madsVm->scene()->_nextScene = dataValue; + break; + case 5: + _madsVm->scene()->_previousScene = dataValue; + break; + case 6: + _madsVm->_player._playerPos.x = dataValue; + break; + case 7: + _madsVm->_player._playerPos.y = dataValue; + break; + case 8: + _madsVm->_player._direction = dataValue; + break; + case 9: + _madsVm->_player._visible = dataValue != 0; + break; + case 10: + case 11: + error("Tried to set read only data field %d", dataId); + break; + default: + // All other data variables get stored in the hash table + _madsVm->globals()->_dataMap[dataId] = dataValue; + break; + } } const char *MadsSceneLogic::formAnimName(char sepChar, int16 suffixNum) { @@ -531,7 +587,7 @@ void MadsSceneLogic::execute(uint32 subOffset) { case OP_DLOAD: { // Gets data variable param = getParam(scriptOffset, opcode); - uint16 v = dataMap().get(param); + uint16 v = getDataValue(param); stack.push(ScriptVar(v)); break; } @@ -539,7 +595,7 @@ void MadsSceneLogic::execute(uint32 subOffset) { case OP_DSTORE: { // Stores data variable param = getParam(scriptOffset, opcode); ScriptVar v = stack.pop(); - dataMap().set(param, v.isInt() ? v.get() : 0); + setDataValue(param, v.isInt() ? v.get() : 0); break; } diff --git a/engines/m4/mads_logic.h b/engines/m4/mads_logic.h index 861a56880c..016adb2ebf 100644 --- a/engines/m4/mads_logic.h +++ b/engines/m4/mads_logic.h @@ -85,14 +85,14 @@ private: void getSceneSpriteSet(); void getAnimName(); - DataMap &dataMap(); + uint32 getDataValue(int dataId); + void setDataValue(int dataId, uint16 dataValue); void getCallParameters(int numParams, Common::Stack &stack, ScriptVar *callParams); public: MadsSceneLogic() { _scriptsData = NULL; } ~MadsSceneLogic() { delete _scriptsData; } void initialiseScripts(); - void initialiseDataMap(); void selectScene(int sceneNum); void setupScene(); diff --git a/engines/m4/mads_scene.h b/engines/m4/mads_scene.h index c39dbb136f..12d7088a2f 100644 --- a/engines/m4/mads_scene.h +++ b/engines/m4/mads_scene.h @@ -110,7 +110,6 @@ public: virtual ~MadsScene(); void initialise() { _sceneLogic.initialiseScripts(); - _sceneLogic.initialiseDataMap(); } // Methods that differ between engines -- cgit v1.2.3