/** ** Check if there's a unit that should be repaired. */ static void AiCheckRepair() { int i; int j; int k; int n; bool repair_flag; n = AiPlayer->Player->TotalNumUnits; k = 0; // Selector for next unit for (i = n - 1; i >= 0; --i) { CUnit *unit = AiPlayer->Player->Units[i]; if (unit && UnitNumber(*unit) == AiPlayer->LastRepairBuilding) { k = i + 1; } } for (i = k; i < n; ++i) { CUnit &unit = *AiPlayer->Player->Units[i]; repair_flag = true; if (!unit.IsAliveOnMap()) { continue; } // Unit damaged? // Don't repair attacked unit (wait 5 sec before repairing) if (unit.Type->RepairHP && unit.CurrentAction() != UnitActionBuilt && unit.CurrentAction() != UnitActionUpgradeTo && unit.Variable[HP_INDEX].Value < unit.Variable[HP_INDEX].Max && unit.Attacked + 5 * CYCLES_PER_SECOND < GameCycle) { // // FIXME: Repair only units under control // if (AiEnemyUnitsInDistance(unit, unit.Stats->Variables[SIGHTRANGE_INDEX].Max)) { continue; } // // Must check, if there are enough resources // for (j = 1; j < MaxCosts; ++j) { if (unit.Stats->Costs[j] && AiPlayer->Player->Resources[j] < 99) { repair_flag = false; break; } } // // Find a free worker, who can build this building can repair it? // if (repair_flag) { AiRepairUnit(unit); AiPlayer->LastRepairBuilding = UnitNumber(unit); return; } } // Building under construction but no worker if (unit.CurrentAction() == UnitActionBuilt) { int j; for (j = 0; j < AiPlayer->Player->TotalNumUnits; ++j) { COrderPtr order = AiPlayer->Player->Units[j]->CurrentOrder(); if (order->Action == UnitActionRepair && order->GetGoal() == &unit) { break; } } if (j == AiPlayer->Player->TotalNumUnits) { // Make sure we have enough resources first for (j = 0; j < MaxCosts; ++j) { // FIXME: the resources don't necessarily have to be in storage if (AiPlayer->Player->Resources[j] < unit.Stats->Costs[j]) { break; } } if (j == MaxCosts) { AiRepairUnit(unit); AiPlayer->LastRepairBuilding = UnitNumber(unit); return; } } } } AiPlayer->LastRepairBuilding = 0; }
/** ** Check if there's a unit that should be repaired. */ static void AiCheckRepair() { const int n = AiPlayer->Player->GetUnitCount(); int k = 0; // Selector for next unit for (int i = n - 1; i >= 0; --i) { const CUnit &unit = AiPlayer->Player->GetUnit(i); if (UnitNumber(unit) == AiPlayer->LastRepairBuilding) { k = i + 1; } } for (int i = k; i < n; ++i) { CUnit &unit = AiPlayer->Player->GetUnit(i); bool repair_flag = true; if (!unit.IsAliveOnMap()) { continue; } // Unit damaged? // Don't repair attacked unit (wait 5 sec before repairing) if (unit.Type->RepairHP //Wyrmgus start // && unit.CurrentAction() != UnitActionBuilt && (unit.CurrentAction() != UnitActionBuilt || unit.Type->BoolFlag[BUILDEROUTSIDE_INDEX].value) //Wyrmgus end && unit.CurrentAction() != UnitActionUpgradeTo //Wyrmgus start // && unit.Variable[HP_INDEX].Value < unit.Variable[HP_INDEX].Max && unit.Variable[HP_INDEX].Value < unit.GetModifiedVariable(HP_INDEX, VariableMax) // && unit.Attacked + 5 * CYCLES_PER_SECOND < GameCycle) { ) { //Wyrmgus end // // FIXME: Repair only units under control // //Wyrmgus start // if (AiEnemyUnitsInDistance(unit, unit.Stats->Variables[SIGHTRANGE_INDEX].Max)) { if (AiEnemyUnitsInDistance(unit, unit.Variable[SIGHTRANGE_INDEX].Max, unit.MapLayer)) { //Wyrmgus end continue; } // // Must check, if there are enough resources // for (int j = 1; j < MaxCosts; ++j) { if (unit.Stats->Costs[j] && (AiPlayer->Player->Resources[j] + AiPlayer->Player->StoredResources[j]) < 99) { repair_flag = false; break; } } // // Find a free worker, who can build this building can repair it? // if (repair_flag) { AiRepairUnit(unit); AiPlayer->LastRepairBuilding = UnitNumber(unit); return; } } // Building under construction but no worker if (unit.CurrentAction() == UnitActionBuilt) { int j; for (j = 0; j < AiPlayer->Player->GetUnitCount(); ++j) { COrder *order = AiPlayer->Player->GetUnit(j).CurrentOrder(); if (order->Action == UnitActionRepair) { COrder_Repair &orderRepair = *static_cast<COrder_Repair *>(order); if (orderRepair.GetReparableTarget() == &unit) { break; } } } if (j == AiPlayer->Player->GetUnitCount()) { // Make sure we have enough resources first for (j = 0; j < MaxCosts; ++j) { // FIXME: the resources don't necessarily have to be in storage if (AiPlayer->Player->Resources[j] + AiPlayer->Player->StoredResources[j] < unit.Stats->Costs[j]) { break; } } if (j == MaxCosts) { AiRepairUnit(unit); AiPlayer->LastRepairBuilding = UnitNumber(unit); return; } } } } AiPlayer->LastRepairBuilding = 0; }