| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include "glk.h" |
| 11 |
#include "restorestate.h" |
| 12 |
#include "utility.h" |
| 13 |
|
| 14 |
#define MAX_UNDOS 100 |
| 15 |
|
| 16 |
extern int StopTime; |
| 17 |
extern int JustStarted; |
| 18 |
int just_undid = 0; |
| 19 |
|
| 20 |
extern uint8_t Flag[]; |
| 21 |
extern uint8_t ObjectLoc[]; |
| 22 |
|
| 23 |
extern winid_t Bottom; |
| 24 |
|
| 25 |
struct SavedState *InitialState = NULL; |
| 26 |
static struct SavedState *ramsave = NULL; |
| 27 |
static struct SavedState *last_undo = NULL; |
| 28 |
static struct SavedState *oldest_undo = NULL; |
| 29 |
|
| 30 |
static int number_of_undos; |
| 31 |
|
| 32 |
struct SavedState *SaveCurrentState(void) |
| 33 |
{ |
| 34 |
struct SavedState *s = (struct SavedState *)MemAlloc(sizeof(struct SavedState)); |
| 35 |
|
| 36 |
memcpy(s->Flags, Flag, 128); |
| 37 |
memcpy(s->ObjectLocations, ObjectLoc, 256); |
| 38 |
|
| 39 |
s->previousState = NULL; |
| 40 |
s->nextState = NULL; |
| 41 |
|
| 42 |
return s; |
| 43 |
} |
| 44 |
|
| 45 |
void RecoverFromBadRestore(struct SavedState *state) |
| 46 |
{ |
| 47 |
Display(Bottom, "BAD DATA! Invalid save file.\n"); |
| 48 |
RestoreState(state); |
| 49 |
free(state); |
| 50 |
} |
| 51 |
|
| 52 |
void RestoreState(struct SavedState *state) |
| 53 |
{ |
| 54 |
memcpy(Flag, state->Flags, 128); |
| 55 |
memcpy(ObjectLoc, state->ObjectLocations, 256); |
| 56 |
|
| 57 |
StopTime = 1; |
| 58 |
} |
| 59 |
|
| 60 |
void SaveUndo(void) |
| 61 |
{ |
| 62 |
if (just_undid) { |
| 63 |
just_undid = 0; |
| 64 |
return; |
| 65 |
} |
| 66 |
if (last_undo == NULL) { |
| 67 |
last_undo = SaveCurrentState(); |
| 68 |
oldest_undo = last_undo; |
| 69 |
number_of_undos = 1; |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
if (number_of_undos == 0) |
| 74 |
Fatal("Number of undos == 0 but last_undo != NULL!"); |
| 75 |
|
| 76 |
last_undo->nextState = SaveCurrentState(); |
| 77 |
struct SavedState *current = last_undo->nextState; |
| 78 |
current->previousState = last_undo; |
| 79 |
last_undo = current; |
| 80 |
if (number_of_undos == MAX_UNDOS) { |
| 81 |
struct SavedState *oldest = oldest_undo; |
| 82 |
oldest_undo = oldest_undo->nextState; |
| 83 |
oldest_undo->previousState = NULL; |
| 84 |
free(oldest); |
| 85 |
} else { |
| 86 |
number_of_undos++; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void RestoreUndo(int from_player) |
| 91 |
{ |
| 92 |
just_undid = 1; |
| 93 |
|
| 94 |
if (JustStarted) { |
| 95 |
Display(Bottom, "You can't undo on first turn\n"); |
| 96 |
return; |
| 97 |
} |
| 98 |
if (last_undo == NULL || last_undo->previousState == NULL) { |
| 99 |
Display(Bottom, "No undo states remaining\n"); |
| 100 |
return; |
| 101 |
} |
| 102 |
struct SavedState *current = last_undo; |
| 103 |
last_undo = current->previousState; |
| 104 |
if (last_undo->previousState == NULL) |
| 105 |
oldest_undo = last_undo; |
| 106 |
RestoreState(last_undo); |
| 107 |
if (from_player) { |
| 108 |
Display(Bottom, "Move undone.\n"); |
| 109 |
} else { |
| 110 |
just_undid = 0; |
| 111 |
} |
| 112 |
free(current); |
| 113 |
number_of_undos--; |
| 114 |
} |
| 115 |
|
| 116 |
void RamSave(int game) |
| 117 |
{ |
| 118 |
if (ramsave != NULL) { |
| 119 |
Display(Bottom, "Previous state deleted. "); |
| 120 |
free(ramsave); |
| 121 |
} |
| 122 |
|
| 123 |
ramsave = SaveCurrentState(); |
| 124 |
if (game) |
| 125 |
Display(Bottom, "State saved.\n"); |
| 126 |
} |
| 127 |
|
| 128 |
void RamLoad(void) |
| 129 |
{ |
| 130 |
if (ramsave == NULL) { |
| 131 |
Display(Bottom, "No saved state exists.\n"); |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
RestoreState(ramsave); |
| 136 |
Display(Bottom, "State restored.\n"); |
| 137 |
SaveUndo(); |
| 138 |
} |