Exemplo n.º 1
0
//Wyrmgus start
//static bool AiRepairBuilding(const CPlayer &player, const CUnitType &type, CUnit &building)
static bool AiRepairBuilding(const CPlayer &player, const CUnitType &type, CUnit &building)
//Wyrmgus end
{
	if (type.RepairRange == 0) {
		return false;
	}

	// We need to send all nearby free workers to repair that building
	// AI shouldn't send workers that are far away from repair point
	// Selection of mining workers.
	std::vector<CUnit *> table;
	FindPlayerUnitsByType(*AiPlayer->Player, type, table, true);
	int num = 0;
	for (size_t i = 0; i != table.size(); ++i) {
		CUnit &unit = *table[i];

		if (IsReadyToRepair(unit)) {
			table[num++] = &unit;
		}
	}
	table.resize(num);

	if (table.empty()) {
		return false;
	}
	TerrainTraversal terrainTraversal;

	//Wyrmgus start
//	terrainTraversal.SetSize(Map.Info.MapWidth, Map.Info.MapHeight);
	terrainTraversal.SetSize(Map.Info.MapWidths[building.MapLayer], Map.Info.MapHeights[building.MapLayer]);
	//Wyrmgus end
	terrainTraversal.Init();

	terrainTraversal.PushUnitPosAndNeighboor(building);

	const int maxRange = 15;
	const int movemask = type.MovementMask & ~(MapFieldLandUnit | MapFieldAirUnit | MapFieldSeaUnit);
	CUnit *unit = NULL;
	//Wyrmgus start
//	UnitFinder unitFinder(player, table, maxRange, movemask, &unit);
	UnitFinder unitFinder(player, table, maxRange, movemask, &unit, building.MapLayer);
	//Wyrmgus end

	if (terrainTraversal.Run(unitFinder) && unit != NULL) {
		const Vec2i invalidPos(-1, -1);
		//Wyrmgus start
//		CommandRepair(*unit, invalidPos, &building, FlushCommands);
		CommandRepair(*unit, invalidPos, &building, FlushCommands, building.MapLayer);
		//Wyrmgus end
		return true;
	}
	return false;
}
Exemplo n.º 2
0
/**
**  Check if we can repair the building.
**
**  @param type      Unit that can repair the building.
**  @param building  Building to be repaired.
**
**  @return          True if can repair, false if can't repair..
*/
static bool AiRepairBuilding(const CPlayer &player, const CUnitType &type, CUnit &building)
{
    if (type.RepairRange == 0) {
        return false;
    }

    // Remove all workers not mining. on the way building something
    // FIXME: It is not clever to use workers with gold
    // Idea: Antonis: Put the rest of the workers in a table in case
    // miners can't reach but others can. This will be useful if AI becomes
    // more flexible (e.g.: transports workers to an island)
    // FIXME: too hardcoded, not nice, needs improvement.
    // FIXME: too many workers repair the same building!

    // Selection of mining workers.
    std::vector<CUnit *> table;
    FindPlayerUnitsByType(*AiPlayer->Player, type, table);
    int num = 0;
    for (size_t i = 0; i != table.size(); ++i) {
        CUnit &unit = *table[i];

        if (IsReadyToRepair(unit)) {
            table[num++] = &unit;
        }
    }
    table.resize(num);

    if (table.empty()) {
        return false;
    }
    TerrainTraversal terrainTraversal;

    terrainTraversal.SetSize(Map.Info.MapWidth, Map.Info.MapHeight);
    terrainTraversal.Init();

    terrainTraversal.PushUnitPosAndNeighboor(building);

    const int maxRange = 100;
    const int movemask = type.MovementMask & ~(MapFieldLandUnit | MapFieldAirUnit | MapFieldSeaUnit);
    CUnit *unit = NULL;
    UnitFinder unitFinder(player, table, maxRange, movemask, &unit);

    if (terrainTraversal.Run(unitFinder) && unit != NULL) {
        const Vec2i invalidPos(-1, -1);
        CommandRepair(*unit, invalidPos, &building, FlushCommands);
        return true;
    }
    return false;
}