| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <inttypes.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "animations.h" |
| 14 |
#include "common.h" |
| 15 |
#include "glk.h" |
| 16 |
#include "graphics.h" |
| 17 |
#include "parseinput.h" |
| 18 |
#include "restorestate.h" |
| 19 |
|
| 20 |
#define MAX_UNDOS 100 |
| 21 |
|
| 22 |
|
| 23 |
extern int JustStarted; |
| 24 |
|
| 25 |
extern uint8_t Flag[]; |
| 26 |
extern uint8_t ObjectLoc[]; |
| 27 |
|
| 28 |
extern winid_t Bottom; |
| 29 |
|
| 30 |
int JustRestored = 0; |
| 31 |
int JustRestarted = 0; |
| 32 |
ImgType SavedImgType; |
| 33 |
int SavedImgIndex; |
| 34 |
|
| 35 |
struct SavedState *InitialState = NULL; |
| 36 |
static struct SavedState *ramsave = NULL; |
| 37 |
static struct SavedState *last_undo = NULL; |
| 38 |
static struct SavedState *oldest_undo = NULL; |
| 39 |
|
| 40 |
static int number_of_undos; |
| 41 |
|
| 42 |
struct SavedState *SaveCurrentState(void) |
| 43 |
{ |
| 44 |
struct SavedState *s = (struct SavedState *)MemAlloc(sizeof(struct SavedState)); |
| 45 |
|
| 46 |
memcpy(s->Counters, Counters, 64 * sizeof(uint16_t)); |
| 47 |
for (int i = 0; i <= GameHeader.NumItems; i++) |
| 48 |
s->ObjectLocations[i] = Items[i].Location; |
| 49 |
|
| 50 |
s->BitFlags = BitFlags; |
| 51 |
s->ProtagonistString = ProtagonistString; |
| 52 |
s->LastImgType = LastImgType; |
| 53 |
s->LastImgIndex = LastImgIndex; |
| 54 |
|
| 55 |
s->previousState = NULL; |
| 56 |
s->nextState = NULL; |
| 57 |
|
| 58 |
return s; |
| 59 |
} |
| 60 |
|
| 61 |
void RecoverFromBadRestore(struct SavedState *state) |
| 62 |
{ |
| 63 |
SystemMessage(BAD_DATA); |
| 64 |
RestoreState(state); |
| 65 |
free(state); |
| 66 |
} |
| 67 |
|
| 68 |
void RestoreState(struct SavedState *state) |
| 69 |
{ |
| 70 |
StopAnimation(); |
| 71 |
memcpy(Counters, state->Counters, 64 * sizeof(uint16_t)); |
| 72 |
|
| 73 |
for (int i = 0; i <= GameHeader.NumItems; i++) |
| 74 |
Items[i].Location = state->ObjectLocations[i]; |
| 75 |
|
| 76 |
BitFlags = state->BitFlags; |
| 77 |
ProtagonistString = state->ProtagonistString; |
| 78 |
|
| 79 |
SetBit(DRAWBIT); |
| 80 |
SetBit(STOPTIMEBIT); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if (CurrentGame == SPIDERMAN) { |
| 87 |
ResetBit(9); |
| 88 |
ResetBit(10); |
| 89 |
} |
| 90 |
if (JustRestarted && CurrentGame != BANZAI) |
| 91 |
Look(0); |
| 92 |
else |
| 93 |
Look(1); |
| 94 |
if (CurrentGame == FANTASTIC4) { |
| 95 |
LastImgType = state->LastImgType; |
| 96 |
LastImgIndex = state->LastImgIndex; |
| 97 |
if (LastImgType == IMG_SPECIAL) { |
| 98 |
DrawCloseup(LastImgIndex); |
| 99 |
} else if (LastImgType == IMG_OBJECT) { |
| 100 |
DrawItemImage(LastImgIndex); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void SaveUndo(void) |
| 106 |
{ |
| 107 |
if (last_undo == NULL) { |
| 108 |
last_undo = SaveCurrentState(); |
| 109 |
oldest_undo = last_undo; |
| 110 |
number_of_undos = 1; |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
if (number_of_undos == 0) |
| 115 |
Fatal("Number of undos == 0 but last_undo != NULL!"); |
| 116 |
|
| 117 |
last_undo->nextState = SaveCurrentState(); |
| 118 |
struct SavedState *current = last_undo->nextState; |
| 119 |
current->previousState = last_undo; |
| 120 |
last_undo = current; |
| 121 |
if (number_of_undos == MAX_UNDOS) { |
| 122 |
struct SavedState *oldest = oldest_undo; |
| 123 |
oldest_undo = oldest_undo->nextState; |
| 124 |
oldest_undo->previousState = NULL; |
| 125 |
free(oldest); |
| 126 |
} else { |
| 127 |
number_of_undos++; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
void RestoreUndo(int game) |
| 132 |
{ |
| 133 |
if (JustStarted) { |
| 134 |
SystemMessage(CANT_UNDO_ON_FIRST_TURN); |
| 135 |
return; |
| 136 |
} |
| 137 |
if (last_undo == NULL || last_undo->previousState == NULL) { |
| 138 |
SystemMessage(NO_UNDO_STATES); |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
if (game) |
| 143 |
SystemMessage(MOVE_UNDONE); |
| 144 |
|
| 145 |
struct SavedState *current = last_undo; |
| 146 |
last_undo = current->previousState; |
| 147 |
if (last_undo->previousState == NULL) |
| 148 |
oldest_undo = last_undo; |
| 149 |
RestoreState(last_undo); |
| 150 |
free(current); |
| 151 |
number_of_undos--; |
| 152 |
} |
| 153 |
|
| 154 |
void RamSave(int game) |
| 155 |
{ |
| 156 |
if (ramsave != NULL) { |
| 157 |
Display(Bottom, "Previous state deleted. "); |
| 158 |
free(ramsave); |
| 159 |
} |
| 160 |
|
| 161 |
ramsave = SaveCurrentState(); |
| 162 |
if (game) |
| 163 |
SystemMessage(STATE_SAVED); |
| 164 |
} |
| 165 |
|
| 166 |
void RamLoad(void) |
| 167 |
{ |
| 168 |
if (ramsave == NULL) { |
| 169 |
SystemMessage(NO_SAVED_STATE); |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
SystemMessage(STATE_RESTORED); |
| 174 |
RestoreState(ramsave); |
| 175 |
SaveUndo(); |
| 176 |
} |
| 177 |
|
| 178 |
void SaveGame(void) |
| 179 |
{ |
| 180 |
strid_t file; |
| 181 |
frefid_t ref; |
| 182 |
int ct, dummy = 0; |
| 183 |
char buf[128]; |
| 184 |
|
| 185 |
ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame, |
| 186 |
filemode_Write, 0); |
| 187 |
if (ref == NULL) |
| 188 |
return; |
| 189 |
|
| 190 |
file = glk_stream_open_file(ref, filemode_Write, 0); |
| 191 |
glk_fileref_destroy(ref); |
| 192 |
if (file == NULL) |
| 193 |
return; |
| 194 |
|
| 195 |
for (ct = 0; ct < 64; ct++) { |
| 196 |
snprintf(buf, sizeof buf, "%d\n", Counters[ct]); |
| 197 |
glk_put_string_stream(file, buf); |
| 198 |
} |
| 199 |
snprintf(buf, sizeof buf, "%llu %d %d %d %d\n", (unsigned long long)BitFlags, ProtagonistString, dummy, (int)LastImgType, LastImgIndex); |
| 200 |
glk_put_string_stream(file, buf); |
| 201 |
for (ct = 0; ct <= GameHeader.NumItems; ct++) { |
| 202 |
snprintf(buf, sizeof buf, "%hd\n", (short)Items[ct].Location); |
| 203 |
glk_put_string_stream(file, buf); |
| 204 |
} |
| 205 |
|
| 206 |
glk_stream_close(file, NULL); |
| 207 |
SystemMessage(SAVED); |
| 208 |
SetBit(STOPTIMEBIT); |
| 209 |
} |
| 210 |
|
| 211 |
int LoadGame(void) |
| 212 |
{ |
| 213 |
strid_t file; |
| 214 |
frefid_t ref; |
| 215 |
char buf[128]; |
| 216 |
int ct = 0, dummy; |
| 217 |
short lo; |
| 218 |
|
| 219 |
ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame, |
| 220 |
filemode_Read, 0); |
| 221 |
if (ref == NULL) |
| 222 |
return 0; |
| 223 |
|
| 224 |
file = glk_stream_open_file(ref, filemode_Read, 0); |
| 225 |
glk_fileref_destroy(ref); |
| 226 |
if (file == NULL) |
| 227 |
return 0; |
| 228 |
|
| 229 |
struct SavedState *state = SaveCurrentState(); |
| 230 |
|
| 231 |
int result; |
| 232 |
|
| 233 |
for (ct = 0; ct < 64; ct++) { |
| 234 |
glk_get_line_stream(file, buf, sizeof buf); |
| 235 |
result = sscanf(buf, "%" SCNu16, &Counters[ct]); |
| 236 |
if (result != 1) { |
| 237 |
RecoverFromBadRestore(state); |
| 238 |
return 0; |
| 239 |
} |
| 240 |
} |
| 241 |
glk_get_line_stream(file, buf, sizeof buf); |
| 242 |
int SavedImgTypeInt; |
| 243 |
result = sscanf(buf, "%" SCNu64 " %d %d %d %d\n", &BitFlags, &ProtagonistString, |
| 244 |
&dummy, &SavedImgTypeInt, &SavedImgIndex); |
| 245 |
SavedImgType = SavedImgTypeInt; |
| 246 |
debug_print("LoadGame: Result of sscanf: %d\n", result); |
| 247 |
if ((result < 3) || MyLoc > GameHeader.NumRooms || MyLoc < 1) { |
| 248 |
RecoverFromBadRestore(state); |
| 249 |
return 0; |
| 250 |
} |
| 251 |
|
| 252 |
for (ct = 0; ct <= GameHeader.NumItems; ct++) { |
| 253 |
glk_get_line_stream(file, buf, sizeof buf); |
| 254 |
result = sscanf(buf, "%hd\n", &lo); |
| 255 |
Items[ct].Location = (unsigned char)lo; |
| 256 |
if (result != 1 || (Items[ct].Location > GameHeader.NumRooms && Items[ct].Location != CARRIED && Items[ct].Location != HIDDEN && Items[ct].Location != HELD_BY_OTHER_GUY)) { |
| 257 |
fprintf(stderr, "LoadGame: Unexpected item location in save game file (Item %d, %s, is in room %d)\n", ct, Items[ct].Text, Items[ct].Location); |
| 258 |
RecoverFromBadRestore(state); |
| 259 |
return 0; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
glui32 position = glk_stream_get_position(file); |
| 264 |
glk_stream_set_position(file, 0, seekmode_End); |
| 265 |
glui32 end = glk_stream_get_position(file); |
| 266 |
if (end != position) { |
| 267 |
fprintf(stderr, "LoadGame: Save game file has unexpected length (%d, expected %d)\n", end, position); |
| 268 |
RecoverFromBadRestore(state); |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
JustStarted = 0; |
| 273 |
ClearAnimationBuffer(); |
| 274 |
LastImgType = SavedImgType; |
| 275 |
LastImgIndex = SavedImgIndex; |
| 276 |
|
| 277 |
SetBit(DRAWBIT); |
| 278 |
Look(0); |
| 279 |
|
| 280 |
if (LastImgType == IMG_SPECIAL) { |
| 281 |
DrawCloseup(LastImgIndex); |
| 282 |
} else if (LastImgType == IMG_OBJECT) { |
| 283 |
DrawItemImage(LastImgIndex); |
| 284 |
} |
| 285 |
|
| 286 |
SaveUndo(); |
| 287 |
JustRestored = 1; |
| 288 |
|
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
void RestartGame(void) |
| 293 |
{ |
| 294 |
FreeInputWords(); |
| 295 |
JustRestarted = 1; |
| 296 |
RestoreState(InitialState); |
| 297 |
JustStarted = 0; |
| 298 |
lastwasnewline = 1; |
| 299 |
ResetBit(STOPTIMEBIT); |
| 300 |
ClearAnimationBuffer(); |
| 301 |
glk_window_clear(Bottom); |
| 302 |
OpenTopWindow(); |
| 303 |
should_restart = 0; |
| 304 |
} |