aboutsummaryrefslogtreecommitdiff
path: root/engines/neverhood/scene.cpp
blob: 8ed988c0fc5ae75fe1543b072df8d2f9c47a41d2 (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
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/* 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 "neverhood/console.h"
#include "neverhood/scene.h"
#include "neverhood/smackerplayer.h"

namespace Neverhood {

Scene::Scene(NeverhoodEngine *vm, Module *parentModule)
	: Entity(vm, 0), _parentModule(parentModule), _dataResource(vm), _hitRects(NULL),
	_mouseCursorWasVisible(true) {

	_isKlaymenBusy = false;
	_doConvertMessages = false;
	_messageList = NULL;
	_rectType = 0;
	_mouseClickPos.x = 0;
	_mouseClickPos.y = 0;
	_mouseClicked = false;
	_rectList = NULL;
	_klaymen = NULL;
	_mouseCursor = NULL;
	_palette = NULL;
	_background = NULL;
	clearHitRects();
	clearCollisionSprites();
	_vm->_screen->setFps(24);
	_vm->_screen->setSmackerDecoder(NULL);
	_canAcceptInput = true;
	_messageList2 = NULL;
	_smackerPlayer = NULL;
	_isMessageListBusy = false;
	_messageValue = -1;
	_messageListStatus = 0;
	_messageListCount = 0;
	_messageListIndex = 0;

	_backgroundFileHash = _cursorFileHash = 0;

	SetUpdateHandler(&Scene::update);
	SetMessageHandler(&Scene::handleMessage);

	_vm->_screen->clearRenderQueue();
}

Scene::~Scene() {

	_vm->_screen->setSmackerDecoder(NULL);

	if (_palette) {
		removeEntity(_palette);
		delete _palette;
	}

	// Delete all entities
	for (Common::Array<Entity*>::iterator iter = _entities.begin(); iter != _entities.end(); iter++)
		delete *iter;

	// Don't delete surfaces since they always belong to an entity

	// Purge the resources after each scene
	_vm->_res->purgeResources();

}

void Scene::draw() {
	if (_smackerPlayer) {
		if (_smackerPlayer->getSurface())
			_smackerPlayer->getSurface()->draw();
	} else {
		for (Common::Array<BaseSurface*>::iterator iter = _surfaces.begin(); iter != _surfaces.end(); iter++)
			(*iter)->draw();
	}
}

void Scene::addEntity(Entity *entity) {
	int index = 0, insertIndex = -1;
	for (Common::Array<Entity*>::iterator iter = _entities.begin(); iter != _entities.end(); iter++) {
		if ((*iter)->getPriority() > entity->getPriority()) {
			insertIndex = index;
			break;
		}
		index++;
	}
	if (insertIndex >= 0)
		_entities.insert_at(insertIndex, entity);
	else
		_entities.push_back(entity);
}

bool Scene::removeEntity(Entity *entity) {
	for (uint index = 0; index < _entities.size(); index++)
		if (_entities[index] == entity) {
			_entities.remove_at(index);
			return true;
		}
	return false;
}

void Scene::addSurface(BaseSurface *surface) {
	if (surface) {
		int index = 0, insertIndex = -1;
		for (Common::Array<BaseSurface*>::iterator iter = _surfaces.begin(); iter != _surfaces.end(); iter++) {
			if ((*iter)->getPriority() > surface->getPriority()) {
				insertIndex = index;
				break;
			}
			index++;
		}
		if (insertIndex >= 0)
			_surfaces.insert_at(insertIndex, surface);
		else
			_surfaces.push_back(surface);
	}
}

bool Scene::removeSurface(BaseSurface *surface) {
	for (uint index = 0; index < _surfaces.size(); index++) {
		if (_surfaces[index] == surface) {
			_surfaces.remove_at(index);
			return true;
		}
	}
	return false;
}

void Scene::printSurfaces(Console *con) {
	for (uint index = 0; index < _surfaces.size(); index++) {
		NDrawRect drawRect = _surfaces[index]->getDrawRect();
		NRect clipRect = _surfaces[index]->getClipRect();
		int priority = _surfaces[index]->getPriority();
		con->debugPrintf("%d ('%s'): Priority %d, draw rect (%d, %d, %d, %d), clip rect (%d, %d, %d, %d)\n",
			index, _surfaces[index]->getName().c_str(), priority,
			drawRect.x, drawRect.y, drawRect.x2(), drawRect.y2(),
			clipRect.x1, clipRect.y1, clipRect.x2, clipRect.y2);
	}
}

Sprite *Scene::addSprite(Sprite *sprite) {
	addEntity(sprite);
	addSurface(sprite->getSurface());
	return sprite;
}

void Scene::removeSprite(Sprite *sprite) {
	removeSurface(sprite->getSurface());
	removeEntity(sprite);
}

void Scene::setSurfacePriority(BaseSurface *surface, int priority) {
	surface->setPriority(priority);
	if (removeSurface(surface))
		addSurface(surface);
}

void Scene::setSpriteSurfacePriority(Sprite *sprite, int priority) {
	if (sprite)
		setSurfacePriority(sprite->getSurface(), priority);
}

void Scene::deleteSprite(Sprite **sprite) {
	removeCollisionSprite(*sprite);
	removeSurface((*sprite)->getSurface());
	removeEntity(*sprite);
	delete *sprite;
	*sprite = NULL;
}

Background *Scene::addBackground(Background *background) {
	addEntity(background);
	addSurface(background->getSurface());
	return background;
}

void Scene::setBackground(uint32 fileHash) {
	_background = addBackground(new Background(_vm, fileHash, 0, 0));
	_backgroundFileHash = fileHash;
}

void Scene::changeBackground(uint32 fileHash) {
	_background->load(fileHash);
}

void Scene::setPalette(uint32 fileHash) {
	_palette = fileHash ? new Palette(_vm, fileHash) : new Palette(_vm);
	_palette->usePalette();
}

void Scene::setHitRects(uint32 id) {
	setHitRects(_vm->_staticData->getHitRectList(id));
}

void Scene::setHitRects(HitRectList *hitRects) {
	_hitRects = hitRects;
}

Sprite *Scene::insertStaticSprite(uint32 fileHash, int surfacePriority) {
	return addSprite(new StaticSprite(_vm, fileHash, surfacePriority));
}

void Scene::insertScreenMouse(uint32 fileHash, const NRect *mouseRect) {
	NRect rect = NRect::make(-1, -1, -1, -1);
	if (mouseRect)
		rect = *mouseRect;
	insertMouse(new Mouse(_vm, fileHash, rect));
	_cursorFileHash = fileHash;
}

void Scene::insertPuzzleMouse(uint32 fileHash, int16 x1, int16 x2) {
	insertMouse(new Mouse(_vm, fileHash, x1, x2));
	_cursorFileHash = fileHash;
}

void Scene::insertNavigationMouse(uint32 fileHash, int type) {
	insertMouse(new Mouse(_vm, fileHash, type));
	_cursorFileHash = fileHash;
}

void Scene::showMouse(bool visible) {
	_mouseCursor->getSurface()->setVisible(visible);
}

void Scene::changeMouseCursor(uint32 fileHash) {
	_mouseCursor->load(fileHash);
	_mouseCursor->updateCursor();
}

SmackerPlayer *Scene::addSmackerPlayer(SmackerPlayer *smackerPlayer) {
	addEntity(smackerPlayer);
	addSurface(smackerPlayer->getSurface());
	return smackerPlayer;
}

void Scene::update() {

	if (_mouseClicked) {
		if (_klaymen) {
			if (_canAcceptInput &&
				_klaymen->hasMessageHandler() &&
				sendMessage(_klaymen, 0x1008, 0) != 0 &&
				queryPositionSprite(_mouseClickPos.x, _mouseClickPos.y)) {
				_mouseClicked = false;
			} else if (_canAcceptInput &&
				_klaymen->hasMessageHandler() &&
				sendMessage(_klaymen, 0x1008, 0) != 0) {
				_mouseClicked = !queryPositionRectList(_mouseClickPos.x, _mouseClickPos.y);
			}
		} else if (queryPositionSprite(_mouseClickPos.x, _mouseClickPos.y)) {
			_mouseClicked = false;
		}
	}

	processMessageList();

	// Update all entities
	for (Common::Array<Entity*>::iterator iter = _entities.begin(); iter != _entities.end(); iter++)
		(*iter)->handleUpdate();

}

void Scene::leaveScene(uint32 result) {
	sendMessage(_parentModule, 0x1009, result);
}

uint32 Scene::handleMessage(int messageNum, const MessageParam &param, Entity *sender) {
	switch (messageNum) {
	case NM_MOUSE_MOVE:
		if (_mouseCursor && _mouseCursor->hasMessageHandler())
			sendMessage(_mouseCursor, 0x4002, param);
		break;
	case NM_MOUSE_CLICK:
		_mouseClicked = true;
		_mouseClickPos = param.asPoint();
		break;
	case 0x0006:
		sendMessage(_parentModule, 0x1009, param);
		break;
	case 0x1006:
		// Sent by Klaymen when its animation sequence has finished
		if (_isKlaymenBusy) {
			_isKlaymenBusy = false;
			if (_messageListIndex == _messageListCount) {
				// If the current message list was processed completely,
				// sent Klaymen into the idle state.
				sendMessage(_klaymen, NM_KLAYMEN_STAND_IDLE, 0);
			} else {
				// Else continue with the next message in the current message list
				processMessageList();
			}
		}
		break;
	case 0x1007:
		// This isn't sent by any code, check if it's in a message list
		// This cancels the current message list and sets Klaymen into the idle state.
		if (_isKlaymenBusy) {
			_isKlaymenBusy = false;
			_messageList = NULL;
			sendMessage(_klaymen, NM_KLAYMEN_STAND_IDLE, 0);
		}
		break;
	case NM_MOUSE_HIDE:
		if (_mouseCursor) {
			_mouseCursorWasVisible = _mouseCursor->getSurface()->getVisible();
			_mouseCursor->getSurface()->setVisible(false);
		}
		break;
	case NM_MOUSE_SHOW:
		if (_mouseCursorWasVisible && _mouseCursor) {
			_mouseCursor->getSurface()->setVisible(true);
		}
		break;
	case NM_PRIORITY_CHANGE:
		// Set the sender's surface priority
		setSurfacePriority(((Sprite*)sender)->getSurface(), param.asInteger());
		break;
	}
	return 0;
}

bool Scene::queryPositionSprite(int16 mouseX, int16 mouseY) {
	for (uint i = 0; i < _collisionSprites.size(); i++) {
		Sprite *sprite = _collisionSprites[i];
		if (sprite->hasMessageHandler() && sprite->isPointInside(mouseX, mouseY) &&
			sendPointMessage(sprite, 0x1011, _mouseClickPos) != 0) {
			return true;
		}
	}
	return false;
}

bool Scene::queryPositionRectList(int16 mouseX, int16 mouseY) {
	int16 klaymenX = _klaymen->getX();
	int16 klaymenY = _klaymen->getY();
	if (_rectType == 1) {
		RectList &rectList = *_rectList;
		for (uint i = 0; i < rectList.size(); i++) {
			debug(2, "(%d, %d) ? (%d, %d, %d, %d)", klaymenX, klaymenY, rectList[i].rect.x1, rectList[i].rect.y1, rectList[i].rect.x2, rectList[i].rect.y2);
			if (rectList[i].rect.contains(klaymenX, klaymenY)) {
				for (uint j = 0; j < rectList[i].subRects.size(); j++) {
					debug(2, "  (%d, %d) ? (%d, %d, %d, %d)", mouseX, mouseY, rectList[i].subRects[j].rect.x1, rectList[i].subRects[j].rect.y1, rectList[i].subRects[j].rect.x2, rectList[i].subRects[j].rect.y2);
					if (rectList[i].subRects[j].rect.contains(mouseX, mouseY)) {
						return setMessageList2(rectList[i].subRects[j].messageListId);
					}
				}
			}
		}
	} else if (_rectType == 2) {
		MessageList *messageList = _dataResource.getMessageListAtPos(klaymenX, klaymenY, mouseX, mouseY);
		if (messageList && messageList->size())
			setMessageList2(messageList, true, true);
	}
	return true;
}

void Scene::setMessageList(uint32 id, bool canAcceptInput, bool doConvertMessages) {
	setMessageList(_vm->_staticData->getMessageList(id), canAcceptInput, doConvertMessages);
}

void Scene::setMessageList(MessageList *messageList, bool canAcceptInput, bool doConvertMessages) {
	_messageList = messageList;
	_messageListCount = _messageList ? _messageList->size() : 0;
	_messageListIndex = 0;
	_isKlaymenBusy = false;
	_canAcceptInput = canAcceptInput;
	_doConvertMessages = doConvertMessages;
	_messageListStatus = 1;
	sendMessage(_klaymen, 0x101C, 0);
}

bool Scene::setMessageList2(uint32 id, bool canAcceptInput, bool doConvertMessages) {
	return setMessageList2(_vm->_staticData->getMessageList(id), canAcceptInput, doConvertMessages);
}

bool Scene::setMessageList2(MessageList *messageList, bool canAcceptInput, bool doConvertMessages) {
	if (_messageListStatus == 0 ||
		(_messageListStatus == 1 && messageList != _messageList2) ||
		(_messageListStatus == 2 && messageList == _messageList2)) {
		// NOTE Skipped unneeded resource preloading code
		_messageValue = -1;
		_messageList2 = messageList;
		setMessageList(messageList, canAcceptInput, doConvertMessages);
		return true;
	}
	return false;
}

bool Scene::isMessageList2(uint32 id) {
	return _messageList2 == _vm->_staticData->getMessageList(id);
}

void Scene::processMessageList() {
	debug(7, "Scene::processMessageList() _isMessageListBusy = %d; _isKlaymenBusy = %d", _isMessageListBusy, _isKlaymenBusy);

	if (_isMessageListBusy || _isKlaymenBusy)
		return;

	_isMessageListBusy = true;

	if (!_messageList) {
		_messageList2 = NULL;
		_messageListStatus = 0;
	}

	if (_messageList && _klaymen) {

#if 0
		debug("MessageList: %p, %d", (void*)_messageList, _messageList->size());
		for (uint i = 0; i < _messageList->size(); ++i) {
			if (i == _messageListIndex) debugN("**"); else debugN("  ");
			debug("(%08X, %08X)", (*_messageList)[i].messageNum, (*_messageList)[i].messageValue);
		}
		debug("--------------------------------");
#endif

		while (_messageList && _messageListIndex < _messageListCount && !_isKlaymenBusy) {
			uint32 messageNum = (*_messageList)[_messageListIndex].messageNum;
			uint32 messageParam = (*_messageList)[_messageListIndex].messageValue;

			++_messageListIndex;
			if (_messageListIndex == _messageListCount)
				sendMessage(_klaymen, 0x1021, 0);
			if (_doConvertMessages)
				messageNum = convertMessageNum(messageNum);
			if (messageNum == 0x1009 || messageNum == 0x1024) {
				sendMessage(_parentModule, messageNum, messageParam);
			} else if (messageNum == 0x100A) {
				_messageValue = messageParam;
				sendMessage(_parentModule, messageNum, messageParam);
			} else if (messageNum == 0x4001) {
				_isKlaymenBusy = true;
				sendPointMessage(_klaymen, 0x4001, _mouseClickPos);
			} else if (messageNum == 0x100D) {
				if (this->hasMessageHandler() && sendMessage(this, NM_ANIMATION_START, messageParam) != 0)
					continue;
			} else if (messageNum == 0x101A) {
				_messageListStatus = 0;
			} else if (messageNum == 0x101B) {
				_messageListStatus = 2;
			} else if (messageNum == 0x1020) {
				_canAcceptInput = false;
			} else if (messageNum >= 0x2000 && messageNum <= 0x2FFF) {
				if (this->hasMessageHandler() && sendMessage(this, messageNum, messageParam) != 0) {
					_isMessageListBusy = false;
					return;
				}
			} else if (messageNum != 0x4003) {
				_isKlaymenBusy = true;
				if (_klaymen->hasMessageHandler() && sendMessage(_klaymen, messageNum, messageParam) != 0) {
					_isKlaymenBusy = false;
				}
			}
			if (_messageListIndex == _messageListCount) {
				_canAcceptInput = true;
				_messageList = NULL;
			}
		}
	}

	_isMessageListBusy = false;

}

void Scene::cancelMessageList() {
	_isKlaymenBusy = false;
	_messageList = NULL;
	_canAcceptInput = true;
	sendMessage(_klaymen, NM_KLAYMEN_STAND_IDLE, 0);
}

void Scene::setRectList(uint32 id) {
	setRectList(_vm->_staticData->getRectList(id));
}

void Scene::setRectList(RectList *rectList) {
	_rectList = rectList;
	_rectType = 1;
}

void Scene::clearRectList() {
	_rectList = NULL;
	_rectType = 0;
}

void Scene::loadHitRectList() {
	HitRectList *hitRectList = _dataResource.getHitRectList();
	if (hitRectList) {
		_hitRectList = *hitRectList;
		setHitRects(&_hitRectList);
	}
}

void Scene::loadDataResource(uint32 fileHash) {
	_dataResource.load(fileHash);
	_rectType = 2;
	if (_klaymen)
		_klaymen->loadDataResource(fileHash);
}

uint16 Scene::convertMessageNum(uint32 messageNum) {
	switch (messageNum) {
	case 0x00004004:
		return 0x4001;
	case 0x00000083:
		return 0x100A;
	case 0x044001C8:
		return 0x481C;
	case 0x02420480:
		return 0x4818;
	case 0x08004025:
		return 0x100D;
	case 0x04404281:
		return 0x4824;
	case 0x08400880:
		return 0x4825;
	case 0x08209081:
		return 0x4823;
	case 0x24000060:
		return 0x1009;
	case 0x42002200:
		return 0x4004;
	case 0x428D4894:
		return 0x101A;
	}
	return 0x1000;
}

void Scene::clearHitRects() {
	_hitRects = NULL;
}

HitRect *Scene::findHitRectAtPos(int16 x, int16 y) {
	static HitRect kDefaultHitRect = {NRect(), 0x5000};
	if (_hitRects)
		for (HitRectList::iterator it = _hitRects->begin(); it != _hitRects->end(); it++)
			if ((*it).rect.contains(x, y))
				return &(*it);
	return &kDefaultHitRect;
}

void Scene::addCollisionSprite(Sprite *sprite) {
	int index = 0, insertIndex = -1;
	for (Common::Array<Sprite*>::iterator iter = _collisionSprites.begin(); iter != _collisionSprites.end(); iter++) {
		if ((*iter)->getPriority() > sprite->getPriority()) {
			insertIndex = index;
			break;
		}
		index++;
	}
	if (insertIndex >= 0)
		_collisionSprites.insert_at(insertIndex, sprite);
	else
		_collisionSprites.push_back(sprite);
}

void Scene::removeCollisionSprite(Sprite *sprite) {
	for (uint index = 0; index < _collisionSprites.size(); index++) {
		if (_collisionSprites[index] == sprite) {
			_collisionSprites.remove_at(index);
			break;
		}
	}
}

void Scene::clearCollisionSprites() {
	_collisionSprites.clear();
}

void Scene::checkCollision(Sprite *sprite, uint16 flags, int messageNum, uint32 messageParam) {
	for (Common::Array<Sprite*>::iterator iter = _collisionSprites.begin(); iter != _collisionSprites.end(); iter++) {
		Sprite *collSprite = *iter;
		if ((sprite->getFlags() & flags) && collSprite->checkCollision(sprite->getCollisionBounds())) {
			sprite->sendMessage(collSprite, messageNum, messageParam);
		}
	}
}

void Scene::insertMouse(Mouse *mouseCursor) {
	if (_mouseCursor)
		deleteSprite((Sprite**)&_mouseCursor);
	_mouseCursor = mouseCursor;
	addEntity(_mouseCursor);
}

// StaticScene

StaticScene::StaticScene(NeverhoodEngine *vm, Module *parentModule, uint32 backgroundFileHash, uint32 cursorFileHash)
	: Scene(vm, parentModule) {

	SetMessageHandler(&StaticScene::handleMessage);

	setBackground(backgroundFileHash);
	setPalette(backgroundFileHash);
	insertPuzzleMouse(cursorFileHash, 20, 620);
}

uint32 StaticScene::handleMessage(int messageNum, const MessageParam &param, Entity *sender) {
	Scene::handleMessage(messageNum, param, sender);
	switch (messageNum) {
	case NM_MOUSE_CLICK:
		if (param.asPoint().x <= 20 || param.asPoint().x >= 620)
			leaveScene(0);
		break;
	}
	return 0;
}

} // End of namespace Neverhood