File: scene.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (572 lines) | stat: -rw-r--r-- 15,182 bytes parent folder | download | duplicates (2)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/events.h"

#include "hypno/grammar.h"
#include "hypno/hypno.h"

namespace Hypno {

extern int parse_mis(const char *);

const char *sceneVariables[] = {
	"GS_NONE",
	"GS_SCTEXT",
	"GS_AMBIENT",
	"GS_MUSIC",
	"GS_VOLUME",
	"GS_MOUSESPEED",
	"GS_MOUSEON",
	"GS_LEVELCOMPLETE",
	"GS_LEVELWON",
	"GS_CHEATS",
	"GS_SWITCH0",
	"GS_SWITCH1",
	"GS_SWITCH2",
	"GS_SWITCH3",
	"GS_SWITCH4",
	"GS_SWITCH5",
	"GS_SWITCH6",
	"GS_SWITCH7",
	"GS_SWITCH8",
	"GS_SWITCH9",
	"GS_SWITCH10",
	"GS_SWITCH11",
	"GS_SWITCH12",
	"GS_COMBATJSON",
	"GS_COMBATLEVEL",
	"GS_PUZZLELEVEL",
	nullptr
};

void HypnoEngine::loadSceneLevel(const Common::String &current, const Common::String &next, const Common::String &prefix) {
	debugC(1, kHypnoDebugParser, "Parsing %s", current.c_str());
	Common::Path name = convertPath(current);

	Common::File test;
	if (!test.open(name))
		error("Failed to open %s", name.toString().c_str());

	const uint32 fileSize = test.size();
	char *buf = (char *)malloc(fileSize + 1);
	test.read(buf, fileSize);
	test.close();
	buf[fileSize] = '\0';
	debugC(1, kHypnoDebugParser, "%s", buf);
	parse_mis(buf);
	Scene *level = new Scene();
	level->prefix = prefix;
	level->levelIfWin = next;
	level->hots = *g_parsedHots;
	_levels[name.toString('/')] = level;
	free(buf);
}

void HypnoEngine::loadSceneLevel(const char *buf, const Common::String &name, const Common::String &next, const Common::String &prefix) {
	debugC(1, kHypnoDebugParser, "Parsing %s", name.c_str());
	debugC(1, kHypnoDebugParser, "%s", buf);
	parse_mis(buf);
	Scene *level = new Scene();
	level->prefix = prefix;
	level->levelIfWin = next;
	level->hots = *g_parsedHots;
	_levels[name] = level;
}

void HypnoEngine::resetSceneState() {
	uint32 i = 0;
	while (sceneVariables[i]) {
		// Preserve difficulty level variables
		if (sceneVariables[i] != Common::String("GS_COMBATLEVEL") && sceneVariables[i] != Common::String("GS_PUZZLELEVEL"))
			_sceneState[sceneVariables[i]] = 0;
		i++;
	}
	_intros.clear();
}

bool HypnoEngine::checkSceneCompleted() {
	return _sceneState["GS_LEVELCOMPLETE"] || _sceneState["GS_LEVELWON"];
}

bool HypnoEngine::checkLevelWon() {
	return _sceneState["GS_LEVELWON"];
}

// Hotspots

void HypnoEngine::clickedHotspot(Common::Point mousePos) {
	Hotspots *hots = stack.back();
	Hotspot selected(MakeHotspot);
	bool found = false;
	int rs = 100000000;
	int cs = 0;
	for (Hotspots::const_iterator it = hots->begin(); it != hots->end(); ++it) {
		const Hotspot h = *it;
		cs = h.rect.width() * h.rect.height();
		if (h.rect.contains(mousePos)) {
			if (cs < rs) {
				selected = h;
				found = true;
				rs = cs;
			}
		}
	}
	if (selected.type == MakeMenu) {
		if (isDemo()) {
			_nextLevel = "sixdemo/mis/demo.mis";
			resetSceneState();
		} else // TODO: remove when proper escape to main menu is implemented
			openMainMenuDialog();
		return;
	}

	if (!found)
		return;

	if (selected.smenu) {
		if (selected.smenu->empty())
			error("Invalid menu selected");
		_nextHotsToAdd = selected.smenu;
	}

	_videosPlaying.clear();
	_nextParallelVideoToPlay.clear();
	_nextSequentialVideoToPlay.clear();

	bool cont = true;
	for (Actions::const_iterator itt = selected.actions.begin(); itt != selected.actions.end() && cont; ++itt) {
		Action *action = *itt;
		switch (action->type) {
		case ChangeLevelAction:
			runChangeLevel((ChangeLevel *)action);
			break;

		case EscapeAction:
			runEscape();
			break;

		case CutsceneAction:
			runCutscene((Cutscene *)action);
			break;

		case PlayAction:
			runPlay((Play *)action);
			break;

		case SoundAction:
			runSound((Sound *)action);
			break;

		case WalNAction:
			runWalN((WalN *)action);
			break;

		case GlobalAction:
			cont = runGlobal((Global *)action);
			break;

		case TalkAction:
			runTalk((Talk *)action);
			break;

		case SaveAction:
			runSave((Save *)action);
			break;

		case LoadAction:
			runLoad((Load *)action);
			break;

		case LoadCheckpointAction:
			runLoadCheckpoint((LoadCheckpoint *)action);
			break;

		case QuitAction:
			runQuit((Quit *)action);
			break;

		case AmbientAction:
			runAmbient((Ambient *)action);
			break;

		case PaletteAction:
			runPalette((Palette *)action);
			break;

		case SwapPointerAction:
			runSwapPointer((SwapPointer *)action);
			break;

		default:
			break;
		}
	}
}

bool HypnoEngine::hoverHotspot(Common::Point mousePos) {
	Hotspots *hots = stack.back();
	Hotspot selected(MakeHotspot);
	bool found = false;
	int rs = 100000000;
	for (Hotspots::const_iterator it = hots->begin(); it != hots->end(); ++it) {
		const Hotspot h = *it;
		if (h.type != MakeHotspot)
			continue;

		int cs = h.rect.width() * h.rect.height();
		if (h.rect.contains(mousePos)) {
			if (cs < rs) {
				selected = h;
				found = true;
				rs = cs;
			}
		}
	}
	if (found) {
		for (Actions::const_iterator itt = selected.actions.begin(); itt != selected.actions.end(); ++itt) {
			Action *action = *itt;
			switch (action->type) {
			case MiceAction:
				runMice((Mice *)action);
				break;
			default:
				break;
			}
		}
		return true;
	}
	return false;
}

Common::String HypnoEngine::findNextLevel(const Transition *trans) { error("Function \"%s\" not implemented", __FUNCTION__); }

void HypnoEngine::runTransition(Transition *trans) {
	Common::String nextLevel = findNextLevel(trans);
	if (!trans->frameImage.empty()) {
		// This is only used in Wetlands, and therefore, resolution should be 320x200
		changeScreenMode("320x200");
		debugC(1, kHypnoDebugScene, "Rendering %s frame in transaction", trans->frameImage.c_str());
		loadImage(trans->frameImage, 0, 0, false, true, trans->frameNumber);
		drawScreen();
		Common::String *ptr = new Common::String(nextLevel);
		if (!startAlarm(2 * 1000000, ptr)) // 2 seconds
			error("Failed to install alarm");
	} else
		_nextLevel = nextLevel;
}

void HypnoEngine::runScene(Scene *scene) {
	changeScreenMode(scene->resolution);
	_refreshConversation = false;
	Common::Event event;
	Common::Point mousePos;
	Common::List<uint32> videosToRemove;
	bool enableLoopingVideos = true;
	int32 lastCountdown = 0;
	// These variables are always resetted
	_sceneState["GS_LEVELCOMPLETE"] = 0;
	_sceneState["GS_LEVELWON"] = 0;

	stack.clear();
	_nextHotsToAdd = &scene->hots;
	defaultCursor();

	while (!shouldQuit() && _nextLevel.empty()) {

		if (_timerStarted && _videosPlaying.empty() && !_nextHotsToRemove) {

			if (lastCountdown == _countdown) {
			} else if (_countdown > 0) {
				uint32 c = 251; // red
				if (stack.size() > 0)
					runMenu(stack.back());
				uint32 minutes = _countdown / 60;
				uint32 seconds = _countdown % 60;
				drawString("console", Common::String::format("TIME: %d:%d", minutes, seconds), 80, 10, 60, c);
				drawScreen();
			} else {
				assert(!scene->levelIfLose.empty());
				_nextLevel = scene->levelIfLose;
				debugC(1, kHypnoDebugScene, "Finishing level with timeout and jumping to %s", _nextLevel.c_str());
				resetSceneState();
				removeTimers();
				_defaultCursorIdx = 0;
				continue;
			}
			lastCountdown = _countdown;
		}

		while (g_system->getEventManager()->pollEvent(event)) {
			mousePos = g_system->getEventManager()->getMousePos();
			// Events
			switch (event.type) {
			case Common::EVENT_KEYDOWN:
				if (event.kbd.keycode == Common::KEYCODE_ESCAPE) {
					for (Videos::iterator it = _videosPlaying.begin(); it != _videosPlaying.end(); ++it) {
						if (it->decoder) {
							skipVideo(*it);
							if (it->scaled) {
								runMenu(stack.back());
								drawScreen();
							}
						}
					}
					_videosPlaying.clear();

					if (!_conversation.empty())
						_refreshConversation = true;
				}

				break;

			case Common::EVENT_QUIT:
			case Common::EVENT_RETURN_TO_LAUNCHER:
				break;

			case Common::EVENT_RBUTTONDOWN:
				if (stack.empty())
					break;
				if (!_conversation.empty()) {
					rightClickedConversation(mousePos);
					break;
				}
				break;

			case Common::EVENT_LBUTTONDOWN:
				if (stack.empty())
					break;
				if (!_conversation.empty()) {
					leftClickedConversation(mousePos);
					break;
				}
				if (!_nextHotsToAdd && !_nextHotsToRemove /*&& _videosPlaying.empty()*/) {
					clickedHotspot(mousePos);
					drawScreen();
				}
				break;

			case Common::EVENT_MOUSEMOVE:
				// Reset cursor to default
				// changeCursor("default");
				// The following functions will return true
				// if the cursor is changed

				if (!_conversation.empty() && !hoverConversation(mousePos))
					defaultCursor();

				if (stack.empty() || !_conversation.empty() || !_videosPlaying.empty())
					break;

				if (!hoverHotspot(mousePos))
					defaultCursor();
				break;

			default:
				break;
			}
		}

		if (_refreshConversation && !_conversation.empty() &&
			_nextSequentialVideoToPlay.empty() &&
			_nextParallelVideoToPlay.empty() &&
			_videosPlaying.empty()) {
			showConversation();
			runMenu(stack.back(), true);
			drawScreen();
			_refreshConversation = false;
		}

		// Movies
		if (!_nextParallelVideoToPlay.empty()) {
			for (Videos::iterator it = _nextParallelVideoToPlay.begin(); it != _nextParallelVideoToPlay.end(); ++it) {
				playVideo(*it);
				if (it->loop)
					_videosLooping.push_back(*it);
				else
					_videosPlaying.push_back(*it);
			}
			_nextParallelVideoToPlay.clear();
		}

		if (!_nextSequentialVideoToPlay.empty() && _videosPlaying.empty()) {
			MVideo *it = _nextSequentialVideoToPlay.begin();
			playVideo(*it);
			if (it->loop)
				_videosLooping.push_back(*it);
			else
				_videosPlaying.push_back(*it);
			_nextSequentialVideoToPlay.remove_at(0);
		}

		for (Videos::iterator it = _videosLooping.begin(); it != _videosLooping.end(); ++it) {
			if (it->decoder && _conversation.empty()) {
				if (it->decoder->endOfVideo()) {
					if (it->loop && enableLoopingVideos) {
						it->decoder->rewind();
						it->decoder->start();
					}
				} else if (it->decoder->needsUpdate()) {
					updateScreen(*it);
				}
			}
		}

		uint32 i = 0;
		videosToRemove.clear();
		for (Videos::iterator it = _videosPlaying.begin(); it != _videosPlaying.end(); ++it) {

			if (it->decoder) {
				if (it->decoder->endOfVideo()) {
					if (it->scaled ||
					(  it->decoder->getWidth() == _screenW
					&& it->decoder->getHeight() == _screenH
					&& it->decoder->getCurFrame() > 0)) {
						runMenu(stack.back());
						drawScreen();
					}
					it->decoder->close();
					delete it->decoder;
					it->decoder = nullptr;
					videosToRemove.push_back(i);
				} else if (it->decoder->needsUpdate()) {
					updateScreen(*it);
				}
			}
			i++;
		}
		if (!videosToRemove.empty()) {

			for (Common::List<uint32>::iterator it = videosToRemove.begin(); it != videosToRemove.end(); ++it) {
				debugC(1, kHypnoDebugScene, "removing %d from %d size", *it, _videosPlaying.size());
				_videosPlaying.remove_at(*it);
			}
			debugC(1, kHypnoDebugScene, "Something to play: %d", _videosPlaying.size());
			// Nothing else to play
			if (_videosPlaying.empty() && _nextSequentialVideoToPlay.empty() && !checkSceneCompleted()) {
				if (!_conversation.empty())
					_refreshConversation = true;
			}
		}

		if (checkSceneCompleted() || checkLevelWon()) {
			if (!checkLevelWon() && stack.size() > 1) {
				debugC(1, kHypnoDebugScene, "Executing escape instead of ending the scene");
				runEscape();
				_sceneState["GS_LEVELCOMPLETE"] = 0;
				continue;
			}

			// Make sure all the videos are played before we finish
			enableLoopingVideos = false;
			if (_conversation.empty() &&
				_videosPlaying.empty() &&
				_nextSequentialVideoToPlay.empty() &&
				_nextParallelVideoToPlay.empty()) {

				if (_nextLevel.empty()) {
					assert(!scene->levelIfWin.empty());
					_nextLevel = scene->levelIfWin;
				}

				if (checkLevelWon()) {
					debugC(1, kHypnoDebugScene, "Resetting level variables");
					resetSceneState();
					_checkpoint = _nextLevel;
					_defaultCursorIdx = 0;
				}
				_sceneState["GS_LEVELCOMPLETE"] = 0;

				debugC(1, kHypnoDebugScene, "Finishing level and jumping to %s", _nextLevel.c_str());
				continue;
			}
		}

		if (_music.empty() && !scene->music.empty() && _videosPlaying.empty() && _nextSequentialVideoToPlay.empty()) {
			_music = scene->music;
			playSound(_music, 0, scene->musicRate);
		}

		if (!_videosPlaying.empty() || !_videosLooping.empty() || !_nextSequentialVideoToPlay.empty()) {
			drawScreen();
			continue;
		}

		if (_nextHotsToRemove) {
			debugC(1, kHypnoDebugScene, "Removing a hotspot list!");
			stack.pop_back();
			runMenu(stack.back());
			_nextHotsToRemove = nullptr;
			drawScreen();
		} else if (_nextHotsToAdd) {
			debugC(1, kHypnoDebugScene, "Adding a hotspot list!");
			stack.push_back(_nextHotsToAdd);
			runMenu(stack.back());
			_nextHotsToAdd = nullptr;
			drawScreen();
		}

		g_system->updateScreen();
		g_system->delayMillis(30);
	}

	// Deallocate videos
	for (Videos::iterator it = _videosLooping.begin(); it != _videosLooping.end(); ++it) {
		if (it->decoder)
			skipVideo(*it);
	}

	for (Videos::iterator it = _videosPlaying.begin(); it != _videosPlaying.end(); ++it) {
		if (it->decoder)
			skipVideo(*it);
	}

	for (Videos::iterator it = _nextParallelVideoToPlay.begin(); it != _nextParallelVideoToPlay.end(); ++it) {
		if (it->decoder)
			skipVideo(*it);
	}

	for (Videos::iterator it = _nextSequentialVideoToPlay.begin(); it != _nextSequentialVideoToPlay.end(); ++it) {
		if (it->decoder)
			skipVideo(*it);
	}

	for (Videos::iterator it = _escapeSequentialVideoToPlay.begin(); it != _escapeSequentialVideoToPlay.end(); ++it) {
		if (it->decoder)
			skipVideo(*it);
	}

	_nextParallelVideoToPlay.clear();
	_nextSequentialVideoToPlay.clear();
	_escapeSequentialVideoToPlay.clear();
	_conversation.clear();

	if (!_keepTimerDuringScenes)
		removeTimers();
}

void HypnoEngine::showConversation() { error("Function \"%s\" not implemented", __FUNCTION__); }
void HypnoEngine::endConversation() { error("Function \"%s\" not implemented", __FUNCTION__); }
void HypnoEngine::rightClickedConversation(const Common::Point &mousePos) { error("Function \"%s\" not implemented", __FUNCTION__); }
void HypnoEngine::leftClickedConversation(const Common::Point &mousePos) { error("Function \"%s\" not implemented", __FUNCTION__); }
bool HypnoEngine::hoverConversation(const Common::Point &mousePos) { error("Function \"%s\" not implemented", __FUNCTION__); }

} // End of namespace Hypno