1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "hopkins/saveload.h"
24
25 #include "hopkins/files.h"
26 #include "hopkins/globals.h"
27 #include "hopkins/hopkins.h"
28
29
30 #include "common/system.h"
31 #include "common/savefile.h"
32 #include "graphics/surface.h"
33 #include "graphics/scaler.h"
34 #include "graphics/thumbnail.h"
35
36 namespace Hopkins {
37
38 const char *SAVEGAME_STR = "HOPKINS";
39 #define SAVEGAME_STR_SIZE 13
40
SaveLoadManager(HopkinsEngine * vm)41 SaveLoadManager::SaveLoadManager(HopkinsEngine *vm) {
42 _vm = vm;
43 }
44
save(const Common::String & file,const void * buf,size_t n)45 bool SaveLoadManager::save(const Common::String &file, const void *buf, size_t n) {
46 Common::OutSaveFile *savefile = g_system->getSavefileManager()->openForSaving(file);
47
48 if (savefile) {
49 size_t bytesWritten = savefile->write(buf, n);
50 savefile->finalize();
51 delete savefile;
52
53 return bytesWritten == n;
54 } else
55 return false;
56 }
57
saveExists(const Common::String & file)58 bool SaveLoadManager::saveExists(const Common::String &file) {
59 Common::InSaveFile *savefile = g_system->getSavefileManager()->openForLoading(file);
60 bool result = savefile != NULL;
61 delete savefile;
62 return result;
63 }
64
65 // Save File
saveFile(const Common::String & file,const void * buf,size_t n)66 bool SaveLoadManager::saveFile(const Common::String &file, const void *buf, size_t n) {
67 return save(file, buf, n);
68 }
69
load(const Common::String & file,byte * buf)70 void SaveLoadManager::load(const Common::String &file, byte *buf) {
71 Common::InSaveFile *savefile = g_system->getSavefileManager()->openForLoading(file);
72 if (savefile == NULL)
73 error("Error opening file - %s", file.c_str());
74
75 int32 filesize = savefile->size();
76 savefile->read(buf, filesize);
77 delete savefile;
78 }
79
readSavegameHeader(Common::InSaveFile * in,hopkinsSavegameHeader & header,bool skipThumbnail)80 WARN_UNUSED_RESULT bool SaveLoadManager::readSavegameHeader(Common::InSaveFile *in, hopkinsSavegameHeader &header, bool skipThumbnail) {
81 char saveIdentBuffer[SAVEGAME_STR_SIZE + 1];
82
83 // Validate the header Id
84 in->read(saveIdentBuffer, SAVEGAME_STR_SIZE + 1);
85 if (strncmp(saveIdentBuffer, SAVEGAME_STR, SAVEGAME_STR_SIZE))
86 return false;
87
88 header._version = in->readByte();
89 if (header._version > HOPKINS_SAVEGAME_VERSION)
90 return false;
91
92 // Read in the string
93 header._saveName.clear();
94 char ch;
95 while ((ch = (char)in->readByte()) != '\0') header._saveName += ch;
96
97 // Get the thumbnail
98 if (!Graphics::loadThumbnail(*in, header._thumbnail, skipThumbnail)) {
99 return false;
100 }
101
102 // Read in save date/time
103 header._year = in->readSint16LE();
104 header._month = in->readSint16LE();
105 header._day = in->readSint16LE();
106 header._hour = in->readSint16LE();
107 header._minute = in->readSint16LE();
108 header._totalFrames = in->readUint32LE();
109
110 return true;
111 }
112
writeSavegameHeader(Common::OutSaveFile * out,hopkinsSavegameHeader & header)113 void SaveLoadManager::writeSavegameHeader(Common::OutSaveFile *out, hopkinsSavegameHeader &header) {
114 // Write out a savegame header
115 out->write(SAVEGAME_STR, SAVEGAME_STR_SIZE + 1);
116
117 out->writeByte(HOPKINS_SAVEGAME_VERSION);
118
119 // Write savegame name
120 out->write(header._saveName.c_str(), header._saveName.size() + 1);
121
122 // Create a thumbnail and save it
123 Graphics::Surface *thumb = new Graphics::Surface();
124 createThumbnail(thumb);
125 Graphics::saveThumbnail(*out, *thumb);
126 thumb->free();
127 delete thumb;
128
129 // Write out the save date/time
130 TimeDate td;
131 g_system->getTimeAndDate(td);
132 out->writeSint16LE(td.tm_year + 1900);
133 out->writeSint16LE(td.tm_mon + 1);
134 out->writeSint16LE(td.tm_mday);
135 out->writeSint16LE(td.tm_hour);
136 out->writeSint16LE(td.tm_min);
137 out->writeUint32LE(_vm->_events->_gameCounter);
138 }
139
saveGame(int slot,const Common::String & saveName)140 Common::Error SaveLoadManager::saveGame(int slot, const Common::String &saveName) {
141 /* Pack any necessary data into the savegame data structure */
142 // Set the selected slot number
143 _vm->_globals->_saveData->_data[svLastSavegameSlot] = slot;
144
145 // Set up the inventory
146 for (int i = 0; i < 35; ++i)
147 _vm->_globals->_saveData->_inventory[i] = _vm->_globals->_inventory[i];
148
149 _vm->_globals->_saveData->_mapCarPosX = _vm->_objectsMan->_mapCarPosX;
150 _vm->_globals->_saveData->_mapCarPosY = _vm->_objectsMan->_mapCarPosY;
151
152 /* Create the savegame */
153 Common::OutSaveFile *savefile = g_system->getSavefileManager()->openForSaving(
154 _vm->getSaveStateName(slot));
155 if (!savefile)
156 return Common::kCreatingFileFailed;
157
158 // Set up the serializer
159 Common::Serializer serializer(NULL, savefile);
160
161 // Write out the savegame header
162 hopkinsSavegameHeader header;
163 header._saveName = saveName;
164 header._version = HOPKINS_SAVEGAME_VERSION;
165 writeSavegameHeader(savefile, header);
166
167 // Write out the savegame data
168 syncSavegameData(serializer, header._version);
169
170 // Save file complete
171 savefile->finalize();
172 delete savefile;
173
174 return Common::kNoError;
175 }
176
loadGame(int slot)177 Common::Error SaveLoadManager::loadGame(int slot) {
178 // Try and open the save file for reading
179 Common::InSaveFile *savefile = g_system->getSavefileManager()->openForLoading(
180 _vm->getSaveStateName(slot));
181 if (!savefile)
182 return Common::kReadingFailed;
183
184 // Set up the serializer
185 Common::Serializer serializer(savefile, NULL);
186
187 // Read in the savegame header
188 hopkinsSavegameHeader header;
189 if (!readSavegameHeader(savefile, header)) {
190 delete savefile;
191 return Common::kReadingFailed;
192 }
193
194 // Read in the savegame data
195 syncSavegameData(serializer, header._version);
196
197 // Loading save file complete
198 delete savefile;
199
200 // Unpack the inventory
201 for (int i = 0; i < 35; ++i)
202 _vm->_globals->_inventory[i] = _vm->_globals->_saveData->_inventory[i];
203
204 // Set variables from loaded data as necessary
205 _vm->_globals->_saveData->_data[svLastSavegameSlot] = slot;
206 _vm->_globals->_exitId = _vm->_globals->_saveData->_data[svLastScreenId];
207 _vm->_globals->_saveData->_data[svLastPrevScreenId] = 0;
208 _vm->_globals->_screenId = 0;
209 _vm->_objectsMan->_mapCarPosX = _vm->_globals->_saveData->_mapCarPosX;
210 _vm->_objectsMan->_mapCarPosY = _vm->_globals->_saveData->_mapCarPosY;
211
212 return Common::kNoError;
213 }
214
readSavegameHeader(int slot,hopkinsSavegameHeader & header,bool skipThumbnail)215 WARN_UNUSED_RESULT bool SaveLoadManager::readSavegameHeader(int slot, hopkinsSavegameHeader &header, bool skipThumbnail) {
216 // Try and open the save file for reading
217 Common::InSaveFile *savefile = g_system->getSavefileManager()->openForLoading(
218 _vm->getSaveStateName(slot));
219 if (!savefile)
220 return false;
221
222 bool result = readSavegameHeader(savefile, header, skipThumbnail);
223 delete savefile;
224 return result;
225 }
226
227 #define REDUCE_AMOUNT 80
228
createThumbnail(Graphics::Surface * s)229 void SaveLoadManager::createThumbnail(Graphics::Surface *s) {
230 int w = _vm->_graphicsMan->zoomOut(SCREEN_WIDTH, REDUCE_AMOUNT);
231 int h = _vm->_graphicsMan->zoomOut(SCREEN_HEIGHT - 40, REDUCE_AMOUNT);
232
233 Graphics::Surface thumb8;
234 thumb8.create(w, h, Graphics::PixelFormat::createFormatCLUT8());
235
236 _vm->_graphicsMan->reduceScreenPart(_vm->_graphicsMan->_frontBuffer, (byte *)thumb8.getPixels(),
237 _vm->_events->_startPos.x, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 40, 80);
238
239 // Convert the 8-bit pixel to 16 bit surface
240 s->create(w, h, Graphics::PixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0));
241
242 const byte *srcP = (const byte *)thumb8.getPixels();
243 uint16 *destP = (uint16 *)s->getPixels();
244
245 for (int yp = 0; yp < h; ++yp) {
246 // Copy over the line, using the source pixels as lookups into the pixels palette
247 const byte *lineSrcP = srcP;
248 uint16 *lineDestP = destP;
249
250 for (int xp = 0; xp < w; ++xp)
251 *lineDestP++ = *(uint16 *)&_vm->_graphicsMan->_palettePixels[*lineSrcP++ * 2];
252
253 // Move to the start of the next line
254 srcP += w;
255 destP += w;
256 }
257 thumb8.free();
258 }
259
syncSavegameData(Common::Serializer & s,int version)260 void SaveLoadManager::syncSavegameData(Common::Serializer &s, int version) {
261 // The brief version 3 had the highscores embedded. They're in a separate file now, so skip
262 if (version == 3 && s.isLoading())
263 s.skip(100);
264
265 s.syncBytes(&_vm->_globals->_saveData->_data[0], 2050);
266 syncCharacterLocation(s, _vm->_globals->_saveData->_cloneHopkins);
267 syncCharacterLocation(s, _vm->_globals->_saveData->_realHopkins);
268 syncCharacterLocation(s, _vm->_globals->_saveData->_samantha);
269
270 for (int i = 0; i < 35; ++i)
271 s.syncAsSint16LE(_vm->_globals->_saveData->_inventory[i]);
272
273 if (version > 1) {
274 s.syncAsSint16LE(_vm->_globals->_saveData->_mapCarPosX);
275 s.syncAsSint16LE(_vm->_globals->_saveData->_mapCarPosY);
276 } else {
277 _vm->_globals->_saveData->_mapCarPosX = _vm->_globals->_saveData->_mapCarPosY = 0;
278 }
279
280 }
281
syncCharacterLocation(Common::Serializer & s,CharacterLocation & item)282 void SaveLoadManager::syncCharacterLocation(Common::Serializer &s, CharacterLocation &item) {
283 s.syncAsSint16LE(item._pos.x);
284 s.syncAsSint16LE(item._pos.y);
285 s.syncAsSint16LE(item._startSpriteIndex);
286 s.syncAsSint16LE(item._location);
287 s.syncAsSint16LE(item._zoomFactor);
288 }
289
convertThumb16To8(Graphics::Surface * thumb16,Graphics::Surface * thumb8)290 void SaveLoadManager::convertThumb16To8(Graphics::Surface *thumb16, Graphics::Surface *thumb8) {
291 thumb8->create(thumb16->w, thumb16->h, Graphics::PixelFormat::createFormatCLUT8());
292 Graphics::PixelFormat pixelFormat16(2, 5, 6, 5, 0, 11, 5, 0, 0);
293
294 byte paletteR[PALETTE_SIZE];
295 byte paletteG[PALETTE_SIZE];
296 byte paletteB[PALETTE_SIZE];
297 for (int palIndex = 0; palIndex < PALETTE_SIZE; ++palIndex) {
298 uint16 p = READ_UINT16(&_vm->_graphicsMan->_palettePixels[palIndex * 2]);
299 pixelFormat16.colorToRGB(p, paletteR[palIndex], paletteG[palIndex], paletteB[palIndex]);
300 }
301
302 const uint16 *srcP = (const uint16 *)thumb16->getPixels();
303 byte *destP = (byte *)thumb8->getPixels();
304
305 for (int yp = 0; yp < thumb16->h; ++yp) {
306 const uint16 *lineSrcP = srcP;
307 byte *lineDestP = destP;
308
309 for (int xp = 0; xp < thumb16->w; ++xp) {
310 byte r, g, b;
311 pixelFormat16.colorToRGB(*lineSrcP++, r, g, b);
312
313 // Do like in the original and show thumbnail as a grayscale picture
314 int lum = (r * 21 + g * 72 + b * 7) / 100;
315 r = g = b = lum;
316
317 // Scan the palette for the closest match
318 int difference = 99999, foundIndex = 0;
319 for (int palIndex = 0; palIndex < PALETTE_SIZE; ++palIndex) {
320 byte rCurrent = paletteR[palIndex];
321 byte gCurrent = paletteG[palIndex];
322 byte bCurrent = paletteB[palIndex];
323
324 int diff = ABS((int)r - (int)rCurrent) + ABS((int)g - (int)gCurrent) + ABS((int)b - (int)bCurrent);
325 if (diff < difference) {
326 difference = diff;
327 foundIndex = palIndex;
328 }
329 }
330
331 *lineDestP++ = foundIndex;
332 }
333
334 // Move to the start of the next line
335 srcP += thumb16->w;
336 destP += thumb16->w;
337 }
338 }
339
340 } // End of namespace Hopkins
341