aboutsummaryrefslogtreecommitdiff
path: root/engines/pink/pink.cpp
blob: d28b05828d761cb217b8975edcac48fce4e22e18 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* 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 <graphics/thumbnail.h>
#include <graphics/surface.h>
#include "common/debug-channels.h"
#include "common/winexe_pe.h"
#include "common/config-manager.h"

#include "engines/util.h"

#include "graphics/cursorman.h"

#include "pink/pink.h"
#include "pink/console.h"
#include "pink/objects/module.h"
#include "pink/objects/actors/lead_actor.h"
#include "pink/objects/sequences/sequencer.h"

namespace Pink {

Pink::PinkEngine::PinkEngine(OSystem *system, const ADGameDescription *desc)
		: Engine(system), _console(nullptr), _rnd("pink"),
		  _desc(*desc), _bro(nullptr), _module(nullptr),
		  _director(), _pdaMgr(this) {
	debug("PinkEngine constructed");

	DebugMan.addDebugChannel(kPinkDebugGeneral, "general", "General issues");
	DebugMan.addDebugChannel(kPinkDebugLoadingObjects, "loading_objects", "Serializing objects from Orb");
	DebugMan.addDebugChannel(kPinkDebugLoadingResources, "loading_resources", "Loading resources data");
	DebugMan.addDebugChannel(kPinkDebugGraphics, "graphics", "Graphics handling");
	DebugMan.addDebugChannel(kPinkDebugSound, "sound", "Sound processing");
}

Pink::PinkEngine::~PinkEngine() {
	delete _console;
	delete _bro;
	for (uint i = 0; i < _modules.size(); ++i) {
		delete _modules[i];
	}
	for (uint j = 0; j < _cursors.size(); ++j) {
		delete _cursors[j];
	}

	DebugMan.clearAllDebugChannels();
}

Common::Error PinkEngine::init() {
	debug("PinkEngine init");

	initGraphics(640, 480);

	_console = new Console(this);

	const Common::String orbName = _desc.filesDescriptions[0].fileName;
	const Common::String broName = _desc.filesDescriptions[1].fileName;

	if (strcmp(_desc.gameId, kPeril) == 0)
		_bro = new BroFile();
	else
		debug("This game doesn't need to use bro");

	if (!_orb.open(orbName) || (_bro && !_bro->open(broName, _orb.getTimestamp())))
		return Common::kNoGameDataFoundError;

	if (!loadCursors())
		return Common::kNoGameDataFoundError;

	setCursor(kLoadingCursor);
	_system->showMouse(1);

	_orb.loadGame(this);

	if (ConfMan.hasKey("save_slot"))
		loadGameState(ConfMan.getInt("save_slot"));
	else
		initModule(_modules[0]->getName(), "", nullptr);

	return Common::kNoError;
}

Common::Error Pink::PinkEngine::run() {
	Common::Error error = init();
	if (error.getCode() != Common::kNoError)
		return error;

	while (!shouldQuit()) {
		Common::Event event;
		while (_eventMan->pollEvent(event)) {
			switch (event.type) {
			case Common::EVENT_QUIT:
			case Common::EVENT_RTL:
				return Common::kNoError;
			case Common::EVENT_MOUSEMOVE:
				_actor->onMouseMove(event.mouse);
				break;
			case Common::EVENT_LBUTTONDOWN:
				_actor->onLeftButtonClick(event.mouse);
				break;
			case Common::EVENT_KEYDOWN:
					_actor->onKeyboardButtonClick(event.kbd.keycode);
				break;
				// don't know why it is used in original
			case Common::EVENT_LBUTTONUP:
			case Common::EVENT_RBUTTONDOWN:
			default:
				break;
			}
		}

		_actor->update();
		_director.update();
		_system->delayMillis(10);
	}

	return Common::kNoError;
}

void PinkEngine::load(Archive &archive) {
	archive.readString();
	archive.readString();
	_modules.deserialize(archive);
}

void PinkEngine::initModule(const Common::String &moduleName, const Common::String &pageName, Archive *saveFile) {
	if (_module) {
		for (uint i = 0; i < _modules.size(); ++i) {
			if (_module == _modules[i]) {
				_modules[i] = new ModuleProxy(_module->getName());

				delete _module;
				_module = nullptr;

				break;
			}
		}
	}

	for (uint i = 0; i < _modules.size(); ++i) {
		if (_modules[i]->getName() == moduleName) {
			loadModule(i);
			_module = static_cast<Module*>(_modules[i]);
			if (saveFile)
				_module->loadState(*saveFile);
			_module->init( saveFile ? kLoadingSave : kLoadingNewGame, pageName);
			break;
		}
	}
}

void PinkEngine::changeScene(Page *page) {
	setCursor(kLoadingCursor);
	if (!_nextModule.empty() && _nextModule.compareTo(_module->getName())) {
		initModule(_nextModule, _nextPage, nullptr);
	} else {
		assert(!_nextPage.empty());
		_module->changePage(_nextPage);
	}
}

void PinkEngine::setNextExecutors(const Common::String &nextModule, const Common::String &nextPage) {
	_nextModule = nextModule;
	_nextPage = nextPage;
}

void PinkEngine::loadModule(int index) {
	Module *module = new Module(this, _modules[index]->getName());

	_orb.loadObject(module, module->getName());

	delete _modules[index];
	_modules[index] = module;
}

bool PinkEngine::checkValueOfVariable(Common::String &variable, Common::String &value) {
	if (!_variables.contains(variable))
		return value == kUndefined;
	return _variables[variable] == value;
}

void PinkEngine::setVariable(Common::String &variable, Common::String &value) {
	_variables[variable] = value;
}

bool PinkEngine::loadCursors() {
	Common::PEResources exeResources;
	bool isPokus = !strcmp(_desc.gameId, kPokus);
	Common::String fileName = isPokus ? _desc.filesDescriptions[1].fileName : _desc.filesDescriptions[2].fileName;
	if (!exeResources.loadFromEXE(fileName))
		return false;

	_cursors.reserve(kCursorsCount);

	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusLoadingCursorID));
	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusExitForwardCursorID));
	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusExitLeftCursorID));
	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusExitRightCursorID));
	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusClickableFirstCursorID));
	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusClickableSecondCursorID));

	if (isPokus) {
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusClickableThirdCursorID));
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusNotClickableCursorID));
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusHoldingItemCursorID));
	} else {
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPerilClickableThirdCursorID));
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPerilNotClickableCursorID));
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPerilHoldingItemCursorID));
	}

	_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusPDADefaultCursorID));

	if (isPokus) {
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusPDAClickableFirstFrameCursorID));
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPokusPDAClickableSecondFrameCursorID));
	} else {
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPerilPDAClickableFirstFrameCursorID));
		_cursors.push_back(Graphics::WinCursorGroup::createCursorGroup(exeResources, kPerilPDAClickableSecondFrameCursorID));
	}
	return true;
}

void PinkEngine::setCursor(uint cursorIndex) {
	Graphics::Cursor *cursor = _cursors[cursorIndex]->cursors[0].cursor;
	_system->setCursorPalette(cursor->getPalette(), cursor->getPaletteStartIndex(), cursor->getPaletteCount());
	_system->setMouseCursor(cursor->getSurface(), cursor->getWidth(), cursor->getHeight(),
							cursor->getHotspotX(), cursor->getHotspotY(), cursor->getKeyColor());
}

Common::Error PinkEngine::loadGameState(int slot) {
	Common::SeekableReadStream *in = _saveFileMan->openForLoading(generateSaveName(slot, _desc.gameId));
	if (!in)
		return Common::kNoGameDataFoundError;

	SaveStateDescriptor desc;
	if (!readSaveHeader(*in, desc))
		return Common::kUnknownError;

	Archive archive(in);
	_variables.deserialize(archive);
	_nextModule = archive.readString();
	_nextPage = archive.readString();
	initModule(archive.readString(), "", &archive);

	delete in;
	return Common::kNoError;
}

bool PinkEngine::canLoadGameStateCurrently() {
	return true;
}

Common::Error PinkEngine::saveGameState(int slot, const Common::String &desc) {
	Common::OutSaveFile *out = _saveFileMan->openForSaving(generateSaveName(slot, _desc.gameId));
	if (!out)
		return Common::kUnknownError;

	Archive archive(out);

	out->write("pink", 4);
	archive.writeString(desc);

	TimeDate curTime;
	_system->getTimeAndDate(curTime);

	out->writeUint32LE(((curTime.tm_mday & 0xFF) << 24) | (((curTime.tm_mon + 1) & 0xFF) << 16) | ((curTime.tm_year + 1900) & 0xFFFF));
	out->writeUint16LE(((curTime.tm_hour & 0xFF) << 8) | ((curTime.tm_min) & 0xFF));

	out->writeUint32LE(getTotalPlayTime() / 1000);

	if (!Graphics::saveThumbnail(*out))
		return Common::kUnknownError;

	_variables.serialize(archive);
	archive.writeString(_nextModule);
	archive.writeString(_nextPage);

	archive.writeString(_module->getName());
	_module->saveState(archive);

	delete out;

	return Common::kNoError;
}

bool PinkEngine::canSaveGameStateCurrently() {
	return true;
}

bool PinkEngine::hasFeature(Engine::EngineFeature f) const {
	return
			f == kSupportsRTL ||
			f == kSupportsLoadingDuringRuntime ||
			f == kSupportsSavingDuringRuntime;
}

void PinkEngine::pauseEngineIntern(bool pause) {
	Engine::pauseEngineIntern(pause);
	_director.pause(pause);
	_system->showMouse(!pause);
}

PDAMgr &PinkEngine::getPdaMgr() {
	return _pdaMgr;
}

Common::String generateSaveName(int slot, const char *gameId) {
	return Common::String::format("%s.s%02d", gameId, slot);
}

bool readSaveHeader(Common::InSaveFile &in, SaveStateDescriptor &desc) {
	char pink[4];
	in.read(&pink, 4);
	if (strcmp(pink, "pink"))
		return false;

	const Common::String description = in.readPascalString();
	uint32 date = in.readUint32LE();
	uint16 time = in.readUint16LE();
	uint32 playTime = in.readUint32LE();
	if (!Graphics::checkThumbnailHeader(in))
		return false;

	Graphics::Surface *thumbnail;
	if (!Graphics::loadThumbnail(in, thumbnail))
		return false;

	int day = (date >> 24) & 0xFF;
	int month = (date >> 16) & 0xFF;
	int year = date & 0xFFFF;

	int hour = (time >> 8) & 0xFF;
	int minutes = time & 0xFF;

	desc.setSaveDate(year, month, day);
	desc.setSaveTime(hour, minutes);
	desc.setPlayTime(playTime * 1000);
	desc.setDescription(description);
	desc.setThumbnail(thumbnail);

	return true;
}

}