aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/scene.cpp
blob: a0888f7d6856cab42c478c03fa527fe0a4b939fc (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
/* 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 "mads/scene.h"
#include "mads/compression.h"
#include "mads/mads.h"
#include "mads/nebular/nebular_scenes.h"

namespace MADS {

Scene::Scene(MADSEngine *vm): _vm(vm), _spriteSlots(vm), _action(_vm),
		_dirtyAreas(_vm), _dynamicHotspots(vm), _interface(vm), _messages(vm), 
		_screenObjects(vm), _sequences(vm), _textDisplay(vm) {
	_priorSceneId = 0;
	_nextSceneId = 0;
	_currentSceneId = 0;
	_vocabBuffer = nullptr;
	_sceneLogic = nullptr;
	_sceneInfo = nullptr;
	_animFlag = false;
	_animVal1 = 0;
	_depthStyle = 0;
	_v1A = _v1C = 0;
	_roomChanged = false;
	_reloadSceneFlag = false;
	_destFacing = 0;
	_freeAnimationFlag = false;
	_animation = nullptr;
	_activeAnimation = nullptr;

	_verbList.push_back(VerbInit(VERB_LOOK, 2, 0));
	_verbList.push_back(VerbInit(VERB_TAKE, 2, 0));
	_verbList.push_back(VerbInit(VERB_PUSH, 2, 0));
	_verbList.push_back(VerbInit(VERB_OPEN, 2, 0));
	_verbList.push_back(VerbInit(VERB_PUT, 1, -1));
	_verbList.push_back(VerbInit(VERB_TALKTO, 2, 0));
	_verbList.push_back(VerbInit(VERB_GIVE, 1, 2));
	_verbList.push_back(VerbInit(VERB_PULL, 2, 0));
	_verbList.push_back(VerbInit(VERB_CLOSE, 2, 0));
	_verbList.push_back(VerbInit(VERB_THROW, 1, 2));
}

Scene::~Scene() {
	delete[] _vocabBuffer;
	delete _sceneLogic;
	delete _sceneInfo;
}

void Scene::clearVocab() {
	freeVocab();
	_activeVocabs.clear();
}

void Scene::freeVocab() {
	delete[] _vocabBuffer;
	_vocabBuffer = nullptr;
}

void Scene::addActiveVocab(int vocabId) {
	if (activeVocabIndexOf(vocabId) == -1) {
		assert(_activeVocabs.size() < 200);
		_activeVocabs.push_back(vocabId);
	}
}

int Scene::activeVocabIndexOf(int vocabId) {
	for (uint i = 0; i < _activeVocabs.size(); ++i) {
		if (_activeVocabs[i] == vocabId)
			return i;
	}

	return -1;
}

void Scene::clearSequenceList() {
	_sequences.clear();
}

void Scene::clearMessageList() {
	_messages.clear();
	_talkFont = "*FONTCONV.FF";
	_textSpacing  = -1;
}

void Scene::loadSceneLogic() {
	delete _sceneLogic;

	switch (_vm->getGameID()) {
	case GType_RexNebular:
		_sceneLogic = Nebular::SceneFactory::createScene(this);
		break;
	default:
		error("Unknown game");
	}
}

void Scene::loadScene(int sceneId, const Common::String &prefix, bool palFlag) {
	// Store the previously active scene number and set the new one
	_priorSceneId = _currentSceneId;
	_currentSceneId = sceneId;

	_v1 = 0;
	if (palFlag)
		_vm->_palette->resetGamePalette(18, 10);

	_spriteSlots.clear(false);
	_sequences.clear();
	_messages.clear();

	// TODO: palletteUsage reset?  setPalette(_nullPalette);
	_sceneInfo = SceneInfo::init(_vm);
	_sceneInfo->load(_currentSceneId, _v1, Common::String(), _vm->_game->_v2 ? 17 : 16,
		_depthSurface, _backgroundSurface);

	// Initialise palette animation for the scene
	initPaletteAnimation(_sceneInfo->_palAnimData, false);

	// Copy over nodes
	_nodes.clear();
	for (uint i = 0; i < _sceneInfo->_nodes.size(); ++i)
		_nodes.push_back(_sceneInfo->_nodes[i]);

	// Load hotspots
	loadHotspots();

	// Load vocab
	loadVocab();

	// Load palette usage
	_vm->_palette->_paletteUsage.load(1, 0xF);

	// Load interface
	int flags = _vm->_game->_v2 ? 0x4101 : 0x4100;
	if (!_vm->_textWindowStill)
		flags |= 0x200;

	_animation = Animation::init(_vm, this);
	MSurface surface;
	_animation->load(surface, _interface, prefix, flags, nullptr, nullptr);
	
	_vm->_palette->_paletteUsage.load(0);

	_bandsRange = _sceneInfo->_yBandsEnd - _sceneInfo->_yBandsStart;
	_scaleRange = _sceneInfo->_maxScale - _sceneInfo->_minScale;

	_spriteSlots.clear(false);
	_screenY = 0;
	_interfaceY = MADS_SCENE_HEIGHT;
	_spritesCount = _sprites.size();

	warning("TODO: sub_1EA80 / showMouse");

	warning("TODO: inventory_anim_allocate");
}

void Scene::loadHotspots() {
	File f(Resources::formatName(RESPREFIX_RM, _currentSceneId, ".HH"));
	MadsPack madsPack(&f);

	Common::SeekableReadStream *stream = madsPack.getItemStream(0);
	int count = stream->readUint16LE();
	delete stream;

	stream = madsPack.getItemStream(1);
	_hotspots.clear();
	for (int i = 0; i < count; ++i)
		_hotspots.push_back(Hotspot(*stream));

	delete stream;
	f.close();
}

void Scene::loadVocab() {
	// Add all the verbs to the active vocab list
	for (uint i = 0; i < _verbList.size(); ++i)
		addActiveVocab(_verbList[i]._id);

	// Load the vocabs for any object descriptions and custom actions
	for (uint objIndex = 0; objIndex < _vm->_game->_objects.size(); ++objIndex) {
		InventoryObject &io = _vm->_game->_objects[objIndex];
		addActiveVocab(io._descId);

		for (int vocabIndex = 0; vocabIndex <io._vocabCount; ++vocabIndex) {
			addActiveVocab(io._vocabList[vocabIndex]._vocabId);
		}
	}

	// Load scene hotspot list vocabs and verbs
	for (uint i = 0; i < _hotspots.size(); ++i) {
		addActiveVocab(_hotspots[i]._vocabId);
		if (_hotspots[i]._verbId)
			addActiveVocab(_hotspots[i]._verbId);
	}

	loadVocabStrings();
}

void Scene::loadVocabStrings() {
	freeVocab();
	File f("*VOCAB.DAT");

	char *textStrings = new char[f.size()];
	f.read(textStrings, f.size());

	for (uint strIndex = 0; strIndex < _activeVocabs.size(); ++strIndex) {
		const char *s = textStrings;
		for (int vocabIndex = 0; vocabIndex < _activeVocabs[strIndex]; ++vocabIndex)
			s += strlen(s) + 1;

		_vocabStrings.push_back(s);
	}

	delete[] textStrings;
	f.close();
}

void Scene::initPaletteAnimation(Common::Array<RGB4> &animData, bool animFlag) {
	// Initialise the animation palette and ticks list
	_animTicksList.clear();
	_animPalData.clear();

	for (uint i = 0; i < animData.size(); ++i) {
		_animTicksList.push_back(_vm->_events->getFrameCounter());
		_animPalData.push_back(animData[i]);
	}

	// Save the initial starting palette
	Common::copy(&_vm->_palette->_mainPalette[0], &_vm->_palette->_mainPalette[PALETTE_SIZE],
		&_vm->_palette->_savedPalette[0]);

	// Calculate total
	_animCount = 0;
	for (uint i = 0; i < _animPalData.size(); ++i)
		_animCount += _animPalData[i].r;

	_animVal1 = (_animCount > 16) ? 3 : 0;
	_animFlag = animFlag;
}

bool Scene::getDepthHighBits(const Common::Point &pt) {
	if (_sceneInfo->_depthStyle == 2) {
		return 0;
	} else {
		const byte *p = _depthSurface.getBasePtr(pt.x, pt.y);
		return (*p & 0x70) >> 4;
	}
}

void Scene::loop() {
	_nextSceneId = _currentSceneId;

	while (!_vm->shouldQuit() && !_reloadSceneFlag && _nextSceneId == _currentSceneId) {
		// Handle drawing a game frame
		doFrame();

		if (_vm->_dialogs->_pendingDialog != DIALOG_NONE && !_vm->_game->_abortTimers
			&& _vm->_game->_player._stepEnabled)
			_reloadSceneFlag = true;
	}
}

void Scene::doFrame() {
	Player &player = _vm->_game->_player;
	bool flag = false;

	if (_action._selectedAction || !player._stepEnabled) {
		_action.clear();
		_action._selectedAction = 0;
	}

	if (!_vm->_game->_abortTimers && !player._unk3) {
		if (_dynamicHotspots._changed)
			_dynamicHotspots.refresh();

		_screenObjects.check(player._stepEnabled && !_action._startWalkFlag &&
				!_vm->_game->_abortTimers2);
	}

	if (_action._selectedAction && player._stepEnabled && !_action._startWalkFlag &&
			!_vm->_game->_abortTimers && !player._unk3) {
		_action.startAction();
		if (_action._activeAction._verbId == Nebular::NOUN_LOOK_AT) {
			_action._activeAction._verbId = VERB_LOOK;
			_action._savedSelectedRow = false;
		}

		flag = true;
	}

	if (flag || (_vm->_game->_abortTimers && _vm->_game->_abortTimersMode == ABORTMODE_2)) {
		doPreactions();
	}

	checkStartWalk();
	if (!_vm->_game->_abortTimers2)
		_vm->_events->_currentTimer = _vm->_events->getFrameCounter();

	if ((_action._inProgress && !player._moving && !_action._startWalkFlag &&
			player._newDirection == player._direction) ||
			(_vm->_game->_abortTimers && _vm->_game->_abortTimersMode == ABORTMODE_0)) {
		doAction();
	}

	if (_currentSceneId != _nextSceneId) {
		_freeAnimationFlag = true;
	} else {
		doSceneStep();
		checkKeyboard();

		if (_currentSceneId != _nextSceneId) {
			_freeAnimationFlag = true;
		} else {
			player.nextFrame();

			// Cursor update code
			CursorType cursorId = CURSOR_ARROW;
			if (_action._v83338 == 1 && !_screenObjects._v7FECA &&
					_screenObjects._category == CAT_HOTSPOT) {
				int idx = _screenObjects._selectedObject - _interface._screenObjectsCount;
				if (idx >= (int)_hotspots.size()) {
					idx -= _hotspots.size();
					_vm->_events->_newCursorId = _dynamicHotspots[idx]._cursor;
				} else {
					_vm->_events->_newCursorId = _hotspots[idx]._cursor;
				}

				cursorId = _vm->_events->_newCursorId == CURSOR_NONE ? 
					CURSOR_ARROW : _vm->_events->_newCursorId;
			}

			if (!player._stepEnabled)
				cursorId = CURSOR_WAIT;
			if (cursorId >= _vm->_events->_cursorSprites->getCount())
				cursorId = (CursorType)_vm->_events->_cursorSprites->getCount();
			_vm->_events->_newCursorId = cursorId;

			if (cursorId != _vm->_events->_cursorId) {
				_vm->_events->setCursor(cursorId);
			}

			if (!_vm->_game->_abortTimers) {
				// Handle any active sequences
				_sequences.tick();

				// Handle any active animation
				if (_activeAnimation)
					_activeAnimation->update();
			}

			// If the debugget flag is set, show the mouse position
			if (_vm->_debugger->_showMousePos) {
				Common::Point pt = _vm->_events->mousePos();
				Common::String msg = Common::String::format("(%d,%d)", pt.x, pt.y);
				_messages.add(Common::Point(5, 5), 0x203, 0, 0, 1, msg);
			}

			// TODO: Rest of Scene::doFrame
		}
	}
}

void Scene::leftClick() {
	warning("TODO: Scene::leftClick");
}

void Scene::doPreactions() {
	warning("TODO: Scene::doPreactions");
}

void Scene::doAction() {
	warning("TODO: Scene::doAction");
}

void Scene::checkStartWalk() {
	if (_action._startWalkFlag && _action._walkFlag) {
		_vm->_game->_player.setDest(_destPos, _destFacing);
		_action._startWalkFlag = false;
	}
}

void Scene::doSceneStep() {
	warning("TODO: Scene::doSceneStep");
}

void Scene::checkKeyboard() {
	warning("TODO: Scene::checkKeyboard");
}

void Scene::free() {
	warning("TODO: Scene::free");
}

} // End of namespace MADS