ai_pathfinder.c
author tron
Thu, 21 Jul 2005 18:44:27 +0000
changeset 2153 ecfc674410b4
parent 2096 32de4f127e79
child 2163 b17b313113a0
permissions -rw-r--r--
(svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     1
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1777
diff changeset
     2
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1245
diff changeset
     3
#include "debug.h"
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 193
diff changeset
     4
#include "map.h"
1209
2e00193652b2 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1095
diff changeset
     5
#include "tile.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
     6
#include "command.h"
2096
32de4f127e79 (svn r2606) -Codechange: renamed ai.c to ai_old.c, and ai.h to ai_new.h to make room
truelight
parents: 2049
diff changeset
     7
#include "ai_new.h"
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1299
diff changeset
     8
#include "depot.h"
2153
ecfc674410b4 (svn r2663) Include variables.h only in these files which need it, not globally via openttd.h
tron
parents: 2096
diff changeset
     9
#include "variables.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    10
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    11
#define TEST_STATION_NO_DIR 0xFF
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    12
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    13
// Tests if a station can be build on the given spot
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    14
// TODO: make it train compatible
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
    15
static bool TestCanBuildStationHere(TileIndex tile, byte dir)
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    16
{
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1891
diff changeset
    17
	Player *p = GetPlayer(_current_player);
1713
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    18
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    19
	if (dir == TEST_STATION_NO_DIR) {
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    20
		int32 ret;
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    21
		// TODO: currently we only allow spots that can be access from al 4 directions...
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    22
		//  should be fixed!!!
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    23
		for (dir = 0; dir < 4; dir++) {
1713
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    24
			ret = AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST);
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    25
			if (!CmdFailed(ret)) return true;
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    26
		}
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    27
		return false;
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    28
	}
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    29
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    30
	// return true if command succeeded, so the inverse of CmdFailed()
659ca3025cc5 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1617
diff changeset
    31
	return !CmdFailed(AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST));
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    32
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    33
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    34
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    35
static bool IsRoad(TileIndex tile)
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    36
{
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    37
	return
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    38
		// MP_STREET, but not a road depot?
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    39
		(IsTileType(tile, MP_STREET) && !IsTileDepotType(tile, TRANSPORT_ROAD)) ||
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    40
		(IsTileType(tile, MP_TUNNELBRIDGE) && (
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    41
			// road tunnel?
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
    42
			((_m[tile].m5 & 0x80) == 0 && (_m[tile].m5 & 0x4) == 0x4) ||
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    43
			// road bridge?
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
    44
			((_m[tile].m5 & 0x80) != 0 && (_m[tile].m5 & 0x2) == 0x2)
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    45
		));
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    46
}
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    47
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
    48
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    49
// Checks if a tile 'a' is between the tiles 'b' and 'c'
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 906
diff changeset
    50
#define TILES_BETWEEN(a, b, c) (TileX(a) >= TileX(b) && TileX(a) <= TileX(c) && TileY(a) >= TileY(b) && TileY(a) <= TileY(c))
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    51
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    52
// Check if the current tile is in our end-area
1617
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    53
static int32 AyStar_AiPathFinder_EndNodeCheck(AyStar *aystar, OpenListNode *current)
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    54
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    55
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    56
	// It is not allowed to have a station on the end of a bridge or tunnel ;)
1617
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    57
	if (current->path.node.user_data[0] != 0) return AYSTAR_DONE;
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    58
	if (TILES_BETWEEN(current->path.node.tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br))
c3d3caad6d1e (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    59
		if (IsTileType(current->path.node.tile, MP_CLEAR) || IsTileType(current->path.node.tile, MP_TREES))
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    60
			if (current->path.parent == NULL || TestCanBuildStationHere(current->path.node.tile, AiNew_GetDirection(current->path.parent->node.tile, current->path.node.tile)))
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    61
				return AYSTAR_FOUND_END_NODE;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
    62
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    63
	return AYSTAR_DONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    64
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    65
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    66
// Calculates the hash
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    67
//   Currently it is a 10 bit hash, so the hash array has a max depth of 6 bits (so 64)
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    68
static uint AiPathFinder_Hash(uint key1, uint key2)
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    69
{
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 906
diff changeset
    70
	return (TileX(key1) & 0x1F) + ((TileY(key1) & 0x1F) << 5);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    71
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    73
// Clear the memory of all the things
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    74
static void AyStar_AiPathFinder_Free(AyStar *aystar)
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    75
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    76
	AyStarMain_Free(aystar);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    77
	free(aystar);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    78
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    79
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    80
static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    81
static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    82
static void AyStar_AiPathFinder_FoundEndNode(AyStar *aystar, OpenListNode *current);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    83
static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *current);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    84
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    85
// This creates the AiPathFinder
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    86
AyStar *new_AyStar_AiPathFinder(int max_tiles_around, Ai_PathFinderInfo *PathFinderInfo)
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    87
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    88
	PathNode start_node;
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    89
	uint x;
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    90
	uint y;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    91
	// Create AyStar
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    92
	AyStar *result = malloc(sizeof(AyStar));
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    93
	init_AyStar(result, AiPathFinder_Hash, 1 << 10);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    94
	// Set the function pointers
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    95
	result->CalculateG = AyStar_AiPathFinder_CalculateG;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    96
	result->CalculateH = AyStar_AiPathFinder_CalculateH;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    97
	result->EndNodeCheck = AyStar_AiPathFinder_EndNodeCheck;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    98
	result->FoundEndNode = AyStar_AiPathFinder_FoundEndNode;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    99
	result->GetNeighbours = AyStar_AiPathFinder_GetNeighbours;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   100
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 1981
diff changeset
   101
	result->BeforeExit = NULL;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 1981
diff changeset
   102
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   103
	result->free = AyStar_AiPathFinder_Free;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   104
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   105
	// Set some information
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   106
	result->loops_per_tick = AI_PATHFINDER_LOOPS_PER_TICK;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   107
	result->max_path_cost = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   108
	result->max_search_nodes = AI_PATHFINDER_MAX_SEARCH_NODES;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   109
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   110
	// Set the user_data to the PathFinderInfo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   111
	result->user_target = PathFinderInfo;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   112
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   113
	// Set the start node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   114
	start_node.parent = NULL;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   115
	start_node.node.direction = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   116
	start_node.node.user_data[0] = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   117
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   118
	// Now we add all the starting tiles
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 906
diff changeset
   119
	for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) {
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 906
diff changeset
   120
		for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) {
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1977
diff changeset
   121
			start_node.node.tile = TileXY(x, y);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1729
diff changeset
   122
			result->addstart(result, &start_node.node, 0);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   123
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   124
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   125
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   126
	return result;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   127
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   128
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   129
// To reuse AyStar we sometimes have to clean all the memory
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   130
void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo)
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   131
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   132
	PathNode start_node;
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   133
	uint x;
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   134
	uint y;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   135
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   136
	aystar->clear(aystar);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   137
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   138
	// Set the user_data to the PathFinderInfo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   139
	aystar->user_target = PathFinderInfo;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   140
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   141
	// Set the start node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   142
	start_node.parent = NULL;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   143
	start_node.node.direction = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   144
	start_node.node.user_data[0] = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   145
	start_node.node.tile = PathFinderInfo->start_tile_tl;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   146
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   147
	// Now we add all the starting tiles
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 906
diff changeset
   148
	for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) {
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 906
diff changeset
   149
		for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) {
1981
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1977
diff changeset
   150
			if (!(IsTileType(TileXY(x, y), MP_CLEAR) || IsTileType(TileXY(x, y), MP_TREES))) continue;
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1977
diff changeset
   151
			if (!TestCanBuildStationHere(TileXY(x, y), TEST_STATION_NO_DIR)) continue;
3c9c682f1212 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1977
diff changeset
   152
			start_node.node.tile = TileXY(x, y);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1729
diff changeset
   153
			aystar->addstart(aystar, &start_node.node, 0);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   154
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   155
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   156
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   157
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   158
// The h-value, simple calculation
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   159
static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   160
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   161
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   162
	int r, r2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   163
	if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   164
		// The station is pointing to a direction, add a tile towards that direction, so the H-value is more accurate
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   165
		r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl + TileOffsByDir(PathFinderInfo->end_direction));
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   166
		r2 = DistanceManhattan(current->tile, PathFinderInfo->end_tile_br + TileOffsByDir(PathFinderInfo->end_direction));
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   167
	} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   168
		// No direction, so just get the fastest route to the station
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   169
		r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl);
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   170
		r2 = DistanceManhattan(current->tile, PathFinderInfo->end_tile_br);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   171
	}
826
fff56bbc3606 (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
   172
	// See if the bottomright is faster than the topleft..
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   173
	if (r2 < r) r = r2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   174
	return r * AI_PATHFINDER_H_MULTIPLER;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   175
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   176
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   177
// We found the end.. let's get the route back and put it in an array
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   178
static void AyStar_AiPathFinder_FoundEndNode(AyStar *aystar, OpenListNode *current)
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   179
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   180
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
959
e6a3bbda610f (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   181
	uint i = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   182
	PathNode *parent = &current->path;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   183
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   184
	do {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   185
		PathFinderInfo->route_extra[i] = parent->node.user_data[0];
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   186
		PathFinderInfo->route[i++] = parent->node.tile;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   187
		if (i > lengthof(PathFinderInfo->route)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   188
			// We ran out of space for the PathFinder
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   189
			DEBUG(ai, 0)("[AiPathFinder] Ran out of space in the route[] array!!!");
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   190
			PathFinderInfo->route_length = -1; // -1 indicates out of space
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   191
			return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   192
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   193
		parent = parent->parent;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   194
	} while (parent != NULL);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   195
	PathFinderInfo->route_length = i;
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   196
	DEBUG(ai, 1)("[Ai-PathFinding] Found route of %d nodes long in %d nodes of searching", i, Hash_Size(&aystar->ClosedListHash));
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   197
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   198
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   199
// What tiles are around us.
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   200
static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *current)
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   201
{
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   202
	uint i;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   203
	int ret;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   204
	int dir;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   205
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   206
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   207
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   208
	aystar->num_neighbours = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   209
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   210
	// Go through all surrounding tiles and check if they are within the limits
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   211
	for (i = 0; i < 4; i++) {
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   212
		TileIndex ctile = current->path.node.tile; // Current tile
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   213
		TileIndex atile = ctile + TileOffsByDir(i); // Adjacent tile
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   214
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   215
		if (TileX(atile) > 1 && TileX(atile) < MapMaxX() - 1 &&
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   216
				TileY(atile) > 1 && TileY(atile) < MapMaxY() - 1) {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   217
			// We also directly test if the current tile can connect to this tile..
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   218
			//  We do this simply by just building the tile!
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   219
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   220
			// If the next step is a bridge, we have to enter it the right way
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   221
			if (!PathFinderInfo->rail_or_road && IsRoad(atile)) {
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   222
				if (IsTileType(atile, MP_TUNNELBRIDGE)) {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   223
					// An existing bridge... let's test the direction ;)
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   224
					if ((_m[atile].m5 & 1U) != (i & 1)) continue;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   225
					// This problem only is valid for tunnels:
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   226
					// When the last tile was not yet a tunnel, check if we enter from the right side..
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   227
					if ((_m[atile].m5 & 0x80) == 0) {
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   228
						if (i != (_m[atile].m5 & 3U)) continue;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   229
					}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   230
				}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   231
			}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   232
			// But also if we are on a bridge, we can only move a certain direction
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   233
			if (!PathFinderInfo->rail_or_road && IsRoad(ctile)) {
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   234
				if (IsTileType(ctile, MP_TUNNELBRIDGE)) {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   235
					// An existing bridge/tunnel... let's test the direction ;)
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   236
					if ((_m[ctile].m5 & 1U) != (i & 1)) continue;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   237
				}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   238
			}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   239
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   240
			if ((AI_PATHFINDER_FLAG_BRIDGE & current->path.node.user_data[0]) != 0 ||
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   241
					(AI_PATHFINDER_FLAG_TUNNEL & current->path.node.user_data[0]) != 0) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   242
				// We are a bridge/tunnel, how cool!!
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   243
				//  This means we can only point forward.. get the direction from the user_data
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   244
				if (i != (current->path.node.user_data[0] >> 8)) continue;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   245
			}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   246
			dir = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   247
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   248
			// First, check if we have a parent
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   249
			if (current->path.parent == NULL && current->path.node.user_data[0] == 0) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   250
				// If not, this means we are at the starting station
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   251
				if (PathFinderInfo->start_direction != AI_PATHFINDER_NO_DIRECTION) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   252
					// We do need a direction?
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   253
					if (AiNew_GetDirection(ctile, atile) != PathFinderInfo->start_direction) {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   254
						// We are not pointing the right way, invalid tile
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   255
						continue;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   256
					}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   257
				}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   258
			} else if (current->path.node.user_data[0] == 0) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   259
				if (PathFinderInfo->rail_or_road) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   260
					// Rail check
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   261
					dir = AiNew_GetRailDirection(current->path.parent->node.tile, ctile, atile);
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   262
					ret = DoCommandByTile(ctile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   263
					if (CmdFailed(ret)) continue;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   264
#ifdef AI_PATHFINDER_NO_90DEGREES_TURN
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   265
					if (current->path.parent->parent != NULL) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   266
						// Check if we don't make a 90degree curve
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   267
						int dir1 = AiNew_GetRailDirection(current->path.parent->parent->node.tile, current->path.parent->node.tile, ctile);
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   268
						if (_illegal_curves[dir1] == dir || _illegal_curves[dir] == dir1) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   269
							continue;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   270
						}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   271
					}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   272
#endif
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   273
				} else {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   274
					// Road check
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   275
					dir = AiNew_GetRoadDirection(current->path.parent->node.tile, ctile, atile);
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   276
					if (IsRoad(ctile)) {
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   277
						if (IsTileType(ctile, MP_TUNNELBRIDGE)) {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   278
							// We have a bridge, how nicely! We should mark it...
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   279
							dir = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   280
						} else {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   281
							// It already has road.. check if we miss any bits!
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   282
							if ((_m[ctile].m5 & dir) != dir) {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   283
								// We do miss some pieces :(
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2008
diff changeset
   284
								dir &= ~_m[ctile].m5;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   285
							} else {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   286
								dir = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   287
							}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   288
						}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   289
					}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   290
					// Only destruct things if it is MP_CLEAR of MP_TREES
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   291
					if (dir != 0) {
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   292
						ret = DoCommandByTile(ctile, dir, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   293
						if (CmdFailed(ret)) continue;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   294
					}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   295
				}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   296
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   297
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   298
			// The tile can be connected
1716
91b3f8762cd9 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   299
			aystar->neighbours[aystar->num_neighbours].tile = atile;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   300
			aystar->neighbours[aystar->num_neighbours].user_data[0] = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   301
			aystar->neighbours[aystar->num_neighbours++].direction = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   302
		}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   303
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   304
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   305
	// Next step, check for bridges and tunnels
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   306
	if (current->path.parent != NULL && current->path.node.user_data[0] == 0) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   307
		TileInfo ti;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   308
		// First we get the dir from this tile and his parent
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   309
		int dir = AiNew_GetDirection(current->path.parent->node.tile, current->path.node.tile);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   310
		// It means we can only walk with the track, so the bridge has to be in the same direction
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   311
		TileIndex tile = current->path.node.tile;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   312
		TileIndex new_tile = tile;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   313
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   314
		FindLandscapeHeightByTile(&ti, tile);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   315
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   316
		// Bridges can only be build on land that is not flat
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   317
		//  And if there is a road or rail blocking
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   318
		if (ti.tileh != 0 ||
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   319
				(PathFinderInfo->rail_or_road && IsTileType(tile + TileOffsByDir(dir), MP_STREET)) ||
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   320
				(!PathFinderInfo->rail_or_road && IsTileType(tile + TileOffsByDir(dir), MP_RAILWAY))) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   321
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   322
			for (;;) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   323
				new_tile += TileOffsByDir(dir);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   324
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   325
				// Precheck, is the length allowed?
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   326
				if (!CheckBridge_Stuff(0, GetBridgeLength(tile, new_tile))) break;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   327
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   328
				// Check if we hit the station-tile.. we don't like that!
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   329
				if (TILES_BETWEEN(new_tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) break;
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   330
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   331
				// Try building the bridge..
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   332
				ret = DoCommandByTile(tile, new_tile, (0 << 8) + (MAX_BRIDGES / 2), DC_AUTO, CMD_BUILD_BRIDGE);
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   333
				if (CmdFailed(ret)) continue;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   334
				// We can build a bridge here.. add him to the neighbours
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   335
				aystar->neighbours[aystar->num_neighbours].tile = new_tile;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   336
				aystar->neighbours[aystar->num_neighbours].user_data[0] = AI_PATHFINDER_FLAG_BRIDGE + (dir << 8);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   337
				aystar->neighbours[aystar->num_neighbours++].direction = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   338
				// We can only have 12 neighbours, and we need 1 left for tunnels
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   339
				if (aystar->num_neighbours == 11) break;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   340
			}
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   341
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   342
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   343
		// Next, check for tunnels!
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   344
		// Tunnels can only be build with tileh of 3, 6, 9 or 12, depending on the direction
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   345
		//  For now, we check both sides for this tile.. terraforming gives fuzzy result
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   346
		if ((dir == 0 && ti.tileh == 12) ||
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   347
				(dir == 1 && ti.tileh == 6) ||
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   348
				(dir == 2 && ti.tileh == 3) ||
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   349
				(dir == 3 && ti.tileh == 9)) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   350
			// Now simply check if a tunnel can be build
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   351
			ret = DoCommandByTile(tile, (PathFinderInfo->rail_or_road?0:0x200), 0, DC_AUTO, CMD_BUILD_TUNNEL);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   352
			FindLandscapeHeightByTile(&ti, _build_tunnel_endtile);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   353
			if (!CmdFailed(ret) && (ti.tileh == 3 || ti.tileh == 6 || ti.tileh == 9 || ti.tileh == 12)) {
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   354
				aystar->neighbours[aystar->num_neighbours].tile = _build_tunnel_endtile;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   355
				aystar->neighbours[aystar->num_neighbours].user_data[0] = AI_PATHFINDER_FLAG_TUNNEL + (dir << 8);
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   356
				aystar->neighbours[aystar->num_neighbours++].direction = 0;
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   357
			}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   358
		}
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   359
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   360
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   361
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   362
extern uint GetRailFoundation(uint tileh, uint bits);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   363
extern uint GetRoadFoundation(uint tileh, uint bits);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   364
extern uint GetBridgeFoundation(uint tileh, byte direction);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   365
enum {
1714
bbf79c691e6c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   366
	BRIDGE_NO_FOUNDATION = 1 << 0 | 1 << 3 | 1 << 6 | 1 << 9 | 1 << 12,
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   367
};
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   368
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   369
// The most important function: it calculates the g-value
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   370
static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   371
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   372
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   373
	int r, res = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   374
	TileInfo ti, parent_ti;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   375
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   376
	// Gather some information about the tile..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   377
	FindLandscapeHeightByTile(&ti, current->tile);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   378
	FindLandscapeHeightByTile(&parent_ti, parent->path.node.tile);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   379
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   380
	// Check if we hit the end-tile
1729
ef865a2a6df0 (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   381
	if (TILES_BETWEEN(current->tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   382
		// We are at the end-tile, check if we had a direction or something...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   383
		if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION && AiNew_GetDirection(current->tile, parent->path.node.tile) != PathFinderInfo->end_direction)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   384
			// We are not pointing the right way, invalid tile
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   385
			return AYSTAR_INVALID_NODE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   386
		// If it was valid, drop out.. we don't build on the endtile
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   387
		return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   388
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   389
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   390
	// Give everything a small penalty
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   391
	res += AI_PATHFINDER_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   392
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   393
	if (!PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   394
		// Road has the lovely advantage it can use other road... check if
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   395
		//  the current tile is road, and if so, give a good bonus
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
   396
		if (IsRoad(current->tile)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   397
			res -= AI_PATHFINDER_ROAD_ALREADY_EXISTS_BONUS;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   398
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   399
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   400
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   401
	// We should give a penalty when the tile is going up or down.. this is one way to do so!
1494
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   402
	//  Too bad we have to count it from the parent.. but that is not so bad.
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   403
	// We also dislike long routes on slopes, since they do not look too realistic
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   404
	//  when there is a flat land all around, they are more expensive to build, and
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   405
	//  especially they essentially block the ability to connect or cross the road
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   406
	//  from one side.
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   407
	if (parent_ti.tileh != 0 && parent->path.parent != NULL) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   408
		// Skip if the tile was from a bridge or tunnel
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   409
		if (parent->path.node.user_data[0] == 0 && current->user_data[0] == 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   410
			if (PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   411
				r = GetRailFoundation(parent_ti.tileh, 1 << AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   412
				// Maybe is BRIDGE_NO_FOUNDATION a bit strange here, but it contains just the right information..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   413
				if (r >= 15 || (r == 0 && (BRIDGE_NO_FOUNDATION & (1 << ti.tileh)))) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   414
					res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
1494
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   415
				} else {
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   416
					res += AI_PATHFINDER_FOUNDATION_PENALTY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   417
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   418
			} else {
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
   419
				if (!(IsRoad(parent->path.node.tile) && IsTileType(parent->path.node.tile, MP_TUNNELBRIDGE))) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   420
					r = GetRoadFoundation(parent_ti.tileh, AiNew_GetRoadDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   421
					if (r >= 15 || r == 0)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   422
						res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
1494
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   423
					else
31436e59176a (svn r1998) Give penalty 100 to the AI for using foundations (buildonslopes). This prevents it from building long road lines on foundations unless really necessary.
pasky
parents: 1459
diff changeset
   424
						res += AI_PATHFINDER_FOUNDATION_PENALTY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   425
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   426
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   427
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   428
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   429
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   430
	// Are we part of a tunnel?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   431
	if ((AI_PATHFINDER_FLAG_TUNNEL & current->user_data[0]) != 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   432
		// Tunnels are very expensive when build on long routes..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   433
		// Ironicly, we are using BridgeCode here ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   434
		r = AI_PATHFINDER_TUNNEL_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   435
		res += r + (r >> 8);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   436
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   437
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   438
	// Are we part of a bridge?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   439
	if ((AI_PATHFINDER_FLAG_BRIDGE & current->user_data[0]) != 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   440
		// That means for every length a penalty
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   441
		res += AI_PATHFINDER_BRIDGE_PENALTY * GetBridgeLength(current->tile, parent->path.node.tile);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   442
		// Check if we are going up or down, first for the starting point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   443
		// In user_data[0] is at the 8th bit the direction
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   444
		if (!(BRIDGE_NO_FOUNDATION & (1 << parent_ti.tileh))) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   445
			if (GetBridgeFoundation(parent_ti.tileh, (current->user_data[0] >> 8) & 1) < 15)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   446
				res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   447
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   448
		// Second for the end point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   449
		if (!(BRIDGE_NO_FOUNDATION & (1 << ti.tileh))) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   450
			if (GetBridgeFoundation(ti.tileh, (current->user_data[0] >> 8) & 1) < 15)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   451
				res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   452
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   453
		if (parent_ti.tileh == 0)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   454
			res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   455
		if (ti.tileh == 0)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   456
			res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   457
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   458
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   459
	//  To prevent the AI from taking the fastest way in tiles, but not the fastest way
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   460
	//    in speed, we have to give a good penalty to direction changing
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   461
	//  This way, we get almost the fastest way in tiles, and a very good speed on the track
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   462
	if (!PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   463
		if (parent->path.parent != NULL &&
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   464
			AiNew_GetDirection(current->tile, parent->path.node.tile) != AiNew_GetDirection(parent->path.node.tile, parent->path.parent->node.tile)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   465
			// When road exists, we don't like turning, but its free, so don't be to piggy about it
1047
df93a1386892 (svn r1548) Move AI_PATHFINDER_IS_ROAD from ai.h to ai_pathfinder.c to avoid global namespace pollution, turn it into a function called IsRoad and improve the commments a bit
tron
parents: 1035
diff changeset
   466
			if (IsRoad(parent->path.node.tile))
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   467
				res += AI_PATHFINDER_DIRECTION_CHANGE_ON_EXISTING_ROAD_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   468
			else
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   469
				res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   470
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   471
	} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   472
		// For rail we have 1 exeption: diagonal rail..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   473
		// So we fetch 2 raildirection. That of the current one, and of the one before that
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   474
		if (parent->path.parent != NULL && parent->path.parent->parent != NULL) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   475
			int dir1 = AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   476
			int dir2 = AiNew_GetRailDirection(parent->path.parent->parent->node.tile, parent->path.parent->node.tile, parent->path.node.tile);
826
fff56bbc3606 (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
   477
			// First, see if we are on diagonal path, that is better than straight path
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   478
			if (dir1 > 1) { res -= AI_PATHFINDER_DIAGONAL_BONUS; }
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   479
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   480
			// First see if they are different
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   481
			if (dir1 != dir2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   482
				// dir 2 and 3 are 1 diagonal track, and 4 and 5.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   483
				if (!(((dir1 == 2 || dir1 == 3) && (dir2 == 2 || dir2 == 3)) || ((dir1 == 4 || dir1 == 5) && (dir2 == 4 || dir2 == 5)))) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   484
					// It is not, so we changed of direction
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   485
					res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   486
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   487
				if (parent->path.parent->parent->parent != NULL) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   488
					int dir3 = AiNew_GetRailDirection(parent->path.parent->parent->parent->node.tile, parent->path.parent->parent->node.tile, parent->path.parent->node.tile);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   489
					// Check if we changed 3 tiles of direction in 3 tiles.. bad!!!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   490
					if ((dir1 == 0 || dir1 == 1) && dir2 > 1 && (dir3 == 0 || dir3 == 1)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   491
						res += AI_PATHFINDER_CURVE_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   492
					}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   493
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   494
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   495
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   496
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   497
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   498
	// Res should never be below zero.. if so, make it zero!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   499
	if (res < 0) { res = 0; }
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   500
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   501
	// Return our value
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   502
	return res;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   503
}