1
0
Fork 0

(svn r1392) Use TileOffsByDir() instead of home brewed table

release/0.4.5
tron 2005-01-05 19:12:47 +00:00
parent 5583fd89b3
commit cc465f0f35
2 changed files with 16 additions and 23 deletions

7
ai.h
View File

@ -168,13 +168,6 @@ static const byte _illegal_curves[6] = {
3, // upright and downright are not valid 3, // upright and downright are not valid
}; };
static const TileIndexDiff _tiles_around[4] = {
TILE_XY(-1,0),
TILE_XY(0,1),
TILE_XY(1,0),
TILE_XY(0,-1),
};
enum { enum {
AI_STATE_STARTUP = 0, AI_STATE_STARTUP = 0,
AI_STATE_FIRST_TIME, AI_STATE_FIRST_TIME,

View File

@ -133,8 +133,8 @@ static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current,
int r, r2; int r, r2;
if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) { if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) {
// The station is pointing to a direction, add a tile towards that direction, so the H-value is more accurate // The station is pointing to a direction, add a tile towards that direction, so the H-value is more accurate
r = GetTileDist(current->tile, PathFinderInfo->end_tile_tl + _tiles_around[PathFinderInfo->end_direction]); r = GetTileDist(current->tile, PathFinderInfo->end_tile_tl + TileOffsByDir(PathFinderInfo->end_direction));
r2 = GetTileDist(current->tile, PathFinderInfo->end_tile_br + _tiles_around[PathFinderInfo->end_direction]); r2 = GetTileDist(current->tile, PathFinderInfo->end_tile_br + TileOffsByDir(PathFinderInfo->end_direction));
} else { } else {
// No direction, so just get the fastest route to the station // No direction, so just get the fastest route to the station
r = GetTileDist(current->tile, PathFinderInfo->end_tile_tl); r = GetTileDist(current->tile, PathFinderInfo->end_tile_tl);
@ -175,20 +175,20 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
// Go through all surrounding tiles and check if they are within the limits // Go through all surrounding tiles and check if they are within the limits
for (i=0;i<4;i++) { for (i=0;i<4;i++) {
if (GET_TILE_X(_tiles_around[i] + current->path.node.tile) > 1 && GET_TILE_X(_tiles_around[i] + current->path.node.tile) < MapMaxX() - 1 && if (GET_TILE_X(TileOffsByDir(i) + current->path.node.tile) > 1 && GET_TILE_X(TileOffsByDir(i) + current->path.node.tile) < MapMaxX() - 1 &&
GET_TILE_Y(_tiles_around[i] + current->path.node.tile) > 1 && GET_TILE_Y(_tiles_around[i] + current->path.node.tile) < MapMaxY() - 1) { GET_TILE_Y(TileOffsByDir(i) + current->path.node.tile) > 1 && GET_TILE_Y(TileOffsByDir(i) + current->path.node.tile) < MapMaxY() - 1) {
// We also directly test if the current tile can connect to this tile.. // We also directly test if the current tile can connect to this tile..
// We do this simply by just building the tile! // We do this simply by just building the tile!
// If the next step is a bridge, we have to enter it the right way // If the next step is a bridge, we have to enter it the right way
if (!PathFinderInfo->rail_or_road && AI_PATHFINDER_IS_ROAD(current->path.node.tile + _tiles_around[i])) { if (!PathFinderInfo->rail_or_road && AI_PATHFINDER_IS_ROAD(current->path.node.tile + TileOffsByDir(i))) {
if (IS_TILETYPE(current->path.node.tile + _tiles_around[i], MP_TUNNELBRIDGE)) { if (IS_TILETYPE(current->path.node.tile + TileOffsByDir(i), MP_TUNNELBRIDGE)) {
// An existing bridge... let's test the direction ;) // An existing bridge... let's test the direction ;)
if ((_map5[current->path.node.tile + _tiles_around[i]] & 1) != (i & 1)) continue; if ((_map5[current->path.node.tile + TileOffsByDir(i)] & 1) != (i & 1)) continue;
// This problem only is valid for tunnels: // This problem only is valid for tunnels:
// When the last tile was not yet a tunnel, check if we enter from the right side.. // When the last tile was not yet a tunnel, check if we enter from the right side..
if (!IS_TILETYPE(current->path.node.tile, MP_TUNNELBRIDGE) && (_map5[current->path.node.tile + _tiles_around[i]] & 0x80) == 0) { if (!IS_TILETYPE(current->path.node.tile, MP_TUNNELBRIDGE) && (_map5[current->path.node.tile + TileOffsByDir(i)] & 0x80) == 0) {
if (i != (_map5[current->path.node.tile + _tiles_around[i]] & 3)) continue; if (i != (_map5[current->path.node.tile + TileOffsByDir(i)] & 3)) continue;
} }
} }
} }
@ -213,14 +213,14 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
// If not, this means we are at the starting station // If not, this means we are at the starting station
if (PathFinderInfo->start_direction != AI_PATHFINDER_NO_DIRECTION) { if (PathFinderInfo->start_direction != AI_PATHFINDER_NO_DIRECTION) {
// We do need a direction? // We do need a direction?
if (AiNew_GetDirection(current->path.node.tile, current->path.node.tile + _tiles_around[i]) != PathFinderInfo->start_direction) if (AiNew_GetDirection(current->path.node.tile, current->path.node.tile + TileOffsByDir(i)) != PathFinderInfo->start_direction)
// We are not pointing the right way, invalid tile // We are not pointing the right way, invalid tile
continue; continue;
} }
} else if (current->path.node.user_data[0] == 0) { } else if (current->path.node.user_data[0] == 0) {
if (PathFinderInfo->rail_or_road) { if (PathFinderInfo->rail_or_road) {
// Rail check // Rail check
dir = AiNew_GetRailDirection(current->path.parent->node.tile, current->path.node.tile, current->path.node.tile + _tiles_around[i]); dir = AiNew_GetRailDirection(current->path.parent->node.tile, current->path.node.tile, current->path.node.tile + TileOffsByDir(i));
r = DoCommandByTile(current->path.node.tile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL); r = DoCommandByTile(current->path.node.tile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
if (r == CMD_ERROR) continue; if (r == CMD_ERROR) continue;
#ifdef AI_PATHFINDER_NO_90DEGREES_TURN #ifdef AI_PATHFINDER_NO_90DEGREES_TURN
@ -234,7 +234,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
#endif #endif
} else { } else {
// Road check // Road check
dir = AiNew_GetRoadDirection(current->path.parent->node.tile, current->path.node.tile, current->path.node.tile + _tiles_around[i]); dir = AiNew_GetRoadDirection(current->path.parent->node.tile, current->path.node.tile, current->path.node.tile + TileOffsByDir(i));
if (AI_PATHFINDER_IS_ROAD(current->path.node.tile)) { if (AI_PATHFINDER_IS_ROAD(current->path.node.tile)) {
if (IS_TILETYPE(current->path.node.tile, MP_TUNNELBRIDGE)) { if (IS_TILETYPE(current->path.node.tile, MP_TUNNELBRIDGE)) {
// We have a bridge, how nicely! We should mark it... // We have a bridge, how nicely! We should mark it...
@ -259,7 +259,7 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
} }
// The tile can be connected // The tile can be connected
aystar->neighbours[aystar->num_neighbours].tile = _tiles_around[i] + current->path.node.tile; aystar->neighbours[aystar->num_neighbours].tile = TileOffsByDir(i) + current->path.node.tile;
aystar->neighbours[aystar->num_neighbours].user_data[0] = 0; aystar->neighbours[aystar->num_neighbours].user_data[0] = 0;
aystar->neighbours[aystar->num_neighbours++].direction = 0; aystar->neighbours[aystar->num_neighbours++].direction = 0;
} }
@ -280,11 +280,11 @@ static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *curr
// Bridges can only be build on land that is not flat // Bridges can only be build on land that is not flat
// And if there is a road or rail blocking // And if there is a road or rail blocking
if (ti.tileh != 0 || if (ti.tileh != 0 ||
(PathFinderInfo->rail_or_road && IS_TILETYPE(tile + _tiles_around[dir], MP_STREET)) || (PathFinderInfo->rail_or_road && IS_TILETYPE(tile + TileOffsByDir(dir), MP_STREET)) ||
(!PathFinderInfo->rail_or_road && IS_TILETYPE(tile + _tiles_around[dir], MP_RAILWAY))) { (!PathFinderInfo->rail_or_road && IS_TILETYPE(tile + TileOffsByDir(dir), MP_RAILWAY))) {
for (;;) { for (;;) {
new_tile += _tiles_around[dir]; new_tile += TileOffsByDir(dir);
// Precheck, is the length allowed? // Precheck, is the length allowed?
if (!CheckBridge_Stuff(0,GetBridgeLength(tile, new_tile))) break; if (!CheckBridge_Stuff(0,GetBridgeLength(tile, new_tile))) break;