aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/alan3/state.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'engines/glk/alan3/state.cpp')
-rw-r--r--engines/glk/alan3/state.cpp310
1 files changed, 155 insertions, 155 deletions
diff --git a/engines/glk/alan3/state.cpp b/engines/glk/alan3/state.cpp
index f7fc2eb0ee..0ebf52f3a4 100644
--- a/engines/glk/alan3/state.cpp
+++ b/engines/glk/alan3/state.cpp
@@ -39,22 +39,22 @@ namespace Alan3 {
/* Implementation of the abstract type typedef struct game_state GameState */
struct game_state {
- /* Event queue */
- EventQueueEntry *eventQueue;
- int eventQueueTop; /* Event queue top pointer */
-
- /* Scores */
- int score;
- Aword *scores; /* Score table pointer */
-
- /* Instance data */
- AdminEntry *admin; /* Administrative data about instances */
- AttributeEntry *attributes; /* Attributes data area */
- /* Sets and strings are dynamically allocated areas for which the
- attribute is just a pointer to. So they are not catched by the
- saving of attributes, instead they require special storage */
- Set **sets; /* Array of set pointers */
- char **strings; /* Array of string pointers */
+ /* Event queue */
+ EventQueueEntry *eventQueue;
+ int eventQueueTop; /* Event queue top pointer */
+
+ /* Scores */
+ int score;
+ Aword *scores; /* Score table pointer */
+
+ /* Instance data */
+ AdminEntry *admin; /* Administrative data about instances */
+ AttributeEntry *attributes; /* Attributes data area */
+ /* Sets and strings are dynamically allocated areas for which the
+ attribute is just a pointer to. So they are not catched by the
+ saving of attributes, instead they require special storage */
+ Set **sets; /* Array of set pointers */
+ char **strings; /* Array of string pointers */
};
/* PRIVATE DATA */
@@ -66,292 +66,292 @@ static char *playerCommand;
/*----------------------------------------------------------------------*/
static int countStrings(void) {
- StringInitEntry *entry;
- int count = 0;
+ StringInitEntry *entry;
+ int count = 0;
- if (header->stringInitTable != 0)
- for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); *(Aword *)entry != EOD; entry++)
- count++;
- return(count);
+ if (header->stringInitTable != 0)
+ for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); * (Aword *)entry != EOD; entry++)
+ count++;
+ return (count);
}
/*----------------------------------------------------------------------*/
static void deallocateStrings(GameState *gState) {
- int count = countStrings();
- int i;
+ int count = countStrings();
+ int i;
- for (i = 0; i < count; i++)
- deallocate(gState->strings[i]);
- deallocate(gState->strings);
+ for (i = 0; i < count; i++)
+ deallocate(gState->strings[i]);
+ deallocate(gState->strings);
}
/*----------------------------------------------------------------------*/
static int countSets(void) {
- SetInitEntry *entry;
- int count = 0;
+ SetInitEntry *entry;
+ int count = 0;
- if (header->setInitTable != 0)
- for (entry = (SetInitEntry *)pointerTo(header->setInitTable); *(Aword *)entry != EOD; entry++)
- count++;
- return(count);
+ if (header->setInitTable != 0)
+ for (entry = (SetInitEntry *)pointerTo(header->setInitTable); * (Aword *)entry != EOD; entry++)
+ count++;
+ return (count);
}
/*----------------------------------------------------------------------*/
static void deallocateSets(GameState *gState) {
- int count = countSets();
- int i;
+ int count = countSets();
+ int i;
- for (i = 0; i < count; i++)
- freeSet(gState->sets[i]);
- deallocate(gState->sets);
+ for (i = 0; i < count; i++)
+ freeSet(gState->sets[i]);
+ deallocate(gState->sets);
}
/*======================================================================*/
void deallocateGameState(GameState *gState) {
- deallocate(gState->admin);
- deallocate(gState->attributes);
+ deallocate(gState->admin);
+ deallocate(gState->attributes);
- if (gState->eventQueueTop > 0) {
- deallocate(gState->eventQueue);
- gState->eventQueue = NULL;
- }
- if (gState->scores)
- deallocate(gState->scores);
+ if (gState->eventQueueTop > 0) {
+ deallocate(gState->eventQueue);
+ gState->eventQueue = NULL;
+ }
+ if (gState->scores)
+ deallocate(gState->scores);
- deallocateStrings(gState);
- deallocateSets(gState);
+ deallocateStrings(gState);
+ deallocateSets(gState);
- memset(gState, 0, sizeof(GameState));
+ memset(gState, 0, sizeof(GameState));
}
/*======================================================================*/
void forgetGameState(void) {
- char *playerCmd;
- popGameState(stateStack, &gameState, &playerCmd);
- deallocateGameState(&gameState);
- if (playerCmd != NULL)
- deallocate(playerCmd);
+ char *playerCmd;
+ popGameState(stateStack, &gameState, &playerCmd);
+ deallocateGameState(&gameState);
+ if (playerCmd != NULL)
+ deallocate(playerCmd);
}
/*======================================================================*/
void initStateStack(void) {
- if (stateStack != NULL)
- deleteStateStack(stateStack);
- stateStack = createStateStack(sizeof(GameState));
+ if (stateStack != NULL)
+ deleteStateStack(stateStack);
+ stateStack = createStateStack(sizeof(GameState));
}
/*======================================================================*/
void terminateStateStack(void) {
- deleteStateStack(stateStack);
- stateStack = NULL;
+ deleteStateStack(stateStack);
+ stateStack = NULL;
}
/*======================================================================*/
bool anySavedState(void) {
- return !stateStackIsEmpty(stateStack);
+ return !stateStackIsEmpty(stateStack);
}
/*----------------------------------------------------------------------*/
static Set **collectSets(void) {
- SetInitEntry *entry;
- int count = countSets();
- Set **sets;
- int i;
+ SetInitEntry *entry;
+ int count = countSets();
+ Set **sets;
+ int i;
- if (count == 0) return NULL;
+ if (count == 0) return NULL;
- sets = (Set **)allocate(count*sizeof(Set));
+ sets = (Set **)allocate(count * sizeof(Set));
- entry = (SetInitEntry *)pointerTo(header->setInitTable);
- for (i = 0; i < count; i++)
- sets[i] = getInstanceSetAttribute(entry[i].instanceCode, entry[i].attributeCode);
+ entry = (SetInitEntry *)pointerTo(header->setInitTable);
+ for (i = 0; i < count; i++)
+ sets[i] = getInstanceSetAttribute(entry[i].instanceCode, entry[i].attributeCode);
- return sets;
+ return sets;
}
/*----------------------------------------------------------------------*/
static char **collectStrings(void) {
- StringInitEntry *entry;
- int count = countStrings();
- char **strings;
- int i;
+ StringInitEntry *entry;
+ int count = countStrings();
+ char **strings;
+ int i;
- if (count == 0) return NULL;
+ if (count == 0) return NULL;
- strings = (char **)allocate(count*sizeof(char *));
+ strings = (char **)allocate(count * sizeof(char *));
- entry = (StringInitEntry *)pointerTo(header->stringInitTable);
- for (i = 0; i < count; i++)
- strings[i] = getInstanceStringAttribute(entry[i].instanceCode, entry[i].attributeCode);
+ entry = (StringInitEntry *)pointerTo(header->stringInitTable);
+ for (i = 0; i < count; i++)
+ strings[i] = getInstanceStringAttribute(entry[i].instanceCode, entry[i].attributeCode);
- return strings;
+ return strings;
}
/*======================================================================*/
void rememberCommands(void) {
- char *command = playerWordsAsCommandString();
- attachPlayerCommandsToLastState(stateStack, command);
- deallocate(command);
+ char *command = playerWordsAsCommandString();
+ attachPlayerCommandsToLastState(stateStack, command);
+ deallocate(command);
}
/*----------------------------------------------------------------------*/
static void collectEvents(void) {
- gameState.eventQueueTop = eventQueueTop;
- if (eventQueueTop > 0)
- gameState.eventQueue = (EventQueueEntry *)duplicate(eventQueue, eventQueueTop*sizeof(EventQueueEntry));
+ gameState.eventQueueTop = eventQueueTop;
+ if (eventQueueTop > 0)
+ gameState.eventQueue = (EventQueueEntry *)duplicate(eventQueue, eventQueueTop * sizeof(EventQueueEntry));
}
/*----------------------------------------------------------------------*/
static void collectInstanceData(void) {
- gameState.admin = (AdminEntry *)duplicate(admin, (header->instanceMax+1)*sizeof(AdminEntry));
- gameState.attributes = (AttributeEntry *)duplicate(attributes, header->attributesAreaSize*sizeof(Aword));
- gameState.sets = collectSets();
- gameState.strings = collectStrings();
+ gameState.admin = (AdminEntry *)duplicate(admin, (header->instanceMax + 1) * sizeof(AdminEntry));
+ gameState.attributes = (AttributeEntry *)duplicate(attributes, header->attributesAreaSize * sizeof(Aword));
+ gameState.sets = collectSets();
+ gameState.strings = collectStrings();
}
/*----------------------------------------------------------------------*/
static void collectScores(void) {
- gameState.score = current.score;
- if (scores == NULL)
- gameState.scores = NULL;
- else
- gameState.scores = (Aword *)duplicate(scores, header->scoreCount*sizeof(Aword));
+ gameState.score = current.score;
+ if (scores == NULL)
+ gameState.scores = NULL;
+ else
+ gameState.scores = (Aword *)duplicate(scores, header->scoreCount * sizeof(Aword));
}
/*======================================================================*/
void rememberGameState(void) {
- collectEvents();
- collectInstanceData();
- collectScores();
+ collectEvents();
+ collectInstanceData();
+ collectScores();
- if (stateStack == NULL)
- initStateStack();
+ if (stateStack == NULL)
+ initStateStack();
- pushGameState(stateStack, &gameState);
- gameStateChanged = FALSE;
+ pushGameState(stateStack, &gameState);
+ gameStateChanged = FALSE;
}
/*----------------------------------------------------------------------*/
static void freeCurrentSetAttributes(void) {
- SetInitEntry *entry;
+ SetInitEntry *entry;
- if (header->setInitTable == 0) return;
- for (entry = (SetInitEntry *)pointerTo(header->setInitTable); *(Aword *)entry != EOD; entry++) {
- Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode);
- freeSet((Set*)fromAptr(attributeValue));
- }
+ if (header->setInitTable == 0) return;
+ for (entry = (SetInitEntry *)pointerTo(header->setInitTable); * (Aword *)entry != EOD; entry++) {
+ Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode);
+ freeSet((Set *)fromAptr(attributeValue));
+ }
}
/*----------------------------------------------------------------------*/
static void recallSets(Set **sets) {
- SetInitEntry *entry;
- int count = countSets();
- int i;
+ SetInitEntry *entry;
+ int count = countSets();
+ int i;
- if (header->setInitTable == 0) return;
+ if (header->setInitTable == 0) return;
- entry = (SetInitEntry *)pointerTo(header->setInitTable);
- for (i = 0; i < count; i++) {
- setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(sets[i]));
- sets[i] = NULL; /* Since we reuse the saved set, we need to clear the pointer */
- }
+ entry = (SetInitEntry *)pointerTo(header->setInitTable);
+ for (i = 0; i < count; i++) {
+ setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(sets[i]));
+ sets[i] = NULL; /* Since we reuse the saved set, we need to clear the pointer */
+ }
}
/*----------------------------------------------------------------------*/
static void freeCurrentStringAttributes(void) {
- StringInitEntry *entry;
+ StringInitEntry *entry;
- if (header->stringInitTable == 0) return;
- for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); *(Aword *)entry != EOD; entry++) {
- Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode);
- deallocate(fromAptr(attributeValue));
- }
+ if (header->stringInitTable == 0) return;
+ for (entry = (StringInitEntry *)pointerTo(header->stringInitTable); * (Aword *)entry != EOD; entry++) {
+ Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode);
+ deallocate(fromAptr(attributeValue));
+ }
}
/*----------------------------------------------------------------------*/
static void recallStrings(char **strings) {
- StringInitEntry *entry;
- int count = countStrings();
- int i;
+ StringInitEntry *entry;
+ int count = countStrings();
+ int i;
- if (header->stringInitTable == 0) return;
+ if (header->stringInitTable == 0) return;
- entry = (StringInitEntry *)pointerTo(header->stringInitTable);
- for (i = 0; i < count; i++) {
- setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(strings[i]));
- strings[i] = NULL; /* Since we reuse the saved, we need to clear the state */
- }
+ entry = (StringInitEntry *)pointerTo(header->stringInitTable);
+ for (i = 0; i < count; i++) {
+ setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(strings[i]));
+ strings[i] = NULL; /* Since we reuse the saved, we need to clear the state */
+ }
}
/*----------------------------------------------------------------------*/
static void recallEvents(void) {
- eventQueueTop = gameState.eventQueueTop;
- if (eventQueueTop > 0) {
- memcpy(eventQueue, gameState.eventQueue,
- (eventQueueTop+1)*sizeof(EventQueueEntry));
- }
+ eventQueueTop = gameState.eventQueueTop;
+ if (eventQueueTop > 0) {
+ memcpy(eventQueue, gameState.eventQueue,
+ (eventQueueTop + 1)*sizeof(EventQueueEntry));
+ }
}
/*----------------------------------------------------------------------*/
static void recallInstances(void) {
- if (admin == NULL)
- syserr("admin[] == NULL in recallInstances()");
+ if (admin == NULL)
+ syserr("admin[] == NULL in recallInstances()");
- memcpy(admin, gameState.admin,
- (header->instanceMax+1)*sizeof(AdminEntry));
+ memcpy(admin, gameState.admin,
+ (header->instanceMax + 1)*sizeof(AdminEntry));
- freeCurrentSetAttributes(); /* Need to free previous set values */
- freeCurrentStringAttributes(); /* Need to free previous string values */
+ freeCurrentSetAttributes(); /* Need to free previous set values */
+ freeCurrentStringAttributes(); /* Need to free previous string values */
- memcpy(attributes, gameState.attributes,
- header->attributesAreaSize*sizeof(Aword));
+ memcpy(attributes, gameState.attributes,
+ header->attributesAreaSize * sizeof(Aword));
- recallSets(gameState.sets);
- recallStrings(gameState.strings);
+ recallSets(gameState.sets);
+ recallStrings(gameState.strings);
}
/*----------------------------------------------------------------------*/
static void recallScores(void) {
- current.score = gameState.score;
- memcpy(scores, gameState.scores, header->scoreCount*sizeof(Aword));
+ current.score = gameState.score;
+ memcpy(scores, gameState.scores, header->scoreCount * sizeof(Aword));
}
/*======================================================================*/
void recallGameState(void) {
- popGameState(stateStack, &gameState, &playerCommand);
- recallEvents();
- recallInstances();
- recallScores();
- deallocateGameState(&gameState);
+ popGameState(stateStack, &gameState, &playerCommand);
+ recallEvents();
+ recallInstances();
+ recallScores();
+ deallocateGameState(&gameState);
}
/*======================================================================*/
char *recreatePlayerCommand(void) {
- return playerCommand;
+ return playerCommand;
}
} // End of namespace Alan3