aboutsummaryrefslogtreecommitdiff
path: root/engines/lure/res_struct.cpp
blob: 654430c46bcc4abb94f547a28ba30b2c515a9429 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2005-2006 The ScummVM project
 *
 * 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "lure/res.h"
#include "lure/disk.h"
#include "lure/scripts.h"
#include "common/endian.h"

namespace Lure {

const Action sortedActions[] = {ASK, BRIBE, BUY, CLOSE, DRINK, EXAMINE, GET, GIVE, 
	GO_TO, LOCK, LOOK, LOOK_AT, LOOK_THROUGH, OPEN, OPERATE, PULL, PUSH, RETURN, 
	STATUS, TALK_TO, TELL, UNLOCK, USE, NONE};

int actionNumParams[NPC_JUMP_ADDRESS+1] = {0, 
	1, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 0, 1,
	0, 1, 1, 1, 1, 0, 0, 2, 1, 1, 0, 0, 1, 1, 2, 2, 5, 2, 2, 1};

// Barman related frame lists

uint16 basicPolish[] = {8+13,8+14,8+15,8+16,8+17,8+18,8+17,8+16,8+15,8+14,
	8+15,8+16,8+17,8+18,8+17,8+16,8+15,8+14,8+13,0};

uint16 sidsFetch[] = {12+1,12+2,12+3,12+4,12+5,12+6,12+5,12+6,12+5,12+4,12+3,12+7,12+8,0};

uint16 nelliesScratch[] = {11+1,11+2,11+3,11+4,11+5,11+4,11+5,11+4,11+5,11+4,11+3,11+2,11+1,0};

uint16 nelliesFetch[] = {1,2,3,4,5,4,5,4,3,2,6,7,0};

uint16 ewansFetch[] = {13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,0};

uint16 ewanExtraGraphic1[]= {
	28,29,30,31,32,33,34,35,36,37,
	38,39,40,41,42,43,44,45,46,47,
	48,
	40,39,38,37,36,35,34,33,32,31,30,29,28,
	0};

uint16 ewanExtraGraphic2[] = {
	1,2,3,4,5,6,7,8,9,
	10,11,12,13,14,15,16,17,18,19,
	20,21,22,23,24,0};

BarEntry barList[3] = {
	{29, SID_ID, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, {&basicPolish[0], &sidsFetch[0], NULL, NULL}, 13, NULL},
	{32, NELLIE_ID, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, {&nelliesScratch[0], &nelliesFetch[0], NULL, NULL}, 14, NULL},
	{35, EWAN_ID, {{0, 0}, {0, 0}, {0, 0}, {0, 0}}, {&ewansFetch[0], &ewansFetch[0], 
		&ewanExtraGraphic1[0], &ewanExtraGraphic2[0]}, 16, NULL}
};

// Room data holding class

RoomData::RoomData(RoomResource *rec, MemoryBlock *pathData) { 
	roomNumber = FROM_LE_16(rec->roomNumber);
	hdrFlags = rec->hdrFlags;
	actions = FROM_LE_32(rec->actions) & 0xfffffff;
	flags = (FROM_LE_32(rec->actions) >> 24) & 0xf0;
	descId = FROM_LE_16(rec->descId);
	sequenceOffset = FROM_LE_16(rec->sequenceOffset);
	numLayers = FROM_LE_16(rec->numLayers);

	paths.load(pathData->data() + (roomNumber - 1) * ROOM_PATHS_SIZE);

	for (int ctr = 0; ctr < 4; ++ctr)
		layers[ctr] = FROM_LE_16(rec->layers[ctr]);

	clippingXStart = FROM_LE_16(rec->clippingXStart);
	clippingXEnd = FROM_LE_16(rec->clippingXEnd);
	walkBounds.left = FROM_LE_16(rec->walkBounds.xs);
	walkBounds.right = FROM_LE_16(rec->walkBounds.xe);
	walkBounds.top = FROM_LE_16(rec->walkBounds.ys);
	walkBounds.bottom = FROM_LE_16(rec->walkBounds.ye);
}

// Room exit hotspot area holding class

RoomExitHotspotData::RoomExitHotspotData(RoomExitHotspotResource *rec) {
	hotspotId = FROM_LE_16(rec->hotspotId);
	xs = FROM_LE_16(rec->xs);
	ys = FROM_LE_16(rec->ys);
	xe = FROM_LE_16(rec->xe);
	ye = FROM_LE_16(rec->ye);
	cursorNum = rec->cursorNum;
	destRoomNumber = FROM_LE_16(rec->destRoomNumber);
}

//  Room exit class

RoomExitData::RoomExitData(RoomExitResource *rec) {
	xs = rec->xs; 
	ys = rec->ys;
	xe = rec->xe;
	ye = rec->ye;
	sequenceOffset = rec->sequenceOffset;
	roomNumber = rec->newRoom;
	x = rec->newRoomX;
	y = rec->newRoomY;

	switch (rec->direction) {
	case 0x80: 
		direction = UP;
		break;
	case 0x40: 
		direction = DOWN;
		break;
	case 0x20: 
		direction = LEFT;
		break;
	case 0x10: 
		direction = RIGHT;
		break;
	default:
		direction = NO_DIRECTION;
		break;
	}
}

bool RoomExitData::insideRect(int16 xp, int16 yp) {
	return ((xp >= xs) && (xp < xe) && (yp >= ys) && (yp < ye));
}

RoomExitData *RoomExitList::checkExits(int16 xp, int16 yp) {
	iterator i;
	for (i = begin(); i != end(); i++) {
		RoomExitData *rec = *i;
		if (rec->insideRect(xp, yp)) {
			return rec;
		}
	}
	return NULL;
}

// Room paths

bool RoomPathsData::isOccupied(int x, int y) {
	if ((x < 0) || (y < 0) || (x >= ROOM_PATHS_WIDTH) || (y >= ROOM_PATHS_HEIGHT))
		// Off screen, so flag as not occupied
		return false;
	return (_data[y * 5 + (x >> 3)] & (0x80 >> (x % 8))) != 0;
}

void RoomPathsData::setOccupied(int x, int y, int width) {
	if ((x < 0) || (y < 0) || (x >= ROOM_PATHS_WIDTH) || (y >= ROOM_PATHS_HEIGHT))
		return;

	byte *p = &_data[y * 5 + (x / 8)];
	byte bitMask = 0x80 >> (x % 8);

	for (int bitCtr = 0; bitCtr < width; ++bitCtr) {
		*p |= bitMask;
		bitMask >>= 1;
		if (bitMask == 0) {
			++p;
			bitMask = 0x80;
		}
	}
}

void RoomPathsData::clearOccupied(int x, int y, int width) {
	if ((x < 0) || (y < 0) || (x >= ROOM_PATHS_WIDTH) || (y >= ROOM_PATHS_HEIGHT))
		return;

	byte *p = &_data[y * 5 + (x / 8)];
	byte bitMask = 0x80 >> (x % 8);

	for (int bitCtr = 0; bitCtr < width; ++bitCtr) {
		*p &= ~bitMask;
		bitMask >>= 1;
		if (bitMask == 0) {
			++p;
			bitMask = 0x80;
		}
	}
}

// decompresses the bit-packed data for which parts of a room are occupied
// into a byte array. It also adds a column and row of padding around the
// edges of the screen, and extends occupied areas to adjust for the width
// of the chracter

void RoomPathsData::decompress(RoomPathsDecompressedData &dataOut, int characterWidth) {
	byte *pIn = &_data[ROOM_PATHS_SIZE - 1];
	uint16 *pOut = &dataOut[DECODED_PATHS_WIDTH * DECODED_PATHS_HEIGHT - 1];
	byte v;
	int paddingCtr;
	int charWidth = (characterWidth - 1) >> 3;
	int charCtr = 0;
	bool charState = false;

	// Handle padding for last row, including left/right edge padding, as
	// well as the right column of the second row
	for (paddingCtr = 0; paddingCtr < (DECODED_PATHS_WIDTH + 1); ++paddingCtr)
		*pOut-- = 0;

	for (int y = 0; y < ROOM_PATHS_HEIGHT; ++y) {
		for (int x = 0; x < (ROOM_PATHS_WIDTH / 8); ++x) {
			// Get next byte, which containing bits for 8 blocks
			v = *pIn--;		

			for (int bitCtr = 0; bitCtr < 8; ++bitCtr) {
				bool isSet = (v & 1) != 0;
				v >>= 1;

				if (charState) {
					// Handling occupied characters adjusted for character width
					if (isSet) 
						// Reset character counter
						charCtr = charWidth;

					*pOut-- = 0xffff;
					charState = (--charCtr != 0);

				} else {
					// Normal decompression
					if (!isSet) {
						// Flag block is available for walking on
						*pOut-- = 0;
					} else {
						// Flag block is occupied
						*pOut-- = 0xffff;

						// Handling for adjusting for character widths
						charCtr = charWidth - 1;
						charState = charCtr >= 0;
					}
				}
			}
		}

		// Store 2 words to allow off-screen row-start/prior row end
		*pOut-- = 0;
		*pOut-- = 0;
		charState = false;
	}

	// Handle padding for final top row - no need for end column, as end of prior
	// row provided padding for it
	for (paddingCtr = 0; paddingCtr < (ROOM_PATHS_WIDTH + 1); ++paddingCtr)
		*pOut-- = 0;
}

// Room exit joins class

RoomExitJoinData::RoomExitJoinData(RoomExitJoinResource *rec) {
	hotspot1Id = FROM_LE_16(rec->hotspot1Id);
	h1CurrentFrame = rec->h1CurrentFrame;
	h1DestFrame = rec->h1DestFrame;
	h1OpenSound = rec->h1OpenSound;
	h1CloseSound = rec->h1CloseSound;
	hotspot2Id = FROM_LE_16(rec->hotspot2Id);
	h2CurrentFrame = rec->h2CurrentFrame;
	h2DestFrame = rec->h2DestFrame;
	h2OpenSound = rec->h2OpenSound;
	h2CloseSound = rec->h2CloseSound;
	blocked = rec->blocked;
}

// Hotspot action record

HotspotActionData::HotspotActionData(HotspotActionResource *rec) {
	action = (Action) rec->action;
	sequenceOffset = READ_LE_UINT16(&rec->sequenceOffset);
}

uint16 HotspotActionList::getActionOffset(Action action) {
	iterator i;
	for (i = begin(); i != end(); ++i) {
		HotspotActionData *rec = *i;
		if (rec->action == action) return rec->sequenceOffset;
	}

	return 0;
}


// Hotspot data

HotspotData::HotspotData(HotspotResource *rec) {
	hotspotId = FROM_LE_16(rec->hotspotId);
	nameId = FROM_LE_16(rec->nameId);
	descId = FROM_LE_16(rec->descId);
	descId2 = FROM_LE_16(rec->descId2);
	actions = READ_LE_UINT32(&rec->actions);
	actionsOffset = FROM_LE_16(rec->actionsOffset);
	flags = (byte) (actions >> 24) & 0xf0;
	actions &= 0xfffffff;

	roomNumber = FROM_LE_16(rec->roomNumber);
	layer = rec->layer;
	scriptLoadFlag = rec->scriptLoadFlag;
	loadOffset = FROM_LE_16(rec->loadOffset);
	startX = FROM_LE_16(rec->startX);
	startY = FROM_LE_16(rec->startY);
	width = FROM_LE_16(rec->width);
	height = FROM_LE_16(rec->height);
	widthCopy = FROM_LE_16(rec->widthCopy);
	heightCopy = FROM_LE_16(rec->heightCopy);
	yCorrection = FROM_LE_16(rec->yCorrection);
	walkX = FROM_LE_16(rec->walkX);
	walkY = FROM_LE_16(rec->walkY);
	talkX = rec->talkX;
	talkY = rec->talkY;
	colourOffset = FROM_LE_16(rec->colourOffset);
	animRecordId = FROM_LE_16(rec->animRecordId);
	sequenceOffset = FROM_LE_16(rec->sequenceOffset);
	tickProcOffset = FROM_LE_16(rec->tickProcOffset);
	tickTimeout = FROM_LE_16(rec->tickTimeout);
	tickSequenceOffset = FROM_LE_16(rec->tickSequenceOffset);
	npcSchedule = FROM_LE_16(rec->npcSchedule);
	characterMode = (CharacterMode) FROM_LE_16(rec->characterMode);
	delayCtr = FROM_LE_16(rec->delayCtr);
	flags2 = FROM_LE_16(rec->flags2);
	headerFlags = FROM_LE_16(rec->hdrFlags);

	// Initialise runtime fields
	actionCtr = 0;
	blockedState = BS_NONE;
	blockedFlag = false;
	coveredFlag = VB_INITIAL;
	talkMessageId = 0;
	talkDestCharacterId = 0;
	talkCountdown = 0;
	pauseCtr = 0;
	useHotspotId = 0;
	v2b = 0;
	actionHotspotId = 0;
	talkOverride = 0;
}

void HotspotData::saveToStream(WriteStream *stream) {
	// Write out the basic fields
	stream->writeUint16LE(descId);
	stream->writeUint16LE(descId2);
	stream->writeUint32LE(actions);
	stream->writeByte(flags);
	stream->writeByte(flags2);
	stream->writeByte(headerFlags);
	stream->writeSint16LE(startX);
	stream->writeSint16LE(startY);
	stream->writeUint16LE(roomNumber);

	stream->writeUint16LE(width);
	stream->writeUint16LE(height);
	stream->writeUint16LE(widthCopy);
	stream->writeUint16LE(heightCopy);
	stream->writeUint16LE(yCorrection);
	stream->writeUint16LE(sequenceOffset);
	stream->writeUint16LE(tickProcOffset);
	stream->writeUint16LE(tickTimeout);
	stream->writeUint16LE(tickSequenceOffset);
	stream->writeUint16LE(characterMode);
	stream->writeUint16LE(delayCtr);

	// Write out the runtime fields
	stream->writeUint16LE(actionCtr);
	stream->writeUint16LE(blockedState);
	stream->writeByte((byte)blockedFlag);
	stream->writeByte((byte)coveredFlag);
	stream->writeUint16LE(talkMessageId);
	stream->writeUint16LE(talkDestCharacterId);
	stream->writeUint16LE(talkCountdown);
	stream->writeUint16LE(pauseCtr);
	stream->writeUint16LE(useHotspotId);
	stream->writeUint16LE(use2HotspotId);
	stream->writeUint16LE(v2b);
	stream->writeUint16LE(actionHotspotId);
	stream->writeUint16LE(talkOverride);
}

void HotspotData::loadFromStream(ReadStream *stream) {
	// Read in the basic fields
	descId = stream->readUint16LE();
	descId2 = stream->readUint16LE();
	actions = stream->readUint32LE();
	flags = stream->readByte();
	flags2 = stream->readByte();
	headerFlags = stream->readByte();
	startX = stream->readSint16LE();
	startY = stream->readSint16LE();
	roomNumber = stream->readUint16LE();

	width = stream->readUint16LE();
	height = stream->readUint16LE();
	widthCopy = stream->readUint16LE();
	heightCopy = stream->readUint16LE();
	yCorrection = stream->readUint16LE();
	sequenceOffset = stream->readUint16LE();
	tickProcOffset = stream->readUint16LE();
	tickTimeout = stream->readUint16LE();
	tickSequenceOffset = stream->readUint16LE();
	characterMode = (CharacterMode) stream->readUint16LE();
	delayCtr = stream->readUint16LE();

	// Read in the runtime fields
	actionCtr = stream->readUint16LE();
	blockedState = (BlockedState)stream->readUint16LE();
	blockedFlag = stream->readByte() != 0;
	coveredFlag = (VariantBool)stream->readByte();
	talkMessageId = stream->readUint16LE();
	talkDestCharacterId = stream->readUint16LE();
	talkCountdown = stream->readUint16LE();
	pauseCtr = stream->readUint16LE();
	useHotspotId = stream->readUint16LE();
	use2HotspotId = stream->readUint16LE();
	v2b = stream->readUint16LE();
	actionHotspotId = stream->readUint16LE();
	talkOverride = stream->readUint16LE();
}

// Hotspot data list

void HotspotDataList::saveToStream(WriteStream *stream) {
	iterator i;
	for (i = begin(); i != end(); ++i)
	{
		HotspotData *hotspot = *i;
		stream->writeUint16LE(hotspot->hotspotId);
		hotspot->saveToStream(stream);
	}
	stream->writeUint16LE(0);
}

void HotspotDataList::loadFromStream(ReadStream *stream) {
	Resources &res = Resources::getReference();
	iterator i;
	uint16 hotspotId = stream->readUint16LE();
	while (hotspotId != 0)
	{
		HotspotData *hotspot = res.getHotspot(hotspotId);
		assert(hotspot);
		hotspot->loadFromStream(stream);
		hotspotId = stream->readUint16LE();
	}
}

// Hotspot override data

HotspotOverrideData::HotspotOverrideData(HotspotOverrideResource *rec) {
	hotspotId = FROM_LE_16(rec->hotspotId);
	xs = FROM_LE_16(rec->xs);
	ys = FROM_LE_16(rec->ys);
	xe = FROM_LE_16(rec->xe);
	ye = FROM_LE_16(rec->ye);
}

// Hotspot animation movement frame

MovementData::MovementData(MovementResource *rec) {
	frameNumber = FROM_LE_16(rec->frameNumber);
	xChange = FROM_LE_16(rec->xChange);
	yChange = FROM_LE_16(rec->yChange);
}

// List of movement frames

bool MovementDataList::getFrame(uint16 currentFrame, int16 &xChange, 
							   int16 &yChange, uint16 &nextFrame) {
	if (empty()) return false;
	bool foundFlag = false;
	iterator i;

	for (i = begin(); i != end(); ++i) {
		MovementData *rec = *i;
		if (foundFlag || (i == begin())) {
			xChange = rec->xChange;
			yChange = rec->yChange;
			nextFrame = rec->frameNumber;
			if (foundFlag) return true;
		}
		if (rec->frameNumber == currentFrame) foundFlag = true;
	}

	return true;
}


// Hotspot animation data

HotspotAnimData::HotspotAnimData(HotspotAnimResource *rec) {
	animRecordId = FROM_LE_16(rec->animRecordId);
	animId = FROM_LE_16(rec->animId);
	flags = FROM_LE_16(rec->flags);

	upFrame = rec->upFrame;
	downFrame = rec->downFrame;
	leftFrame = rec->leftFrame;
	rightFrame = rec->rightFrame;
}

// Hotspot action lists

HotspotActionList::HotspotActionList(uint16 id, byte *data) {
	recordId = id;
	uint16  numItems = READ_LE_UINT16(data);
	data += 2;

	HotspotActionResource *actionRec = (HotspotActionResource *) data;
	
	for (int actionCtr = 0; actionCtr < numItems; ++actionCtr, ++actionRec) {
		HotspotActionData *actionEntry = new HotspotActionData(actionRec);
		push_back(actionEntry);
	}
}

HotspotActionList *HotspotActionSet::getActions(uint16 recordId) {
	HotspotActionSet::iterator i;
	for (i = begin(); i != end(); ++i) {
		HotspotActionList *list = *i;
		if (list->recordId == recordId) return list;
	}

	return NULL;
}

// The following class holds the set of offsets for a character's talk set

TalkHeaderData::TalkHeaderData(uint16 charId, uint16 *entries) {
	uint16 *src, *dest;
	characterId = charId;

	// Get number of entries
	_numEntries = 0;
	src = entries;
	while (READ_LE_UINT16(src) != 0xffff) { ++src; ++_numEntries; }

	// Duplicate the list
	_data = (uint16 *) Memory::alloc(_numEntries * sizeof(uint16));
	src = entries; dest = _data;

	for (int ctr = 0; ctr < _numEntries; ++ctr, ++src, ++dest)
		*dest = READ_LE_UINT16(src);
}

TalkHeaderData::~TalkHeaderData() {
	free(_data);
}

uint16 TalkHeaderData::getEntry(int index) {
	if (index >= _numEntries) 
		error("Invalid talk index %d specified for hotspot %xh", 
			_numEntries, characterId);
	return _data[index];
}

// The following class holds a single talking entry

TalkEntryData::TalkEntryData(TalkDataResource *rec) {
	preSequenceId = FROM_LE_16(rec->preSequenceId);
	descId = FROM_LE_16(rec->descId);
	postSequenceId = FROM_LE_16(rec->postSequenceId);
}

// The following class acts as a container for all the talk entries and
// responses for a single record Id

TalkData::TalkData(uint16 id) {
	recordId = id;
}

TalkData::~TalkData() {
	entries.clear();
	responses.clear();
}

TalkEntryData *TalkData::getResponse(int index) {
	TalkEntryList::iterator i = responses.begin();
	int v = index;
	while (v-- > 0) {
		if (i == responses.end()) 
			error("Invalid talk response index %d specified", index);
		++i;
	}

	return *i;
}

// The following class handles a set of coordinates a character should walk to
// if they're to exit a room to a designated secondary room

RoomExitCoordinates::RoomExitCoordinates(RoomExitCoordinateEntryResource *rec) {
	int ctr;

	for (ctr = 0; ctr < ROOM_EXIT_COORDINATES_NUM_ENTRIES; ++ctr) {
		uint16 tempY = FROM_LE_16(rec->entries[ctr].y);
		_entries[ctr].x = FROM_LE_16(rec->entries[ctr].x);
		_entries[ctr].y = tempY & 0xfff;
		_entries[ctr].roomNumber = FROM_LE_16(rec->entries[ctr].roomNumber);
		_entries[ctr].hotspotIndexId = (tempY >> 12) << 4;
	}

	for (ctr = 0; ctr < ROOM_EXIT_COORDINATES_NUM_ROOMS; ++ctr) 
		_roomIndex[ctr] = rec->roomIndex[ctr];
}

RoomExitCoordinates &RoomExitCoordinatesList::getEntry(uint16 roomNumber) {
	RoomExitCoordinatesList::iterator i = begin();
	while (--roomNumber > 0) 
		++i;
	return **i;
}

RoomExitCoordinateData &RoomExitCoordinates::getData(uint16 destRoomNumber) {
	return _entries[_roomIndex[destRoomNumber - 1]];
}

// The following classes hold any sequence offsets that are being delayed

SequenceDelayData::SequenceDelayData(uint16 delay, uint16 seqOffset, bool canClearFlag) {
	OSystem &system = *g_system;

	// The delay is in number of ticks (1/18th of a second) - convert to milliseconds
	timeoutCtr = system.getMillis() + (delay * 1000 / 18);
	sequenceOffset = seqOffset;
	canClear = canClearFlag;
}

void SequenceDelayList::add(uint16 delay, uint16 seqOffset, bool canClear) {
	SequenceDelayData *entry = new SequenceDelayData(delay, seqOffset, canClear);
	push_front(entry);
}

void SequenceDelayList::tick() {
	uint32 currTime = g_system->getMillis();
	SequenceDelayList::iterator i;

	for (i = begin(); i != end(); i++) {
		SequenceDelayData *entry = *i;
		if (currTime >= entry->timeoutCtr) {
			// Timeout reached - delete entry from list and execute the sequence
			uint16 seqOffset = entry->sequenceOffset;
			erase(i);
			Script::execute(seqOffset);
			return;
		}
	}
}

void SequenceDelayList::clear() {
	SequenceDelayList::iterator i = begin();

	while (i != end()) {
		SequenceDelayData *entry = *i;
		if (entry->canClear)
			i = erase(i);
		else
			++i;
	}
}

// The following classes hold the NPC schedule classes

CharacterScheduleEntry::CharacterScheduleEntry(Action theAction, ...) {
	_parent = NULL;
	_action = theAction;

	va_list u_Arg;
	va_start(u_Arg, theAction);

	for (int paramCtr = 0; paramCtr < actionNumParams[_action]; ++paramCtr) 
		_params[paramCtr] = (uint16) va_arg(u_Arg, int);

	va_end(u_Arg);
	_numParams = actionNumParams[_action];
}

CharacterScheduleEntry::CharacterScheduleEntry(CharacterScheduleSet *parentSet, 
		CharacterScheduleResource *&rec) {
	_parent = parentSet;

	if ((rec->action == 0) || (rec->action > NPC_JUMP_ADDRESS))
		error("Invalid action encountered reading NPC schedule");

	_action = (Action) rec->action;
	_numParams = actionNumParams[_action];
	for (int index = 0; index < _numParams; ++index) 
		_params[index] = FROM_LE_16(rec->params[index]);

	rec = (CharacterScheduleResource *) ((byte *) rec + 
		(_numParams + 1) * sizeof(uint16));
}

uint16 CharacterScheduleEntry::param(int index) {
	if ((index < 0) || (index >= numParams()))
		error("Invalid parameter index %d on handling action %d", index, _action);
	return _params[index];
}

void CharacterScheduleEntry::setDetails(Action theAction, ...) {
	_action = theAction;
	_numParams = actionNumParams[_action];

	va_list list;
	va_start(list, theAction);

	for (int paramCtr = 0; paramCtr < actionNumParams[_action]; ++paramCtr) 
		_params[paramCtr] = (uint16) va_arg(list, int);

	va_end(list);
}

void CharacterScheduleEntry::setDetails2(Action theAction, int numParamEntries, uint16 *paramList) {
	_action = theAction;
	_numParams = numParamEntries;

	assert((numParamEntries >= 0) && (numParamEntries < (MAX_TELL_COMMANDS * 3)));
	for (int paramCtr = 0; paramCtr < numParamEntries; ++paramCtr)
		_params[paramCtr] = paramList[paramCtr];
}

CharacterScheduleEntry *CharacterScheduleEntry::next() {
	if (_parent) {
		CharacterScheduleSet::iterator i;
		for (i = _parent->begin(); i != _parent->end(); ++i) {
			if (*i == this) {
				++i;
				CharacterScheduleEntry *result = (i == _parent->end()) ? NULL : *i;
				return result;
			}
		}
	}

	return NULL;
}

uint16 CharacterScheduleEntry::id() {
	return (_parent == NULL) ? 0 : _parent->getId(this);
}

CharacterScheduleSet::CharacterScheduleSet(CharacterScheduleResource *rec, uint16 setId) {
	// Load up all the entries in the schedule

	while (rec->action != 0) {
		CharacterScheduleEntry *r = new CharacterScheduleEntry(this, rec);
		push_back(r);
	}

	_id = setId;
}

// Given a support data entry identifier, locates that entry in the list of data sets

CharacterScheduleEntry *CharacterScheduleList::getEntry(uint16 id, CharacterScheduleSet *currentSet) {
	// Respond to the special no entry with no record
	if (id == 0xffff) return NULL;

	// Handle jumps within a current set versus external jumps
	if ((id >> 10) == 0) {
		// Jump within current set
		if (currentSet == NULL)
			error("Local support data jump encountered outside of a support data sequence");
	} else {
		// Inter-set jump - locate the appropriate set
		int index = (id >> 10) - 1;

		iterator i = begin();
		while ((i != end()) && (index > 0)) {
			++i;
			--index;
		}

		if (i == end()) 
			error("Invalid index %d specified for support data set", id >> 8);
		currentSet = *i;
	}

	// Get the indexed instruction in the specified set
	int instructionIndex = id & 0x3ff;
	CharacterScheduleSet::iterator i = currentSet->begin();
	while ((i != currentSet->end()) && (instructionIndex > 0)) {
		++i;
		--instructionIndex;
	}
	if (i == currentSet->end())
		error("Invalid index %d specified within support data set", id & 0x3ff);

	return *i;
}

uint16 CharacterScheduleSet::getId(CharacterScheduleEntry *rec) {
	// Return an Id for the entry based on the id of the set combined with the 
	// index of the specific entry
	uint16 result = _id << 10;

	iterator i;
	for (i = begin(); i != end(); ++i, ++result) 
		if (*i == rec) break;
	if (i == end())
		error("Parent child relationship missing in character schedule set");
	return result;
}

// This classes is used to store a list of random action sets - one set per room

RandomActionSet::RandomActionSet(uint16 *&offset) {
	_roomNumber = READ_LE_UINT16(offset++);
	uint16 actionDetails = READ_LE_UINT16(offset++);
	_numActions = (actionDetails & 0xff);
	assert(_numActions <= 8);
	_types = new RandomActionType[_numActions];
	_ids = new uint16[_numActions];

	for (int actionIndex = 0; actionIndex < _numActions; ++actionIndex) {
		_ids[actionIndex] = READ_LE_UINT16(offset++);
		_types[actionIndex] = (actionDetails & (0x100 << actionIndex)) != 0 ? REPEATABLE : REPEAT_ONCE;
	}
}

RandomActionSet::~RandomActionSet() {
	delete _types;
	delete _ids;
}

RandomActionSet *RandomActionList::getRoom(uint16 roomNumber) {
	iterator i;
	for (i = begin(); i != end(); ++i) 
	{
		RandomActionSet *v = *i;
		if (v->roomNumber() == roomNumber)
			return v;
	}
	return NULL;
}

void RandomActionSet::saveToStream(Common::WriteStream *stream) {
	stream->writeByte(numActions());
	for (int actionIndex = 0; actionIndex < _numActions; ++actionIndex)
		stream->writeByte((byte)_types[actionIndex]);
}

void RandomActionSet::loadFromStream(Common::ReadStream *stream) {
	int amount = stream->readByte();
	assert(amount == _numActions);
	for (int actionIndex = 0; actionIndex < _numActions; ++actionIndex)
		_types[actionIndex] = (RandomActionType)stream->readByte();
}


void RandomActionList::saveToStream(Common::WriteStream *stream) {
	for (iterator i = begin(); i != end(); ++i) 
		(*i)->saveToStream(stream);
}

void RandomActionList::loadFromStream(Common::ReadStream *stream) {
	for (iterator i = begin(); i != end(); ++i) 
		(*i)->loadFromStream(stream);
}

// This class handles an indexed hotspot entry - which is used by the NPC code to
// determine whether exiting a room to another given room has an exit hotspot or not

RoomExitIndexedHotspotData::RoomExitIndexedHotspotData(RoomExitIndexedHotspotResource *rec) {
	roomNumber = rec->roomNumber;
	hotspotIndex = rec->hotspotIndex;
	hotspotId = FROM_LE_16(rec->hotspotId);
}

uint16 RoomExitIndexedHotspotList::getHotspot(uint16 roomNumber, uint8 hotspotIndexId) {
	iterator i;
	for (i = begin(); i != end(); ++i) {
		RoomExitIndexedHotspotData *entry = *i;
		if ((entry->roomNumber == roomNumber) && (entry->hotspotIndex == hotspotIndexId))
			return entry->hotspotId;
	}

	// No hotspot
	return 0xffff;
}

// Paused character list methods

PausedCharacter::PausedCharacter(uint16 SrcCharId, uint16 DestCharId) {
	srcCharId = SrcCharId;
	destCharId = DestCharId;
	counter = IDLE_COUNTDOWN_SIZE;
	charHotspot = Resources::getReference().getHotspot(DestCharId);
	assert(charHotspot);
}

void PausedCharacterList::reset(uint16 hotspotId) {
	iterator i;
	for (i = begin(); i != end(); ++i) {
		PausedCharacter *rec = *i;

		if (rec->srcCharId == hotspotId) {
			rec->counter = 1;
			if (rec->destCharId < START_EXIT_ID)
				rec->charHotspot->pauseCtr = 1;
		}
	}
}

void PausedCharacterList::countdown() {
	iterator i = begin();

	while (i != end()) {
		PausedCharacter *rec = *i;
		--rec->counter;

		// Handle reflecting counter to hotspot
		if (rec->destCharId < START_EXIT_ID) 
			rec->charHotspot->pauseCtr = rec->counter + 1;

		// If counter has reached zero, remove entry from list
		if (rec->counter == 0) 
			i = erase(i);
		else 
			++i;
	}
}

void PausedCharacterList::scan(Hotspot &h) {
	iterator i;

	if (h.blockedState() != BS_NONE) {

		for (i = begin(); i != end(); ++i) {
			PausedCharacter *rec = *i;

			if (rec->srcCharId == h.hotspotId()) {
				rec->counter = IDLE_COUNTDOWN_SIZE;
				
				if (rec->destCharId < START_EXIT_ID) 
					rec->charHotspot->pauseCtr = IDLE_COUNTDOWN_SIZE;
			}
		}
	}
}

int PausedCharacterList::check(uint16 charId, int numImpinging, uint16 *impingingList) {
	Resources &res = Resources::getReference();
	PausedCharacterList::iterator i;
	int result = 0;
	Hotspot *charHotspot = res.getActiveHotspot(charId);
	assert(charHotspot);

	for (int index = 0; index < numImpinging; ++index) {
		Hotspot *hotspot = res.getActiveHotspot(impingingList[index]);
		if ((!hotspot) || (!hotspot->currentActions().isEmpty() &&
			(hotspot->currentActions().top().action() == EXEC_HOTSPOT_SCRIPT)))
			// Entry is skipped if hotspot not present or is executing hotspot script		
			continue;

		// Scan through the pause list to see if there's a record for the
		// calling character and the impinging list entry
		bool foundEntry = false;
		for (i = res.pausedList().begin(); !foundEntry && (i != res.pausedList().end()); ++i) {
			PausedCharacter *rec = *i;
			foundEntry = (rec->srcCharId == charId) && 
				(rec->destCharId == hotspot->hotspotId());
		}

		if (foundEntry)
			// There was, so move to next impinging character entry
			continue;

		if ((hotspot->hotspotId() == PLAYER_ID) && !hotspot->coveredFlag())
		{
			hotspot->updateMovement();
			return 1;
		}

		// Add a new paused character entry
		PausedCharacter *entry = new PausedCharacter(charId, hotspot->hotspotId());
		res.pausedList().push_back(entry);
		charHotspot->setBlockedState(BS_INITIAL); 

		if (hotspot->hotspotId() < START_EXIT_ID) {
			if ((charHotspot->characterMode() == CHARMODE_PAUSED) || 
				((charHotspot->pauseCtr() == 0) && 
				(charHotspot->characterMode() == CHARMODE_NONE))) {
				hotspot->resource()->use2HotspotId = charId;
			}

			hotspot->setPauseCtr(IDLE_COUNTDOWN_SIZE);
		}

		result = 2;
		if (charHotspot->currentActions().isEmpty())
			charHotspot->currentActions().addFront(START_WALKING, charHotspot->roomNumber());
		else
			charHotspot->currentActions().top().setAction(START_WALKING);
	}

	return result;
}

// Wrapper class for the barman lists

BarEntry &BarmanLists::getDetails(uint16 roomNumber)
{
	for (int index = 0; index < 3; ++index)
		if (barList[index].roomNumber == roomNumber)
			return barList[index];
	error("Invalid room %d specified for barman details retrieval", roomNumber);
}

void BarmanLists::saveToStream(Common::WriteStream *stream)
{
	for (int index = 0; index < 2; ++index)
	{
		uint16 value = (barList[index].currentCustomer - &barList[index].customers[0]) / sizeof(BarEntry);
		stream->writeUint16LE(value);
		for (int ctr = 0; ctr < NUM_SERVE_CUSTOMERS; ++ctr)
		{
			stream->writeUint16LE(barList[index].customers[ctr].hotspotId);
			stream->writeByte(barList[index].customers[ctr].serveFlags);
		}
	}
}

void BarmanLists::loadFromStream(Common::ReadStream *stream)
{
	for (int index = 0; index < 2; ++index)
	{
		int16 value = stream->readUint16LE();
		barList[index].currentCustomer = (value == 0) ? NULL : &barList[index].customers[value];

		for (int ctr = 0; ctr < NUM_SERVE_CUSTOMERS; ++ctr)
		{
			barList[index].customers[ctr].hotspotId = stream->readUint16LE();
			barList[index].customers[ctr].serveFlags = stream->readByte();
		}
	}
}

// String list resource class

void StringList::load(MemoryBlock *data) {
	_data = Memory::allocate(data->size());
	_data->copyFrom(data);

	_numEntries = READ_LE_UINT16(_data->data());
	char *p = (char *) _data->data() + sizeof(uint16);

	_entries = (char **) Memory::alloc(_numEntries * sizeof(char *));

	for (int index = 0; index < _numEntries; ++index) {
		_entries[index] = p;
		p += strlen(p) + 1;	
	}
}

void StringList::clear() {
	if (_numEntries != 0) {
		Memory::dealloc(_entries);
		delete _data;
		_numEntries = 0;
	}
}

// Field list and miscellaneous variables

ValueTableData::ValueTableData() {
	_numGroats = 0;
	_playerNewPos.roomNumber = 0;
	_playerNewPos.position.x = 0;
	_playerNewPos.position.y = 0;
	_playerPendingPos.pos.x = 0;
	_playerPendingPos.pos.y = 0;
	_playerPendingPos.isSet = false;
	_flags = GAMEFLAG_4 | GAMEFLAG_1;
	_hdrFlagMask = 1;
	_wanderingCharsLoaded = false;

	for (uint16 index = 0; index < NUM_VALUE_FIELDS; ++index)
		_fieldList[index] = 0;
}

bool ValueTableData::isKnownField(uint16 fieldIndex) {
	return ((fieldIndex <= 10) && (fieldIndex != 6)) ||
		(fieldIndex == 15) || ((fieldIndex >= 18) && (fieldIndex <= 20));
}

uint16 ValueTableData::getField(uint16 fieldIndex) {
	if (fieldIndex > NUM_VALUE_FIELDS)
		error("Invalid field index specified %d", fieldIndex);
//	if (!isKnownField(fieldIndex))
//		warning("Unknown field index %d in GET_FIELD opcode", fieldIndex);
	return _fieldList[fieldIndex];
}

uint16 ValueTableData::getField(FieldName fieldName) {
	return getField((uint16) fieldName);
}

void ValueTableData::setField(uint16 fieldIndex, uint16 value) {
	if (fieldIndex > NUM_VALUE_FIELDS)
		error("Invalid field index specified %d", fieldIndex);
	_fieldList[fieldIndex] = value;
//	if (!isKnownField(fieldIndex))
//		warning("Unknown field index %d in SET_FIELD opcode", fieldIndex);
}

void ValueTableData::setField(FieldName fieldName, uint16 value) {
	setField((uint16) fieldName, value);
}

void ValueTableData::saveToStream(Common::WriteStream *stream)
{
	// Write out the special fields
	stream->writeUint16LE(_numGroats);
	stream->writeSint16LE(_playerNewPos.position.x);
	stream->writeSint16LE(_playerNewPos.position.y);
	stream->writeUint16LE(_playerNewPos.roomNumber);
	stream->writeByte(_playerPendingPos.isSet);
	stream->writeSint16LE(_playerPendingPos.pos.x);
	stream->writeSint16LE(_playerPendingPos.pos.y);
	stream->writeByte(_flags);
	stream->writeByte(_hdrFlagMask);
	stream->writeByte(_wanderingCharsLoaded);
	
	// Write out the special fields
	for (int index = 0; index < NUM_VALUE_FIELDS; ++index)
		stream->writeUint16LE(_fieldList[index]);
}

void ValueTableData::loadFromStream(Common::ReadStream *stream)
{
	// Load special fields
	_numGroats = stream->readUint16LE();
	_playerNewPos.position.x = stream->readSint16LE();
	_playerNewPos.position.y = stream->readSint16LE();
	_playerNewPos.roomNumber = stream->readUint16LE();
	_playerPendingPos.isSet = stream->readByte() != 0;
	_playerPendingPos.pos.x = stream->readSint16LE();
	_playerPendingPos.pos.y = stream->readSint16LE();
	_flags = stream->readByte();
	_hdrFlagMask = stream->readByte();
	_wanderingCharsLoaded = stream->readByte() != 0;
	
	// Read in the field list
	for (int index = 0; index < NUM_VALUE_FIELDS; ++index)
		_fieldList[index] = stream->readUint16LE();
}

} // end of namespace Lure