Skip to content

Commit deecb4c

Browse files
authored
[Minor] Fix ForceWeapon.InRange not taking effect when attacking non-techno (#1693)
- Also encapsulated the function for #1631 to use. > 顺便封装了函数,以便 #1631 使用。
1 parent 751a94e commit deecb4c

File tree

6 files changed

+82
-48
lines changed

6 files changed

+82
-48
lines changed

docs/New-or-Enhanced-Logics.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ FLHKEY.BurstN= ; integer - Forward,Lateral,Height. FLHKey refers to weapon-spec
14041404
- `ForceWeapon.Cloaked` forces specified weapon to be used against any cloaked targets.
14051405
- `ForceWeapon.Disguised` forces specified weapon to be used against any disguised targets.
14061406
- `ForceWeapon.UnderEMP` forces specified weapon to be used if the target is under EMP effect.
1407-
- `ForceWeapon.InRange` forces specified a list of weapons to be used once the target is within their `Range`. The first weapon in the listed order satisfied will be selected. Can be applied to both ground and air target if `ForceAAWeapon.InRange` is not set.
1407+
- `ForceWeapon.InRange` forces specified a list of weapons to be used once the target is within their `Range`. If `ForceWeapon.InRange.TechnoOnly` set to true, it'll only be forced on TechnoTypes like other forced weapons, otherwise it'll also be forced when attacking empty grounds. The first weapon in the listed order satisfied will be selected. Can be applied to both ground and air target if `ForceAAWeapon.InRange` is not set.
14081408
- `ForceAAWeapon.InRange` does the same thing but only for air target. Taking priority to `ForceWeapon.InRange`, which means that it can only be applied to ground target when they're both set.
14091409
- `Force(AA)Weapon.InRange.Overrides` overrides the range when decides which weapon to use. Value from position matching the position from `Force(AA)Weapon.InRange` is used if found, or the weapon's own `Range` if not found or set to a value below 0.
14101410
- If `Force(AA)Weapon.InRange.ApplyRangeModifiers` is set to true, any applicable weapon range modifiers from the firer are applied to the decision range.
@@ -1419,6 +1419,7 @@ ForceWeapon.UnderEMP=-1 ; integer. 0 for primary weapon,
14191419
ForceWeapon.InRange= ; List of integers. 0 for primary weapon, 1 for secondary weapon, -1 to disable
14201420
ForceWeapon.InRange.Overrides= ; List of floating-point values
14211421
ForceWeapon.InRange.ApplyRangeModifiers=false ; boolean
1422+
ForceWeapon.InRange.TechnoOnly=true ; boolean
14221423
ForceAAWeapon.InRange= ; List of integers. 0 for primary weapon, 1 for secondary weapon, -1 to disable
14231424
ForceAAWeapon.InRange.Overrides= ; List of floating-point values
14241425
ForceAAWeapon.InRange.ApplyRangeModifiers=false ; boolean

src/Ext/Techno/Body.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class TechnoExt
148148
bool HasAttachedEffects(std::vector<AttachEffectTypeClass*> attachEffectTypes, bool requireAll, bool ignoreSameSource, TechnoClass* pInvoker, AbstractClass* pSource, std::vector<int> const* minCounts, std::vector<int> const* maxCounts) const;
149149
int GetAttachedEffectCumulativeCount(AttachEffectTypeClass* pAttachEffectType, bool ignoreSameSource = false, TechnoClass* pInvoker = nullptr, AbstractClass* pSource = nullptr) const;
150150
void ApplyMindControlRangeLimit();
151-
int ApplyForceWeaponInRange(TechnoClass* pTarget);
151+
int ApplyForceWeaponInRange(AbstractClass* pTarget);
152152

153153
UnitTypeClass* GetUnitTypeExtra() const;
154154

src/Ext/Techno/Hooks.Firing.cpp

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212

1313
#pragma region TechnoClass_SelectWeapon
1414

15-
namespace ForceWeaponInRangeTemp
16-
{
17-
bool SelectWeaponByRange = false;
18-
}
19-
2015
DEFINE_HOOK(0x6F3339, TechnoClass_WhatWeaponShouldIUse_Interceptor, 0x8)
2116
{
2217
enum { SkipGameCode = 0x6F3341, ReturnValue = 0x6F3406 };
@@ -84,48 +79,14 @@ DEFINE_HOOK(0x6F3428, TechnoClass_WhatWeaponShouldIUse_ForceWeapon, 0x6)
8479
GET(TechnoClass*, pThis, ECX);
8580
GET_STACK(AbstractClass*, pTarget, STACK_OFFSET(0x18, 0x4));
8681

87-
if (ForceWeaponInRangeTemp::SelectWeaponByRange || !pThis)
88-
return 0;
82+
auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->GetTechnoType());
8983

90-
if (auto const pTargetTechno = abstract_cast<TechnoClass*>(pTarget))
84+
// Force weapon
85+
const int forceWeaponIndex = pTypeExt->SelectForceWeapon(pThis, pTarget);
86+
if (forceWeaponIndex >= 0)
9187
{
92-
int forceWeaponIndex = -1;
93-
auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->GetTechnoType());
94-
auto const pTargetType = pTargetTechno->GetTechnoType();
95-
96-
if (pTypeExt->ForceWeapon_Naval_Decloaked >= 0 &&
97-
pTargetType->Cloakable && pTargetType->Naval &&
98-
pTargetTechno->CloakState == CloakState::Uncloaked)
99-
{
100-
forceWeaponIndex = pTypeExt->ForceWeapon_Naval_Decloaked;
101-
}
102-
else if (pTypeExt->ForceWeapon_Cloaked >= 0 &&
103-
pTargetTechno->CloakState == CloakState::Cloaked)
104-
{
105-
forceWeaponIndex = pTypeExt->ForceWeapon_Cloaked;
106-
}
107-
else if (pTypeExt->ForceWeapon_Disguised >= 0 &&
108-
pTargetTechno->IsDisguised())
109-
{
110-
forceWeaponIndex = pTypeExt->ForceWeapon_Disguised;
111-
}
112-
else if (pTypeExt->ForceWeapon_UnderEMP >= 0 &&
113-
pTargetTechno->IsUnderEMP())
114-
{
115-
forceWeaponIndex = pTypeExt->ForceWeapon_UnderEMP;
116-
}
117-
else if (!pTypeExt->ForceWeapon_InRange.empty() || !pTypeExt->ForceAAWeapon_InRange.empty())
118-
{
119-
ForceWeaponInRangeTemp::SelectWeaponByRange = true;
120-
forceWeaponIndex = TechnoExt::ExtMap.Find(pThis)->ApplyForceWeaponInRange(pTargetTechno);
121-
ForceWeaponInRangeTemp::SelectWeaponByRange = false;
122-
}
123-
124-
if (forceWeaponIndex >= 0)
125-
{
126-
R->EAX(forceWeaponIndex);
127-
return UseWeaponIndex;
128-
}
88+
R->EAX(forceWeaponIndex);
89+
return UseWeaponIndex;
12990
}
13091

13192
return 0;

src/Ext/Techno/WeaponHelpers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void TechnoExt::ApplyRevengeWeapon(TechnoClass* pThis, TechnoClass* pSource, War
255255
}
256256
}
257257

258-
int TechnoExt::ExtData::ApplyForceWeaponInRange(TechnoClass* pTarget)
258+
int TechnoExt::ExtData::ApplyForceWeaponInRange(AbstractClass* pTarget)
259259
{
260260
int forceWeaponIndex = -1;
261261
auto const pThis = this->OwnerObject();

src/Ext/TechnoType/Body.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <Utilities/GeneralUtils.h>
1717

1818
TechnoTypeExt::ExtContainer TechnoTypeExt::ExtMap;
19+
bool TechnoTypeExt::SelectWeaponMutex = false;
1920

2021
void TechnoTypeExt::ExtData::Initialize()
2122
{
@@ -33,6 +34,54 @@ void TechnoTypeExt::ExtData::ApplyTurretOffset(Matrix3D* mtx, double factor)
3334
mtx->Translate(x, y, z);
3435
}
3536

37+
int TechnoTypeExt::ExtData::SelectForceWeapon(TechnoClass* pThis, AbstractClass* pTarget)
38+
{
39+
if (TechnoTypeExt::SelectWeaponMutex || !this->ForceWeapon_Check || !pTarget) // In theory, pTarget must exist
40+
return -1;
41+
42+
int forceWeaponIndex = -1;
43+
const auto pTargetTechno = abstract_cast<TechnoClass*>(pTarget);
44+
45+
if (pTargetTechno)
46+
{
47+
const auto pTargetType = pTargetTechno->GetTechnoType();
48+
49+
if (this->ForceWeapon_Naval_Decloaked >= 0
50+
&& pTargetType->Cloakable
51+
&& pTargetType->Naval
52+
&& pTargetTechno->CloakState == CloakState::Uncloaked)
53+
{
54+
forceWeaponIndex = this->ForceWeapon_Naval_Decloaked;
55+
}
56+
else if (this->ForceWeapon_Cloaked >= 0
57+
&& pTargetTechno->CloakState == CloakState::Cloaked)
58+
{
59+
forceWeaponIndex = this->ForceWeapon_Cloaked;
60+
}
61+
else if (this->ForceWeapon_Disguised >= 0
62+
&& pTargetTechno->IsDisguised())
63+
{
64+
forceWeaponIndex = this->ForceWeapon_Disguised;
65+
}
66+
else if (this->ForceWeapon_UnderEMP >= 0
67+
&& pTargetTechno->IsUnderEMP())
68+
{
69+
forceWeaponIndex = this->ForceWeapon_UnderEMP;
70+
}
71+
}
72+
73+
if (forceWeaponIndex == -1
74+
&& (pTargetTechno || !this->ForceWeapon_InRange_TechnoOnly)
75+
&& (!this->ForceWeapon_InRange.empty() || !this->ForceAAWeapon_InRange.empty()))
76+
{
77+
TechnoTypeExt::SelectWeaponMutex = true;
78+
forceWeaponIndex = TechnoExt::ExtMap.Find(pThis)->ApplyForceWeaponInRange(pTarget);
79+
TechnoTypeExt::SelectWeaponMutex = false;
80+
}
81+
82+
return forceWeaponIndex;
83+
}
84+
3685
// Ares 0.A source
3786
const char* TechnoTypeExt::ExtData::GetSelectionGroupID() const
3887
{
@@ -405,16 +454,27 @@ void TechnoTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
405454
this->DeployingAnim_UseUnitDrawer.Read(exINI, pSection, "DeployingAnim.UseUnitDrawer");
406455

407456
this->EnemyUIName.Read(exINI, pSection, "EnemyUIName");
457+
408458
this->ForceWeapon_Naval_Decloaked.Read(exINI, pSection, "ForceWeapon.Naval.Decloaked");
409459
this->ForceWeapon_Cloaked.Read(exINI, pSection, "ForceWeapon.Cloaked");
410460
this->ForceWeapon_Disguised.Read(exINI, pSection, "ForceWeapon.Disguised");
411461
this->ForceWeapon_UnderEMP.Read(exINI, pSection, "ForceWeapon.UnderEMP");
462+
this->ForceWeapon_InRange_TechnoOnly.Read(exINI, pSection, "ForceWeapon.InRange.TechnoOnly");
412463
this->ForceWeapon_InRange.Read(exINI, pSection, "ForceWeapon.InRange");
413464
this->ForceWeapon_InRange_Overrides.Read(exINI, pSection, "ForceWeapon.InRange.Overrides");
414465
this->ForceWeapon_InRange_ApplyRangeModifiers.Read(exINI, pSection, "ForceWeapon.InRange.ApplyRangeModifiers");
415466
this->ForceAAWeapon_InRange.Read(exINI, pSection, "ForceAAWeapon.InRange");
416467
this->ForceAAWeapon_InRange_Overrides.Read(exINI, pSection, "ForceAAWeapon.InRange.Overrides");
417468
this->ForceAAWeapon_InRange_ApplyRangeModifiers.Read(exINI, pSection, "ForceAAWeapon.InRange.ApplyRangeModifiers");
469+
this->ForceWeapon_Check = (
470+
this->ForceWeapon_Naval_Decloaked >= 0
471+
|| this->ForceWeapon_Cloaked >= 0
472+
|| this->ForceWeapon_Disguised >= 0
473+
|| this->ForceWeapon_UnderEMP >= 0
474+
|| !this->ForceWeapon_InRange.empty()
475+
|| !this->ForceAAWeapon_InRange.empty()
476+
);
477+
418478
this->Ammo_Shared.Read(exINI, pSection, "Ammo.Shared");
419479
this->Ammo_Shared_Group.Read(exINI, pSection, "Ammo.Shared.Group");
420480
this->SelfHealGainType.Read(exINI, pSection, "SelfHealGainType");
@@ -943,16 +1003,20 @@ void TechnoTypeExt::ExtData::Serialize(T& Stm)
9431003
.Process(this->DeployingAnim_UseUnitDrawer)
9441004

9451005
.Process(this->EnemyUIName)
1006+
1007+
.Process(this->ForceWeapon_Check)
9461008
.Process(this->ForceWeapon_Naval_Decloaked)
9471009
.Process(this->ForceWeapon_Cloaked)
9481010
.Process(this->ForceWeapon_Disguised)
9491011
.Process(this->ForceWeapon_UnderEMP)
1012+
.Process(this->ForceWeapon_InRange_TechnoOnly)
9501013
.Process(this->ForceWeapon_InRange)
9511014
.Process(this->ForceWeapon_InRange_Overrides)
9521015
.Process(this->ForceWeapon_InRange_ApplyRangeModifiers)
9531016
.Process(this->ForceAAWeapon_InRange)
9541017
.Process(this->ForceAAWeapon_InRange_Overrides)
9551018
.Process(this->ForceAAWeapon_InRange_ApplyRangeModifiers)
1019+
9561020
.Process(this->Ammo_Shared)
9571021
.Process(this->Ammo_Shared_Group)
9581022
.Process(this->SelfHealGainType)

src/Ext/TechnoType/Body.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,13 @@ class TechnoTypeExt
165165
Valueable<bool> DeployingAnim_UseUnitDrawer;
166166

167167
Valueable<CSFText> EnemyUIName;
168+
169+
bool ForceWeapon_Check;
168170
Valueable<int> ForceWeapon_Naval_Decloaked;
169171
Valueable<int> ForceWeapon_Cloaked;
170172
Valueable<int> ForceWeapon_Disguised;
171173
Valueable<int> ForceWeapon_UnderEMP;
174+
Valueable<bool> ForceWeapon_InRange_TechnoOnly;
172175
ValueableVector<int> ForceWeapon_InRange;
173176
ValueableVector<double> ForceWeapon_InRange_Overrides;
174177
Valueable<bool> ForceWeapon_InRange_ApplyRangeModifiers;
@@ -484,10 +487,12 @@ class TechnoTypeExt
484487
, VoiceCreated {}
485488
, VoicePickup {}
486489

490+
, ForceWeapon_Check { false }
487491
, ForceWeapon_Naval_Decloaked { -1 }
488492
, ForceWeapon_Cloaked { -1 }
489493
, ForceWeapon_Disguised { -1 }
490494
, ForceWeapon_UnderEMP { -1 }
495+
, ForceWeapon_InRange_TechnoOnly { true }
491496
, ForceWeapon_InRange {}
492497
, ForceWeapon_InRange_Overrides {}
493498
, ForceWeapon_InRange_ApplyRangeModifiers { false }
@@ -680,6 +685,8 @@ class TechnoTypeExt
680685

681686
void ApplyTurretOffset(Matrix3D* mtx, double factor = 1.0);
682687

688+
int SelectForceWeapon(TechnoClass* pThis, AbstractClass* pTarget);
689+
683690
// Ares 0.A
684691
const char* GetSelectionGroupID() const;
685692

@@ -699,6 +706,7 @@ class TechnoTypeExt
699706
};
700707

701708
static ExtContainer ExtMap;
709+
static bool SelectWeaponMutex;
702710

703711
static void ApplyTurretOffset(TechnoTypeClass* pType, Matrix3D* mtx, double factor = 1.0);
704712
static TechnoTypeClass* GetTechnoType(ObjectTypeClass* pType);

0 commit comments

Comments
 (0)