MCPcopy Create free account
hub / github.com/Phobos-developers/Phobos / UpdatePrimaryFactoryAI

Method UpdatePrimaryFactoryAI

src/Ext/Building/Body.cpp:91–187  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

89}
90
91void BuildingExt::ExtData::UpdatePrimaryFactoryAI()
92{
93 auto pOwner = this->OwnerObject()->Owner;
94
95 if (!pOwner || pOwner->ProducingAircraftTypeIndex < 0)
96 return;
97
98 AircraftTypeClass* pAircraft = AircraftTypeClass::Array.GetItem(pOwner->ProducingAircraftTypeIndex);
99 FactoryClass* currFactory = pOwner->GetFactoryProducing(pAircraft);
100 std::vector<BuildingClass*> airFactoryBuilding;
101 BuildingClass* newBuilding = nullptr;
102
103 // Update what is the current air factory for future comparisons
104 if (this->CurrentAirFactory)
105 {
106 int nDocks = 0;
107 if (this->CurrentAirFactory->Type)
108 nDocks = this->CurrentAirFactory->Type->NumberOfDocks;
109
110 int nOccupiedDocks = BuildingExt::CountOccupiedDocks(this->CurrentAirFactory);
111
112 if (nOccupiedDocks < nDocks)
113 currFactory = this->CurrentAirFactory->Factory;
114 else
115 this->CurrentAirFactory = nullptr;
116 }
117
118 // Obtain a list of air factories for optimizing the comparisons
119 for (auto pBuilding : pOwner->Buildings)
120 {
121 if (pBuilding->Type->Factory == AbstractType::AircraftType)
122 {
123 if (!currFactory && pBuilding->Factory)
124 currFactory = pBuilding->Factory;
125
126 airFactoryBuilding.emplace_back(pBuilding);
127 }
128 }
129
130 if (this->CurrentAirFactory)
131 {
132 for (auto pBuilding : airFactoryBuilding)
133 {
134 if (pBuilding == this->CurrentAirFactory)
135 {
136 this->CurrentAirFactory->Factory = currFactory;
137 this->CurrentAirFactory->IsPrimaryFactory = true;
138 }
139 else
140 {
141 pBuilding->IsPrimaryFactory = false;
142
143 if (pBuilding->Factory)
144 {
145 auto const* prodType = pBuilding->Factory->Object->GetType();
146 pBuilding->Factory->AbandonProduction();
147 Debug::Log("%s is not CurrentAirFactory of %s, production of %s aborted\n", pBuilding->Type->ID, pOwner->PlainName, prodType->ID);
148 }

Callers 1

Calls 1

GetTypeMethod · 0.45

Tested by

no test coverage detected