Skip to content

[Customized] Undeploy building on impact #1468

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

Merged
merged 23 commits into from
Mar 2, 2025
Merged
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 @@ -391,6 +391,7 @@ This page lists all the individual contributions to the project by their author.
- No turret unit turn to the target
- Damage multiplier for different houses
- Extended gattling rate down logic
- Sell or undeploy building on impact
- **Ollerus**:
- Build limit group enhancement
- Customizable rocker amplitude
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 @@ -1883,6 +1883,22 @@ In `rulesmd.ini`:
NotHuman.DeathSequence= ; integer (1 to 5)
```

### Sell or undeploy building on impact

- Warheads with `BuildingSell` can now sell buildings with build up image. It has a higher priority than `BuildingUndeploy`.
- `BuildingSell.IgnoreUnsellable` controls whether to ignore all possible situations where sales may be disabled except for build up image.
- Warheads with `BuildingUndeploy` can now undeploy buildings with `UndeploysInto`.
- `BuildingUndeploy.Leave` controls whether need to let them move to low threat locations nearby. The threat degree here is calculated using the technos' cost. If a nearby techno has no primary weapons or is owned by your allies, it will not be included.

In `rulesmd.ini`:
```ini
[SOMEWARHEAD] ; Warhead
BuildingSell=false ; boolean
BuildingSell.IgnoreUnsellable=false ; boolean
BuildingUndeploy=false ; boolean
BuildingUndeploy.Leave=false ; boolean
```

### Damage multiplier for different houses

- Warheads are now able to define the extra damage multiplier for owner house, ally houses and enemy houses. If the warhead's own `Damage(Owner|Allies|Enemies)Multiplier` are not set, these will default to respective `[CombatDamage] -> Damage(Owner|Allies|Enemies)Multiplier` which all default to 1.0 .Note that `DamageAlliesMultiplier` won't affect your own units like `AffectsAllies` did, and this function will not affect damage with ignore defenses like `Suicide`.etc .
Expand Down
1 change: 1 addition & 0 deletions docs/Whats-New.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ New:
- Damage multiplier for different houses (by CrimRecya)
- Customizable duration for electric bolts (by Starkku)
- Extended gattling rate down logic (by CrimRecya)
- Sell or undeploy building on impact (by CrimRecya)

Vanilla fixes:
- Prevent the units with locomotors that cause problems from entering the tank bunker (by TaranDahl)
Expand Down
2 changes: 1 addition & 1 deletion src/Ext/Building/Hooks.Selling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool __forceinline BuildingExt::CanUndeployOnSell(BuildingClass* pThis)
if (!GameModeOptionsClass::Instance->MCVRedeploy)
return false;
// or MindControlledBy YURIX (why? for balance?)
if (pThis->MindControlledBy || !pThis->Owner->IsControlledByHuman())
if (pThis->MindControlledBy)
return false;
}

Expand Down
14 changes: 13 additions & 1 deletion src/Ext/WarheadType/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ void WarheadTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
this->SuppressReflectDamage.Read(exINI, pSection, "SuppressReflectDamage");
this->SuppressReflectDamage_Types.Read(exINI, pSection, "SuppressReflectDamage.Types");

this->BuildingSell.Read(exINI, pSection, "BuildingSell");
this->BuildingSell_IgnoreUnsellable.Read(exINI, pSection, "BuildingSell.IgnoreUnsellable");
this->BuildingUndeploy.Read(exINI, pSection, "BuildingUndeploy");
this->BuildingUndeploy_Leave.Read(exINI, pSection, "BuildingUndeploy.Leave");

this->CombatAlert_Suppress.Read(exINI, pSection, "CombatAlert.Suppress");

// Convert.From & Convert.To
Expand Down Expand Up @@ -325,6 +330,8 @@ void WarheadTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
|| this->AttachEffects.AttachTypes.size() > 0
|| this->AttachEffects.RemoveTypes.size() > 0
|| this->AttachEffects.RemoveGroups.size() > 0
|| this->BuildingSell
|| this->BuildingUndeploy
);

char tempBuffer[32];
Expand Down Expand Up @@ -500,8 +507,13 @@ void WarheadTypeExt::ExtData::Serialize(T& Stm)
.Process(this->CLIsBlack)
.Process(this->Particle_AlphaImageIsLightFlash)

.Process(this->BuildingSell)
.Process(this->BuildingSell_IgnoreUnsellable)
.Process(this->BuildingUndeploy)
.Process(this->BuildingUndeploy_Leave)

.Process(this->CombatAlert_Suppress)

// Ares tags
.Process(this->AffectsEnemies)
.Process(this->AffectsOwner)
Expand Down
11 changes: 11 additions & 0 deletions src/Ext/WarheadType/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class WarheadTypeExt
Valueable<bool> SuppressReflectDamage;
ValueableVector<AttachEffectTypeClass*> SuppressReflectDamage_Types;

Valueable<bool> BuildingSell;
Valueable<bool> BuildingSell_IgnoreUnsellable;
Valueable<bool> BuildingUndeploy;
Valueable<bool> BuildingUndeploy_Leave;

Nullable<bool> CombatAlert_Suppress;

// Ares tags
Expand Down Expand Up @@ -313,6 +318,11 @@ class WarheadTypeExt
, SuppressReflectDamage { false }
, SuppressReflectDamage_Types {}

, BuildingSell { false }
, BuildingSell_IgnoreUnsellable { false }
, BuildingUndeploy { false }
, BuildingUndeploy_Leave { false }

, CombatAlert_Suppress {}

, AffectsEnemies { true }
Expand Down Expand Up @@ -363,6 +373,7 @@ class WarheadTypeExt
void ApplyCrit(HouseClass* pHouse, TechnoClass* pTarget, TechnoClass* Owner, TechnoExt::ExtData* pTargetExt);
void ApplyShieldModifiers(TechnoClass* pTarget, TechnoExt::ExtData* pTargetExt);
void ApplyAttachEffects(TechnoClass* pTarget, HouseClass* pInvokerHouse, TechnoClass* pInvoker);
void ApplyBuildingUndeploy(TechnoClass* pTarget);
double GetCritChance(TechnoClass* pFirer) const;
};

Expand Down
102 changes: 102 additions & 0 deletions src/Ext/WarheadType/Detonate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ void WarheadTypeExt::ExtData::DetonateOnOneUnit(HouseClass* pHouse, TechnoClass*
if (this->AttachEffects.AttachTypes.size() > 0 || this->AttachEffects.RemoveTypes.size() > 0 || this->AttachEffects.RemoveGroups.size() > 0)
this->ApplyAttachEffects(pTarget, pHouse, pOwner);

if (this->BuildingSell || this->BuildingUndeploy)
this->ApplyBuildingUndeploy(pTarget);

#ifdef LOCO_TEST_WARHEADS
if (this->InflictLocomotor)
this->ApplyLocomotorInfliction(pTarget);
Expand All @@ -180,6 +183,105 @@ void WarheadTypeExt::ExtData::DetonateOnOneUnit(HouseClass* pHouse, TechnoClass*

}

void WarheadTypeExt::ExtData::ApplyBuildingUndeploy(TechnoClass* pTarget)
{
const auto pBuilding = abstract_cast<BuildingClass*>(pTarget);

if (!pBuilding || !pBuilding->IsAlive || pBuilding->Health <= 0 || !pBuilding->IsOnMap || pBuilding->InLimbo)
return;

// Higher priority for selling
if (this->BuildingSell)
{
if ((pBuilding->CanBeSold() && !pBuilding->IsStrange()) || this->BuildingSell_IgnoreUnsellable)
pBuilding->Sell(1);

return;
}

// CanBeSold() will also check this
const auto mission = pBuilding->CurrentMission;

if (mission == Mission::Construction || mission == Mission::Selling)
return;

const auto pType = pBuilding->Type;

if (!pType->UndeploysInto || (pType->ConstructionYard && !GameModeOptionsClass::Instance->MCVRedeploy))
return;

auto cell = pBuilding->GetMapCoords();
const auto width = pType->GetFoundationWidth();
const auto height = pType->GetFoundationHeight(false);

// Offset of undeployment on large-scale buildings
if (width > 2 || height > 2)
cell += CellStruct { 1, 1 };

if (this->BuildingUndeploy_Leave)
{
const auto pHouse = pBuilding->Owner;
const auto pItems = Helpers::Alex::getCellSpreadItems(pBuilding->GetCoords(), 20);

// Divide the surrounding units into 16 directions and record their costs
int record[16] = {0};

for (const auto& pItem : pItems)
{
// Only armed units that are not considered allies will be recorded
if ((!pHouse || !pHouse->IsAlliedWith(pItem)) && pItem->IsArmed())
record[pBuilding->GetTargetDirection(pItem).GetValue<4>()] += pItem->GetTechnoType()->Cost;
}

int costs = 0;
int dir = 0;

// Starting from 16, prevent negative numbers
for (int i = 16; i < 32; ++i)
{
int newCosts = 0;

// Assign weights to values in the direction
// The highest value being 8 times in the positive direction
// And the lowest value being 0 times in the opposite direction
// The greater difference between positive direction to both sides, the lower value it is
for (int j = -7; j < 8; ++j)
newCosts += ((8 - std::abs(j)) * record[(i + j) & 15]);

// Record the direction with the highest weight
if (newCosts > costs)
{
dir = (i - 16);
costs = newCosts;
}
}

// If there is no threat in the surrounding area, randomly select one side
if (!costs)
dir = ScenarioClass::Instance->Random.RandomRanged(0, 15);

// Reverse the direction and convert it into radians
const double radian = -(((dir - 4) / 16.0) * Math::TwoPi);

// Base on a location about 14 grids away
cell.X -= static_cast<short>(14 * cos(radian));
cell.Y += static_cast<short>(14 * sin(radian));

// Find a location where the conyard can be deployed
const auto newCell = MapClass::Instance->NearByLocation(cell, pType->UndeploysInto->SpeedType, -1, pType->UndeploysInto->MovementZone,
false, (width + 2), (height + 2), false, false, false, false, CellStruct::Empty, false, false);

// If it can find a more suitable location, go to the new one
if (newCell != CellStruct::Empty)
cell = newCell;
}

if (const auto pCell = MapClass::Instance->TryGetCellAt(cell))
pBuilding->SetArchiveTarget(pCell);

pBuilding->Sell(1);
}

void WarheadTypeExt::ExtData::ApplyShieldModifiers(TechnoClass* pTarget, TechnoExt::ExtData* pTargetExt = nullptr)
{
if (!pTargetExt)
Expand Down