aboutsummaryrefslogtreecommitdiff
path: root/engines/sherlock/scene.cpp
blob: 7c66a1dc624f16263980925d2fbd2455e5f44c08 (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
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
/* 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 "sherlock/scene.h"
#include "sherlock/sherlock.h"
#include "sherlock/scalpel/scalpel.h"
#include "sherlock/decompress.h"

namespace Sherlock {

void BgFileHeader::synchronize(Common::SeekableReadStream &s) {
	_numStructs = s.readUint16LE();
	_numImages = s.readUint16LE();
	_numcAnimations = s.readUint16LE();
	_descSize = s.readUint16LE();
	_seqSize = s.readUint16LE();
	_fill = s.readUint16LE();
}

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

void BgfileheaderInfo::synchronize(Common::SeekableReadStream &s) {
	_filesize = s.readUint32LE();
	_maxFrames = s.readByte();

	char buffer[9];
	s.read(buffer, 9);
	_filename = Common::String(buffer);
}

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

void Exit::synchronize(Common::SeekableReadStream &s) {
	int xp = s.readSint16LE();
	int yp = s.readSint16LE();
	int xSize = s.readSint16LE();
	int ySize = s.readSint16LE();
	_bounds = Common::Rect(xp, yp, xp + xSize, yp + ySize);

	_scene = s.readSint16LE();
	_allow = s.readSint16LE();
	_people.x = s.readSint16LE();
	_people.y = s.readSint16LE();
	_peopleDir = s.readUint16LE();
}

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

void SceneEntry::synchronize(Common::SeekableReadStream &s) {
	_startPosition.x = s.readSint16LE();
	_startPosition.y = s.readSint16LE();
	_startDir = s.readByte();
	_allow = s.readByte();
}

void SceneSound::synchronize(Common::SeekableReadStream &s) {
	char buffer[9];
	s.read(buffer, 8);
	buffer[8] = '\0';

	_name = Common::String(buffer);
	_priority = s.readByte();
}

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

Scene::Scene(SherlockEngine *vm): _vm(vm) {
	for (int idx = 0; idx < SCENES_COUNT; ++idx)
		Common::fill(&_stats[idx][0], &_stats[idx][9], false);
	_currentScene = -1;
	_goToScene = -1;
	_changes = false;
	_charPoint = _oldCharPoint = 0;
	_keyboardInput = 0;
	_walkedInScene = false;
	_ongoingCans = 0;
	_version = 0;
	_lzwMode = false;
	_invGraphicItems = 0;
	_hsavedPos = Common::Point(-1, -1);
	_hsavedFs = -1;
	_cAnimFramePause = 0;
	_restoreFlag = false;
	_invLookFlag = false;
	_lookHelp = false;
	_animating = 0;
	_doBgAnimDone = true;
	_tempFadeStyle = 0;
}

Scene::~Scene() {
	freeScene();
}

/**
 * Handles loading the scene specified by _goToScene
 */
void Scene::selectScene() {
	Events &events = *_vm->_events;
	People &people = *_vm->_people;
	Screen &screen = *_vm->_screen;
	Scripts &scripts = *_vm->_scripts;
	UserInterface &ui = *_vm->_ui;

	// Reset fields
	ui._windowOpen = ui._infoFlag = false;
	ui._menuMode = STD_MODE;
	_keyboardInput = 0;
	_oldKey = _help = _oldHelp = 0;
	_oldTemp = _temp = 0;

	// Load the scene
	Common::String sceneFile = Common::String::format("res%02d", _goToScene);
	_rrmName = Common::String::format("res%02d.rrm", _goToScene);
	_currentScene = _goToScene;
	_goToScene = -1;

	loadScene(sceneFile);

	// If the fade style was changed from running amovie, then reset it
	if (_tempFadeStyle) {
		screen._fadeStyle = _tempFadeStyle;
		_tempFadeStyle = 0;
	}

	people._walkDest = Common::Point(people[AL]._position.x / 100,
		people[AL]._position.y / 100);

	_restoreFlag = true;
	events.clearEvents();

	// If there were any scripst waiting to be run, but were interrupt by a running
	// canimation (probably the last scene's exit canim), clear the _scriptMoreFlag
	if (scripts._scriptMoreFlag == 3)
		scripts._scriptMoreFlag = 0;
}

/**
 * Fres all the graphics and other dynamically allocated data for the scene
 */
void Scene::freeScene() {
	_vm->_talk->freeTalkVars();
	_vm->_inventory->freeInv();
	_vm->_sound->freeSong();
	_vm->_sound->freeLoadedSounds();

	if (!_vm->_loadingSavedGame)
		saveSceneStatus();
	else
		_vm->_loadingSavedGame = false;

	_sequenceBuffer.clear();
	_descText.clear();
	_walkData.clear();
	_cAnim.clear();
	_bgShapes.clear();
	_zones.clear();
	_canimShapes.clear();

	for (uint idx = 0; idx < _images.size(); ++idx)
		delete _images[idx]._images;
	_images.clear();
}

/**
 * Loads the data associated for a given scene. The .BGD file's format is:
 * BGHEADER: Holds an index for the rest of the file
 * STRUCTS:  The objects for the scene
 * IMAGES:   The graphic information for the structures
 *
 * The _misc field of the structures contains the number of the graphic image
 * that it should point to after loading; _misc is then set to 0.
 */
bool Scene::loadScene(const Common::String &filename) {
	Events &events = *_vm->_events;
	People &people = *_vm->_people;
	Screen &screen = *_vm->_screen;
	Sound &sound = *_vm->_sound;
	UserInterface &ui = *_vm->_ui;
	bool flag;

	freeScene();
	_walkedInScene = false;
	_ongoingCans = 0;

	// Reset the list of walkable areas
	_zones.clear();
	_zones.push_back(Common::Rect(0, 0, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCREEN_HEIGHT));

	_descText.clear();
	_comments = "";
	_bgShapes.clear();
	_cAnim.clear();
	_sequenceBuffer.clear();

	//
	// Load background shapes from <filename>.rrm
	//

	Common::String rrmFile = filename + ".rrm";
	flag = _vm->_res->exists(rrmFile);
	if (flag) {
		Common::SeekableReadStream *rrmStream = _vm->_res->load(rrmFile);

		rrmStream->seek(39);
		_version = rrmStream->readByte();
		_lzwMode = _version == 10;

		// Go to header and read it in
		rrmStream->seek(rrmStream->readUint32LE());
		BgFileHeader bgHeader;
		bgHeader.synchronize(*rrmStream);
		_invGraphicItems = bgHeader._numImages + 1;

		// Read in the shapes header info
		Common::Array<BgfileheaderInfo> bgInfo;
		bgInfo.resize(bgHeader._numStructs);

		for (uint idx = 0; idx < bgInfo.size(); ++idx)
			bgInfo[idx].synchronize(*rrmStream);

		// Read information
		Common::SeekableReadStream *infoStream = !_lzwMode ? rrmStream :
			decompressLZ(*rrmStream, bgHeader._numImages * 569 + 
				bgHeader._descSize + bgHeader._seqSize);

		_bgShapes.resize(bgHeader._numStructs);
		for (int idx = 0; idx < bgHeader._numStructs; ++idx)
			_bgShapes[idx].synchronize(*infoStream);

		if (bgHeader._descSize) {
			_descText.resize(bgHeader._descSize);
			infoStream->read(&_descText[0], bgHeader._descSize);
		}

		if (bgHeader._seqSize) {
			_sequenceBuffer.resize(bgHeader._seqSize);
			infoStream->read(&_sequenceBuffer[0], bgHeader._seqSize);
		}			

		if (_lzwMode)
			delete infoStream;

		// Set up the list of images used by the scene
		_images.resize(bgHeader._numImages + 1);
		for (int idx = 0; idx < bgHeader._numImages; ++idx) {
			_images[idx + 1]._filesize = bgInfo[idx]._filesize;
			_images[idx + 1]._maxFrames = bgInfo[idx]._maxFrames;

			// Read in the image data
			Common::SeekableReadStream *imageStream = _lzwMode ?
				decompressLZ(*rrmStream, bgInfo[idx]._filesize) :
				rrmStream->readStream(bgInfo[idx]._filesize);

			_images[idx + 1]._images = new ImageFile(*imageStream);

			delete imageStream;
		}

		// Set up the bgShapes
		for (int idx = 0; idx < bgHeader._numStructs; ++idx) {
			_bgShapes[idx]._images = _images[_bgShapes[idx]._misc]._images;
			_bgShapes[idx]._imageFrame = !_bgShapes[idx]._images ? (ImageFrame *)nullptr :
				&(*_bgShapes[idx]._images)[0];

			_bgShapes[idx]._examine = Common::String(&_descText[_bgShapes[idx]._descOffset]);
			_bgShapes[idx]._sequences = &_sequenceBuffer[_bgShapes[idx]._sequenceOffset];
			_bgShapes[idx]._misc = 0;
			_bgShapes[idx]._seqCounter = 0;
			_bgShapes[idx]._seqCounter2 = 0;
			_bgShapes[idx]._seqStack = 0;
			_bgShapes[idx]._frameNumber = -1;
			_bgShapes[idx]._oldPosition = Common::Point(0, 0);
			_bgShapes[idx]._oldSize = Common::Point(1, 1);
		}

		// Load in cAnim list
		Common::SeekableReadStream *canimStream = _lzwMode ?
			decompressLZ(*rrmStream, 65 * bgHeader._numcAnimations) :
			rrmStream->readStream(65 * bgHeader._numcAnimations);

		_cAnim.resize(bgHeader._numcAnimations);
		for (uint idx = 0; idx < _cAnim.size(); ++idx)
			_cAnim[idx].synchronize(*canimStream);

		delete canimStream;
		
		// Read in the room bounding areas
		int size = rrmStream->readUint16LE();
		Common::SeekableReadStream *boundsStream = !_lzwMode ? rrmStream :
			decompressLZ(*rrmStream, size);

		_zones.resize(size / 10);
		for (uint idx = 0; idx < _zones.size(); ++idx) {
			_zones[idx].left = boundsStream->readSint16LE();
			_zones[idx].top = boundsStream->readSint16LE();
			_zones[idx].setWidth(boundsStream->readSint16LE());
			_zones[idx].setHeight(boundsStream->readSint16LE());
			boundsStream->skip(2);	// Skip unused scene number field
		}

		if (_lzwMode)
			delete boundsStream;

		// Ensure we've reached the path version byte
		if (rrmStream->readByte() != 254)
			error("Invalid scene path data");

		// Load the walk directory
		for (uint idx1 = 0; idx1 < _zones.size(); ++idx1) {
			for (uint idx2 = 0; idx2 < _zones.size(); ++idx2)
				_walkDirectory[idx1][idx2] = rrmStream->readSint16LE();
		}

		// Read in the walk data
		size = rrmStream->readUint16LE();
		Common::SeekableReadStream *walkStream = !_lzwMode ? rrmStream :
			decompressLZ(*rrmStream, size);

		_walkData.resize(size);
		walkStream->read(&_walkData[0], size);

		if (_lzwMode)
			delete walkStream;

		// Read in the exits
		int numExits = rrmStream->readByte();
		_exits.resize(numExits);

		for (int idx = 0; idx < numExits; ++idx)
			_exits[idx].synchronize(*rrmStream);

		// Read in the entrance
		_entrance.synchronize(*rrmStream);

		// Initialize sound list
		int numSounds = rrmStream->readByte();
		_sounds.resize(numSounds);

		for (int idx = 0; idx < numSounds; ++idx)
			_sounds[idx].synchronize(*rrmStream);

		// If sound is turned on, load the sounds into memory
		if (sound._soundOn) {
			for (int idx = 0; idx < numSounds; ++idx) {
				sound.loadSound(_sounds[idx]._name, _sounds[idx]._priority);
				_sounds[idx]._name = "";
			}
		}

		// Read in palette
		rrmStream->read(screen._cMap, PALETTE_SIZE);
		for (int idx = 0; idx < PALETTE_SIZE; ++idx)
			screen._cMap[idx] = VGA_COLOR_TRANS(screen._cMap[idx]);
		
		Common::copy(screen._cMap, screen._cMap + PALETTE_SIZE, screen._sMap);

		// Read in the background
		Common::SeekableReadStream *bgStream = !_lzwMode ? rrmStream :
			decompressLZ(*rrmStream, SHERLOCK_SCREEN_WIDTH * SHERLOCK_SCENE_HEIGHT);

		bgStream->read(screen._backBuffer1.getPixels(), SHERLOCK_SCREEN_WIDTH * SHERLOCK_SCENE_HEIGHT);

		if (_lzwMode)
			delete bgStream;

		// Backup the image and set the palette
		screen._backBuffer2.blitFrom(screen._backBuffer1);
		screen.setPalette(screen._cMap);

		delete rrmStream;
	}

	// Clear user interface area and draw controls
	ui.drawInterface();

	_changes = false;
	checkSceneStatus();

	if (!_vm->_justLoaded) {
		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			if (_bgShapes[idx]._type == HIDDEN && _bgShapes[idx]._aType == TALK_EVERY)
				_bgShapes[idx].toggleHidden();
		}

		// Check for TURNON objects
		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			if (_bgShapes[idx]._type == HIDDEN && (_bgShapes[idx]._flags & 0x20))
				_bgShapes[idx].toggleHidden();
		}

		// Check for TURNOFF objects
		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			if (_bgShapes[idx]._type != HIDDEN && (_bgShapes[idx]._flags & 0x40) &&
					_bgShapes[idx]._type != INVALID)
				_bgShapes[idx].toggleHidden();
		}
	}

	checkSceneFlags(false);
	checkInventory();

	// Handle starting any music for the scene
	if (sound._musicOn && sound.loadSong(_currentScene)) {
		if (sound._music)
			sound.startSong();
	}

	// Load walking images if not already loaded
	people.loadWalk();

	// Transition to the scene and setup entrance co-ordinates and animations
	transitionToScene();

	// Player has not yet walked in this scene
	_walkedInScene = false;

	// Reset the position on the overland map
	_vm->_oldCharPoint = _currentScene;
	_vm->_over.x = _vm->_map[_currentScene].x * 100 - 600;
	_vm->_over.y = _vm->_map[_currentScene].y * 100 + 900;

	events.clearEvents();
	return flag;
}

/**
 * Set objects to their current persistent state. This includes things such as
 * opening or moving them
 */
void Scene::checkSceneStatus() {
	if (_stats[_currentScene][8]) {
		for (int idx = 0; idx < 8; ++idx) {
			int val = _stats[_currentScene][idx];

			for (int bit = 0; bit < 8; ++bit) {
				uint objNumber = idx * 8 + bit;
				if (objNumber < _bgShapes.size()) {
					Object &obj = _bgShapes[objNumber];

					if (val & 1) {
						// No shape to erase, so flag as hidden
						obj._type = HIDDEN;
					} else if (obj._images == nullptr || obj._images->size() == 0) {
						// No shape
						obj._type = NO_SHAPE;
					} else {
						obj._type = ACTIVE_BG_SHAPE;
					}
				} else {
					// Finished checks
					return;
				}

				val >>= 1;
			}
		}
	}
}

/**
 * Check the scene's objects against the game flags. If false is passed,
 * it means the scene has just been loaded. A value of true means that the scene
 * is in use (ie. not just loaded)
 */
void Scene::checkSceneFlags(bool flag) {
	SpriteType mode = flag ? HIDE_SHAPE : HIDDEN;

	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		
		if (o._requiredFlag) {
			if (!_vm->readFlags(_bgShapes[idx]._requiredFlag)) {
				// Kill object
				if (o._type != HIDDEN && o._type != INVALID) {
					if (o._images == nullptr || o._images->size() == 0)
						// No shape to erase, so flag as hidden
						o._type = HIDDEN;
					else
						// Flag it as needing to be hidden after first erasing it
						o._type = mode;
				}
			} else if (_bgShapes[idx]._requiredFlag) {
				// Restore object
				if (o._images == nullptr || o._images->size() == 0)
					o._type = NO_SHAPE;
				else
					o._type = ACTIVE_BG_SHAPE;
			}
		}
	}

	// Check inventory
	for (int idx = 0; idx < _vm->_inventory->_holdings; ++idx) {
		InventoryItem &ii = (*_vm->_inventory)[idx];
		if (ii._requiredFlag && !_vm->readFlags(ii._requiredFlag)) {
			// Kill object: move it after the active holdings
			InventoryItem tempItem = (*_vm->_inventory)[idx];
			_vm->_inventory->insert_at(_vm->_inventory->_holdings, tempItem);
			_vm->_inventory->remove_at(idx);
			_vm->_inventory->_holdings--;
			break;
		}
	}

	for (uint idx = _vm->_inventory->_holdings; idx < _vm->_inventory->size(); ++idx) {
		InventoryItem &ii = (*_vm->_inventory)[idx];
		if (ii._requiredFlag && _vm->readFlags(ii._requiredFlag)) {
			// Restore object: move it after the active holdings
			InventoryItem tempItem = (*_vm->_inventory)[idx];
			_vm->_inventory->remove_at(idx);
			_vm->_inventory->insert_at(_vm->_inventory->_holdings, tempItem);
			_vm->_inventory->_holdings++;
			break;
		}
	}
}

/**
 * Checks scene objects against the player's inventory items. If there are any
 * matching names, it means the given item has already been picked up, and should
 * be hidden in the scene.
 */
void Scene::checkInventory() {
	for (uint shapeIdx = 0; shapeIdx < _bgShapes.size(); ++shapeIdx) {
		for (uint invIdx = 0; invIdx < _vm->_inventory->size(); ++invIdx) {
			if (scumm_stricmp(_bgShapes[shapeIdx]._name.c_str(),
				(*_vm->_inventory)[invIdx]._name.c_str()) == 0) {
				_bgShapes[shapeIdx]._type = INVALID;
				break;
			}
		}
	}
}

/**
 * Set up any entrance co-ordinates or entrance canimations, and then transition
 * in the scene
 */
void Scene::transitionToScene() {
	People &people = *_vm->_people;
	Screen &screen = *_vm->_screen;
	Talk &talk = *_vm->_talk;

	const int FS_TRANS[8] = {
		STOP_UP, STOP_UPRIGHT, STOP_RIGHT, STOP_DOWNRIGHT, STOP_DOWN,
		STOP_DOWNLEFT, STOP_LEFT, STOP_UPLEFT
	};

	if (_hsavedPos.x < 1) {
		// No exit information from last scene-check entrance info
		if (_entrance._startPosition.x < 1) {
			// No entrance info either, so use defaults
			_hsavedPos = Common::Point(16000, 10000);
			_hsavedFs = 4;
		} else {
			// setup entrance info
			_hsavedPos = _entrance._startPosition;
			_hsavedFs = _entrance._startDir;
		}
	} else {
		// Exit information exists, translate it to real sequence info
		// Note: If a savegame was just loaded, then the data is already correct.
		// Otherwise, this is a linked scene or entrance info, and must be translated
		if (_hsavedFs < 8 && !_vm->_justLoaded) {
			_hsavedFs = FS_TRANS[_hsavedFs];
			_hsavedPos.x *= 100;
			_hsavedPos.y *= 100;
		}
	}

	int cAnimNum = -1;

	if (_hsavedFs < 101) {
		// Standard info, so set it
		people[PLAYER]._position = _hsavedPos;
		people[PLAYER]._sequenceNumber = _hsavedFs;
	} else {
		// It's canimation information
		cAnimNum = _hsavedFs - 101;
	}

	// Reset positioning for next load
	_hsavedPos = Common::Point(-1, -1);
	_hsavedFs = -1;

	if (cAnimNum != -1) {
		// Prevent Holmes from being drawn
		people[PLAYER]._position = Common::Point(0, 0);
	}

	for (uint objIdx = 0; objIdx < _bgShapes.size(); ++objIdx) {
		Object &obj = _bgShapes[objIdx];

		if (obj._aType > 1 && obj._type != INVALID && obj._type != HIDDEN) {
			Common::Point topLeft = obj._position;
			Common::Point bottomRight;

			if (obj._type != NO_SHAPE) {
				topLeft += obj._imageFrame->_offset;
				bottomRight.x = topLeft.x + obj._imageFrame->_frame.w;
				bottomRight.y = topLeft.y + obj._imageFrame->_frame.h;			
			} else {
				bottomRight = topLeft + obj._noShapeSize;
			}

			if (Common::Rect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y).contains(
				Common::Point(people[PLAYER]._position.x / 100, people[PLAYER]._position.y / 100))) {
				// Current point is already inside box - impact occurred on
				// a previous call. So simply do nothing except talk until the
				// player is clear of the box
				switch (obj._aType) {
				case FLAG_SET:
					for (int useNum = 0; useNum < 4; ++useNum) {
						if (obj._use[useNum]._useFlag) {
							if (!_vm->readFlags(obj._use[useNum]._useFlag))
								_vm->setFlags(obj._use[useNum]._useFlag);
						}

						if (!talk._talkToAbort) {
							for (int nameIdx = 0; nameIdx < 4; ++nameIdx) {
								toggleObject(obj._use[useNum]._names[nameIdx]);
							}
						}
					}

					obj._type = HIDDEN;
					break;

				default:
					break;
				}
			}
		}
	}

	updateBackground();

	if (screen._fadeStyle)
		screen.randomTransition();
	else
		screen.blitFrom(screen._backBuffer1);

	if (cAnimNum != -1) {
		CAnim &c = _cAnim[cAnimNum];
		Common::Point pt = c._position;

		c._position = Common::Point(-1, -1);
		people[AL]._position = Common::Point(0, 0);

		startCAnim(cAnimNum, 1);
		c._position = pt;
	}
}

/**
 * Scans through the object list to find one with a matching name, and will
 * call toggleHidden with all matches found. Returns the numer of matches found
 */
int Scene::toggleObject(const Common::String &name) {
	int count = 0;

	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		if (scumm_stricmp(name.c_str(), _bgShapes[idx]._name.c_str()) == 0) {
			++count;
			_bgShapes[idx].toggleHidden();
		}
	}

	return count;
}

/**
 * Update the screen back buffer with all of the scene objects which need
 * to be drawn
 */
void Scene::updateBackground() {
	People &people = *_vm->_people;
	Screen &screen = *_vm->_screen;
	Surface surface = screen._backBuffer1.getSubArea(
		Common::Rect(0, 0, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCENE_HEIGHT));
	Sprite &player = people[AL];

	// Update Holmes if he's turned on
	if (people._holmesOn)
		player.adjustSprite();

	// Flag the bg shapes which need to be redrawn
	checkBgShapes(player._imageFrame, Common::Point(player._position.x / 100,
		player._position.y / 100));

	// Draw all active shapes which are behind the person
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		if (_bgShapes[idx]._type == ACTIVE_BG_SHAPE && _bgShapes[idx]._misc == BEHIND)
			surface.transBlitFrom(_bgShapes[idx]._imageFrame->_frame,
				_bgShapes[idx]._position, _bgShapes[idx]._flags & 2);
	}

	// Draw all canimations which are behind the person
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		if (_canimShapes[idx]._type == ACTIVE_BG_SHAPE && _canimShapes[idx]._misc == BEHIND)
			surface.transBlitFrom(_canimShapes[idx]._imageFrame->_frame,
				_canimShapes[idx]._position, _canimShapes[idx]._flags & 2);
	}

	// Draw all active shapes which are normal and behind the person
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		if (_bgShapes[idx]._type == ACTIVE_BG_SHAPE && _bgShapes[idx]._misc == NORMAL_BEHIND)
			surface.transBlitFrom(_bgShapes[idx]._imageFrame->_frame,
			_bgShapes[idx]._position, _bgShapes[idx]._flags & 2);
	}

	// Draw all canimations which are normal and behind the person
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		if (_canimShapes[idx]._type == ACTIVE_BG_SHAPE && _canimShapes[idx]._misc == NORMAL_BEHIND)
			surface.transBlitFrom(_canimShapes[idx]._imageFrame->_frame,
			_canimShapes[idx]._position, _canimShapes[idx]._flags & 2);
	}

	// Draw the player if he's active
	if (player._type == CHARACTER && people.isHolmesActive()) {
		bool flipped = player._sequenceNumber == WALK_LEFT || player._sequenceNumber == STOP_LEFT ||
			player._sequenceNumber == WALK_UPLEFT || player._sequenceNumber == STOP_UPLEFT ||
			player._sequenceNumber == WALK_DOWNRIGHT || player._sequenceNumber == STOP_DOWNRIGHT;

		surface.transBlitFrom(player._imageFrame->_frame,
			Common::Point(player._position.x / 100, 
				player._position.y / 100 - player.frameHeight()), flipped);
	}

	// Draw all static and active shapes that are NORMAL and are in front of the player
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		if ((_bgShapes[idx]._type == ACTIVE_BG_SHAPE || _bgShapes[idx]._type == STATIC_BG_SHAPE) &&
				_bgShapes[idx]._misc == NORMAL_FORWARD)
			surface.transBlitFrom(_bgShapes[idx]._imageFrame->_frame,
				_bgShapes[idx]._position, _bgShapes[idx]._flags & 2);
	}

	// Draw all static and active canimations that are NORMAL and are in front of the player
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		if ((_canimShapes[idx]._type == ACTIVE_BG_SHAPE || _canimShapes[idx]._type == STATIC_BG_SHAPE) &&
				_canimShapes[idx]._misc == NORMAL_FORWARD)
			surface.transBlitFrom(_canimShapes[idx]._imageFrame->_frame,
				_canimShapes[idx]._position, _canimShapes[idx]._flags & 2);
	}

	// Draw all static and active shapes that are FORWARD
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		_bgShapes[idx]._oldPosition = _bgShapes[idx]._position;
		_bgShapes[idx]._oldSize = Common::Point(_bgShapes[idx].frameWidth(),
			_bgShapes[idx].frameHeight());

		if ((_bgShapes[idx]._type == ACTIVE_BG_SHAPE || _bgShapes[idx]._type == STATIC_BG_SHAPE) &&
				_bgShapes[idx]._misc == FORWARD)
			surface.transBlitFrom(_bgShapes[idx]._imageFrame->_frame,
				_bgShapes[idx]._position, _bgShapes[idx]._flags & 2);
	}

	// Draw all static and active canimations that are forward
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		if ((_canimShapes[idx]._type == ACTIVE_BG_SHAPE || _canimShapes[idx]._type == STATIC_BG_SHAPE) &&
			_canimShapes[idx]._misc == FORWARD)
			surface.transBlitFrom(_canimShapes[idx]._imageFrame->_frame,
				_canimShapes[idx]._position, _canimShapes[idx]._flags & 2);
	}
}

Exit *Scene::checkForExit(const Common::Rect &r) {
	for (uint idx = 0; idx < _exits.size(); ++idx) {
		if (_exits[idx]._bounds.intersects(r))
			return &_exits[idx];
	}

	return nullptr;
}

/**
 * Checks all the background shapes. If a background shape is animating,
 * it will flag it as needing to be drawn. If a non-animating shape is
 * colliding with another shape, it will also flag it as needing drawing
 */
void Scene::checkBgShapes(ImageFrame *frame, const Common::Point &pt) {
	// Iterate through the shapes
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &obj = _bgShapes[idx];
		if (obj._type == STATIC_BG_SHAPE || obj._type == ACTIVE_BG_SHAPE) {
			if ((obj._flags & 5) == 1) {
				obj._misc = (pt.y < (obj._position.y + obj.frameHeight() - 1)) ?
					NORMAL_FORWARD : NORMAL_BEHIND;
			} else if (!(obj._flags & 1)) {
				obj._misc = BEHIND;
			} else if (obj._flags & 4) {
				obj._misc = FORWARD;
			}
		}
	}

	// Iterate through the canimshapes
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		Object &obj = _canimShapes[idx];
		if (obj._type == STATIC_BG_SHAPE || obj._type == ACTIVE_BG_SHAPE) {
			if ((obj._flags & 5) == 1) {
				obj._misc = (pt.y < (obj._position.y + obj._imageFrame->_frame.h - 1)) ?
				NORMAL_FORWARD : NORMAL_BEHIND;
			}
			else if (!(obj._flags & 1)) {
				obj._misc = BEHIND;
			}
			else if (obj._flags & 4) {
				obj._misc = FORWARD;
			}
		}
	}
}

/**
 * Attempt to start a canimation sequence. It will load the requisite graphics, and
 * then copy the canim object into the _canimShapes array to start the animation.
 *
 * @param cAnimNum		The canim object within the current scene
 * @param playRate		Play rate. 0 is invalid; 1=normal speed, 2=1/2 speed, etc.
 *		A negative playRate can also be specified to play the animation in reverse
 */
int Scene::startCAnim(int cAnimNum, int playRate) {
	Events &events = *_vm->_events;
	People &people = *_vm->_people;
	Resources &res = *_vm->_res;
	Talk &talk = *_vm->_talk;
	UserInterface &ui = *_vm->_ui;
	Common::Point tpPos, walkPos;
	int tpDir, walkDir;
	int tFrames = 0;
	int gotoCode = -1;

	// Validation
	if (cAnimNum >= (int)_cAnim.size())
		// number out of bounds
		return -1;
	if (_canimShapes.size() >= 3 || playRate == 0)
		// Too many active animations, or invalid play rate
		return 0;

	CAnim &cAnim = _cAnim[cAnimNum];
	if (playRate < 0) {
		// Reverse direction
		walkPos = cAnim._teleportPos;
		walkDir = cAnim._teleportDir;
		tpPos = cAnim._goto;
		tpDir = cAnim._gotoDir;
	} else {
		// Forward direction
		walkPos = cAnim._goto;
		walkDir = cAnim._gotoDir;
		tpPos = cAnim._teleportPos;
		tpDir = cAnim._teleportDir;
	}

	events.setCursor(WAIT);
	_canimShapes.push_back(Object());
	Object &cObj = _canimShapes[_canimShapes.size() - 1];

	if (walkPos.x != -1) {
		// Holmes must walk to the walk point before the cAnimation is started
		if (people[AL]._position != walkPos)
			people.walkToCoords(walkPos, walkDir);
	}

	if (talk._talkToAbort)
		return 1;

	// Copy the canimation into the bgShapes type canimation structure so it can be played
	cObj._allow = cAnimNum + 1;				// Keep track of the parent structure
	cObj._name = _cAnim[cAnimNum]._name;	// Copy name

	// Remove any attempt to draw object frame
	if (cAnim._type == NO_SHAPE && cAnim._sequences[0] < 100)
		cAnim._sequences[0] = 0;

	cObj._sequences = cAnim._sequences;
	cObj._images = nullptr;
	cObj._position = cAnim._position;
	cObj._delta = Common::Point(0, 0);
	cObj._type = cAnim._type;
	cObj._flags = cAnim._flags;

	cObj._maxFrames = 0;
	cObj._frameNumber = -1;
	cObj._sequenceNumber = cAnimNum;
	cObj._oldPosition = Common::Point(0, 0);
	cObj._oldPosition = Common::Point(0, 0);
	cObj._goto = Common::Point(0, 0);
	cObj._status = 0;
	cObj._misc = 0;
	cObj._imageFrame = nullptr;

	if (cAnim._name.size() > 0 && cAnim._type != NO_SHAPE) {
		if (tpPos.x != -1)
			people[AL]._type = REMOVE;

		Common::String fname = cAnim._name + ".vgs";
		if (!res.isInCache(fname)) {
			// Set up RRM scene data
			Common::SeekableReadStream *rrmStream = res.load(_rrmName);
			rrmStream->seek(44 + cAnimNum * 4);
			rrmStream->seek(rrmStream->readUint32LE());

			// Load the canimation into the cache
			Common::SeekableReadStream *imgStream = !_lzwMode ? rrmStream :
				decompressLZ(*rrmStream, cAnim._size);
			res.addToCache(fname, *imgStream);

			if (_lzwMode)
				delete imgStream;

			delete rrmStream;
		}

		// Now load the resource as an image
		cObj._maxFrames = cObj._images->size();
		cObj._images = new ImageFile(fname);
		cObj._imageFrame = &(*cObj._images)[0];

		int frames = 0;
		if (playRate < 0) {
			// Reverse direction
			// Count number of frames
			while (cObj._sequences[frames] && frames < MAX_FRAME)
				++frames;
		}
		else {
			// Forward direction
			Object::_countCAnimFrames = true;

			while (cObj._type == ACTIVE_BG_SHAPE) {
				cObj.checkObject(_bgShapes[0]);
				++frames;

				if (frames >= 1000)
					error("CAnim has infinite loop sequence");
			}

			if (frames > 1)
				--frames;

			Object::_countCAnimFrames = false;

			cObj._type = cAnim._type;
			cObj._frameNumber = -1;
			cObj._position = cAnim._position;
			cObj._delta = Common::Point(0, 0);
		}

		// Return if animation has no frames in it
		if (frames == 0)
			return -2;

		++frames;
		int repeat = ABS(playRate);
		int dir;

		if (playRate < 0) {
			// Play in reverse
			dir = -2;
			cObj._frameNumber = frames - 3;
		} else {
			dir = 0;
		}

		tFrames = frames - 1;
		int pauseFrame = (_cAnimFramePause) ? frames - _cAnimFramePause : -1;

		while (--frames) {
			if (frames == pauseFrame)
				ui.printObjectDesc();

			doBgAnim();

			// Repeat same frame
			int temp = repeat;
			while (--temp > 0) {
				cObj._frameNumber--;
				doBgAnim();
			}

			cObj._frameNumber += dir;
		}

		people[AL]._type = CHARACTER;
	}

	// Teleport to ending coordinates if necessary
	if (tpPos.x != -1) {
		people[AL]._position = tpPos;	// Place the player
		people[AL]._sequenceNumber = tpDir;
		people.gotoStand(people[AL]);
	}

	if (playRate < 0)
		// Reverse direction - set to end sequence
		cObj._frameNumber = tFrames - 1;
	
	if (cObj._frameNumber <= 26)
		gotoCode = cObj._sequences[cObj._frameNumber + 3];

	// Set canim to REMOVE type and free memory
	cObj.checkObject(_bgShapes[0]);

	if (gotoCode > 0 && !talk._talkToAbort) {
		_goToScene = gotoCode;

		if (_goToScene < 97 && _vm->_map[_goToScene].x) {
			_overPos = _vm->_map[_goToScene];
		}
	}

	people.loadWalk();

	if (tpPos.x != -1 && !talk._talkToAbort) {
		// Teleport to ending coordinates
		people[AL]._position = tpPos;
		people[AL]._sequenceNumber = tpDir;

		people.gotoStand(people[AL]);
	}

	return 1;
}

/**
 * Animate all objects and people.
 */
void Scene::doBgAnim() {
	Events &events = *_vm->_events;
	Inventory &inv = *_vm->_inventory;
	People &people = *_vm->_people;
	Screen &screen = *_vm->_screen;
	Scripts &scripts = *_vm->_scripts;
	Sound &sound = *_vm->_sound;
	Talk &talk = *_vm->_talk;
	UserInterface &ui = *_vm->_ui;
	Surface surface = screen._backBuffer1.getSubArea(Common::Rect(0, 0,
		SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCENE_HEIGHT));
	int cursorId = events.getCursor();
	Common::Point mousePos = events.mousePos();

	talk._talkToAbort = false;
	
	// Animate the mouse cursor
	if (cursorId >= WAIT) {
		if (++cursorId > (WAIT + 2))
			cursorId = WAIT;

		events.setCursor((CursorId)cursorId);
	}

	if (ui._menuMode == LOOK_MODE) {
		if (mousePos.y > CONTROLS_Y1)
			events.setCursor(ARROW);
		else if (mousePos.y < CONTROLS_Y)
			events.setCursor(MAGNIFY);
	}

	// Check for setting magnifying glass cursor
	if (ui._menuMode == INV_MODE || ui._menuMode == USE_MODE || ui._menuMode == GIVE_MODE) {
		if (inv._invMode == INVMODE_LOOK) {
			// Only show Magnifying glass cursor if it's not on the inventory command line
			if (mousePos.y < CONTROLS_Y || mousePos.y >(CONTROLS_Y1 + 13))
				events.setCursor(MAGNIFY);
			else
				events.setCursor(ARROW);
		} else {
			events.setCursor(ARROW);
		}
	}

	if (sound._diskSoundPlaying && !*sound._soundIsOn) {
		// Loaded sound just finished playing
		// TODO: This is horrible.. refactor into the Sound class
		delete[] sound._digiBuf;
		sound._diskSoundPlaying = false;
	}

	if (_restoreFlag) {
		if (people[AL]._type == CHARACTER)
			people[AL].checkSprite();

		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			if (_bgShapes[idx]._type == ACTIVE_BG_SHAPE)
				_bgShapes[idx].checkObject(_bgShapes[idx]);
		}

		if (people._portraitLoaded && people._portrait._type == ACTIVE_BG_SHAPE)
			people._portrait.checkObject(people._portrait);

		for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
			if (_canimShapes[idx]._type != INVALID && _canimShapes[idx]._type != REMOVE)
				_canimShapes[idx].checkObject(_bgShapes[0]);
		}

		if (_currentScene == 12 && _vm->getGameID() == GType_SerratedScalpel)
			((Scalpel::ScalpelEngine *)_vm)->eraseMirror12();

		// Restore the back buffer from the back buffer 2 in the changed area
		Common::Rect bounds(people[AL]._oldPosition.x, people[AL]._oldPosition.y,
			people[AL]._oldPosition.x + people[AL]._oldSize.x,
			people[AL]._oldPosition.y + people[AL]._oldSize.y);
		Common::Point pt(bounds.left, bounds.top);

		if (people[AL]._type == CHARACTER)
			screen.restoreBackground(bounds);
		else if (people[AL]._type == REMOVE)
			screen._backBuffer1.blitFrom(screen._backBuffer2, pt, bounds);

		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			Object &o = _bgShapes[idx];
			if (o._type == ACTIVE_BG_SHAPE || o._type == HIDE_SHAPE || o._type == REMOVE)
				screen.restoreBackground(o.getOldBounds());
		}

		if (people._portraitLoaded)
			screen.restoreBackground(Common::Rect(
				people._portrait._oldPosition.x, people._portrait._oldPosition.y,
				people._portrait._oldPosition.x + people._portrait._oldSize.x,
				people._portrait._oldPosition.y + people._portrait._oldSize.y
			));

		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			Object &o = _bgShapes[idx];
			if (o._type == NO_SHAPE && ((o._flags & 1) == 0)) {
				// Restore screen area
				screen._backBuffer1.blitFrom(screen._backBuffer2, o._position,
					Common::Rect(o._position.x, o._position.y,
					o._position.x + o._noShapeSize.x, o._position.y + o._noShapeSize.y));

				o._oldPosition = o._position;
				o._oldSize = o._noShapeSize;
			}
		}

		for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
			Object &o = _canimShapes[idx];
			if (o._type == ACTIVE_BG_SHAPE || o._type == HIDE_SHAPE || o._type == REMOVE)
				screen.restoreBackground(Common::Rect(o._oldPosition.x, o._oldPosition.y,
					o._oldPosition.x + o._oldSize.x, o._oldPosition.y + o._oldSize.y));
		}
	}

	//
	// Update the background objects and canimations
	//

	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if (o._type == ACTIVE_BG_SHAPE || o._type == NO_SHAPE)
			o.adjustObject();
	}

	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		if (_canimShapes[idx]._type != INVALID)
			_canimShapes[idx].adjustObject();
	}

	if (people[AL]._type == CHARACTER && people._holmesOn)
		people[AL].adjustSprite();

	// Flag the bg shapes which need to be redrawn
	checkBgShapes(people[AL]._imageFrame, 
		Common::Point(people[AL]._position.x / 100, people[AL]._position.y / 100));

	if (_currentScene == 12 && _vm->getGameID() == GType_SerratedScalpel)
		((Scalpel::ScalpelEngine *)_vm)->doMirror12();

	// Draw all active shapes which are behind the person
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if (o._type == ACTIVE_BG_SHAPE && o._misc == BEHIND)
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
	}
	
	// Draw all canimations which are behind the person
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		Object &o = _canimShapes[idx];
		if (o._type == ACTIVE_BG_SHAPE && o._misc == BEHIND) {
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
		}
	}

	// Draw all active shapes which are HAPPEN and behind the person
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if (o._type == ACTIVE_BG_SHAPE && o._misc == NORMAL_BEHIND)
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
	}

	// Draw all canimations which are NORMAL and behind the person
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		Object &o = _canimShapes[idx];
		if (o._type == ACTIVE_BG_SHAPE && o._misc == NORMAL_BEHIND) {
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
		}
	}

	// Draw the person if not animating
	if (people[AL]._type == CHARACTER && people.isHolmesActive()) {
		// If Holmes is too far to the right, move him back so he's on-screen
		int xRight = SHERLOCK_SCREEN_WIDTH - 2 - people[AL]._imageFrame->_frame.w;
		int tempX = MIN(people[AL]._position.x / 100, xRight);

		bool flipped = people[AL]._frameNumber == WALK_LEFT || people[AL]._frameNumber == STOP_LEFT ||
			people[AL]._frameNumber == WALK_UPLEFT || people[AL]._frameNumber == STOP_UPLEFT ||
			people[AL]._frameNumber == WALK_DOWNRIGHT || people[AL]._frameNumber == STOP_DOWNRIGHT;
		screen._backBuffer1.transBlitFrom(people[AL]._imageFrame->_frame,
			Common::Point(tempX, people[AL]._position.y / 100 - people[AL]._imageFrame->_frame.h), flipped);
	}

	// Draw all static and active shapes are NORMAL and are in front of the person
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if ((o._type == ACTIVE_BG_SHAPE || o._type == STATIC_BG_SHAPE) && o._misc == NORMAL_FORWARD)
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
	}

	// Draw all static and active canimations that are NORMAL and are in front of the person
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		Object &o = _canimShapes[idx];
		if ((o._type == ACTIVE_BG_SHAPE || o._type == STATIC_BG_SHAPE) && o._misc == NORMAL_BEHIND) {
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
		}
	}

	// Draw all static and active shapes that are in front of the person
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if ((o._type == ACTIVE_BG_SHAPE || o._type == STATIC_BG_SHAPE) && o._misc == FORWARD)
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
	}

	// Draw any active portrait
	if (people._portraitLoaded && people._portrait._type == ACTIVE_BG_SHAPE)
		screen._backBuffer1.transBlitFrom(people._portrait._imageFrame->_frame,
			people._portrait._position, people._portrait._flags & 2);

	// Draw all static and active canimations that are in front of the person
	for (uint idx = 0; idx < _canimShapes.size(); ++idx) {
		Object &o = _canimShapes[idx];
		if ((o._type == ACTIVE_BG_SHAPE || o._type == STATIC_BG_SHAPE) && o._misc == FORWARD) {
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
		}
	}

	// Draw all NO_SHAPE shapes which have flag bit 0 clear
	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if (o._type == NO_SHAPE && (o._flags & 1) == 0)
			screen._backBuffer1.transBlitFrom(o._imageFrame->_frame, o._position, o._flags & 2);
	}

	// Bring the newly built picture to the screen
	if (_animating == 2) {
		_animating = 0;
		screen.slamRect(Common::Rect(0, 0, SHERLOCK_SCREEN_WIDTH, SHERLOCK_SCENE_HEIGHT));
	} else {
		if (people[AL]._type != INVALID && ((_goToScene == -1 || _ongoingCans == 0))) {
			if (people[AL]._type == REMOVE) {
				screen.slamRect(Common::Rect(
					people[AL]._oldPosition.x, people[AL]._oldPosition.y,
					people[AL]._oldPosition.x + people[AL]._oldSize.x,
					people[AL]._oldPosition.y + people[AL]._oldSize.y
				));
			} else {
				screen.flushImage(people[AL]._imageFrame,
					Common::Point(people[AL]._position.x / 100, 
						people[AL]._position.y / 100 - people[AL].frameHeight()),
					&people[AL]._oldPosition.x, &people[AL]._oldPosition.y,
					&people[AL]._oldSize.x, &people[AL]._oldSize.y);
			}
		}

		if (_currentScene == 12 && _vm->getGameID() == GType_SerratedScalpel)
			((Scalpel::ScalpelEngine *)_vm)->flushMirror12();

		for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
			Object &o = _bgShapes[idx];
			if ((o._type == ACTIVE_BG_SHAPE || o._type == REMOVE) && _goToScene == -1) {
				screen.flushImage(o._imageFrame, o._position,
					&o._oldPosition.x, &o._oldPosition.y, &o._oldSize.x, &o._oldSize.y);
			}
		}

		if (people._portraitLoaded) {
			if (people._portrait._type == REMOVE)
				screen.slamRect(Common::Rect(
					people._portrait._position.x, people._portrait._position.y,
					people._portrait._position.x + people._portrait._delta.x,
					people._portrait._position.y + people._portrait._delta.y
				));
			else
				screen.flushImage(people._portrait._imageFrame, people._portrait._position,
					&people._portrait._oldPosition.x, &people._portrait._oldPosition.y,
					&people._portrait._oldSize.x, &people._portrait._oldSize.y);
				
			if (people._portrait._type == REMOVE)
				people._portrait._type = INVALID;
		}

		if (_goToScene == -1) {
			for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
				Object &o = _bgShapes[idx];
				if (o._type == NO_SHAPE && (o._flags & 1) == 0) {
					screen.slamArea(o._position.x, o._position.y, o._oldSize.x, o._oldSize.y);
					screen.slamArea(o._oldPosition.x, o._oldPosition.y, o._oldSize.x, o._oldSize.y);
				} else if (o._type == HIDE_SHAPE) {
					// Hiding shape, so flush it out and mark it as hidden
					screen.flushImage(o._imageFrame, o._position,
						&o._oldPosition.x, &o._oldPosition.y, &o._oldSize.x, &o._oldSize.y);
					o._type = HIDDEN;
				}
			}
		}

		for (int idx = _canimShapes.size() - 1; idx >= 0; --idx) {
			Object &o = _canimShapes[idx];
			if (o._type == REMOVE) {
				if (_goToScene == -1)
					screen.slamArea(o._position.x, o._position.y, o._delta.x, o._delta.y);

				_canimShapes.remove_at(idx);
			} else if (o._type == ACTIVE_BG_SHAPE) {
				screen.flushImage(o._imageFrame, o._position,
					&o._oldPosition.x, &o._oldPosition.y, &o._oldSize.x, &o._oldSize.y);
			}
		}
	}

	_restoreFlag = true;

	events.wait(1);
	_doBgAnimDone = true;

	// Check if the method was called for calling a portrait, and a talk was
	// interrupting it. This talk file would not have been executed at the time, 
	// since we needed to finish the 'doBgAnim' to finish clearing the portrait
	if (people._clearingThePortrait && scripts._scriptMoreFlag == 3) {
		// Reset the flags and call to talk
		people._clearingThePortrait = scripts._scriptMoreFlag = 0;
		talk.talkTo(scripts._scriptName);
	}
}

void Scene::saveSceneStatus() {
	// TODO
}

/**
 * Attempts to find a background shape within the passed bounds. If found,
 * it will return the shape number, or -1 on failure.
 */
int Scene::findBgShape(const Common::Rect &r) {
	if (!_doBgAnimDone)
		// New frame hasn't been drawn yet
		return -1;

	for (int idx = (int)_bgShapes.size() - 1; idx >= 0; --idx) {
		Object &o = _bgShapes[idx];
		if (o._type != INVALID && o._type != NO_SHAPE && o._type != HIDDEN
			&& o._aType <= PERSON) {
			if (r.intersects(o.getNewBounds()))
				return idx;
		} else if (o._type == NO_SHAPE) {
			if (r.intersects(o.getNoShapeBounds()))
				return idx;
		}
	}

	return -1;
}

/**
 * Checks to see if the given position in the scene belongs to a given zone type.
 * If it is, the zone is activated and used just like a TAKL zone or aFLAG_SET zone.
 */
int Scene::checkForZones(const Common::Point &pt, int zoneType) {
	int matches = 0;

	for (uint idx = 0; idx < _bgShapes.size(); ++idx) {
		Object &o = _bgShapes[idx];
		if ((o._aType == zoneType && o._type != INVALID) && o._type != HIDDEN) {
			Common::Rect r = o._type == NO_SHAPE ? o.getNoShapeBounds() : o.getNewBounds();

			if (r.contains(pt)) {
				++matches;
				o.setFlagsAndToggles();
				_vm->_talk->talkTo(o._use[0]._target);
			}
		}
	}

	return matches;
}

/**
 * Check which zone the the given position is located in.
 */
int Scene::whichZone(const Common::Point &pt) {
	for (uint idx = 0; idx < _zones.size(); ++idx) {
		if (_zones[idx].contains(pt))
			return idx;
	}

	return -1;
}

/**
 * Returns the index of the closest zone to a given point.
 */
int Scene::closestZone(const Common::Point &pt) {
	int dist = 1000;
	int zone = -1;

	for (uint idx = 0; idx < _zones.size(); ++idx) {
		Common::Point zc((_zones[idx].left + _zones[idx].right) / 2,
			(_zones[idx].top + _zones[idx].bottom) / 2);
		int d = ABS(zc.x - pt.x) + ABS(zc.y - pt.y);

		if (d < dist) {
			// Found a closer zone
			dist = d;
			zone = idx;
		}
	}

	return zone;
}

} // End of namespace Sherlock