aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/savegame.cpp
blob: 41f7b2787e0c64ce0e190873a02ac4d91e980bb8 (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
/* 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 "common/savefile.h"

#include "graphics/surface.h"
#include "graphics/thumbnail.h"

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

namespace Lab {

// Labyrinth of Time ScummVM
#define SAVEGAME_ID       MKTAG('L', 'O', 'T', 'S')
#define SAVEGAME_VERSION  1

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


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


/* Lab: Labyrinth specific */
extern byte combination[6];
extern uint16 CurTile[4] [4];
extern char *getPictName(CloseDataPtr *LCPtr);

void writeSaveGameHeader(Common::OutSaveFile *out, const Common::String &saveName) {
	out->writeUint32BE(SAVEGAME_ID);

	// Write version
	out->writeByte(SAVEGAME_VERSION);

	// Write savegame name
	out->writeString(saveName);
	out->writeByte(0);

	// Save the game thumbnail
	Graphics::saveThumbnail(*out);

	// Creation date/time
	TimeDate curTime;
	g_system->getTimeAndDate(curTime);

	uint32 saveDate = ((curTime.tm_mday & 0xFF) << 24) | (((curTime.tm_mon + 1) & 0xFF) << 16) | ((curTime.tm_year + 1900) & 0xFFFF);
	uint16 saveTime = ((curTime.tm_hour & 0xFF) << 8) | ((curTime.tm_min) & 0xFF);
	uint32 playTime = g_engine->getTotalPlayTime() / 1000;

	out->writeUint32BE(saveDate);
	out->writeUint16BE(saveTime);
	out->writeUint32BE(playTime);
}

bool readSaveGameHeader(Common::InSaveFile *in, SaveGameHeader &header) {
	uint32 id = in->readUint32BE();
	
	// Check if it's a valid ScummVM savegame
	if (id != SAVEGAME_ID)
		return false;

	// Read in the version
	header._version = in->readByte();

	// Check that the save version isn't newer than this binary
	if (header._version > SAVEGAME_VERSION)
		return false;

	// Read in the save name
	Common::String saveName;
	char ch;
	while ((ch = (char)in->readByte()) != '\0')
		saveName += ch;
	header._descr.setDescription(saveName);

	// Get the thumbnail
	header._descr.setThumbnail(Graphics::loadThumbnail(*in));

	uint32 saveDate = in->readUint32BE();
	uint16 saveTime = in->readUint16BE();
	uint32 playTime = in->readUint32BE();

	int day = (saveDate >> 24) & 0xFF;
	int month = (saveDate >> 16) & 0xFF;
	int year = saveDate & 0xFFFF;
	header._descr.setSaveDate(year, month, day);

	int hour = (saveTime >> 8) & 0xFF;
	int minutes = saveTime & 0xFF;
	header._descr.setSaveTime(hour, minutes);

	header._descr.setPlayTime(playTime * 1000);
	g_engine->setTotalPlayTime(playTime * 1000);

	return true;
}

/*****************************************************************************/
/* Writes the game out to disk.                                              */
/*****************************************************************************/
bool saveGame(uint16 Direction, uint16 Quarters, int slot, Common::String desc) {
	uint16 i, j;
	Common::String fileName = g_lab->generateSaveFileName(slot);
	Common::SaveFileManager *saveFileManager = g_system->getSavefileManager();
	Common::OutSaveFile *file = saveFileManager->openForSaving(fileName);

	if (!file)
		return false;

	// Load scene pic
	CloseDataPtr CPtr = NULL;
	readPict(getPictName(&CPtr), true);

	writeSaveGameHeader(file, desc);
	file->writeUint16LE(g_lab->_roomNum);
	file->writeUint16LE(Direction);
	file->writeUint16LE(Quarters);

	// Conditions
	for (i = 0; i < g_lab->_conditions->_lastElement / (8 * 2); i++)
		file->writeUint16LE(g_lab->_conditions->_array[i]);

	// Rooms found
	for (i = 0; i < g_lab->_roomsFound->_lastElement / (8 * 2); i++)
		file->writeUint16LE(g_lab->_roomsFound->_array[i]);

	// Combination lock and tile stuff
	for (i = 0; i < 6; i++)
		file->writeByte(combination[i]);

	// Tiles
	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			file->writeUint16LE(CurTile[i][j]);

	// Breadcrumbs
	for (i = 0; i < sizeof(g_lab->_breadCrumbs); i++) {
		file->writeUint16LE(g_lab->_breadCrumbs[i]._roomNum);
		file->writeUint16LE(g_lab->_breadCrumbs[i]._direction);
	}

	file->flush();
	file->finalize();
	delete file;

	return true;
}



/*****************************************************************************/
/* Reads the game from disk.                                                 */
/*****************************************************************************/
bool loadGame(uint16 *Direction, uint16 *Quarters, int slot) {
	uint16 i, j;
	Common::String fileName = g_lab->generateSaveFileName(slot);
	Common::SaveFileManager *saveFileManager = g_system->getSavefileManager();
	Common::InSaveFile *file = saveFileManager->openForLoading(fileName);
	
	if (!file)
		return false;

	SaveGameHeader header;
	readSaveGameHeader(file, header);
	g_lab->_roomNum = file->readUint16LE();
	*Direction = file->readUint16LE();
	*Quarters = file->readUint16LE();

	// Conditions
	for (i = 0; i < g_lab->_conditions->_lastElement / (8 * 2); i++)
		g_lab->_conditions->_array[i] = file->readUint16LE();

	// Rooms found
	for (i = 0; i < g_lab->_roomsFound->_lastElement / (8 * 2); i++)
		g_lab->_roomsFound->_array[i] = file->readUint16LE();

	// Combination lock and tile stuff
	for (i = 0; i < 6; i++)
		combination[i] = file->readByte();

	// Tiles
	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++)
			CurTile[i][j] = file->readUint16LE();

	// Breadcrumbs
	for (i = 0; i < 128; i++) {
		g_lab->_breadCrumbs[i]._roomNum = file->readUint16LE();
		g_lab->_breadCrumbs[i]._direction = file->readUint16LE();
	}

	g_lab->_droppingCrumbs = (g_lab->_breadCrumbs[0]._roomNum != 0);
	g_lab->_followingCrumbs = false;

	for (i = 0; i < 128; i++) {
		if (g_lab->_breadCrumbs[i]._roomNum == 0)
			break;
		g_lab->_numCrumbs = i;
	}

	delete file;

	return true;
}


} // End of namespace Lab