aboutsummaryrefslogtreecommitdiff
path: root/engines/hdb/saveload.cpp
blob: b8d2252f2b074ed0664ed43ae47c3956c9de7c5b (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
/* 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.
 *
 */

#include "hdb/hdb.h"

namespace HDB {

Common::Error HDBGame::saveGameState(int slot, const Common::String &desc) {

	// If no map is loaded, don't try to save
	if (!g_hdb->_map->isLoaded())
		return Common::kCreatingFileFailed;

	Common::OutSaveFile *out;

	Common::String saveFileName = Common::String::format("%s.%03d", _targetName.c_str(), slot);
	if (!(out = _saveFileMan->openForSaving(saveFileName)))
		error("Unable to open save file");

	Graphics::saveThumbnail(*out);

	// Actual Save Data
	saveGame(out);
	_lua->save(out, _targetName.c_str(), slot);

	out->finalize();
	if (out->err())
		warning("Can't write file '%s'. (Disk full?)", saveFileName.c_str());

	delete out;

	return Common::kNoError;
}

Common::Error HDBGame::loadGameState(int slot) {
	Common::InSaveFile *in;

	Common::String saveFileName = Common::String::format("%s.%03d", _targetName.c_str(), slot);
	if (!(in = _saveFileMan->openForLoading(saveFileName))) {
		warning("missing savegame file %s", saveFileName.c_str());
		if (g_hdb->_map->isLoaded())
			g_hdb->setGameState(GAME_PLAY);
		return Common::kReadingFailed;
	}

	Graphics::skipThumbnail(*in);

	// Actual Save Data
	loadGame(in);

	_lua->loadLua(_currentLuaName); // load the Lua code FIRST! (if no file, it's ok)

	saveFileName = Common::String::format("%s.l.%03d", _targetName.c_str(), slot);
	_lua->loadSaveFile(in, saveFileName.c_str());

	delete in;

	return Common::kNoError;
}

void HDBGame::saveGame(Common::OutSaveFile *out) {

	// Save Map Name and Time
	out->writeUint32LE(_timeSeconds + (_timePlayed / 1000));
	out->write(_inMapName, 32);

	// Save Map Object Data
	_map->save(out);

	// Save Window Object Data
	_window->save(out);

	// Save Gfx Object Data
	_gfx->save(out);

	// Save Sound Object Data
	_sound->save(out);

	// Save Game Object Data
	save(out);

	// Save AI Object Data

	_ai->save(out);
}

void HDBGame::loadGame(Common::InSaveFile *in) {
	// Load Map Name and Time
	_timeSeconds = in->readUint32LE();;
	_timePlayed = 0;
	in->read(_inMapName, 32);

	g_hdb->_sound->stopMusic();

	// Load Map Object Data
	_map->loadSaveFile(in);

	// Load Window Object Data
	_window->loadSaveFile(in);

	// Load Gfx Object Data
	_gfx->loadSaveFile(in);

	// Load Sound Object Data
	_sound->loadSaveFile(in);

	// Load Game Object Data
	loadSaveFile(in);

	// Load AI Object Data

	_ai->loadSaveFile(in);

	_gfx->turnOffFade();
}

} // End of Namespace