From 399b59a583b0ac92941cfe7756712d80970e47ee Mon Sep 17 00:00:00 2001 From: strangerke Date: Fri, 18 Feb 2011 08:11:55 +0100 Subject: HUGO: Avoid duplicated code in functions related to HUGO.DAT --- engines/hugo/object.cpp | 193 +++++++++++++++++++++--------------------------- 1 file changed, 83 insertions(+), 110 deletions(-) (limited to 'engines/hugo/object.cpp') diff --git a/engines/hugo/object.cpp b/engines/hugo/object.cpp index 786ea4762a..f9364a8781 100644 --- a/engines/hugo/object.cpp +++ b/engines/hugo/object.cpp @@ -368,141 +368,114 @@ bool ObjectHandler::findObjectSpace(object_t *obj, int16 *destx, int16 *desty) { return foundFl; } +void ObjectHandler::readUse(Common::ReadStream &in, uses_t &curUse) { + curUse.objId = in.readSint16BE(); + curUse.dataIndex = in.readUint16BE(); + uint16 numSubElem = in.readUint16BE(); + curUse.targets = (target_t *)malloc(sizeof(target_t) * numSubElem); + for (int j = 0; j < numSubElem; j++) { + curUse.targets[j].nounIndex = in.readUint16BE(); + curUse.targets[j].verbIndex = in.readUint16BE(); + } +} /** * Load _uses from Hugo.dat */ void ObjectHandler::loadObjectUses(Common::ReadStream &in) { + uses_t tmpUse; //Read _uses for (int varnt = 0; varnt < _vm->_numVariant; varnt++) { + tmpUse.targets = 0; uint16 numElem = in.readUint16BE(); - uses_t *wrkUses = (uses_t *)malloc(sizeof(uses_t) * numElem); - - for (int i = 0; i < numElem; i++) { - wrkUses[i].objId = in.readSint16BE(); - wrkUses[i].dataIndex = in.readUint16BE(); - uint16 numSubElem = in.readUint16BE(); - wrkUses[i].targets = (target_t *)malloc(sizeof(target_t) * numSubElem); - for (int j = 0; j < numSubElem; j++) { - wrkUses[i].targets[j].nounIndex = in.readUint16BE(); - wrkUses[i].targets[j].verbIndex = in.readUint16BE(); - } - } - if (varnt == _vm->_gameVariant) { _usesSize = numElem; - _uses = wrkUses; - } else { - for (int i = 0; i < numElem; i++) - free(wrkUses[i].targets); - free(wrkUses); + _uses = (uses_t *)malloc(sizeof(uses_t) * numElem); } + + for (int i = 0; i < numElem; i++) + readUse(in, (varnt == _vm->_gameVariant) ? _uses[i] : tmpUse); + + if (tmpUse.targets) + free(tmpUse.targets); } } +void ObjectHandler::readObject(Common::ReadStream &in, object_t &curObject) { + curObject.nounIndex = in.readUint16BE(); + curObject.dataIndex = in.readUint16BE(); + uint16 numSubElem = in.readUint16BE(); + + if (numSubElem == 0) + curObject.stateDataIndex = 0; + else + curObject.stateDataIndex = (uint16 *)malloc(sizeof(uint16) * numSubElem); + for (int j = 0; j < numSubElem; j++) + curObject.stateDataIndex[j] = in.readUint16BE(); + + curObject.pathType = (path_t) in.readSint16BE(); + curObject.vxPath = in.readSint16BE(); + curObject.vyPath = in.readSint16BE(); + curObject.actIndex = in.readUint16BE(); + curObject.seqNumb = in.readByte(); + curObject.currImagePtr = 0; + + if (curObject.seqNumb == 0) { + curObject.seqList[0].imageNbr = 0; + curObject.seqList[0].seqPtr = 0; + } + + for (int j = 0; j < curObject.seqNumb; j++) { + curObject.seqList[j].imageNbr = in.readUint16BE(); + curObject.seqList[j].seqPtr = 0; + } + + curObject.cycling = (cycle_t)in.readByte(); + curObject.cycleNumb = in.readByte(); + curObject.frameInterval = in.readByte(); + curObject.frameTimer = in.readByte(); + curObject.radius = in.readByte(); + curObject.screenIndex = in.readByte(); + curObject.x = in.readSint16BE(); + curObject.y = in.readSint16BE(); + curObject.oldx = in.readSint16BE(); + curObject.oldy = in.readSint16BE(); + curObject.vx = in.readByte(); + curObject.vy = in.readByte(); + curObject.objValue = in.readByte(); + curObject.genericCmd = in.readSint16BE(); + curObject.cmdIndex = in.readUint16BE(); + curObject.carriedFl = (in.readByte() != 0); + curObject.state = in.readByte(); + curObject.verbOnlyFl = (in.readByte() != 0); + curObject.priority = in.readByte(); + curObject.viewx = in.readSint16BE(); + curObject.viewy = in.readSint16BE(); + curObject.direction = in.readSint16BE(); + curObject.curSeqNum = in.readByte(); + curObject.curImageNum = in.readByte(); + curObject.oldvx = in.readByte(); + curObject.oldvy = in.readByte(); +} /** * Load ObjectArr from Hugo.dat */ void ObjectHandler::loadObjectArr(Common::ReadStream &in) { debugC(6, kDebugObject, "loadObject(&in)"); + object_t tmpObject; for (int varnt = 0; varnt < _vm->_numVariant; varnt++) { uint16 numElem = in.readUint16BE(); + tmpObject.stateDataIndex = 0; if (varnt == _vm->_gameVariant) { _objCount = numElem; _objects = (object_t *)malloc(sizeof(object_t) * numElem); - for (int i = 0; i < numElem; i++) { - _objects[i].nounIndex = in.readUint16BE(); - _objects[i].dataIndex = in.readUint16BE(); - uint16 numSubElem = in.readUint16BE(); - if (numSubElem == 0) - _objects[i].stateDataIndex = 0; - else - _objects[i].stateDataIndex = (uint16 *)malloc(sizeof(uint16) * numSubElem); - for (int j = 0; j < numSubElem; j++) - _objects[i].stateDataIndex[j] = in.readUint16BE(); - _objects[i].pathType = (path_t) in.readSint16BE(); - _objects[i].vxPath = in.readSint16BE(); - _objects[i].vyPath = in.readSint16BE(); - _objects[i].actIndex = in.readUint16BE(); - _objects[i].seqNumb = in.readByte(); - _objects[i].currImagePtr = 0; - if (_objects[i].seqNumb == 0) { - _objects[i].seqList[0].imageNbr = 0; - _objects[i].seqList[0].seqPtr = 0; - } - for (int j = 0; j < _objects[i].seqNumb; j++) { - _objects[i].seqList[j].imageNbr = in.readUint16BE(); - _objects[i].seqList[j].seqPtr = 0; - } - _objects[i].cycling = (cycle_t)in.readByte(); - _objects[i].cycleNumb = in.readByte(); - _objects[i].frameInterval = in.readByte(); - _objects[i].frameTimer = in.readByte(); - _objects[i].radius = in.readByte(); - _objects[i].screenIndex = in.readByte(); - _objects[i].x = in.readSint16BE(); - _objects[i].y = in.readSint16BE(); - _objects[i].oldx = in.readSint16BE(); - _objects[i].oldy = in.readSint16BE(); - _objects[i].vx = in.readByte(); - _objects[i].vy = in.readByte(); - _objects[i].objValue = in.readByte(); - _objects[i].genericCmd = in.readSint16BE(); - _objects[i].cmdIndex = in.readUint16BE(); - _objects[i].carriedFl = (in.readByte() != 0); - _objects[i].state = in.readByte(); - _objects[i].verbOnlyFl = (in.readByte() != 0); - _objects[i].priority = in.readByte(); - _objects[i].viewx = in.readSint16BE(); - _objects[i].viewy = in.readSint16BE(); - _objects[i].direction = in.readSint16BE(); - _objects[i].curSeqNum = in.readByte(); - _objects[i].curImageNum = in.readByte(); - _objects[i].oldvx = in.readByte(); - _objects[i].oldvy = in.readByte(); - } - } else { - for (int i = 0; i < numElem; i++) { - in.readUint16BE(); - in.readUint16BE(); - uint16 numSubElem = in.readUint16BE(); - for (int j = 0; j < numSubElem; j++) - in.readUint16BE(); - in.readSint16BE(); - in.readSint16BE(); - in.readSint16BE(); - in.readUint16BE(); - numSubElem = in.readByte(); - for (int j = 0; j < numSubElem; j++) - in.readUint16BE(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readSint16BE(); - in.readSint16BE(); - in.readSint16BE(); - in.readSint16BE(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readSint16BE(); - in.readUint16BE(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readSint16BE(); - in.readSint16BE(); - in.readUint16BE(); - in.readByte(); - in.readByte(); - in.readByte(); - in.readByte(); - } } + + for (int i = 0; i < numElem; i++) + readObject(in, (varnt == _vm->_gameVariant) ? _objects[i] : tmpObject); + + if (tmpObject.stateDataIndex) + free(tmpObject.stateDataIndex); } } -- cgit v1.2.3