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
|
/* 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/movesens.h"
#include "dm/champion.h"
#include "dm/inventory.h"
#include "dm/dungeonman.h"
#include "dm/objectman.h"
#include "dm/timeline.h"
#include "dm/group.h"
#include "dm/projexpl.h"
#include "dm/text.h"
#include "dm/sounds.h"
namespace DM {
MovesensMan::MovesensMan(DMEngine *vm) : _vm(vm) {
_moveResultMapX = 0;
_moveResultMapY = 0;
_moveResultMapIndex = 0;
_moveResultDir = 0;
_moveResultCell = 0;
_useRopeToClimbDownPit = false;
_sensorRotationEffect = 0;
_sensorRotationEffMapX = 0;
_sensorRotationEffMapY = 0;
_sensorRotationEffCell = 0;
}
bool MovesensMan::sensorIsTriggeredByClickOnWall(int16 mapX, int16 mapY, uint16 cellParam) {
bool atLeastOneSensorWasTriggered = false;
Thing leaderHandObject = _vm->_championMan->_leaderHandObject;
int16 sensorCountToProcessPerCell[4];
for (int16 i = kDMCellNorthWest; i < kDMCellSouthWest + 1; i++)
sensorCountToProcessPerCell[i] = 0;
Thing squareFirstThing = _vm->_dungeonMan->getSquareFirstThing(mapX, mapY);
Thing thingBeingProcessed = squareFirstThing;
while (thingBeingProcessed != Thing::_endOfList) {
ThingType thingType = thingBeingProcessed.getType();
if (thingType == kDMThingTypeSensor)
sensorCountToProcessPerCell[thingBeingProcessed.getCell()]++;
else if (thingType >= kDMThingTypeGroup)
break;
thingBeingProcessed = _vm->_dungeonMan->getNextThing(thingBeingProcessed);
}
for (thingBeingProcessed = squareFirstThing; thingBeingProcessed != Thing::_endOfList; thingBeingProcessed = _vm->_dungeonMan->getNextThing(thingBeingProcessed)) {
Thing lastProcessedThing = thingBeingProcessed;
uint16 ProcessedThingType = thingBeingProcessed.getType();
if (ProcessedThingType == kDMThingTypeSensor) {
int16 cellIdx = thingBeingProcessed.getCell();
sensorCountToProcessPerCell[cellIdx]--;
Sensor *currentSensor = (Sensor *)_vm->_dungeonMan->getThingData(thingBeingProcessed);
SensorType processedSensorType = currentSensor->getType();
if (processedSensorType == kDMSensorDisabled)
continue;
if ((_vm->_championMan->_leaderIndex == kDMChampionNone) && (processedSensorType != kDMSensorWallChampionPortrait))
continue;
if (cellIdx != cellParam)
continue;
bool doNotTriggerSensor;
int16 sensorData = currentSensor->getData();
SensorEffect sensorEffect = (SensorEffect)currentSensor->getAttrEffectA();
switch (processedSensorType) {
case kDMSensorWallOrnClick:
doNotTriggerSensor = false;
if (currentSensor->getAttrEffectA() == kDMSensorEffectHold)
continue;
break;
case kDMSensorWallOrnClickWithAnyObj:
doNotTriggerSensor = (_vm->_championMan->_leaderEmptyHanded != currentSensor->getAttrRevertEffectA());
break;
case kDMSensorWallOrnClickWithSpecObjRemovedSensor:
case kDMSensorWallOrnClickWithSpecObjRemovedRotateSensors:
if (sensorCountToProcessPerCell[cellIdx]) /* If the sensor is not the last one of its type on the cell */
continue;
// No break on purpose
case kDMSensorWallOrnClickWithSpecObj:
case kDMSensorWallOrnClickWithSpecObjRemoved:
doNotTriggerSensor = ((sensorData == _vm->_objectMan->getObjectType(leaderHandObject)) == currentSensor->getAttrRevertEffectA());
if (!doNotTriggerSensor && (processedSensorType == kDMSensorWallOrnClickWithSpecObjRemovedSensor)) {
if (lastProcessedThing == thingBeingProcessed) /* If the sensor is the only one of its type on the cell */
break;
Sensor *lastSensor = (Sensor *)_vm->_dungeonMan->getThingData(lastProcessedThing);
lastSensor->setNextThing(currentSensor->getNextThing());
currentSensor->setNextThing(Thing::_none);
thingBeingProcessed = lastProcessedThing;
}
if (!doNotTriggerSensor && (processedSensorType == kDMSensorWallOrnClickWithSpecObjRemovedRotateSensors))
triggerLocalEffect(kDMSensorEffectToggle, mapX, mapY, cellIdx); /* This will cause a rotation of the sensors at the specified cell on the specified square after all sensors have been processed */
break;
case kDMSensorWallObjGeneratorRotateSensors:
if (sensorCountToProcessPerCell[cellIdx]) /* If the sensor is not the last one of its type on the cell */
continue;
doNotTriggerSensor = !_vm->_championMan->_leaderEmptyHanded;
if (!doNotTriggerSensor)
triggerLocalEffect(kDMSensorEffectToggle, mapX, mapY, cellIdx); /* This will cause a rotation of the sensors at the specified cell on the specified square after all sensors have been processed */
break;
case kDMSensorWallSingleObjStorageRotateSensors:
if (_vm->_championMan->_leaderEmptyHanded) {
leaderHandObject = getObjectOfTypeInCell(mapX, mapY, cellIdx, sensorData);
if (leaderHandObject == Thing::_none)
continue;
_vm->_dungeonMan->unlinkThingFromList(leaderHandObject, Thing(0), mapX, mapY);
_vm->_championMan->putObjectInLeaderHand(leaderHandObject, true);
} else {
if ((_vm->_objectMan->getObjectType(leaderHandObject) != sensorData) || (getObjectOfTypeInCell(mapX, mapY, cellIdx, sensorData) != Thing::_none))
continue;
_vm->_championMan->getObjectRemovedFromLeaderHand();
_vm->_dungeonMan->linkThingToList(_vm->thingWithNewCell(leaderHandObject, cellIdx), Thing(0), mapX, mapY);
leaderHandObject = Thing::_none;
}
triggerLocalEffect(kDMSensorEffectToggle, mapX, mapY, cellIdx); /* This will cause a rotation of the sensors at the specified cell on the specified square after all sensors have been processed */
if ((sensorEffect == kDMSensorEffectHold) && !_vm->_championMan->_leaderEmptyHanded)
doNotTriggerSensor = true;
else
doNotTriggerSensor = false;
break;
case kDMSensorWallObjExchanger: {
if (sensorCountToProcessPerCell[cellIdx]) /* If the sensor is not the last one of its type on the cell */
continue;
Thing thingOnSquare = _vm->_dungeonMan->getSquareFirstObject(mapX, mapY);
if ((_vm->_objectMan->getObjectType(leaderHandObject) != sensorData) || (thingOnSquare == Thing::_none))
continue;
_vm->_dungeonMan->unlinkThingFromList(thingOnSquare, Thing(0), mapX, mapY);
_vm->_championMan->getObjectRemovedFromLeaderHand();
_vm->_dungeonMan->linkThingToList(_vm->thingWithNewCell(leaderHandObject, cellIdx), Thing(0), mapX, mapY);
_vm->_championMan->putObjectInLeaderHand(thingOnSquare, true);
doNotTriggerSensor = false;
}
break;
case kDMSensorWallChampionPortrait:
_vm->_championMan->addCandidateChampionToParty(sensorData);
continue;
break;
default:
continue;
break;
}
if (sensorEffect == kDMSensorEffectHold) {
sensorEffect = doNotTriggerSensor ? kDMSensorEffectClear : kDMSensorEffectSet;
doNotTriggerSensor = false;
}
if (!doNotTriggerSensor) {
atLeastOneSensorWasTriggered = true;
if (currentSensor->getAttrAudibleA())
_vm->_sound->requestPlay(k01_soundSWITCH, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, kDMSoundModePlayIfPrioritized);
if (!_vm->_championMan->_leaderEmptyHanded && ((processedSensorType == kDMSensorWallOrnClickWithSpecObjRemoved) || (processedSensorType == kDMSensorWallOrnClickWithSpecObjRemovedRotateSensors) || (processedSensorType == kDMSensorWallOrnClickWithSpecObjRemovedSensor))) {
Thing *leaderThing = (Thing *)_vm->_dungeonMan->getThingData(leaderHandObject);
*leaderThing = Thing::_none;
_vm->_championMan->getObjectRemovedFromLeaderHand();
leaderHandObject = Thing::_none;
} else if (_vm->_championMan->_leaderEmptyHanded
&& (processedSensorType == kDMSensorWallObjGeneratorRotateSensors)) {
leaderHandObject = _vm->_dungeonMan->getObjForProjectileLaucherOrObjGen(sensorData);
if (leaderHandObject != Thing::_none)
_vm->_championMan->putObjectInLeaderHand(leaderHandObject, true);
}
triggerEffect(currentSensor, sensorEffect, mapX, mapY, cellIdx);
}
continue;
}
if (ProcessedThingType >= kDMThingTypeGroup)
break;
}
processRotationEffect();
return atLeastOneSensorWasTriggered;
}
bool MovesensMan::getMoveResult(Thing thing, int16 mapX, int16 mapY, int16 destMapX, int16 destMapY) {
ThingType thingType = kDMThingTypeParty;
int16 traversedPitCount = 0;
uint16 moveGroupResult = 0;
uint16 thingCell = 0;
bool thingLevitates = false;
if (thing != Thing::_party) {
thingType = thing.getType();
thingCell = thing.getCell();
thingLevitates = isLevitating(thing);
}
/* If moving the party or a creature on the party map from a dungeon square then check for a projectile impact */
if ((mapX >= 0) && ((thing == Thing::_party) || ((thingType == kDMThingTypeGroup) && (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex)))) {
if (moveIsKilledByProjectileImpact(mapX, mapY, destMapX, destMapY, thing))
return true; /* The specified group thing cannot be moved because it was killed by a projectile impact */
}
uint16 mapIndexSource = 0;
uint16 mapIndexDestination = 0;
bool groupOnPartyMap = false;
bool partySquare = false;
bool audibleTeleporter = false;
if (destMapX >= 0) {
mapIndexSource = mapIndexDestination = _vm->_dungeonMan->_currMapIndex;
groupOnPartyMap = (mapIndexSource == _vm->_dungeonMan->_partyMapIndex) && (mapX >= 0);
uint16 direction = 0;
bool fallKilledGroup = false;
bool drawDungeonViewWhileFalling = false;
bool destinationIsTeleporterTarget = false;
int16 requiredTeleporterScope;
if (thing == Thing::_party) {
_vm->_dungeonMan->_partyMapX = destMapX;
_vm->_dungeonMan->_partyMapY = destMapY;
requiredTeleporterScope = kDMTeleporterScopeObjectsOrParty;
drawDungeonViewWhileFalling = !_vm->_inventoryMan->_inventoryChampionOrdinal && !_vm->_championMan->_partyIsSleeping;
direction = _vm->_dungeonMan->_partyDir;
} else if (thingType == kDMThingTypeGroup)
requiredTeleporterScope = kDMTeleporterScopeCreatures;
else
requiredTeleporterScope = (kDMTeleporterScopeCreatures | kDMTeleporterScopeObjectsOrParty);
if (thingType == kDMThingTypeProjectile) {
Teleporter *L0712_ps_Teleporter = (Teleporter *)_vm->_dungeonMan->getThingData(thing);
_moveResultDir = (_vm->_timeline->_events[((Projectile *)L0712_ps_Teleporter)->_eventIndex])._Cu._projectile.getDir();
}
int16 destinationSquareData = 0;
/* No more than 1000 chained moves at once (in a chain of teleporters and pits for example) */
for (int16 chainedMoveCount = 1000; --chainedMoveCount; ) {
destinationSquareData = _vm->_dungeonMan->_currMapData[destMapX][destMapY];
ElementType destinationSquareType = Square(destinationSquareData).getType();
if (destinationSquareType == (int)kDMElementTypeTeleporter) {
if (!getFlag(destinationSquareData, kDMSquareMaskTeleporterOpen))
break;
Teleporter *teleporter = (Teleporter *)_vm->_dungeonMan->getSquareFirstThingData(destMapX, destMapY);
if ((teleporter->getScope() == kDMTeleporterScopeCreatures) && (thingType != kDMThingTypeGroup))
break;
if ((requiredTeleporterScope != (kDMTeleporterScopeCreatures | kDMTeleporterScopeObjectsOrParty)) && !getFlag(teleporter->getScope(), requiredTeleporterScope))
break;
destinationIsTeleporterTarget = (destMapX == teleporter->getTargetMapX()) && (destMapY == teleporter->getTargetMapY()) && (mapIndexDestination == teleporter->getTargetMapIndex());
destMapX = teleporter->getTargetMapX();
destMapY = teleporter->getTargetMapY();
audibleTeleporter = teleporter->isAudible();
_vm->_dungeonMan->setCurrentMap(mapIndexDestination = teleporter->getTargetMapIndex());
if (thing == Thing::_party) {
_vm->_dungeonMan->_partyMapX = destMapX;
_vm->_dungeonMan->_partyMapY = destMapY;
if (teleporter->isAudible())
_vm->_sound->requestPlay(k17_soundBUZZ, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, kDMSoundModePlayImmediately);
drawDungeonViewWhileFalling = true;
if (teleporter->getAbsoluteRotation())
_vm->_championMan->setPartyDirection(teleporter->getRotation());
else
_vm->_championMan->setPartyDirection(_vm->normalizeModulo4(_vm->_dungeonMan->_partyDir + teleporter->getRotation()));
} else {
if (thingType == kDMThingTypeGroup) {
if (teleporter->isAudible())
_vm->_sound->requestPlay(k17_soundBUZZ, destMapX, destMapY, kDMSoundModePlayIfPrioritized);
moveGroupResult = getTeleporterRotatedGroupResult(teleporter, thing, mapIndexSource);
} else {
if (thingType == kDMThingTypeProjectile)
thing = getTeleporterRotatedProjectileThing(teleporter, thing);
else if (!(teleporter->getAbsoluteRotation()) && (mapX != -2))
thing = _vm->thingWithNewCell(thing, _vm->normalizeModulo4(thing.getCell() + teleporter->getRotation()));
}
}
if (destinationIsTeleporterTarget)
break;
} else {
if ((destinationSquareType == (int)kDMElementTypePit) && !thingLevitates && getFlag(destinationSquareData, kDMSquareMaskPitOpen) && !getFlag(destinationSquareData, kDMSquareMaskPitImaginary)) {
if (drawDungeonViewWhileFalling && !_useRopeToClimbDownPit) {
drawDungeonViewWhileFalling = true;
if (traversedPitCount) {
_vm->_dungeonMan->setCurrentMapAndPartyMap(mapIndexDestination);
_vm->_displayMan->loadCurrentMapGraphics();
}
traversedPitCount++;
_vm->_displayMan->drawDungeon(_vm->_dungeonMan->_partyDir, destMapX, destMapY); /* BUG0_28 When falling through multiple pits the dungeon view is updated to show each traversed map but the graphics used for creatures, wall and floor ornaments may not be correct. The dungeon view is drawn for each map by using the graphics loaded for the source map. Therefore the graphics for creatures, wall and floor ornaments may not look like what they should */
/* BUG0_71 Some timings are too short on fast computers. When the party falls in a series of pits, the dungeon view is refreshed too quickly because the execution speed is not limited */
/* BUG0_01 While drawing creatures the engine will read invalid ACTIVE_GROUP data in _vm->_groupMan->_g375_activeGroups because the data is for the creatures on the source map and not the map being drawn. The only consequence is that creatures may be drawn with incorrect bitmaps and/or directions */
}
mapIndexDestination = _vm->_dungeonMan->getLocationAfterLevelChange(mapIndexDestination, 1, &destMapX, &destMapY);
_vm->_dungeonMan->setCurrentMap(mapIndexDestination);
if (thing == Thing::_party) {
_vm->_dungeonMan->_partyMapX = destMapX;
_vm->_dungeonMan->_partyMapY = destMapY;
if (_vm->_championMan->_partyChampionCount > 0) {
if (_useRopeToClimbDownPit) {
Champion *curChampion = _vm->_championMan->_champions;
for (int16 championIdx = kDMChampionFirst; championIdx < _vm->_championMan->_partyChampionCount; championIdx++, curChampion++) {
if (curChampion->_currHealth)
_vm->_championMan->decrementStamina(championIdx, ((curChampion->_load * 25) / _vm->_championMan->getMaximumLoad(curChampion)) + 1);
}
} else if (_vm->_championMan->getDamagedChampionCount(20, kDMWoundLegs | kDMWoundFeet, kDMAttackTypeSelf))
_vm->_sound->requestPlay(k06_soundSCREAM, _vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, kDMSoundModePlayImmediately);
}
_useRopeToClimbDownPit = false;
} else if (thingType == kDMThingTypeGroup) {
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
uint16 outcome = _vm->_groupMan->getDamageAllCreaturesOutcome((Group *)_vm->_dungeonMan->getThingData(thing), mapX, mapY, 20, false);
_vm->_dungeonMan->setCurrentMap(mapIndexDestination);
fallKilledGroup = (outcome == k2_outcomeKilledAllCreaturesInGroup);
if (fallKilledGroup)
break;
if (outcome == k1_outcomeKilledSomeCreaturesInGroup)
_vm->_groupMan->dropMovingCreatureFixedPossession(thing, destMapX, destMapY);
}
} else if ((destinationSquareType == (int)kDMElementTypeStairs) && (thing != Thing::_party) && (thingType != kDMThingTypeProjectile)) {
if (!getFlag(destinationSquareData, kDMSquareMaskStairsUp)) {
mapIndexDestination = _vm->_dungeonMan->getLocationAfterLevelChange(mapIndexDestination, 1, &destMapX, &destMapY);
_vm->_dungeonMan->setCurrentMap(mapIndexDestination);
}
direction = _vm->_dungeonMan->getStairsExitDirection(destMapX, destMapY);
destMapX += _vm->_dirIntoStepCountEast[direction], destMapY += _vm->_dirIntoStepCountNorth[direction];
direction = _vm->returnOppositeDir((Direction)direction);
thingCell = thing.getCell();
thingCell = _vm->normalizeModulo4((((thingCell - direction + 1) & 0x0002) >> 1) + direction);
thing = _vm->thingWithNewCell(thing, thingCell);
} else
break;
}
}
if ((thingType == kDMThingTypeGroup) && (fallKilledGroup || !_vm->_dungeonMan->isCreatureAllowedOnMap(thing, mapIndexDestination))) {
_vm->_groupMan->dropMovingCreatureFixedPossession(thing, destMapX, destMapY);
_vm->_groupMan->dropGroupPossessions(destMapX, destMapY, thing, kDMSoundModePlayOneTickLater);
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
if (mapX >= 0)
_vm->_groupMan->groupDelete(mapX, mapY);
return true; /* The specified group thing cannot be moved because it was killed by a fall or because it is not allowed on the destination map */
}
_moveResultMapX = destMapX;
_moveResultMapY = destMapY;
_moveResultMapIndex = mapIndexDestination;
_moveResultCell = thing.getCell();
partySquare = (mapIndexDestination == mapIndexSource) && (destMapX == mapX) && (destMapY == mapY);
if (partySquare) {
if (thing == Thing::_party) {
if (_vm->_dungeonMan->_partyDir == direction)
return false;
} else if ((_moveResultCell == thingCell) && (thingType != kDMThingTypeProjectile))
return false;
} else {
if ((thing == Thing::_party) && _vm->_championMan->_partyChampionCount) {
uint16 oldDestinationSquare = destinationSquareData;
int16 scentIndex = _vm->_championMan->_party._scentCount;
while (scentIndex >= 24) {
_vm->_championMan->deleteScent(0);
scentIndex--;
}
if (scentIndex)
_vm->_championMan->addScentStrength(mapX, mapY, (int)(_vm->_gameTime - _vm->_projexpl->_lastPartyMovementTime));
_vm->_projexpl->_lastPartyMovementTime = _vm->_gameTime;
_vm->_championMan->_party._scentCount++;
if (_vm->_championMan->_party._event79Count_Footprints)
_vm->_championMan->_party._lastScentIndex = _vm->_championMan->_party._scentCount;
_vm->_championMan->_party._scents[scentIndex].setMapX(destMapX);
_vm->_championMan->_party._scents[scentIndex].setMapY(destMapY);
_vm->_championMan->_party._scents[scentIndex].setMapIndex(mapIndexDestination);
_vm->_championMan->_party._scentStrengths[scentIndex] = 0;
_vm->_championMan->addScentStrength(destMapX, destMapY, kDMMaskMergeCycles | 24);
destinationSquareData = oldDestinationSquare;
}
if (mapIndexDestination != mapIndexSource)
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
}
}
if (mapX >= 0) {
if (thing == Thing::_party)
processThingAdditionOrRemoval(mapX, mapY, Thing::_party, partySquare, false);
else if (thingLevitates)
_vm->_dungeonMan->unlinkThingFromList(thing, Thing::_none, mapX, mapY);
else
processThingAdditionOrRemoval(mapX, mapY, thing, (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex) && (mapX == _vm->_dungeonMan->_partyMapX) && (mapY == _vm->_dungeonMan->_partyMapY), false);
}
if (destMapX >= 0) {
if (thing == Thing::_party) {
_vm->_dungeonMan->setCurrentMap(mapIndexDestination);
if ((thing = _vm->_groupMan->groupGetThing(_vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY)) != Thing::_endOfList) { /* Delete group if party moves onto its square */
_vm->_groupMan->dropGroupPossessions(_vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, thing, kDMSoundModePlayIfPrioritized);
_vm->_groupMan->groupDelete(_vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY);
}
if (mapIndexDestination == mapIndexSource)
processThingAdditionOrRemoval(_vm->_dungeonMan->_partyMapX, _vm->_dungeonMan->_partyMapY, Thing::_party, partySquare, true);
else {
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
_vm->_newPartyMapIndex = mapIndexDestination;
}
} else {
if (thingType == kDMThingTypeGroup) {
_vm->_dungeonMan->setCurrentMap(mapIndexDestination);
Teleporter *L0712_ps_Teleporter = (Teleporter *)_vm->_dungeonMan->getThingData(thing);
int16 activeGroupIndex = ((Group *)L0712_ps_Teleporter)->getActiveGroupIndex();
if (((mapIndexDestination == _vm->_dungeonMan->_partyMapIndex) && (destMapX == _vm->_dungeonMan->_partyMapX) && (destMapY == _vm->_dungeonMan->_partyMapY)) || (_vm->_groupMan->groupGetThing(destMapX, destMapY) != Thing::_endOfList)) { /* If a group tries to move to the party square or over another group then create an event to move the group later */
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
if (mapX >= 0)
_vm->_groupMan->groupDeleteEvents(mapX, mapY);
if (groupOnPartyMap)
_vm->_groupMan->removeActiveGroup(activeGroupIndex);
createEventMoveGroup(thing, destMapX, destMapY, mapIndexDestination, audibleTeleporter);
return true; /* The specified group thing cannot be moved because the party or another group is on the destination square */
}
uint16 movementSoundIndex = getSound(((Group *)_vm->_dungeonMan->_thingData[kDMThingTypeGroup])[thing.getIndex()]._type);
if (movementSoundIndex < k34_D13_soundCount)
_vm->_sound->requestPlay(movementSoundIndex, destMapX, destMapY, kDMSoundModePlayIfPrioritized);
if (groupOnPartyMap && (mapIndexDestination != _vm->_dungeonMan->_partyMapIndex)) { /* If the group leaves the party map */
_vm->_groupMan->removeActiveGroup(activeGroupIndex);
moveGroupResult = true;
} else if ((mapIndexDestination == _vm->_dungeonMan->_partyMapIndex) && (!groupOnPartyMap)) { /* If the group arrives on the party map */
_vm->_groupMan->addActiveGroup(thing, destMapX, destMapY);
moveGroupResult = true;
}
if (thingLevitates)
_vm->_dungeonMan->linkThingToList(thing, Thing(0), destMapX, destMapY);
else
processThingAdditionOrRemoval(destMapX, destMapY, thing, false, true);
if (moveGroupResult || (mapX < 0)) /* If group moved from one map to another or if it was just placed on a square */
_vm->_groupMan->startWandering(destMapX, destMapY);
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
if (mapX >= 0) {
if (moveGroupResult > 1) /* If the group behavior was C6_BEHAVIOR_ATTACK before being teleported from and to the party map */
_vm->_groupMan->stopAttacking(&_vm->_groupMan->_activeGroups[moveGroupResult - 2], mapX, mapY);
else if (moveGroupResult) /* If the group was teleported or leaved the party map or entered the party map */
_vm->_groupMan->groupDeleteEvents(mapX, mapY);
}
return moveGroupResult;
}
_vm->_dungeonMan->setCurrentMap(mapIndexDestination);
if (thingType == kDMThingTypeProjectile) /* BUG0_29 An explosion can trigger a floor sensor. Explosions do not trigger floor sensors on the square where they are created. However, if an explosion is moved by a teleporter (or by falling into a pit, see BUG0_26) after it was created, it can trigger floor sensors on the destination square. This is because explosions are not considered as levitating in the code, while projectiles are. The condition here should be (L0713_B_ThingLevitates) so that explosions would not start sensor processing on their destination square as they should be Levitating. This would work if F0264_MOVE_IsLevitating returned true for explosions (see BUG0_26) */
_vm->_dungeonMan->linkThingToList(thing, Thing(0), destMapX, destMapY);
else
processThingAdditionOrRemoval(destMapX, destMapY, thing, (_vm->_dungeonMan->_currMapIndex == _vm->_dungeonMan->_partyMapIndex) && (destMapX == _vm->_dungeonMan->_partyMapX) && (destMapY == _vm->_dungeonMan->_partyMapY), true);
_vm->_dungeonMan->setCurrentMap(mapIndexSource);
}
}
return false;
}
bool MovesensMan::isLevitating(Thing thing) {
ThingType thingType = thing.getType();
bool retVal = false;
if (thingType == kDMThingTypeGroup)
retVal = getFlag(_vm->_dungeonMan->getCreatureAttributes(thing), k0x0020_MaskCreatureInfo_levitation);
else if ((thingType == kDMThingTypeProjectile) || (thingType == kDMThingTypeExplosion))
// Fix original bug involving explosions falling in pits
retVal = true;
return retVal;
}
bool MovesensMan::moveIsKilledByProjectileImpact(int16 srcMapX, int16 srcMapY, int16 destMapX, int16 destMapY, Thing thing) {
/* This array is used only when moving between two adjacent squares and is used to test projectile
impacts when the party or group is in the 'intermediary' step between the two squares. Without
this test, in the example below no impact would be detected. In this example, the party moves from
the source square on the left (which contains a single champion at cell 2) to the destination square
on the right (which contains a single projectile at cell 3).
Party: Projectiles on target square: Incorrect result without the test for the intermediary step (the champion would have passed through the projectile without impact):
00 -> 00 00
01 P0 P1 */
byte intermediaryChampionOrCreatureOrdinalInCell[4];
/* This array has an entry for each cell on the source square, containing the ordinal of the champion
or creature (0 if there is no champion or creature at this cell) */
byte championOrCreatureOrdinalInCell[4];
bool checkDestinationSquareProjectileImpacts = false;
for (int16 i = 0; i < 4; ++i)
championOrCreatureOrdinalInCell[i] = 0;
ElementType impactType;
if (thing == Thing::_party) {
impactType = kDMElementTypeChampion;
for (uint16 cellIdx = kDMCellNorthWest; cellIdx < kDMCellSouthWest + 1; cellIdx++) {
if (_vm->_championMan->getIndexInCell((ViewCell)cellIdx) >= 0)
championOrCreatureOrdinalInCell[cellIdx] = _vm->indexToOrdinal(cellIdx);
}
} else {
impactType = kDMElementTypeCreature;
Group *curGroup = (Group *)_vm->_dungeonMan->getThingData(thing);
int16 creatureAlive = 0;
for (uint16 cellIdx = kDMCellNorthWest; cellIdx < kDMCellSouthWest + 1; cellIdx++) {
creatureAlive |= curGroup->_health[cellIdx];
if (_vm->_groupMan->getCreatureOrdinalInCell(curGroup, cellIdx))
championOrCreatureOrdinalInCell[cellIdx] = _vm->indexToOrdinal(cellIdx);
}
if (!creatureAlive)
return false;
}
if ((destMapX >= 0) && ((abs(srcMapX - destMapX) + abs(srcMapY - destMapY)) == 1)) {
/* If source and destination squares are adjacent (if party or group is not being teleported) */
int16 primaryDirection = _vm->_groupMan->getDirsWhereDestIsVisibleFromSource(srcMapX, srcMapY, destMapX, destMapY);
int16 secondaryDirection = _vm->turnDirRight(primaryDirection);
for (int16 i = 0; i < 4; ++i)
intermediaryChampionOrCreatureOrdinalInCell[i] = 0;
intermediaryChampionOrCreatureOrdinalInCell[_vm->turnDirLeft(primaryDirection)] = championOrCreatureOrdinalInCell[primaryDirection];
if (intermediaryChampionOrCreatureOrdinalInCell[_vm->turnDirLeft(primaryDirection)])
checkDestinationSquareProjectileImpacts = true;
intermediaryChampionOrCreatureOrdinalInCell[_vm->turnDirRight(secondaryDirection)] = championOrCreatureOrdinalInCell[secondaryDirection];
if (intermediaryChampionOrCreatureOrdinalInCell[_vm->turnDirRight(secondaryDirection)])
checkDestinationSquareProjectileImpacts = true;
if (!championOrCreatureOrdinalInCell[primaryDirection])
championOrCreatureOrdinalInCell[primaryDirection] = championOrCreatureOrdinalInCell[_vm->turnDirLeft(primaryDirection)];
if (!championOrCreatureOrdinalInCell[secondaryDirection])
championOrCreatureOrdinalInCell[secondaryDirection] = championOrCreatureOrdinalInCell[_vm->turnDirRight(secondaryDirection)];
}
uint16 projectileMapX = srcMapX; /* Check impacts with projectiles on the source square */
uint16 projectileMapY = srcMapY;
T0266017_CheckProjectileImpacts:
Thing curThing = _vm->_dungeonMan->getSquareFirstThing(projectileMapX, projectileMapY);
while (curThing != Thing::_endOfList) {
if ((curThing.getType() == kDMThingTypeProjectile) &&
(_vm->_timeline->_events[(((Projectile *)_vm->_dungeonMan->_thingData[kDMThingTypeProjectile])[curThing.getIndex()])._eventIndex]._type != k48_TMEventTypeMoveProjectileIgnoreImpacts)) {
int16 championOrCreatureOrdinal = championOrCreatureOrdinalInCell[curThing.getCell()];
if (championOrCreatureOrdinal && _vm->_projexpl->hasProjectileImpactOccurred(impactType, srcMapX, srcMapY, _vm->ordinalToIndex(championOrCreatureOrdinal), curThing)) {
_vm->_projexpl->projectileDeleteEvent(curThing);
if (_vm->_projexpl->_creatureDamageOutcome == k2_outcomeKilledAllCreaturesInGroup)
return true;
goto T0266017_CheckProjectileImpacts;
}
}
curThing = _vm->_dungeonMan->getNextThing(curThing);
}
if (checkDestinationSquareProjectileImpacts) {
srcMapX |= ((projectileMapX = destMapX) + 1) << 8; /* Check impacts with projectiles on the destination square */
srcMapY |= (projectileMapY = destMapY) << 8;
for (uint16 i = 0; i < 4; ++i)
championOrCreatureOrdinalInCell[i] = intermediaryChampionOrCreatureOrdinalInCell[i];
checkDestinationSquareProjectileImpacts = false;
goto T0266017_CheckProjectileImpacts;
}
return false;
}
void MovesensMan::addEvent(byte type, byte mapX, byte mapY, Cell cell, SensorEffect effect, int32 time) {
TimelineEvent newEvent;
_vm->setMapAndTime(newEvent._mapTime, _vm->_dungeonMan->_currMapIndex, time);
newEvent._type = type;
newEvent._priority = 0;
newEvent._Bu._location._mapX = mapX;
newEvent._Bu._location._mapY = mapY;
newEvent._Cu.A._cell = cell;
newEvent._Cu.A._effect = effect;
_vm->_timeline->addEventGetEventIndex(&newEvent);
}
int16 MovesensMan::getSound(byte creatureType) {
if (_vm->_championMan->_partyIsSleeping)
return 35;
switch (creatureType) {
case k3_CreatureTypeWizardEyeFlyingEye:
case k8_CreatureTypeGhostRive:
case k11_CreatureTypeBlackFlame:
case k19_CreatureTypeMaterializerZytaz:
case k23_CreatureTypeLordChaos:
case k25_CreatureTypeLordOrder:
case k26_CreatureTypeGreyLord:
return 35;
case k2_CreatureTypeGiggler:
case k9_CreatureTypeStoneGolem:
case k10_CreatureTypeMummy:
case k14_CreatureTypeVexirk:
case k16_CreatureTypeTrolinAntman:
case k22_CreatureTypeDemon:
return k24_soundMOVE_MUMMY_TROLIN_ANTMAN_STONE_GOLEM_GIGGLER_VEXIRK_DEMON;
case k0_CreatureTypeGiantScorpionScorpion:
case k4_CreatureTypePainRatHellHound:
case k5_CreatureTypeRuster:
case k6_CreatureTypeScreamer:
case k7_CreatureTypeRockpile:
case k15_CreatureTypeMagnetaWormWorm:
case k21_CreatureTypeOitu:
return k26_soundMOVE_SCREAMER_ROCK_ROCKPILE_MAGENTA_WORM_WORM_PAIN_RAT_HELLHOUND_RUSTER_GIANT_SCORPION_SCORPION_OITU;
case k24_CreatureTypeRedDragon:
return k32_soundMOVE_RED_DRAGON;
case k12_CreatureTypeSkeleton:
return k33_soundMOVE_SKELETON;
case k18_CreatureTypeAnimatedArmourDethKnight:
return k22_soundMOVE_ANIMATED_ARMOUR_DETH_KNIGHT;
case k1_CreatureTypeSwampSlimeSlime:
case k20_CreatureTypeWaterElemental:
return k27_soundMOVE_SWAMP_SLIME_SLIME_DEVIL_WATER_ELEMENTAL;
case k13_CreatureTypeCouatl:
case k17_CreatureTypeGiantWaspMuncher:
return k23_soundMOVE_COUATL_GIANT_WASP_MUNCHER;
}
return 35;
}
int16 MovesensMan::getTeleporterRotatedGroupResult(Teleporter *teleporter, Thing thing, uint16 mapIndex) {
Group *group = (Group *)_vm->_dungeonMan->getThingData(thing);
Direction rotation = teleporter->getRotation();
uint16 groupDirections = _vm->_groupMan->getGroupDirections(group, mapIndex);
bool absoluteRotation = teleporter->getAbsoluteRotation();
uint16 updatedGroupDirections;
if (absoluteRotation)
updatedGroupDirections = rotation;
else
updatedGroupDirections = _vm->normalizeModulo4(groupDirections + rotation);
uint16 updatedGroupCells = _vm->_groupMan->getGroupCells(group, mapIndex);
if (updatedGroupCells != k255_CreatureTypeSingleCenteredCreature) {
int16 groupCells = updatedGroupCells;
int16 creatureSize = getFlag(_vm->_dungeonMan->_creatureInfos[group->_type]._attributes, k0x0003_MaskCreatureInfo_size);
int16 relativeRotation = _vm->normalizeModulo4(4 + updatedGroupDirections - groupDirections);
for (int16 creatureIdx = 0; creatureIdx <= group->getCount(); creatureIdx++) {
updatedGroupDirections = _vm->_groupMan->getGroupValueUpdatedWithCreatureValue(updatedGroupDirections, creatureIdx, absoluteRotation ? (uint16)rotation : _vm->normalizeModulo4(groupDirections + rotation));
if (creatureSize == k0_MaskCreatureSizeQuarter) {
relativeRotation = absoluteRotation ? 1 : 0;
if (relativeRotation)
relativeRotation = rotation;
}
if (relativeRotation)
updatedGroupCells = _vm->_groupMan->getGroupValueUpdatedWithCreatureValue(updatedGroupCells, creatureIdx, _vm->normalizeModulo4(groupCells + relativeRotation));
groupDirections >>= 2;
groupCells >>= 2;
}
}
_vm->_dungeonMan->setGroupDirections(group, updatedGroupDirections, mapIndex);
_vm->_dungeonMan->setGroupCells(group, updatedGroupCells, mapIndex);
if ((mapIndex == _vm->_dungeonMan->_partyMapIndex) && (group->setBehaviour(k6_behavior_ATTACK)))
return group->getActiveGroupIndex() + 2;
return 1;
}
Thing MovesensMan::getTeleporterRotatedProjectileThing(Teleporter *teleporter, Thing projectileThing) {
int16 updatedDirection = _moveResultDir;
int16 rotation = teleporter->getRotation();
if (teleporter->getAbsoluteRotation())
updatedDirection = rotation;
else {
updatedDirection = _vm->normalizeModulo4(updatedDirection + rotation);
projectileThing = _vm->thingWithNewCell(projectileThing, _vm->normalizeModulo4(projectileThing.getCell() + rotation));
}
_moveResultDir = updatedDirection;
return projectileThing;
}
void MovesensMan::processThingAdditionOrRemoval(uint16 mapX, uint16 mapY, Thing thing, bool partySquare, bool addThing) {
int16 thingType;
IconIndice objectType;
if (thing != Thing::_party) {
thingType = thing.getType();
objectType = _vm->_objectMan->getObjectType(thing);
} else {
thingType = kDMThingTypeParty;
objectType = kDMIconIndiceNone;
}
if ((!addThing) && (thingType != kDMThingTypeParty))
_vm->_dungeonMan->unlinkThingFromList(thing, Thing(0), mapX, mapY);
Square curSquare = Square(_vm->_dungeonMan->_currMapData[mapX][mapY]);
int16 sensorTriggeredCell;
if (curSquare.getType() == kDMElementTypeWall)
sensorTriggeredCell = thing.getCell();
else
sensorTriggeredCell = kDMCellAny; // this will wrap around
bool squareContainsObject = false;
bool squareContainsGroup = false;
bool squareContainsThingOfSameType = false;
bool squareContainsThingOfDifferentType = false;
Thing curThing = _vm->_dungeonMan->getSquareFirstThing(mapX, mapY);
if (sensorTriggeredCell == kDMCellAny) {
while (curThing != Thing::_endOfList) {
uint16 curThingType = curThing.getType();
if (curThingType == kDMThingTypeGroup)
squareContainsGroup = true;
else if ((curThingType == kDMstringTypeText) && (thingType == kDMThingTypeParty) && addThing && !partySquare) {
_vm->_dungeonMan->decodeText(_vm->_stringBuildBuffer, curThing, kDMTextTypeMessage);
_vm->_textMan->printMessage(k15_ColorWhite, _vm->_stringBuildBuffer);
} else if ((curThingType > kDMThingTypeGroup) && (curThingType < kDMThingTypeProjectile)) {
squareContainsObject = true;
squareContainsThingOfSameType |= (_vm->_objectMan->getObjectType(curThing) == objectType);
squareContainsThingOfDifferentType |= (_vm->_objectMan->getObjectType(curThing) != objectType);
}
curThing = _vm->_dungeonMan->getNextThing(curThing);
}
} else {
while (curThing != Thing::_endOfList) {
if ((sensorTriggeredCell == curThing.getCell()) && (curThing.getType() > kDMThingTypeGroup)) {
squareContainsObject = true;
squareContainsThingOfSameType |= (_vm->_objectMan->getObjectType(curThing) == objectType);
squareContainsThingOfDifferentType |= (_vm->_objectMan->getObjectType(curThing) != objectType);
}
curThing = _vm->_dungeonMan->getNextThing(curThing);
}
}
if (addThing && (thingType != kDMThingTypeParty))
_vm->_dungeonMan->linkThingToList(thing, Thing(0), mapX, mapY);
for (curThing = _vm->_dungeonMan->getSquareFirstThing(mapX, mapY); curThing != Thing::_endOfList; curThing = _vm->_dungeonMan->getNextThing(curThing)) {
uint16 curThingType = curThing.getType();
if (curThingType == kDMThingTypeSensor) {
Sensor *curSensor = (Sensor *)_vm->_dungeonMan->getThingData(curThing);
if (curSensor->getType() == kDMSensorDisabled)
continue;
int16 curSensorData = curSensor->getData();
bool triggerSensor = addThing;
if (sensorTriggeredCell == kDMCellAny) {
switch (curSensor->getType()) {
case kDMSensorFloorTheronPartyCreatureObj:
if (partySquare || squareContainsObject || squareContainsGroup) /* BUG0_30 A floor sensor is not triggered when you put an object on the floor if a levitating creature is present on the same square. The condition to determine if the sensor should be triggered checks if there is a creature on the square but does not check whether the creature is levitating. While it is normal not to trigger the sensor if there is a non levitating creature on the square (because it was already triggered by the creature itself), a levitating creature should not prevent triggering the sensor with an object. */
continue;
break;
case kDMSensorFloorTheronPartyCreature:
if ((thingType > kDMThingTypeGroup) || partySquare || squareContainsGroup)
continue;
break;
case kDMSensorFloorParty:
if ((thingType != kDMThingTypeParty) || (_vm->_championMan->_partyChampionCount == 0))
continue;
if (curSensorData == 0) {
if (partySquare)
continue;
} else if (!addThing)
triggerSensor = false;
else
triggerSensor = (curSensorData == _vm->indexToOrdinal(_vm->_dungeonMan->_partyDir));
break;
case kDMSensorFloorObj:
if ((curSensorData != _vm->_objectMan->getObjectType(thing)) || squareContainsThingOfSameType)
continue;
break;
case kDMSensorFloorPartyOnStairs:
if ((thingType != kDMThingTypeParty) || (curSquare.getType() != kDMElementTypeStairs))
continue;
break;
case kDMSensorFloorGroupGenerator:
continue;
break;
case kDMSensorFloorCreature:
if ((thingType > kDMThingTypeGroup) || (thingType == kDMThingTypeParty) || squareContainsGroup)
continue;
break;
case kDMSensorFloorPartyPossession:
if (thingType != kDMThingTypeParty)
continue;
triggerSensor = isObjectInPartyPossession(curSensorData);
break;
case kDMSensorFloorVersionChecker:
if ((thingType != kDMThingTypeParty) || !addThing || partySquare)
continue;
// Strangerke: 20 is a hardcoded version of the game. later version uses 21. Not present in the original dungeons anyway.
triggerSensor = (curSensorData <= 20);
break;
default:
continue;
break;
}
} else {
if (sensorTriggeredCell != curThing.getCell())
continue;
switch (curSensor->getType()) {
case kDMSensorWallOrnClick:
if (squareContainsObject)
continue;
break;
case kDMSensorWallOrnClickWithAnyObj:
if (squareContainsThingOfSameType || (curSensor->getData() != _vm->_objectMan->getObjectType(thing)))
continue;
break;
case kDMSensorWallOrnClickWithSpecObj:
if (squareContainsThingOfDifferentType || (curSensor->getData() == _vm->_objectMan->getObjectType(thing)))
continue;
break;
default:
continue;
break;
}
}
triggerSensor ^= curSensor->getAttrRevertEffectA();
SensorEffect curSensorEffect = (SensorEffect)curSensor->getAttrEffectA();
if (curSensorEffect == kDMSensorEffectHold)
curSensorEffect = triggerSensor ? kDMSensorEffectSet : kDMSensorEffectClear;
else if (!triggerSensor)
continue;
if (curSensor->getAttrAudibleA())
_vm->_sound->requestPlay(k01_soundSWITCH, mapX, mapY, kDMSoundModePlayIfPrioritized);
triggerEffect(curSensor, curSensorEffect, mapX, mapY, (uint16)kDMCellAny); // this will wrap around
continue;
}
if (curThingType >= kDMThingTypeGroup)
break;
}
processRotationEffect();
}
bool MovesensMan::isObjectInPartyPossession(int16 objectType) {
bool leaderHandObjectProcessed = false;
Champion *curChampion = _vm->_championMan->_champions;
int16 championIdx;
uint16 slotIdx = 0;
Thing curThing;
Thing *curSlotThing = nullptr;
for (championIdx = kDMChampionFirst; championIdx < _vm->_championMan->_partyChampionCount; championIdx++, curChampion++) {
if (curChampion->_currHealth) {
curSlotThing = curChampion->_slots;
for (slotIdx = kDMSlotReadyHand; (slotIdx < kDMSlotChest1) || !leaderHandObjectProcessed; slotIdx++) {
if (slotIdx < kDMSlotChest1)
curThing = *curSlotThing++;
else {
leaderHandObjectProcessed = true;
curThing = _vm->_championMan->_leaderHandObject;
}
int16 curObjectType = _vm->_objectMan->getObjectType(curThing);
if (curObjectType == objectType)
return true;
if (curObjectType == kDMIconIndiceContainerChestClosed) {
Container *container = (Container *)_vm->_dungeonMan->getThingData(curThing);
curThing = container->getSlot();
while (curThing != Thing::_endOfList) {
if (_vm->_objectMan->getObjectType(curThing) == objectType)
return true;
curThing = _vm->_dungeonMan->getNextThing(curThing);
}
}
}
}
}
return false;
}
void MovesensMan::triggerEffect(Sensor *sensor, SensorEffect effect, int16 mapX, int16 mapY, uint16 cell) {
TimelineEventType squareTypeToEventTypeArray[7] = { // @ G0059_auc_Graphic562_SquareTypeToEventType
k6_TMEventTypeWall,
k5_TMEventTypeCorridor,
k9_TMEventTypePit,
k0_TMEventTypeNone,
k10_TMEventTypeDoor,
k8_TMEventTypeTeleporter,
k7_TMEventTypeFakeWall
};
if (sensor->getAttrOnlyOnce())
sensor->setTypeDisabled();
int32 endTime = _vm->_gameTime + sensor->getAttrValue();
if (sensor->getAttrLocalEffect())
triggerLocalEffect((SensorEffect)sensor->getActionLocalEffect(), mapX, mapY, cell);
else {
int16 targetMapX = sensor->getActionTargetMapX();
int16 targetMapY = sensor->getActionTargetMapY();
ElementType curSquareType = Square(_vm->_dungeonMan->_currMapData[targetMapX][targetMapY]).getType();
Cell targetCell;
if (curSquareType == kDMElementTypeWall)
targetCell = sensor->getActionTargetCell();
else
targetCell = kDMCellNorthWest;
addEvent(squareTypeToEventTypeArray[curSquareType], targetMapX, targetMapY, targetCell, effect, endTime);
}
}
void MovesensMan::triggerLocalEffect(SensorEffect localEffect, int16 effX, int16 effY, int16 effCell) {
if (localEffect == kDMSensorEffectAddExperience) {
addSkillExperience(kDMSkillSteal, 300, localEffect != kDMSensorEffectNone);
return;
}
_sensorRotationEffect = localEffect;
_sensorRotationEffMapX = effX;
_sensorRotationEffMapY = effY;
_sensorRotationEffCell = effCell;
}
void MovesensMan::addSkillExperience(int16 skillIndex, uint16 exp, bool leaderOnly) {
if (leaderOnly) {
if (_vm->_championMan->_leaderIndex != kDMChampionNone)
_vm->_championMan->addSkillExperience(_vm->_championMan->_leaderIndex, skillIndex, exp);
} else {
exp /= _vm->_championMan->_partyChampionCount;
Champion *curChampion = _vm->_championMan->_champions;
for (int16 championIdx = kDMChampionFirst; championIdx < _vm->_championMan->_partyChampionCount; championIdx++, curChampion++) {
if (curChampion->_currHealth)
_vm->_championMan->addSkillExperience(championIdx, skillIndex, exp);
}
}
}
void MovesensMan::processRotationEffect() {
if (_sensorRotationEffect == kDMSensorEffectNone)
return;
switch (_sensorRotationEffect) {
case kDMSensorEffectClear:
case kDMSensorEffectToggle:
Thing firstSensorThing = _vm->_dungeonMan->getSquareFirstThing(_sensorRotationEffMapX, _sensorRotationEffMapY);
while ((firstSensorThing.getType() != kDMThingTypeSensor)
|| ((_sensorRotationEffCell != kDMCellAny) && (firstSensorThing.getCell() != _sensorRotationEffCell))) {
firstSensorThing = _vm->_dungeonMan->getNextThing(firstSensorThing);
}
Sensor *firstSensor = (Sensor *)_vm->_dungeonMan->getThingData(firstSensorThing);
Thing lastSensorThing = firstSensor->getNextThing();
while ((lastSensorThing != Thing::_endOfList)
&& ((lastSensorThing.getType() != kDMThingTypeSensor)
|| ((_sensorRotationEffCell != kDMCellAny) && (lastSensorThing.getCell() != _sensorRotationEffCell)))) {
lastSensorThing = _vm->_dungeonMan->getNextThing(lastSensorThing);
}
if (lastSensorThing == Thing::_endOfList)
break;
_vm->_dungeonMan->unlinkThingFromList(firstSensorThing, Thing(0), _sensorRotationEffMapX, _sensorRotationEffMapY);
Sensor *lastSensor = (Sensor *)_vm->_dungeonMan->getThingData(lastSensorThing);
lastSensorThing = _vm->_dungeonMan->getNextThing(lastSensorThing);
while (((lastSensorThing != Thing::_endOfList) && (lastSensorThing.getType() == kDMThingTypeSensor))) {
if ((_sensorRotationEffCell == kDMCellAny) || (lastSensorThing.getCell() == _sensorRotationEffCell))
lastSensor = (Sensor *)_vm->_dungeonMan->getThingData(lastSensorThing);
lastSensorThing = _vm->_dungeonMan->getNextThing(lastSensorThing);
}
firstSensor->setNextThing(lastSensor->getNextThing());
lastSensor->setNextThing(firstSensorThing);
}
_sensorRotationEffect = kDMSensorEffectNone;
}
void MovesensMan::createEventMoveGroup(Thing groupThing, int16 mapX, int16 mapY, int16 mapIndex, bool audible) {
TimelineEvent newEvent;
_vm->setMapAndTime(newEvent._mapTime, mapIndex, _vm->_gameTime + 5);
newEvent._type = audible ? k61_TMEventTypeMoveGroupAudible : k60_TMEventTypeMoveGroupSilent;
newEvent._priority = 0;
newEvent._Bu._location._mapX = mapX;
newEvent._Bu._location._mapY = mapY;
newEvent._Cu._slot = groupThing.toUint16();
_vm->_timeline->addEventGetEventIndex(&newEvent);
}
Thing MovesensMan::getObjectOfTypeInCell(int16 mapX, int16 mapY, int16 cell, int16 objectType) {
Thing curThing = _vm->_dungeonMan->getSquareFirstObject(mapX, mapY);
while (curThing != Thing::_endOfList) {
if (_vm->_objectMan->getObjectType(curThing) == objectType) {
if ((cell == kDMCellAny) || (curThing.getCell() == cell))
return curThing;
}
curThing = _vm->_dungeonMan->getNextThing(curThing);
}
return Thing::_none;
}
}
|