aboutsummaryrefslogtreecommitdiff
path: root/engines/hugo/schedule.cpp
blob: 0c3c1f44bd4d28b1c4942168bc71e3e2082c5456 (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
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
/* 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 original Hugo Trilogy source code
 *
 * Copyright (c) 1989-1995 David P. Gray
 *
 */

// This module contains all the scheduling and timing stuff

#include "common/debug.h"
#include "common/system.h"
#include "common/textconsole.h"

#include "hugo/hugo.h"
#include "hugo/schedule.h"
#include "hugo/file.h"
#include "hugo/display.h"
#include "hugo/util.h"
#include "hugo/object.h"
#include "hugo/sound.h"
#include "hugo/parser.h"
#include "hugo/text.h"
#include "hugo/route.h"
#include "hugo/mouse.h"

namespace Hugo {

Scheduler::Scheduler(HugoEngine *vm) : _vm(vm) {
	_actListArr = nullptr;
	_curTick = 0;
	_oldTime = 0;
	_refreshTimeout = 0;
	_points = nullptr;
	_screenActs = nullptr;

	memset(_events, 0, sizeof(_events));
	_numBonuses = 0;
	_screenActsSize = 0;

	_actListArrSize = 0;
	_alNewscrIndex = 0;
	_freeEvent = nullptr;
	_headEvent = nullptr;
	_tailEvent = nullptr;
}

Scheduler::~Scheduler() {
}

void Scheduler::initCypher() {
	_cypher = getCypher();
}

/**
 * Initialize the timer event queue
 */
void Scheduler::initEventQueue() {
	debugC(1, kDebugSchedule, "initEventQueue");

	// Chain nextEvent from first to last
	for (int i = kMaxEvents; --i;)
		_events[i - 1]._nextEvent = &_events[i];
	_events[kMaxEvents - 1]._nextEvent = nullptr;

	// Chain prevEvent from last to first
	for (int i = 1; i < kMaxEvents; i++)
		_events[i]._prevEvent = &_events[i - 1];
	_events[0]._prevEvent = nullptr;

	_headEvent = _tailEvent = nullptr;              // Event list is empty
	_freeEvent = _events;                           // Free list is full
}

/**
 * Return a ptr to an event structure from the free list
 */
Event *Scheduler::getQueue() {
	debugC(4, kDebugSchedule, "getQueue");

	if (!_freeEvent)                                // Error: no more events available
		error("An error has occurred: %s", "getQueue");
	Event *resEvent = _freeEvent;
	_freeEvent = _freeEvent->_nextEvent;
	resEvent->_nextEvent = nullptr;
	return resEvent;
}

/**
 * Call Insert_action for each action in the list supplied
 */
void Scheduler::insertActionList(const uint16 actIndex) {
	debugC(1, kDebugSchedule, "insertActionList(%d)", actIndex);

	if (_actListArr[actIndex]) {
		for (int i = 0; _actListArr[actIndex][i]._a0._actType != ANULL; i++)
			insertAction(&_actListArr[actIndex][i]);
	}
}

/**
 * Return system time in ticks.  A tick is 1/TICKS_PER_SEC mS
 */
uint32 Scheduler::getWinTicks() const {
	debugC(5, kDebugSchedule, "getWinTicks()");

	return _vm->getGameStatus()._tick;
}

/**
 * Return system time in ticks.  A tick is 1/TICKS_PER_SEC mS
 * If update FALSE, simply return last known time
 * Note that this is real time unless a processing cycle takes longer than
 * a real tick, in which case the system tick is simply incremented
 */
uint32 Scheduler::getDosTicks(const bool updateFl) {
	debugC(5, kDebugSchedule, "getDosTicks(%s)", (updateFl) ? "TRUE" : "FALSE");

	uint32 t_now;                                   // Current wall time in ticks

	if (!updateFl)
		return(_curTick);

	if (_oldTime == 0)
		_oldTime = (uint32) floor((double) (g_system->getMillis() * _vm->getTPS() / 1000));
	// Calculate current wall time in ticks
	t_now = g_system->getMillis() * _vm->getTPS() / 1000;

	if ((t_now - _oldTime) > 0) {
		_oldTime = t_now;
		_curTick++;
	}
	return(_curTick);
}

/**
 * Add indecated bonus to score if not added already
 */
void Scheduler::processBonus(const int bonusIndex) {
	debugC(1, kDebugSchedule, "processBonus(%d)", bonusIndex);

	if (!_points[bonusIndex]._scoredFl) {
		_vm->adjustScore(_points[bonusIndex]._score);
		_points[bonusIndex]._scoredFl = true;
	}
}

/**
 * Transition to a new screen as follows:
 * 1. Clear out all non-global events from event list.
 * 2. Set the new screen (in the hero object and any carried objects)
 * 3. Read in the screen files for the new screen
 * 4. Schedule action list for new screen
 * 5. Initialize prompt line and status line
 */
void Scheduler::newScreen(const int screenIndex) {
	debugC(1, kDebugSchedule, "newScreen(%d)", screenIndex);

	// Make sure the background file exists!
	if (!_vm->isPacked()) {
		Common::String filename = Common::String(_vm->_text->getScreenNames(screenIndex));
		if (!Common::File::exists(_vm->_picDir + filename + ".PCX") &&
			!Common::File::exists(filename + ".ART")) {
				error("Unable to find background file for %s", filename.c_str());
			return;
		}
	}

	// 1. Clear out all local events
	Event *curEvent = _headEvent;                 // The earliest event
	Event *wrkEvent;                              // Event ptr
	while (curEvent) {                              // While mature events found
		wrkEvent = curEvent->_nextEvent;             // Save p (becomes undefined after Del)
		if (curEvent->_localActionFl)
			delQueue(curEvent);                     // Return event to free list
		curEvent = wrkEvent;
	}

	// 2. Set the new screen in the hero object and any being carried
	_vm->setNewScreen(screenIndex);

	// 3. Read in new screen files
	_vm->readScreenFiles(screenIndex);

	// 4. Schedule action list for this screen
	_vm->_scheduler->screenActions(screenIndex);

	// 5. Initialize prompt line and status line
	_vm->_screen->initNewScreenDisplay();
}

/**
 * Transition to a new screen as follows:
 * 1. Set the new screen (in the hero object and any carried objects)
 * 2. Read in the screen files for the new screen
 * 3. Initialize prompt line and status line
 */
void Scheduler::restoreScreen(const int screenIndex) {
	debugC(1, kDebugSchedule, "restoreScreen(%d)", screenIndex);

	// 1. Set the new screen in the hero object and any being carried
	_vm->setNewScreen(screenIndex);

	// 2. Read in new screen files
	_vm->readScreenFiles(screenIndex);

	// 3. Initialize prompt line and status line
	_vm->_screen->initNewScreenDisplay();
}

/**
 * Wait (if necessary) for next synchronizing tick
 * Slow machines won't make it by the end of tick, so will just plod on
 * at their own speed, not waiting here, but free running.
 * Note: DOS Versions only
 */
void Scheduler::waitForRefresh() {
	debugC(5, kDebugSchedule, "waitForRefresh()");

	uint32 t;

	if (_refreshTimeout == 0)
		_refreshTimeout = getDosTicks(true);

	while ((t = getDosTicks(true)) < _refreshTimeout)
		;
	_refreshTimeout = ++t;
}

/**
 * Read kALnewscr used by maze (Hugo 2)
 */
void Scheduler::loadAlNewscrIndex(Common::ReadStream &in) {
	debugC(6, kDebugSchedule, "loadAlNewscrIndex(&in)");

	for (int varnt = 0; varnt < _vm->_numVariant; varnt++) {
		int numElem = in.readUint16BE();
		if (varnt == _vm->_gameVariant)
			_alNewscrIndex = numElem;
	}
}

/**
 * Load Points from Hugo.dat
 */
void Scheduler::loadPoints(Common::SeekableReadStream &in) {
	debugC(6, kDebugSchedule, "loadPoints(&in)");

	for (int varnt = 0; varnt < _vm->_numVariant; varnt++) {
		uint16 numElem = in.readUint16BE();
		if (varnt == _vm->_gameVariant) {
			_numBonuses = numElem;
			_points = (Point *)malloc(sizeof(Point) * _numBonuses);
			for (int i = 0; i < _numBonuses; i++) {
				_points[i]._score = in.readByte();
				_points[i]._scoredFl = false;
			}
		} else {
			in.skip(numElem);
		}
	}
}

void Scheduler::readAct(Common::ReadStream &in, Act &curAct) {
	uint16 numSubAct;

	curAct._a0._actType = (Action) in.readByte();
	switch (curAct._a0._actType) {
	case ANULL:              // -1
		break;
	case ASCHEDULE:          // 0
		curAct._a0._timer = in.readSint16BE();
		curAct._a0._actIndex = in.readUint16BE();
		break;
	case START_OBJ:          // 1
		curAct._a1._timer = in.readSint16BE();
		curAct._a1._objIndex = in.readSint16BE();
		curAct._a1._cycleNumb = in.readSint16BE();
		curAct._a1._cycle = (Cycle) in.readByte();
		break;
	case INIT_OBJXY:         // 2
		curAct._a2._timer = in.readSint16BE();
		curAct._a2._objIndex = in.readSint16BE();
		curAct._a2._x = in.readSint16BE();
		curAct._a2._y = in.readSint16BE();
		break;
	case PROMPT:             // 3
		curAct._a3._timer = in.readSint16BE();
		curAct._a3._promptIndex = in.readSint16BE();
		numSubAct = in.readUint16BE();
		curAct._a3._responsePtr = (int *)malloc(sizeof(int) * numSubAct);
		for (int k = 0; k < numSubAct; k++)
			curAct._a3._responsePtr[k] = in.readSint16BE();
		curAct._a3._actPassIndex = in.readUint16BE();
		curAct._a3._actFailIndex = in.readUint16BE();
		curAct._a3._encodedFl = (in.readByte() == 1) ? true : false;
		break;
	case BKGD_COLOR:         // 4
		curAct._a4._timer = in.readSint16BE();
		curAct._a4._newBackgroundColor = in.readUint32BE();
		break;
	case INIT_OBJVXY:        // 5
		curAct._a5._timer = in.readSint16BE();
		curAct._a5._objIndex = in.readSint16BE();
		curAct._a5._vx = in.readSint16BE();
		curAct._a5._vy = in.readSint16BE();
		break;
	case INIT_CARRY:         // 6
		curAct._a6._timer = in.readSint16BE();
		curAct._a6._objIndex = in.readSint16BE();
		curAct._a6._carriedFl = (in.readByte() == 1) ? true : false;
		break;
	case INIT_HF_COORD:      // 7
		curAct._a7._timer = in.readSint16BE();
		curAct._a7._objIndex = in.readSint16BE();
		break;
	case NEW_SCREEN:         // 8
		curAct._a8._timer = in.readSint16BE();
		curAct._a8._screenIndex = in.readSint16BE();
		break;
	case INIT_OBJSTATE:      // 9
		curAct._a9._timer = in.readSint16BE();
		curAct._a9._objIndex = in.readSint16BE();
		curAct._a9._newState = in.readByte();
		break;
	case INIT_PATH:          // 10
		curAct._a10._timer = in.readSint16BE();
		curAct._a10._objIndex = in.readSint16BE();
		curAct._a10._newPathType = in.readSint16BE();
		curAct._a10._vxPath = in.readByte();
		curAct._a10._vyPath = in.readByte();
		break;
	case COND_R:             // 11
		curAct._a11._timer = in.readSint16BE();
		curAct._a11._objIndex = in.readSint16BE();
		curAct._a11._stateReq = in.readByte();
		curAct._a11._actPassIndex = in.readUint16BE();
		curAct._a11._actFailIndex = in.readUint16BE();
		break;
	case TEXT:               // 12
		curAct._a12._timer = in.readSint16BE();
		curAct._a12._stringIndex = in.readSint16BE();
		break;
	case SWAP_IMAGES:        // 13
		curAct._a13._timer = in.readSint16BE();
		curAct._a13._objIndex1 = in.readSint16BE();
		curAct._a13._objIndex2 = in.readSint16BE();
		break;
	case COND_SCR:           // 14
		curAct._a14._timer = in.readSint16BE();
		curAct._a14._objIndex = in.readSint16BE();
		curAct._a14._screenReq = in.readSint16BE();
		curAct._a14._actPassIndex = in.readUint16BE();
		curAct._a14._actFailIndex = in.readUint16BE();
		break;
	case AUTOPILOT:          // 15
		curAct._a15._timer = in.readSint16BE();
		curAct._a15._objIndex1 = in.readSint16BE();
		curAct._a15._objIndex2 = in.readSint16BE();
		curAct._a15._dx = in.readByte();
		curAct._a15._dy = in.readByte();
		break;
	case INIT_OBJ_SEQ:       // 16
		curAct._a16._timer = in.readSint16BE();
		curAct._a16._objIndex = in.readSint16BE();
		curAct._a16._seqIndex = in.readSint16BE();
		break;
	case SET_STATE_BITS:     // 17
		curAct._a17._timer = in.readSint16BE();
		curAct._a17._objIndex = in.readSint16BE();
		curAct._a17._stateMask = in.readSint16BE();
		break;
	case CLEAR_STATE_BITS:   // 18
		curAct._a18._timer = in.readSint16BE();
		curAct._a18._objIndex = in.readSint16BE();
		curAct._a18._stateMask = in.readSint16BE();
		break;
	case TEST_STATE_BITS:    // 19
		curAct._a19._timer = in.readSint16BE();
		curAct._a19._objIndex = in.readSint16BE();
		curAct._a19._stateMask = in.readSint16BE();
		curAct._a19._actPassIndex = in.readUint16BE();
		curAct._a19._actFailIndex = in.readUint16BE();
		break;
	case DEL_EVENTS:         // 20
		curAct._a20._timer = in.readSint16BE();
		curAct._a20._actTypeDel = (Action) in.readByte();
		break;
	case GAMEOVER:           // 21
		curAct._a21._timer = in.readSint16BE();
		break;
	case INIT_HH_COORD:      // 22
		curAct._a22._timer = in.readSint16BE();
		curAct._a22._objIndex = in.readSint16BE();
		break;
	case EXIT:               // 23
		curAct._a23._timer = in.readSint16BE();
		break;
	case BONUS:              // 24
		curAct._a24._timer = in.readSint16BE();
		curAct._a24._pointIndex = in.readSint16BE();
		break;
	case COND_BOX:           // 25
		curAct._a25._timer = in.readSint16BE();
		curAct._a25._objIndex = in.readSint16BE();
		curAct._a25._x1 = in.readSint16BE();
		curAct._a25._y1 = in.readSint16BE();
		curAct._a25._x2 = in.readSint16BE();
		curAct._a25._y2 = in.readSint16BE();
		curAct._a25._actPassIndex = in.readUint16BE();
		curAct._a25._actFailIndex = in.readUint16BE();
		break;
	case SOUND:              // 26
		curAct._a26._timer = in.readSint16BE();
		curAct._a26._soundIndex = in.readSint16BE();
		break;
	case ADD_SCORE:          // 27
		curAct._a27._timer = in.readSint16BE();
		curAct._a27._objIndex = in.readSint16BE();
		break;
	case SUB_SCORE:          // 28
		curAct._a28._timer = in.readSint16BE();
		curAct._a28._objIndex = in.readSint16BE();
		break;
	case COND_CARRY:         // 29
		curAct._a29._timer = in.readSint16BE();
		curAct._a29._objIndex = in.readSint16BE();
		curAct._a29._actPassIndex = in.readUint16BE();
		curAct._a29._actFailIndex = in.readUint16BE();
		break;
	case INIT_MAZE:          // 30
		curAct._a30._timer = in.readSint16BE();
		curAct._a30._mazeSize = in.readByte();
		curAct._a30._x1 = in.readSint16BE();
		curAct._a30._y1 = in.readSint16BE();
		curAct._a30._x2 = in.readSint16BE();
		curAct._a30._y2 = in.readSint16BE();
		curAct._a30._x3 = in.readSint16BE();
		curAct._a30._x4 = in.readSint16BE();
		curAct._a30._firstScreenIndex = in.readByte();
		break;
	case EXIT_MAZE:          // 31
		curAct._a31._timer = in.readSint16BE();
		break;
	case INIT_PRIORITY:      // 32
		curAct._a32._timer = in.readSint16BE();
		curAct._a32._objIndex = in.readSint16BE();
		curAct._a32._priority = in.readByte();
		break;
	case INIT_SCREEN:        // 33
		curAct._a33._timer = in.readSint16BE();
		curAct._a33._objIndex = in.readSint16BE();
		curAct._a33._screenIndex = in.readSint16BE();
		break;
	case AGSCHEDULE:         // 34
		curAct._a34._timer = in.readSint16BE();
		curAct._a34._actIndex = in.readUint16BE();
		break;
	case REMAPPAL:           // 35
		curAct._a35._timer = in.readSint16BE();
		curAct._a35._oldColorIndex = in.readSint16BE();
		curAct._a35._newColorIndex = in.readSint16BE();
		break;
	case COND_NOUN:          // 36
		curAct._a36._timer = in.readSint16BE();
		curAct._a36._nounIndex = in.readUint16BE();
		curAct._a36._actPassIndex = in.readUint16BE();
		curAct._a36._actFailIndex = in.readUint16BE();
		break;
	case SCREEN_STATE:       // 37
		curAct._a37._timer = in.readSint16BE();
		curAct._a37._screenIndex = in.readSint16BE();
		curAct._a37._newState = in.readByte();
		break;
	case INIT_LIPS:          // 38
		curAct._a38._timer = in.readSint16BE();
		curAct._a38._lipsObjIndex = in.readSint16BE();
		curAct._a38._objIndex = in.readSint16BE();
		curAct._a38._dxLips = in.readByte();
		curAct._a38._dyLips = in.readByte();
		break;
	case INIT_STORY_MODE:    // 39
		curAct._a39._timer = in.readSint16BE();
		curAct._a39._storyModeFl = (in.readByte() == 1);
		break;
	case WARN:               // 40
		curAct._a40._timer = in.readSint16BE();
		curAct._a40._stringIndex = in.readSint16BE();
		break;
	case COND_BONUS:         // 41
		curAct._a41._timer = in.readSint16BE();
		curAct._a41._bonusIndex = in.readSint16BE();
		curAct._a41._actPassIndex = in.readUint16BE();
		curAct._a41._actFailIndex = in.readUint16BE();
		break;
	case TEXT_TAKE:          // 42
		curAct._a42._timer = in.readSint16BE();
		curAct._a42._objIndex = in.readSint16BE();
		break;
	case YESNO:              // 43
		curAct._a43._timer = in.readSint16BE();
		curAct._a43._promptIndex = in.readSint16BE();
		curAct._a43._actYesIndex = in.readUint16BE();
		curAct._a43._actNoIndex = in.readUint16BE();
		break;
	case STOP_ROUTE:         // 44
		curAct._a44._timer = in.readSint16BE();
		break;
	case COND_ROUTE:         // 45
		curAct._a45._timer = in.readSint16BE();
		curAct._a45._routeIndex = in.readSint16BE();
		curAct._a45._actPassIndex = in.readUint16BE();
		curAct._a45._actFailIndex = in.readUint16BE();
		break;
	case INIT_JUMPEXIT:      // 46
		curAct._a46._timer = in.readSint16BE();
		curAct._a46._jumpExitFl = (in.readByte() == 1);
		break;
	case INIT_VIEW:          // 47
		curAct._a47._timer = in.readSint16BE();
		curAct._a47._objIndex = in.readSint16BE();
		curAct._a47._viewx = in.readSint16BE();
		curAct._a47._viewy = in.readSint16BE();
		curAct._a47._direction = in.readSint16BE();
		break;
	case INIT_OBJ_FRAME:     // 48
		curAct._a48._timer = in.readSint16BE();
		curAct._a48._objIndex = in.readSint16BE();
		curAct._a48._seqIndex = in.readSint16BE();
		curAct._a48._frameIndex = in.readSint16BE();
		break;
	case OLD_SONG:           //49
		curAct._a49._timer = in.readSint16BE();
		curAct._a49._songIndex = in.readUint16BE();
		break;
	default:
		error("Engine - Unknown action type encountered: %d", curAct._a0._actType);
	}
}

/**
 * Load actListArr from Hugo.dat
 */
void Scheduler::loadActListArr(Common::ReadStream &in) {
	debugC(6, kDebugSchedule, "loadActListArr(&in)");

	Act tmpAct;

	int numSubElem;
	for (int varnt = 0; varnt < _vm->_numVariant; varnt++) {
		int numElem = in.readUint16BE();
		if (varnt == _vm->_gameVariant) {
			_actListArrSize = numElem;
			_actListArr = (Act **)malloc(sizeof(Act *) * _actListArrSize);
		}

		for (int i = 0; i < numElem; i++) {
			numSubElem = in.readUint16BE();
			if (varnt == _vm->_gameVariant)
				_actListArr[i] = (Act *)malloc(sizeof(Act) * (numSubElem + 1));
			for (int j = 0; j < numSubElem; j++) {
				if (varnt == _vm->_gameVariant) {
					readAct(in, _actListArr[i][j]);
				} else {
					readAct(in, tmpAct);
					if (tmpAct._a0._actType == PROMPT)
						free(tmpAct._a3._responsePtr);
				}
			}

			if (varnt == _vm->_gameVariant)
				_actListArr[i][numSubElem]._a0._actType = ANULL;
		}
	}
}


/**
 * Read _screenActs
 */
void Scheduler::loadScreenAct(Common::SeekableReadStream &in) {
	for (int varnt = 0; varnt < _vm->_numVariant; varnt++) {
		uint16 numElem = in.readUint16BE();

		if (varnt == _vm->_gameVariant) {
			_screenActsSize = numElem;
			_screenActs = (uint16 **)malloc(sizeof(uint16 *) * numElem);
			for (int i = 0; i < numElem; i++) {
				uint16 numSubElem = in.readUint16BE();
				if (numSubElem == 0) {
					_screenActs[i] = nullptr;
				} else {
					_screenActs[i] = (uint16 *)malloc(sizeof(uint16) * numSubElem);
					for (int j = 0; j < numSubElem; j++)
						_screenActs[i][j] = in.readUint16BE();
				}
			}
		} else {
			for (int i = 0; i < numElem; i++) {
				uint16 numSubElem = in.readUint16BE();
				in.skip(numSubElem * sizeof(uint16));
			}
		}
	}
}

void Scheduler::freeScheduler() {
	debugC(6, kDebugSchedule, "freeActListArr()");

	free(_points);
	_points = nullptr;

	if (_screenActs) {
		for (int i = 0; i < _screenActsSize; i++)
			free(_screenActs[i]);
		free(_screenActs);
		_screenActs = nullptr;
		_screenActsSize = 0;
	}

	if (_actListArr) {
		for (int i = 0; i < _actListArrSize; i++) {
			for (int j = 0; _actListArr[i][j]._a0._actType != ANULL; j++) {
				if (_actListArr[i][j]._a0._actType == PROMPT)
					free(_actListArr[i][j]._a3._responsePtr);
			}
			free(_actListArr[i]);
		}
		free(_actListArr);
		_actListArr = nullptr;
		_actListArrSize = 0;
	}
}

/**
 * Add action lists for this screen to event queue
 */
void Scheduler::screenActions(const int screenNum) {
	debugC(1, kDebugEngine, "screenActions(%d)", screenNum);

	uint16 *screenAct = _screenActs[screenNum];
	if (screenAct) {
		for (int i = 0; screenAct[i]; i++)
			insertActionList(screenAct[i]);
	}
}

/**
 * Maze mode is enabled.  Check to see whether hero has crossed the maze
 * bounding box, if so, go to the next room
 */
void Scheduler::processMaze(const int x1, const int x2, const int y1, const int y2) {
	debugC(1, kDebugSchedule, "processMaze");

	if (x1 < _vm->_maze._x1) {
		// Exit west
		_actListArr[_alNewscrIndex][3]._a8._screenIndex = *_vm->_screenPtr - 1;
		_actListArr[_alNewscrIndex][0]._a2._x = _vm->_maze._x2 - kShiftSize - (x2 - x1);
		_actListArr[_alNewscrIndex][0]._a2._y = _vm->_hero->_y;
		_vm->_route->resetRoute();
		insertActionList(_alNewscrIndex);
	} else if (x2 > _vm->_maze._x2) {
		// Exit east
		_actListArr[_alNewscrIndex][3]._a8._screenIndex = *_vm->_screenPtr + 1;
		_actListArr[_alNewscrIndex][0]._a2._x = _vm->_maze._x1 + kShiftSize;
		_actListArr[_alNewscrIndex][0]._a2._y = _vm->_hero->_y;
		_vm->_route->resetRoute();
		insertActionList(_alNewscrIndex);
	} else if (y1 < _vm->_maze._y1 - kShiftSize) {
		// Exit north
		_actListArr[_alNewscrIndex][3]._a8._screenIndex = *_vm->_screenPtr - _vm->_maze._size;
		_actListArr[_alNewscrIndex][0]._a2._x = _vm->_maze._x3;
		_actListArr[_alNewscrIndex][0]._a2._y = _vm->_maze._y2 - kShiftSize - (y2 - y1);
		_vm->_route->resetRoute();
		insertActionList(_alNewscrIndex);
	} else if (y2 > _vm->_maze._y2 - kShiftSize / 2) {
		// Exit south
		_actListArr[_alNewscrIndex][3]._a8._screenIndex = *_vm->_screenPtr + _vm->_maze._size;
		_actListArr[_alNewscrIndex][0]._a2._x = _vm->_maze._x4;
		_actListArr[_alNewscrIndex][0]._a2._y = _vm->_maze._y1 + kShiftSize;
		_vm->_route->resetRoute();
		insertActionList(_alNewscrIndex);
	}
}

/**
 * Write the event queue to the file with handle f
 * Note that we convert all the event structure ptrs to indexes
 * using -1 for NULL.  We can't convert the action ptrs to indexes
 * so we save address of first dummy action ptr to compare on restore.
 */
void Scheduler::saveEvents(Common::WriteStream *f) {
	debugC(1, kDebugSchedule, "saveEvents()");

	f->writeUint32BE(getTicks());

	int16 freeIndex = (_freeEvent == nullptr) ? -1 : _freeEvent - _events;
	int16 headIndex = (_headEvent == nullptr) ? -1 : _headEvent - _events;
	int16 tailIndex = (_tailEvent == nullptr) ? -1 : _tailEvent - _events;

	f->writeSint16BE(freeIndex);
	f->writeSint16BE(headIndex);
	f->writeSint16BE(tailIndex);

	// Convert event ptrs to indexes
	for (int16 i = 0; i < kMaxEvents; i++) {
		Event *wrkEvent = &_events[i];

		// fix up action pointer (to do better)
		int16 index, subElem;
		findAction(wrkEvent->_action, &index, &subElem);
		f->writeSint16BE(index);
		f->writeSint16BE(subElem);
		f->writeByte((wrkEvent->_localActionFl) ? 1 : 0);
		f->writeUint32BE(wrkEvent->_time);
		f->writeSint16BE((wrkEvent->_prevEvent == nullptr) ? -1 : (wrkEvent->_prevEvent - _events));
		f->writeSint16BE((wrkEvent->_nextEvent == nullptr) ? -1 : (wrkEvent->_nextEvent - _events));
	}
}

/**
 * Restore the action data from file with handle f
 */

void Scheduler::restoreActions(Common::ReadStream *f) {
	for (int i = 0; i < _actListArrSize; i++) {
		uint16 numSubElem = f->readUint16BE();
		for (int j = 0; j < numSubElem; j++) {
			readAct(*f, _actListArr[i][j]);
		}
	}
}

int16 Scheduler::calcMaxPoints() const {
	int16 tmpScore = 0;
	for (int i = 0; i < _numBonuses; i++)
		tmpScore += _points[i]._score;
	return tmpScore;
}

/*
* Save the action data in the file with handle f
*/
void Scheduler::saveActions(Common::WriteStream *f) const {
	byte  subElemType;
	int16 nbrCpt;
	uint16 nbrSubElem;

	for (int i = 0; i < _actListArrSize; i++) {
		// write all the sub elems data
		for (nbrSubElem = 1; _actListArr[i][nbrSubElem - 1]._a0._actType != ANULL; nbrSubElem++)
			;

		f->writeUint16BE(nbrSubElem);
		for (int j = 0; j < nbrSubElem; j++) {
			subElemType = _actListArr[i][j]._a0._actType;
			f->writeByte(subElemType);
			switch (subElemType) {
			case ANULL:              // -1
				break;
			case ASCHEDULE:          // 0
				f->writeSint16BE(_actListArr[i][j]._a0._timer);
				f->writeUint16BE(_actListArr[i][j]._a0._actIndex);
				break;
			case START_OBJ:          // 1
				f->writeSint16BE(_actListArr[i][j]._a1._timer);
				f->writeSint16BE(_actListArr[i][j]._a1._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a1._cycleNumb);
				f->writeByte(_actListArr[i][j]._a1._cycle);
				break;
			case INIT_OBJXY:         // 2
				f->writeSint16BE(_actListArr[i][j]._a2._timer);
				f->writeSint16BE(_actListArr[i][j]._a2._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a2._x);
				f->writeSint16BE(_actListArr[i][j]._a2._y);
				break;
			case PROMPT:             // 3
				f->writeSint16BE(_actListArr[i][j]._a3._timer);
				f->writeSint16BE(_actListArr[i][j]._a3._promptIndex);
				for (nbrCpt = 0; _actListArr[i][j]._a3._responsePtr[nbrCpt] != -1; nbrCpt++)
					;
				nbrCpt++;
				f->writeUint16BE(nbrCpt);
				for (int k = 0; k < nbrCpt; k++)
					f->writeSint16BE(_actListArr[i][j]._a3._responsePtr[k]);
				f->writeUint16BE(_actListArr[i][j]._a3._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a3._actFailIndex);
				f->writeByte((_actListArr[i][j]._a3._encodedFl) ? 1 : 0);
				break;
			case BKGD_COLOR:         // 4
				f->writeSint16BE(_actListArr[i][j]._a4._timer);
				f->writeUint32BE(_actListArr[i][j]._a4._newBackgroundColor);
				break;
			case INIT_OBJVXY:        // 5
				f->writeSint16BE(_actListArr[i][j]._a5._timer);
				f->writeSint16BE(_actListArr[i][j]._a5._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a5._vx);
				f->writeSint16BE(_actListArr[i][j]._a5._vy);
				break;
			case INIT_CARRY:         // 6
				f->writeSint16BE(_actListArr[i][j]._a6._timer);
				f->writeSint16BE(_actListArr[i][j]._a6._objIndex);
				f->writeByte((_actListArr[i][j]._a6._carriedFl) ? 1 : 0);
				break;
			case INIT_HF_COORD:      // 7
				f->writeSint16BE(_actListArr[i][j]._a7._timer);
				f->writeSint16BE(_actListArr[i][j]._a7._objIndex);
				break;
			case NEW_SCREEN:         // 8
				f->writeSint16BE(_actListArr[i][j]._a8._timer);
				f->writeSint16BE(_actListArr[i][j]._a8._screenIndex);
				break;
			case INIT_OBJSTATE:      // 9
				f->writeSint16BE(_actListArr[i][j]._a9._timer);
				f->writeSint16BE(_actListArr[i][j]._a9._objIndex);
				f->writeByte(_actListArr[i][j]._a9._newState);
				break;
			case INIT_PATH:          // 10
				f->writeSint16BE(_actListArr[i][j]._a10._timer);
				f->writeSint16BE(_actListArr[i][j]._a10._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a10._newPathType);
				f->writeByte(_actListArr[i][j]._a10._vxPath);
				f->writeByte(_actListArr[i][j]._a10._vyPath);
				break;
			case COND_R:             // 11
				f->writeSint16BE(_actListArr[i][j]._a11._timer);
				f->writeSint16BE(_actListArr[i][j]._a11._objIndex);
				f->writeByte(_actListArr[i][j]._a11._stateReq);
				f->writeUint16BE(_actListArr[i][j]._a11._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a11._actFailIndex);
				break;
			case TEXT:               // 12
				f->writeSint16BE(_actListArr[i][j]._a12._timer);
				f->writeSint16BE(_actListArr[i][j]._a12._stringIndex);
				break;
			case SWAP_IMAGES:        // 13
				f->writeSint16BE(_actListArr[i][j]._a13._timer);
				f->writeSint16BE(_actListArr[i][j]._a13._objIndex1);
				f->writeSint16BE(_actListArr[i][j]._a13._objIndex2);
				break;
			case COND_SCR:           // 14
				f->writeSint16BE(_actListArr[i][j]._a14._timer);
				f->writeSint16BE(_actListArr[i][j]._a14._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a14._screenReq);
				f->writeUint16BE(_actListArr[i][j]._a14._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a14._actFailIndex);
				break;
			case AUTOPILOT:          // 15
				f->writeSint16BE(_actListArr[i][j]._a15._timer);
				f->writeSint16BE(_actListArr[i][j]._a15._objIndex1);
				f->writeSint16BE(_actListArr[i][j]._a15._objIndex2);
				f->writeByte(_actListArr[i][j]._a15._dx);
				f->writeByte(_actListArr[i][j]._a15._dy);
				break;
			case INIT_OBJ_SEQ:       // 16
				f->writeSint16BE(_actListArr[i][j]._a16._timer);
				f->writeSint16BE(_actListArr[i][j]._a16._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a16._seqIndex);
				break;
			case SET_STATE_BITS:     // 17
				f->writeSint16BE(_actListArr[i][j]._a17._timer);
				f->writeSint16BE(_actListArr[i][j]._a17._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a17._stateMask);
				break;
			case CLEAR_STATE_BITS:   // 18
				f->writeSint16BE(_actListArr[i][j]._a18._timer);
				f->writeSint16BE(_actListArr[i][j]._a18._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a18._stateMask);
				break;
			case TEST_STATE_BITS:    // 19
				f->writeSint16BE(_actListArr[i][j]._a19._timer);
				f->writeSint16BE(_actListArr[i][j]._a19._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a19._stateMask);
				f->writeUint16BE(_actListArr[i][j]._a19._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a19._actFailIndex);
				break;
			case DEL_EVENTS:         // 20
				f->writeSint16BE(_actListArr[i][j]._a20._timer);
				f->writeByte(_actListArr[i][j]._a20._actTypeDel);
				break;
			case GAMEOVER:           // 21
				f->writeSint16BE(_actListArr[i][j]._a21._timer);
				break;
			case INIT_HH_COORD:      // 22
				f->writeSint16BE(_actListArr[i][j]._a22._timer);
				f->writeSint16BE(_actListArr[i][j]._a22._objIndex);
				break;
			case EXIT:               // 23
				f->writeSint16BE(_actListArr[i][j]._a23._timer);
				break;
			case BONUS:              // 24
				f->writeSint16BE(_actListArr[i][j]._a24._timer);
				f->writeSint16BE(_actListArr[i][j]._a24._pointIndex);
				break;
			case COND_BOX:           // 25
				f->writeSint16BE(_actListArr[i][j]._a25._timer);
				f->writeSint16BE(_actListArr[i][j]._a25._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a25._x1);
				f->writeSint16BE(_actListArr[i][j]._a25._y1);
				f->writeSint16BE(_actListArr[i][j]._a25._x2);
				f->writeSint16BE(_actListArr[i][j]._a25._y2);
				f->writeUint16BE(_actListArr[i][j]._a25._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a25._actFailIndex);
				break;
			case SOUND:              // 26
				f->writeSint16BE(_actListArr[i][j]._a26._timer);
				f->writeSint16BE(_actListArr[i][j]._a26._soundIndex);
				break;
			case ADD_SCORE:          // 27
				f->writeSint16BE(_actListArr[i][j]._a27._timer);
				f->writeSint16BE(_actListArr[i][j]._a27._objIndex);
				break;
			case SUB_SCORE:          // 28
				f->writeSint16BE(_actListArr[i][j]._a28._timer);
				f->writeSint16BE(_actListArr[i][j]._a28._objIndex);
				break;
			case COND_CARRY:         // 29
				f->writeSint16BE(_actListArr[i][j]._a29._timer);
				f->writeSint16BE(_actListArr[i][j]._a29._objIndex);
				f->writeUint16BE(_actListArr[i][j]._a29._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a29._actFailIndex);
				break;
			case INIT_MAZE:          // 30
				f->writeSint16BE(_actListArr[i][j]._a30._timer);
				f->writeByte(_actListArr[i][j]._a30._mazeSize);
				f->writeSint16BE(_actListArr[i][j]._a30._x1);
				f->writeSint16BE(_actListArr[i][j]._a30._y1);
				f->writeSint16BE(_actListArr[i][j]._a30._x2);
				f->writeSint16BE(_actListArr[i][j]._a30._y2);
				f->writeSint16BE(_actListArr[i][j]._a30._x3);
				f->writeSint16BE(_actListArr[i][j]._a30._x4);
				f->writeByte(_actListArr[i][j]._a30._firstScreenIndex);
				break;
			case EXIT_MAZE:          // 31
				f->writeSint16BE(_actListArr[i][j]._a31._timer);
				break;
			case INIT_PRIORITY:      // 32
				f->writeSint16BE(_actListArr[i][j]._a32._timer);
				f->writeSint16BE(_actListArr[i][j]._a32._objIndex);
				f->writeByte(_actListArr[i][j]._a32._priority);
				break;
			case INIT_SCREEN:        // 33
				f->writeSint16BE(_actListArr[i][j]._a33._timer);
				f->writeSint16BE(_actListArr[i][j]._a33._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a33._screenIndex);
				break;
			case AGSCHEDULE:         // 34
				f->writeSint16BE(_actListArr[i][j]._a34._timer);
				f->writeUint16BE(_actListArr[i][j]._a34._actIndex);
				break;
			case REMAPPAL:           // 35
				f->writeSint16BE(_actListArr[i][j]._a35._timer);
				f->writeSint16BE(_actListArr[i][j]._a35._oldColorIndex);
				f->writeSint16BE(_actListArr[i][j]._a35._newColorIndex);
				break;
			case COND_NOUN:          // 36
				f->writeSint16BE(_actListArr[i][j]._a36._timer);
				f->writeUint16BE(_actListArr[i][j]._a36._nounIndex);
				f->writeUint16BE(_actListArr[i][j]._a36._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a36._actFailIndex);
				break;
			case SCREEN_STATE:       // 37
				f->writeSint16BE(_actListArr[i][j]._a37._timer);
				f->writeSint16BE(_actListArr[i][j]._a37._screenIndex);
				f->writeByte(_actListArr[i][j]._a37._newState);
				break;
			case INIT_LIPS:          // 38
				f->writeSint16BE(_actListArr[i][j]._a38._timer);
				f->writeSint16BE(_actListArr[i][j]._a38._lipsObjIndex);
				f->writeSint16BE(_actListArr[i][j]._a38._objIndex);
				f->writeByte(_actListArr[i][j]._a38._dxLips);
				f->writeByte(_actListArr[i][j]._a38._dyLips);
				break;
			case INIT_STORY_MODE:    // 39
				f->writeSint16BE(_actListArr[i][j]._a39._timer);
				f->writeByte((_actListArr[i][j]._a39._storyModeFl) ? 1 : 0);
				break;
			case WARN:               // 40
				f->writeSint16BE(_actListArr[i][j]._a40._timer);
				f->writeSint16BE(_actListArr[i][j]._a40._stringIndex);
				break;
			case COND_BONUS:         // 41
				f->writeSint16BE(_actListArr[i][j]._a41._timer);
				f->writeSint16BE(_actListArr[i][j]._a41._bonusIndex);
				f->writeUint16BE(_actListArr[i][j]._a41._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a41._actFailIndex);
				break;
			case TEXT_TAKE:          // 42
				f->writeSint16BE(_actListArr[i][j]._a42._timer);
				f->writeSint16BE(_actListArr[i][j]._a42._objIndex);
				break;
			case YESNO:              // 43
				f->writeSint16BE(_actListArr[i][j]._a43._timer);
				f->writeSint16BE(_actListArr[i][j]._a43._promptIndex);
				f->writeUint16BE(_actListArr[i][j]._a43._actYesIndex);
				f->writeUint16BE(_actListArr[i][j]._a43._actNoIndex);
				break;
			case STOP_ROUTE:         // 44
				f->writeSint16BE(_actListArr[i][j]._a44._timer);
				break;
			case COND_ROUTE:         // 45
				f->writeSint16BE(_actListArr[i][j]._a45._timer);
				f->writeSint16BE(_actListArr[i][j]._a45._routeIndex);
				f->writeUint16BE(_actListArr[i][j]._a45._actPassIndex);
				f->writeUint16BE(_actListArr[i][j]._a45._actFailIndex);
				break;
			case INIT_JUMPEXIT:      // 46
				f->writeSint16BE(_actListArr[i][j]._a46._timer);
				f->writeByte((_actListArr[i][j]._a46._jumpExitFl) ? 1 : 0);
				break;
			case INIT_VIEW:          // 47
				f->writeSint16BE(_actListArr[i][j]._a47._timer);
				f->writeSint16BE(_actListArr[i][j]._a47._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a47._viewx);
				f->writeSint16BE(_actListArr[i][j]._a47._viewy);
				f->writeSint16BE(_actListArr[i][j]._a47._direction);
				break;
			case INIT_OBJ_FRAME:     // 48
				f->writeSint16BE(_actListArr[i][j]._a48._timer);
				f->writeSint16BE(_actListArr[i][j]._a48._objIndex);
				f->writeSint16BE(_actListArr[i][j]._a48._seqIndex);
				f->writeSint16BE(_actListArr[i][j]._a48._frameIndex);
				break;
			case OLD_SONG:           // 49, Added by Strangerke for DOS versions
				f->writeSint16BE(_actListArr[i][j]._a49._timer);
				f->writeUint16BE(_actListArr[i][j]._a49._songIndex);
				break;
			default:
				error("Unknown action %d", subElemType);
			}
		}
	}
}

/*
* Find the index in the action list to be able to serialize the action to save game
*/
void Scheduler::findAction(const Act *action, int16 *index, int16 *subElem) {

	assert(index && subElem);
	if (!action) {
		*index = -1;
		*subElem = -1;
		return;
	}

	for (int i = 0; i < _actListArrSize; i++) {
		int j = 0;
		do {
			if (action == &_actListArr[i][j]) {
				*index = i;
				*subElem = j;
				return;
			}
			j++;
		} while (_actListArr[i][j-1]._a0._actType != ANULL);
	}
	// action not found ??
	assert(0);
}

void Scheduler::saveSchedulerData(Common::WriteStream *out) {
	savePoints(out);

	// Now save current time and all current events in event queue
	saveEvents(out);

	// Now save current actions
	saveActions(out);
}

void Scheduler::restoreSchedulerData(Common::ReadStream *in) {
	restorePoints(in);
	_vm->_object->restoreAllSeq();

	// Now restore time of the save and the event queue
	restoreEvents(in);

	// Now restore actions
	restoreActions(in);
}

/**
 * Restore the event list from file with handle f
 */
void Scheduler::restoreEvents(Common::ReadStream *f) {
	debugC(1, kDebugSchedule, "restoreEvents");

	uint32 saveTime = f->readUint32BE();              // time of save
	int16 freeIndex = f->readSint16BE();              // Free list index
	int16 headIndex = f->readSint16BE();              // Head of list index
	int16 tailIndex = f->readSint16BE();              // Tail of list index

	// Restore events indexes to pointers
	for (int i = 0; i < kMaxEvents; i++) {
		int16 index = f->readSint16BE();
		int16 subElem = f->readSint16BE();

		// fix up action pointer (to do better)
		if ((index == -1) && (subElem == -1))
			_events[i]._action = nullptr;
		else
			_events[i]._action = (Act *)&_actListArr[index][subElem];

		_events[i]._localActionFl = (f->readByte() == 1) ? true : false;
		_events[i]._time = f->readUint32BE();

		int16 prevIndex = f->readSint16BE();
		int16 nextIndex = f->readSint16BE();

		_events[i]._prevEvent = (prevIndex == -1) ? nullptr : &_events[prevIndex];
		_events[i]._nextEvent = (nextIndex == -1) ? nullptr : &_events[nextIndex];
	}
	_freeEvent = (freeIndex == -1) ? nullptr : &_events[freeIndex];
	_headEvent = (headIndex == -1) ? nullptr : &_events[headIndex];
	_tailEvent = (tailIndex == -1) ? nullptr : &_events[tailIndex];

	// Adjust times to fit our time
	uint32 curTime = getTicks();
	Event *wrkEvent = _headEvent;                     // The earliest event
	while (wrkEvent) {                                // While mature events found
		wrkEvent->_time = wrkEvent->_time - saveTime + curTime;
		wrkEvent = wrkEvent->_nextEvent;
	}
}

/**
 * Insert the action pointed to by p into the timer event queue
 * The queue goes from head (earliest) to tail (latest) timewise
 */
void Scheduler::insertAction(Act *action) {
	debugC(1, kDebugSchedule, "insertAction() - Action type A%d", action->_a0._actType);

	// First, get and initialize the event structure
	Event *curEvent = getQueue();
	curEvent->_action = action;
	switch (action->_a0._actType) {                   // Assign whether local or global
	case AGSCHEDULE:
		curEvent->_localActionFl = false;               // Lasts over a new screen
		break;
	// Workaround: When dying, switch to storyMode in order to block the keyboard.
	case GAMEOVER:
		_vm->getGameStatus()._storyModeFl = true;
		// fall through
	default:
		curEvent->_localActionFl = true;                // Rest are for current screen only
		break;
	}

	curEvent->_time = action->_a0._timer + getTicks(); // Convert rel to abs time

	// Now find the place to insert the event
	if (!_tailEvent) {                                // Empty queue
		_tailEvent = _headEvent = curEvent;
		curEvent->_nextEvent = curEvent->_prevEvent = nullptr;
	} else {
		Event *wrkEvent = _tailEvent;                   // Search from latest time back
		bool found = false;

		while (wrkEvent && !found) {
			if (wrkEvent->_time <= curEvent->_time) {     // Found if new event later
				found = true;
				if (wrkEvent == _tailEvent)                 // New latest in list
					_tailEvent = curEvent;
				else
					wrkEvent->_nextEvent->_prevEvent = curEvent;
				curEvent->_nextEvent = wrkEvent->_nextEvent;
				wrkEvent->_nextEvent = curEvent;
				curEvent->_prevEvent = wrkEvent;
			}
			wrkEvent = wrkEvent->_prevEvent;
		}

		if (!found) {                                   // Must be earliest in list
			_headEvent->_prevEvent = curEvent;            // So insert as new head
			curEvent->_nextEvent = _headEvent;
			curEvent->_prevEvent = nullptr;
			_headEvent = curEvent;
		}
	}
}

/**
 * This function performs the action in the event structure pointed to by p
 * It dequeues the event and returns it to the free list.  It returns a ptr
 * to the next action in the list, except special case of NEW_SCREEN
 */
Event *Scheduler::doAction(Event *curEvent) {
	debugC(1, kDebugSchedule, "doAction - Event action type : %d", curEvent->_action->_a0._actType);

	Status &gameStatus = _vm->getGameStatus();
	Act    *action = curEvent->_action;
	Object *obj1;
	int     dx, dy;
	switch (action->_a0._actType) {
	case ANULL:                                       // Big NOP from DEL_EVENTS
		break;
	case ASCHEDULE:                                   // act0: Schedule an action list
		insertActionList(action->_a0._actIndex);
		break;
	case START_OBJ:                                   // act1: Start an object cycling
		_vm->_object->_objects[action->_a1._objIndex]._cycleNumb = action->_a1._cycleNumb;
		_vm->_object->_objects[action->_a1._objIndex]._cycling = action->_a1._cycle;
		break;
	case INIT_OBJXY:                                  // act2: Initialize an object
		_vm->_object->_objects[action->_a2._objIndex]._x = action->_a2._x;          // Coordinates
		_vm->_object->_objects[action->_a2._objIndex]._y = action->_a2._y;
		break;
	case PROMPT:                                      // act3: Prompt user for key phrase
		promptAction(action);
		break;
	case BKGD_COLOR:                                  // act4: Set new background color
		_vm->_screen->setBackgroundColor(action->_a4._newBackgroundColor);
		break;
	case INIT_OBJVXY:                                 // act5: Initialize an object velocity
		_vm->_object->setVelocity(action->_a5._objIndex, action->_a5._vx, action->_a5._vy);
		break;
	case INIT_CARRY:                                  // act6: Initialize an object
		_vm->_object->setCarry(action->_a6._objIndex, action->_a6._carriedFl);  // carried status
		break;
	case INIT_HF_COORD:                               // act7: Initialize an object to hero's "feet" coords
		_vm->_object->_objects[action->_a7._objIndex]._x = _vm->_hero->_x - 1;
		_vm->_object->_objects[action->_a7._objIndex]._y = _vm->_hero->_y + _vm->_hero->_currImagePtr->_y2 - 1;
		_vm->_object->_objects[action->_a7._objIndex]._screenIndex = *_vm->_screenPtr;  // Don't forget screen!
		break;
	case NEW_SCREEN:                                  // act8: Start new screen
		newScreen(action->_a8._screenIndex);
		break;
	case INIT_OBJSTATE:                               // act9: Initialize an object state
		_vm->_object->_objects[action->_a9._objIndex]._state = action->_a9._newState;
		break;
	case INIT_PATH:                                   // act10: Initialize an object path and velocity
		_vm->_object->setPath(action->_a10._objIndex, (Path) action->_a10._newPathType, action->_a10._vxPath, action->_a10._vyPath);
		break;
	case COND_R:                                      // act11: action lists conditional on object state
		if (_vm->_object->_objects[action->_a11._objIndex]._state == action->_a11._stateReq)
			insertActionList(action->_a11._actPassIndex);
		else
			insertActionList(action->_a11._actFailIndex);
		break;
	case TEXT:                                        // act12: Text box (CF WARN)
		Utils::notifyBox(_vm->_file->fetchString(action->_a12._stringIndex));   // Fetch string from file
		break;
	case SWAP_IMAGES:                                 // act13: Swap 2 object images
		_vm->_object->swapImages(action->_a13._objIndex1, action->_a13._objIndex2);
		break;
	case COND_SCR:                                    // act14: Conditional on current screen
		if (_vm->_object->_objects[action->_a14._objIndex]._screenIndex == action->_a14._screenReq)
			insertActionList(action->_a14._actPassIndex);
		else
			insertActionList(action->_a14._actFailIndex);
		break;
	case AUTOPILOT:                                   // act15: Home in on a (stationary) object
		_vm->_object->homeIn(action->_a15._objIndex1, action->_a15._objIndex2, action->_a15._dx, action->_a15._dy);
		break;
	case INIT_OBJ_SEQ:                                // act16: Set sequence number to use
		// Note: Don't set a sequence at time 0 of a new screen, it causes
		// problems clearing the boundary bits of the object!  t>0 is safe
		_vm->_object->_objects[action->_a16._objIndex]._currImagePtr = _vm->_object->_objects[action->_a16._objIndex]._seqList[action->_a16._seqIndex]._seqPtr;
		break;
	case SET_STATE_BITS:                              // act17: OR mask with curr obj state
		_vm->_object->_objects[action->_a17._objIndex]._state |= action->_a17._stateMask;
		break;
	case CLEAR_STATE_BITS:                            // act18: AND ~mask with curr obj state
		_vm->_object->_objects[action->_a18._objIndex]._state &= ~action->_a18._stateMask;
		break;
	case TEST_STATE_BITS:                             // act19: If all bits set, do apass else afail
		if ((_vm->_object->_objects[action->_a19._objIndex]._state & action->_a19._stateMask) == action->_a19._stateMask)
			insertActionList(action->_a19._actPassIndex);
		else
			insertActionList(action->_a19._actFailIndex);
		break;
	case DEL_EVENTS:                                  // act20: Remove all events of this action type
		delEventType(action->_a20._actTypeDel);
		break;
	case GAMEOVER:                                    // act21: Game over!
		// NOTE: Must wait at least 1 tick before issuing this action if
		// any objects are to be made invisible!
		gameStatus._gameOverFl = true;
		break;
	case INIT_HH_COORD:                               // act22: Initialize an object to hero's actual coords
		_vm->_object->_objects[action->_a22._objIndex]._x = _vm->_hero->_x;
		_vm->_object->_objects[action->_a22._objIndex]._y = _vm->_hero->_y;
		_vm->_object->_objects[action->_a22._objIndex]._screenIndex = *_vm->_screenPtr;// Don't forget screen!
		break;
	case EXIT:                                        // act23: Exit game back to DOS
		_vm->endGame();
		break;
	case BONUS:                                       // act24: Get bonus score for action
		processBonus(action->_a24._pointIndex);
		break;
	case COND_BOX:                                    // act25: Conditional on bounding box
		obj1 = &_vm->_object->_objects[action->_a25._objIndex];
		dx = obj1->_x + obj1->_currImagePtr->_x1;
		dy = obj1->_y + obj1->_currImagePtr->_y2;
		if ((dx >= action->_a25._x1) && (dx <= action->_a25._x2) &&
		        (dy >= action->_a25._y1) && (dy <= action->_a25._y2))
			insertActionList(action->_a25._actPassIndex);
		else
			insertActionList(action->_a25._actFailIndex);
		break;
	case SOUND:                                       // act26: Play a sound (or tune)
		if (action->_a26._soundIndex < _vm->_tunesNbr)
			_vm->_sound->playMusic(action->_a26._soundIndex);
		else
			_vm->_sound->playSound(action->_a26._soundIndex, kSoundPriorityMedium);
		break;
	case ADD_SCORE:                                   // act27: Add object's value to score
		_vm->adjustScore(_vm->_object->_objects[action->_a27._objIndex]._objValue);
		break;
	case SUB_SCORE:                                   // act28: Subtract object's value from score
		_vm->adjustScore(-_vm->_object->_objects[action->_a28._objIndex]._objValue);
		break;
	case COND_CARRY:                                  // act29: Conditional on object being carried
		if (_vm->_object->isCarried(action->_a29._objIndex))
			insertActionList(action->_a29._actPassIndex);
		else
			insertActionList(action->_a29._actFailIndex);
		break;
	case INIT_MAZE:                                   // act30: Enable and init maze structure
		_vm->_maze._enabledFl = true;
		_vm->_maze._size = action->_a30._mazeSize;
		_vm->_maze._x1 = action->_a30._x1;
		_vm->_maze._y1 = action->_a30._y1;
		_vm->_maze._x2 = action->_a30._x2;
		_vm->_maze._y2 = action->_a30._y2;
		_vm->_maze._x3 = action->_a30._x3;
		_vm->_maze._x4 = action->_a30._x4;
		_vm->_maze._firstScreenIndex = action->_a30._firstScreenIndex;
		break;
	case EXIT_MAZE:                                   // act31: Disable maze mode
		_vm->_maze._enabledFl = false;
		break;
	case INIT_PRIORITY:
		_vm->_object->_objects[action->_a32._objIndex]._priority = action->_a32._priority;
		break;
	case INIT_SCREEN:
		_vm->_object->_objects[action->_a33._objIndex]._screenIndex = action->_a33._screenIndex;
		break;
	case AGSCHEDULE:                                  // act34: Schedule a (global) action list
		insertActionList(action->_a34._actIndex);
		break;
	case REMAPPAL:                                    // act35: Remap a palette color
		_vm->_screen->remapPal(action->_a35._oldColorIndex, action->_a35._newColorIndex);
		break;
	case COND_NOUN:                                   // act36: Conditional on noun mentioned
		if (_vm->_parser->isWordPresent(_vm->_text->getNounArray(action->_a36._nounIndex)))
			insertActionList(action->_a36._actPassIndex);
		else
			insertActionList(action->_a36._actFailIndex);
		break;
	case SCREEN_STATE:                                // act37: Set new screen state
		_vm->_screenStates[action->_a37._screenIndex] = action->_a37._newState;
		break;
	case INIT_LIPS:                                   // act38: Position lips on object
		_vm->_object->_objects[action->_a38._lipsObjIndex]._x = _vm->_object->_objects[action->_a38._objIndex]._x + action->_a38._dxLips;
		_vm->_object->_objects[action->_a38._lipsObjIndex]._y = _vm->_object->_objects[action->_a38._objIndex]._y + action->_a38._dyLips;
		_vm->_object->_objects[action->_a38._lipsObjIndex]._screenIndex = *_vm->_screenPtr; // Don't forget screen!
		_vm->_object->_objects[action->_a38._lipsObjIndex]._cycling = kCycleForward;
		break;
	case INIT_STORY_MODE:                             // act39: Init story_mode flag
		// This is similar to the QUIET path mode, except that it is
		// independant of it and it additionally disables the ">" prompt
		gameStatus._storyModeFl = action->_a39._storyModeFl;
		break;
	case WARN:                                        // act40: Text box (CF TEXT)
		Utils::notifyBox(_vm->_file->fetchString(action->_a40._stringIndex));
		break;
	case COND_BONUS:                                  // act41: Perform action if got bonus
		if (_points[action->_a41._bonusIndex]._scoredFl)
			insertActionList(action->_a41._actPassIndex);
		else
			insertActionList(action->_a41._actFailIndex);
		break;
	case TEXT_TAKE:                                   // act42: Text box with "take" message
		Utils::notifyBox(Common::String::format(TAKE_TEXT, _vm->_text->getNoun(_vm->_object->_objects[action->_a42._objIndex]._nounIndex, TAKE_NAME)));
		break;
	case YESNO:                                       // act43: Prompt user for Yes or No
		if (Utils::yesNoBox(_vm->_file->fetchString(action->_a43._promptIndex)))
			insertActionList(action->_a43._actYesIndex);
		else
			insertActionList(action->_a43._actNoIndex);
		break;
	case STOP_ROUTE:                                  // act44: Stop any route in progress
		_vm->_route->resetRoute();
		break;
	case COND_ROUTE:                                  // act45: Conditional on route in progress
		if (_vm->_route->getRouteIndex() >= action->_a45._routeIndex)
			insertActionList(action->_a45._actPassIndex);
		else
			insertActionList(action->_a45._actFailIndex);
		break;
	case INIT_JUMPEXIT:                               // act46: Init status.jumpexit flag
		// This is to allow left click on exit to get there immediately
		// For example the plane crash in Hugo2 where hero is invisible
		// Couldn't use INVISIBLE flag since conflicts with boat in Hugo1
		_vm->_mouse->setJumpExitFl(action->_a46._jumpExitFl);
		break;
	case INIT_VIEW:                                   // act47: Init object._viewx, viewy, dir
		_vm->_object->_objects[action->_a47._objIndex]._viewx = action->_a47._viewx;
		_vm->_object->_objects[action->_a47._objIndex]._viewy = action->_a47._viewy;
		_vm->_object->_objects[action->_a47._objIndex]._direction = action->_a47._direction;
		break;
	case INIT_OBJ_FRAME:                              // act48: Set seq,frame number to use
		// Note: Don't set a sequence at time 0 of a new screen, it causes
		// problems clearing the boundary bits of the object!  t>0 is safe
		_vm->_object->_objects[action->_a48._objIndex]._currImagePtr = _vm->_object->_objects[action->_a48._objIndex]._seqList[action->_a48._seqIndex]._seqPtr;
		for (dx = 0; dx < action->_a48._frameIndex; dx++)
			_vm->_object->_objects[action->_a48._objIndex]._currImagePtr = _vm->_object->_objects[action->_a48._objIndex]._currImagePtr->_nextSeqPtr;
		break;
	case OLD_SONG:
		// Replaces ACT26 for DOS games.
		_vm->_sound->_DOSSongPtr = _vm->_text->getTextData(action->_a49._songIndex);
		break;
	default:
		error("An error has occurred: %s", "doAction");
		break;
	}

	if (action->_a0._actType == NEW_SCREEN) {         // New_screen() deletes entire list
		return nullptr;                               // nextEvent = nullptr since list now empty
	} else {
		Event *wrkEvent = curEvent->_nextEvent;
		delQueue(curEvent);                             // Return event to free list
		return wrkEvent;                                // Return next event ptr
	}
}

/**
 * Delete an event structure (i.e. return it to the free list)
 * Historical note:  Originally event p was assumed to be at head of queue
 * (i.e. earliest) since all events were deleted in order when proceeding to
 * a new screen.  To delete an event from the middle of the queue, the action
 * was overwritten to be ANULL.  With the advent of GLOBAL events, delQueue
 * was modified to allow deletes anywhere in the list, and the DEL_EVENT
 * action was modified to perform the actual delete.
 */
void Scheduler::delQueue(Event *curEvent) {
	debugC(4, kDebugSchedule, "delQueue()");

	if (curEvent == _headEvent) {                     // If p was the head ptr
		_headEvent = curEvent->_nextEvent;              // then make new head_p
	} else {                                          // Unlink p
		curEvent->_prevEvent->_nextEvent = curEvent->_nextEvent;
		if (curEvent->_nextEvent)
			curEvent->_nextEvent->_prevEvent = curEvent->_prevEvent;
		else
			_tailEvent = curEvent->_prevEvent;
	}

	if (_headEvent)
		_headEvent->_prevEvent = nullptr;             // Mark end of list
	else
		_tailEvent = nullptr;                         // Empty queue

	curEvent->_nextEvent = _freeEvent;                // Return p to free list
	if (_freeEvent)                                   // Special case, if free list was empty
		_freeEvent->_prevEvent = curEvent;
	_freeEvent = curEvent;
}

/**
 * Delete all the active events of a given type
 */
void Scheduler::delEventType(const Action _actTypeDel) {
	// Note: actions are not deleted here, simply turned into NOPs!
	Event *wrkEvent = _headEvent;                     // The earliest event
	Event *saveEvent;

	while (wrkEvent) {                                // While events found in list
		saveEvent = wrkEvent->_nextEvent;
		if (wrkEvent->_action->_a20._actType == _actTypeDel)
			delQueue(wrkEvent);
		wrkEvent = saveEvent;
	}
}

/**
 * Save the points table
 */
void Scheduler::savePoints(Common::WriteStream *out) const {
	for (int i = 0; i < _numBonuses; i++) {
		out->writeByte(_points[i]._score);
		out->writeByte((_points[i]._scoredFl) ? 1 : 0);
	}
}

/**
 * Restore the points table
 */
void Scheduler::restorePoints(Common::ReadStream *in) {
	// Restore points table
	for (int i = 0; i < _numBonuses; i++) {
		_points[i]._score = in->readByte();
		_points[i]._scoredFl = (in->readByte() == 1);
	}
}

Scheduler_v1d::Scheduler_v1d(HugoEngine *vm) : Scheduler(vm) {
}

Scheduler_v1d::~Scheduler_v1d() {
}

const char *Scheduler_v1d::getCypher() const {
	return "Copyright (c) 1990, Gray Design Associates";
}

uint32 Scheduler_v1d::getTicks() {
	return getDosTicks(false);
}

/**
 * This is the scheduler which runs every tick.  It examines the event queue
 * for any events whose time has come.  It dequeues these events and performs
 * the action associated with the event, returning it to the free queue
 */
void Scheduler_v1d::runScheduler() {
	debugC(6, kDebugSchedule, "runScheduler");

	uint32 ticker = getTicks();                       // The time now, in ticks
	Event *curEvent = _headEvent;                     // The earliest event

	while (curEvent && (curEvent->_time <= ticker))   // While mature events found
		curEvent = doAction(curEvent);                  // Perform the action (returns nextEvent)
}

void Scheduler_v1d::promptAction(Act *action) {
	Common::String response;

	response = Utils::promptBox(_vm->_file->fetchString(action->_a3._promptIndex));

	response.toLowercase();

	char resp[256];
	Common::strlcpy(resp, response.c_str(), 256);

	if (action->_a3._encodedFl)
		decodeString(resp);

	if (strstr(resp, _vm->_file->fetchString(action->_a3._responsePtr[0])))
		insertActionList(action->_a3._actPassIndex);
	else
		insertActionList(action->_a3._actFailIndex);
}

/**
 * Decode a response to a prompt
 */
void Scheduler_v1d::decodeString(char *line) {
	debugC(1, kDebugSchedule, "decodeString(%s)", line);

	uint16 linelength = strlen(line);
	for (uint16 i = 0; i < linelength; i++) {
		line[i] = (line[i] + _cypher.c_str()[i % _cypher.size()]) % '~';
		if (line[i] < ' ')
			line[i] += ' ';
	}
}

Scheduler_v2d::Scheduler_v2d(HugoEngine *vm) : Scheduler_v1d(vm) {
}

Scheduler_v2d::~Scheduler_v2d() {
}

const char *Scheduler_v2d::getCypher() const {
	return "Copyright 1991, Gray Design Associates";
}

void Scheduler_v2d::promptAction(Act *action) {
	Common::String response;

	response = Utils::promptBox(_vm->_file->fetchString(action->_a3._promptIndex));
	response.toLowercase();

	debug(1, "doAction(act3), expecting answer %s", _vm->_file->fetchString(action->_a3._responsePtr[0]));

	bool  found = false;

	for (int dx = 0; !found && (action->_a3._responsePtr[dx] != -1); dx++) {
		const char *tmpStr = _vm->_file->fetchString(action->_a3._responsePtr[dx]);
		if (response.contains(tmpStr))
			found = true;
	}

	if (found)
		insertActionList(action->_a3._actPassIndex);
	else
		insertActionList(action->_a3._actFailIndex);
}

/**
 * Decode a string
 */
void Scheduler_v2d::decodeString(char *line) {
	debugC(1, kDebugSchedule, "decodeString(%s)", line);

	int16 lineLength = strlen(line);
	for (uint16 i = 0; i < lineLength; i++)
		line[i] -= _cypher.c_str()[i % _cypher.size()];

	debugC(1, kDebugSchedule, "result : %s", line);
}

Scheduler_v3d::Scheduler_v3d(HugoEngine *vm) : Scheduler_v2d(vm) {
}

Scheduler_v3d::~Scheduler_v3d() {
}

const char *Scheduler_v3d::getCypher() const {
	return "Copyright 1992, Gray Design Associates";
}

Scheduler_v1w::Scheduler_v1w(HugoEngine *vm) : Scheduler_v3d(vm) {
}

Scheduler_v1w::~Scheduler_v1w() {
}

uint32 Scheduler_v1w::getTicks() {
	return getWinTicks();
}

/**
 * This is the scheduler which runs every tick.  It examines the event queue
 * for any events whose time has come.  It dequeues these events and performs
 * the action associated with the event, returning it to the free queue
 */
void Scheduler_v1w::runScheduler() {
	debugC(6, kDebugSchedule, "runScheduler");

	uint32 ticker = getTicks();                       // The time now, in ticks
	Event *curEvent = _headEvent;                     // The earliest event

	while (curEvent && (curEvent->_time <= ticker))   // While mature events found
		curEvent = doAction(curEvent);                  // Perform the action (returns nextEvent)

	_vm->getGameStatus()._tick++;                     // Accessed elsewhere via getTicks()
}
} // End of namespace Hugo