truelight@110: #include "stdafx.h" truelight@110: #include "ttd.h" tron@679: #include "map.h" tron@1209: #include "tile.h" truelight@110: #include "command.h" truelight@110: #include "ai.h" truelight@110: #include "engine.h" truelight@110: truelight@110: // Build HQ truelight@110: // Params: truelight@110: // tile : tile where HQ is going to be build truelight@110: bool AiNew_Build_CompanyHQ(Player *p, uint tile) { truelight@110: if (DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ) == CMD_ERROR) truelight@110: return false; truelight@110: DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ); truelight@110: return true; truelight@110: } truelight@110: truelight@110: // Build station truelight@110: // Params: truelight@110: // type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station truelight@110: // tile : tile where station is going to be build truelight@110: // length : in case of AI_TRAIN: length of station truelight@110: // numtracks : in case of AI_TRAIN: tracks of station truelight@110: // direction : the direction of the station truelight@110: // flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it) truelight@110: int AiNew_Build_Station(Player *p, byte type, uint tile, byte length, byte numtracks, byte direction, byte flag) { truelight@110: if (type == AI_TRAIN) truelight@110: return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION); truelight@110: else if (type == AI_BUS) truelight@110: return DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_BUS_STATION); truelight@110: else truelight@110: return DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRUCK_STATION); truelight@110: } truelight@110: truelight@110: // Builds a brdige. The second best out of the ones available for this player truelight@110: // Params: truelight@110: // tile_a : starting point truelight@110: // tile_b : end point truelight@110: // flag : flag passed to DoCommand truelight@110: int AiNew_Build_Bridge(Player *p, uint tile_a, uint tile_b, byte flag) { truelight@110: int bridge_type, bridge_len, type, type2; truelight@110: truelight@110: // Find a good bridgetype (the best money can buy) truelight@110: bridge_len = GetBridgeLength(tile_a, tile_b); truelight@110: type = type2 = 0; truelight@110: for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) { truelight@110: if (CheckBridge_Stuff(bridge_type, bridge_len)) { truelight@110: type2 = type; truelight@110: type = bridge_type; truelight@110: // We found two bridges, exit truelight@110: if (type2 != 0) truelight@110: break; truelight@110: } truelight@110: } truelight@110: // There is only one bridge that can be build.. truelight@110: if (type2 == 0 && type != 0) type2 = type; truelight@110: truelight@110: // Now, simply, build the bridge! truelight@110: if (p->ainew.tbt == AI_TRAIN) truelight@110: return DoCommandByTile(tile_a, tile_b, (0<<8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); truelight@110: else truelight@110: return DoCommandByTile(tile_a, tile_b, (0x80 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE); truelight@110: } truelight@110: truelight@110: truelight@110: // Build the route part by part truelight@110: // Basicly what this function do, is build that amount of parts of the route truelight@110: // that go in the same direction. It sets 'part' to the last part of the route builded. truelight@110: // The return value is the cost for the builded parts truelight@110: // truelight@110: // Params: truelight@110: // PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder truelight@110: // part : Which part we need to build truelight@110: // truelight@110: // TODO: skip already builded road-pieces (e.g.: cityroad) truelight@110: int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag) { truelight@110: int part = PathFinderInfo->position; truelight@110: byte *route_extra = PathFinderInfo->route_extra; truelight@110: TileIndex *route = PathFinderInfo->route; truelight@110: int dir; truelight@110: int old_dir = -1; truelight@110: int cost = 0; truelight@110: int res; truelight@110: // We need to calculate the direction with the parent of the parent.. so we skip truelight@110: // the first pieces and the last piece truelight@110: if (part < 1) part = 1; truelight@110: // When we are done, stop it truelight@110: if (part >= PathFinderInfo->route_length - 1) { PathFinderInfo->position = -2; return 0; } truelight@193: truelight@193: truelight@110: if (PathFinderInfo->rail_or_road) { truelight@110: // Tunnel code truelight@110: if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { truelight@110: cost += DoCommandByTile(route[part], 0, 0, flag, CMD_BUILD_TUNNEL); truelight@110: PathFinderInfo->position++; truelight@110: // TODO: problems! truelight@110: if (cost == CMD_ERROR) { truelight@110: DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!"); truelight@110: return 0; truelight@110: } truelight@110: return cost; truelight@110: } truelight@110: // Bridge code truelight@110: if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { truelight@110: cost += AiNew_Build_Bridge(p, route[part], route[part-1], flag); truelight@110: PathFinderInfo->position++; truelight@110: // TODO: problems! truelight@110: if (cost == CMD_ERROR) { truelight@110: DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!"); truelight@110: return 0; truelight@110: } truelight@110: return cost; truelight@110: } truelight@110: truelight@110: // Build normal rail truelight@110: // Keep it doing till we go an other way truelight@110: if (route_extra[part-1] == 0 && route_extra[part] == 0) { truelight@110: while (route_extra[part] == 0) { truelight@110: // Get the current direction truelight@110: dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]); truelight@110: // Is it the same as the last one? truelight@110: if (old_dir != -1 && old_dir != dir) break; truelight@110: old_dir = dir; truelight@110: // Build the tile truelight@110: res = DoCommandByTile(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL); truelight@110: if (res == CMD_ERROR) { truelight@110: // Problem.. let's just abort it all! truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return 0; truelight@110: } truelight@110: cost += res; truelight@110: // Go to the next tile truelight@110: part++; truelight@110: // Check if it is still in range.. truelight@110: if (part >= PathFinderInfo->route_length - 1) break; truelight@110: } truelight@110: part--; truelight@110: } truelight@110: // We want to return the last position, so we go back one truelight@110: PathFinderInfo->position = part; truelight@110: } else { truelight@110: // Tunnel code truelight@110: if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) { truelight@110: cost += DoCommandByTile(route[part], 0x200, 0, flag, CMD_BUILD_TUNNEL); truelight@110: PathFinderInfo->position++; truelight@110: // TODO: problems! truelight@110: if (cost == CMD_ERROR) { truelight@110: DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: tunnel could not be build!"); truelight@110: return 0; truelight@110: } truelight@110: return cost; truelight@110: } truelight@110: // Bridge code truelight@110: if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) { truelight@110: cost += AiNew_Build_Bridge(p, route[part], route[part+1], flag); truelight@110: PathFinderInfo->position++; truelight@110: // TODO: problems! truelight@110: if (cost == CMD_ERROR) { truelight@110: DEBUG(ai,0)("[AiNew - BuildPath] We have a serious problem: bridge could not be build!"); truelight@110: return 0; truelight@110: } truelight@110: return cost; truelight@110: } truelight@110: truelight@110: // Build normal road truelight@110: // Keep it doing till we go an other way truelight@110: // EnsureNoVehicle makes sure we don't build on a tile where a vehicle is. This way truelight@110: // it will wait till the vehicle is gone.. truelight@110: if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { truelight@110: while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicle(route[part]))) { truelight@110: // Get the current direction truelight@110: dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]); truelight@110: // Is it the same as the last one? truelight@110: if (old_dir != -1 && old_dir != dir) break; truelight@110: old_dir = dir; truelight@110: // There is already some road, and it is a bridge.. don't build!!! tron@1035: if (!IsTileType(route[part], MP_TUNNELBRIDGE)) { truelight@110: // Build the tile truelight@110: res = DoCommandByTile(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD); truelight@110: // Currently, we ignore CMD_ERRORs! tron@1035: if (res == CMD_ERROR && flag == DC_EXEC && !IsTileType(route[part], MP_STREET) && !EnsureNoVehicle(route[part])) { truelight@110: // Problem.. let's just abort it all! truelight@110: DEBUG(ai,0)("Darn, the route could not be builded.. aborting!"); truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return 0; truelight@110: } else { truelight@110: if (res != CMD_ERROR) truelight@110: cost += res; truelight@110: } truelight@110: } truelight@110: // Go to the next tile truelight@110: part++; truelight@110: // Check if it is still in range.. truelight@110: if (part >= PathFinderInfo->route_length - 1) break; truelight@110: } truelight@110: part--; truelight@110: // We want to return the last position, so we go back one truelight@110: } truelight@110: if (!EnsureNoVehicle(route[part]) && flag == DC_EXEC) part--; truelight@110: PathFinderInfo->position = part; truelight@110: } truelight@193: truelight@110: return cost; truelight@110: } truelight@110: truelight@110: // This functions tries to find the best vehicle for this type of cargo truelight@110: // It returns vehicle_id or -1 if not found truelight@110: int AiNew_PickVehicle(Player *p) { truelight@110: if (p->ainew.tbt == AI_TRAIN) { truelight@110: // Not supported yet truelight@110: return -1; truelight@110: } else { truelight@110: int start, count, i, r = CMD_ERROR; truelight@110: start = _cargoc.ai_roadveh_start[p->ainew.cargo]; truelight@110: count = _cargoc.ai_roadveh_count[p->ainew.cargo]; truelight@110: truelight@110: // Let's check it backwards.. we simply want to best engine available.. truelight@110: for (i=start+count-1;i>=start;i--) { truelight@110: // Is it availiable? truelight@110: // Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY truelight@110: if (!HASBIT(_engines[i].player_avail, _current_player) || _engines[i].reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue; truelight@110: // Can we build it? truelight@110: r = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH); truelight@110: if (r != CMD_ERROR) break; truelight@110: } truelight@110: // We did not find a vehicle :( truelight@110: if (r == CMD_ERROR) { return -1; } truelight@110: return i; truelight@110: } truelight@110: } truelight@110: truelight@110: // Builds the best vehicle possible truelight@110: int AiNew_Build_Vehicle(Player *p, uint tile, byte flag) { truelight@110: int i = AiNew_PickVehicle(p); truelight@110: if (i == -1) return CMD_ERROR; truelight@193: truelight@110: if (p->ainew.tbt == AI_TRAIN) { truelight@110: return CMD_ERROR; truelight@110: } else { truelight@110: return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH); truelight@110: } truelight@110: } truelight@110: truelight@110: int AiNew_Build_Depot(Player *p, uint tile, byte direction, byte flag) { truelight@110: static const byte _roadbits_by_dir[4] = {2,1,8,4}; truelight@110: int r, r2; truelight@110: if (p->ainew.tbt == AI_TRAIN) { truelight@110: return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT); truelight@110: } else { truelight@110: r = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT); truelight@110: if (r == CMD_ERROR) return r; truelight@110: // Try to build the road from the depot tron@900: r2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); truelight@110: // If it fails, ignore it.. truelight@110: if (r2 == CMD_ERROR) return r; truelight@110: return r + r2; truelight@110: } truelight@110: }