aboutsummaryrefslogtreecommitdiff
path: root/engines/avalanche/animation.cpp
blob: 57f9cdb7c5f55909ca1830a580848a63f7bea9d6 (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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
/* 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.
 *
 */

/*
 * This code is based on the original source code of Lord Avalot d'Argent version 1.3.
 * Copyright (c) 1994-1995 Mike, Mark and Thomas Thurman.
 */

/* TRIP5	Trippancy V - the sprite animation subsystem */

#include "avalanche/avalanche.h"
#include "avalanche/animation.h"
#include "avalanche/dialogs.h"
#include "avalanche/background.h"
#include "avalanche/sequence.h"
#include "avalanche/timer.h"

#include "common/scummsys.h"
#include "common/textconsole.h"
#include "common/file.h"

namespace Avalanche {

void AnimationType::init(byte spritenum, bool doCheck, Animation *anim) {
	_anim = anim;

	const int32 idshould = -1317732048;

	if (spritenum == 177)
		return; // Already running!

	Common::File inf;
	Common::String filename = Common::String::format("sprite%d.avd", spritenum);
	if (!inf.open(filename))
		error("AVALANCHE: Trip: File not found: %s", filename.c_str());

	inf.seek(177);

	int32 id = inf.readSint32LE();
	if (id != idshould) {
		inf.close();
		return;
	}

	// Replace variable named 'soa' in the original code.
	inf.skip(2);

	if (!_stat._name.empty())
		_stat._name.clear();
	byte nameSize = inf.readByte();
	for (int i = 0; i < nameSize; i++)
		_stat._name += inf.readByte();
	inf.skip(12 - nameSize);

	byte commentSize = inf.readByte();
	for (int i = 0; i < commentSize; i++)
		_stat._comment += inf.readByte();
	inf.skip(16 - commentSize);

	_stat._frameNum = inf.readByte();
	_info._xLength = inf.readByte();
	_info._yLength = inf.readByte();
	_stat._seq = inf.readByte();
	_info._size = inf.readUint16LE();
	_stat._fgBubbleCol = (Color)inf.readByte();
	_stat._bgBubbleCol = (Color)inf.readByte();
	_stat._acciNum = inf.readByte();

	_animCount = 0; // = 1;
	_info._xWidth = _info._xLength / 8;
	if ((_info._xLength % 8) > 0)
		_info._xWidth++;
	for (int i = 0; i < _stat._frameNum; i++) {
		_info._sil[_animCount] = new SilType[11 * (_info._yLength + 1)];
		_info._mani[_animCount] = new ManiType[_info._size - 6];
		for (int j = 0; j <= _info._yLength; j++)
			inf.read((*_info._sil[_animCount])[j], _info._xWidth);
		inf.read(*_info._mani[_animCount], _info._size - 6);

		_animCount++;
	}
	_animCount++;

	CursorMan.showMouse(true);
	_x = 0;
	_y = 0;
	_quick = true;
	_visible = false;
	_speedX = 3;
	_speedY = 1;
	if (spritenum == 1)
		_anim->updateSpeed(); // Just for the lights.

	_homing = false;
	_moveX = 0;
	_moveY = 0;
	_stepNum = 0;
	_doCheck = doCheck;
	_count = 0;
	_id = spritenum;
	_vanishIfStill = false;
	_callEachStepFl = false;

	inf.close();
}

void AnimationType::reset() {
	_quick = false;
	_id = 177;
}

void AnimationType::draw() {
	if ((_vanishIfStill) && (_moveX == 0) && (_moveY == 0))
		return;

	byte picnum = _facingDir * _stat._seq + _stepNum;

	_anim->_vm->_graphics->drawSprite(_info, picnum, _x, _y);
}

void AnimationType::turn(Direction whichway) {
	if (whichway == 8)
		_facingDir = kDirUp;
	else
		_facingDir = whichway;
}

void AnimationType::appear(int16 wx, int16 wy, Direction wf) {
	_x = (wx / 8) * 8;
	_y = wy;
	_oldX[_anim->_vm->_cp] = wx;
	_oldY[_anim->_vm->_cp] = wy;
	turn(wf);
	_visible = true;
	_moveX = 0;
	_moveY = 0;
}

/**
 * Check collision
 * @remarks	Originally called 'collision_check'
 */
bool AnimationType::checkCollision() {
	for (int i = 0; i < _anim->kSpriteNumbMax; i++) {
		AnimationType *spr = &_anim->_sprites[i];
		if (spr->_quick && (spr->_id != _id) && (_x + _info._xLength > spr->_x) && (_x < spr->_x + spr->_info._xLength) && (spr->_y == _y))
			return true;
	}

	return false;
}

void AnimationType::walk() {
	if (_visible) {
		ByteField r;
		r._x1 = (_x / 8) - 1;
		if (r._x1 == 255)
			r._x1 = 0;
		r._y1 = _y - 2;
		r._x2 = ((_x + _info._xLength) / 8) + 1;
		r._y2 = _y + _info._yLength + 2;
	}

	if (!_anim->_vm->_doingSpriteRun) {
		_oldX[_anim->_vm->_cp] = _x;
		_oldY[_anim->_vm->_cp] = _y;
		if (_homing)
			homeStep();
		_x += _moveX;
		_y += _moveY;
	}

	if (_doCheck) {
		if (checkCollision()) {
			bounce();
			return;
		}

		byte magicColor = _anim->checkFeet(_x, _x + _info._xLength, _oldY[_anim->_vm->_cp], _y, _info._yLength) - 1;
		// -1  is because the modified array indexes of magics[] compared to Pascal .

		if ((magicColor != 255) & (!_anim->_vm->_doingSpriteRun)) {
			MagicType *magic = &_anim->_vm->_magics[magicColor];
			switch (magic->_operation) {
			case AvalancheEngine::kMagicExclaim:
				bounce();
				_anim->_mustExclaim = true;
				_anim->_sayWhat = magic->_data;
				break;
			case AvalancheEngine::kMagicBounce:
				bounce();
				break;
			case AvalancheEngine::kMagicTransport:
				_anim->_vm->flipRoom((Room)(magic->_data >> 8), magic->_data & 0xff);
				break;
			case AvalancheEngine::kMagicUnfinished: {
				bounce();
				Common::String tmpStr = Common::String::format("%c%cSorry.%cThis place is not available yet!", Dialogs::kControlBell, Dialogs::kControlCenter, Dialogs::kControlRoman);
				_anim->_vm->_dialogs->displayText(tmpStr);
				}
				break;
			case AvalancheEngine::kMagicSpecial:
				_anim->callSpecial(magic->_data);
				break;
			case AvalancheEngine::kMagicOpenDoor:
				_anim->_vm->openDoor((Room)(magic->_data >> 8), magic->_data & 0xff, magicColor);
				break;
			}
		}
	}

	if (!_anim->_vm->_doingSpriteRun) {
		_count++;
		if (((_moveX != 0) || (_moveY != 0)) && (_count > 1)) {
			_stepNum++;
			if (_stepNum == _stat._seq)
				_stepNum = 0;
			_count = 0;
		}
	}
}

void AnimationType::bounce() {
	_x = _oldX[_anim->_vm->_cp];
	_y = _oldY[_anim->_vm->_cp];
	if (_doCheck)
		_anim->stopWalking();
	else
		stopWalk();
	_anim->_vm->drawDirection();
}

int8 AnimationType::getSign(int16 val) {
	if (val > 0)
		return 1;
	else if (val < 0)
		return -1;
	else
		return 0;
}

void AnimationType::walkTo(byte pedNum) {
	PedType *curPed = &_anim->_vm->_peds[pedNum];

	setSpeed(getSign(curPed->_x - _x) * 4, getSign(curPed->_y - _y));
	_homingX = curPed->_x - _info._xLength / 2;
	_homingY = curPed->_y - _info._yLength;
	_homing = true;
}

void AnimationType::stopHoming() {
	_homing = false;
}

void AnimationType::homeStep() {
	int16 temp;

	if ((_homingX == _x) && (_homingY == _y)) {
		// touching the target
		stopWalk();
		return;
	}
	_moveX = 0;
	_moveY = 0;
	if (_homingY != _y) {
		temp = _homingY - _y;
		if (temp > 4)
			_moveY = 4;
		else if (temp < -4)
			_moveY = -4;
		else
			_moveY = temp;
	}
	if (_homingX != _x) {
		temp = _homingX - _x;
		if (temp > 4)
			_moveX = 4;
		else if (temp < -4)
			_moveX = -4;
		else
			_moveX = temp;
	}
}

void AnimationType::setSpeed(int8 xx, int8 yy) {
	_moveX = xx;
	_moveY = yy;
	if ((_moveX == 0) && (_moveY == 0))
		return; // no movement
	if (_moveX == 0) {
		// No horz movement
		if (_moveY < 0)
			turn(kDirUp);
		else
			turn(kDirDown);
	} else {
		if (_moveX < 0)
			turn(kDirLeft);
		else
			turn(kDirRight);
	}
}

void AnimationType::stopWalk() {
	_moveX = 0;
	_moveY = 0;
	_homing = false;
}

void AnimationType::chatter() {
	_anim->_vm->_talkX = _x + _info._xLength / 2;
	_anim->_vm->_talkY = _y;
	_anim->_vm->_talkFontColor = _stat._fgBubbleCol;
	_anim->_vm->_talkBackgroundColor = _stat._bgBubbleCol;
}

void AnimationType::remove() {
	_animCount--;
	_info._xWidth = _info._xLength / 8;
	if ((_info._xLength % 8) > 0)
		_info._xWidth++;
	for (int i = 0; i < _stat._frameNum; i++) {
		assert(_animCount > 0);
		_animCount--;
		delete[] _info._mani[_animCount];
		delete[] _info._sil[_animCount];
	}

	_quick = false;
	_id = 177;
}

Animation::Animation(AvalancheEngine *vm) {
	_vm = vm;

	_mustExclaim = false;
}

Animation::~Animation() {
	for (int16 i = 0; i < kSpriteNumbMax; i++) {
		if (_sprites[i]._quick)
			_sprites[i].remove();
	}
}

void Animation::resetAnims() {
	setDirection(kDirStopped);
	for (int16 i = 0; i < kSpriteNumbMax; i++)
		_sprites[i].reset();
}

byte Animation::checkFeet(int16 x1, int16 x2, int16 oy, int16 y, byte yl) {
	if (!_vm->_alive)
		return 0;

	if (x1 < 0)
		x1 = 0;
	if (x2 > 639)
		x2 = 639;

	int16 minY = MIN(oy, y) + yl;
	int16 maxY = MAX(oy, y) + yl;

	return _vm->_graphics->getAlsoColor(x1, minY, x2, maxY);
}

byte Animation::geidaPed(byte ped) {
	switch (ped) {
	case 1:
		return 6;
	case 2:
	case 6:
		return 7;
	case 3:
	case 5:
		return 8;
	case 4:
		return 9;
	default:
		error("geidaPed(): Unhandled ped value %d", ped);
	}
}

void Animation::catacombMove(byte ped) {
	int32 here;
	uint16 xy_uint16;

	// XY_uint16 is cat_x+cat_y*256. Thus, every room in the
	// catacombs has a different number for it.
	xy_uint16 = _vm->_catacombX + _vm->_catacombY * 256;
	_geidaSpin = 0;

	switch (xy_uint16) {
	case 1801: // Exit catacombs
		_vm->flipRoom(kRoomLustiesRoom, 4);
		_vm->_dialogs->displayText("Phew! Nice to be out of there!");
		return;
	case 1033:{ // Oubliette
		_vm->flipRoom(kRoomOubliette, 1);
		Common::String tmpStr = Common::String::format("Oh, NO!%c1%c", Dialogs::kControlRegister, Dialogs::kControlSpeechBubble);
		_vm->_dialogs->displayText(tmpStr);
		}
		return;
	case 4:
		_vm->flipRoom(kRoomGeidas, 1);
		return;
	case 2307:
		_vm->flipRoom(kRoomLusties, 5);
		_vm->_dialogs->displayText("Oh no... here we go again...");
		_vm->_userMovesAvvy = false;
		_sprites[0]._moveY = 1;
		_sprites[0]._moveX = 0;
		return;
	}

	if (!_vm->_enterCatacombsFromLustiesRoom)
		_vm->loadRoom(29);
	here = _vm->kCatacombMap[_vm->_catacombY - 1][_vm->_catacombX - 1];

	switch (here & 0xf) { // West.
	case 0: // no connection (wall)
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 27);
		break;
	case 0x1: // no connection (wall + shield),
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 28); // ...shield.
		break;
	case 0x2: // wall with door
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 29); // ...door.
		break;
	case 0x3: // wall with door and shield
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 29); // ...door, and...
		_vm->_background->draw(-1, -1, 28); // ...shield.
		break;
	case 0x4: // no connection (wall + window),
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 4);  // ...window.
		break;
	case 0x5: // wall with door and window
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 29); // ...door, and...
		_vm->_background->draw(-1, -1, 4); // ...window.
		break;
	case 0x6: // no connection (wall + torches),
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicNothing; // No door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 6); // ...torches.
		break;
	case 0x7: // wall with door and torches
		_vm->_magics[1]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[4]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 27); // Wall, plus...
		_vm->_background->draw(-1, -1, 29); // ...door, and...
		_vm->_background->draw(-1, -1, 6); // ...torches.
		break;
	case 0xf: // straight-through corridor.
		_vm->_magics[1]._operation = AvalancheEngine::kMagicNothing; // Sloping wall.
		_vm->_magics[2]._operation = AvalancheEngine::kMagicSpecial; // Straight wall.
		break;
	}

	/*  ---- */

	switch ((here & 0xf0) >> 4) { // East
	case 0: // no connection (wall)
		_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 18);
		break;
	case 0x1: // no connection (wall + window),
		_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 18); // Wall, plus...
		_vm->_background->draw(-1, -1, 19); // ...window.
		break;
	case 0x2: // wall with door
		_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 18); // Wall, plus...
		_vm->_background->draw(-1, -1, 20); // ...door.
		break;
	case 0x3: // wall with door and window
		_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 18); // Wall, plus...
		_vm->_background->draw(-1, -1, 19); // ...door, and...
		_vm->_background->draw(-1, -1, 20); // ...window.
		break;
	case 0x6: // no connection (wall + torches),
		_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicNothing; // No door.
		_vm->_background->draw(-1, -1, 18); // Wall, plus...
		_vm->_background->draw(-1, -1, 17); // ...torches.
		break;
	case 0x7: // wall with door and torches
		_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicNothing; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicSpecial; // Door.
		_vm->_background->draw(-1, -1, 18); // Wall, plus...
		_vm->_background->draw(-1, -1, 20); // ...door, and...
		_vm->_background->draw(-1, -1, 17); // ...torches.
		break;
	case 0xf: // straight-through corridor.
		_vm->_magics[4]._operation = AvalancheEngine::kMagicNothing; // Sloping wall.
		_vm->_magics[5]._operation = AvalancheEngine::kMagicSpecial; // Straight wall.
		_vm->_portals[6]._operation = AvalancheEngine::kMagicNothing; // Door.
		break;
	}

	switch ((here & 0xf00) >> 8) { // South
	case 0: // No connection.
		_vm->_magics[6]._operation = AvalancheEngine::kMagicBounce;
		_vm->_magics[11]._operation = AvalancheEngine::kMagicBounce;
		_vm->_magics[12]._operation = AvalancheEngine::kMagicBounce;
		break;
	case 0x1:
		_vm->_background->draw(-1, -1, 21);

		if ((xy_uint16 == 2051) && (_vm->_geidaFollows))
			_vm->_magics[12]._operation = AvalancheEngine::kMagicExclaim;
		else
			_vm->_magics[12]._operation = AvalancheEngine::kMagicSpecial; // Right exit south.

		_vm->_magics[6]._operation = AvalancheEngine::kMagicBounce;
		_vm->_magics[11]._operation = AvalancheEngine::kMagicBounce;
		break;
	case 0x2:
		_vm->_background->draw(-1, -1, 22);
		_vm->_magics[6]._operation = AvalancheEngine::kMagicSpecial; // Middle exit south.
		_vm->_magics[11]._operation = AvalancheEngine::kMagicBounce;
		_vm->_magics[12]._operation = AvalancheEngine::kMagicBounce;
		break;
	case 0x3:
		_vm->_background->draw(-1, -1, 23);
		_vm->_magics[11]._operation = AvalancheEngine::kMagicSpecial; // Left exit south.
		_vm->_magics[6]._operation = AvalancheEngine::kMagicBounce;
		_vm->_magics[12]._operation = AvalancheEngine::kMagicBounce;
		break;
	}

	switch ((here & 0xf000) >> 12) { // North
	case 0: // No connection
		_vm->_magics[0]._operation = AvalancheEngine::kMagicBounce;
		_vm->_portals[3]._operation = AvalancheEngine::kMagicNothing; // Door.
		break;
	// LEFT handles:
#if 0
	case 0x1:
		_vm->_celer->show_one(-1, -1, 4);
		_vm->magics[1].op = _vm->bounces; // { Left exit north. } { Change magic number! }
		_vm->portals[12].op = _vm->special; // { Door. }
		break;
#endif
	case 0x2:
		_vm->_background->draw(-1, -1, 3);
		_vm->_magics[0]._operation = AvalancheEngine::kMagicBounce; // Middle exit north.
		_vm->_portals[3]._operation = AvalancheEngine::kMagicSpecial; // Door.
		break;
#if 0
	case 0x3:
		_vm->_celer->show_one(-1, -1, 4);
		_vm->magics[1].op = _vm->bounces; // { Right exit north. } { Change magic number! }
		_vm->portals[12].op = _vm->special; // { Door. }
		break;
	// RIGHT handles:
	case 0x4:
		_vm->_celer->show_one(-1, -1, 3);
		_vm->magics[1].op = _vm->bounces; // { Left exit north. } { Change magic number! }
		_vm->portals[12].op = _vm->special; // { Door. }
		break;
#endif
	case 0x5:
		_vm->_background->draw(-1, -1, 2);
		_vm->_magics[0]._operation = AvalancheEngine::kMagicBounce; // Middle exit north.
		_vm->_portals[3]._operation = AvalancheEngine::kMagicSpecial; // Door.
		break;
#if 0
	case 0x6:
		_vm->_celer->show_one(-1, -1, 3);
		_vm->magics[1].op = _vm->bounces; // { Right exit north. }
		_vm->portals[12].op = _vm->special; // { Door. }
		break;
#endif
	// ARCHWAYS:
	case 0x7:
	case 0x8:
	case 0x9:
		_vm->_background->draw(-1, -1, 5);

		if (((here & 0xf000) >> 12) > 0x7)
			_vm->_background->draw(-1, -1, 30);
		if (((here & 0xf000) >> 12) == 0x9)
			_vm->_background->draw(-1, -1, 31);

		_vm->_magics[0]._operation = AvalancheEngine::kMagicSpecial; // Middle arch north.
		_vm->_portals[3]._operation = AvalancheEngine::kMagicNothing; // Door.
		break;
	// DECORATIONS:
	case 0xd: // No connection + WINDOW
		_vm->_magics[0]._operation = AvalancheEngine::kMagicBounce;
		_vm->_portals[3]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 13);
		break;
	case 0xe: // No connection + TORCH
		_vm->_magics[0]._operation = AvalancheEngine::kMagicBounce;
		_vm->_portals[3]._operation = AvalancheEngine::kMagicNothing; // Door.
		_vm->_background->draw(-1, -1, 7);
		break;
	// Recessed door:
	case 0xf:
		_vm->_magics[0]._operation = AvalancheEngine::kMagicNothing; // Door to Geida's room.
		_vm->_background->draw(-1, -1, 0);
		_vm->_portals[3]._operation = AvalancheEngine::kMagicSpecial; // Door.
		break;
	}

	switch (xy_uint16) {
	case 514:
		_vm->_background->draw(-1, -1, 16);
		break;     // [2,2] : "Art Gallery" sign over door.
	case 264:
		_vm->_background->draw(-1, -1, 8);
		break;      // [8,1] : "The Wrong Way!" sign.
	case 1797:
		_vm->_background->draw(-1, -1, 1);
		break;      // [5,7] : "Ite Mingite" sign.
	case 258:
		for (int i = 0; i <= 2; i++) { // [2,1] : Art gallery - pictures
			_vm->_background->draw(130 + i * 120, 70, 14);
			_vm->_background->draw(184 + i * 120, 78, 15);
		}
		break;
	case 1287:
		for (int i = 10; i <= 13; i++)
			_vm->_background->draw(-1, -1, i - 1);
		break; // [7,5] : 4 candles.
	case 776:
		_vm->_background->draw(-1, -1, 9);
		break;     // [8,3] : 1 candle.
	case 2049:
		_vm->_background->draw(-1, -1, 10);
		break;     // [1,8] : another candle.
	case 257:
		_vm->_background->draw(-1, -1, 11);
		_vm->_background->draw(-1, -1, 12);
		break; // [1,1] : the other two.
	}

	if ((_vm->_geidaFollows) && (ped > 0)) {
		if (!_sprites[1]._quick)  // If we don't already have her...
			_sprites[1].init(5, true, this); // ...Load Geida.
		appearPed(1, geidaPed(ped));
		_sprites[1]._callEachStepFl = true;
		_sprites[1]._eachStepProc = kProcGeida;
	}
}

// This proc gets called whenever you touch a line defined as _vm->special.
void Animation::dawnDelay() {
	_vm->_timer->addTimer(2, Timer::kProcDawnDelay, Timer::kReasonDawndelay);
}

void Animation::callSpecial(uint16 which) {
	switch (which) {
	case 1: // _vm->special 1: Room 22: top of stairs.
		_vm->_background->draw(-1, -1, 0);
		_vm->_brummieStairs = 1;
		_vm->_magics[9]._operation = AvalancheEngine::kMagicNothing;
		_vm->_timer->addTimer(10, Timer::kProcStairs, Timer::kReasonBrummieStairs);
		stopWalking();
		_vm->_userMovesAvvy = false;
		break;
	case 2: // _vm->special 2: Room 22: bottom of stairs.
		_vm->_brummieStairs = 3;
		_vm->_magics[10]._operation = AvalancheEngine::kMagicNothing;
		_vm->_magics[11]._operation = AvalancheEngine::kMagicExclaim;
		_vm->_magics[11]._data = 5;
		_vm->_magics[3]._operation = AvalancheEngine::kMagicBounce; // Now works as planned!
		stopWalking();
		_vm->_dialogs->displayScrollChain('q', 26);
		_vm->_userMovesAvvy = true;
		break;
	case 3: // _vm->special 3: Room 71: triggers dart.
		_sprites[0].bounce(); // Must include that.

		if (!_vm->_arrowTriggered) {
			_vm->_arrowTriggered = true;
			appearPed(1, 3); // The dart starts at ped 4, and...
			_sprites[1].walkTo(4); // flies to ped 5 (- 1 for pascal to C conversion).
			_sprites[1]._facingDir = kDirUp; // Only face.
			// Should call some kind of Eachstep procedure which will deallocate
			// the sprite when it hits the wall, and replace it with the chunk
			// graphic of the arrow buried in the plaster. */

			// OK!
			_sprites[1]._callEachStepFl = true;
			_sprites[1]._eachStepProc = kProcArrow;
		}
		break;
	case 4: // This is the ghost room link.
		_vm->dusk();
		_sprites[0].turn(kDirRight); // you'll see this after we get back from bootstrap
		_vm->_timer->addTimer(1, Timer::kProcGhostRoomPhew, Timer::kReasonGhostRoomPhew);
		//_vm->_enid->backToBootstrap(3); TODO: Replace it with proper ScummVM-friendly function(s)!  Do not remove until then!
		break;
	case 5:
		if (_vm->_friarWillTieYouUp) {
			// _vm->special 5: Room 42: touched tree, and get tied up.
			_vm->_magics[4]._operation = AvalancheEngine::kMagicBounce; // Boundary effect is now working again.
			_vm->_dialogs->displayScrollChain('q', 35);
			_sprites[0].remove();
			//tr[1].vanishifstill:=true;
			_vm->_background->draw(-1, -1, 1);
			_vm->_dialogs->displayScrollChain('q', 36);
			_vm->_tiedUp = true;
			_vm->_friarWillTieYouUp = false;
			_sprites[1].walkTo(2);
			_sprites[1]._vanishIfStill = true;
			_sprites[1]._doCheck = true; // One of them must have Check_Me switched on.
			_vm->setRoom(kPeopleFriarTuck, kRoomDummy); // Not here, then.
			_vm->_timer->addTimer(364, Timer::kProcHangAround, Timer::kReasonHangingAround);
		}
		break;
	case 6: // _vm->special 6: fall down oubliette.
		_vm->_userMovesAvvy = false;
		_sprites[0]._moveX = 3;
		_sprites[0]._moveY = 0;
		_sprites[0]._facingDir = kDirRight;
		_vm->_timer->addTimer(1, Timer::kProcFallDownOubliette, Timer::kReasonFallingDownOubliette);
		break;
	case 7: // _vm->special 7: stop falling down oubliette.
		_sprites[0]._visible = false;
		_vm->_magics[9]._operation = AvalancheEngine::kMagicNothing;
		stopWalking();
		_vm->_timer->loseTimer(Timer::kReasonFallingDownOubliette);
		//_vm->mblit(12, 80, 38, 160, 3, 0);
		//_vm->mblit(12, 80, 38, 160, 3, 1);
		_vm->_dialogs->displayText("Oh dear, you seem to be down the bottom of an oubliette.");
		_vm->_timer->addTimer(200, Timer::kProcMeetAvaroid, Timer::kReasonMeetingAvaroid);
		break;
	case 8:        // _vm->special 8: leave du Lustie's room.
		if ((_vm->_geidaFollows) && (!_vm->_lustieIsAsleep)) {
			_vm->_dialogs->displayScrollChain('q', 63);
			_sprites[1].turn(kDirDown);
			_sprites[1].stopWalk();
			_sprites[1]._callEachStepFl = false; // Geida
			_vm->gameOver();
		}
		break;
	case 9: // _vm->special 9: lose Geida to Robin Hood...
		if (!_vm->_geidaFollows)
			return;   // DOESN'T COUNT: no Geida.
		_sprites[1]._callEachStepFl = false; // She no longer follows Avvy around.
		_sprites[1].walkTo(3); // She walks to somewhere...
		_sprites[0].remove();     // Lose Avvy.
		_vm->_userMovesAvvy = false;
		_vm->_timer->addTimer(40, Timer::kProcRobinHoodAndGeida, Timer::kReasonRobinHoodAndGeida);
		break;
	case 10: // _vm->special 10: transfer north in catacombs.
		if ((_vm->_catacombX == 4) && (_vm->_catacombY == 1)) {
			// Into Geida's room.
			if (_vm->_objects[kObjectKey - 1])
				_vm->_dialogs->displayScrollChain('q', 62);
			else {
				_vm->_dialogs->displayScrollChain('q', 61);
				return;
			}
		}
		_vm->dusk();
		_vm->_catacombY--;
		catacombMove(4);
		if (_vm->_room != kRoomCatacombs)
			return;
		switch ((_vm->kCatacombMap[_vm->_catacombY - 1][_vm->_catacombX - 1] & 0xf00) >> 8) {
		case 0x1:
			appearPed(0, 11);
			break;
		case 0x3:
			appearPed(0, 10);
			break;
		default:
			appearPed(0, 3);
		}
		dawnDelay();
		break;
	case 11: // _vm->special 11: transfer east in catacombs.
		_vm->dusk();
		_vm->_catacombX++;
		catacombMove(1);
		if (_vm->_room != kRoomCatacombs)
			return;
		appearPed(0, 0);
		dawnDelay();
		break;
	case 12: // _vm->special 12: transfer south in catacombs.
		_vm->dusk();
		_vm->_catacombY++;
		catacombMove(2);
		if (_vm->_room != kRoomCatacombs)
			return;
		appearPed(0, 1);
		dawnDelay();
		break;
	case 13: // _vm->special 13: transfer west in catacombs.
		_vm->dusk();
		_vm->_catacombX--;
		catacombMove(3);
		if (_vm->_room != kRoomCatacombs)
			return;
		appearPed(0, 2);
		dawnDelay();
		break;
	}
}

void Animation::updateSpeed() {
	// Given that you've just changed the speed in _speedX, this adjusts _moveX.
	_sprites[0]._moveX = (_sprites[0]._moveX / 3) * _sprites[0]._speedX;
	_vm->_graphics->drawSpeedBar(_sprites[0]._speedX);
}

void Animation::setMoveSpeed(byte t, Direction dir) {
	switch (dir) {
	case kDirUp:
		_sprites[t].setSpeed(0, -_sprites[t]._speedY);
		break;
	case kDirDown:
		_sprites[t].setSpeed(0, _sprites[t]._speedY);
		break;
	case kDirLeft:
		_sprites[t].setSpeed(-_sprites[t]._speedX,  0);
		break;
	case kDirRight:
		_sprites[t].setSpeed(_sprites[t]._speedX,  0);
		break;
	case kDirUpLeft:
		_sprites[t].setSpeed(-_sprites[t]._speedX, -_sprites[t]._speedY);
		break;
	case kDirUpRight:
		_sprites[t].setSpeed(_sprites[t]._speedX, -_sprites[t]._speedY);
		break;
	case kDirDownLeft:
		_sprites[t].setSpeed(-_sprites[t]._speedX, _sprites[t]._speedY);
		break;
	case kDirDownRight:
		_sprites[t].setSpeed(_sprites[t]._speedX, _sprites[t]._speedY);
		break;
	default:
		break;
	}
}

void Animation::appearPed(byte sprNum, byte pedNum) {
	AnimationType *curSpr = &_sprites[sprNum];
	PedType *curPed = &_vm->_peds[pedNum];
	curSpr->appear(curPed->_x - curSpr->_info._xLength / 2, curPed->_y - curSpr->_info._yLength, curPed->_direction);
	setMoveSpeed(sprNum, curPed->_direction);
}

void Animation::followAvalotY(byte tripnum) {
	if (_sprites[0]._facingDir == kDirLeft)
		return;
	if (_sprites[tripnum]._homing)
		_sprites[tripnum]._homingY = _sprites[1]._y;
	else {
		if (_sprites[tripnum]._y < _sprites[1]._y)
			_sprites[tripnum]._y++;
		else if (_sprites[tripnum]._y > _sprites[1]._y)
			_sprites[tripnum]._y--;
		else
			return;
		if (_sprites[tripnum]._moveX == 0)  {
			_sprites[tripnum]._stepNum++;
			if (_sprites[tripnum]._stepNum == _sprites[tripnum]._stat._seq)
				_sprites[tripnum]._stepNum = 0;
			_sprites[tripnum]._count = 0;
		}
	}
}

void Animation::backAndForth(byte tripnum) {
	if (!_sprites[tripnum]._homing) {
		if (_sprites[tripnum]._facingDir == kDirRight)
			_sprites[tripnum].walkTo(3);
		else
			_sprites[tripnum].walkTo(4);
	}
}

void Animation::faceAvvy(byte tripnum) {
	if (!_sprites[tripnum]._homing) {
		if (_sprites[0]._x >= _sprites[tripnum]._x)
			_sprites[tripnum]._facingDir = kDirRight;
		else
			_sprites[tripnum]._facingDir = kDirLeft;
	}
}

void Animation::arrowProcs(byte tripnum) {
	if (_sprites[tripnum]._homing) {
		// Arrow is still in flight.
		// We must check whether or not the arrow has collided tr[tripnum] Avvy's head.
		// This is so if: a) the bottom of the arrow is below Avvy's head,
		// b) the left of the arrow is left of the right of Avvy's head, and
		// c) the right of the arrow is right of the left of Avvy's head.
		if (((_sprites[tripnum]._y + _sprites[tripnum]._info._yLength) >= _sprites[0]._y) // A
				&& (_sprites[tripnum]._x <= (_sprites[0]._x + _sprites[0]._info._xLength)) // B
				&& ((_sprites[tripnum]._x + _sprites[tripnum]._info._xLength) >= _sprites[0]._x)) { // C
			// OK, it's hit him... what now?

			_sprites[1]._callEachStepFl = false; // prevent recursion.
			_vm->_dialogs->displayScrollChain('Q', 47); // Complaint!
			_sprites[tripnum].remove(); // Deallocate the arrow.

			_vm->gameOver();

			_vm->_userMovesAvvy = false; // Stop the user from moving him.
			_vm->_timer->addTimer(55, Timer::kProcNaughtyDuke, Timer::kReasonNaughtyDuke);
		}
	} else { // Arrow has hit the wall!
		_sprites[tripnum].remove(); // Deallocate the arrow.
		_vm->_background->draw(-1, -1, 2); // Show pic of arrow stuck into the door.
		_vm->_arrowInTheDoor = true; // So that we can pick it up.
	}

}

void Animation::grabAvvy(byte tripnum) {     // For Friar Tuck, in Nottingham.
	int16 tox = _sprites[0]._x + 17;
	int16 toy = _sprites[0]._y - 1;
	if ((_sprites[tripnum]._x == tox) && (_sprites[tripnum]._y == toy)) {
		_sprites[tripnum]._callEachStepFl = false;
		_sprites[tripnum]._facingDir = kDirLeft;
		_sprites[tripnum].stopWalk();
		// ... whatever ...
	} else {
		// Still some way to go.
		if (_sprites[tripnum]._x < tox) {
			_sprites[tripnum]._x += 5;
			if (_sprites[tripnum]._x > tox)
				_sprites[tripnum]._x = tox;
		}
		if (_sprites[tripnum]._y < toy)
			_sprites[tripnum]._y++;
		_sprites[tripnum]._stepNum++;
		if (_sprites[tripnum]._stepNum == _sprites[tripnum]._stat._seq)
			_sprites[tripnum]._stepNum = 0;
	}
}

void Animation::takeAStep(byte &tripnum) {
	if (_sprites[tripnum]._moveX == 0) {
		_sprites[tripnum]._stepNum++;
		if (_sprites[tripnum]._stepNum == _sprites[tripnum]._stat._seq)
			_sprites[tripnum]._stepNum = 0;
		_sprites[tripnum]._count = 0;
	}
}

void Animation::spin(Direction dir, byte &tripnum) {
	if (_sprites[tripnum]._facingDir == dir)
		return;

	_sprites[tripnum]._facingDir = dir;
	if (_sprites[tripnum]._id == 2)
		return; // Not for Spludwick

	_geidaSpin++;
	_geidaTime = 20;
	if (_geidaSpin == 5) {
		_vm->_dialogs->displayText("Steady on, Avvy, you'll make the poor girl dizzy!");
		_geidaSpin = 0;
		_geidaTime = 0; // knock out records
	}
}

void Animation::geidaProcs(byte tripnum) {
	if (_geidaTime > 0) {
		_geidaTime--;
		if (_geidaTime == 0)
			_geidaSpin = 0;
	}

	if (_sprites[tripnum]._y < (_sprites[0]._y - 2)) {
		// Geida is further from the screen than Avvy.
		spin(kDirDown, tripnum);
		_sprites[tripnum]._moveY = 1;
		_sprites[tripnum]._moveX = 0;
		takeAStep(tripnum);
		return;
	} else if (_sprites[tripnum]._y > (_sprites[0]._y + 2)) {
		// Avvy is further from the screen than Geida.
		spin(kDirUp, tripnum);
		_sprites[tripnum]._moveY = -1;
		_sprites[tripnum]._moveX = 0;
		takeAStep(tripnum);
		return;
	}

	_sprites[tripnum]._moveY = 0;
	// These 12-s are not in the original, I added them to make the following method more "smooth".
	// Now the NPC which is following Avvy won't block his way and will walk next to him properly.
	if (_sprites[tripnum]._x < _sprites[0]._x - _sprites[0]._speedX * 8 - 12) {
		_sprites[tripnum]._moveX = _sprites[0]._speedX;
		spin(kDirRight, tripnum);
		takeAStep(tripnum);
	} else if (_sprites[tripnum]._x > _sprites[0]._x + _sprites[0]._speedX * 8 + 12) {
		_sprites[tripnum]._moveX = -_sprites[0]._speedX;
		spin(kDirLeft, tripnum);
		takeAStep(tripnum);
	} else
		_sprites[tripnum]._moveX = 0;
}

// That's all...

void Animation::drawSprites() {
	int8 order[5];
	byte temp;
	bool ok;

	for (int i = 0; i < 5; i++)
		order[i] = -1;

	for (int16 i = 0; i < kSpriteNumbMax; i++) {
		if (_sprites[i]._quick && _sprites[i]._visible)
			order[i] = i;
	}

	do {
		ok = true;
		for (int i = 0; i < 4; i++) {
			if (((order[i] != -1) && (order[i + 1] != -1))
					&& (_sprites[order[i]]._y > _sprites[order[i + 1]]._y)) {
				// Swap them!
				temp = order[i];
				order[i] = order[i + 1];
				order[i + 1] = temp;
				ok = false;
			}
		}
	} while (!ok);

	_vm->_graphics->refreshBackground();

	for (int i = 0; i < 5; i++) {
		if (order[i] > -1)
			_sprites[order[i]].draw();
	}
}

/**
 * Animation links
 * @remarks	Originally called 'trippancy_link'
 */
void Animation::animLink() {
	if (_vm->_menu->isActive() | _vm->_onToolbar | _vm->_seeScroll)
		return;
	for (int16 i = 0; i < kSpriteNumbMax; i++) {
		if (_sprites[i]._quick && _sprites[i]._visible)
			_sprites[i].walk();
	}

	drawSprites();

	for (int16 i = 0; i < kSpriteNumbMax; i++) {
		if (_sprites[i]._quick && _sprites[i]._callEachStepFl) {
			switch (_sprites[i]._eachStepProc) {
			case kProcFollowAvvyY :
				followAvalotY(i);
				break;
			case kProcBackAndForth :
				backAndForth(i);
				break;
			case kProcFaceAvvy :
				faceAvvy(i);
				break;
			case kProcArrow :
				arrowProcs(i);
				break;
				//    PROCSpludwick_procs : spludwick_procs(fv);
			case kProcGrabAvvy :
				grabAvvy(i);
				break;
			case kProcGeida :
				geidaProcs(i);
				break;
			}
		}
	}

	if (_mustExclaim) {
		_mustExclaim = false;
		_vm->_dialogs->displayScrollChain('x', _sayWhat);
	}
}

void Animation::stopWalking() {
	_sprites[0].stopWalk();
	_direction = kDirStopped;
	if (_vm->_alive)
		_sprites[0]._stepNum = 1;
}

/**
 * Hide in the cupboard
 * @remarks	Originally called 'hide_in_the_cupboard'
 */
void Animation::hideInCupboard() {
	if (_vm->_avvysInTheCupboard) {
		if (_vm->_parser->_wearing == kObjectDummy) {
			Common::String tmpStr = Common::String::format("%cAVVY!%cGet dressed first!", Dialogs::kControlItalic, Dialogs::kControlRoman);
			_vm->_dialogs->displayText(tmpStr);
		} else {
			_sprites[0]._visible = true;
			_vm->_userMovesAvvy = true;
			appearPed(0, 2); // Walk out of the cupboard.
			_vm->_dialogs->displayText("You leave the cupboard. Nice to be out of there!");
			_vm->_avvysInTheCupboard = false;
			_vm->_sequence->startCupboardSeq();
		}
	} else {
		// Not hiding in the cupboard
		_sprites[0]._visible = false;
		_vm->_userMovesAvvy = false;
		Common::String tmpStr = Common::String::format("You walk into the room...%cIt seems to be an empty, " \
			"but dusty, cupboard. Hmmmm... you leave the door slightly open to avoid suffocation.", Dialogs::kControlParagraph);
		_vm->_dialogs->displayText(tmpStr);
		_vm->_avvysInTheCupboard = true;
		_vm->_background->draw(-1, -1, 7);
	}
}

bool Animation::inField(byte which) {
	FieldType *curField = &_vm->_fields[which];
	int16 yy = _sprites[0]._y + _sprites[0]._info._yLength;

	return (_sprites[0]._x >= curField->_x1) && (_sprites[0]._x <= curField->_x2) && (yy >= curField->_y1) && (yy <= curField->_y2);
}

bool Animation::nearDoor() {
	if (_vm->_fieldNum < 8) {
		// there ARE no doors here!
		return false;
	}

	int16 ux = _sprites[0]._x;
	int16 uy = _sprites[0]._y + _sprites[0]._info._yLength;

	for (int i = 8; i < _vm->_fieldNum; i++) {
		FieldType *curField = &_vm->_fields[i];
		if ((ux >= curField->_x1) && (ux <= curField->_x2) && (uy >= curField->_y1) && (uy <= curField->_y2))
			return true;
	}

	return false;
}

void Animation::handleMoveKey(const Common::Event &event) {
	if (!_vm->_userMovesAvvy)
		return;

	if (_vm->_menu->_activeMenuItem._activeNow)
		_vm->_parser->tryDropdown();
	else {
		switch (event.kbd.keycode) {
		case Common::KEYCODE_UP:
			if (_direction != kDirUp) {
				_direction = kDirUp;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_DOWN:
			if (_direction != kDirDown) {
				_direction = kDirDown;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_LEFT:
			if (_direction != kDirLeft) {
				_direction = kDirLeft;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_RIGHT:
			if (_direction != kDirRight) {
				_direction = kDirRight;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_PAGEUP:
			if (_direction != kDirUpRight) {
				_direction = kDirUpRight;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_PAGEDOWN:
			if (_direction != kDirDownRight) {
				_direction = kDirDownRight;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_END:
			if (_direction != kDirDownLeft) {
				_direction = kDirDownLeft;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_HOME:
			if (_direction != kDirUpLeft) {
				_direction = kDirUpLeft;
				setMoveSpeed(0, _direction);
			} else
				stopWalking();
			break;
		case Common::KEYCODE_KP5:
			stopWalking();
			break;
		default:
			break;
		}
	}
}

void Animation::setDirection(Direction dir) {
	_direction = dir;
}

void Animation::setOldDirection(Direction dir) {
	_oldDirection = dir;
}

Direction Animation::getDirection() {
	return _direction;
}

Direction Animation::getOldDirection() {
	return _oldDirection;
}

void Animation::resetVariables() {
	_geidaSpin = 0;
	_geidaTime = 0;
}

void Animation::synchronize(Common::Serializer &sz) {
	sz.syncAsByte(_direction);
	sz.syncAsByte(_geidaSpin);
	sz.syncAsByte(_geidaTime);

	byte spriteNum = 0;
	if (sz.isSaving()) {
		for (int i = 0; i < kSpriteNumbMax; i++) {
			if (_sprites[i]._quick)
				spriteNum++;
		}
	}
	sz.syncAsByte(spriteNum);

	if (sz.isLoading()) {
		for (int i = 0; i < kSpriteNumbMax; i++) { // Deallocate sprites.
			AnimationType *spr = &_sprites[i];
			if (spr->_quick)
				spr->remove();
		}
	}

	for (int i = 0; i < spriteNum; i++) {
		AnimationType *spr = &_sprites[i];
		sz.syncAsByte(spr->_id);
		sz.syncAsByte(spr->_doCheck);

		if (sz.isLoading()) {
			spr->_quick = true;
			spr->init(spr->_id, spr->_doCheck, this);
		}

		sz.syncAsByte(spr->_moveX);
		sz.syncAsByte(spr->_moveY);
		sz.syncAsByte(spr->_facingDir);
		sz.syncAsByte(spr->_stepNum);
		sz.syncAsByte(spr->_visible);
		sz.syncAsByte(spr->_homing);
		sz.syncAsByte(spr->_count);
		sz.syncAsByte(spr->_info._xWidth);
		sz.syncAsByte(spr->_speedX);
		sz.syncAsByte(spr->_speedY);
		sz.syncAsByte(spr->_animCount);
		sz.syncAsSint16LE(spr->_homingX);
		sz.syncAsSint16LE(spr->_homingY);
		sz.syncAsByte(spr->_callEachStepFl);
		sz.syncAsByte(spr->_eachStepProc);
		sz.syncAsByte(spr->_vanishIfStill);
		sz.syncAsSint16LE(spr->_x);
		sz.syncAsSint16LE(spr->_y);

		if (sz.isLoading() && spr->_visible)
			spr->appear(spr->_x, spr->_y, spr->_facingDir);
	}

}

} // End of namespace Avalanche.