aboutsummaryrefslogtreecommitdiff
path: root/engines/zvision/zvision.cpp
blob: 41d3eea6aa0f9d1027d6416ed0e8f5c59ccbce59 (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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* 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 "common/scummsys.h"

#include "zvision/zvision.h"

#include "zvision/core/console.h"
#include "zvision/scripting/script_manager.h"
#include "zvision/graphics/render_manager.h"
#include "zvision/cursors/cursor_manager.h"
#include "zvision/core/save_manager.h"
#include "zvision/text/string_manager.h"
#include "zvision/archives/zfs_archive.h"
#include "zvision/detection.h"
#include "zvision/core/menu.h"
#include "zvision/core/search_manager.h"
#include "zvision/text/text.h"
#include "zvision/fonts/truetype_font.h"
#include "zvision/core/midi.h"

#include "common/config-manager.h"
#include "common/str.h"
#include "common/debug.h"
#include "common/debug-channels.h"
#include "common/textconsole.h"
#include "common/error.h"
#include "common/system.h"
#include "common/file.h"

#include "engines/util.h"

#include "audio/mixer.h"


namespace ZVision {

#define ZVISION_SETTINGS_KEYS_COUNT 17

struct zvisionIniSettings {
	const char *name;
	int16 slot;
	int16 deflt;
} settingsKeys[ZVISION_SETTINGS_KEYS_COUNT] = {
	{"ZVision_KeyboardTurnSpeed", StateKey_KbdRotateSpeed, 5},
	{"ZVision_PanaRotateSpeed", StateKey_RotateSpeed, 540},
	{"ZVision_QSoundEnabled", StateKey_Qsound, 1},
	{"ZVision_VenusEnabled", StateKey_VenusEnable, 1},
	{"ZVision_HighQuality", StateKey_HighQuality, 1},
	{"ZVision_Platform", StateKey_Platform, 0},
	{"ZVision_InstallLevel", StateKey_InstallLevel, 0},
	{"ZVision_CountryCode", StateKey_CountryCode, 0},
	{"ZVision_CPU", StateKey_CPU, 1},
	{"ZVision_MovieCursor", StateKey_MovieCursor, 1},
	{"ZVision_NoAnimWhileTurning", StateKey_NoTurnAnim, 0},
	{"ZVision_Win958", StateKey_WIN958, 0},
	{"ZVision_ShowErrorDialogs", StateKey_ShowErrorDlg, 0},
	{"ZVision_ShowSubtitles", StateKey_Subtitles, 1},
	{"ZVision_DebugCheats", StateKey_DebugCheats, 0},
	{"ZVision_JapaneseFonts", StateKey_JapanFonts, 0},
	{"ZVision_Brightness", StateKey_Brightness, 0}
};

ZVision::ZVision(OSystem *syst, const ZVisionGameDescription *gameDesc)
	: Engine(syst),
	  _gameDescription(gameDesc),
	  _workingWindow_ZGI((WINDOW_WIDTH - WORKING_WINDOW_WIDTH) / 2, (WINDOW_HEIGHT - WORKING_WINDOW_HEIGHT) / 2, ((WINDOW_WIDTH - WORKING_WINDOW_WIDTH) / 2) + WORKING_WINDOW_WIDTH, ((WINDOW_HEIGHT - WORKING_WINDOW_HEIGHT) / 2) + WORKING_WINDOW_HEIGHT),
	  _workingWindow_ZNM((WINDOW_WIDTH - ZNM_WORKING_WINDOW_WIDTH) / 2, (WINDOW_HEIGHT - ZNM_WORKING_WINDOW_HEIGHT) / 2, ((WINDOW_WIDTH - ZNM_WORKING_WINDOW_WIDTH) / 2) + ZNM_WORKING_WINDOW_WIDTH, ((WINDOW_HEIGHT - ZNM_WORKING_WINDOW_HEIGHT) / 2) + ZNM_WORKING_WINDOW_HEIGHT),
	  _workingWindow(gameDesc->gameId == GID_NEMESIS ? _workingWindow_ZNM : _workingWindow_ZGI),
	  _pixelFormat(2, 5, 6, 5, 0, 11, 5, 0, 0), /*RGB 565*/
	  _desiredFrameTime(33), /* ~30 fps */
	  _clock(_system),
	  _scriptManager(nullptr),
	  _renderManager(nullptr),
	  _saveManager(nullptr),
	  _stringManager(nullptr),
	  _cursorManager(nullptr),
	  _midiManager(nullptr),
	  _aud_id(0),
	  _rendDelay(2),
	  _velocity(0) {

	debug(1, "ZVision::ZVision");
}

ZVision::~ZVision() {
	debug(1, "ZVision::~ZVision");

	// Dispose of resources
	delete _console;
	delete _cursorManager;
	delete _stringManager;
	delete _saveManager;
	delete _renderManager;
	delete _scriptManager;
	delete _rnd;
	delete _midiManager;

	// Remove all of our debug levels
	DebugMan.clearAllDebugChannels();
}

void ZVision::registerDefaultSettings() {
	for (int i = 0; i < ZVISION_SETTINGS_KEYS_COUNT; i++)
		ConfMan.registerDefault(settingsKeys[i].name, settingsKeys[i].deflt);
	ConfMan.registerDefault("doublefps", false);
}

void ZVision::loadSettings() {
	for (int i = 0; i < ZVISION_SETTINGS_KEYS_COUNT; i++)
		_scriptManager->setStateValue(settingsKeys[i].slot, ConfMan.getInt(settingsKeys[i].name));

	if (getGameId() == GID_NEMESIS)
		_scriptManager->setStateValue(StateKey_ExecScopeStyle, 1);
	else
		_scriptManager->setStateValue(StateKey_ExecScopeStyle, 0);
}

void ZVision::saveSettings() {
	for (int i = 0; i < ZVISION_SETTINGS_KEYS_COUNT; i++)
		ConfMan.setInt(settingsKeys[i].name, _scriptManager->getStateValue(settingsKeys[i].slot));
	ConfMan.flushToDisk();
}

void ZVision::initialize() {
	const Common::FSNode gameDataDir(ConfMan.get("path"));

	_searchManager = new sManager(ConfMan.get("path"), 6);

	_searchManager->addDir("FONTS");
	_searchManager->addDir("addon");

	if (_gameDescription->gameId == GID_GRANDINQUISITOR) {
		_searchManager->loadZix("INQUIS.ZIX");
		_searchManager->addPatch("C000H01Q.RAW", "C000H01Q.SRC");
		_searchManager->addPatch("CM00H01Q.RAW", "CM00H01Q.SRC");
		_searchManager->addPatch("DM00H01Q.RAW", "DM00H01Q.SRC");
		_searchManager->addPatch("E000H01Q.RAW", "E000H01Q.SRC");
		_searchManager->addPatch("EM00H50Q.RAW", "EM00H50Q.SRC");
		_searchManager->addPatch("GJNPH65P.RAW", "GJNPH65P.SRC");
		_searchManager->addPatch("GJNPH72P.RAW", "GJNPH72P.SRC");
		_searchManager->addPatch("H000H01Q.RAW", "H000H01Q.SRC");
		_searchManager->addPatch("M000H01Q.RAW", "M000H01Q.SRC");
		_searchManager->addPatch("P000H01Q.RAW", "P000H01Q.SRC");
		_searchManager->addPatch("Q000H01Q.RAW", "Q000H01Q.SRC");
		_searchManager->addPatch("SW00H01Q.RAW", "SW00H01Q.SRC");
		_searchManager->addPatch("T000H01Q.RAW", "T000H01Q.SRC");
		_searchManager->addPatch("U000H01Q.RAW", "U000H01Q.SRC");
	} else if (_gameDescription->gameId == GID_NEMESIS)
		_searchManager->loadZix("NEMESIS.ZIX");
	// TODO: There are 10 file clashes when we flatten the directories.
	// From a quick look, the files are exactly the same, so it shouldn't matter.
	// But I'm noting it here just in-case it does become a problem.
	SearchMan.addSubDirectoryMatching(gameDataDir, "data1", 0, 4, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "data2", 0, 4, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "data3", 0, 4, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "zassets1", 0, 2, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "zassets2", 0, 2, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "znemmx", 0, 1, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "zgi", 0, 4, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "zgi_mx", 0, 1, true);
	SearchMan.addSubDirectoryMatching(gameDataDir, "fonts", 0, 1, true);

	// Find zfs archive files
	Common::ArchiveMemberList list;
	SearchMan.listMatchingMembers(list, "*.zfs");

	// Register the file entries within the zfs archives with the SearchMan
	for (Common::ArchiveMemberList::iterator iter = list.begin(); iter != list.end(); ++iter) {
		Common::String name = (*iter)->getName();
		Common::SeekableReadStream *stream = (*iter)->createReadStream();
		ZfsArchive *archive = new ZfsArchive(name, stream);

		delete stream;

		SearchMan.add(name, archive);
	}

	initGraphics(WINDOW_WIDTH, WINDOW_HEIGHT, true, &_pixelFormat);

	// Register random source
	_rnd = new Common::RandomSource("zvision");

	// Create managers
	_scriptManager = new ScriptManager(this);
	_renderManager = new RenderManager(this, WINDOW_WIDTH, WINDOW_HEIGHT, _workingWindow, _pixelFormat);
	_saveManager = new SaveManager(this);
	_stringManager = new StringManager(this);
	_cursorManager = new CursorManager(this, &_pixelFormat);
	_textRenderer = new textRenderer(this);
	_midiManager = new midiManager();

	if (_gameDescription->gameId == GID_GRANDINQUISITOR)
		_menu = new menuZgi(this);
	else
		_menu = new menuNem(this);

	// Initialize the managers
	_cursorManager->initialize();
	_scriptManager->initialize();
	_stringManager->initialize(_gameDescription->gameId);

	registerDefaultSettings();

	loadSettings();

	// Create debugger console. It requires GFX to be initialized
	_console = new Console(this);
	_halveDelay = ConfMan.getBool("doublefps");
}

Common::Error ZVision::run() {
	initialize();

	// Main loop
	while (!shouldQuit()) {
		_clock.update();
		uint32 currentTime = _clock.getLastMeasuredTime();
		uint32 deltaTime = _clock.getDeltaTime();

		_cursorManager->setItemID(_scriptManager->getStateValue(StateKey_InventoryItem));

		processEvents();
		updateRotation();

		// Call _renderManager->update() first so the background renders
		// before anything that puzzles/controls will render
		_scriptManager->update(deltaTime);
		_menu->process(deltaTime);

		// Render the backBuffer to the screen
		_renderManager->prepareBkg();
		_renderManager->renderMenuToScreen();
		_renderManager->processSubs(deltaTime);
		_renderManager->renderBackbufferToScreen();

		// Update the screen
		if (_rendDelay <= 0)
			_system->updateScreen();
		else
			_rendDelay--;

		// Calculate the frame delay based off a desired frame time
		int delay = _desiredFrameTime - int32(_system->getMillis() - currentTime);
		// Ensure non-negative
		delay = delay < 0 ? 0 : delay;
		if (_halveDelay)
			delay >>= 1;
		_system->delayMillis(delay);
	}

	return Common::kNoError;
}

bool ZVision::askQuestion(const Common::String &str) {
	uint16 msgid = _renderManager->createSubArea();
	_renderManager->updateSubArea(msgid, str);
	_renderManager->processSubs(0);
	_renderManager->renderBackbufferToScreen();
	_clock.stop();

	int result = 0;

	while (result == 0) {
		Common::Event evnt;
		while (_eventMan->pollEvent(evnt)) {
			if (evnt.type == Common::EVENT_KEYDOWN) {
				switch (evnt.kbd.keycode) {
				case Common::KEYCODE_y:
					result = 2;
					break;
				case Common::KEYCODE_n:
					result = 1;
					break;
				default:
					break;
				}
			}
		}
		_system->updateScreen();
		_system->delayMillis(66);
	}
	_renderManager->deleteSubArea(msgid);
	_clock.start();
	return result == 2;
}

void ZVision::delayedMessage(const Common::String &str, uint16 milsecs) {
	uint16 msgid = _renderManager->createSubArea();
	_renderManager->updateSubArea(msgid, str);
	_renderManager->processSubs(0);
	_renderManager->renderBackbufferToScreen();
	_clock.stop();

	uint32 stop_time = _system->getMillis() + milsecs;
	while (_system->getMillis() < stop_time) {
		Common::Event evnt;
		while (_eventMan->pollEvent(evnt)) {
			if (evnt.type == Common::EVENT_KEYDOWN &&
			        (evnt.kbd.keycode == Common::KEYCODE_SPACE ||
			         evnt.kbd.keycode == Common::KEYCODE_RETURN ||
			         evnt.kbd.keycode == Common::KEYCODE_ESCAPE))
				break;
		}
		_system->updateScreen();
		_system->delayMillis(66);
	}
	_renderManager->deleteSubArea(msgid);
	_clock.start();
}

void ZVision::timedMessage(const Common::String &str, uint16 milsecs) {
	uint16 msgid = _renderManager->createSubArea();
	_renderManager->updateSubArea(msgid, str);
	_renderManager->processSubs(0);
	_renderManager->renderBackbufferToScreen();
	_renderManager->deleteSubArea(msgid, milsecs);
}

void ZVision::pauseEngineIntern(bool pause) {
	_mixer->pauseAll(pause);

	if (pause) {
		_clock.stop();
	} else {
		_clock.start();
	}
}

Common::String ZVision::generateSaveFileName(uint slot) {
	return Common::String::format("%s.%02u", _targetName.c_str(), slot);
}

Common::String ZVision::generateAutoSaveFileName() {
	return Common::String::format("%s.auto", _targetName.c_str());
}

void ZVision::setRenderDelay(uint delay) {
	_rendDelay = delay;
}

bool ZVision::canRender() {
	return _rendDelay <= 0;
}

void ZVision::updateRotation() {
	if (_velocity) {
		RenderTable::RenderState renderState = _renderManager->getRenderTable()->getRenderState();
		if (renderState == RenderTable::PANORAMA) {
			int16 st_pos = _scriptManager->getStateValue(StateKey_ViewPos);

			int16 new_pos = st_pos + _velocity * (1 - 2 * 0);

			int16 zero_point = 0;
			if (st_pos >= zero_point && new_pos < zero_point)
				_scriptManager->setStateValue(StateKey_Rounds, _scriptManager->getStateValue(StateKey_Rounds) - 1);
			if (st_pos <= zero_point && new_pos > zero_point)
				_scriptManager->setStateValue(StateKey_Rounds, _scriptManager->getStateValue(StateKey_Rounds) + 1);

			int16 scr_width = _renderManager->getBkgSize().x;
			if (scr_width)
				new_pos %= scr_width;

			if (new_pos < 0)
				new_pos += scr_width;

			_renderManager->setBackgroundPosition(new_pos);
		} else if (renderState == RenderTable::TILT) {
			int16 st_pos = _scriptManager->getStateValue(StateKey_ViewPos);

			int16 new_pos = st_pos + _velocity * (1 - 2 * 0);

			int16 scr_height = _renderManager->getBkgSize().y;
			int16 tilt_gap = _renderManager->getRenderTable()->getTiltGap();

			if (new_pos >= (scr_height - tilt_gap))
				new_pos = scr_height - tilt_gap;
			if (new_pos <= tilt_gap)
				new_pos = tilt_gap;

			_renderManager->setBackgroundPosition(new_pos);
		}
	}
}

} // End of namespace ZVision