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
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
|
/* 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.
*
*/
/*
* Based on the Reverse Engineering work of Christophe Fontanel,
* maintainer of the Dungeon Master Encyclopaedia (http://dmweb.free.fr/)
*/
#include "dm/group.h"
#include "dm/dungeonman.h"
#include "dm/champion.h"
#include "dm/movesens.h"
#include "dm/projexpl.h"
#include "dm/timeline.h"
#include "dm/objectman.h"
#include "dm/menus.h"
#include "dm/sounds.h"
namespace DM {
int32 GroupMan::setTime(int32 &map_time, int32 time) {
return map_time = (map_time & 0xFF000000) | time;
}
GroupMan::GroupMan(DMEngine *vm) : _vm(vm) {
for (uint16 i = 0; i < 4; ++i)
_dropMovingCreatureFixedPossessionsCell[i] = 0;
_dropMovingCreatureFixedPossCellCount = 0;
_fluxCageCount = 0;
for (uint16 i = 0; i < 4; ++i)
_fluxCages[i] = 0;
_currentGroupMapX = 0;
_currentGroupMapY = 0;
_currGroupThing = Thing(0);
for (uint16 i = 0; i < 4; ++i)
_groupMovementTestedDirections[i] = 0;
_currGroupDistanceToParty = 0;
_currGroupPrimaryDirToParty = 0;
_currGroupSecondaryDirToParty = 0;
_groupMovementBlockedByGroupThing = Thing(0);
_groupMovementBlockedByDoor = false;
_groupMovementBlockedByParty = false;
_groupMovBlockedByWallStairsPitFakeWalFluxCageTeleporter = false;
_maxActiveGroupCount = 60;
_activeGroups = nullptr;
_currActiveGroupCount = 0;
twoHalfSquareSizedCreaturesGroupLastDirectionSetTime = 0;
}
GroupMan::~GroupMan() {
delete[] _activeGroups;
}
uint16 GroupMan::toggleFlag(uint16& val, uint16 mask) {
return val ^= mask;
}
void GroupMan::initActiveGroups() {
if (_vm->_gameMode != kDMModeLoadSavedGame)
_maxActiveGroupCount = 60;
if (_activeGroups)
delete[] _activeGroups;
_activeGroups = new ActiveGroup[_maxActiveGroupCount];
for (uint16 i = 0; i < _maxActiveGroupCount; ++i)
_activeGroups[i]._groupThingIndex = -1;
}
uint16 GroupMan::getGroupCells(Group *group, int16 mapIndex) {
byte cells = group->_cells;
if (mapIndex == _vm->_dungeonMan->_partyMapIndex)
cells = _activeGroups[cells]._cells;
return cells;
}
uint16 GroupMan::getGroupDirections(Group *group, int16 mapIndex) {
static byte groupDirections[4] = {0x00, 0x55, 0xAA, 0xFF}; // @ G0258_auc_Graphic559_GroupDirections
if (mapIndex == _vm->_dungeonMan->_partyMapIndex)
return _activeGroups[group->getActiveGroupIndex()]._directions;
return groupDirections[group->getDir()];
}
int16 GroupMan::getCreatureOrdinalInCell(Group *group, uint16 cell) {
uint16 currMapIndex = _vm->_dungeonMan->_currMapIndex;
byte groupCells = getGroupCells(group, currMapIndex);
if (groupCells == kDMCreatureTypeSingleCenteredCreature)
return _vm->indexToOrdinal(0);
int retval = 0;
byte creatureIndex = group->getCount();
if (getFlag(_vm->_dungeonMan->_creatureInfos[group->_type]._attributes, kDMCreatureMaskSize) == kDMCreatureSizeHalf) {
if ((getGroupDirections(group, currMapIndex) & 1) == (cell & 1))
cell = _vm->turnDirLeft(cell);
do {
byte creatureCell = getCreatureValue(groupCells, creatureIndex);
if (creatureCell == cell || creatureCell == _vm->turnDirRight(cell)) {
retval = _vm->indexToOrdinal(creatureIndex);
break;
}
} while (creatureIndex--);
} else {
do {
if (getCreatureValue(groupCells, creatureIndex) == cell) {
retval = _vm->indexToOrdinal(creatureIndex);
break;
}
} while (creatureIndex--);
}
return retval;
}
uint16 GroupMan::getCreatureValue(uint16 groupVal, uint16 creatureIndex) {
return (groupVal >> (creatureIndex << 1)) & 0x3;
}
void GroupMan::dropGroupPossessions(int16 mapX, int16 mapY, Thing groupThing, SoundMode soundMode) {
Group *group = (Group *)_vm->_dungeonMan->getThingData(groupThing);
CreatureType creatureType = group->_type;
if ((soundMode != kDMSoundModeDoNotPlaySound) && getFlag(_vm->_dungeonMan->_creatureInfos[creatureType]._attributes, kDMCreatureMaskDropFixedPoss)) {
int16 creatureIndex = group->getCount();
uint16 groupCells = getGroupCells(group, _vm->_dungeonMan->_currMapIndex);
do {
dropCreatureFixedPossessions(creatureType, mapX, mapY,
(groupCells == kDMCreatureTypeSingleCenteredCreature) ? (uint16)kDMCreatureTypeSingleCenteredCreature : getCreatureValue(groupCells, creatureIndex), soundMode);
} while (creatureIndex--);
}
Thing currentThing = group->_slot;
if ((currentThing) != Thing::_endOfList) {
bool weaponDropped = false;
Thing nextThing;
do {
nextThing = _vm->_dungeonMan->getNextThing(currentThing);
currentThing = _vm->thingWithNewCell(currentThing, _vm->getRandomNumber(4));
if ((currentThing).getType() == kDMThingTypeWeapon) {
weaponDropped = true;
}
_vm->_moveSens->getMoveResult(currentThing, kDMMapXNotOnASquare, 0, mapX, mapY);
} while ((currentThing = nextThing) != Thing::_endOfList);
if (soundMode != kDMSoundModeDoNotPlaySound)
_vm->_sound->requestPlay(weaponDropped ? kDMSoundIndexMetallicThud : kDMSoundIndexWoodenThudAttackTrolinAntmanStoneGolem, mapX, mapY, soundMode);
}
}
void GroupMan::dropCreatureFixedPossessions(CreatureType creatureType, int16 mapX, int16 mapY, uint16 cell, SoundMode soundMode) {
static uint16 fixedPossessionCreature12Skeleton[3] = { // @ G0245_aui_Graphic559_FixedPossessionsCreature12Skeleton
kDMObjectInfoIndexFirstWeapon + kDMWeaponFalchion,
kDMObjectInfoIndexFirstArmour + kDMArmourWoodenShield,
0}
;
static uint16 fixedPossessionCreature9StoneGolem[2] = { // @ G0246_aui_Graphic559_FixedPossessionsCreature09StoneGolem
kDMObjectInfoIndexFirstWeapon + kDMWeaponStoneClub,
0
};
static uint16 fixedPossessionCreatur16TrolinAntman[2] = { // @ G0247_aui_Graphic559_FixedPossessionsCreature16Trolin_Antman
kDMObjectInfoIndexFirstWeapon + kDMWeaponClub,
0
};
static uint16 fixedPossessionCreature18AnimatedArmourDethKnight[7] = { // @ G0248_aui_Graphic559_FixedPossessionsCreature18AnimatedArmour_DethKnight
kDMObjectInfoIndexFirstArmour + kDMArmourFootPlate,
kDMObjectInfoIndexFirstArmour + kDMArmourLegPlate,
kDMObjectInfoIndexFirstArmour + kDMArmourTorsoPlate,
kDMObjectInfoIndexFirstWeapon + kDMWeaponSword,
kDMObjectInfoIndexFirstArmour + kDMArmourArmet,
kDMObjectInfoIndexFirstWeapon + kDMWeaponSword,
0
};
static uint16 fixedPossessionCreature7rockRockPile[5] = { // @ G0249_aui_Graphic559_FixedPossessionsCreature07Rock_RockPile
kDMObjectInfoIndexFirstJunk + kDMJunkTypeBoulder,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeBoulder) | kDMMaskRandomDrop,
(kDMObjectInfoIndexFirstWeapon + kDMWeaponRock) | kDMMaskRandomDrop,
(kDMObjectInfoIndexFirstWeapon + kDMWeaponRock) | kDMMaskRandomDrop,
0
};
static uint16 fixedPossessionCreature4PainRatHellHound[3] = { // @ G0250_aui_Graphic559_FixedPossessionsCreature04PainRat_Hellhound
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDrumstickShank,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeDrumstickShank) | kDMMaskRandomDrop,
0
};
static uint16 fixedPossessionCreature6screamer[3] = { // @ G0251_aui_Graphic559_FixedPossessionsCreature06Screamer
kDMObjectInfoIndexFirstJunk + kDMJunkTypeScreamerSlice,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeScreamerSlice) | kDMMaskRandomDrop,
0
};
static uint16 fixedPossessionCreature15MagnetaWormWorm[4] = { // @ G0252_aui_Graphic559_FixedPossessionsCreature15MagentaWorm_Worm
kDMObjectInfoIndexFirstJunk + kDMJunkTypeWormRound,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeWormRound) | kDMMaskRandomDrop,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeWormRound) | kDMMaskRandomDrop,
0
};
static uint16 fixedPossessionCreature24RedDragon[11] = { // @ G0253_aui_Graphic559_FixedPossessionsCreature24RedDragon
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak) | kDMMaskRandomDrop,
(kDMObjectInfoIndexFirstJunk + kDMJunkTypeDragonSteak) | kDMMaskRandomDrop,
0
};
uint16 *fixedPossessions;
bool cursedPossessions = false;
switch (creatureType) {
case kDMCreatureTypePainRat:
fixedPossessions = fixedPossessionCreature4PainRatHellHound;
break;
case kDMCreatureTypeScreamer:
fixedPossessions = fixedPossessionCreature6screamer;
break;
case kDMCreatureTypeRockpile:
fixedPossessions = fixedPossessionCreature7rockRockPile;
break;
case kDMCreatureTypeStoneGolem:
fixedPossessions = fixedPossessionCreature9StoneGolem;
break;
case kDMCreatureTypeSkeleton:
fixedPossessions = fixedPossessionCreature12Skeleton;
break;
case kDMCreatureTypeAntman:
fixedPossessions = fixedPossessionCreatur16TrolinAntman;
break;
case kDMCreatureTypeMagentaWorm:
fixedPossessions = fixedPossessionCreature15MagnetaWormWorm;
break;
case kDMCreatureTypeAnimatedArmour:
cursedPossessions = true;
fixedPossessions = fixedPossessionCreature18AnimatedArmourDethKnight;
break;
case kDMCreatureTypeRedDragon:
fixedPossessions = fixedPossessionCreature24RedDragon;
break;
default:
return;
}
uint16 currFixedPossession = *fixedPossessions++;
bool weaponDropped = false;
while (currFixedPossession) {
if (getFlag(currFixedPossession, kDMMaskRandomDrop) && _vm->getRandomNumber(2))
continue;
int16 currThingType;
if (clearFlag(currFixedPossession, kDMMaskRandomDrop) >= kDMObjectInfoIndexFirstJunk) {
currThingType = kDMThingTypeJunk;
currFixedPossession -= kDMObjectInfoIndexFirstJunk;
} else if (currFixedPossession >= kDMObjectInfoIndexFirstArmour) {
currThingType = kDMThingTypeArmour;
currFixedPossession -= kDMObjectInfoIndexFirstArmour;
} else {
weaponDropped = true;
currThingType = kDMThingTypeWeapon;
currFixedPossession -= kDMObjectInfoIndexFirstWeapon;
}
Thing nextUnusedThing = _vm->_dungeonMan->getUnusedThing(currThingType);
if ((nextUnusedThing) == Thing::_none)
continue;
Weapon *currWeapon = (Weapon *)_vm->_dungeonMan->getThingData(nextUnusedThing);
/* The same pointer type is used no matter the actual type k5_WeaponThingType, k6_ArmourThingType or k10_JunkThingType */
currWeapon->setType(currFixedPossession);
currWeapon->setCursed(cursedPossessions);
nextUnusedThing = _vm->thingWithNewCell(nextUnusedThing, ((cell == kDMCreatureTypeSingleCenteredCreature) || !_vm->getRandomNumber(4)) ? _vm->getRandomNumber(4) : cell);
_vm->_moveSens->getMoveResult(nextUnusedThing, kDMMapXNotOnASquare, 0, mapX, mapY);
currFixedPossession = *fixedPossessions++;
}
_vm->_sound->requestPlay(weaponDropped ? kDMSoundIndexMetallicThud : kDMSoundIndexWoodenThudAttackTrolinAntmanStoneGolem, mapX, mapY, soundMode);
}
int16 GroupMan::getDirsWhereDestIsVisibleFromSource(int16 srcMapX, int16 srcMapY, int16 destMapX, int16 destMapY) {
if (srcMapX == destMapX) {
_vm->_projexpl->_secondaryDirToOrFromParty = (_vm->getRandomNumber(65536) & 0x0002) + 1; /* Resulting direction may be 1 or 3 (East or West) */
if (srcMapY > destMapY)
return kDMDirNorth;
return kDMDirSouth;
}
if (srcMapY == destMapY) {
_vm->_projexpl->_secondaryDirToOrFromParty = (_vm->getRandomNumber(65536) & 0x0002) + 0; /* Resulting direction may be 0 or 2 (North or South) */
if (srcMapX > destMapX)
return kDMDirWest;
return kDMDirEast;
}
int16 curDirection = kDMDirNorth;
for (;;) {
if (isDestVisibleFromSource(curDirection, srcMapX, srcMapY, destMapX, destMapY)) {
_vm->_projexpl->_secondaryDirToOrFromParty = _vm->turnDirRight(curDirection);
if (!isDestVisibleFromSource(_vm->_projexpl->_secondaryDirToOrFromParty, srcMapX, srcMapY, destMapX, destMapY)) {
_vm->_projexpl->_secondaryDirToOrFromParty = _vm->turnDirLeft(curDirection);
if ((curDirection != kDMDirNorth) || !isDestVisibleFromSource(_vm->_projexpl->_secondaryDirToOrFromParty, srcMapX, srcMapY, destMapX, destMapY)) {
_vm->_projexpl->_secondaryDirToOrFromParty = _vm->turnDirRight((_vm->getRandomNumber(65536) & 0x0002) + curDirection);
return curDirection;
}
}
if (_vm->getRandomNumber(2)) {
int16 primaryDirection = _vm->_projexpl->_secondaryDirToOrFromParty;
_vm->_projexpl->_secondaryDirToOrFromParty = curDirection;
return primaryDirection;
}
return curDirection;
}
curDirection++;
}
}
bool GroupMan::isDestVisibleFromSource(uint16 dir, int16 srcMapX, int16 srcMapY, int16 destMapX, int16 destMapY) {
switch (dir) { /* If direction is not 'West' then swap variables so that the same test as for west can be applied */
case kDMDirSouth:
SWAP(srcMapX, destMapY);
SWAP(destMapX, srcMapY);
break;
case kDMDirEast:
SWAP(srcMapX, destMapX);
SWAP(destMapY, srcMapY);
break;
case kDMDirNorth:
SWAP(srcMapX, srcMapY);
SWAP(destMapX, destMapY);
break;
}
return ((srcMapX -= (destMapX - 1)) > 0) && ((((srcMapY -= destMapY) < 0) ? -srcMapY : srcMapY) <= srcMapX);
}
bool GroupMan::groupIsDoorDestoryedByAttack(uint16 mapX, uint16 mapY, int16 attack, bool magicAttack, int16 ticks) {
Door *curDoor = (Door *)_vm->_dungeonMan->getSquareFirstThingData(mapX, mapY);
if ((magicAttack && !curDoor->isMagicDestructible()) || (!magicAttack && !curDoor->isMeleeDestructible()))
return false;
if (attack >= _vm->_dungeonMan->_currMapDoorInfo[curDoor->getType()]._defense) {
byte *curSquare = &_vm->_dungeonMan->_currMapData[mapX][mapY];
if (Square(*curSquare).getDoorState() == kDMDoorStateClosed) {
if (ticks) {
TimelineEvent newEvent;
newEvent._mapTime = _vm->setMapAndTime(_vm->_dungeonMan->_currMapIndex, _vm->_gameTime + ticks);
newEvent._type = k2_TMEventTypeDoorDestruction;
newEvent._priority = 0;
newEvent._Bu._location._mapX = mapX;
newEvent._Bu._location._mapY = mapY;
_vm->_timeline->addEventGetEventIndex(&newEvent);
} else {
((Square *)curSquare)->setDoorState(kDMDoorStateDestroyed);
}
return true;
}
}
return false;
}
Thing GroupMan::groupGetThing(int16 mapX, int16 mapY) {
Thing curThing = _vm->_dungeonMan->getSquareFirstThing(mapX, mapY);
while ((curThing != Thing::_endOfList) && (curThing.getType() != kDMThingTypeGroup))
curThing = _vm->_dungeonMan->getNextThing(curThing);
return curThing;
}
int16 GroupMan::groupGetDamageCreatureOutcome(Group *group, uint16 creatureIndex, int16 mapX, int16 mapY, int16 damage, bool notMoving) {
CreatureType creatureType = group->_type;
CreatureInfo *creatureInfo = &_vm->_dungeonMan->_creatureInfos[creatureType];
if (getFlag(creatureInfo->_attributes, kDMCreatureMaskArchenemy)) /* Lord Chaos cannot be damaged */
return kDMKillOutcomeNoCreaturesInGroup;
if (group->_health[creatureIndex] <= damage) {
uint16 groupCells = getGroupCells(group, _vm->_dungeonMan->_currMapIndex);
uint16 cell = (groupCells == kDMCreatureTypeSingleCenteredCreature) ? (uint16)kDMCreatureTypeSingleCenteredCreature : getCreatureValue(groupCells, creatureIndex);
uint16 creatureCount = group->getCount();
uint16 retVal;
if (!creatureCount) { /* If there is a single creature in the group */
if (notMoving) {
dropGroupPossessions(mapX, mapY, groupGetThing(mapX, mapY), kDMSoundModePlayOneTickLater);
groupDelete(mapX, mapY);
}
retVal = kDMKillOutcomeAllCreaturesInGroup;
} else { /* If there are several creatures in the group */
uint16 groupDirections = getGroupDirections(group, _vm->_dungeonMan->_currMapIndex);
if (getFlag(creatureInfo->_attributes, kDMCreatureMaskDropFixedPoss)) {
if (notMoving)
dropCreatureFixedPossessions(creatureType, mapX, mapY, cell, kDMSoundModePlayOneTickLater);
else
_dropMovingCreatureFixedPossessionsCell[_dropMovingCreatureFixedPossCellCount++] = cell;
}
bool currentMapIsPartyMap = (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex);
ActiveGroup *activeGroup = nullptr;
if (currentMapIsPartyMap)
activeGroup = &_activeGroups[group->getActiveGroupIndex()];
if (group->getBehaviour() == kDMBehaviorAttack) {
TimelineEvent *curEvent = _vm->_timeline->_events;
for (uint16 eventIndex = 0; eventIndex < _vm->_timeline->_eventMaxCount; eventIndex++) {
uint16 curEventType = curEvent->_type;
if ((_vm->getMap(curEvent->_mapTime) == _vm->_dungeonMan->_currMapIndex) &&
(curEvent->_Bu._location._mapX == mapX) &&
(curEvent->_Bu._location._mapY == mapY) &&
(curEventType > k32_TMEventTypeUpdateAspectGroup) &&
(curEventType < k41_TMEventTypeUpdateBehaviour_3 + 1)) {
uint16 nextCreatureIndex;
if (curEventType < k37_TMEventTypeUpdateBehaviourGroup)
nextCreatureIndex = curEventType - k33_TMEventTypeUpdateAspectCreature_0; /* Get creature index for events 33 to 36 */
else
nextCreatureIndex = curEventType - k38_TMEventTypeUpdateBehaviour_0; /* Get creature index for events 38 to 41 */
if (nextCreatureIndex == creatureIndex)
_vm->_timeline->deleteEvent(eventIndex);
else if (nextCreatureIndex > creatureIndex) {
curEvent->_type -= 1;
_vm->_timeline->fixChronology(_vm->_timeline->getIndex(eventIndex));
}
}
curEvent++;
}
uint16 fearResistance = creatureInfo->getFearResistance();
if (currentMapIsPartyMap && (fearResistance != kDMImmuneToFear)) {
fearResistance += creatureCount - 1;
if (fearResistance < _vm->getRandomNumber(16)) { /* Test if the death of a creature frightens the remaining creatures in the group */
activeGroup->_delayFleeingFromTarget = _vm->getRandomNumber(100 - (fearResistance << 2)) + 20;
group->setBehaviour(kDMBehaviorFlee);
}
}
}
uint16 nextCreatureIndex = creatureIndex;
for (uint16 curCreatureIndex = creatureIndex; curCreatureIndex < creatureCount; curCreatureIndex++) {
nextCreatureIndex++;
group->_health[curCreatureIndex] = group->_health[nextCreatureIndex];
groupDirections = getGroupValueUpdatedWithCreatureValue(groupDirections, curCreatureIndex, getCreatureValue(groupDirections, nextCreatureIndex));
groupCells = getGroupValueUpdatedWithCreatureValue(groupCells, curCreatureIndex, getCreatureValue(groupCells, nextCreatureIndex));
if (currentMapIsPartyMap)
activeGroup->_aspect[curCreatureIndex] = activeGroup->_aspect[nextCreatureIndex];
}
groupCells &= 0x003F;
_vm->_dungeonMan->setGroupCells(group, groupCells, _vm->_dungeonMan->_currMapIndex);
_vm->_dungeonMan->setGroupDirections(group, groupDirections, _vm->_dungeonMan->_currMapIndex);
group->setCount(group->getCount() - 1);
retVal = kDMKillOutcomeSomeCreaturesInGroup;
}
CreatureSize creatureSize = (CreatureSize)getFlag(creatureInfo->_attributes, kDMCreatureMaskSize);
uint16 attack;
if (creatureSize == kDMCreatureSizeQuarter)
attack = 110;
else if (creatureSize == kDMCreatureSizeHalf)
attack = 190;
else
attack = 255;
_vm->_projexpl->createExplosion(Thing::_explSmoke, attack, mapX, mapY, cell); /* BUG0_66 Smoke is placed on the source map instead of the destination map when a creature dies by falling through a pit. The game has a special case to correctly drop the creature possessions on the destination map but there is no such special case for the smoke. Note that the death must be caused by the damage of the fall (there is no smoke if the creature is removed because its type is not allowed on the destination map). However this bug has no visible consequence because of BUG0_26: the smoke explosion falls in the pit right after being placed in the dungeon and before being drawn on screen so it is only visible on the destination square */
return retVal;
}
if (damage > 0)
group->_health[creatureIndex] -= damage;
return kDMKillOutcomeNoCreaturesInGroup;
}
void GroupMan::groupDelete(int16 mapX, int16 mapY) {
Thing groupThing = groupGetThing(mapX, mapY);
if (groupThing == Thing::_endOfList)
return;
Group *group = (Group *)_vm->_dungeonMan->getThingData(groupThing);
for (uint16 i = 0; i < 4; ++i)
group->_health[i] = 0;
_vm->_moveSens->getMoveResult(groupThing, mapX, mapY, kDMMapXNotOnASquare, 0);
group->_nextThing = Thing::_none;
if (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex) {
_activeGroups[group->getActiveGroupIndex()]._groupThingIndex = -1;
_currActiveGroupCount--;
}
groupDeleteEvents(mapX, mapY);
}
void GroupMan::groupDeleteEvents(int16 mapX, int16 mapY) {
TimelineEvent *curEvent = _vm->_timeline->_events;
for (int16 eventIndex = 0; eventIndex < _vm->_timeline->_eventMaxCount; eventIndex++) {
uint16 curEventType = curEvent->_type;
if ((_vm->getMap(curEvent->_mapTime) == _vm->_dungeonMan->_currMapIndex) &&
(curEventType > k29_TMEventTypeGroupReactionDangerOnSquare - 1) && (curEventType < k41_TMEventTypeUpdateBehaviour_3 + 1) &&
(curEvent->_Bu._location._mapX == mapX) && (curEvent->_Bu._location._mapY == mapY)) {
_vm->_timeline->deleteEvent(eventIndex);
}
curEvent++;
}
}
uint16 GroupMan::getGroupValueUpdatedWithCreatureValue(uint16 groupVal, uint16 creatureIndex, uint16 creatureVal) {
creatureVal &= 0x0003;
creatureIndex <<= 1;
creatureVal <<= creatureIndex;
return creatureVal | (groupVal & ~(3 << creatureVal));
}
int16 GroupMan::getDamageAllCreaturesOutcome(Group *group, int16 mapX, int16 mapY, int16 attack, bool notMoving) {
bool killedSomeCreatures = false;
bool killedAllCreatures = true;
_dropMovingCreatureFixedPossCellCount = 0;
if (attack > 0) {
int16 creatureIndex = group->getCount();
uint16 randomAttackSeed = (attack >> 3) + 1;
attack -= randomAttackSeed;
randomAttackSeed <<= 1;
do {
int16 outcomeVal = groupGetDamageCreatureOutcome(group, creatureIndex, mapX, mapY, attack + _vm->getRandomNumber(randomAttackSeed), notMoving);
killedAllCreatures = outcomeVal && killedAllCreatures;
killedSomeCreatures = killedSomeCreatures || outcomeVal;
} while (creatureIndex--);
if (killedAllCreatures)
return kDMKillOutcomeAllCreaturesInGroup;
if (killedSomeCreatures)
return kDMKillOutcomeSomeCreaturesInGroup;
}
return kDMKillOutcomeNoCreaturesInGroup;
}
int16 GroupMan::groupGetResistanceAdjustedPoisonAttack(CreatureType creatureType, int16 poisonAttack) {
int16 poisonResistance = _vm->_dungeonMan->_creatureInfos[creatureType].getPoisonResistance();
if (!poisonAttack || (poisonResistance == kDMImmuneToPoison))
return 0;
return ((poisonAttack + _vm->getRandomNumber(4)) << 3) / (poisonResistance + 1);
}
void GroupMan::processEvents29to41(int16 eventMapX, int16 eventMapY, int16 eventType, uint16 ticks) {
int16 L0446_i_Multiple = 0;
#define AL0446_i_Direction L0446_i_Multiple
#define AL0446_i_Ticks L0446_i_Multiple
#define AL0446_i_Behavior2Or3 L0446_i_Multiple
#define AL0446_i_CreatureAspectIndex L0446_i_Multiple
#define AL0446_i_Range L0446_i_Multiple
#define AL0446_i_CreatureAttributes L0446_i_Multiple
#define AL0446_i_Cell L0446_i_Multiple
#define AL0446_i_GroupCellsCriteria L0446_i_Multiple
int16 L0447_i_Multiple;
#define AL0447_i_Behavior L0447_i_Multiple
#define AL0447_i_CreatureIndex L0447_i_Multiple
#define AL0447_i_ReferenceDirection L0447_i_Multiple
#define AL0447_i_Ticks L0447_i_Multiple
int16 L0450_i_Multiple;
#define AL0450_i_DestinationMapX L0450_i_Multiple
#define AL0450_i_DistanceXToParty L0450_i_Multiple
#define AL0450_i_TargetMapX L0450_i_Multiple
int16 L0451_i_Multiple;
#define AL0451_i_DestinationMapY L0451_i_Multiple
#define AL0451_i_DistanceYToParty L0451_i_Multiple
#define AL0451_i_TargetMapY L0451_i_Multiple
/* If the party is not on the map specified in the event and the event type is not one of 32, 33, 37, 38 then the event is ignored */
if ((_vm->_dungeonMan->_currMapIndex != _vm->_dungeonMan->_partyMapIndex)
&& (eventType != k37_TMEventTypeUpdateBehaviourGroup) && (eventType != k32_TMEventTypeUpdateAspectGroup)
&& (eventType != k38_TMEventTypeUpdateBehaviour_0) && (eventType != k33_TMEventTypeUpdateAspectCreature_0))
return;
Thing groupThing = groupGetThing(eventMapX, eventMapY);
/* If there is no creature at the location specified in the event then the event is ignored */
if (groupThing == Thing::_endOfList)
return;
Group *curGroup = (Group *)_vm->_dungeonMan->getThingData(groupThing);
CreatureInfo creatureInfo = _vm->_dungeonMan->_creatureInfos[curGroup->_type];
/* Update the event */
TimelineEvent nextEvent;
nextEvent._mapTime = _vm->setMapAndTime(_vm->_dungeonMan->_currMapIndex, _vm->_gameTime);
nextEvent._priority = kDMMovementTicksImmobile - creatureInfo._movementTicks; /* The fastest creatures (with small MovementTicks value) get higher event priority */
nextEvent._Bu._location._mapX = eventMapX;
nextEvent._Bu._location._mapY = eventMapY;
/* If the creature is not on the party map then try and move the creature in a random direction and place a new event 37 in the timeline for the next creature movement */
if (_vm->_dungeonMan->_currMapIndex != _vm->_dungeonMan->_partyMapIndex) {
if (isMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->getRandomNumber(4), false)) { /* BUG0_67 A group that is not on the party map may wrongly move or not move into a teleporter. Normally, a creature type with Wariness >= 10 (Vexirk, Materializer / Zytaz, Demon, Lord Chaos, Red Dragon / Dragon) would only move into a teleporter if the creature type is allowed on the destination map. However, the variable G0380_T_CurrentGroupThing identifying the group is not set before being used by F0139_DUNGEON_IsCreatureAllowedOnMap called by f202_isMovementPossible so the check to see if the creature type is allowed may operate on another creature type and thus return an incorrect result, causing the creature to teleport while it should not, or not to teleport while it should */
AL0450_i_DestinationMapX = eventMapX;
AL0451_i_DestinationMapY = eventMapY;
AL0450_i_DestinationMapX += _vm->_dirIntoStepCountEast[AL0446_i_Direction];
AL0451_i_DestinationMapY += _vm->_dirIntoStepCountNorth[AL0446_i_Direction];
if (_vm->_moveSens->getMoveResult(groupThing, eventMapX, eventMapY, AL0450_i_DestinationMapX, AL0451_i_DestinationMapY))
return;
nextEvent._Bu._location._mapX = _vm->_moveSens->_moveResultMapX;
nextEvent._Bu._location._mapY = _vm->_moveSens->_moveResultMapY;
}
nextEvent._type = k37_TMEventTypeUpdateBehaviourGroup;
AL0446_i_Ticks = MAX(ABS(_vm->_dungeonMan->_currMapIndex - _vm->_dungeonMan->_partyMapIndex) << 4, creatureInfo._movementTicks << 1);
/* BUG0_68 A group moves or acts with a wrong timing. Event is added below but L0465_s_NextEvent.C.Ticks has not been initialized. No consequence while the group is not on the party map. When the party enters the group map the first group event may have a wrong timing */
T0209005_AddEventAndReturn:
nextEvent._mapTime += AL0446_i_Ticks;
_vm->_timeline->addEventGetEventIndex(&nextEvent);
return;
}
/* If the creature is Lord Chaos then ignore the event if the game is won. Initialize data to analyze Fluxcages */
bool isArchEnemy = getFlag(creatureInfo._attributes, kDMCreatureMaskArchenemy);
if (isArchEnemy) {
if (_vm->_gameWon)
return;
_fluxCageCount = 0;
_fluxCages[0] = 0;
}
ActiveGroup *activeGroup = &_activeGroups[curGroup->getActiveGroupIndex()];
// CHECKME: Terrible mix of types
int16 ticksSinceLastMove = (unsigned char)_vm->_gameTime - activeGroup->_lastMoveTime;
if (ticksSinceLastMove < 0)
ticksSinceLastMove += 256;
int16 movementTicks = creatureInfo._movementTicks;
if (movementTicks == kDMMovementTicksImmobile)
movementTicks = 100;
if (_vm->_championMan->_party._freezeLifeTicks && !isArchEnemy) { /* If life is frozen and the creature is not Lord Chaos (Lord Chaos is immune to Freeze Life) then reschedule the event later (except for reactions which are ignored when life if frozen) */
if (eventType < 0)
return;
nextEvent._type = eventType;
nextEvent._Cu._ticks = ticks;
AL0446_i_Ticks = 4; /* Retry in 4 ticks */
goto T0209005_AddEventAndReturn;
}
/* If the specified event type is a 'reaction' instead of a real event from the timeline then create the corresponding reaction event with a delay:
For event kM1_TMEventTypeCreateReactionEvent31ParyIsAdjacent, the reaction time is 1 tick
For event kM2_TMEventTypeCreateReactionEvent30HitByProjectile and kM3_TMEventTypeCreateReactionEvent29DangerOnSquare, the reaction time may be 1 tick or slower: slow moving creatures react more slowly. The more recent is the last creature move, the slower the reaction */
if (eventType < 0) {
nextEvent._type = eventType + k32_TMEventTypeUpdateAspectGroup;
if (eventType == kM1_TMEventTypeCreateReactionEvent31ParyIsAdjacent) {
AL0446_i_Ticks = 1; /* Retry in 1 tick */
} else {
AL0446_i_Ticks = ((movementTicks + 2) >> 2) - ticksSinceLastMove;
if (AL0446_i_Ticks < 1) /* AL0446_i_Ticks is the reaction time */
AL0446_i_Ticks = 1; /* Retry in 1 tick */
}
goto T0209005_AddEventAndReturn; /* BUG0_68 A group moves or acts with a wrong timing. Event is added but L0465_s_NextEvent.C.Ticks has not been initialized */
}
AL0447_i_Behavior = curGroup->getBehaviour();
uint16 creatureCount = curGroup->getCount();
int16 creatureSize = getFlag(creatureInfo._attributes, kDMCreatureMaskSize);
AL0450_i_DistanceXToParty = ABS(eventMapX - _vm->_dungeonMan->_partyMapX);
AL0451_i_DistanceYToParty = ABS(eventMapY - _vm->_dungeonMan->_partyMapY);
_currentGroupMapX = eventMapX;
_currentGroupMapY = eventMapY;
_currGroupThing = groupThing;
_groupMovementTestedDirections[0] = 0;
_currGroupDistanceToParty = getDistanceBetweenSquares(eventMapX, eventMapY, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY);
_currGroupPrimaryDirToParty = getDirsWhereDestIsVisibleFromSource(eventMapX, eventMapY, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY);
_currGroupSecondaryDirToParty = _vm->_projexpl->_secondaryDirToOrFromParty;
int32 nextAspectUpdateTime = 0;
bool notUpdateBehaviorFl = true;
bool newGroupDirectionFound;
bool approachAfterReaction = false;
bool moveToPriorLocation = false;
int16 distanceToVisibleParty = 0;
if (eventType <= k31_TMEventTypeGroupReactionPartyIsAdjecent) { /* Process Reaction events 29 to 31 */
switch (eventType = eventType - k32_TMEventTypeUpdateAspectGroup) {
case kM1_TMEventTypeCreateReactionEvent31ParyIsAdjacent: /* This event is used when the party bumps into a group or attacks a group physically (not with a spell). It causes the creature behavior to change to attack if it is not already attacking the party or fleeing from target */
if ((AL0447_i_Behavior != kDMBehaviorAttack) && (AL0447_i_Behavior != kDMBehaviorFlee)) {
groupDeleteEvents(eventMapX, eventMapY);
goto T0209044_SetBehavior6_Attack;
}
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
return;
case kM2_TMEventTypeCreateReactionEvent30HitByProjectile: /* This event is used for the reaction of a group after a projectile impacted with one creature in the group (some creatures may have been killed) */
if ((AL0447_i_Behavior == kDMBehaviorAttack) || (AL0447_i_Behavior == kDMBehaviorFlee)) /* If the creature is attacking the party or fleeing from the target then there is no reaction */
return;
AL0446_i_Behavior2Or3 = ((AL0447_i_Behavior == kDMBehaviorUnknown3) || (AL0447_i_Behavior == kDMBehaviorUnknown2));
if (AL0446_i_Behavior2Or3 || (_vm->getRandomNumber(4))) { /* BUG0_00 Useless code. Behavior cannot be 2 nor 3 because these values are never used. The actual condition is thus: if 3/4 chances */
if (!groupGetDistanceToVisibleParty(curGroup, kDMWholeCreatureGroup, eventMapX, eventMapY)) { /* If the group cannot see the party then look in a random direction to try and search for the party */
approachAfterReaction = newGroupDirectionFound = false;
goto T0209073_SetDirectionGroup;
}
if (AL0446_i_Behavior2Or3 || (_vm->getRandomNumber(4))) /* BUG0_00 Useless code. Behavior cannot be 2 nor 3 because these values are never used. The actual condition is thus: if 3/4 chances then no reaction */
return;
} /* No 'break': proceed to instruction after the next 'case' below. Reaction is to move in a random direction to try and avoid other projectiles */
case kM3_TMEventTypeCreateReactionEvent29DangerOnSquare: /* This event is used when some creatures in the group were killed by a Poison Cloud or by a closing door or if Lord Chaos is surrounded by 3 Fluxcages. It causes the creature to move in a random direction to avoid the danger */
approachAfterReaction = (AL0447_i_Behavior == kDMBehaviorAttack); /* If the creature behavior is 'Attack' and it has to move to avoid danger then it will change its behavior to 'Approach' after the movement */
newGroupDirectionFound = false;
goto T0209058_MoveInRandomDirection;
}
}
if (eventType < k37_TMEventTypeUpdateBehaviourGroup) { /* Process Update Aspect events 32 to 36 */
nextEvent._type = eventType + 5;
if (groupGetDistanceToVisibleParty(curGroup, kDMWholeCreatureGroup, eventMapX, eventMapY)) {
if ((AL0447_i_Behavior != kDMBehaviorAttack) && (AL0447_i_Behavior != kDMBehaviorFlee)) {
if (_vm->getDistance(_vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, eventMapX, eventMapY) <= 1)
goto T0209044_SetBehavior6_Attack;
if (((AL0447_i_Behavior == kDMBehaviorWander) || (AL0447_i_Behavior == kDMBehaviorUnknown3)) && (AL0447_i_Behavior != kDMBehaviorApproach)) /* BUG0_00 Useless code. Behavior cannot be 3 because this value is never used. Moreover, the second condition in the && is redundant (if the value is 0 or 3, it cannot be 7). The actual condition is: if (AL0447_i_Behavior == k0_behavior_WANDER) */
goto T0209054_SetBehavior7_Approach;
}
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
}
if (AL0447_i_Behavior == kDMBehaviorAttack) {
AL0446_i_CreatureAspectIndex = eventType - k33_TMEventTypeUpdateAspectCreature_0; /* Value -1 for event 32, meaning aspect will be updated for all creatures in the group */
nextAspectUpdateTime = getCreatureAspectUpdateTime(activeGroup, AL0446_i_CreatureAspectIndex, getFlag(activeGroup->_aspect[AL0446_i_CreatureAspectIndex], kDMAspectMaskActiveGroupIsAttacking));
goto T0209136;
}
if ((AL0450_i_DistanceXToParty > 3) || (AL0451_i_DistanceYToParty > 3)) {
nextAspectUpdateTime = _vm->_gameTime + ((creatureInfo._animationTicks >> 4) & 0xF);
goto T0209136;
}
} else { /* Process Update Behavior events 37 to 41 */
int16 primaryDirectionToOrFromParty;
notUpdateBehaviorFl = false;
if (ticks)
nextAspectUpdateTime = _vm->_gameTime;
if (eventType == k37_TMEventTypeUpdateBehaviourGroup) { /* Process event 37, Update Group Behavior */
bool allowMovementOverFakePitsAndFakeWalls;
if ((AL0447_i_Behavior == kDMBehaviorWander) || (AL0447_i_Behavior == kDMBehaviorUnknown2) || (AL0447_i_Behavior == kDMBehaviorUnknown3)) { /* BUG0_00 Useless code. Behavior cannot be 2 nor 3 because these values are never used. The actual condition is: if (AL0447_i_Behavior == k0_behavior_WANDER) */
distanceToVisibleParty = groupGetDistanceToVisibleParty(curGroup, kDMWholeCreatureGroup, eventMapX, eventMapY);
if (distanceToVisibleParty) {
if ((distanceToVisibleParty <= (creatureInfo.getAttackRange())) && ((!AL0450_i_DistanceXToParty) || (!AL0451_i_DistanceYToParty))) { /* If the creature is in range for attack and on the same row or column as the party on the map */
T0209044_SetBehavior6_Attack:
if (eventType == kM2_TMEventTypeCreateReactionEvent30HitByProjectile) {
groupDeleteEvents(eventMapX, eventMapY);
}
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
curGroup->setBehaviour(kDMBehaviorAttack);
AL0446_i_Direction = _currGroupPrimaryDirToParty;
for (AL0447_i_CreatureIndex = creatureCount; AL0447_i_CreatureIndex >= 0; AL0447_i_CreatureIndex--) {
if ((getCreatureValue(activeGroup->_directions, AL0447_i_CreatureIndex) != AL0446_i_Direction) &&
((!AL0447_i_CreatureIndex) || (!_vm->getRandomNumber(2)))) {
setGroupDirection(activeGroup, AL0446_i_Direction, AL0447_i_CreatureIndex, creatureCount && (creatureSize == kDMCreatureSizeHalf));
setTime(nextEvent._mapTime, _vm->_gameTime + _vm->getRandomNumber(4) + 2); /* Random delay represents the time for the creature to turn */
} else {
setTime(nextEvent._mapTime, _vm->_gameTime + 1);
}
if (notUpdateBehaviorFl) {
nextEvent._mapTime += MIN((uint16)((creatureInfo._attackTicks >> 1) + _vm->getRandomNumber(4)), ticks);
}
nextEvent._type = k38_TMEventTypeUpdateBehaviour_0 + AL0447_i_CreatureIndex;
addGroupEvent(&nextEvent, getCreatureAspectUpdateTime(activeGroup, AL0447_i_CreatureIndex, false));
}
return;
}
if (AL0447_i_Behavior != kDMBehaviorUnknown2) { /* BUG0_00 Useless code. Behavior cannot be 2 because this value is never used */
T0209054_SetBehavior7_Approach:
curGroup->setBehaviour(kDMBehaviorApproach);
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
nextEvent._mapTime += 1;
goto T0209134_SetEvent37;
}
} else {
if (AL0447_i_Behavior == kDMBehaviorWander) {
primaryDirectionToOrFromParty = getSmelledPartyPrimaryDirOrdinal(&creatureInfo, eventMapX, eventMapY);
if (primaryDirectionToOrFromParty) {
primaryDirectionToOrFromParty--;
allowMovementOverFakePitsAndFakeWalls = false;
goto T0209085_SingleSquareMove;
}
newGroupDirectionFound = false;
if (_vm->getRandomNumber(2)) {
T0209058_MoveInRandomDirection:
AL0446_i_Direction = _vm->getRandomNumber(4);
AL0447_i_ReferenceDirection = AL0446_i_Direction;
do {
AL0450_i_DestinationMapX = eventMapX;
AL0451_i_DestinationMapY = eventMapY;
AL0450_i_DestinationMapX += _vm->_dirIntoStepCountEast[AL0446_i_Direction], AL0451_i_DestinationMapY += _vm->_dirIntoStepCountNorth[AL0446_i_Direction];
if (((activeGroup->_priorMapX != AL0450_i_DestinationMapX) ||
(activeGroup->_priorMapY != AL0451_i_DestinationMapY) ||
(moveToPriorLocation = !_vm->getRandomNumber(4))) /* 1/4 chance of moving back to the square that the creature comes from */
&& isMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction, false)) {
T0209061_MoveGroup:
AL0447_i_Ticks = (movementTicks >> 1) - ticksSinceLastMove;
newGroupDirectionFound = (AL0447_i_Ticks <= 0);
if (newGroupDirectionFound) {
if (_vm->_moveSens->getMoveResult(groupThing, eventMapX, eventMapY, AL0450_i_DestinationMapX, AL0451_i_DestinationMapY))
return;
nextEvent._Bu._location._mapX = _vm->_moveSens->_moveResultMapX;
nextEvent._Bu._location._mapY = _vm->_moveSens->_moveResultMapY;;
activeGroup->_priorMapX = eventMapX;
activeGroup->_priorMapY = eventMapY;
activeGroup->_lastMoveTime = _vm->_gameTime;
} else {
movementTicks = AL0447_i_Ticks;
ticksSinceLastMove = -1;
}
break;
}
if (_groupMovementBlockedByParty) {
if ((eventType != kM3_TMEventTypeCreateReactionEvent29DangerOnSquare) &&
((curGroup->getBehaviour() != kDMBehaviorFlee) ||
!getFirstPossibleMovementDirOrdinal(&creatureInfo, eventMapX, eventMapY, false) ||
_vm->getRandomNumber(2)))
goto T0209044_SetBehavior6_Attack;
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
}
AL0446_i_Direction = _vm->turnDirRight(AL0446_i_Direction);
} while (AL0446_i_Direction != AL0447_i_ReferenceDirection);
}
if (!newGroupDirectionFound &&
(ticksSinceLastMove != -1) &&
isArchEnemy &&
((eventType == kM3_TMEventTypeCreateReactionEvent29DangerOnSquare) || !_vm->getRandomNumber(4))) { /* BUG0_15 The game hangs when you close a door on Lord Chaos. A condition is missing in the code to manage creatures and this may create an infinite loop between two parts in the code */
_vm->_projexpl->_secondaryDirToOrFromParty = _vm->turnDirRight(primaryDirectionToOrFromParty = _vm->getRandomNumber(4));
goto T0209089_DoubleSquareMove; /* BUG0_69 Memory corruption when you close a door on Lord Chaos. The local variable (L0454_i_PrimaryDirectionToOrFromParty) containing the direction where Lord Chaos tries to move may be used as an array index without being initialized and cause memory corruption */
}
if (newGroupDirectionFound || ((!_vm->getRandomNumber(4) || (distanceToVisibleParty <= creatureInfo.getSmellRange())) && (eventType != kM3_TMEventTypeCreateReactionEvent29DangerOnSquare))) {
T0209073_SetDirectionGroup:
if (!newGroupDirectionFound && (ticksSinceLastMove >= 0)) { /* If direction is not found yet then look around in a random direction */
AL0446_i_Direction = _vm->getRandomNumber(4);
}
setDirGroup(activeGroup, AL0446_i_Direction, creatureCount, creatureSize);
}
/* If event is kM3_TMEventTypeCreateReactionEvent29DangerOnSquare or kM2_TMEventTypeCreateReactionEvent30HitByProjectile */
if (eventType < kM1_TMEventTypeCreateReactionEvent31ParyIsAdjacent) {
if (!newGroupDirectionFound)
return;
if (approachAfterReaction)
curGroup->setBehaviour(kDMBehaviorApproach);
stopAttacking(activeGroup, eventMapX, eventMapY);
}
}
}
} else {
if (AL0447_i_Behavior == kDMBehaviorApproach) {
distanceToVisibleParty = groupGetDistanceToVisibleParty(curGroup, kDMWholeCreatureGroup, eventMapX, eventMapY);
if (distanceToVisibleParty) {
if ((distanceToVisibleParty <= creatureInfo.getAttackRange()) && ((!AL0450_i_DistanceXToParty) || (!AL0451_i_DistanceYToParty))) /* If the creature is in range for attack and on the same row or column as the party on the map */
goto T0209044_SetBehavior6_Attack;
T0209081_RunTowardParty:
movementTicks++;
movementTicks = movementTicks >> 1; /* Running speed is half the movement ticks */
AL0450_i_TargetMapX = (activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX);
AL0451_i_TargetMapY = (activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY);
} else {
T0209082_WalkTowardTarget:
AL0450_i_TargetMapX = activeGroup->_targetMapX;
AL0451_i_TargetMapY = activeGroup->_targetMapY;
/* If the creature reached its target but the party is not there anymore */
if ((eventMapX == AL0450_i_TargetMapX) && (eventMapY == AL0451_i_TargetMapY)) {
newGroupDirectionFound = false;
curGroup->setBehaviour(kDMBehaviorWander);
goto T0209073_SetDirectionGroup;
}
}
allowMovementOverFakePitsAndFakeWalls = true;
T0209084_SingleSquareMoveTowardParty:
primaryDirectionToOrFromParty = getDirsWhereDestIsVisibleFromSource(eventMapX, eventMapY, AL0450_i_TargetMapX, AL0451_i_TargetMapY);
T0209085_SingleSquareMove:
if (isMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = primaryDirectionToOrFromParty, allowMovementOverFakePitsAndFakeWalls) ||
isMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->_projexpl->_secondaryDirToOrFromParty, allowMovementOverFakePitsAndFakeWalls && _vm->getRandomNumber(2)) ||
isMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->returnOppositeDir((Direction)AL0446_i_Direction), false) ||
(!_vm->getRandomNumber(4) && isMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->returnOppositeDir((Direction)primaryDirectionToOrFromParty), false))) {
AL0450_i_DestinationMapX = eventMapX;
AL0451_i_DestinationMapY = eventMapY;
AL0450_i_DestinationMapX += _vm->_dirIntoStepCountEast[AL0446_i_Direction], AL0451_i_DestinationMapY += _vm->_dirIntoStepCountNorth[AL0446_i_Direction];
goto T0209061_MoveGroup;
}
if (isArchEnemy) {
T0209089_DoubleSquareMove:
getFirstPossibleMovementDirOrdinal(&creatureInfo, eventMapX, eventMapY, false); /* BUG0_00 Useless code. Returned value is ignored. When Lord Chaos teleports two squares away the ability to move to the first square is ignored which means Lord Chaos can teleport through walls or any other obstacle */
if (isArchenemyDoubleMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = primaryDirectionToOrFromParty) ||
isArchenemyDoubleMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->_projexpl->_secondaryDirToOrFromParty) ||
(_fluxCageCount && isArchenemyDoubleMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->returnOppositeDir((Direction)AL0446_i_Direction))) ||
((_fluxCageCount >= 2) && isArchenemyDoubleMovementPossible(&creatureInfo, eventMapX, eventMapY, AL0446_i_Direction = _vm->returnOppositeDir((Direction)primaryDirectionToOrFromParty)))) {
AL0450_i_DestinationMapX = eventMapX;
AL0451_i_DestinationMapY = eventMapY;
AL0450_i_DestinationMapX += _vm->_dirIntoStepCountEast[AL0446_i_Direction] * 2, AL0451_i_DestinationMapY += _vm->_dirIntoStepCountNorth[AL0446_i_Direction] * 2;
_vm->_sound->requestPlay(kDMSoundIndexBuzz, AL0450_i_DestinationMapX, AL0451_i_DestinationMapY, kDMSoundModePlayIfPrioritized);
goto T0209061_MoveGroup;
}
}
setDirGroup(activeGroup, primaryDirectionToOrFromParty, creatureCount, creatureSize);
} else {
if (AL0447_i_Behavior == kDMBehaviorFlee) {
T0209094_FleeFromTarget:
allowMovementOverFakePitsAndFakeWalls = true;
/* If the creature can see the party then update target coordinates */
distanceToVisibleParty = groupGetDistanceToVisibleParty(curGroup, kDMWholeCreatureGroup, eventMapX, eventMapY);
if (distanceToVisibleParty) {
AL0450_i_TargetMapX = (activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX);
AL0451_i_TargetMapY = (activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY);
} else {
if (!(--(activeGroup->_delayFleeingFromTarget))) { /* If the creature is not afraid anymore then stop fleeing from target */
T0209096_SetBehavior0_Wander:
newGroupDirectionFound = false;
curGroup->setBehaviour(kDMBehaviorWander);
goto T0209073_SetDirectionGroup;
}
if (_vm->getRandomNumber(2)) {
/* If the creature cannot move and the party is adjacent then stop fleeing */
if (!getFirstPossibleMovementDirOrdinal(&creatureInfo, eventMapX, eventMapY, false)) {
if (_vm->getDistance(eventMapX, eventMapY, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY) <= 1)
goto T0209096_SetBehavior0_Wander;
}
/* Set creature target to the home square where the creature was located when the party entered the map */
AL0450_i_TargetMapX = activeGroup->_homeMapX;
AL0451_i_TargetMapY = activeGroup->_homeMapY;
goto T0209084_SingleSquareMoveTowardParty;
}
AL0450_i_TargetMapX = activeGroup->_targetMapX;
AL0451_i_TargetMapY = activeGroup->_targetMapY;
}
/* Try and flee from the party (opposite direction) */
primaryDirectionToOrFromParty = _vm->returnOppositeDir((Direction)getDirsWhereDestIsVisibleFromSource(eventMapX, eventMapY, AL0450_i_TargetMapX, AL0451_i_TargetMapY));
_vm->_projexpl->_secondaryDirToOrFromParty = _vm->returnOppositeDir((Direction)_vm->_projexpl->_secondaryDirToOrFromParty);
movementTicks -= (movementTicks >> 2);
goto T0209085_SingleSquareMove;
}
}
}
} else { /* Process events 38 to 41, Update Creature Behavior */
if (AL0447_i_Behavior == kDMBehaviorFlee) {
if (creatureCount) {
stopAttacking(activeGroup, eventMapX, eventMapY);
}
goto T0209094_FleeFromTarget;
}
/* If the creature is attacking, then compute the next aspect update time and the next attack time */
if (getFlag(activeGroup->_aspect[AL0447_i_CreatureIndex = eventType - k38_TMEventTypeUpdateBehaviour_0], kDMAspectMaskActiveGroupIsAttacking)) {
nextAspectUpdateTime = getCreatureAspectUpdateTime(activeGroup, AL0447_i_CreatureIndex, false);
nextEvent._mapTime += ((AL0447_i_Ticks = creatureInfo._attackTicks) + _vm->getRandomNumber(4) - 1);
if (AL0447_i_Ticks > 15)
nextEvent._mapTime += _vm->getRandomNumber(8) - 2;
} else { /* If the creature is not attacking, then try attacking if possible */
if (AL0447_i_CreatureIndex > creatureCount) /* Ignore event if it is for a creature that is not in the group */
return;
primaryDirectionToOrFromParty = _currGroupPrimaryDirToParty;
distanceToVisibleParty = groupGetDistanceToVisibleParty(curGroup, AL0447_i_CreatureIndex, eventMapX, eventMapY);
/* If the party is visible, update the target coordinates */
if (distanceToVisibleParty) {
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
}
/* If there is a single creature in the group that is not full square sized and 1/4 chance */
if (!creatureCount && (creatureSize != kDMCreatureSizeFull) && !((AL0446_i_GroupCellsCriteria = _vm->getRandomNumber(65536)) & 0x00C0)) {
if (activeGroup->_cells != kDMCreatureTypeSingleCenteredCreature) {
/* If the creature is not already on the center of the square then change its cell */
if (AL0446_i_GroupCellsCriteria & 0x0038) /* 7/8 chances of changing cell to the center of the square */
activeGroup->_cells = kDMCreatureTypeSingleCenteredCreature;
else /* 1/8 chance of changing cell to the next or previous cell on the square */
AL0446_i_GroupCellsCriteria = _vm->normalizeModulo4(_vm->normalizeModulo4(activeGroup->_cells) + ((AL0446_i_GroupCellsCriteria & 0x0001) ? 1 : -1));
}
/* If 1/8 chance and the creature is not adjacent to the party and is a quarter square sized creature then process projectile impacts and update the creature cell if still alive. When the creature is not in front of the party, it has 7/8 chances of dodging a projectile by moving to another cell or staying in the center of the square */
if (!(AL0446_i_GroupCellsCriteria & 0x0038) && (distanceToVisibleParty != 1) && (creatureSize == kDMCreatureSizeQuarter)) {
if (_vm->_projexpl->projectileGetImpactCount(kDMElementTypeCreature, eventMapX, eventMapY, activeGroup->_cells) && (_vm->_projexpl->_creatureDamageOutcome == kDMKillOutcomeAllCreaturesInGroup)) /* This call to F0218_PROJECTILE_GetImpactCount works fine because there is a single creature in the group so L0445_ps_ActiveGroup->Cells contains only one cell index */
return;
activeGroup->_cells = _vm->normalizeModulo4(AL0446_i_GroupCellsCriteria);
}
}
/* If the creature can see the party and is looking in the party direction or can attack in all direction */
if (distanceToVisibleParty &&
(getFlag(creatureInfo._attributes, kDMCreatureMaskSideAttack) ||
getCreatureValue(activeGroup->_directions, AL0447_i_CreatureIndex) == primaryDirectionToOrFromParty)) {
/* If the creature is in range to attack the party and random test succeeds */
if ((distanceToVisibleParty <= (AL0446_i_Range = creatureInfo.getAttackRange())) &&
(!AL0450_i_DistanceXToParty || !AL0451_i_DistanceYToParty) &&
(AL0446_i_Range <= (_vm->getRandomNumber(16) + 1))) {
if ((AL0446_i_Range == 1) &&
(!getFlag(AL0446_i_CreatureAttributes = creatureInfo._attributes, kDMCreatureMaskPreferBackRow) || !_vm->getRandomNumber(4) || !getFlag(AL0446_i_CreatureAttributes, kDMCreatureMaskAttackAnyChamp)) &&
(creatureSize == kDMCreatureSizeQuarter) &&
(activeGroup->_cells != kDMCreatureTypeSingleCenteredCreature) &&
((AL0446_i_Cell = getCreatureValue(activeGroup->_cells, AL0447_i_CreatureIndex)) != primaryDirectionToOrFromParty) &&
(AL0446_i_Cell != _vm->turnDirRight(primaryDirectionToOrFromParty))) { /* If the creature cannot cast spells (range = 1) and is not on a cell where it can attack the party directly and is a quarter square sized creature not in the center of the square then the creature moves to another cell and attack does not occur immediately */
if (!creatureCount && _vm->getRandomNumber(2)) {
activeGroup->_cells = kDMCreatureTypeSingleCenteredCreature;
} else {
if ((primaryDirectionToOrFromParty & 0x0001) == (AL0446_i_Cell & 0x0001))
AL0446_i_Cell--;
else
AL0446_i_Cell++;
AL0446_i_Cell = _vm->normalizeModulo4(AL0446_i_Cell);
if (!getCreatureOrdinalInCell(curGroup, AL0446_i_Cell) ||
(_vm->getRandomNumber(2) && !getCreatureOrdinalInCell(curGroup, AL0446_i_Cell = _vm->returnOppositeDir((Direction)AL0446_i_Cell)))) { /* If the selected cell (or the opposite cell) is not already occupied by a creature */
if (_vm->_projexpl->projectileGetImpactCount(kDMElementTypeCreature, eventMapX, eventMapY, activeGroup->_cells) && (_vm->_projexpl->_creatureDamageOutcome == kDMKillOutcomeAllCreaturesInGroup)) /* BUG0_70 A projectile impact on a creature may be ignored. The function F0218_PROJECTILE_GetImpactCount to detect projectile impacts when a quarter square sized creature moves inside a group (to another cell on the same square) may fail if there are several creatures in the group because the function expects a single cell index for its last parameter. The function should be called once for each cell where there is a creature */
return;
if (_vm->_projexpl->_creatureDamageOutcome != kDMKillOutcomeSomeCreaturesInGroup) {
activeGroup->_cells = getGroupValueUpdatedWithCreatureValue(activeGroup->_cells, AL0447_i_CreatureIndex, AL0446_i_Cell);
}
}
}
nextEvent._mapTime += MAX(1, (creatureInfo._movementTicks >> 1) + _vm->getRandomNumber(2)); /* Time for the creature to change cell */
nextEvent._type = eventType;
goto T0209135;
}
nextAspectUpdateTime = getCreatureAspectUpdateTime(activeGroup, AL0447_i_CreatureIndex, isCreatureAttacking(curGroup, eventMapX, eventMapY, AL0447_i_CreatureIndex));
nextEvent._mapTime += (creatureInfo._animationTicks & 0xF) + _vm->getRandomNumber(2);
} else {
curGroup->setBehaviour(kDMBehaviorApproach);
if (creatureCount) {
stopAttacking(activeGroup, eventMapX, eventMapY);
}
goto T0209081_RunTowardParty;
}
} else {
/* If the party is visible, update target coordinates */
if (groupGetDistanceToVisibleParty(curGroup, kDMWholeCreatureGroup, eventMapX, eventMapY)) {
activeGroup->_targetMapX = _vm->_dungeonMan->_partyMapX;
activeGroup->_targetMapY = _vm->_dungeonMan->_partyMapY;
setGroupDirection(activeGroup, primaryDirectionToOrFromParty, AL0447_i_CreatureIndex, creatureCount && (creatureSize == kDMCreatureSizeHalf));
nextEvent._mapTime += 2;
nextAspectUpdateTime = _vm->filterTime(nextEvent._mapTime);
} else { /* If the party is not visible, move to the target (last known party location) */
curGroup->setBehaviour(kDMBehaviorApproach);
if (creatureCount) {
stopAttacking(activeGroup, eventMapX, eventMapY);
}
goto T0209082_WalkTowardTarget;
}
}
}
nextEvent._type = eventType;
goto T0209136;
}
nextEvent._mapTime += MAX(1, _vm->getRandomNumber(4) + movementTicks - 1);
T0209134_SetEvent37:
nextEvent._type = k37_TMEventTypeUpdateBehaviourGroup;
}
T0209135:
if (!nextAspectUpdateTime) {
nextAspectUpdateTime = getCreatureAspectUpdateTime(activeGroup, kDMWholeCreatureGroup, false);
}
T0209136:
if (notUpdateBehaviorFl) {
nextEvent._mapTime += ticks;
} else {
nextAspectUpdateTime += ticks;
}
addGroupEvent(&nextEvent, nextAspectUpdateTime);
}
bool GroupMan::isMovementPossible(CreatureInfo *creatureInfo, int16 mapX, int16 mapY, uint16 dir, bool allowMovementOverImaginaryPitsAndFakeWalls) {
_groupMovementTestedDirections[dir] = true;
_groupMovementBlockedByGroupThing = Thing::_endOfList;
_groupMovementBlockedByDoor = false;
_groupMovementBlockedByParty = false;
if (creatureInfo->_movementTicks == kDMMovementTicksImmobile)
return false;
_vm->_dungeonMan->mapCoordsAfterRelMovement((Direction)dir, 1, 0, mapX, mapY);
uint16 curSquare = _vm->_dungeonMan->_currMapData[mapX][mapY];
int16 curSquareType = Square(curSquare).getType();
_groupMovBlockedByWallStairsPitFakeWalFluxCageTeleporter =
!(((mapX >= 0) && (mapX < _vm->_dungeonMan->_currMapWidth)) &&
((mapY >= 0) && (mapY < _vm->_dungeonMan->_currMapHeight)) &&
(curSquareType != kDMElementTypeWall) &&
(curSquareType != kDMElementTypeStairs) &&
((curSquareType != kDMElementTypePit) || (getFlag(curSquare, kDMSquareMaskPitImaginary) && allowMovementOverImaginaryPitsAndFakeWalls) || !getFlag(curSquare, kDMSquareMaskPitOpen) || getFlag(creatureInfo->_attributes, kDMCreatureMaskLevitation)) &&
((curSquareType != kDMElementTypeFakeWall) || getFlag(curSquare, kDMSquareMaskFakeWallOpen) || (getFlag(curSquare, kDMSquareMaskFakeWallImaginary) && allowMovementOverImaginaryPitsAndFakeWalls)));
if (_groupMovBlockedByWallStairsPitFakeWalFluxCageTeleporter)
return false;
if (getFlag(creatureInfo->_attributes, kDMCreatureMaskArchenemy)) {
Thing curThing = _vm->_dungeonMan->getSquareFirstThing(mapX, mapY);
while (curThing != Thing::_endOfList) {
if ((curThing).getType() == kDMThingTypeExplosion) {
Teleporter *curTeleporter = (Teleporter *)_vm->_dungeonMan->getThingData(curThing);
if (((Explosion *)curTeleporter)->setType(kDMExplosionTypeFluxcage)) {
_fluxCages[dir] = true;
_fluxCageCount++;
_groupMovBlockedByWallStairsPitFakeWalFluxCageTeleporter = true;
return false;
}
}
curThing = _vm->_dungeonMan->getNextThing(curThing);
}
}
if ((curSquareType == kDMElementTypeTeleporter) && getFlag(curSquare, kDMSquareMaskTeleporterOpen) && (creatureInfo->getWariness() >= 10)) {
Teleporter *curTeleporter = (Teleporter *)_vm->_dungeonMan->getSquareFirstThingData(mapX, mapY);
if (getFlag(curTeleporter->getScope(), kDMTeleporterScopeCreatures) && !_vm->_dungeonMan->isCreatureAllowedOnMap(_currGroupThing, curTeleporter->getTargetMapIndex())) {
_groupMovBlockedByWallStairsPitFakeWalFluxCageTeleporter = true;
return false;
}
}
_groupMovementBlockedByParty = (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex) && (mapX == _vm->_dungeonMan->_partyMapX) && (mapY == _vm->_dungeonMan->_partyMapY);
if (_groupMovementBlockedByParty)
return false;
if (curSquareType == kDMElementTypeDoor) {
Teleporter *curTeleporter = (Teleporter *)_vm->_dungeonMan->getSquareFirstThingData(mapX, mapY);
if (((Square(curSquare).getDoorState()) > (((Door *)curTeleporter)->opensVertically() ? CreatureInfo::getHeight(creatureInfo->_attributes) : 1)) && ((Square(curSquare).getDoorState()) != kDMDoorStateDestroyed) && !getFlag(creatureInfo->_attributes, kDMCreatureMaskNonMaterial)) {
_groupMovementBlockedByDoor = true;
return false;
}
}
_groupMovementBlockedByGroupThing = groupGetThing(mapX, mapY);
return (_groupMovementBlockedByGroupThing == Thing::_endOfList);
}
int16 GroupMan::getDistanceBetweenSquares(int16 srcMapX, int16 srcMapY, int16 destMapX, int16 destMapY) {
return ABS(srcMapX - destMapX) + ABS(srcMapY - destMapY);
}
int16 GroupMan::groupGetDistanceToVisibleParty(Group *group, int16 creatureIndex, int16 mapX, int16 mapY) {
uint16 groupDirections;
CreatureInfo *groupCreatureInfo = &_vm->_dungeonMan->_creatureInfos[group->_type];
if (_vm->_championMan->_party._event71Count_Invisibility && !getFlag(groupCreatureInfo->_attributes, kDMCreatureMaskSeeInvisible))
return 0;
bool alwaysSee = false;
int16 checkDirectionsCount; /* Count of directions to test in L0425_ai_CreatureViewDirections */
int16 creatureViewDirections[4]; /* List of directions to test */
if (getFlag(groupCreatureInfo->_attributes, kDMCreatureMaskSideAttack)) { /* If creature can see in all directions */
alwaysSee = true;
checkDirectionsCount = 1;
creatureViewDirections[0] = kDMDirNorth;
} else {
groupDirections = _activeGroups[group->getActiveGroupIndex()]._directions;
if (creatureIndex < 0) { /* Negative index means test if each creature in the group can see the party in their respective direction */
checkDirectionsCount = 0;
for (creatureIndex = group->getCount(); creatureIndex >= 0; creatureIndex--) {
int16 creatureDirection = _vm->normalizeModulo4(groupDirections >> (creatureIndex << 1));
int16 counter = checkDirectionsCount;
bool skipSet = false;
while (counter--) {
if (creatureViewDirections[counter] == creatureDirection) { /* If the creature looks in the same direction as another one in the group */
skipSet = true;
break;
}
}
if (!skipSet)
creatureViewDirections[checkDirectionsCount++] = creatureDirection;
}
} else { /* Positive index means test only if the specified creature in the group can see the party in its direction */
creatureViewDirections[0] = getCreatureValue(groupDirections, creatureIndex);
checkDirectionsCount = 1;
}
}
while (checkDirectionsCount--) {
if (alwaysSee || isDestVisibleFromSource(creatureViewDirections[checkDirectionsCount], mapX, mapY, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY)) {
int16 sightRange = groupCreatureInfo->getSightRange();
if (!getFlag(groupCreatureInfo->_attributes, kDMCreatureMaskNightVision))
sightRange -= _vm->_displayMan->_dungeonViewPaletteIndex >> 1;
if (_currGroupDistanceToParty > MAX<int16>(1, sightRange))
return 0;
return getDistanceBetweenUnblockedSquares(mapX, mapY, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, &GroupMan::isViewPartyBlocked);
}
}
return 0;
}
int16 GroupMan::getDistanceBetweenUnblockedSquares(int16 srcMapX, int16 srcMapY,
int16 destMapX, int16 destMapY, bool (GroupMan::*isBlocked)(uint16, uint16)) {
if (_vm->getDistance(srcMapX, srcMapY, destMapX, destMapY) <= 1)
return 1;
int16 distanceX = ABS(destMapX - srcMapX);
int16 distanceY = ABS(destMapY - srcMapY);
bool isDistanceXSmallerThanDistanceY = (distanceX < distanceY);
bool isDistanceXEqualsDistanceY = (distanceX == distanceY);
int16 pathMapX = destMapX;
int16 pathMapY = destMapY;
int16 axisStepX = ((pathMapX - srcMapX) > 0) ? -1 : 1;
int16 axisStepY = ((pathMapY - srcMapY) > 0) ? -1 : 1;
int16 largestAxisDistance;
int16 valueA;
int16 valueB;
int16 valueC;
if (isDistanceXSmallerThanDistanceY) {
largestAxisDistance = pathMapY - srcMapY;
valueC = (largestAxisDistance ? ((pathMapX - srcMapX) << 6) / largestAxisDistance : 128);
} else {
largestAxisDistance = pathMapX - srcMapX;
valueC = (largestAxisDistance ? ((pathMapY - srcMapY) << 6) / largestAxisDistance : 128);
}
/* 128 when the creature is on the same row or column as the party */
do {
if (isDistanceXEqualsDistanceY) {
if (( (CALL_MEMBER_FN(*_vm->_groupMan, isBlocked))(pathMapX + axisStepX, pathMapY)
&& (CALL_MEMBER_FN(*_vm->_groupMan, isBlocked))(pathMapX, pathMapY + axisStepY))
|| (CALL_MEMBER_FN(*_vm->_groupMan, isBlocked))(pathMapX = pathMapX + axisStepX, pathMapY = pathMapY + axisStepY))
return 0;
} else {
if (isDistanceXSmallerThanDistanceY) {
valueA = ABS(((pathMapY - srcMapY) ? ((pathMapX + axisStepX - srcMapX) << 6) / largestAxisDistance : 128) - valueC);
valueB = ABS(((pathMapY + axisStepY - srcMapY) ? ((pathMapX - srcMapX) << 6) / largestAxisDistance : 128) - valueC);
} else {
valueA = ABS(((pathMapX + axisStepX - srcMapX) ? ((pathMapY - srcMapY) << 6) / largestAxisDistance : 128) - valueC);
valueB = ABS(((pathMapX - srcMapX) ? ((pathMapY + axisStepY - srcMapY) << 6) / largestAxisDistance : 128) - valueC);
}
if (valueA < valueB)
pathMapX += axisStepX;
else
pathMapY += axisStepY;
if ((CALL_MEMBER_FN(*_vm->_groupMan, isBlocked))(pathMapX, pathMapY)) {
pathMapX += axisStepX;
pathMapY -= axisStepY;
if (((valueA != valueB) || (CALL_MEMBER_FN(*_vm->_groupMan, isBlocked))(pathMapX, pathMapY)))
return 0;
}
}
} while (_vm->getDistance(pathMapX, pathMapY, srcMapX, srcMapY) > 1);
return getDistanceBetweenSquares(srcMapX, srcMapY, destMapX, destMapY);
}
bool GroupMan::isViewPartyBlocked(uint16 mapX, uint16 mapY) {
uint16 curSquare = _vm->_dungeonMan->_currMapData[mapX][mapY];
int16 curSquareType = Square(curSquare).getType();
if (curSquareType == kDMElementTypeDoor) {
Door *curDoor = (Door *)_vm->_dungeonMan->getSquareFirstThingData(mapX, mapY);
int16 curDoorState = Square(curSquare).getDoorState();
return ((curDoorState == kDMDoorStateThreeFourth) || (curDoorState == kDMDoorStateClosed)) && !getFlag(_vm->_dungeonMan->_currMapDoorInfo[curDoor->getType()]._attributes, kDMMaskDoorInfoCreaturesCanSeeThrough);
}
return (curSquareType == kDMElementTypeWall) || ((curSquareType == kDMElementTypeFakeWall) && !getFlag(curSquare, kDMSquareMaskFakeWallOpen));
}
int32 GroupMan::getCreatureAspectUpdateTime(ActiveGroup *activeGroup, int16 creatureIndex, bool isAttacking) {
Group *group = &(((Group *)_vm->_dungeonMan->_thingData[kDMThingTypeGroup])[activeGroup->_groupThingIndex]);
CreatureType creatureType = group->_type;
uint16 creatureGraphicInfo = _vm->_dungeonMan->_creatureInfos[creatureType]._graphicInfo;
bool processGroup = (creatureIndex < 0);
if (processGroup) /* If the creature index is negative then all creatures in the group are processed */
creatureIndex = group->getCount();
do {
uint16 aspect = activeGroup->_aspect[creatureIndex];
aspect &= kDMAspectMaskActiveGroupIsAttacking | kDMAspectMaskActiveGroupFlipBitmap;
int16 offset = ((creatureGraphicInfo >> 12) & 0x3);
if (offset) {
offset = _vm->getRandomNumber(offset);
if (_vm->getRandomNumber(2))
offset = (-offset) & 0x0007;
aspect |= offset;
}
offset = ((creatureGraphicInfo >> 14) & 0x3);
if (offset) {
offset = _vm->getRandomNumber(offset);
if (_vm->getRandomNumber(2))
offset = (-offset) & 0x0007;
aspect |= (offset << 3);
}
if (isAttacking) {
if (getFlag(creatureGraphicInfo, kDMCreatureMaskFlipAttack)) {
if (getFlag(aspect, kDMAspectMaskActiveGroupIsAttacking) && (creatureType == kDMCreatureTypeAnimatedArmour)) {
if (_vm->getRandomNumber(2)) {
toggleFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
_vm->_sound->requestPlay(kDMSoundIndexAttackSkelettonAnimatedArmorDethKnight, _currentGroupMapX, _currentGroupMapY, kDMSoundModePlayIfPrioritized);
}
} else if (!getFlag(aspect, kDMAspectMaskActiveGroupIsAttacking) || !getFlag(creatureGraphicInfo, kDMCreatureMaskFlipDuringAttack)) {
if (_vm->getRandomNumber(2))
setFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
else
clearFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
}
} else
clearFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
setFlag(aspect, kDMAspectMaskActiveGroupIsAttacking);
} else {
if (getFlag(creatureGraphicInfo, kDMCreatureMaskFlipNonAttack)) {
if (creatureType == kDMCreatureTypeCouatl) {
if (_vm->getRandomNumber(2)) {
toggleFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
uint16 soundIndex = _vm->_moveSens->getSound(kDMCreatureTypeCouatl);
if (soundIndex <= kDMSoundCount)
_vm->_sound->requestPlay(soundIndex, _currentGroupMapX, _currentGroupMapY, kDMSoundModePlayIfPrioritized);
}
} else if (_vm->getRandomNumber(2))
setFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
else
clearFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
} else
clearFlag(aspect, kDMAspectMaskActiveGroupFlipBitmap);
clearFlag(aspect, kDMAspectMaskActiveGroupIsAttacking);
}
activeGroup->_aspect[creatureIndex] = aspect;
} while (processGroup && (creatureIndex--));
uint16 animationTicks = _vm->_dungeonMan->_creatureInfos[group->_type]._animationTicks;
return _vm->_gameTime + (isAttacking ? ((animationTicks >> 8) & 0xF) : ((animationTicks >> 4) & 0xF)) + _vm->getRandomNumber(2);
}
void GroupMan::setGroupDirection(ActiveGroup *activeGroup, int16 dir, int16 creatureIndex, bool twoHalfSquareSizedCreatures) {
static ActiveGroup *G0396_ps_TwoHalfSquareSizedCreaturesGroupLastDirectionSetActiveGroup;
if (twoHalfSquareSizedCreatures
&& (_vm->_gameTime == twoHalfSquareSizedCreaturesGroupLastDirectionSetTime)
&& (activeGroup == G0396_ps_TwoHalfSquareSizedCreaturesGroupLastDirectionSetActiveGroup))
return;
uint16 groupDirections = activeGroup->_directions;
if (_vm->normalizeModulo4(getCreatureValue(groupDirections, creatureIndex) - dir) == 2) { /* If current and new direction are opposites then change direction only one step at a time */
dir = _vm->turnDirRight((_vm->getRandomNumber(65536) & 0x0002) + dir);
groupDirections = getGroupValueUpdatedWithCreatureValue(groupDirections, creatureIndex, dir);
} else
groupDirections = getGroupValueUpdatedWithCreatureValue(groupDirections, creatureIndex, dir);
if (twoHalfSquareSizedCreatures) {
groupDirections = getGroupValueUpdatedWithCreatureValue(groupDirections, creatureIndex ^ 1, dir); /* Set direction of the second half square sized creature */
twoHalfSquareSizedCreaturesGroupLastDirectionSetTime = _vm->_gameTime;
G0396_ps_TwoHalfSquareSizedCreaturesGroupLastDirectionSetActiveGroup = activeGroup;
}
activeGroup->_directions = (Direction)groupDirections;
}
void GroupMan::addGroupEvent(TimelineEvent *event, uint32 time) {
warning("potentially dangerous cast to uint32 below");
if (time < (uint32)_vm->filterTime(event->_mapTime)) {
event->_type -= 5;
event->_Cu._ticks = _vm->filterTime(event->_mapTime) - time;
setTime(event->_mapTime, time);
} else
event->_Cu._ticks = time - _vm->filterTime(event->_mapTime);
_vm->_timeline->addEventGetEventIndex(event);
}
int16 GroupMan::getSmelledPartyPrimaryDirOrdinal(CreatureInfo *creatureInfo, int16 mapY, int16 mapX) {
uint16 smellRange = creatureInfo->getSmellRange();
if (!smellRange)
return 0;
if ((((smellRange + 1) >> 1) >= _currGroupDistanceToParty) && getDistanceBetweenUnblockedSquares(mapY, mapX, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, &GroupMan::isSmellPartyBlocked)) {
_vm->_projexpl->_secondaryDirToOrFromParty = _currGroupSecondaryDirToParty;
return _vm->indexToOrdinal(_currGroupPrimaryDirToParty);
}
int16 scentOrdinal = _vm->_championMan->getScentOrdinal(mapY, mapX);
if (scentOrdinal && ((_vm->_championMan->_party._scentStrengths[_vm->ordinalToIndex(scentOrdinal)] + _vm->getRandomNumber(4)) > (30 - (smellRange << 1)))) { /* If there is a fresh enough party scent on the group square */
return _vm->indexToOrdinal(getDirsWhereDestIsVisibleFromSource(mapY, mapX, _vm->_championMan->_party._scents[scentOrdinal].getMapX(), _vm->_championMan->_party._scents[scentOrdinal].getMapY()));
}
return 0;
}
bool GroupMan::isSmellPartyBlocked(uint16 mapX, uint16 mapY) {
uint16 square = _vm->_dungeonMan->_currMapData[mapX][mapY];
int16 squareType = Square(square).getType();
return ( (squareType) == kDMElementTypeWall) || ((squareType == kDMElementTypeFakeWall)
&& !getFlag(square, kDMSquareMaskFakeWallOpen));
}
int16 GroupMan::getFirstPossibleMovementDirOrdinal(CreatureInfo *info, int16 mapX, int16 mapY, bool allowMovementOverImaginaryPitsAndFakeWalls) {
for (int16 direction = kDMDirNorth; direction <= kDMDirWest; direction++) {
if ((!_groupMovementTestedDirections[direction]) && isMovementPossible(info, mapX, mapY, direction, allowMovementOverImaginaryPitsAndFakeWalls)) {
return _vm->indexToOrdinal(direction);
}
}
return 0;
}
void GroupMan::setDirGroup(ActiveGroup *activeGroup, int16 dir, int16 creatureIndex, int16 creatureSize) {
bool twoHalfSquareSizedCreatures = creatureIndex && (creatureSize == kDMCreatureSizeHalf);
if (twoHalfSquareSizedCreatures)
creatureIndex--;
do {
if (!creatureIndex || _vm->getRandomNumber(2))
setGroupDirection(activeGroup, dir, creatureIndex, twoHalfSquareSizedCreatures);
} while (creatureIndex--);
}
void GroupMan::stopAttacking(ActiveGroup *group, int16 mapX, int16 mapY) {
for (int16 creatureIndex = 0; creatureIndex < 4; creatureIndex++)
clearFlag(group->_aspect[creatureIndex++], kDMAspectMaskActiveGroupIsAttacking);
groupDeleteEvents(mapX, mapY);
}
bool GroupMan::isArchenemyDoubleMovementPossible(CreatureInfo *info, int16 mapX, int16 mapY, uint16 dir) {
if (_fluxCages[dir])
return false;
mapX += _vm->_dirIntoStepCountEast[dir], mapY += _vm->_dirIntoStepCountNorth[dir];
return isMovementPossible(info, mapX, mapY, dir, false);
}
bool GroupMan::isCreatureAttacking(Group *group, int16 mapX, int16 mapY, uint16 creatureIndex) {
static const uint8 creatureAttackSounds[11] = { 3, 7, 14, 15, 19, 21, 29, 30, 31, 4, 16 }; /* Atari ST: { 3, 7, 14, 15, 19, 21, 4, 16 } */
_vm->_projexpl->_lastCreatureAttackTime = _vm->_gameTime;
ActiveGroup activeGroup = _activeGroups[group->getActiveGroupIndex()];
CreatureType creatureType = group->_type;
CreatureInfo *creatureInfo = &_vm->_dungeonMan->_creatureInfos[creatureType];
uint16 primaryDirectionToParty = _currGroupPrimaryDirToParty;
int16 targetCell;
byte groupCells = activeGroup._cells;
if (groupCells == kDMCreatureTypeSingleCenteredCreature)
targetCell = _vm->getRandomNumber(2);
else
targetCell = ((getCreatureValue(groupCells, creatureIndex) + 5 - primaryDirectionToParty) & 0x0002) >> 1;
targetCell += primaryDirectionToParty;
targetCell &= 0x0003;
if ((creatureInfo->getAttackRange() > 1) && ((_currGroupDistanceToParty > 1) || _vm->getRandomNumber(2))) {
Thing projectileThing = Thing::_none;
switch (creatureType) {
case kDMCreatureTypeVexirk:
case kDMCreatureTypeLordChaos:
if (_vm->getRandomNumber(2)) {
projectileThing = Thing::_explFireBall;
} else {
switch (_vm->getRandomNumber(4)) {
case 0:
projectileThing = Thing::_explHarmNonMaterial;
break;
case 1:
projectileThing = Thing::_explLightningBolt;
break;
case 2:
projectileThing = Thing::_explPoisonCloud;
break;
case 3:
projectileThing = Thing::_explOpenDoor;
}
}
break;
case kDMCreatureTypeSwampSlime:
projectileThing = Thing::_explSlime;
break;
case kDMCreatureTypeWizardEye:
if (_vm->getRandomNumber(8)) {
projectileThing = Thing::_explLightningBolt;
} else {
projectileThing = Thing::_explOpenDoor;
}
break;
case kDMCreatureTypeMaterializerZytaz:
if (_vm->getRandomNumber(2)) {
projectileThing = Thing::_explPoisonCloud;
break;
}
case kDMCreatureTypeDemon:
case kDMCreatureTypeRedDragon:
projectileThing = Thing::_explFireBall;
break;
default:
break;
} /* BUG0_13 The game may crash when 'Lord Order' or 'Grey Lord' cast spells. This cannot happen with the original dungeons as they do not contain any groups of these types. 'Lord Order' and 'Grey Lord' creatures can cast spells (attack range > 1) but no projectile type is defined for them in the code. If these creatures are present in a dungeon they will cast projectiles containing undefined things because the variable is not initialized */
int16 kineticEnergy = (creatureInfo->_attack >> 2) + 1;
kineticEnergy += _vm->getRandomNumber(kineticEnergy);
kineticEnergy += _vm->getRandomNumber(kineticEnergy);
_vm->_sound->requestPlay(kDMSoundIndexSpell, mapX, mapY, kDMSoundModePlayImmediately);
_vm->_projexpl->createProjectile(projectileThing, mapX, mapY, targetCell, (Direction)_currGroupPrimaryDirToParty, CLIP<byte>(20, kineticEnergy, 255), creatureInfo->_dexterity, 8);
} else {
int16 championIndex;
if (getFlag(creatureInfo->_attributes, kDMCreatureMaskAttackAnyChamp)) {
championIndex = _vm->getRandomNumber(4);
int cpt;
for (cpt = 0; (cpt < 4) && !_vm->_championMan->_champions[championIndex]._currHealth; cpt++)
championIndex = _vm->turnDirRight(championIndex);
if (cpt == 4)
return false;
} else {
championIndex = _vm->_championMan->getTargetChampionIndex(mapX, mapY, targetCell);
if (championIndex < 0)
return false;
}
if (creatureType == kDMCreatureTypeGiggler)
stealFromChampion(group, championIndex);
else {
int16 damage = getChampionDamage(group, championIndex) + 1;
Champion *damagedChampion = &_vm->_championMan->_champions[championIndex];
if (damage > damagedChampion->_maximumDamageReceived) {
damagedChampion->_maximumDamageReceived = damage;
damagedChampion->_directionMaximumDamageReceived = _vm->returnOppositeDir((Direction)primaryDirectionToParty);
}
}
}
int16 attackSoundOrdinal = creatureInfo->_attackSoundOrdinal;
if (attackSoundOrdinal)
_vm->_sound->requestPlay(creatureAttackSounds[--attackSoundOrdinal], mapX, mapY, kDMSoundModePlayIfPrioritized);
return true;
}
void GroupMan::setOrderedCellsToAttack(signed char *orderedCellsToAttack, int16 targetMapX, int16 targetMapY, int16 attackerMapX, int16 attackerMapY, uint16 cellSource) {
static signed char attackOrder[8][4] = { // @ G0023_aac_Graphic562_OrderedCellsToAttack
{0, 1, 3, 2}, /* Attack South from position Northwest or Southwest */
{1, 0, 2, 3}, /* Attack South from position Northeast or Southeast */
{1, 2, 0, 3}, /* Attack West from position Northwest or Northeast */
{2, 1, 3, 0}, /* Attack West from position Southeast or Southwest */
{3, 2, 0, 1}, /* Attack North from position Northwest or Southwest */
{2, 3, 1, 0}, /* Attack North from position Southeast or Northeast */
{0, 3, 1, 2}, /* Attack East from position Northwest or Northeast */
{3, 0, 2, 1} /* Attack East from position Southeast or Southwest */
};
uint16 orderedCellsToAttackIndex = getDirsWhereDestIsVisibleFromSource(targetMapX, targetMapY, attackerMapX, attackerMapY) << 1;
if (!(orderedCellsToAttackIndex & 0x0002))
cellSource++;
orderedCellsToAttackIndex += (cellSource >> 1) & 0x0001;
for (uint16 i = 0; i < 4; ++i)
orderedCellsToAttack[i] = attackOrder[orderedCellsToAttackIndex][i];
}
void GroupMan::stealFromChampion(Group *group, uint16 championIndex) {
static unsigned char G0394_auc_StealFromSlotIndices[8]; /* Initialized with 0 bytes by C loader */
bool objectStolen = false;
Champion *champion = &_vm->_championMan->_champions[championIndex];
int16 percentage = 100 - _vm->_championMan->getDexterity(champion);
uint16 slotIdx = _vm->getRandomNumber(8);
while ((percentage > 0) && !_vm->_championMan->isLucky(champion, percentage)) {
uint16 stealFromSlotIndex = G0394_auc_StealFromSlotIndices[slotIdx];
if (stealFromSlotIndex == kDMSlotBackpackLine1_1)
stealFromSlotIndex += _vm->getRandomNumber(17); /* Select a random slot in the backpack */
Thing slotThing = champion->_slots[stealFromSlotIndex];
if ((slotThing != Thing::_none)) {
objectStolen = true;
slotThing = _vm->_championMan->getObjectRemovedFromSlot(championIndex, stealFromSlotIndex);
if (group->_slot == Thing::_endOfList) {
group->_slot = slotThing;
/* BUG0_12 An object is cloned and appears at two different locations in the dungeon and/or inventory. The game may crash when interacting with this object. If a Giggler with no possessions steals an object that was previously in a chest and was not the last object in the chest then the objects that followed it are cloned. In the chest, the object is part of a linked list of objects that is not reset when the object is removed from the chest and placed in the inventory (but not in the dungeon), nor when it is stolen and added as the first Giggler possession. If the Giggler already has a possession before stealing the object then this does not create a cloned object.
The following statement is missing: L0394_T_Thing->Next = Thing::_endOfList;
This creates cloned things if L0394_T_Thing->Next is not Thing::_endOfList which is the case when the object comes from a chest in which it was not the last object */
} else {
_vm->_dungeonMan->linkThingToList(slotThing, group->_slot, kDMMapXNotOnASquare, 0);
}
_vm->_championMan->drawChampionState((ChampionIndex)championIndex);
}
++slotIdx;
slotIdx &= 0x0007;
percentage -= 20;
}
if (!_vm->getRandomNumber(8) || (objectStolen && _vm->getRandomNumber(2))) {
_activeGroups[group->getActiveGroupIndex()]._delayFleeingFromTarget = _vm->getRandomNumber(64) + 20;
group->setBehaviour(kDMBehaviorFlee);
}
}
int16 GroupMan::getChampionDamage(Group *group, uint16 champIndex) {
unsigned char allowedWoundMasks[4] = {32, 16, 8, 4}; // @ G0024_auc_Graphic562_WoundProbabilityIndexToWoundMask
Champion *curChampion = &_vm->_championMan->_champions[champIndex];
if (champIndex >= _vm->_championMan->_partyChampionCount)
return 0;
if (!curChampion->_currHealth)
return 0;
if (_vm->_championMan->_partyIsSleeping)
_vm->_championMan->wakeUp();
int16 doubledMapDifficulty = _vm->_dungeonMan->_currMap->_difficulty << 1;
CreatureInfo creatureInfo = _vm->_dungeonMan->_creatureInfos[group->_type];
_vm->_championMan->addSkillExperience(champIndex, kDMSkillParry, creatureInfo.getExperience());
if (_vm->_championMan->_partyIsSleeping || (((_vm->_championMan->getDexterity(curChampion) < (_vm->getRandomNumber(32) + creatureInfo._dexterity + doubledMapDifficulty - 16)) || !_vm->getRandomNumber(4)) && !_vm->_championMan->isLucky(curChampion, 60))) {
uint16 allowedWound;
uint16 woundTest = _vm->getRandomNumber(65536);
if (woundTest & 0x0070) {
woundTest &= 0x000F;
uint16 woundProbabilities = creatureInfo._woundProbabilities;
uint16 woundProbabilityIndex;
for (woundProbabilityIndex = 0; woundTest > (woundProbabilities & 0x000F); woundProbabilityIndex++) {
woundProbabilities >>= 4;
}
allowedWound = allowedWoundMasks[woundProbabilityIndex];
} else
allowedWound = woundTest & 0x0001; /* 0 (Ready hand) or 1 (action hand) */
int16 attack = (_vm->getRandomNumber(16) + creatureInfo._attack + doubledMapDifficulty) - (_vm->_championMan->getSkillLevel(champIndex, kDMSkillParry) << 1);
if (attack <= 1) {
if (_vm->getRandomNumber(2))
return 0;
attack = _vm->getRandomNumber(4) + 2;
}
attack >>= 1;
attack += _vm->getRandomNumber(attack) + _vm->getRandomNumber(4);
attack += _vm->getRandomNumber(attack);
attack >>= 2;
attack += _vm->getRandomNumber(4) + 1;
if (_vm->getRandomNumber(2))
attack -= _vm->getRandomNumber((attack >> 1) + 1) - 1;
int16 damage = _vm->_championMan->addPendingDamageAndWounds_getDamage(champIndex, attack, allowedWound, creatureInfo._attackType);
if (damage) {
_vm->_sound->requestPlay(kDMSoundIndexChampion0Damaged + champIndex, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, kDMSoundModePlayOneTickLater);
uint16 poisonAttack = creatureInfo._poisonAttack;
if (poisonAttack && _vm->getRandomNumber(2)) {
poisonAttack = _vm->_championMan->getStatisticAdjustedAttack(curChampion, kDMStatVitality, poisonAttack);
if (poisonAttack >= 0)
_vm->_championMan->championPoison(champIndex, poisonAttack);
}
return damage;
}
}
return 0;
}
void GroupMan::dropMovingCreatureFixedPossession(Thing thing, int16 mapX, int16 mapY) {
if (_dropMovingCreatureFixedPossCellCount) {
Group *group = (Group *)_vm->_dungeonMan->getThingData(thing);
CreatureType creatureType = group->_type;
while (_dropMovingCreatureFixedPossCellCount) {
dropCreatureFixedPossessions(creatureType, mapX, mapY, _dropMovingCreatureFixedPossessionsCell[--_dropMovingCreatureFixedPossCellCount], kDMSoundModePlayOneTickLater);
}
}
}
void GroupMan::startWandering(int16 mapX, int16 mapY) {
Group *L0332_ps_Group = (Group *)_vm->_dungeonMan->getThingData(groupGetThing(mapX, mapY));
if (L0332_ps_Group->getBehaviour() >= kDMBehaviorUnknown4) {
L0332_ps_Group->setBehaviour(kDMBehaviorWander);
}
TimelineEvent nextEvent;
nextEvent._mapTime = _vm->setMapAndTime(_vm->_dungeonMan->_currMapIndex, (_vm->_gameTime + 1));
nextEvent._type = k37_TMEventTypeUpdateBehaviourGroup;
nextEvent._priority = kDMMovementTicksImmobile - _vm->_dungeonMan->_creatureInfos[L0332_ps_Group->_type]._movementTicks; /* The fastest creatures (with small MovementTicks value) get higher event priority */
nextEvent._Cu._ticks = 0;
nextEvent._Bu._location._mapX = mapX;
nextEvent._Bu._location._mapY = mapY;
_vm->_timeline->addEventGetEventIndex(&nextEvent);
}
void GroupMan::addActiveGroup(Thing thing, int16 mapX, int16 mapY) {
ActiveGroup *activeGroup = _activeGroups;
int16 activeGroupIndex = 0;
while (activeGroup->_groupThingIndex >= 0) {
if (++activeGroupIndex >= _maxActiveGroupCount)
return;
activeGroup++;
}
_currActiveGroupCount++;
activeGroup->_groupThingIndex = (thing).getIndex();
Group *curGroup = (Group *)(_vm->_dungeonMan->_thingData[kDMThingTypeGroup] +
_vm->_dungeonMan->_thingDataWordCount[kDMThingTypeGroup] * activeGroup->_groupThingIndex);
activeGroup->_cells = curGroup->_cells;
curGroup->getActiveGroupIndex() = activeGroupIndex;
activeGroup->_priorMapX = activeGroup->_homeMapX = mapX;
activeGroup->_priorMapY = activeGroup->_homeMapY = mapY;
activeGroup->_lastMoveTime = _vm->_gameTime - 127;
uint16 creatureIndex = curGroup->getCount();
do {
activeGroup->_directions = (Direction)getGroupValueUpdatedWithCreatureValue(activeGroup->_directions, creatureIndex, curGroup->getDir());
activeGroup->_aspect[creatureIndex] = 0;
} while (creatureIndex--);
getCreatureAspectUpdateTime(activeGroup, kDMWholeCreatureGroup, false);
}
void GroupMan::removeActiveGroup(uint16 activeGroupIndex) {
if ((activeGroupIndex > _maxActiveGroupCount) || (_activeGroups[activeGroupIndex]._groupThingIndex < 0))
return;
ActiveGroup *activeGroup = &_activeGroups[activeGroupIndex];
Group *group = &((Group *)_vm->_dungeonMan->_thingData[kDMThingTypeGroup])[activeGroup->_groupThingIndex];
_currActiveGroupCount--;
group->_cells = activeGroup->_cells;
group->setDir(_vm->normalizeModulo4(activeGroup->_directions));
if (group->getBehaviour() >= kDMBehaviorUnknown4) {
group->setBehaviour(kDMBehaviorWander);
}
activeGroup->_groupThingIndex = -1;
}
void GroupMan::removeAllActiveGroups() {
for (int16 idx = 0; _currActiveGroupCount > 0; idx++) {
if (_activeGroups[idx]._groupThingIndex >= 0) {
removeActiveGroup(idx);
}
}
}
void GroupMan::addAllActiveGroups() {
byte *curSquare = _vm->_dungeonMan->_currMapData[0];
Thing *squareCurThing = &_vm->_dungeonMan->_squareFirstThings[_vm->_dungeonMan->_currMapColCumulativeSquareFirstThingCount[0]];
for (uint16 mapX = 0; mapX < _vm->_dungeonMan->_currMapWidth; mapX++) {
for (uint16 mapY = 0; mapY < _vm->_dungeonMan->_currMapHeight; mapY++) {
if (getFlag(*curSquare++, kDMSquareMaskThingListPresent)) {
Thing curThing = *squareCurThing++;
do {
if (curThing.getType() == kDMThingTypeGroup) {
groupDeleteEvents(mapX, mapY);
addActiveGroup(curThing, mapX, mapY);
startWandering(mapX, mapY);
break;
}
curThing = _vm->_dungeonMan->getNextThing(curThing);
} while (curThing != Thing::_endOfList);
}
}
}
}
Thing GroupMan::groupGetGenerated(CreatureType creatureType, int16 healthMultiplier, uint16 creatureCount, Direction dir, int16 mapX, int16 mapY) {
Thing groupThing = _vm->_dungeonMan->getUnusedThing(kDMThingTypeGroup);
if (((_currActiveGroupCount >= (_maxActiveGroupCount - 5)) && (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex))
|| (groupThing == Thing::_none)) {
return Thing::_none;
}
Group *group = (Group *)_vm->_dungeonMan->getThingData(groupThing);
group->_slot = Thing::_endOfList;
group->setDoNotDiscard(false);
group->setDir(dir);
group->setCount(creatureCount);
bool severalCreaturesInGroup = creatureCount;
uint16 cell = 0;
uint16 groupCells = 0;
if (severalCreaturesInGroup)
cell = _vm->getRandomNumber(4);
else
groupCells = kDMCreatureTypeSingleCenteredCreature;
group->_type = creatureType;
CreatureInfo *creatureInfo = &_vm->_dungeonMan->_creatureInfos[group->_type];
uint16 baseHealth = creatureInfo->_baseHealth;
do {
group->_health[creatureCount] = (baseHealth * healthMultiplier) + _vm->getRandomNumber((baseHealth >> 2) + 1);
if (severalCreaturesInGroup) {
groupCells = getGroupValueUpdatedWithCreatureValue(groupCells, creatureCount, cell++);
if (getFlag(creatureInfo->_attributes, kDMCreatureMaskSize) == kDMCreatureSizeHalf)
cell++;
cell &= 0x0003;
}
} while (creatureCount--);
group->_cells = groupCells;
if (_vm->_moveSens->getMoveResult(groupThing, kDMMapXNotOnASquare, 0, mapX, mapY)) {
/* If F0267_MOVE_GetMoveResult_CPSCE returns true then the group was either killed by a projectile
impact (in which case the thing data was marked as unused) or the party is on the destination
square and an event is created to move the creature into the dungeon later
(in which case the thing is referenced in the event) */
return Thing::_none;
}
_vm->_sound->requestPlay(kDMSoundIndexBuzz, mapX, mapY, kDMSoundModePlayIfPrioritized);
return groupThing;
}
bool GroupMan::isSquareACorridorTeleporterPitOrDoor(int16 mapX, int16 mapY) {
int16 squareType = Square(_vm->_dungeonMan->getSquare(mapX, mapY)).getType();
return ((squareType == kDMElementTypeCorridor) || (squareType == kDMElementTypeTeleporter)
|| (squareType == kDMElementTypePit) || (squareType == kDMElementTypeDoor));
}
int16 GroupMan::getMeleeTargetCreatureOrdinal(int16 groupX, int16 groupY, int16 partyX, int16 partyY, uint16 champCell) {
Thing groupThing = groupGetThing(groupX, groupY);
if (groupThing == Thing::_endOfList)
return 0;
Group *group = (Group *)_vm->_dungeonMan->getThingData(groupThing);
signed char orderedCellsToAttack[4];
setOrderedCellsToAttack(orderedCellsToAttack, groupX, groupY, partyX, partyY, champCell);
uint16 counter = 0;
for (;;) { /*_Infinite loop_*/
int16 creatureOrdinal = getCreatureOrdinalInCell(group, orderedCellsToAttack[counter]);
if (creatureOrdinal)
return creatureOrdinal;
counter++;
}
}
int16 GroupMan::getMeleeActionDamage(Champion *champ, int16 champIndex, Group *group, int16 creatureIndex, int16 mapX, int16 mapY, uint16 actionHitProbability, uint16 actionDamageFactor, int16 skillIndex) {
int16 L0565_i_Damage = 0;
int16 L0566_i_Damage = 0;
int16 L0568_i_Defense;
int16 L0569_i_Outcome;
if (champIndex >= _vm->_championMan->_partyChampionCount)
return 0;
if (!champ->_currHealth)
return 0;
int16 doubledMapDifficulty = _vm->_dungeonMan->_currMap->_difficulty << 1;
CreatureInfo *creatureInfo = &_vm->_dungeonMan->_creatureInfos[group->_type];
int16 actionHandObjectIconIndex = _vm->_objectMan->getIconIndex(champ->_slots[kDMSlotActionHand]);
bool actionHitsNonMaterialCreatures = getFlag(actionHitProbability, kDMActionMaskHitNonMaterialCreatures);
if (actionHitsNonMaterialCreatures)
clearFlag(actionHitProbability, kDMActionMaskHitNonMaterialCreatures);
if ((!getFlag(creatureInfo->_attributes, kDMCreatureMaskNonMaterial) || actionHitsNonMaterialCreatures) &&
((_vm->_championMan->getDexterity(champ) > (_vm->getRandomNumber(32) + creatureInfo->_dexterity + doubledMapDifficulty - 16)) ||
(!_vm->getRandomNumber(4)) ||
(_vm->_championMan->isLucky(champ, 75 - actionHitProbability)))) {
L0565_i_Damage = _vm->_championMan->getStrength(champIndex, kDMSlotActionHand);
if (!(L0565_i_Damage))
goto T0231009;
L0565_i_Damage += _vm->getRandomNumber((L0565_i_Damage >> 1) + 1);
L0565_i_Damage = ((long)L0565_i_Damage * (long)actionDamageFactor) >> 5;
L0568_i_Defense = _vm->getRandomNumber(32) + creatureInfo->_defense + doubledMapDifficulty;
if (actionHandObjectIconIndex == kDMIconIndiceWeaponDiamondEdge)
L0568_i_Defense -= L0568_i_Defense >> 2;
else if (actionHandObjectIconIndex == kDMIconIndiceWeaponHardcleaveExecutioner)
L0568_i_Defense -= L0568_i_Defense >> 3;
L0565_i_Damage += _vm->getRandomNumber(32) - L0568_i_Defense;
L0566_i_Damage = L0565_i_Damage;
if (L0566_i_Damage <= 1) {
T0231009:
L0565_i_Damage = _vm->getRandomNumber(4);
if (!L0565_i_Damage)
goto T0231015;
L0565_i_Damage++;
L0566_i_Damage += _vm->getRandomNumber(16);
if ((L0566_i_Damage > 0) || (_vm->getRandomNumber(2))) {
L0565_i_Damage += _vm->getRandomNumber(4);
if (!_vm->getRandomNumber(4))
L0565_i_Damage += MAX(0, L0566_i_Damage + _vm->getRandomNumber(16));
}
}
L0565_i_Damage >>= 1;
L0565_i_Damage += _vm->getRandomNumber(L0565_i_Damage) + _vm->getRandomNumber(4);
L0565_i_Damage += _vm->getRandomNumber(L0565_i_Damage);
L0565_i_Damage >>= 2;
L0565_i_Damage += _vm->getRandomNumber(4) + 1;
if ((actionHandObjectIconIndex == kDMIconIndiceWeaponVorpalBlade)
&& !getFlag(creatureInfo->_attributes, kDMCreatureMaskNonMaterial)
&& !(L0565_i_Damage >>= 1))
goto T0231015;
if (_vm->getRandomNumber(64) < _vm->_championMan->getSkillLevel(champIndex, skillIndex))
L0565_i_Damage += L0565_i_Damage + 10;
L0569_i_Outcome = groupGetDamageCreatureOutcome(group, creatureIndex, mapX, mapY, L0565_i_Damage, true);
_vm->_championMan->addSkillExperience(champIndex, skillIndex, (L0565_i_Damage * creatureInfo->getExperience() >> 4) + 3);
_vm->_championMan->decrementStamina(champIndex, _vm->getRandomNumber(4) + 4);
goto T0231016;
}
T0231015:
L0565_i_Damage = 0;
L0569_i_Outcome = kDMKillOutcomeNoCreaturesInGroup;
_vm->_championMan->decrementStamina(champIndex, _vm->getRandomNumber(2) + 2);
T0231016:
_vm->_championMan->drawChampionState((ChampionIndex)champIndex);
if (L0569_i_Outcome != kDMKillOutcomeAllCreaturesInGroup) {
processEvents29to41(mapX, mapY, kM1_TMEventTypeCreateReactionEvent31ParyIsAdjacent, 0);
}
return L0565_i_Damage;
}
void GroupMan::fluxCageAction(int16 mapX, int16 mapY) {
ElementType squareType = _vm->_dungeonMan->getSquare(mapX, mapY).getType();
if ((squareType == kDMElementTypeWall) || (squareType == kDMElementTypeStairs))
return;
Thing unusedThing = _vm->_dungeonMan->getUnusedThing(kDMThingTypeExplosion);
if (unusedThing == Thing::_none)
return;
_vm->_dungeonMan->linkThingToList(unusedThing, Thing(0), mapX, mapY);
(((Explosion *)_vm->_dungeonMan->_thingData[kDMThingTypeExplosion])[unusedThing.getIndex()]).setType(kDMExplosionTypeFluxcage);
TimelineEvent newEvent;
newEvent._mapTime = _vm->setMapAndTime(_vm->_dungeonMan->_currMapIndex, _vm->_gameTime + 100);
newEvent._type = k24_TMEventTypeRemoveFluxcage;
newEvent._priority = 0;
newEvent._Cu._slot = unusedThing.toUint16();
newEvent._Bu._location._mapX = mapX;
newEvent._Bu._location._mapY = mapY;
newEvent._Bu._location._mapY = mapY;
_vm->_timeline->addEventGetEventIndex(&newEvent);
int16 fluxcageCount;
if (isLordChaosOnSquare(mapX, mapY - 1)) {
mapY--;
fluxcageCount = isFluxcageOnSquare(mapX + 1, mapY);
fluxcageCount += isFluxcageOnSquare(mapX, mapY - 1) + isFluxcageOnSquare(mapX - 1, mapY);
} else if (isLordChaosOnSquare(mapX - 1, mapY)) {
mapX--;
fluxcageCount = isFluxcageOnSquare(mapX, mapY + 1);
fluxcageCount += isFluxcageOnSquare(mapX, mapY - 1) + isFluxcageOnSquare(mapX - 1, mapY);
} else if (isLordChaosOnSquare(mapX + 1, mapY)) {
mapX++;
fluxcageCount = isFluxcageOnSquare(mapX, mapY - 1);
fluxcageCount += isFluxcageOnSquare(mapX, mapY + 1) + isFluxcageOnSquare(mapX + 1, mapY);
} else if (isLordChaosOnSquare(mapX, mapY + 1)) {
mapY++;
fluxcageCount = isFluxcageOnSquare(mapX - 1, mapY);
fluxcageCount += isFluxcageOnSquare(mapX, mapY + 1) + isFluxcageOnSquare(mapX + 1, mapY);
} else
fluxcageCount = 0;
if (fluxcageCount == 2)
processEvents29to41(mapX, mapY, kM3_TMEventTypeCreateReactionEvent29DangerOnSquare, 0);
}
uint16 GroupMan::isLordChaosOnSquare(int16 mapX, int16 mapY) {
Thing thing = groupGetThing(mapX, mapY);
if (thing == Thing::_endOfList)
return 0;
Group *group = (Group *)_vm->_dungeonMan->getThingData(thing);
if (group->_type == kDMCreatureTypeLordChaos)
return thing.toUint16();
return 0;
}
bool GroupMan::isFluxcageOnSquare(int16 mapX, int16 mapY) {
ElementType squareType = _vm->_dungeonMan->getSquare(mapX, mapY).getType();
if ((squareType == kDMElementTypeWall) || (squareType == kDMElementTypeStairs))
return false;
Thing thing = _vm->_dungeonMan->getSquareFirstThing(mapX, mapY);
while (thing != Thing::_endOfList) {
if ((thing.getType() == kDMThingTypeExplosion) && (((Explosion *)_vm->_dungeonMan->_thingData[kDMThingTypeExplosion])[thing.getIndex()].getType() == kDMExplosionTypeFluxcage))
return true;
thing = _vm->_dungeonMan->getNextThing(thing);
}
return false;
}
void GroupMan::fuseAction(uint16 mapX, uint16 mapY) {
if ((mapX >= _vm->_dungeonMan->_currMapWidth) || (mapY >= _vm->_dungeonMan->_currMapHeight))
return;
_vm->_projexpl->createExplosion(Thing::_explHarmNonMaterial, 255, mapX, mapY, kDMCreatureTypeSingleCenteredCreature); /* BUG0_17 The game crashes after the Fuse action is performed while looking at a wall on a map boundary. An explosion thing is created on the square in front of the party but there is no check to ensure the square coordinates are in the map bounds. This corrupts a memory location and leads to a game crash */
Thing lordChaosThing = Thing(isLordChaosOnSquare(mapX, mapY));
if (lordChaosThing.toUint16()) {
bool isFluxcages[4];
isFluxcages[0] = isFluxcageOnSquare(mapX - 1, mapY);
isFluxcages[1] = isFluxcageOnSquare(mapX + 1, mapY);
isFluxcages[2] = isFluxcageOnSquare(mapX, mapY - 1);
isFluxcages[3] = isFluxcageOnSquare(mapX, mapY + 1);
uint16 fluxcageCount = 0;
for (int i = 0; i < 4; i++) {
if (isFluxcages[i])
fluxcageCount++;
}
while (fluxcageCount++ < 4) {
int16 destMapX = mapX;
int16 destMapY = mapY;
uint16 fluxcageIndex = _vm->getRandomNumber(4);
for (uint16 i = 5; --i; fluxcageIndex = _vm->turnDirRight(fluxcageIndex)) {
if (!isFluxcages[fluxcageIndex]) {
isFluxcages[fluxcageIndex] = true;
switch (fluxcageIndex) {
case 0:
destMapX--;
break;
case 1:
destMapX++;
break;
case 2:
destMapY--;
break;
case 3:
destMapY++;
}
break;
}
}
if (isSquareACorridorTeleporterPitOrDoor(destMapX, destMapY)) {
if (!_vm->_moveSens->getMoveResult(lordChaosThing, mapX, mapY, destMapX, destMapY))
startWandering(destMapX, destMapY);
return;
}
}
_vm->fuseSequence();
}
}
void GroupMan::saveActiveGroupPart(Common::OutSaveFile *file) {
for (uint16 i = 0; i < _maxActiveGroupCount; ++i) {
ActiveGroup *group = &_activeGroups[i];
file->writeUint16BE(group->_groupThingIndex);
file->writeUint16BE(group->_directions);
file->writeByte(group->_cells);
file->writeByte(group->_lastMoveTime);
file->writeByte(group->_delayFleeingFromTarget);
file->writeByte(group->_targetMapX);
file->writeByte(group->_targetMapY);
file->writeByte(group->_priorMapX);
file->writeByte(group->_priorMapY);
file->writeByte(group->_homeMapX);
file->writeByte(group->_homeMapY);
for (uint16 j = 0; j < 4; ++j)
file->writeByte(group->_aspect[j]);
}
}
void GroupMan::loadActiveGroupPart(Common::InSaveFile *file) {
for (uint16 i = 0; i < _maxActiveGroupCount; ++i) {
ActiveGroup *group = &_activeGroups[i];
group->_groupThingIndex = file->readUint16BE();
group->_directions = (Direction)file->readUint16BE();
group->_cells = file->readByte();
group->_lastMoveTime = file->readByte();
group->_delayFleeingFromTarget = file->readByte();
group->_targetMapX = file->readByte();
group->_targetMapY = file->readByte();
group->_priorMapX = file->readByte();
group->_priorMapY = file->readByte();
group->_homeMapX = file->readByte();
group->_homeMapY = file->readByte();
for (uint16 j = 0; j < 4; ++j)
group->_aspect[j] = file->readByte();
}
}
}
|