| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <stdlib.h> |
| 9 |
|
| 10 |
#include "restorestate.h" |
| 11 |
|
| 12 |
#include "scott.h" |
| 13 |
|
| 14 |
#define MAX_UNDOS 100 |
| 15 |
|
| 16 |
extern int CurrentCounter; |
| 17 |
extern int RoomSaved[]; |
| 18 |
|
| 19 |
extern int StopTime; |
| 20 |
extern int JustStarted; |
| 21 |
|
| 22 |
struct SavedState *InitialState = NULL; |
| 23 |
static struct SavedState *ramsave = NULL; |
| 24 |
static struct SavedState *last_undo = NULL; |
| 25 |
static struct SavedState *oldest_undo = NULL; |
| 26 |
|
| 27 |
static int number_of_undos; |
| 28 |
|
| 29 |
struct SavedState *SaveCurrentState(void) |
| 30 |
{ |
| 31 |
struct SavedState *s = (struct SavedState *)MemAlloc(sizeof(struct SavedState)); |
| 32 |
for (int ct = 0; ct < 16; ct++) { |
| 33 |
s->Counters[ct] = Counters[ct]; |
| 34 |
s->RoomSaved[ct] = RoomSaved[ct]; |
| 35 |
} |
| 36 |
|
| 37 |
s->BitFlags = BitFlags; |
| 38 |
s->CurrentLoc = MyLoc; |
| 39 |
s->CurrentCounter = CurrentCounter; |
| 40 |
s->SavedRoom = SavedRoom; |
| 41 |
s->LightTime = GameHeader.LightTime; |
| 42 |
s->AutoInventory = AutoInventory; |
| 43 |
|
| 44 |
s->ItemLocations = MemAlloc(GameHeader.NumItems + 1); |
| 45 |
|
| 46 |
for (int ct = 0; ct <= GameHeader.NumItems; ct++) { |
| 47 |
s->ItemLocations[ct] = Items[ct].Location; |
| 48 |
} |
| 49 |
|
| 50 |
s->previousState = NULL; |
| 51 |
s->nextState = NULL; |
| 52 |
|
| 53 |
return s; |
| 54 |
} |
| 55 |
|
| 56 |
void RecoverFromBadRestore(struct SavedState *state) |
| 57 |
{ |
| 58 |
Output(sys[BAD_DATA]); |
| 59 |
RestoreState(state); |
| 60 |
free(state); |
| 61 |
} |
| 62 |
|
| 63 |
void RestoreState(struct SavedState *state) |
| 64 |
{ |
| 65 |
for (int ct = 0; ct < 16; ct++) { |
| 66 |
Counters[ct] = state->Counters[ct]; |
| 67 |
RoomSaved[ct] = state->RoomSaved[ct]; |
| 68 |
} |
| 69 |
|
| 70 |
BitFlags = state->BitFlags; |
| 71 |
|
| 72 |
MyLoc = state->CurrentLoc; |
| 73 |
CurrentCounter = state->CurrentCounter; |
| 74 |
SavedRoom = state->SavedRoom; |
| 75 |
GameHeader.LightTime = state->LightTime; |
| 76 |
AutoInventory = state->AutoInventory; |
| 77 |
|
| 78 |
for (int ct = 0; ct <= GameHeader.NumItems; ct++) { |
| 79 |
Items[ct].Location = state->ItemLocations[ct]; |
| 80 |
} |
| 81 |
|
| 82 |
if (StopTime == 0) |
| 83 |
StopTime = 1; |
| 84 |
} |
| 85 |
|
| 86 |
void SaveUndo(void) |
| 87 |
{ |
| 88 |
if (last_undo == NULL) { |
| 89 |
last_undo = SaveCurrentState(); |
| 90 |
oldest_undo = last_undo; |
| 91 |
number_of_undos = 1; |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if (number_of_undos == 0) |
| 96 |
Fatal("Number of undos == 0 but last_undo != NULL!"); |
| 97 |
|
| 98 |
last_undo->nextState = SaveCurrentState(); |
| 99 |
struct SavedState *current = last_undo->nextState; |
| 100 |
current->previousState = last_undo; |
| 101 |
last_undo = current; |
| 102 |
if (number_of_undos == MAX_UNDOS) { |
| 103 |
struct SavedState *oldest = oldest_undo; |
| 104 |
oldest_undo = oldest_undo->nextState; |
| 105 |
oldest_undo->previousState = NULL; |
| 106 |
free(oldest->ItemLocations); |
| 107 |
free(oldest); |
| 108 |
} else { |
| 109 |
number_of_undos++; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void RestoreUndo(void) |
| 114 |
{ |
| 115 |
if (JustStarted) { |
| 116 |
Output(sys[CANT_UNDO_ON_FIRST_TURN]); |
| 117 |
return; |
| 118 |
} |
| 119 |
if (last_undo == NULL || last_undo->previousState == NULL) { |
| 120 |
Output(sys[NO_UNDO_STATES]); |
| 121 |
return; |
| 122 |
} |
| 123 |
struct SavedState *current = last_undo; |
| 124 |
last_undo = current->previousState; |
| 125 |
if (last_undo->previousState == NULL) |
| 126 |
oldest_undo = last_undo; |
| 127 |
RestoreState(last_undo); |
| 128 |
Output(sys[MOVE_UNDONE]); |
| 129 |
free(current->ItemLocations); |
| 130 |
free(current); |
| 131 |
number_of_undos--; |
| 132 |
} |
| 133 |
|
| 134 |
void RamSave(void) |
| 135 |
{ |
| 136 |
if (ramsave != NULL) { |
| 137 |
free(ramsave->ItemLocations); |
| 138 |
free(ramsave); |
| 139 |
} |
| 140 |
|
| 141 |
ramsave = SaveCurrentState(); |
| 142 |
Output(sys[STATE_SAVED]); |
| 143 |
} |
| 144 |
|
| 145 |
void RamRestore(void) |
| 146 |
{ |
| 147 |
if (ramsave == NULL) { |
| 148 |
Output(sys[NO_SAVED_STATE]); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
RestoreState(ramsave); |
| 153 |
Output(sys[STATE_RESTORED]); |
| 154 |
SaveUndo(); |
| 155 |
} |