-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathMission.Attack.cpp
More file actions
1508 lines (1213 loc) · 36.2 KB
/
Mission.Attack.cpp
File metadata and controls
1508 lines (1213 loc) · 36.2 KB
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
#include "Body.h"
#include <Ext/Building/Body.h>
#include <Ext/BulletType/Body.h>
#include <Ext/WeaponType/Body.h>
// Contains ScriptExt::Mission_Attack and its helper functions.
void ScriptExt::Mission_Attack(TeamClass* pTeam, int calcThreatMode, bool repeatAction, int attackAITargetType, int idxAITargetTypeItem)
{
bool noWaitLoop = false;
bool bAircraftsWithoutAmmo = false;
bool agentMode = false;
bool pacifistTeam = true;
const auto pTeamData = TeamExt::ExtMap.Find(pTeam);
auto& waitNoTargetCounter = pTeamData->WaitNoTargetCounter;
auto& waitNoTargetTimer = pTeamData->WaitNoTargetTimer;
auto& waitNoTargetAttempts = pTeamData->WaitNoTargetAttempts;
const auto pHouseExt = HouseExt::ExtMap.Find(pTeam->Owner);
// When the new target wasn't found it sleeps some few frames before the new attempt. This can save cycles and cycles of unnecessary executed lines.
if (waitNoTargetCounter > 0)
{
if (waitNoTargetTimer.InProgress())
return;
waitNoTargetTimer.Stop();
noWaitLoop = true;
waitNoTargetCounter = 0;
if (waitNoTargetAttempts > 0)
waitNoTargetAttempts--;
}
auto pFocus = abstract_cast<TechnoClass*>(pTeam->Focus);
auto& pTeamFocus = pTeam->Focus;
if (!ScriptExt::IsUnitAvailable(pFocus, true))
{
pTeamFocus = nullptr;
pFocus = nullptr;
}
const auto pTeamType = pTeam->Type;
const auto pTeamTypeID = pTeamType->ID;
const auto pFirstUnit = pTeam->FirstUnit;
const auto pScript = pTeam->CurrentScript;
bool& stepCompleted = pTeam->StepCompleted;
const auto pScriptType = pScript->Type;
const auto& scriptActions = pScriptType->ScriptActions;
const auto currentMission = pScript->CurrentMission;
auto& nextSuccessWeightAward = pTeamData->NextSuccessWeightAward;
auto& idxSelectedObjectFromAIList = pTeamData->IdxSelectedObjectFromAIList;
for (auto pFoot = pFirstUnit; pFoot; pFoot = pFoot->NextTeamMember)
{
const auto pKillerTechnoData = TechnoExt::ExtMap.Find(pFoot);
if (pKillerTechnoData->LastKillWasTeamTarget)
{
// Time for Team award check! (if set any)
if (nextSuccessWeightAward > 0)
{
ScriptExt::IncreaseCurrentTriggerWeight(pTeam, false, nextSuccessWeightAward);
nextSuccessWeightAward = 0;
}
// Let's clean the Killer mess
pKillerTechnoData->LastKillWasTeamTarget = false;
pFocus = nullptr;
pTeamFocus = nullptr;
if (!repeatAction)
{
// If the previous Team's Target was killed by this Team Member and the script was a 1-time-use then this script action must be finished.
for (auto pFootTeam = pFirstUnit; pFootTeam; pFootTeam = pFootTeam->NextTeamMember)
{
// Let's reset all Team Members objective
const auto pKillerTeamUnitData = TechnoExt::ExtMap.Find(pFootTeam);
pKillerTeamUnitData->LastKillWasTeamTarget = false;
if (pFootTeam->WhatAmI() == AbstractType::Aircraft)
{
pFootTeam->SetTarget(nullptr);
pFootTeam->LastTarget = nullptr;
pFootTeam->QueueMission(Mission::Guard, true);
}
}
idxSelectedObjectFromAIList = -1;
// This action finished
stepCompleted = true;
/*const auto& node = pScriptType->ScriptActions[pScript->CurrentMission];
const int nextMission = pScript->CurrentMission + 1;
const auto& nextNode = pScriptType->ScriptActions[nextMission];
ScriptExt::Log("AI Scripts - Attack: [%s] [%s] (line: %d = %d,%d) Force the jump to next line: %d = %d,%d (This action wont repeat)\n",
pTeamTypeID,
pScriptType->ID,
currentMission,
node.Action,
node.Argument,
nextMission,
nextNode.Action,
nextNode.Argument);*/
return;
}
}
}
for (auto pFoot = pFirstUnit; pFoot; pFoot = pFoot->NextTeamMember)
{
if (ScriptExt::IsUnitAvailable(pFoot, true))
{
const auto pTechnoType = pFoot->GetTechnoType();
const auto whatAmI = pFoot->WhatAmI();
if (whatAmI == AbstractType::Aircraft)
{
if (!pFoot->IsInAir()
&& static_cast<AircraftTypeClass*>(pTechnoType)->AirportBound
&& pFoot->Ammo < pTechnoType->Ammo)
{
bAircraftsWithoutAmmo = true;
}
}
else if (whatAmI == AbstractType::Infantry)
{
const auto pTypeInf = static_cast<InfantryTypeClass*>(pTechnoType);
// Any Team member (infantry) is a special agent? If yes ignore some checks based on Weapons.
if ((pTypeInf->Agent && pTypeInf->Infiltrate) || pTypeInf->Engineer)
agentMode = true;
}
pacifistTeam &= !ScriptExt::IsUnitArmed(pFoot, pTechnoType);
}
}
// Find the Leader
auto pLeaderUnit = pTeamData->TeamLeader;
if (!ScriptExt::IsUnitAvailable(pLeaderUnit, true))
{
pLeaderUnit = ScriptExt::FindTheTeamLeader(pTeam);
pTeamData->TeamLeader = pLeaderUnit;
}
if (!pLeaderUnit || bAircraftsWithoutAmmo || (pacifistTeam && !agentMode))
{
idxSelectedObjectFromAIList = -1;
if (waitNoTargetAttempts != 0)
{
waitNoTargetTimer.Stop();
waitNoTargetCounter = 0;
waitNoTargetAttempts = 0;
}
// This action finished
stepCompleted = true;
const auto& node = scriptActions[currentMission];
const int nextMission = currentMission + 1;
const auto& nextNode = scriptActions[nextMission];
ScriptExt::Log("AI Scripts - Attack: [%s] [%s] (line: %d = %d,%d) Jump to next line: %d = %d,%d -> (Reason: No Leader found | Exists Aircrafts without ammo | Team members have no weapons)\n",
pTeamTypeID,
pScriptType->ID,
currentMission,
node.Action,
node.Argument,
nextMission,
nextNode.Action,
nextNode.Argument);
return;
}
if (!pFocus && !bAircraftsWithoutAmmo)
{
// This part of the code is used for picking a new target.
HouseClass* enemyHouse = nullptr;
bool onlyTargetHouseEnemy = pTeam->Type->OnlyTargetHouseEnemy;
if (pHouseExt->ForceOnlyTargetHouseEnemyMode != -1)
onlyTargetHouseEnemy = pHouseExt->ForceOnlyTargetHouseEnemy;
// Favorite Enemy House case. If set, AI will focus against that House
if (onlyTargetHouseEnemy && pLeaderUnit->Owner->EnemyHouseIndex >= 0)
enemyHouse = HouseClass::Array.GetItem(pLeaderUnit->Owner->EnemyHouseIndex);
const auto& node = scriptActions[currentMission];
const int targetMask = node.Argument; // This is the target type
const auto pSelectedTarget = ScriptExt::GreatestThreat(pLeaderUnit, targetMask, calcThreatMode, enemyHouse, attackAITargetType, idxAITargetTypeItem, agentMode);
if (pSelectedTarget)
{
/*ScriptExt::Log("AI Scripts - Attack: [%s] [%s] (line: %d = %d,%d) Leader [%s] (UID: %lu) selected [%s] (UID: %lu) as target.\n",
pTeamType->ID,
pScriptType->ID,
currentMission,
node.Action,
node.Argument,
pLeaderUnit->GetTechnoType()->get_ID(),
pLeaderUnit->UniqueID,
pSelectedTarget->GetTechnoType()->get_ID(),
pSelectedTarget->UniqueID);*/
pTeamFocus = pSelectedTarget;
waitNoTargetAttempts = 0; // Disable Script Waits if there are any because a new target was selected
waitNoTargetTimer.Stop();
waitNoTargetCounter = 0; // Disable Script Waits if there are any because a new target was selected
for (auto pFoot = pFirstUnit; pFoot; pFoot = pFoot->NextTeamMember)
{
if (pFoot->IsAlive && !pFoot->InLimbo)
{
const auto pTechnoType = pFoot->GetTechnoType();
if (pFoot != pSelectedTarget && pFoot->Target != pSelectedTarget)
{
if (pTechnoType->Underwater
&& pTechnoType->LandTargeting == LandTargetingType::Land_Not_OK
&& pSelectedTarget->GetCell()->LandType != LandType::Water) // Land not OK for the Naval unit
{
// Naval units like Submarines are unable to target ground targets
// except if they have anti-ground weapons. Ignore the attack
pFoot->SetTarget(nullptr);
pFoot->SetDestination(nullptr, false);
pFoot->QueueMission(Mission::Area_Guard, true);
continue;
}
// If the unit cannot be moved, perhaps it is better this way.
if (TechnoExt::CannotMove(pFoot, true) && !pFoot->IsCloseEnough(pSelectedTarget, pFoot->SelectWeapon(pSelectedTarget)))
continue;
const auto whatAmI = pFoot->WhatAmI();
// Aircraft hack. I hate how this game auto-manages the aircraft missions.
if (whatAmI == AbstractType::Aircraft
&& pFoot->Ammo > 0
&& pFoot->GetHeight() <= 0)
{
pFoot->SetDestination(pSelectedTarget, false);
pFoot->QueueMission(Mission::Attack, true);
}
pFoot->SetTarget(pSelectedTarget);
if (pFoot->IsEngineer())
pFoot->QueueMission(Mission::Capture, true);
else if (whatAmI != AbstractType::Aircraft) // Aircraft hack. I hate how this game auto-manages the aircraft missions.
pFoot->QueueMission(Mission::Attack, true);
if (whatAmI == AbstractType::Infantry)
{
const auto pInfantryType = static_cast<InfantryTypeClass*>(pTechnoType);
// Spy case
if (pInfantryType->Infiltrate
&& pInfantryType->Agent
&& pFoot->GetCurrentMission() != Mission::Enter)
{
pFoot->QueueMission(Mission::Enter, true); // Check if target is an structure and see if spiable
}
// Tanya / Commando C4 case
if ((pInfantryType->C4 || pFoot->HasAbility(Ability::C4))
&& pFoot->GetCurrentMission() != Mission::Sabotage)
{
pFoot->QueueMission(Mission::Sabotage, true);
}
}
}
else
{
pFoot->QueueMission(Mission::Attack, true);
}
}
}
}
else
{
// No target was found with the specific criteria.
if (!noWaitLoop && waitNoTargetTimer.Completed())
{
waitNoTargetCounter = 30;
waitNoTargetTimer.Start(30);
}
if (idxSelectedObjectFromAIList >= 0)
idxSelectedObjectFromAIList = -1;
if (waitNoTargetAttempts != 0 && waitNoTargetTimer.Completed())
{
// No target? let's wait some frames
waitNoTargetCounter = 30;
waitNoTargetTimer.Start(30);
return;
}
// This action finished
stepCompleted = true;
const int nextMission = currentMission + 1;
const auto& nextNode = scriptActions[nextMission];
ScriptExt::Log("AI Scripts - Attack: [%s] [%s] (line: %d = %d,%d) Jump to next line: %d = %d,%d (Leader [%s] (UID: %lu) can't find a new target)\n",
pTeamTypeID,
pScriptType->ID,
currentMission,
node.Action,
node.Argument,
nextMission,
nextNode.Action,
nextNode.Argument,
pLeaderUnit->GetTechnoType()->get_ID(),
pLeaderUnit->UniqueID);
return;
}
}
else
{
// This part of the code is used for updating the "Attack" mission in each team unit
if (ScriptExt::IsUnitAvailable(pFocus, true)
&& !pFocus->GetTechnoType()->Immune
&& ScriptExt::CheckUnitTargetingCapability(pLeaderUnit, pFocus->IsInAir(), agentMode)
&& (!pLeaderUnit->Owner->IsAlliedWith(pFocus->Owner)
|| ScriptExt::IsMindControlledByEnemy(pLeaderUnit->Owner, pFocus)))
{
bool bForceNextAction = false;
for (auto pFoot = pFirstUnit; pFoot && !bForceNextAction; pFoot = pFoot->NextTeamMember)
{
if (ScriptExt::IsUnitAvailable(pFoot, true))
{
const auto pTechnoType = pFoot->GetTechnoType();
const auto whatAmI = pFoot->WhatAmI();
// Aircraft case 1
if ((whatAmI == AbstractType::Aircraft
&& static_cast<AircraftTypeClass*>(pTechnoType)->AirportBound)
&& pFoot->Ammo > 0
&& (pFoot->Target != pFocus && !pFoot->InAir))
{
pFoot->SetTarget(pFocus);
continue;
}
// Naval units like Submarines are unable to target ground targets except if they have anti-ground weapons. Ignore the attack
if (pTechnoType->Underwater
&& pTechnoType->LandTargeting == LandTargetingType::Land_Not_OK
&& pFocus->GetCell()->LandType != LandType::Water) // Land not OK for the Naval unit
{
pFoot->SetTarget(nullptr);
pFoot->SetDestination(nullptr, false);
pFoot->QueueMission(Mission::Area_Guard, true);
bForceNextAction = true;
continue;
}
const auto mission = pFoot->GetCurrentMission();
// Aircraft case 2
if (whatAmI == AbstractType::Aircraft
&& mission != Mission::Attack
&& mission != Mission::Enter)
{
if (pFoot->Ammo > 0)
{
if (pFoot->Target != pFocus)
pFoot->SetTarget(pFocus);
pFoot->QueueMission(Mission::Attack, true);
}
else
{
pFoot->EnterIdleMode(false, true);
}
continue;
}
// Tanya / Commando C4 case
if (mission != Mission::Sabotage
&& (pFoot->HasAbility(Ability::C4)
|| (whatAmI == AbstractType::Infantry
&& static_cast<InfantryTypeClass*>(pTechnoType)->C4)))
{
pFoot->QueueMission(Mission::Sabotage, true);
continue;
}
// Other cases
if (whatAmI != AbstractType::Aircraft)
{
if (pFoot->Target != pFocus)
pFoot->SetTarget(pFocus);
if (mission != Mission::Attack
&& mission != Mission::Unload
&& mission != Mission::Selling)
{
pFoot->QueueMission(Mission::Attack, false);
}
continue;
}
}
}
if (bForceNextAction)
{
idxSelectedObjectFromAIList = -1;
stepCompleted = true;
const auto& node = scriptActions[currentMission];
const int nextMission = currentMission + 1;
const auto& nextNode = scriptActions[nextMission];
ScriptExt::Log("AI Scripts - Attack: [%s] [%s] (line: %d = %d,%d) Jump to NEXT line: %d = %d,%d (Naval is unable to target ground)\n",
pTeamTypeID,
pScriptType->ID,
currentMission,
node.Action,
node.Argument,
nextMission,
nextNode.Action,
nextNode.Argument);
return;
}
}
else
{
pTeamFocus = nullptr;
}
}
}
TechnoClass* ScriptExt::GreatestThreat(TechnoClass* pTechno, int method, int calcThreatMode, HouseClass* onlyTargetThisHouseEnemy, int attackAITargetType, int idxAITargetTypeItem, bool agentMode)
{
TechnoClass* pBestObject = nullptr;
double bestVal = -1;
bool unitWeaponsHaveAA = false;
bool unitWeaponsHaveAG = false;
const auto pTechnoTypeExt = TechnoExt::ExtMap.Find(pTechno)->TypeExtData;
const auto pTechnoType = pTechnoTypeExt->OwnerObject();
const auto pTechnoOwner = pTechno->Owner;
const auto targetZoneScanType = pTechnoTypeExt->TargetZoneScanType;
// Generic method for targeting
for (int i = 0; i < TechnoClass::Array.Count; i++)
{
const auto pTarget = TechnoClass::Array.GetItem(i);
if (pTechnoOwner->IsAlliedWith(pTarget->Owner) && !ScriptExt::IsMindControlledByEnemy(pTechnoOwner, pTarget))
continue;
if (pTarget->Owner == pTechnoOwner)
continue;
// Exclude most of invalid target first
if (!ScriptExt::EvaluateObjectWithMask(pTarget, method, attackAITargetType, idxAITargetTypeItem, pTechno))
continue;
// OnlyTargetHouseEnemy forces targets of a specific (hated) house
if (onlyTargetThisHouseEnemy && pTarget->Owner != onlyTargetThisHouseEnemy)
continue;
if (pTarget->TemporalTargetingMe || pTarget->BeingWarpedOut)
continue;
const auto pTargetType = pTarget->GetTechnoType();
bool skipImmune = false;
if (const auto pTargetBuilding = abstract_cast<BuildingClass*>(pTarget))
{
// Discard invisible structures
if (pTargetBuilding->Type->InvisibleInGame)
continue;
// Skip immunity check for buildings in agent mode.
if (agentMode)
skipImmune = true;
}
if (!pTargetType->LegalTarget || (!skipImmune && pTargetType->Immune))
continue;
// Note: the TEAM LEADER is picked for this task, be careful with leadership values in your mod
const auto pWeaponType = pTechno->GetWeapon(pTechno->SelectWeapon(pTarget))->WeaponType;
if (pWeaponType)
{
const auto pBulletType = pWeaponType->Projectile;
if (pBulletType->AA)
unitWeaponsHaveAA = true;
if (pBulletType->AG || agentMode)
unitWeaponsHaveAG = true;
}
if (!agentMode)
{
if (pWeaponType && GeneralUtils::GetWarheadVersusArmor(pWeaponType->Warhead, pTarget, pTargetType) == 0.0)
continue;
if (!(pTarget->IsInAir() ? unitWeaponsHaveAA : unitWeaponsHaveAG))
continue;
if (MissionControlClass::Array[(int)pTarget->CurrentMission].NoThreat)
continue;
if (pTarget->EstimatedHealth <= 0 && pTechnoType->VHPScan == 2)
continue;
}
const auto cloakState = pTarget->CloakState;
if (pTargetType->Naval)
{
// Submarines aren't a valid target
if (cloakState == CloakState::Cloaked
&& pTargetType->Underwater)
{
const auto navalTargeting = pTechnoType->NavalTargeting;
if (navalTargeting == NavalTargetingType::Underwater_Never
|| navalTargeting == NavalTargetingType::Naval_None)
{
continue;
}
}
// Land not OK for the Naval unit
if (pTechnoType->LandTargeting == LandTargetingType::Land_Not_OK
&& (pTarget->GetCell()->LandType != LandType::Water))
{
continue;
}
}
// Stealth check.
if (cloakState == CloakState::Cloaked)
{
const auto pCell = pTarget->GetCell();
if (!pCell->Sensors_InclHouse(pTechnoOwner->ArrayIndex))
continue;
}
if (!ScriptExt::IsUnitAvailable(pTarget, true))
continue;
// Check map zone
if (!TechnoExt::AllowedTargetByZone(pTechno, pTarget, targetZoneScanType, pWeaponType))
continue;
double value = 0;
bool isGoodTarget = false;
switch (calcThreatMode)
{
case 0:
case 1:
{
// Threat affected by distance
double threatMultiplier = 128.0;
double objectThreatValue = pTargetType->ThreatPosed;
if (pTargetType->SpecialThreatValue > 0)
{
double const& TargetSpecialThreatCoefficientDefault = RulesClass::Instance->TargetSpecialThreatCoefficientDefault;
objectThreatValue += pTargetType->SpecialThreatValue * TargetSpecialThreatCoefficientDefault;
}
// Is Defender house targeting Attacker House? if "yes" then more Threat
if (pTechnoOwner == HouseClass::Array.GetItem(pTarget->Owner->EnemyHouseIndex))
{
double const& EnemyHouseThreatBonus = RulesClass::Instance->EnemyHouseThreatBonus;
objectThreatValue += EnemyHouseThreatBonus;
}
// Extra threat based on current health. More damaged == More threat (almost destroyed objects gets more priority)
objectThreatValue += pTarget->Health * (1 - pTarget->GetHealthPercentage());
value = (objectThreatValue * threatMultiplier) / ((pTechno->DistanceFrom(pTarget) / (double)Unsorted::LeptonsPerCell) + 1.0);
if (pTechnoType->VHPScan == 1)
{
const auto estimatedHealth = pTarget->EstimatedHealth;
if (estimatedHealth <= 0)
value /= 2;
else if (estimatedHealth <= pTargetType->Strength / 2)
value *= 2;
}
if (calcThreatMode == 0)
{
// Is this object very FAR? then LESS THREAT against pTechno.
// More CLOSER? MORE THREAT for pTechno.
if (value > bestVal || bestVal < 0)
isGoodTarget = true;
}
else
{
// Is this object very FAR? then MORE THREAT against pTechno.
// More CLOSER? LESS THREAT for pTechno.
if (value < bestVal || bestVal < 0)
isGoodTarget = true;
}
break;
}
case 2:
case 3:
{
// Selection affected by distance
value = pTechno->DistanceFrom(pTarget); // Note: distance is in leptons (*256)
if (calcThreatMode == 2)
{
// Is this object very FAR? then LESS THREAT against pTechno.
// More CLOSER? MORE THREAT for pTechno.
if (value < bestVal || bestVal < 0)
isGoodTarget = true;
}
else
{
// Is this object very FAR? then MORE THREAT against pTechno.
// More CLOSER? LESS THREAT for pTechno.
if (value > bestVal || bestVal < 0)
isGoodTarget = true;
}
break;
}
default:
{
break;
}
}
if (isGoodTarget)
{
pBestObject = pTarget;
bestVal = value;
}
}
return pBestObject;
}
bool ScriptExt::EvaluateObjectWithMask(TechnoClass* pTechno, int mask, int attackAITargetType, int idxAITargetTypeItem, TechnoClass* pTeamLeader)
{
const auto pTechnoType = pTechno->GetTechnoType();
// Special case: validate target if is part of a technos list in [AITargetTypes] section
if (attackAITargetType >= 0)
{
const auto& lists = RulesExt::Global()->AITargetTypesLists;
if (lists.size() > static_cast<size_t>(attackAITargetType))
{
for (const auto& item : lists[attackAITargetType])
{
if (pTechnoType == item)
return true;
}
return false;
}
}
switch (mask)
{
case 1:
// Anything ;-)
if (!pTechno->Owner->IsNeutral())
return true;
break;
case 2:
// Building
if (!pTechno->Owner->IsNeutral())
{
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*, true>(pTechnoType))
{
if (!pBuildingType->IsVehicle())
return true;
}
}
break;
case 3:
// Harvester
if (!pTechno->Owner->IsNeutral())
{
switch (pTechno->WhatAmI())
{
case AbstractType::Unit:
if (static_cast<UnitTypeClass*>(pTechnoType)->Harvester)
return true;
// No break
case AbstractType::Building:
if (pTechnoType->ResourceGatherer)
return true;
break;
default:
break;
}
}
break;
case 4:
// Infantry
if (!pTechno->Owner->IsNeutral()
&& pTechno->WhatAmI() == AbstractType::Infantry)
{
return true;
}
break;
case 5:
// Vehicle, Aircraft, Deployed vehicle into structure
if (!pTechno->Owner->IsNeutral())
{
switch (pTechno->WhatAmI())
{
case AbstractType::Building:
if (!static_cast<BuildingTypeClass*>(pTechnoType)->IsVehicle())
break;
// No break
case AbstractType::Aircraft:
case AbstractType::Unit:
return true;
break;
default:
break;
}
}
break;
case 6:
// Factory
if (!pTechno->Owner->IsNeutral())
{
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*, true>(pTechnoType))
{
if (pBuildingType->Factory != AbstractType::None)
return true;
}
}
break;
case 7:
// Defense
if (!pTechno->Owner->IsNeutral())
{
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*, true>(pTechnoType))
{
if (pBuildingType->IsBaseDefense)
return true;
}
}
break;
case 8:
// House threats
if (pTeamLeader)
{
if (const auto pTarget = abstract_cast<TechnoClass*>(pTechno->Target))
{
// The possible Target is aiming against me? Revenge!
if (pTarget->Owner == pTeamLeader->Owner)
return true;
// Then check if this possible target is too near of the Team Leader
if (!pTechno->Owner->IsNeutral())
{
const int distanceToTarget = pTeamLeader->DistanceFrom(pTechno);
if (const auto pWeapon = TechnoExt::GetCurrentWeapon(pTechno, pTechnoType))
{
if (distanceToTarget <= (WeaponTypeExt::GetRangeWithModifiers(pWeapon, pTechno) * 4))
return true;
}
if (const auto pWeapon = TechnoExt::GetCurrentWeapon(pTechno, pTechnoType, true))
{
if (distanceToTarget <= (WeaponTypeExt::GetRangeWithModifiers(pWeapon, pTechno) * 4))
return true;
}
const int guardRange = pTeamLeader->GetTechnoType()->GuardRange;
if (guardRange > 0
&& distanceToTarget <= (guardRange * 2))
{
return true;
}
}
}
}
break;
case 9:
// Power Plant
if (!pTechno->Owner->IsNeutral())
{
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*, true>(pTechnoType))
{
if (pBuildingType->PowerBonus > 0)
return true;
}
}
break;
case 10:
// Occupied Building
if (const auto pBuilding = abstract_cast<BuildingClass*, true>(pTechno))
{
if (pBuilding->Occupants.Count > 0)
return true;
}
break;
case 11:
// Civilian Tech
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*, true>(pTechnoType))
{
if (pBuildingType->Capturable && pBuildingType->NeedsEngineer)
return true;
}
break;
case 12:
// Refinery
if (!pTechno->Owner->IsNeutral())
{
switch (pTechno->WhatAmI())
{
case AbstractType::Building:
if (static_cast<BuildingTypeClass*>(pTechnoType)->Refinery
|| pTechnoType->ResourceGatherer)
{
return true;
}
break;
case AbstractType::Unit:
if (!static_cast<UnitTypeClass*>(pTechnoType)->Harvester
&& pTechnoType->ResourceGatherer)
{
return true;
}
break;
default:
break;
}
}
break;
case 13:
// Mind Controller
if (!pTechno->Owner->IsNeutral())
{
if (const auto pWeapon = TechnoExt::GetCurrentWeapon(pTechno, pTechnoType))
{
if (pWeapon->Warhead->MindControl)
return true;
}
if (const auto pWeapon = TechnoExt::GetCurrentWeapon(pTechno, pTechnoType, true))
{
if (pWeapon->Warhead->MindControl)
return true;
}
}
break;
case 14:
// Aircraft and Air Unit including landed
if (!pTechno->Owner->IsNeutral()
&& (pTechno->WhatAmI() == AbstractType::Aircraft
|| pTechnoType->JumpJet
|| pTechno->IsInAir()))
{
return true;
}
break;
case 15:
// Naval Unit & Structure
if (!pTechno->Owner->IsNeutral()
&& (pTechnoType->Naval
|| pTechno->GetCell()->LandType == LandType::Water))
{
return true;
}
break;
case 16:
// Cloak Generator, Gap Generator, Radar Jammer or Inhibitor
if (!pTechno->Owner->IsNeutral())
{
const auto pTechnoTypeExt = TechnoTypeExt::ExtMap.Find(pTechnoType);
if (pTechnoTypeExt->RadarJamRadius > 0
|| pTechnoTypeExt->InhibitorRange.isset())
{
return true;
}
if (const auto pBuildingType = abstract_cast<BuildingTypeClass*, true>(pTechnoType))
{
if (pBuildingType->GapGenerator
|| pBuildingType->CloakGenerator)
{
return true;
}
}
}
break;
case 17:
// Ground Vehicle
if (!pTechno->Owner->IsNeutral())
{
switch (pTechno->WhatAmI())
{
case AbstractType::Building:
if (static_cast<BuildingTypeClass*>(pTechnoType)->IsVehicle()
&& !pTechnoType->Naval)
{
return true;
}
break;