From ad1cecaeac5d54b0973d6c7edce8181cb965ff68 Mon Sep 17 00:00:00 2001 From: Paul Gilbert Date: Thu, 20 Jun 2019 21:20:22 -0700 Subject: GLK: ALAN2: Added savegame code --- engines/glk/alan2/alan2.cpp | 69 +++++++++++++++++++++++++++++++++++++++++---- engines/glk/alan2/alan2.h | 6 ++++ engines/glk/alan2/exe.h | 2 ++ engines/glk/alan2/types.h | 12 ++++++++ engines/glk/module.mk | 1 + 5 files changed, 85 insertions(+), 5 deletions(-) diff --git a/engines/glk/alan2/alan2.cpp b/engines/glk/alan2/alan2.cpp index c9625a7076..73b8a5e1a3 100644 --- a/engines/glk/alan2/alan2.cpp +++ b/engines/glk/alan2/alan2.cpp @@ -21,13 +21,15 @@ */ #include "glk/alan2/alan2.h" +#include "glk/alan2/exe.h" #include "glk/alan2/main.h" #include "glk/alan2/glkio.h" +#include "common/system.h" #include "common/config-manager.h" #include "common/translation.h" #include "common/error.h" #include "common/scummsys.h" -#include "common/system.h" +#include "common/serializer.h" #include "glk/glk.h" #include "glk/streams.h" @@ -75,13 +77,70 @@ void Alan2::initialize() { } Common::Error Alan2::readSaveData(Common::SeekableReadStream *rs) { - // TODO - return Common::kReadingFailed; + Common::Serializer s(rs, nullptr); + synchronizeSave(s); + + return Common::kNoError; } Common::Error Alan2::writeGameData(Common::WriteStream *ws) { - // TODO - return Common::kWritingFailed; + Common::Serializer s(nullptr, ws); + synchronizeSave(s); + + ws->flush(); + return Common::kNoError; +} + +void Alan2::synchronizeSave(Common::Serializer &s) { + AtrElem *atr; + int i; + + // Sync current values + cur.synchronize(s); + + // Save actors + for (i = ACTMIN; i <= ACTMAX; ++i) { + s.syncAsSint32LE(acts[i-ACTMIN].loc); + s.syncAsSint32LE(acts[i-ACTMIN].script); + s.syncAsSint32LE(acts[i-ACTMIN].step); + s.syncAsSint32LE(acts[i-ACTMIN].count); + + if (acts[i-ACTMIN].atrs) { + for (atr = (AtrElem *)addrTo(acts[i-ACTMIN].atrs); !endOfTable(atr); ++atr) + s.syncAsSint32LE(atr->val); + } + } + + // Sync locations + for (i = LOCMIN; i <= LOCMAX; ++i) { + s.syncAsSint32LE(locs[i-LOCMIN].describe); + if (locs[i-LOCMIN].atrs) + for (atr = (AtrElem *)addrTo(locs[i-LOCMIN].atrs); !endOfTable(atr); atr++) + s.syncAsSint32LE(atr->val); + } + + // Sync objects + for (i = OBJMIN; i <= OBJMAX; ++i) { + s.syncAsSint32LE(objs[i-OBJMIN].loc); + if (objs[i-OBJMIN].atrs) + for (atr = (AtrElem *)addrTo(objs[i-OBJMIN].atrs); !endOfTable(atr); atr++) + s.syncAsSint32LE(atr->val); + } + + // Sync the event queue + if (s.isSaving()) { + eventq[etop].time = 0; // Mark the top + for (i = 0; i <= etop; ++i) + eventq[i].synchronize(s); + } else { + for (etop = 0; eventq[etop - 1].time; ++etop) + eventq[etop].synchronize(s); + --etop; + } + + // Sync scores + for (i = 0; scores[i] != EOF; i++) + s.syncAsSint32LE(scores[i]); } bool Alan2::is_gamefile_valid() { diff --git a/engines/glk/alan2/alan2.h b/engines/glk/alan2/alan2.h index 78c5107406..8960648dd7 100644 --- a/engines/glk/alan2/alan2.h +++ b/engines/glk/alan2/alan2.h @@ -24,6 +24,7 @@ #define GLK_ALAN2 #include "common/scummsys.h" +#include "common/serializer.h" #include "common/stack.h" #include "glk/glk_api.h" @@ -47,6 +48,11 @@ private: * Initialization */ void initialize(); + + /** + * Synchronize data to or from a save file + */ + void synchronizeSave(Common::Serializer &s); public: /** * Constructor diff --git a/engines/glk/alan2/exe.h b/engines/glk/alan2/exe.h index 0acfdda6eb..fbecac5975 100644 --- a/engines/glk/alan2/exe.h +++ b/engines/glk/alan2/exe.h @@ -26,6 +26,8 @@ /* Header file for instruction execution unit in Alan interpreter */ +#include "glk/alan2/types.h" + namespace Glk { namespace Alan2 { diff --git a/engines/glk/alan2/types.h b/engines/glk/alan2/types.h index 11a17a539d..699dd0e149 100644 --- a/engines/glk/alan2/types.h +++ b/engines/glk/alan2/types.h @@ -25,6 +25,8 @@ #include "glk/alan2/sysdep.h" #include "glk/alan2/acode.h" +#include "common/serializer.h" +#include "common/stream.h" namespace Glk { namespace Alan2 { @@ -89,6 +91,11 @@ struct CurVars { tick, score, visits; + + /** + * Read or write data to/from a save file + */ + void synchronize(Common::Serializer &s); }; #include "common/pack-start.h" // START STRUCT PACKING @@ -246,6 +253,11 @@ struct EvtqElem { /* EVENT QUEUE ELEMENT */ int time; int event; int where; + + /** + * Read or write data to/from a save file + */ + void synchronize(Common::Serializer &s); } PACKED_STRUCT; struct IniElem { /* STRING INITIALISATION TABLE */ diff --git a/engines/glk/module.mk b/engines/glk/module.mk index 3537fcd553..6536cf071f 100644 --- a/engines/glk/module.mk +++ b/engines/glk/module.mk @@ -49,6 +49,7 @@ MODULE_OBJS := \ alan2/stack.o \ alan2/sysdep.o \ alan2/term.o \ + alan2/types.o \ frotz/bitmap_font.o \ frotz/config.o \ frotz/detection.o \ -- cgit v1.2.3