| 89 | } |
| 90 | |
| 91 | void 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 | } |
no test coverage detected