Skip to content

[MultiWeapon] Use WeaponX #1630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CREDITS.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ This page lists all the individual contributions to the project by their author.
- Fix `Hospital=yes` building can't kick out infantry after loading a save
- `Edit/Clear Hate-Value` Trigger Action
- `Set Force Enemy` Trigger Action
- Use WeaponX freely
- **NetsuNegi**:
- Forbidding parallel AI queues by type
- Jumpjet crash speed fix when crashing onto building
Expand Down
2 changes: 2 additions & 0 deletions Phobos.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<ClCompile Include="src\Ext\House\Hooks.ForceEnemy.cpp" />
<ClCompile Include="src\Ext\TechnoType\Hooks.MatrixOp.cpp" />
<ClCompile Include="src\Ext\Techno\Hooks.Airstrike.cpp" />
<ClCompile Include="src\Ext\Techno\Body.MultiWeapon.cpp" />
<ClCompile Include="src\Ext\Techno\Hooks.MultiWeapon.cpp" />
<ClCompile Include="src\Ext\Unit\Hooks.Harvester.cpp" />
<ClCompile Include="src\Ext\Unit\Hooks.Sinking.cpp" />
<ClCompile Include="src\Misc\Hooks.AlphaImage.cpp" />
Expand Down
16 changes: 16 additions & 0 deletions docs/New-or-Enhanced-Logics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,22 @@ AmphibiousEnter= ; boolean
AmphibiousUnload= ; boolean
```

### Multi Weapon

![Multi Weapon](_static/images/multiweapon.gif)
*`NoAmmoWeapon=2` can be used normally after `MultiWeapon=yes` in a private mod by @Stormsulfur*

- You are free to decide whether to use Weapon x or not, instead of passively using Primary/secondary.
- TechnoType reads `WeaponX` as their weapon when `MultiWeapon=yes`, be careful not to forget `WeaponCount`.
- `MultiWeapon.IsSecondary` can only be used for infantry and is responsible for determining which weapons should use `SecondaryFire` in the `Sequence`.

In `rulesmd.ini`:
```ini
[SOMETECHNO] ; TechnoType
MultiWeapon= ; boolean
MultiWeapon.IsSecondary= ; list of integer
```

## Terrain

### Destroy animation & sound
Expand Down
1 change: 1 addition & 0 deletions docs/Whats-New.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ New:
- Customize airstrike targets (by NetsuNegi)
- Aggressive attack move mission (by CrimRecya)
- Amphibious access vehicle (by CrimRecya)
- Use WeaponX freely (by FlyStar)

Vanilla fixes:
- Fixed sidebar not updating queued unit numbers when adding or removing units when the production is on hold (by CrimRecya)
Expand Down
Binary file added docs/_static/images/multiweapon.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
151 changes: 151 additions & 0 deletions src/Ext/Techno/Body.MultiWeapon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
#include "Body.h"

#include <InfantryClass.h>
#include <HouseClass.h>
#include <OverlayTypeClass.h>
#include <TerrainClass.h>

#include <Ext/BulletType/Body.h>
#include <Ext/WeaponType/Body.h>
#include <Utilities/GeneralUtils.h>
#include <Utilities/EnumFunctions.h>

bool TechnoExt::IsSecondary(TechnoClass* const pThis, const int& nWeaponIndex)
{
if (!pThis)
return false;

if (pThis->GetTechnoType()->IsGattling)
return nWeaponIndex != 0 && nWeaponIndex % 2 != 0;

const auto pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->GetTechnoType());

if (pTypeExt && pTypeExt->MultiWeapon.Get() &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do sanity check for pTypeExt

!pTypeExt->MultiWeapon_IsSecondary.empty())
{
int index = nWeaponIndex + 1;
return pTypeExt->MultiWeapon_IsSecondary.Contains(index);
}

return nWeaponIndex != 0;
}

// maybe it could be used as a new SelectWeapon, but not for now.
bool TechnoExt::CheckMultiWeapon(TechnoClass* const pThis, AbstractClass* const pTarget, WeaponTypeClass* const pWeaponType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this function is not being used yet?

{
if (!pThis || !pWeaponType || pWeaponType->NeverUse ||
!pWeaponType->Projectile || !pWeaponType->Warhead ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check if Projectile or Warhead exist since they'll still crash elsewhere

(pThis->InOpenToppedTransport && !pWeaponType->FireInTransport))
return false;

const auto pWH = pWeaponType->Warhead;

if (TechnoClass* pTargetTechno = abstract_cast<TechnoClass*>(pTarget))
{
if (pTargetTechno->Health <= 0 || !pTargetTechno->IsAlive)
return false;

if (const auto pTargetExt = TechnoExt::ExtMap.Find(pTargetTechno))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, no need to do sanity check for pTargetExt

{
const auto pShield = pTargetExt->Shield.get();

if (pShield && pShield->IsActive() &&
!pShield->CanBeTargeted(pWeaponType))
return false;
}

if (GeneralUtils::GetWarheadVersusArmor(pWH, pTargetTechno->GetTechnoType()->Armor) == 0.0)
return false;
Copy link
Contributor

@Coronia Coronia Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong when having a shield with a different armor that could be targeted. Should check it separately

if (pShield || pShield->IsActive())
{ // shield checking }
else if (GeneralUtils::GetWarheadVersusArmor(pWH, pTargetTechno->GetTechnoType()->Armor) == 0.0)
{ return false; }


if (pTargetTechno->IsInAir())
{
if (!pWeaponType->Projectile->AA)
return false;
}
else
{
const auto pBulletTypeExt = BulletTypeExt::ExtMap.Find(pWeaponType->Projectile);

if (!pBulletTypeExt->AAOnly.Get())
return false;
}

if (pWH->IsLocomotor && pTarget->WhatAmI() == AbstractType::Building)
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

put AA check and Locomotor check prior to armor check so there'll be more cases to bail out earlier with less calculation. Also Locomotor check can be put within ground target check since building can never fly


if (pWH->ElectricAssault)
{
if (!pThis->Owner->IsAlliedWith(pTargetTechno->Owner))
return false;

if (pTargetTechno->WhatAmI() != AbstractType::Building)
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check here since there's already a pBld check below


const auto pBld = abstract_cast<BuildingClass*>(pTargetTechno);

if (!pBld || !pBld->Type || !pBld->Type->Overpowerable)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check if pBld->Type exists

return false;
}

if (pWeaponType->DrainWeapon && (pTargetTechno->DrainingMe ||
!pTargetTechno->GetTechnoType()->Drainable))
return false;

if (pTargetTechno->AttachedBomb)
{
if (pWH->IvanBomb)
return false;

if (pWH->BombDisarm &&
!pThis->Owner->IsControlledByHuman() &&
!pThis->Owner->IsAlliedWith(pTargetTechno->Owner))
return false;
}
else
{
if (pWH->BombDisarm)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if (pWH->BombDisarm)

return false;
}

if (pWH->Airstrike && pTargetTechno->IsInAir())
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, could be put to above ground unit code block


if (pWH->MindControl && (pTargetTechno->Owner == pThis->Owner ||
pTargetTechno->IsMindControlled() || pTargetTechno->GetTechnoType()->ImmuneToPsionics))
return false;

if (pWH->Parasite && (pTargetTechno->WhatAmI() == AbstractType::Building ||
abstract_cast<FootClass*>(pTargetTechno)->ParasiteEatingMe))
return false;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, maybe armor versus check can be put to here to save as many as calculations

const auto pWeaponExt = WeaponTypeExt::ExtMap.Find(pWeaponType);

if (pWeaponExt && !pWeaponExt->HasRequiredAttachedEffects(pTargetTechno, pThis))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to do sanity check for pWeaponExt, but CanTarget/CanTargetHouse should be checked

return false;
}
else
{
if (pWH->ElectricAssault || pWH->BombDisarm || pWH->Airstrike || pWeaponType->DrainWeapon)
return false;

if (pTarget->WhatAmI() == AbstractType::Terrain)
{
if (!pWH->Wood)
return false;
}
else if (pTarget->WhatAmI() == AbstractType::Cell)
{
const auto pCell = abstract_cast<CellClass*>(pTarget);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if (const auto pCell = abstract_cast<CellClass*>(pTarget))


if (pCell && pCell->OverlayTypeIndex >= 0)
{
auto overlayType = OverlayTypeClass::Array.GetItem(pCell->OverlayTypeIndex);

if (overlayType->Wall && !pWH->Wall)
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should check CanTarget

}
}

return true;
}
3 changes: 3 additions & 0 deletions src/Ext/Techno/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,7 @@ class TechnoExt
static int GetWeaponIndexAgainstWall(TechnoClass* pThis, OverlayTypeClass* pWallOverlayType);
static void ApplyKillWeapon(TechnoClass* pThis, TechnoClass* pSource, WarheadTypeClass* pWH);
static void ApplyRevengeWeapon(TechnoClass* pThis, TechnoClass* pSource, WarheadTypeClass* pWH);

static bool CheckMultiWeapon(TechnoClass* const pThis, AbstractClass* const pTarget, WeaponTypeClass* const pWeaponType);
static bool IsSecondary(TechnoClass* const pThis, const int& nWeaponIndex);
};
14 changes: 14 additions & 0 deletions src/Ext/Techno/Hooks.Firing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ DEFINE_HOOK(0x6F3339, TechnoClass_WhatWeaponShouldIUse_Interceptor, 0x8)
return SkipGameCode;
}

DEFINE_HOOK(0x6F3360, TechnoClass_WhatWeaponShouldIUse_MultiWeapon, 0x6)
{
GET(TechnoClass*, pThis, ESI);
enum { SkipGameCode = 0x6F3379 };

auto const pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->GetTechnoType());

if (pTypeExt->MultiWeapon.Get() &&
(pThis->WhatAmI() != AbstractType::Unit || !pThis->GetTechnoType()->Gunner))
return SkipGameCode;

return 0;
}

DEFINE_HOOK(0x6F33CD, TechnoClass_WhatWeaponShouldIUse_ForceFire, 0x6)
{
enum { ReturnWeaponIndex = 0x6F37AF };
Expand Down
71 changes: 71 additions & 0 deletions src/Ext/Techno/Hooks.MultiWeapon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "Body.h"

// There was no way to do it, so I decided to leave it at that.
/*
bool _fastcall CanElectricAssault(FootClass* pThis)
{
const auto pType = pThis->GetTechnoType();
const auto pTypeExt = TechnoTypeExt::ExtMap.Find(pThis->GetTechnoType());
bool allowAssault = !pThis->GetTechnoType()->IsGattling &&
(pThis->WhatAmI() != AbstractType::Unit || !pThis->GetTechnoType()->Gunner);

if (pTypeExt->MultiWeapon.Get() && allowAssault)
{
for (int index = 0; index < pThis->GetTechnoType()->WeaponCount; index++)
{
const auto pWeaponType = pThis->Veterancy.IsElite() ?
pType->GetEliteWeapon(index)->WeaponType : pType->GetWeapon(index)->WeaponType;

if (!pWeaponType || !pWeaponType->Warhead ||
!pWeaponType->Warhead->ElectricAssault)
continue;

return true;
}
}
else if (allowAssault)
{
const auto secondary = pThis->GetWeapon(1)->WeaponType;

if (secondary && secondary->Warhead &&
secondary->Warhead->ElectricAssault)
return true;
}

return false;
}

DEFINE_HOOK(0x4D50E1, FootClass_Mission_Guard_ElectricAssult, 0xA)
{
GET(FootClass*, pThis, ESI);
enum { ElectricAssult = 0x4D5116, Skip = 0x4D51D4, SkipAll = 0x4D52F5 };

if (!pThis)
return SkipAll;

return CanElectricAssault(pThis) ? ElectricAssult : Skip;
}

DEFINE_HOOK(0x4D6F44, FootClass_Mission_AreaGuard_ElectricAssult, 0x6)
{
GET(FootClass*, pThis, ESI);
enum { ElectricAssult = 0x4D6F78, Skip = 0x4D7025, SkipAll = 0x4D715A };

if (!pThis)
return SkipAll;

return CanElectricAssault(pThis) ? ElectricAssult : Skip;
}
*/

// Do you think the infantry's way of determining that weapons are secondary is stupid ?
// I think it's kind of stupid.
DEFINE_HOOK(0x520888, InfantryClass_UpdateFiring_IsSecondary, 0x8)
{
GET(InfantryClass*, pThis, EBP);
GET(int, weaponIdx, EDI);
enum { Primary = 0x5208D6, Secondary = 0x520890 };

R->AL(pThis->Crawling);
return TechnoExt::IsSecondary(pThis, weaponIdx) ? Secondary : Primary;
}
7 changes: 6 additions & 1 deletion src/Ext/TechnoType/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void TechnoTypeExt::ExtData::ParseBurstFLHs(INI_EX& exArtINI, const char* pArtSe
char tempBuffer[32];
char tempBufferFLH[48];
auto pThis = this->OwnerObject();
bool parseMultiWeapons = pThis->TurretCount > 0 && pThis->WeaponCount > 0;
bool parseMultiWeapons = this->MultiWeapon.Get() || (pThis->TurretCount > 0 && pThis->WeaponCount > 0);
auto weaponCount = parseMultiWeapons ? pThis->WeaponCount : 2;
nFLH.resize(weaponCount);
nEFlh.resize(weaponCount);
Expand Down Expand Up @@ -1016,6 +1016,11 @@ void TechnoTypeExt::ExtData::Serialize(T& Stm)
.Process(this->Overload_DeathSound)
.Process(this->Overload_ParticleSys)
.Process(this->Overload_ParticleSysCount)

.Process(this->MultiWeapon)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong spacing

.Process(this->MultiWeapon_IsSecondary)
//.Process(this->MultiWeapon_SelectWeapon)
.Process(this->LastMultiWeapon)
;
}
void TechnoTypeExt::ExtData::LoadFromStream(PhobosStreamReader& Stm)
Expand Down
10 changes: 10 additions & 0 deletions src/Ext/TechnoType/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ class TechnoTypeExt
NullableIdx<VocClass> Overload_DeathSound;
Nullable<ParticleSystemTypeClass*> Overload_ParticleSys;
Valueable<int> Overload_ParticleSysCount;

Valueable<bool> MultiWeapon;
ValueableVector<int> MultiWeapon_IsSecondary;
//Valueable<bool> MultiWeapon_SelectWeapon;
bool LastMultiWeapon;

ExtData(TechnoTypeClass* OwnerObject) : Extension<TechnoTypeClass>(OwnerObject)
, HealthBar_Hide { false }
Expand Down Expand Up @@ -641,6 +646,11 @@ class TechnoTypeExt
, Overload_DeathSound {}
, Overload_ParticleSys {}
, Overload_ParticleSysCount { 5 }

, MultiWeapon { false }
, MultiWeapon_IsSecondary {}
//, MultiWeapon_SelectWeapon { false }
, LastMultiWeapon { false }
{ }

virtual ~ExtData() = default;
Expand Down
Loading