/** ** Check if we can build the building. ** ** @param type Unit that can build the building. ** @param building Building to be build. ** ** @return True if made, false if can't be made. ** ** @note We must check if the dependencies are fulfilled. */ static int AiBuildBuilding(const CUnitType &type, CUnitType &building, int near_x, int near_y) { CUnit *table[UnitMax]; int num = 0; // // Remove all workers on the way building something // const int nunits = FindPlayerUnitsByType(AiPlayer->Player, type, table); for (int i = 0; i < nunits; ++i) { CUnit& unit = *table[i]; int j; for (j = 0; j < unit.OrderCount; ++j) { int action = unit.Orders[j]->Action; if (action == UnitActionBuild || action == UnitActionRepair || action == UnitActionReturnGoods || (action == UnitActionResource && unit.SubAction > 55) /* SUB_START_GATHERING */) { break; } } if (j == unit.OrderCount) { table[num++] = &unit; } } if (num == 0) { // No workers available to build return 0; } CUnit& unit = (num == 1) ? *table[0] : *table[SyncRand() % num]; Vec2i pos; // Find a place to build. if (AiFindBuildingPlace(unit, building, near_x, near_y, &pos)) { CommandBuildBuilding(unit, pos, building, FlushCommands); return 1; } else { //when first worker can't build then rest also won't be able (save CPU) if (near_x != -1 && near_y != -1) { //Crush CPU !!!!! for (int i = 0; i < num && table[i] != &unit; ++i) { // Find a place to build. if (AiFindBuildingPlace(*table[i], building, near_x, near_y, &pos)) { CommandBuildBuilding(*table[i], pos, building, FlushCommands); return 1; } } } } return 0; }
/** ** Check if we can build the building. ** ** @param type Unit that can build the building. ** @param building Building to be build. ** ** @return True if made, false if can't be made. ** ** @note We must check if the dependencies are fulfilled. */ static int AiBuildBuilding(const CUnitType &type, CUnitType &building, const Vec2i &nearPos) { std::vector<CUnit *> table; FindPlayerUnitsByType(*AiPlayer->Player, type, table); int num = 0; // Remove all workers on the way building something for (size_t i = 0; i != table.size(); ++i) { CUnit &unit = *table[i]; if (IsAlreadyWorking(unit) == false) { table[num++] = &unit; } } if (num == 0) { // No workers available to build return 0; } CUnit &unit = (num == 1) ? *table[0] : *table[SyncRand() % num]; Vec2i pos; // Find a place to build. if (AiFindBuildingPlace(unit, building, nearPos, &pos)) { CommandBuildBuilding(unit, pos, building, FlushCommands); return 1; } else { //when first worker can't build then rest also won't be able (save CPU) if (Map.Info.IsPointOnMap(nearPos)) { //Crush CPU !!!!! for (int i = 0; i < num && table[i] != &unit; ++i) { // Find a place to build. if (AiFindBuildingPlace(*table[i], building, nearPos, &pos)) { CommandBuildBuilding(*table[i], pos, building, FlushCommands); return 1; } } } } return 0; }