truelight@2381: /* $Id$ */ truelight@2381: truelight@2381: #include "../../stdafx.h" truelight@2381: #include "../../openttd.h" tron@3234: #include "../../bridge_map.h" truelight@2381: #include "../../debug.h" truelight@2381: #include "../../functions.h" truelight@2381: #include "../../map.h" truelight@2381: #include "../../tile.h" truelight@2381: #include "../../command.h" truelight@2381: #include "trolly.h" truelight@2381: #include "../../depot.h" tron@3179: #include "../../tunnel_map.h" celestar@3359: #include "../../bridge.h" truelight@2682: #include "../ai.h" truelight@2381: truelight@2381: #define TEST_STATION_NO_DIR 0xFF truelight@2381: truelight@2381: // Tests if a station can be build on the given spot truelight@2381: // TODO: make it train compatible truelight@2381: static bool TestCanBuildStationHere(TileIndex tile, byte dir) truelight@2381: { truelight@2381: Player *p = GetPlayer(_current_player); truelight@2381: truelight@2381: if (dir == TEST_STATION_NO_DIR) { truelight@2381: int32 ret; truelight@2381: // TODO: currently we only allow spots that can be access from al 4 directions... truelight@2381: // should be fixed!!! truelight@2381: for (dir = 0; dir < 4; dir++) { truelight@2381: ret = AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST); truelight@2381: if (!CmdFailed(ret)) return true; truelight@2381: } truelight@2381: return false; truelight@2381: } truelight@2381: truelight@2381: // return true if command succeeded, so the inverse of CmdFailed() truelight@2381: return !CmdFailed(AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST)); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: static bool IsRoad(TileIndex tile) truelight@2381: { truelight@2381: return truelight@2381: // MP_STREET, but not a road depot? truelight@2381: (IsTileType(tile, MP_STREET) && !IsTileDepotType(tile, TRANSPORT_ROAD)) || truelight@2381: (IsTileType(tile, MP_TUNNELBRIDGE) && ( tron@3184: (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) || tron@3234: (IsBridge(tile) && GetBridgeTransportType(tile) == TRANSPORT_ROAD) truelight@2381: )); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // Checks if a tile 'a' is between the tiles 'b' and 'c' truelight@2381: #define TILES_BETWEEN(a, b, c) (TileX(a) >= TileX(b) && TileX(a) <= TileX(c) && TileY(a) >= TileY(b) && TileY(a) <= TileY(c)) truelight@2381: truelight@2381: truelight@2381: // Check if the current tile is in our end-area truelight@2381: static int32 AyStar_AiPathFinder_EndNodeCheck(AyStar *aystar, OpenListNode *current) truelight@2381: { truelight@2381: Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; truelight@2381: // It is not allowed to have a station on the end of a bridge or tunnel ;) truelight@2381: if (current->path.node.user_data[0] != 0) return AYSTAR_DONE; truelight@2381: if (TILES_BETWEEN(current->path.node.tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) truelight@2381: if (IsTileType(current->path.node.tile, MP_CLEAR) || IsTileType(current->path.node.tile, MP_TREES)) truelight@2381: if (current->path.parent == NULL || TestCanBuildStationHere(current->path.node.tile, AiNew_GetDirection(current->path.parent->node.tile, current->path.node.tile))) truelight@2381: return AYSTAR_FOUND_END_NODE; truelight@2381: truelight@2381: return AYSTAR_DONE; truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // Calculates the hash truelight@2381: // Currently it is a 10 bit hash, so the hash array has a max depth of 6 bits (so 64) truelight@2381: static uint AiPathFinder_Hash(uint key1, uint key2) truelight@2381: { truelight@2381: return (TileX(key1) & 0x1F) + ((TileY(key1) & 0x1F) << 5); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // Clear the memory of all the things truelight@2381: static void AyStar_AiPathFinder_Free(AyStar *aystar) truelight@2381: { truelight@2381: AyStarMain_Free(aystar); truelight@2381: free(aystar); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent); truelight@2381: static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent); truelight@2381: static void AyStar_AiPathFinder_FoundEndNode(AyStar *aystar, OpenListNode *current); truelight@2381: static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *current); truelight@2381: truelight@2381: truelight@2381: // This creates the AiPathFinder truelight@2381: AyStar *new_AyStar_AiPathFinder(int max_tiles_around, Ai_PathFinderInfo *PathFinderInfo) truelight@2381: { truelight@2381: PathNode start_node; truelight@2381: uint x; truelight@2381: uint y; truelight@2381: // Create AyStar truelight@2381: AyStar *result = malloc(sizeof(AyStar)); truelight@2381: init_AyStar(result, AiPathFinder_Hash, 1 << 10); truelight@2381: // Set the function pointers truelight@2381: result->CalculateG = AyStar_AiPathFinder_CalculateG; truelight@2381: result->CalculateH = AyStar_AiPathFinder_CalculateH; truelight@2381: result->EndNodeCheck = AyStar_AiPathFinder_EndNodeCheck; truelight@2381: result->FoundEndNode = AyStar_AiPathFinder_FoundEndNode; truelight@2381: result->GetNeighbours = AyStar_AiPathFinder_GetNeighbours; truelight@2381: truelight@2381: result->free = AyStar_AiPathFinder_Free; truelight@2381: truelight@2381: // Set some information truelight@2381: result->loops_per_tick = AI_PATHFINDER_LOOPS_PER_TICK; truelight@2381: result->max_path_cost = 0; truelight@2381: result->max_search_nodes = AI_PATHFINDER_MAX_SEARCH_NODES; truelight@2381: truelight@2381: // Set the user_data to the PathFinderInfo truelight@2381: result->user_target = PathFinderInfo; truelight@2381: truelight@2381: // Set the start node truelight@2381: start_node.parent = NULL; truelight@2381: start_node.node.direction = 0; truelight@2381: start_node.node.user_data[0] = 0; truelight@2381: truelight@2381: // Now we add all the starting tiles truelight@2381: for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) { truelight@2381: for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) { truelight@2381: start_node.node.tile = TileXY(x, y); truelight@2381: result->addstart(result, &start_node.node, 0); truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: return result; truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // To reuse AyStar we sometimes have to clean all the memory truelight@2381: void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo) truelight@2381: { truelight@2381: PathNode start_node; truelight@2381: uint x; truelight@2381: uint y; truelight@2381: truelight@2381: aystar->clear(aystar); truelight@2381: truelight@2381: // Set the user_data to the PathFinderInfo truelight@2381: aystar->user_target = PathFinderInfo; truelight@2381: truelight@2381: // Set the start node truelight@2381: start_node.parent = NULL; truelight@2381: start_node.node.direction = 0; truelight@2381: start_node.node.user_data[0] = 0; truelight@2381: start_node.node.tile = PathFinderInfo->start_tile_tl; truelight@2381: truelight@2381: // Now we add all the starting tiles truelight@2381: for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) { truelight@2381: for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) { truelight@2381: if (!(IsTileType(TileXY(x, y), MP_CLEAR) || IsTileType(TileXY(x, y), MP_TREES))) continue; truelight@2381: if (!TestCanBuildStationHere(TileXY(x, y), TEST_STATION_NO_DIR)) continue; truelight@2381: start_node.node.tile = TileXY(x, y); truelight@2381: aystar->addstart(aystar, &start_node.node, 0); truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // The h-value, simple calculation truelight@2381: static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent) truelight@2381: { truelight@2381: Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; truelight@2381: int r, r2; truelight@2381: if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) { truelight@2381: // The station is pointing to a direction, add a tile towards that direction, so the H-value is more accurate truelight@2381: r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl + TileOffsByDir(PathFinderInfo->end_direction)); truelight@2381: r2 = DistanceManhattan(current->tile, PathFinderInfo->end_tile_br + TileOffsByDir(PathFinderInfo->end_direction)); truelight@2381: } else { truelight@2381: // No direction, so just get the fastest route to the station truelight@2381: r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl); truelight@2381: r2 = DistanceManhattan(current->tile, PathFinderInfo->end_tile_br); truelight@2381: } truelight@2381: // See if the bottomright is faster than the topleft.. truelight@2381: if (r2 < r) r = r2; truelight@2381: return r * AI_PATHFINDER_H_MULTIPLER; truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // We found the end.. let's get the route back and put it in an array truelight@2381: static void AyStar_AiPathFinder_FoundEndNode(AyStar *aystar, OpenListNode *current) truelight@2381: { truelight@2381: Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; truelight@2381: uint i = 0; truelight@2381: PathNode *parent = ¤t->path; truelight@2381: truelight@2381: do { truelight@2381: PathFinderInfo->route_extra[i] = parent->node.user_data[0]; truelight@2381: PathFinderInfo->route[i++] = parent->node.tile; truelight@2381: if (i > lengthof(PathFinderInfo->route)) { truelight@2381: // We ran out of space for the PathFinder truelight@2381: DEBUG(ai, 0)("[AiPathFinder] Ran out of space in the route[] array!!!"); truelight@2381: PathFinderInfo->route_length = -1; // -1 indicates out of space truelight@2381: return; truelight@2381: } truelight@2381: parent = parent->parent; truelight@2381: } while (parent != NULL); truelight@2381: PathFinderInfo->route_length = i; truelight@2381: DEBUG(ai, 1)("[Ai-PathFinding] Found route of %d nodes long in %d nodes of searching", i, Hash_Size(&aystar->ClosedListHash)); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // What tiles are around us. truelight@2381: static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *current) truelight@2381: { truelight@2381: uint i; truelight@2381: int ret; truelight@2381: int dir; truelight@2381: truelight@2381: Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; truelight@2381: truelight@2381: aystar->num_neighbours = 0; truelight@2381: truelight@2381: // Go through all surrounding tiles and check if they are within the limits truelight@2381: for (i = 0; i < 4; i++) { truelight@2381: TileIndex ctile = current->path.node.tile; // Current tile truelight@2381: TileIndex atile = ctile + TileOffsByDir(i); // Adjacent tile truelight@2381: truelight@2381: if (TileX(atile) > 1 && TileX(atile) < MapMaxX() - 1 && truelight@2381: TileY(atile) > 1 && TileY(atile) < MapMaxY() - 1) { truelight@2381: // We also directly test if the current tile can connect to this tile.. truelight@2381: // We do this simply by just building the tile! truelight@2381: truelight@2381: // If the next step is a bridge, we have to enter it the right way truelight@2381: if (!PathFinderInfo->rail_or_road && IsRoad(atile)) { truelight@2381: if (IsTileType(atile, MP_TUNNELBRIDGE)) { tron@3184: if (IsTunnel(atile)) { tron@3179: if (GetTunnelDirection(atile) != i) continue; tron@3184: } else { tron@3484: if ((_m[atile].m5 & 1U) != DiagDirToAxis(i)) continue; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: // But also if we are on a bridge, we can only move a certain direction truelight@2381: if (!PathFinderInfo->rail_or_road && IsRoad(ctile)) { truelight@2381: if (IsTileType(ctile, MP_TUNNELBRIDGE)) { truelight@2381: // An existing bridge/tunnel... let's test the direction ;) truelight@2381: if ((_m[ctile].m5 & 1U) != (i & 1)) continue; truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: if ((AI_PATHFINDER_FLAG_BRIDGE & current->path.node.user_data[0]) != 0 || truelight@2381: (AI_PATHFINDER_FLAG_TUNNEL & current->path.node.user_data[0]) != 0) { truelight@2381: // We are a bridge/tunnel, how cool!! truelight@2381: // This means we can only point forward.. get the direction from the user_data truelight@2381: if (i != (current->path.node.user_data[0] >> 8)) continue; truelight@2381: } truelight@2381: dir = 0; truelight@2381: truelight@2381: // First, check if we have a parent truelight@2381: if (current->path.parent == NULL && current->path.node.user_data[0] == 0) { truelight@2381: // If not, this means we are at the starting station truelight@2381: if (PathFinderInfo->start_direction != AI_PATHFINDER_NO_DIRECTION) { truelight@2381: // We do need a direction? truelight@2381: if (AiNew_GetDirection(ctile, atile) != PathFinderInfo->start_direction) { truelight@2381: // We are not pointing the right way, invalid tile truelight@2381: continue; truelight@2381: } truelight@2381: } truelight@2381: } else if (current->path.node.user_data[0] == 0) { truelight@2381: if (PathFinderInfo->rail_or_road) { truelight@2381: // Rail check truelight@2381: dir = AiNew_GetRailDirection(current->path.parent->node.tile, ctile, atile); truelight@2682: ret = AI_DoCommand(ctile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); truelight@2381: if (CmdFailed(ret)) continue; truelight@2381: #ifdef AI_PATHFINDER_NO_90DEGREES_TURN truelight@2381: if (current->path.parent->parent != NULL) { truelight@2381: // Check if we don't make a 90degree curve truelight@2381: int dir1 = AiNew_GetRailDirection(current->path.parent->parent->node.tile, current->path.parent->node.tile, ctile); truelight@2381: if (_illegal_curves[dir1] == dir || _illegal_curves[dir] == dir1) { truelight@2381: continue; truelight@2381: } truelight@2381: } truelight@2381: #endif truelight@2381: } else { truelight@2381: // Road check truelight@2381: dir = AiNew_GetRoadDirection(current->path.parent->node.tile, ctile, atile); truelight@2381: if (IsRoad(ctile)) { truelight@2381: if (IsTileType(ctile, MP_TUNNELBRIDGE)) { truelight@2381: // We have a bridge, how nicely! We should mark it... truelight@2381: dir = 0; truelight@2381: } else { truelight@2381: // It already has road.. check if we miss any bits! truelight@2381: if ((_m[ctile].m5 & dir) != dir) { truelight@2381: // We do miss some pieces :( truelight@2381: dir &= ~_m[ctile].m5; truelight@2381: } else { truelight@2381: dir = 0; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: // Only destruct things if it is MP_CLEAR of MP_TREES truelight@2381: if (dir != 0) { truelight@2682: ret = AI_DoCommand(ctile, dir, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD); truelight@2381: if (CmdFailed(ret)) continue; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: // The tile can be connected truelight@2381: aystar->neighbours[aystar->num_neighbours].tile = atile; truelight@2381: aystar->neighbours[aystar->num_neighbours].user_data[0] = 0; truelight@2381: aystar->neighbours[aystar->num_neighbours++].direction = 0; truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: // Next step, check for bridges and tunnels truelight@2381: if (current->path.parent != NULL && current->path.node.user_data[0] == 0) { truelight@2381: // First we get the dir from this tile and his parent tron@3644: DiagDirection dir = AiNew_GetDirection(current->path.parent->node.tile, current->path.node.tile); truelight@2381: // It means we can only walk with the track, so the bridge has to be in the same direction truelight@2381: TileIndex tile = current->path.node.tile; truelight@2381: TileIndex new_tile = tile; tron@3644: Slope tileh = GetTileSlope(tile, NULL); truelight@2381: truelight@2381: // Bridges can only be build on land that is not flat truelight@2381: // And if there is a road or rail blocking tron@3644: if (tileh != SLOPE_FLAT || truelight@2381: (PathFinderInfo->rail_or_road && IsTileType(tile + TileOffsByDir(dir), MP_STREET)) || truelight@2381: (!PathFinderInfo->rail_or_road && IsTileType(tile + TileOffsByDir(dir), MP_RAILWAY))) { truelight@2381: for (;;) { truelight@2381: new_tile += TileOffsByDir(dir); truelight@2381: truelight@2381: // Precheck, is the length allowed? truelight@2381: if (!CheckBridge_Stuff(0, GetBridgeLength(tile, new_tile))) break; truelight@2381: truelight@2381: // Check if we hit the station-tile.. we don't like that! truelight@2381: if (TILES_BETWEEN(new_tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) break; truelight@2381: truelight@2381: // Try building the bridge.. truelight@2682: ret = AI_DoCommand(tile, new_tile, (0 << 8) + (MAX_BRIDGES / 2), DC_AUTO, CMD_BUILD_BRIDGE); truelight@2381: if (CmdFailed(ret)) continue; truelight@2381: // We can build a bridge here.. add him to the neighbours truelight@2381: aystar->neighbours[aystar->num_neighbours].tile = new_tile; truelight@2381: aystar->neighbours[aystar->num_neighbours].user_data[0] = AI_PATHFINDER_FLAG_BRIDGE + (dir << 8); truelight@2381: aystar->neighbours[aystar->num_neighbours++].direction = 0; truelight@2381: // We can only have 12 neighbours, and we need 1 left for tunnels truelight@2381: if (aystar->num_neighbours == 11) break; truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: // Next, check for tunnels! tron@3644: // Tunnels can only be built on slopes corresponding to the direction truelight@2381: // For now, we check both sides for this tile.. terraforming gives fuzzy result tron@3644: if ((dir == DIAGDIR_NE && tileh == SLOPE_NE) || tron@3644: (dir == DIAGDIR_SE && tileh == SLOPE_SE) || tron@3644: (dir == DIAGDIR_SW && tileh == SLOPE_SW) || tron@3644: (dir == DIAGDIR_NW && tileh == SLOPE_NW)) { truelight@2381: // Now simply check if a tunnel can be build truelight@2682: ret = AI_DoCommand(tile, (PathFinderInfo->rail_or_road?0:0x200), 0, DC_AUTO, CMD_BUILD_TUNNEL); tron@3055: tileh = GetTileSlope(_build_tunnel_endtile, NULL); tron@3644: if (!CmdFailed(ret) && (tileh == SLOPE_SW || tileh == SLOPE_SE || tileh == SLOPE_NW || tileh == SLOPE_NE)) { truelight@2381: aystar->neighbours[aystar->num_neighbours].tile = _build_tunnel_endtile; truelight@2381: aystar->neighbours[aystar->num_neighbours].user_data[0] = AI_PATHFINDER_FLAG_TUNNEL + (dir << 8); truelight@2381: aystar->neighbours[aystar->num_neighbours++].direction = 0; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: tron@3644: extern uint GetRailFoundation(Slope tileh, TrackBits bits); // XXX function declaration in .c tron@3644: extern uint GetRoadFoundation(Slope tileh, uint bits); // XXX function declaration in .c tron@3644: extern uint GetBridgeFoundation(Slope tileh, Axis); // XXX function declaration in .c truelight@2381: enum { truelight@2381: BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12, truelight@2381: }; truelight@2381: truelight@2381: // The most important function: it calculates the g-value truelight@2381: static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent) truelight@2381: { truelight@2381: Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target; truelight@2381: int r, res = 0; tron@3644: Slope tileh = GetTileSlope(current->tile, NULL); tron@3644: Slope parent_tileh = GetTileSlope(parent->path.node.tile, NULL); truelight@2381: truelight@2381: // Check if we hit the end-tile truelight@2381: if (TILES_BETWEEN(current->tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) { truelight@2381: // We are at the end-tile, check if we had a direction or something... truelight@2381: if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION && AiNew_GetDirection(current->tile, parent->path.node.tile) != PathFinderInfo->end_direction) { truelight@2381: // We are not pointing the right way, invalid tile truelight@2381: return AYSTAR_INVALID_NODE; truelight@2381: } truelight@2381: // If it was valid, drop out.. we don't build on the endtile truelight@2381: return 0; truelight@2381: } truelight@2381: truelight@2381: // Give everything a small penalty truelight@2381: res += AI_PATHFINDER_PENALTY; truelight@2381: truelight@2381: if (!PathFinderInfo->rail_or_road) { truelight@2381: // Road has the lovely advantage it can use other road... check if truelight@2381: // the current tile is road, and if so, give a good bonus truelight@2381: if (IsRoad(current->tile)) { truelight@2381: res -= AI_PATHFINDER_ROAD_ALREADY_EXISTS_BONUS; truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: // We should give a penalty when the tile is going up or down.. this is one way to do so! truelight@2381: // Too bad we have to count it from the parent.. but that is not so bad. truelight@2381: // We also dislike long routes on slopes, since they do not look too realistic truelight@2381: // when there is a flat land all around, they are more expensive to build, and truelight@2381: // especially they essentially block the ability to connect or cross the road truelight@2381: // from one side. tron@3644: if (parent_tileh != SLOPE_FLAT && parent->path.parent != NULL) { truelight@2381: // Skip if the tile was from a bridge or tunnel truelight@2381: if (parent->path.node.user_data[0] == 0 && current->user_data[0] == 0) { truelight@2381: if (PathFinderInfo->rail_or_road) { tron@3055: r = GetRailFoundation(parent_tileh, 1 << AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile)); truelight@2381: // Maybe is BRIDGE_NO_FOUNDATION a bit strange here, but it contains just the right information.. tron@3644: if (r >= 15 || (r == 0 && HASBIT(BRIDGE_NO_FOUNDATION, tileh))) { truelight@2381: res += AI_PATHFINDER_TILE_GOES_UP_PENALTY; truelight@2381: } else { truelight@2381: res += AI_PATHFINDER_FOUNDATION_PENALTY; truelight@2381: } truelight@2381: } else { truelight@2381: if (!(IsRoad(parent->path.node.tile) && IsTileType(parent->path.node.tile, MP_TUNNELBRIDGE))) { tron@3055: r = GetRoadFoundation(parent_tileh, AiNew_GetRoadDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile)); truelight@2381: if (r >= 15 || r == 0) truelight@2381: res += AI_PATHFINDER_TILE_GOES_UP_PENALTY; truelight@2381: else truelight@2381: res += AI_PATHFINDER_FOUNDATION_PENALTY; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: // Are we part of a tunnel? truelight@2381: if ((AI_PATHFINDER_FLAG_TUNNEL & current->user_data[0]) != 0) { truelight@2381: // Tunnels are very expensive when build on long routes.. truelight@2381: // Ironicly, we are using BridgeCode here ;) truelight@2381: r = AI_PATHFINDER_TUNNEL_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile); truelight@2381: res += r + (r >> 8); truelight@2381: } truelight@2381: truelight@2381: // Are we part of a bridge? truelight@2381: if ((AI_PATHFINDER_FLAG_BRIDGE & current->user_data[0]) != 0) { truelight@2381: // That means for every length a penalty truelight@2381: res += AI_PATHFINDER_BRIDGE_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile); truelight@2381: // Check if we are going up or down, first for the starting point truelight@2381: // In user_data[0] is at the 8th bit the direction tron@3644: if (!HASBIT(BRIDGE_NO_FOUNDATION, parent_tileh)) { tron@3055: if (GetBridgeFoundation(parent_tileh, (current->user_data[0] >> 8) & 1) < 15) truelight@2381: res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; truelight@2381: } truelight@2381: // Second for the end point tron@3644: if (!HASBIT(BRIDGE_NO_FOUNDATION, tileh)) { tron@3055: if (GetBridgeFoundation(tileh, (current->user_data[0] >> 8) & 1) < 15) truelight@2381: res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; truelight@2381: } tron@3644: if (parent_tileh == SLOPE_FLAT) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; tron@3644: if (tileh == SLOPE_FLAT) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY; truelight@2381: } truelight@2381: truelight@2381: // To prevent the AI from taking the fastest way in tiles, but not the fastest way truelight@2381: // in speed, we have to give a good penalty to direction changing truelight@2381: // This way, we get almost the fastest way in tiles, and a very good speed on the track truelight@2381: if (!PathFinderInfo->rail_or_road) { truelight@2381: if (parent->path.parent != NULL && truelight@2381: AiNew_GetDirection(current->tile, parent->path.node.tile) != AiNew_GetDirection(parent->path.node.tile, parent->path.parent->node.tile)) { truelight@2381: // When road exists, we don't like turning, but its free, so don't be to piggy about it truelight@2381: if (IsRoad(parent->path.node.tile)) truelight@2381: res += AI_PATHFINDER_DIRECTION_CHANGE_ON_EXISTING_ROAD_PENALTY; truelight@2381: else truelight@2381: res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY; truelight@2381: } truelight@2381: } else { truelight@2381: // For rail we have 1 exeption: diagonal rail.. truelight@2381: // So we fetch 2 raildirection. That of the current one, and of the one before that truelight@2381: if (parent->path.parent != NULL && parent->path.parent->parent != NULL) { truelight@2381: int dir1 = AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile); truelight@2381: int dir2 = AiNew_GetRailDirection(parent->path.parent->parent->node.tile, parent->path.parent->node.tile, parent->path.node.tile); truelight@2381: // First, see if we are on diagonal path, that is better than straight path tron@3033: if (dir1 > 1) res -= AI_PATHFINDER_DIAGONAL_BONUS; truelight@2381: truelight@2381: // First see if they are different truelight@2381: if (dir1 != dir2) { truelight@2381: // dir 2 and 3 are 1 diagonal track, and 4 and 5. truelight@2381: if (!(((dir1 == 2 || dir1 == 3) && (dir2 == 2 || dir2 == 3)) || ((dir1 == 4 || dir1 == 5) && (dir2 == 4 || dir2 == 5)))) { truelight@2381: // It is not, so we changed of direction truelight@2381: res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY; truelight@2381: } truelight@2381: if (parent->path.parent->parent->parent != NULL) { truelight@2381: int dir3 = AiNew_GetRailDirection(parent->path.parent->parent->parent->node.tile, parent->path.parent->parent->node.tile, parent->path.parent->node.tile); truelight@2381: // Check if we changed 3 tiles of direction in 3 tiles.. bad!!! truelight@2381: if ((dir1 == 0 || dir1 == 1) && dir2 > 1 && (dir3 == 0 || dir3 == 1)) { truelight@2381: res += AI_PATHFINDER_CURVE_PENALTY; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: tron@2549: return (res < 0) ? 0 : res; truelight@2381: }