| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "StateStack.h" |
| 7 |
|
| 8 |
|
| 9 |
#include "syserr.h" |
| 10 |
#include "memory.h" |
| 11 |
#include "state.h" |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define EXTENT 10 |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
typedef struct StateStackStructure { |
| 20 |
void **states; |
| 21 |
char **commands; |
| 22 |
int stackSize; |
| 23 |
int stackPointer; |
| 24 |
int elementSize; |
| 25 |
} StateStackStructure; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
static void *reallocate(void *from, int newSize) |
| 30 |
{ |
| 31 |
void *newArea = realloc(from, newSize*sizeof(void*)); |
| 32 |
if (newArea == NULL) |
| 33 |
syserr("Out of memory in 'reallocateStack()'"); |
| 34 |
return newArea; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
StateStackP createStateStack(int elementSize) { |
| 39 |
StateStackP stack = NEW(StateStackStructure); |
| 40 |
stack->stackSize = 0; |
| 41 |
stack->stackPointer = 0; |
| 42 |
stack->elementSize = elementSize; |
| 43 |
return stack; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
void deleteStateStack(StateStackP stateStack) { |
| 49 |
if (stateStack != NULL) { |
| 50 |
while (stateStack->stackPointer > 0) { |
| 51 |
stateStack->stackPointer--; |
| 52 |
deallocateGameState(stateStack->states[stateStack->stackPointer]); |
| 53 |
deallocate(stateStack->states[stateStack->stackPointer]); |
| 54 |
deallocate(stateStack->commands[stateStack->stackPointer]); |
| 55 |
} |
| 56 |
if (stateStack->stackSize > 0) { |
| 57 |
deallocate(stateStack->states); |
| 58 |
deallocate(stateStack->commands); |
| 59 |
} |
| 60 |
deallocate(stateStack); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
bool stateStackIsEmpty(StateStackP stateStack) { |
| 67 |
return stateStack->stackPointer == 0; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
static void ensureSpaceForGameState(StateStackP stack) |
| 73 |
{ |
| 74 |
if (stack->stackPointer == stack->stackSize) { |
| 75 |
stack->states = reallocate(stack->states, stack->stackSize+EXTENT); |
| 76 |
stack->commands = reallocate(stack->commands, stack->stackSize+EXTENT); |
| 77 |
stack->stackSize += EXTENT; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void pushGameState(StateStackP stateStack, void *gameState) { |
| 84 |
void *element = allocate(stateStack->elementSize); |
| 85 |
memcpy(element, gameState, stateStack->elementSize); |
| 86 |
ensureSpaceForGameState(stateStack); |
| 87 |
stateStack->commands[stateStack->stackPointer] = NULL; |
| 88 |
stateStack->states[stateStack->stackPointer++] = element; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
void attachPlayerCommandsToLastState(StateStackP stateStack, char *playerCommands) { |
| 94 |
stateStack->commands[stateStack->stackPointer-1] = strdup(playerCommands); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
void popGameState(StateStackP stateStack, void *gameState, char** playerCommand) { |
| 100 |
if (stateStack->stackPointer == 0) |
| 101 |
syserr("Popping GameState from empty stack"); |
| 102 |
else { |
| 103 |
stateStack->stackPointer--; |
| 104 |
memcpy(gameState, stateStack->states[stateStack->stackPointer], stateStack->elementSize); |
| 105 |
deallocate(stateStack->states[stateStack->stackPointer]); |
| 106 |
*playerCommand = stateStack->commands[stateStack->stackPointer]; |
| 107 |
} |
| 108 |
} |