From 2c012197910972fc5a16a3316b21b2b4ad7d8687 Mon Sep 17 00:00:00 2001 From: Andrew Kurushin Date: Wed, 27 Apr 2005 11:51:11 +0000 Subject: some load-save preparetion svn-id: r17836 --- saga/actor.cpp | 8 +-- saga/actor.h | 211 +++++++++++++++++++++++++++++++++++++++++------------- saga/isomap.cpp | 7 +- saga/saveload.cpp | 76 +++----------------- 4 files changed, 175 insertions(+), 127 deletions(-) diff --git a/saga/actor.cpp b/saga/actor.cpp index 297919d67c..d879c70ed4 100644 --- a/saga/actor.cpp +++ b/saga/actor.cpp @@ -229,10 +229,6 @@ Actor::Actor(SagaEngine *vm) : _vm(vm) { actor->currentAction = ITE_ActorTable[i].currentAction; actor->facingDirection = ITE_ActorTable[i].facingDirection; actor->actionDirection = ITE_ActorTable[i].actionDirection; - actor->frameNumber = 0; - actor->targetObject = ID_NOTHING; - actor->actorFlags = 0; - actor->lastZone = NULL; actor->location.x = ITE_ActorTable[i].x; actor->location.y = ITE_ActorTable[i].y; @@ -255,12 +251,10 @@ Actor::Actor(SagaEngine *vm) : _vm(vm) { obj->spriteListResourceId = ITE_ObjectTable[i].spriteListResourceId; obj->sceneNumber = ITE_ObjectTable[i].sceneIndex; obj->interactBits = ITE_ObjectTable[i].interactBits; - obj->flags = 0; obj->location.x = ITE_ObjectTable[i].x; obj->location.y = ITE_ObjectTable[i].y; obj->location.z = ITE_ObjectTable[i].z; - obj->disabled = false; } } else { @@ -1248,7 +1242,7 @@ bool Actor::getSpriteParams(CommonObjectData *commonObjectData, int &frameNumber } else { if (validActorId(commonObjectData->id)) { spriteList = &((ActorData*)commonObjectData)->spriteList; - frameNumber = commonObjectData->frameNumber; + frameNumber = ((ActorData*)commonObjectData)->frameNumber; } else { if (validObjId(commonObjectData->id)) { spriteList = &_vm->_sprite->_mainSprites; diff --git a/saga/actor.h b/saga/actor.h index 542687d4a5..243c426c88 100644 --- a/saga/actor.h +++ b/saga/actor.h @@ -26,6 +26,8 @@ #ifndef SAGA_ACTOR_H__ #define SAGA_ACTOR_H__ +#include "common/file.h" + #include "saga/sprite.h" #include "saga/itedata.h" #include "saga/list.h" @@ -131,28 +133,39 @@ struct ActorFrameSequence { }; struct Location { - int x; // logical coordinates - int y; // - int z; // + int32 x; // logical coordinates + int32 y; // + int32 z; // Location() { x = y = z = 0; } + void saveState(File& out) { + out.writeSint32LE(x); + out.writeSint32LE(y); + out.writeSint32LE(z); + } + void loadState(File& in) { + x = in.readSint32LE(); + y = in.readSint32LE(); + z = in.readSint32LE(); + } + int distance(const Location &location) const { return MAX(ABS(x - location.x), ABS(y - location.y)); } - int &u() { + int32 &u() { return x; } - int &v() { + int32 &v() { return y; } - int u() const { + int32 u() const { return x; } - int v() const { + int32 v() const { return y; } - int uv() const { + int32 uv() const { return u() + v(); } void delta(const Location &location, Location &result) const { @@ -190,30 +203,43 @@ struct Location { class CommonObjectData { public: +//constant bool disabled; // disabled in init section - int index; // index in local array + int32 index; // index in local array uint16 id; // object id - uint16 flags; // initial flags - int nameIndex; // index in name string list - int sceneNumber; // scene - int scriptEntrypointNumber; // script entrypoint number - + int32 scriptEntrypointNumber; // script entrypoint number int32 spriteListResourceId; // sprite list resource id +//variables + uint16 flags; // initial flags + int32 nameIndex; // index in name string list + int32 sceneNumber; // scene + Location location; // logical coordinates Point screenPosition; // screen coordinates - int screenDepth; // - int screenScale; // - - int frameNumber; // current frame number - - CommonObjectData() { - screenDepth = screenScale = 0; - flags = 0; - frameNumber = 0; - spriteListResourceId = 0; + int32 screenDepth; // + int32 screenScale; // + + void saveState(File& out) { + out.writeUint16LE(flags); + out.writeSint32LE(nameIndex); + out.writeSint32LE(sceneNumber); + location.saveState(out); + out.writeSint16LE(screenPosition.x); + out.writeSint16LE(screenPosition.y); + out.writeSint32LE(screenDepth); + out.writeSint32LE(screenScale); + } + void loadState(File& in) { + flags = in.readUint16LE(); + nameIndex = in.readSint32LE(); + sceneNumber = in.readSint32LE(); + location.loadState(in); + screenPosition.x = in.readSint16LE(); + screenPosition.y = in.readSint16LE(); + screenDepth = in.readSint32LE(); + screenScale = in.readSint32LE(); } - }; typedef CommonObjectData *CommonObjectDataPointer; @@ -222,65 +248,150 @@ typedef SortedList CommonObjectOrderList; class ObjectData: public CommonObjectData { public: + //constant uint16 interactBits; + ObjectData() { + memset(this, 0, sizeof(*this)); + } }; class ActorData: public CommonObjectData { public: + //constant + SpriteList spriteList; // sprite list data + + ActorFrameSequence *frames; // Actor's frames + int framesCount; // Actor's frames count + int frameListResourceId; // Actor's frame list resource id byte speechColor; // Actor dialogue color + //variables uint16 actorFlags; // dynamic flags - int currentAction; // ActorActions type - int facingDirection; // orientation - int actionDirection; - int actionCycle; + int32 currentAction; // ActorActions type + int32 facingDirection; // orientation + int32 actionDirection; + int32 actionCycle; uint16 targetObject; const HitZone *lastZone; - int cycleFrameSequence; + int32 cycleFrameSequence; uint8 cycleDelay; uint8 cycleTimeCount; uint8 cycleFlags; - SpriteList spriteList; // sprite list data - - ActorFrameSequence *frames; // Actor's frames - int framesCount; // Actor's frames count - int frameListResourceId; // Actor's frame list resource id + int32 frameNumber; // current frame number - int tileDirectionsAlloced; + int32 tileDirectionsAlloced; byte *tileDirections; - int walkStepsAlloced; + int32 walkStepsAlloced; Point *walkStepsPoints; - int walkStepsCount; - int walkStepIndex; + int32 walkStepsCount; + int32 walkStepIndex; Location finalTarget; Location partialTarget; - int walkFrameSequence; - + int32 walkFrameSequence; + + void saveState(File& out) { + CommonObjectData::saveState(out); + out.writeUint16LE(actorFlags); + out.writeSint32LE(currentAction); + out.writeSint32LE(facingDirection); + out.writeSint32LE(actionDirection); + out.writeSint32LE(actionCycle); + out.writeUint16LE(targetObject); + + //TODO: write lastZone + out.writeSint32LE(cycleFrameSequence); + out.writeByte(cycleDelay); + out.writeByte(cycleTimeCount); + out.writeByte(cycleFlags); + out.writeSint32LE(frameNumber); + + out.writeSint32LE(tileDirectionsAlloced); + for(int i = 0; i < tileDirectionsAlloced; i++) { + out.writeByte(tileDirections[i]); + } + + out.writeSint32LE(walkStepsAlloced); + for(int i = 0; i < walkStepsAlloced; i++) { + out.writeSint16LE(walkStepsPoints[i].x); + out.writeSint16LE(walkStepsPoints[i].y); + } + + out.writeSint32LE(walkStepsCount); + out.writeSint32LE(walkStepIndex); + finalTarget.saveState(out); + partialTarget.saveState(out); + out.writeSint32LE(walkFrameSequence); + } + void loadState(File& in) { + CommonObjectData::loadState(in); + actorFlags = in.readUint16LE(); + currentAction = in.readSint32LE(); + facingDirection = in.readSint32LE(); + actionDirection = in.readSint32LE(); + actionCycle = in.readSint32LE(); + targetObject = in.readUint16LE(); + + //TODO: read lastZone + lastZone = NULL; + cycleFrameSequence = in.readSint32LE(); + cycleDelay = in.readByte(); + cycleTimeCount = in.readByte(); + cycleFlags = in.readByte(); + frameNumber = in.readSint32LE(); + + + setTileDirectionsSize(in.readSint32LE(), true); + for(int i = 0; i < tileDirectionsAlloced; i++) { + tileDirections[i] = in.readByte(); + } + + setWalkStepsPointsSize(in.readSint32LE(), true); + for(int i = 0; i < walkStepsAlloced; i++) { + walkStepsPoints[i].x = in.readSint16LE(); + walkStepsPoints[i].y = in.readSint16LE(); + } + + walkStepsCount = in.readSint32LE(); + walkStepIndex = in.readSint32LE(); + finalTarget.loadState(in); + partialTarget.loadState(in); + walkFrameSequence = in.readSint32LE(); + } + + void setTileDirectionsSize(int size, bool forceRealloc) { + if ((size <= tileDirectionsAlloced) && !forceRealloc) { + return; + } + tileDirectionsAlloced = size; + tileDirections = (byte*)realloc(tileDirections, tileDirectionsAlloced * sizeof(*tileDirections)); + } + void cycleWrap(int cycleLimit) { if (actionCycle >= cycleLimit) actionCycle = 0; } - void addWalkStepPoint(const Point &point) { - if (walkStepsCount + 1 > walkStepsAlloced) { - walkStepsAlloced += 100; - walkStepsPoints = (Point*)realloc(walkStepsPoints, walkStepsAlloced * sizeof(*walkStepsPoints)); + void setWalkStepsPointsSize(int size, bool forceRealloc) { + if ((size <= walkStepsAlloced) && !forceRealloc) { + return; } + walkStepsAlloced = size; + walkStepsPoints = (Point*)realloc(walkStepsPoints, walkStepsAlloced * sizeof(*walkStepsPoints)); + } + + void addWalkStepPoint(const Point &point) { + setWalkStepsPointsSize(walkStepsCount + 1, false); walkStepsPoints[walkStepsCount++] = point; } ActorData() { - memset(this, 0xFE, sizeof(*this)); - walkStepsPoints = NULL; - tileDirectionsAlloced = walkStepsAlloced = walkStepsCount = walkStepIndex = 0; - tileDirections = NULL; - memset(&spriteList, 0, sizeof(spriteList)); + memset(this, 0, sizeof(*this)); } ~ActorData() { free(frames); diff --git a/saga/isomap.cpp b/saga/isomap.cpp index 7b0a3b4ee4..8a3e90a8c5 100644 --- a/saga/isomap.cpp +++ b/saga/isomap.cpp @@ -1337,11 +1337,8 @@ void IsoMap::findTilePath(ActorData* actor, const Location &start, const Locatio i = 64; }*/ actor->walkStepsCount = i; - if (i) { - if (actor->tileDirectionsAlloced < i) { - actor->tileDirectionsAlloced = i; - actor->tileDirections = (byte*)realloc(actor->tileDirections, actor->tileDirectionsAlloced * sizeof(*actor->tileDirections)); - } + if (i) { + actor->setTileDirectionsSize(i, false); memcpy(actor->tileDirections, res, i ); } } diff --git a/saga/saveload.cpp b/saga/saveload.cpp index 71661128a2..a0daf0e93f 100644 --- a/saga/saveload.cpp +++ b/saga/saveload.cpp @@ -42,10 +42,9 @@ namespace Saga { void SagaEngine::save() { File out; - out.open("ite.sav", File::kFileWriteMode); + out.open("iteSCUMMVM.sav", File::kFileWriteMode); + //TODO: version number - out.writeSint16LE(_actor->_actorsCount); - out.writeSint16LE(_actor->_objsCount); out.writeSint16LE(_script->_commonBufferSize); out.writeSint16LE(_actor->getProtagState()); @@ -61,37 +60,12 @@ void SagaEngine::save() { for (i = 0; i < _actor->_actorsCount; i++) { ActorData *a = _actor->_actors[i]; - - out.writeSint32LE(a->sceneNumber); - out.writeSint16LE(a->location.x); - out.writeSint16LE(a->location.y); - out.writeSint16LE(a->location.z); - out.writeSint16LE(a->finalTarget.x); - out.writeSint16LE(a->finalTarget.y); - out.writeSint16LE(a->finalTarget.z); - out.writeByte(a->currentAction); - out.writeByte(a->facingDirection); - out.writeSint16LE(a->targetObject); - out.writeByte(a->flags & (kProtagonist | kFollower)); - out.writeByte(a->frameNumber); - out.writeSint16LE(0); + a->saveState(out); } for (i = 0; i < _actor->_objsCount; i++) { ObjectData *o = _actor->_objs[i]; - - out.writeSint32LE(o->sceneNumber); - out.writeSint32LE(o->id); - out.writeSint16LE(o->location.x); - out.writeSint16LE(o->location.y); - out.writeSint16LE(o->location.z); - out.writeSint16LE(o->nameIndex); - if (o->sceneNumber == ITE_SCENE_INV) { - out.writeSint16LE(_interface->inventoryItemPosition(_actor->objIndexToId(i))); - } else { - out.writeSint16LE(0); - } - out.writeByte(0); + o->saveState(out); } for (i = 0; i < _script->_commonBufferSize; i++) @@ -105,17 +79,15 @@ void SagaEngine::save() { void SagaEngine::load() { File in; - int actorsCount, objsCount, commonBufferSize; + int commonBufferSize; int scenenum, inset; int mapx, mapy; - in.open("ite.sav"); + in.open("iteSCUMMVM.sav"); if (!in.isOpen()) return; - actorsCount = in.readSint16LE(); - objsCount = in.readSint16LE(); commonBufferSize = in.readSint16LE(); _actor->setProtagState(in.readSint16LE()); @@ -131,42 +103,16 @@ void SagaEngine::load() { uint16 i; - for (i = 0; i < actorsCount; i++) { + for (i = 0; i < _actor->_actorsCount; i++) { ActorData *a = _actor->_actors[i]; - - a->sceneNumber = in.readSint32LE(); - a->location.x = in.readSint16LE(); - a->location.y = in.readSint16LE(); - a->location.z = in.readSint16LE(); - a->finalTarget.x = in.readSint16LE(); - a->finalTarget.y = in.readSint16LE(); - a->finalTarget.z = in.readSint16LE(); - a->currentAction = in.readByte(); - a->facingDirection = in.readByte(); - a->targetObject = in.readSint16LE(); - a->flags = (a->flags & ~(kProtagonist | kFollower) | in.readByte()); - a->frameNumber = in.readByte(); - in.readSint16LE(); + a->loadState(in); } - _interface->clearInventory(); + _interface->clearInventory(); //TODO: interface load-save-state - for (i = 0; i < objsCount; i++) { + for (i = 0; i < _actor->_objsCount; i++) { ObjectData *o = _actor->_objs[i]; - int pos; - - o->sceneNumber = in.readSint32LE(); - o->id = in.readSint32LE(); - o->location.x = in.readSint16LE(); - o->location.y = in.readSint16LE(); - o->location.z = in.readSint16LE(); - o->nameIndex = in.readSint16LE(); - - pos = in.readSint16LE(); - if (o->sceneNumber == ITE_SCENE_INV) { - _interface->addToInventory(_actor->objIndexToId(i), pos); - } - in.readByte(); + o->loadState(in); } for (i = 0; i < commonBufferSize; i++) -- cgit v1.2.3