aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/savegame.cpp
blob: c5d349907155bf75f24b8fc1af968604656485c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on Labyrinth of Time code with assistance of
 *
 * Copyright (c) 1993 Terra Nova Development
 * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
 *
 */

#include "lab/lab.h"
#include "lab/stddefines.h"
#include "lab/labfun.h"
#include "lab/modernsavegame.h"

namespace Lab {

/* The version string */
#if defined(DOSCODE)
#define SAVEVERSION         "LBS2"
#else
#define SAVEVERSION         "LBS3"
#define SAVEVERSION_COMPAT  "LBS2"
#endif

#define BOOKMARK  0
#define CARDMARK  1
#define FLOPPY    2

typedef void *LABFH;
#define INVALID_LABFH   NULL

uint16 FileType, FileNum;



/*----- The Amiga specific area of saveGame.c -----*/


/*****************************************************************************/
/* Opens a file to write to from disk.                                       */
/*****************************************************************************/
static LABFH saveGameOpen(char *filename, bool iswrite) {
	warning("STUB: saveGameOpen");
	return 0;

#if 0
	if (iswrite) {
		unlink(filename);
		return fopen(filename, "wb");
	} else
		return fopen(filename, "rb");
#endif
}




/*****************************************************************************/
/* Closes a file.                                                            */
/*****************************************************************************/
static void saveGameClose(LABFH file, bool iswrite) {
	warning("STUB: saveGameClose");
	return;

#if 0
	if (file != INVALID_LABFH)
		fclose(file);
#endif
}




/*****************************************************************************/
/* Writes a block of memory to whatever it is that we're writing to.         */
/*****************************************************************************/
static void saveGameWriteBlock(LABFH file, void *data, uint32 size) {
	warning("STUB: saveGameWriteBlock");
	return;

	//fwrite(data, 1, size, file);
}



/*****************************************************************************/
/* Writes a block of memory to whatever it is that we're writing to.         */
/*****************************************************************************/
static void saveGameReadBlock(LABFH file, void *data, uint32 size) {
	warning("STUB: saveGameReadBlock");
	return;

	//fread(data, 1, size, file);
}




/*----- The machine independent section of saveGame.c -----*/


/* Lab: Labyrinth specific */
extern uint16 combination[6];
extern uint16 CurTile[4] [4];

#if !defined(DOSCODE)
extern CrumbData BreadCrumbs[MAX_CRUMBS];
extern uint16 NumCrumbs;
extern bool DroppingCrumbs;
extern bool FollowingCrumbs;
#endif

/*****************************************************************************/
/* Writes the game out to disk.                                              */
/* Assumes that the file has already been openned and is there.              */
/*****************************************************************************/
static bool saveGame(uint16 RoomNum, uint16 Direction, uint16 Quarters, LABFH file) {
#if !defined(DOSCODE)
	uint16 temp;
	CrumbData crumbs[sizeof(BreadCrumbs) / sizeof(CrumbData)];
#endif
	uint16 last, counter, counter1;
	char c;

	saveGameWriteBlock(file, (void *)SAVEVERSION, 4L);
#if defined(DOSCODE)
	saveGameWriteBlock(file, &RoomNum, 2L);
	saveGameWriteBlock(file, &Direction, 2L);
	saveGameWriteBlock(file, &Quarters, 2L);
#else
	temp = swapUShort(RoomNum);
	saveGameWriteBlock(file, &temp, 2L);
	temp = swapUShort(Direction);
	saveGameWriteBlock(file, &temp, 2L);
	temp = swapUShort(Quarters);
	saveGameWriteBlock(file, &temp, 2L);
#endif

	last = g_lab->_conditions->_lastElement / 8;
	saveGameWriteBlock(file, g_lab->_conditions->_array, (uint32) last);

	last = g_lab->_roomsFound->_lastElement / 8;
	saveGameWriteBlock(file, g_lab->_roomsFound->_array, (uint32) last);

	/* LAB: the combination lock and tile stuff */
	for (counter = 0; counter < 6; counter++) {
		c = (char)combination[counter];
		saveGameWriteBlock(file, &c, 1L);
	}

	for (counter = 0; counter < 4; counter++)
		for (counter1 = 0; counter1 < 4; counter1++)
#if defined(DOSCODE)
			saveGameWriteBlock(file, &(CurTile[counter] [counter1]), 2L);

#else
		{
			temp = swapUShort(CurTile[counter] [counter1]);
			saveGameWriteBlock(file, &temp, 2L);
		}
#endif

#if !defined(DOSCODE)
	saveGameWriteBlock(file, g_SaveGameImage, SAVED_IMAGE_SIZE);
	memcpy(crumbs, BreadCrumbs, sizeof BreadCrumbs);
	swapUShortPtr(&crumbs[0].RoomNum, sizeof(BreadCrumbs) / sizeof(uint16));
	saveGameWriteBlock(file, crumbs, sizeof BreadCrumbs);
#endif

	saveGameClose(file, true);

	return true;
}



/*****************************************************************************/
/* Reads the game from disk.                                                 */
/* Assumes that the file has already been openned and is there.              */
/*****************************************************************************/
static bool loadGame(uint16 *RoomNum, uint16 *Direction, uint16 *Quarters, LABFH file) {
#if !defined(DOSCODE)
	uint16 t;
	CrumbData crumbs[sizeof(BreadCrumbs) / sizeof(CrumbData)];
#endif
	char temp[5], c;
	uint16 last, counter, counter1;

	saveGameReadBlock(file, temp, 4L);
	temp[4] = 0;

	/*
	   if (strcmp(temp, SAVEVERSION) != 0)
	   {
	    saveGameClose(file, false);
	    return false;
	   }
	 */

#if defined(DOSCODE)
	saveGameReadBlock(file, RoomNum, 2L);
	saveGameReadBlock(file, Direction, 2L);
	saveGameReadBlock(file, Quarters, 2L);
#else
	saveGameReadBlock(file, &t, 2L);
	*RoomNum = swapUShort(t);
	saveGameReadBlock(file, &t, 2L);
	*Direction = swapUShort(t);
	saveGameReadBlock(file, &t, 2L);
	*Quarters = swapUShort(t);
#endif

	last = g_lab->_conditions->_lastElement / 8;
	saveGameReadBlock(file, g_lab->_conditions->_array, (uint32) last);

	last = g_lab->_roomsFound->_lastElement / 8;
	saveGameReadBlock(file, g_lab->_roomsFound->_array, (uint32) last);

	/* LAB: the combination lock and tile stuff */
	for (counter = 0; counter < 6; counter++) {
		saveGameReadBlock(file, &c, 1L);
		combination[counter] = c;
	}

	for (counter = 0; counter < 4; counter++)
		for (counter1 = 0; counter1 < 4; counter1++)
#if defined(DOSCODE)
			saveGameReadBlock(file, &(CurTile[counter] [counter1]), 2L);

#else
		{
			saveGameReadBlock(file, &t, 2L);
			CurTile[counter] [counter1] = swapUShort(t);
		}
#endif

	if (strcmp(temp, SAVEVERSION) == 0) {
		saveGameReadBlock(file, g_SaveGameImage, SAVED_IMAGE_SIZE);

		memset(crumbs, 0, sizeof BreadCrumbs);
		saveGameReadBlock(file, crumbs, sizeof BreadCrumbs);
		swapUShortPtr(&crumbs[0].RoomNum, sizeof(BreadCrumbs) / sizeof(uint16));
		memcpy(BreadCrumbs, crumbs, sizeof BreadCrumbs);
		DroppingCrumbs = (BreadCrumbs[0].RoomNum != 0);
		FollowingCrumbs = false;

		for (counter = 0; counter < MAX_CRUMBS; counter++)
			if (BreadCrumbs[counter].RoomNum == 0) break;

		NumCrumbs = counter;
	}

	saveGameClose(file, false);

	return true;
}




/*****************************************************************************/
/* Saves the game to the floppy disk.                                        */
/*****************************************************************************/
bool saveFloppy(char *path, uint16 RoomNum, uint16 Direction, uint16 NumQuarters, uint16 filenum, uint16 type) {
	LABFH FPtr;

	g_music->checkMusic();

	FileType = type;
	FileNum  = filenum;

	if ((FPtr = saveGameOpen(path, true)) != INVALID_LABFH)
		saveGame(RoomNum, Direction, NumQuarters, FPtr);
	else
		return false;

	return true;
}




/*****************************************************************************/
/* Reads the game from the floppy disk.                                      */
/*****************************************************************************/
bool readFloppy(char *path, uint16 *RoomNum, uint16 *Direction, uint16 *NumQuarters, uint16 filenum, uint16 type) {
	LABFH FPtr;

	g_music->checkMusic();

	FileType = type;
	FileNum  = filenum;

	if ((FPtr = saveGameOpen(path, false)) != INVALID_LABFH) {
		if (!loadGame(RoomNum, Direction, NumQuarters, FPtr))
			return false;
	} else
		return false;

	return true;
}

} // End of namespace Lab