Skip to content

Commit

Permalink
Harvester counter improvements
Browse files Browse the repository at this point in the history
- Improve performance
- Fix bugs with considering harvester active in scenarios where it isn't e.g when being loaded into transport
  • Loading branch information
Starkku committed Feb 25, 2025
1 parent 99f9805 commit 962a8a4
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 21 deletions.
2 changes: 1 addition & 1 deletion YRpp
Submodule YRpp updated 5 files
+11 −0 ColorScheme.h
+9 −21 HashTable.h
+3 −5 MapClass.h
+2 −2 RadarClass.h
+1 −1 TechnoClass.h
2 changes: 1 addition & 1 deletion src/Commands/NextIdleHarvester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void NextIdleHarvesterCommandClass::Execute(WWKey eInput) const
{
if (auto pTypeExt = TechnoTypeExt::ExtMap.Find(pTechno->GetTechnoType()))
{
if (pTypeExt->Harvester_Counted.Get() && !TechnoExt::IsHarvesting(pTechno))
if (pTypeExt->Harvester_Counted && !TechnoExt::IsHarvesting(pTechno))
{
pObjectToSelect = pNextObject;
idleHarvestersPresent = true;
Expand Down
18 changes: 10 additions & 8 deletions src/Ext/House/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,11 @@ size_t HouseExt::FindBuildableIndex(
int HouseExt::ActiveHarvesterCount(HouseClass* pThis)
{
int result = 0;
auto const pExt = HouseExt::ExtMap.Find(pThis);

for (auto pTechno : *TechnoClass::Array)
for (auto const pTechno : pExt->OwnedCountedHarvesters)
{
if (pTechno->Owner == pThis)
{
auto pTypeExt = TechnoTypeExt::ExtMap.Find(pTechno->GetTechnoType());
result += pTypeExt->Harvester_Counted && TechnoExt::IsHarvesting(pTechno);
}
result += TechnoExt::IsHarvesting(pTechno);
}

return result;
Expand All @@ -294,9 +291,13 @@ int HouseExt::ActiveHarvesterCount(HouseClass* pThis)
int HouseExt::TotalHarvesterCount(HouseClass* pThis)
{
int result = 0;
auto const pExt = HouseExt::ExtMap.Find(pThis);

for (auto pType : RulesExt::Global()->HarvesterTypes)
result += pThis->CountOwnedAndPresent(pType);
for (auto const pTechno : pExt->OwnedCountedHarvesters)
{
auto const pExt = TechnoExt::ExtMap.Find(pTechno);

Check warning on line 298 in src/Ext/House/Body.cpp

View workflow job for this annotation

GitHub Actions / build

declaration of 'pExt' hides previous local declaration [D:\a\Phobos\Phobos\Phobos.vcxproj]

Check warning on line 298 in src/Ext/House/Body.cpp

View workflow job for this annotation

GitHub Actions / build

auto const pExt = TechnoExt::ExtMap.Find(pTechno); [D:\a\Phobos\Phobos\Phobos.vcxproj]

Check warning on line 298 in src/Ext/House/Body.cpp

View workflow job for this annotation

GitHub Actions / build

^ [D:\a\Phobos\Phobos\Phobos.vcxproj]
result += pExt->HasBeenPlacedOnMap;
}

return result;
}
Expand Down Expand Up @@ -607,6 +608,7 @@ void HouseExt::ExtData::Serialize(T& Stm)
Stm
.Process(this->PowerPlantEnhancers)
.Process(this->OwnedLimboDeliveredBuildings)
.Process(this->OwnedCountedHarvesters)
.Process(this->LimboAircraft)
.Process(this->LimboBuildings)
.Process(this->LimboInfantry)
Expand Down
2 changes: 2 additions & 0 deletions src/Ext/House/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class HouseExt
public:
std::map<int, int> PowerPlantEnhancers;
std::vector<BuildingClass*> OwnedLimboDeliveredBuildings;
std::vector<TechnoClass*> OwnedCountedHarvesters;

CounterClass LimboAircraft; // Currently owned aircraft in limbo
CounterClass LimboBuildings; // Currently owned buildings in limbo
Expand Down Expand Up @@ -66,6 +67,7 @@ class HouseExt
ExtData(HouseClass* OwnerObject) : Extension<HouseClass>(OwnerObject)
, PowerPlantEnhancers {}
, OwnedLimboDeliveredBuildings {}
, OwnedCountedHarvesters {}
, LimboAircraft {}
, LimboBuildings {}
, LimboInfantry {}
Expand Down
8 changes: 8 additions & 0 deletions src/Ext/House/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,14 @@ DEFINE_HOOK(0x7015C9, TechnoClass_Captured_UpdateTracking, 0x6)
pNewOwnerExt->AddToLimboTracking(pType);
}

if (pExt->TypeExtData->Harvester_Counted)
{
auto& vec = pOwnerExt->OwnedCountedHarvesters;
vec.erase(std::remove(vec.begin(), vec.end(), pThis), vec.end());

pNewOwnerExt->OwnedCountedHarvesters.push_back(pThis);
}

if (auto pMe = generic_cast<FootClass*>(pThis))
{
bool I_am_human = pThis->Owner->IsControlledByHuman();
Expand Down
1 change: 0 additions & 1 deletion src/Ext/Rules/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ void RulesExt::ExtData::Serialize(T& Stm)
Stm
.Process(this->AITargetTypesLists)
.Process(this->AIScriptsLists)
.Process(this->HarvesterTypes)
.Process(this->Storage_TiberiumIndex)
.Process(this->InfantryGainSelfHealCap)
.Process(this->UnitsGainSelfHealCap)
Expand Down
1 change: 0 additions & 1 deletion src/Ext/Rules/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class RulesExt
public:
std::vector<std::vector<TechnoTypeClass*>> AITargetTypesLists;
std::vector<std::vector<ScriptTypeClass*>> AIScriptsLists;
ValueableVector<TechnoTypeClass*> HarvesterTypes;

Valueable<int> Storage_TiberiumIndex;
Nullable<int> InfantryGainSelfHealCap;
Expand Down
7 changes: 7 additions & 0 deletions src/Ext/Techno/Body.Update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,13 @@ void TechnoExt::ExtData::UpdateTypeData(TechnoTypeClass* pCurrentType)
vec.erase(std::remove(vec.begin(), vec.end(), this), vec.end());
}

// Remove from harvesters list if no longer a harvester.
if (pOldTypeExt->Harvester_Counted && !!this->TypeExtData->Harvester_Counted)
{
auto& vec = HouseExt::ExtMap.Find(pThis->Owner)->OwnedCountedHarvesters;
vec.erase(std::remove(vec.begin(), vec.end(), pThis), vec.end());
}

// Remove from limbo reloaders if no longer applicable
if (pOldType->Ammo > 0 && pOldTypeExt->ReloadInTransport && !this->TypeExtData->ReloadInTransport)
{
Expand Down
50 changes: 46 additions & 4 deletions src/Ext/Techno/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ScenarioClass.h>

#include <Ext/Anim/Body.h>
#include <Ext/House/Body.h>
#include <Ext/Scenario/Body.h>
#include <Ext/WeaponType/Body.h>

Expand Down Expand Up @@ -33,6 +34,12 @@ TechnoExt::ExtData::~ExtData()

if (this->AnimRefCount > 0)
AnimExt::InvalidateTechnoPointers(pThis);

if (this->TypeExtData->Harvester_Counted)
{
auto& vec = HouseExt::ExtMap.Find(pThis->Owner)->OwnedCountedHarvesters;
vec.erase(std::remove(vec.begin(), vec.end(), pThis), vec.end());
}
}

bool TechnoExt::IsActiveIgnoreEMP(TechnoClass* pThis)
Expand All @@ -59,8 +66,9 @@ bool TechnoExt::IsHarvesting(TechnoClass* pThis)
if (!TechnoExt::IsActive(pThis))
return false;

auto slave = pThis->SlaveManager;
if (slave && slave->State != SlaveManagerStatus::Ready)
auto const pSlaveManager = pThis->SlaveManager;

if (pSlaveManager && pSlaveManager->State != SlaveManagerStatus::Ready)
return true;

if (pThis->WhatAmI() == AbstractType::Building)
Expand All @@ -71,12 +79,33 @@ bool TechnoExt::IsHarvesting(TechnoClass* pThis)
switch (pThis->GetCurrentMission())
{
case Mission::Harvest:
if (auto const pUnit = abstract_cast<UnitClass*>(pThis))
{
if (pUnit->HasAnyLink() && !TechnoExt::HasRadioLinkWithDock(pUnit)) // Probably still in factory.
return false;

if (pUnit->IsUseless) // Harvesters currently sitting without purpose are idle even if they are on harvest mission.
return false;
}
return true;
case Mission::Unload:
return true;
case Mission::Enter:
if (pThis->HasAnyLink())
{
auto const pLink = pThis->GetNthLink(0);

if (pLink->WhatAmI() != AbstractType::Building) // Enter mission + non-building link = not trying to unload
return false;
}
return true;
case Mission::Guard: // issue#603: not exactly correct, but idk how to do better
case Mission::Guard:
if (auto pUnit = abstract_cast<UnitClass*>(pThis))
return pUnit->IsHarvesting || pUnit->Locomotor->Is_Really_Moving_Now() || pUnit->HasAnyLink();
{
if (pUnit->ArchiveTarget && pUnit->GetStoragePercentage() > 0.0 && pUnit->Locomotor->Is_Moving()) // Edge-case, waiting to be able to unload.
return true;
}
return false;
default:
return false;
}
Expand All @@ -96,6 +125,19 @@ bool TechnoExt::HasAvailableDock(TechnoClass* pThis)
return false;
}

bool TechnoExt::HasRadioLinkWithDock(TechnoClass* pThis)
{
if (pThis->HasAnyLink())
{
auto const pLink = abstract_cast<BuildingClass*>(pThis->GetNthLink(0));

if (pLink && pThis->GetTechnoType()->Dock.FindItemIndex(pLink->Type) >= 0)
return true;
}

return false;
}

// Syncs Iron Curtain or Force Shield timer to another techno.
void TechnoExt::SyncInvulnerability(TechnoClass* pFrom, TechnoClass* pTo)
{
Expand Down
1 change: 1 addition & 0 deletions src/Ext/Techno/Body.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class TechnoExt

static bool IsHarvesting(TechnoClass* pThis);
static bool HasAvailableDock(TechnoClass* pThis);
static bool HasRadioLinkWithDock(TechnoClass* pThis);

static CoordStruct GetFLHAbsoluteCoords(TechnoClass* pThis, CoordStruct flh, bool turretFLH = false);

Expand Down
3 changes: 3 additions & 0 deletions src/Ext/Techno/Hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ DEFINE_HOOK(0x6F42F7, TechnoClass_Init, 0x2)
pExt->InitializeLaserTrails();
pExt->InitializeAttachEffects();

if (pExt->TypeExtData->Harvester_Counted)
HouseExt::ExtMap.Find(pThis->Owner)->OwnedCountedHarvesters.push_back(pThis);

return 0;
}

Expand Down
5 changes: 0 additions & 5 deletions src/Ext/TechnoType/Body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,6 @@ void TechnoTypeExt::ExtData::LoadFromINIFile(CCINIClass* const pINI)
this->Harvester_Counted.Read(exINI, pSection, "Harvester.Counted");
if (!this->Harvester_Counted.isset() && pThis->Enslaves)
this->Harvester_Counted = true;
if (this->Harvester_Counted.Get())
RulesExt::Global()->HarvesterTypes.AddUnique(pThis);

this->Promote_IncludeSpawns.Read(exINI, pSection, "Promote.IncludeSpawns");
this->ImmuneToCrit.Read(exINI, pSection, "ImmuneToCrit");
Expand Down Expand Up @@ -956,10 +954,7 @@ DEFINE_HOOK(0x747E90, UnitTypeClass_LoadFromINI, 0x5)
if (auto pTypeExt = TechnoTypeExt::ExtMap.Find(pItem))
{
if (!pTypeExt->Harvester_Counted.isset() && pItem->Harvester)
{
pTypeExt->Harvester_Counted = true;
RulesExt::Global()->HarvesterTypes.AddUnique(pItem);
}
}

return 0;
Expand Down

0 comments on commit 962a8a4

Please sign in to comment.