| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "state.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "syserr.h" |
| 13 |
#include "current.h" |
| 14 |
#include "word.h" |
| 15 |
#include "StateStack.h" |
| 16 |
#include "instance.h" |
| 17 |
#include "attribute.h" |
| 18 |
#include "memory.h" |
| 19 |
#include "score.h" |
| 20 |
#include "event.h" |
| 21 |
#include "set.h" |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
struct game_state { |
| 31 |
|
| 32 |
EventQueueEntry *eventQueue; |
| 33 |
int eventQueueTop; |
| 34 |
|
| 35 |
|
| 36 |
int score; |
| 37 |
Aword *scores; |
| 38 |
|
| 39 |
|
| 40 |
AdminEntry *admin; |
| 41 |
AttributeEntry *attributes; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
Set **sets; |
| 46 |
char **strings; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
static GameState gameState; |
| 51 |
static StateStackP stateStack = NULL; |
| 52 |
|
| 53 |
static char *playerCommand; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static int countStrings(void) { |
| 58 |
StringInitEntry *entry; |
| 59 |
int count = 0; |
| 60 |
|
| 61 |
if (header->stringInitTable != 0) |
| 62 |
for (entry = pointerTo(header->stringInitTable); *(Aword *)entry != EOF; entry++) |
| 63 |
count++; |
| 64 |
return(count); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static void deallocateStrings(GameState *gameState) { |
| 70 |
int count = countStrings(); |
| 71 |
int i; |
| 72 |
|
| 73 |
for (i = 0; i < count; i++) |
| 74 |
deallocate(gameState->strings[i]); |
| 75 |
deallocate(gameState->strings); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
static int countSets(void) { |
| 80 |
SetInitEntry *entry; |
| 81 |
int count = 0; |
| 82 |
|
| 83 |
if (header->setInitTable != 0) |
| 84 |
for (entry = pointerTo(header->setInitTable); *(Aword *)entry != EOF; entry++) |
| 85 |
count++; |
| 86 |
return(count); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static void deallocateSets(GameState *gameState) { |
| 92 |
int count = countSets(); |
| 93 |
int i; |
| 94 |
|
| 95 |
for (i = 0; i < count; i++) |
| 96 |
freeSet(gameState->sets[i]); |
| 97 |
deallocate(gameState->sets); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
void deallocateGameState(GameState *gameState) { |
| 102 |
|
| 103 |
deallocate(gameState->admin); |
| 104 |
deallocate(gameState->attributes); |
| 105 |
|
| 106 |
if (gameState->eventQueueTop > 0) { |
| 107 |
deallocate(gameState->eventQueue); |
| 108 |
gameState->eventQueue = NULL; |
| 109 |
} |
| 110 |
if (gameState->scores) |
| 111 |
deallocate(gameState->scores); |
| 112 |
|
| 113 |
deallocateStrings(gameState); |
| 114 |
deallocateSets(gameState); |
| 115 |
|
| 116 |
memset(gameState, 0, sizeof(GameState)); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
void forgetGameState(void) { |
| 122 |
char *playerCommand; |
| 123 |
popGameState(stateStack, &gameState, &playerCommand); |
| 124 |
deallocateGameState(&gameState); |
| 125 |
if (playerCommand != NULL) |
| 126 |
deallocate(playerCommand); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
void initStateStack(void) { |
| 132 |
if (stateStack != NULL) |
| 133 |
deleteStateStack(stateStack); |
| 134 |
stateStack = createStateStack(sizeof(GameState)); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
void terminateStateStack(void) { |
| 140 |
deleteStateStack(stateStack); |
| 141 |
stateStack = NULL; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
bool anySavedState(void) { |
| 147 |
return !stateStackIsEmpty(stateStack); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static Set **collectSets(void) { |
| 153 |
SetInitEntry *entry; |
| 154 |
int count = countSets(); |
| 155 |
Set **sets; |
| 156 |
int i; |
| 157 |
|
| 158 |
if (count == 0) return NULL; |
| 159 |
|
| 160 |
sets = allocate(count*sizeof(Set)); |
| 161 |
|
| 162 |
entry = pointerTo(header->setInitTable); |
| 163 |
for (i = 0; i < count; i++) |
| 164 |
sets[i] = getInstanceSetAttribute(entry[i].instanceCode, entry[i].attributeCode); |
| 165 |
|
| 166 |
return sets; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
static char **collectStrings(void) { |
| 172 |
StringInitEntry *entry; |
| 173 |
int count = countStrings(); |
| 174 |
char **strings; |
| 175 |
int i; |
| 176 |
|
| 177 |
if (count == 0) return NULL; |
| 178 |
|
| 179 |
strings = allocate(count*sizeof(char *)); |
| 180 |
|
| 181 |
entry = pointerTo(header->stringInitTable); |
| 182 |
for (i = 0; i < count; i++) |
| 183 |
strings[i] = getInstanceStringAttribute(entry[i].instanceCode, entry[i].attributeCode); |
| 184 |
|
| 185 |
return strings; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
void rememberCommands(void) { |
| 191 |
char *command = playerWordsAsCommandString(); |
| 192 |
attachPlayerCommandsToLastState(stateStack, command); |
| 193 |
deallocate(command); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static void collectEvents(void) { |
| 199 |
gameState.eventQueueTop = eventQueueTop; |
| 200 |
if (eventQueueTop > 0) |
| 201 |
gameState.eventQueue = duplicate(eventQueue, eventQueueTop*sizeof(EventQueueEntry)); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
static void collectInstanceData(void) { |
| 207 |
gameState.admin = duplicate(admin, (header->instanceMax+1)*sizeof(AdminEntry)); |
| 208 |
gameState.attributes = duplicate(attributes, header->attributesAreaSize*sizeof(Aword)); |
| 209 |
gameState.sets = collectSets(); |
| 210 |
gameState.strings = collectStrings(); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
static void collectScores(void) { |
| 216 |
gameState.score = current.score; |
| 217 |
if (scores == NULL) |
| 218 |
gameState.scores = NULL; |
| 219 |
else |
| 220 |
gameState.scores = duplicate(scores, header->scoreCount*sizeof(Aword)); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
void rememberGameState(void) { |
| 226 |
collectEvents(); |
| 227 |
collectInstanceData(); |
| 228 |
collectScores(); |
| 229 |
|
| 230 |
if (stateStack == NULL) |
| 231 |
initStateStack(); |
| 232 |
|
| 233 |
pushGameState(stateStack, &gameState); |
| 234 |
gameStateChanged = false; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
static void freeCurrentSetAttributes(void) { |
| 240 |
SetInitEntry *entry; |
| 241 |
|
| 242 |
if (header->setInitTable == 0) return; |
| 243 |
for (entry = pointerTo(header->setInitTable); *(Aword *)entry != EOF; entry++) { |
| 244 |
Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode); |
| 245 |
freeSet((Set*)fromAptr(attributeValue)); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static void recallSets(Set **sets) { |
| 252 |
SetInitEntry *entry; |
| 253 |
int count = countSets(); |
| 254 |
int i; |
| 255 |
|
| 256 |
if (header->setInitTable == 0) return; |
| 257 |
|
| 258 |
entry = pointerTo(header->setInitTable); |
| 259 |
for (i = 0; i < count; i++) { |
| 260 |
setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(sets[i])); |
| 261 |
sets[i] = NULL; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static void freeCurrentStringAttributes(void) { |
| 268 |
StringInitEntry *entry; |
| 269 |
|
| 270 |
if (header->stringInitTable == 0) return; |
| 271 |
for (entry = pointerTo(header->stringInitTable); *(Aword *)entry != EOF; entry++) { |
| 272 |
Aptr attributeValue = getAttribute(admin[entry->instanceCode].attributes, entry->attributeCode); |
| 273 |
deallocate(fromAptr(attributeValue)); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
static void recallStrings(char **strings) { |
| 280 |
StringInitEntry *entry; |
| 281 |
int count = countStrings(); |
| 282 |
int i; |
| 283 |
|
| 284 |
if (header->stringInitTable == 0) return; |
| 285 |
|
| 286 |
entry = pointerTo(header->stringInitTable); |
| 287 |
for (i = 0; i < count; i++) { |
| 288 |
setAttribute(admin[entry[i].instanceCode].attributes, entry[i].attributeCode, toAptr(strings[i])); |
| 289 |
strings[i] = NULL; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static void recallEvents(void) { |
| 296 |
eventQueueTop = gameState.eventQueueTop; |
| 297 |
if (eventQueueTop > 0) { |
| 298 |
memcpy(eventQueue, gameState.eventQueue, |
| 299 |
(eventQueueTop+1)*sizeof(EventQueueEntry)); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
static void recallInstances(void) { |
| 306 |
|
| 307 |
if (admin == NULL) |
| 308 |
syserr("admin[] == NULL in recallInstances()"); |
| 309 |
|
| 310 |
memcpy(admin, gameState.admin, |
| 311 |
(header->instanceMax+1)*sizeof(AdminEntry)); |
| 312 |
|
| 313 |
freeCurrentSetAttributes(); |
| 314 |
freeCurrentStringAttributes(); |
| 315 |
|
| 316 |
memcpy(attributes, gameState.attributes, |
| 317 |
header->attributesAreaSize*sizeof(Aword)); |
| 318 |
|
| 319 |
recallSets(gameState.sets); |
| 320 |
recallStrings(gameState.strings); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
static void recallScores(void) { |
| 326 |
current.score = gameState.score; |
| 327 |
memcpy(scores, gameState.scores, header->scoreCount*sizeof(Aword)); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
void recallGameState(void) { |
| 333 |
popGameState(stateStack, &gameState, &playerCommand); |
| 334 |
recallEvents(); |
| 335 |
recallInstances(); |
| 336 |
recallScores(); |
| 337 |
deallocateGameState(&gameState); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
char *recreatePlayerCommand(void) { |
| 343 |
return playerCommand; |
| 344 |
} |