aboutsummaryrefslogtreecommitdiff
path: root/engines/mads/sequence.cpp
blob: cbe0471315af1996248afe59519fc5f2e1c779b6 (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
/* 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/mads.h"
#include "mads/assets.h"
#include "mads/sequence.h"
#include "mads/scene.h"

namespace MADS {

SequenceEntry::SequenceEntry() {
	_spritesIndex = 0;
	_flipped = 0;
	_frameIndex = 0;
	_frameStart = 0;
	_numSprites = 0;
	_animType = ANIMTYPE_NONE;
	_frameInc = 0;
	_depth = 0;
	_scale = 0;
	_dynamicHotspotIndex = -1;
	_field18 = 0;
	_field1A = 0;
	_field1C = 0;
	_field1E = 0;
	_field20 = 0;
	_field22 = 0;
	_triggerCountdown = 0;
	_doneFlag = 0;
	_triggerMode = SEQUENCE_TRIGGER_DAEMON;
	_numTicks = 0;
	_extraTicks = 0;
	_timeout = 0;

	_entries._count = 0;
	Common::fill(&_entries._mode[0], &_entries._mode[SEQUENCE_ENTRY_SUBSET_MAX], SEQUENCE_TRIGGER_EXPIRE);
	Common::fill(&_entries._frameIndex[0], &_entries._frameIndex[SEQUENCE_ENTRY_SUBSET_MAX], 0);
	Common::fill(&_entries._trigger[0], &_entries._trigger[SEQUENCE_ENTRY_SUBSET_MAX], 0);
}

/*------------------------------------------------------------------------*/

#define SEQUENCE_LIST_SIZE 30

SequenceList::SequenceList(MADSEngine *vm) : _vm(vm) {
	// IMPORTANT: Preallocate timer slots. Note that sprite slots refer to entries 
	// in this list by index, so we can't just add or delete entries later
	for (int i = 0; i < SEQUENCE_LIST_SIZE; ++i) {
		SequenceEntry rec;
		rec._active = false;
		rec._dynamicHotspotIndex = -1;
		_entries.push_back(rec);
	}
}

void SequenceList::clear() {
	for (uint i = 0; i < _entries.size(); ++i) {
		_entries[i]._active = false;
		_entries[i]._dynamicHotspotIndex = -1;
	}
}

bool SequenceList::addSubEntry(int index, SequenceTrigger mode, int frameIndex, int trigger) {
	if (_entries[index]._entries._count >= SEQUENCE_ENTRY_SUBSET_MAX)
		return true;

	int subIndex = _entries[index]._entries._count++;
	_entries[index]._entries._mode[subIndex] = mode;
	_entries[index]._entries._frameIndex[subIndex] = frameIndex;
	_entries[index]._entries._trigger[subIndex] = trigger;

	return false;
}

int SequenceList::add(int spriteListIndex, bool flipped, int frameIndex, int triggerCountdown, int delayTicks, int extraTicks, int numTicks,
	int msgX, int msgY, bool nonFixed, int scale, int depth, int frameInc, SpriteAnimType animType, int numSprites,
	int frameStart) {
	Scene &scene = _vm->_game->_scene;

	// Find a free slot
	uint seqIndex = 0;
	while ((seqIndex < _entries.size()) && _entries[seqIndex]._active)
		++seqIndex;
	if (seqIndex == _entries.size())
		error("TimerList full");

	if (frameStart <= 0)
		frameStart = 1;
	if (numSprites == 0)
		numSprites = scene._sprites[spriteListIndex]->getCount();
	if (frameStart == numSprites)
		frameInc = 0;

	// Set the list entry fields
	_entries[seqIndex]._active = true;
	_entries[seqIndex]._spritesIndex = spriteListIndex;
	_entries[seqIndex]._flipped = flipped;
	_entries[seqIndex]._frameIndex = frameIndex;
	_entries[seqIndex]._frameStart = frameStart;
	_entries[seqIndex]._numSprites = numSprites;
	_entries[seqIndex]._animType = animType;
	_entries[seqIndex]._frameInc = frameInc;
	_entries[seqIndex]._depth = depth;
	_entries[seqIndex]._scale = scale;
	_entries[seqIndex]._nonFixed = nonFixed;
	_entries[seqIndex]._msgPos.x = msgX;
	_entries[seqIndex]._msgPos.y = msgY;
	_entries[seqIndex]._numTicks = numTicks;
	_entries[seqIndex]._extraTicks = extraTicks;

	_entries[seqIndex]._timeout = scene._frameStartTime + delayTicks;

	_entries[seqIndex]._triggerCountdown = triggerCountdown;
	_entries[seqIndex]._doneFlag = false;
	_entries[seqIndex]._flags = 0;
	_entries[seqIndex]._dynamicHotspotIndex = -1;
	_entries[seqIndex]._entries._count = 0;
	_entries[seqIndex]._triggerMode = _vm->_game->_triggerSetupMode;

	_entries[seqIndex]._actionNouns = _vm->_game->_scene._action._activeAction;

	return seqIndex;
}

int SequenceList::addTimer(int timeout, int abortVal) {
	Scene &scene = _vm->_game->_scene;
	uint seqIndex;
	for (seqIndex = 0; seqIndex < _entries.size(); ++seqIndex) {
		if (!_entries[seqIndex]._active)
			break;
	}
	assert(seqIndex < _entries.size());

	SequenceEntry &se = _entries[seqIndex];
	se._active = true;
	se._spritesIndex = -1;
	se._numTicks = timeout;
	se._extraTicks = 0;
	se._timeout = scene._frameStartTime + timeout;
	se._triggerCountdown = true;
	se._doneFlag = false;
	se._entries._count = 0;
	se._triggerMode = _vm->_game->_triggerSetupMode;
	se._actionNouns = _vm->_game->_scene._action._activeAction;
	addSubEntry(seqIndex, SEQUENCE_TRIGGER_EXPIRE, 0, abortVal);

	return seqIndex;
}

void SequenceList::remove(int seqIndex) {
	Scene &scene = _vm->_game->_scene;

	if (_entries[seqIndex]._active) {
		if (_entries[seqIndex]._dynamicHotspotIndex >= 0)
			scene._dynamicHotspots.remove(_entries[seqIndex]._dynamicHotspotIndex);
	}

	_entries[seqIndex]._active = false;
	scene._spriteSlots.deleteTimer(seqIndex);
}

void SequenceList::setSpriteSlot(int seqIndex, SpriteSlot &spriteSlot) {
	Scene &scene = _vm->_game->_scene;
	SequenceEntry &timerEntry = _entries[seqIndex];
	SpriteAsset &spriteSet = *scene._sprites[timerEntry._spritesIndex];

	spriteSlot._flags = spriteSet.isBackground() ? IMG_DELTA : IMG_UPDATE;
	spriteSlot._seqIndex = seqIndex;
	spriteSlot._spritesIndex = timerEntry._spritesIndex;
	spriteSlot._frameNumber = timerEntry._flipped ? -timerEntry._frameIndex : timerEntry._frameIndex;
	spriteSlot._depth = timerEntry._depth;
	spriteSlot._scale = timerEntry._scale;

	if (!timerEntry._nonFixed) {
		spriteSlot._position = timerEntry._msgPos;
	} else {
		MSprite *sprite = spriteSet.getFrame(timerEntry._frameIndex - 1);
		spriteSlot._position = sprite->_offset;
	}
}

bool SequenceList::loadSprites(int seqIndex) {
	Scene &scene = _vm->_game->_scene;
	SequenceEntry &seqEntry = _entries[seqIndex];
	int slotIndex;
	bool result = false;
	int idx = -1;

	scene._spriteSlots.deleteTimer(seqIndex);
	if (seqEntry._doneFlag) {
		remove(seqIndex);
		return false;
	}

	if (seqEntry._spritesIndex == -1) {
		// Doesn't have an associated sprite anymore, so mark as done
		seqEntry._doneFlag = true;
	} else if ((slotIndex = scene._spriteSlots.add()) >= 0) {
		SpriteSlot &spriteSlot = scene._spriteSlots[slotIndex];
		setSpriteSlot(seqIndex, spriteSlot);

		if ((seqEntry._flags != 0) || (seqEntry._dynamicHotspotIndex >= 0)) {
			SpriteAsset &spriteSet = *scene._sprites[seqEntry._spritesIndex];
			MSprite *frame = spriteSet.getFrame(seqEntry._frameIndex - 1);
			int width = frame->getWidth() * seqEntry._scale / 200;
			int height = frame->getHeight() * seqEntry._scale / 100;
			Common::Point pt = spriteSlot._position;

			// ToDO: Find out the proper meanings of the following fields
			if (seqEntry._flags & 1) {
				seqEntry._field20 += seqEntry._field18;
				if (seqEntry._field20 >= 100) {
					int v = seqEntry._field20 / 100;
					seqEntry._msgPos.x += v * seqEntry._field1C;
					seqEntry._field20 -= v * 100;
				}

				seqEntry._field22 += seqEntry._field1A;
				if (seqEntry._field22 >= 100) {
					int v = seqEntry._field22 / 100;
					seqEntry._msgPos.y += v * seqEntry._field1E;
					seqEntry._field22 -= v * 100;
				}
			}

			if (seqEntry._flags & 2) {
				if ((pt.x + width) < 0 || (pt.x + width) >= MADS_SCREEN_WIDTH ||
						pt.y < 0 || (pt.y + height) >= MADS_SCENE_HEIGHT) {
					result = true;
					seqEntry._doneFlag = true;
				}
			}

			if (seqEntry._dynamicHotspotIndex >= 0) {
				DynamicHotspot &dynHotspot = scene._dynamicHotspots[seqEntry._dynamicHotspotIndex];

				dynHotspot._bounds.left = MAX(pt.x - width, 0);
				dynHotspot._bounds.top = MAX(pt.y - height, 0);
				dynHotspot._bounds.right = dynHotspot._bounds.left + width;
				dynHotspot._bounds.bottom = dynHotspot._bounds.top + height;

				scene._dynamicHotspots._changed = true;
			}
		}

		// Frame adjustments
		if (seqEntry._frameStart != seqEntry._numSprites)
			seqEntry._frameIndex += seqEntry._frameInc;

		if (seqEntry._frameIndex >= seqEntry._frameStart) {
			if (seqEntry._frameIndex > seqEntry._numSprites) {
				result = true;
				if (seqEntry._animType == ANIMTYPE_CYCLED) {
					//  back to the starting frame (cyclic)
					seqEntry._frameIndex = seqEntry._frameStart;
				} else {
					// Switch into reverse mode
					seqEntry._frameIndex = seqEntry._numSprites - 1;
					seqEntry._frameInc = -1;
				}
			}
		} else {
			// Currently in reverse mode and moved past starting frame
			result = true;

			if (seqEntry._animType == ANIMTYPE_CYCLED)
			{
				// Switch back to forward direction again
				seqEntry._frameIndex = seqEntry._frameStart + 1;
				seqEntry._frameInc = 1;
			} else {
				// Otherwise reset back to last sprite for further reverse animating
				seqEntry._frameIndex = seqEntry._numSprites;
			}
		}

		if (result && (seqEntry._triggerCountdown != 0)) {
			if (--seqEntry._triggerCountdown == 0)
				seqEntry._doneFlag = true;
		}
	} else {
		// Out of sprite display slots, so mark entry as done
		seqEntry._doneFlag = true;
	}

	for (int i = 0; i < seqEntry._entries._count; ++i) {
		switch (seqEntry._entries._mode[i]) {
		case SEQUENCE_TRIGGER_EXPIRE:
		case SEQUENCE_TRIGGER_LOOP:
			if (((seqEntry._entries._mode[i] == SEQUENCE_TRIGGER_EXPIRE) && seqEntry._doneFlag) ||
				((seqEntry._entries._mode[i] == SEQUENCE_TRIGGER_LOOP) && result))
				idx = i;
			break;

		case SEQUENCE_TRIGGER_SPRITE: {
			int v = seqEntry._entries._frameIndex[i];
			if ((v == seqEntry._frameIndex) || (v == 0))
				idx = i;
			break;
		}

		default:
			break;
		}
	}

	if (idx >= 0) {
		_vm->_game->_trigger = seqEntry._entries._trigger[idx];
		_vm->_game->_triggerMode = seqEntry._triggerMode;

		if (seqEntry._triggerMode != SEQUENCE_TRIGGER_DAEMON)
			scene._action._activeAction = seqEntry._actionNouns;
	}

	return result;
}

/**
* Handles counting down entries in the timer list for action
*/
void SequenceList::tick() {
	Scene &scene = _vm->_game->_scene;
	for (uint idx = 0; idx < _entries.size(); ++idx) {
		if ((_vm->_game->_fx == 0) && (_vm->_game->_trigger != 0))
			break;

		SequenceEntry &seqEntry = _entries[idx];
		uint32 currentTimer = scene._frameStartTime;

		if (!seqEntry._active || (currentTimer < seqEntry._timeout))
			continue;

		// Set the next timeout for the timer entry
		seqEntry._timeout = currentTimer + seqEntry._numTicks;

		// Action the sprite
		if (loadSprites(idx)) {
			seqEntry._timeout += seqEntry._extraTicks;
		}
	}
}

void SequenceList::delay(uint32 priorFrameTime, uint32 currentTime) {
	for (uint idx = 0; idx < _entries.size(); ++idx) {
		if (_entries[idx]._active) {
			_entries[idx]._timeout += currentTime - priorFrameTime;
		}
	}
}

void SequenceList::setAnimRange(int seqIndex, int startVal, int endVal) {
	Scene &scene = _vm->_game->_scene;	
	SequenceEntry &seqEntry = _entries[seqIndex];
	SpriteAsset &spriteSet = *scene._sprites[seqEntry._spritesIndex];
	int numSprites = spriteSet.getCount();
	int tempStart, tempEnd;

	switch (startVal) {
	case -2:
		tempStart = numSprites;
		break;
	case -1:
		tempStart = 1;
		break;
	default:
		tempStart = startVal;
		break;
	}

	switch (endVal) {
	case -2:
	case 0:
		tempEnd = numSprites;
		break;
	case -1:
		tempEnd = 1;
		break;
	default:
		tempEnd = endVal;
		break;
	}

	seqEntry._frameStart = tempStart;
	seqEntry._numSprites = tempEnd;

	seqEntry._frameIndex = (seqEntry._frameInc >= 0) ? tempStart : tempEnd;
}

void SequenceList::scan() {
	Scene &scene = _vm->_game->_scene;

	for (uint i = 0; i < _entries.size(); ++i) {
		if (_entries[i]._active && (_entries[i]._spritesIndex != -1)) {
			int idx = scene._spriteSlots.add();
			setSpriteSlot(i, scene._spriteSlots[idx]);
		}
	}
}

/**
* Sets the depth of the specified entry in the sequence list
*/
void SequenceList::setDepth(int seqIndex, int depth) {
	_entries[seqIndex]._depth = depth;
}

void SequenceList::setMsgPosition(int seqIndex, const Common::Point &pt) {
	_entries[seqIndex]._msgPos = pt;
	_entries[seqIndex]._nonFixed = false;
}

int SequenceList::addSpriteCycle(int srcSpriteIdx, bool flipped, int numTicks, int triggerCountdown, int timeoutTicks, int extraTicks) {
	Scene &scene = _vm->_game->_scene;
	MSprite *spriteFrame = scene._sprites[srcSpriteIdx]->getFrame(0);
	int depth = scene._depthSurface.getDepth(Common::Point(
		spriteFrame->_offset.x + (spriteFrame->w / 2), 
		spriteFrame->_offset.y + (spriteFrame->h / 2)));

	return add(srcSpriteIdx, flipped, 1, triggerCountdown, timeoutTicks, extraTicks, numTicks, 0, 0,
		true, 100, depth - 1, 1, ANIMTYPE_CYCLED, 0, 0);
}

int SequenceList::addReverseSpriteCycle(int srcSpriteIdx, bool flipped, int numTicks,
		int triggerCountdown, int timeoutTicks, int extraTicks) {
	Scene &scene = _vm->_game->_scene;

	SpriteAsset *asset = scene._sprites[srcSpriteIdx];
	MSprite *spriteFrame = asset->getFrame(0);
	int depth = scene._depthSurface.getDepth(Common::Point(
		spriteFrame->_offset.x + (spriteFrame->w / 2),
		spriteFrame->_offset.y + (spriteFrame->h / 2)));

	return add(srcSpriteIdx, flipped, asset->getCount(), triggerCountdown, timeoutTicks, extraTicks, 
		numTicks, 0, 0, true, 100, depth - 1, -1, ANIMTYPE_CYCLED, 0, 0);
}


int SequenceList::startCycle(int srcSpriteIndex, bool flipped, int cycleIndex) {
	int result = addSpriteCycle(srcSpriteIndex, flipped, INDEFINITE_TIMEOUT, 0, 0, 0);
	if (result >= 0)
		setAnimRange(result, cycleIndex, cycleIndex);

	return result;
}

int SequenceList::startReverseCycle(int srcSpriteIndex, bool flipped, int numTicks, 
		int triggerCountdown, int timeoutTicks, int extraTicks) {
	SpriteAsset *sprites = _vm->_game->_scene._sprites[srcSpriteIndex];
	MSprite *frame = sprites->getFrame(0);
	int depth = _vm->_game->_scene._depthSurface.getDepth(Common::Point(
		frame->_offset.x + frame->w / 2, frame->_offset.y + frame->h / 2));

	return add(srcSpriteIndex, flipped, 1, triggerCountdown, timeoutTicks, extraTicks,
		numTicks, 0, 0, true, 100, depth - 1, 1, ANIMTYPE_REVERSIBLE, 0, 0);
}

void SequenceList::updateTimeout(int spriteIdx, int seqIndex) {
	Player &player = _vm->_game->_player;
	int timeout;

	if (spriteIdx >= 0)
		timeout = _entries[spriteIdx]._timeout;
	else
		timeout = player._priorTimer + player._ticksAmount;

	if (seqIndex >= 0)
		_entries[seqIndex]._timeout = timeout;
	else
		player._priorTimer = timeout - player._ticksAmount;

}

void SequenceList::setScale(int spriteIdx, int scale) {
	_entries[spriteIdx]._scale = scale;
}

void SequenceList::setMsgLayout(int seqIndex) {
	Player &player = _vm->_game->_player;
	int yp = player._playerPos.y + (player._centerOfGravity * player._currentScale) / 100;
	setMsgPosition(seqIndex, Common::Point(player._playerPos.x, yp));
	setDepth(seqIndex, player._currentDepth);
	setScale(seqIndex, player._currentScale);
	updateTimeout(-1, seqIndex);
}

void SequenceList::setDone(int seqIndex) {
	_entries[seqIndex]._doneFlag = true;
	_entries[seqIndex]._timeout = _vm->_game->_player._priorTimer;
}

} // End of namespace