src/ai/trolly/pathfinder.cpp
author rubidium
Tue, 22 Jan 2008 21:00:30 +0000
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6871 5a9dc001e1ad
child 6877 889301acc299
permissions -rw-r--r--
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
2381
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     3
#include "../../stdafx.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     4
#include "../../openttd.h"
3234
986c30171e92 (svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents: 3184
diff changeset
     5
#include "../../bridge_map.h"
2381
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     6
#include "../../debug.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     7
#include "../../command_func.h"
2381
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     8
#include "trolly.h"
de9053fe2a2c (svn r2907) -Codechange: splitted the AIs to their own directory. AINew becomes 'trolly', AIOld becomes 'default', both in their own dir in the 'ai' dir. More AIs to come.
truelight
parents: 2366
diff changeset
     9
#include "../../depot.h"
3179
774b1e6553a7 (svn r3816) Use existing accessors
tron
parents: 3157
diff changeset
    10
#include "../../tunnel_map.h"
3359
d4316b1af327 (svn r4154) -Moved MAX_BRIDGES in bridge.h and made it an enum. This makes two drops ...
celestar
parents: 3234
diff changeset
    11
#include "../../bridge.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    12
#include "../../tunnelbridge_map.h"
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2549
diff changeset
    13
#include "../ai.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    14
#include "../../variables.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    15
#include "../../player_base.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    16
#include "../../player_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    17
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    18
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    19
#define TEST_STATION_NO_DIR 0xFF
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    20
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    21
// 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
    22
// TODO: make it train compatible
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
    23
static bool TestCanBuildStationHere(TileIndex tile, byte dir)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    24
{
1962
51ee4f459268 (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1891
diff changeset
    25
	Player *p = GetPlayer(_current_player);
1713
d970350410b2 (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
d970350410b2 (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
	if (dir == TEST_STATION_NO_DIR) {
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
    28
		CommandCost ret;
1713
d970350410b2 (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
		// TODO: currently we only allow spots that can be access from al 4 directions...
d970350410b2 (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
		//  should be fixed!!!
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    31
		for (dir = 0; dir < 4; dir++) {
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    32
			ret = AiNew_Build_Station(p, _players_ainew[p->index].tbt, tile, 1, 1, dir, DC_QUERY_COST);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
    33
			if (CmdSucceeded(ret)) return true;
1713
d970350410b2 (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
    34
		}
d970350410b2 (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
    35
		return false;
d970350410b2 (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
    36
	}
d970350410b2 (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
    37
d970350410b2 (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
    38
	// return true if command succeeded, so the inverse of CmdFailed()
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    39
	return CmdSucceeded(AiNew_Build_Station(p, _players_ainew[p->index].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
    40
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    41
1047
d4c04332d837 (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
    42
d4c04332d837 (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
static bool IsRoad(TileIndex tile)
d4c04332d837 (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
    44
{
d4c04332d837 (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
	return
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
    46
		// MP_ROAD, but not a road depot?
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
    47
		(IsTileType(tile, MP_ROAD) && !IsTileDepotType(tile, TRANSPORT_ROAD)) ||
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    48
		(IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD);
1047
d4c04332d837 (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
    49
}
d4c04332d837 (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
    50
d4c04332d837 (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
    51
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    52
// Checks if a tile 'a' is between the tiles 'b' and 'c'
926
bd4312619522 (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
    53
#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
    54
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    55
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    56
// Check if the current tile is in our end-area
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    57
static int32 AyStar_AiPathFinder_EndNodeCheck(AyStar *aystar, OpenListNode *current)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    58
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    59
	const Ai_PathFinderInfo* PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
    60
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    61
	// It is not allowed to have a station on the end of a bridge or tunnel ;)
1617
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    62
	if (current->path.node.user_data[0] != 0) return AYSTAR_DONE;
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    63
	if (TILES_BETWEEN(current->path.node.tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br))
55878ca5ada9 (svn r2121) -Fix: changed the 2nd param of AyStar_EndNodeCheck back to what it should be
truelight
parents: 1494
diff changeset
    64
		if (IsTileType(current->path.node.tile, MP_CLEAR) || IsTileType(current->path.node.tile, MP_TREES))
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    65
			if (current->path.parent == NULL || TestCanBuildStationHere(current->path.node.tile, AiNew_GetDirection(current->path.parent->node.tile, current->path.node.tile)))
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    66
				return AYSTAR_FOUND_END_NODE;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
    67
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    68
	return AYSTAR_DONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    69
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    70
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    71
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    72
// Calculates the hash
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    73
//   Currently it is a 10 bit hash, so the hash array has a max depth of 6 bits (so 64)
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    74
static uint AiPathFinder_Hash(uint key1, uint key2)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    75
{
926
bd4312619522 (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
    76
	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
    77
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    78
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    79
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    80
// Clear the memory of all the things
1095
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    81
static void AyStar_AiPathFinder_Free(AyStar *aystar)
90220990fd7c (svn r1596) Add some more statics
tron
parents: 1047
diff changeset
    82
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    83
	AyStarMain_Free(aystar);
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    84
	delete aystar;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    85
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    86
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    87
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    88
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
    89
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
    90
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
    91
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
    92
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
    93
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    94
// This creates the AiPathFinder
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    95
AyStar *new_AyStar_AiPathFinder(int max_tiles_around, Ai_PathFinderInfo *PathFinderInfo)
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    96
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
    97
	PathNode start_node;
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    98
	uint x;
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
    99
	uint y;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   100
	// Create AyStar
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   101
	AyStar *result = new AyStar();
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   102
	init_AyStar(result, AiPathFinder_Hash, 1 << 10);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   103
	// Set the function pointers
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   104
	result->CalculateG = AyStar_AiPathFinder_CalculateG;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   105
	result->CalculateH = AyStar_AiPathFinder_CalculateH;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   106
	result->EndNodeCheck = AyStar_AiPathFinder_EndNodeCheck;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   107
	result->FoundEndNode = AyStar_AiPathFinder_FoundEndNode;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   108
	result->GetNeighbours = AyStar_AiPathFinder_GetNeighbours;
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
	result->free = AyStar_AiPathFinder_Free;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   111
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   112
	// Set some information
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   113
	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
   114
	result->max_path_cost = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   115
	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
   116
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   117
	// Set the user_data to the PathFinderInfo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   118
	result->user_target = PathFinderInfo;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   119
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   120
	// Set the start node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   121
	start_node.parent = NULL;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   122
	start_node.node.direction = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   123
	start_node.node.user_data[0] = 0;
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
	// Now we add all the starting tiles
926
bd4312619522 (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
   126
	for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) {
bd4312619522 (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
   127
		for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) {
1981
de031d2aed47 (svn r2487) Replace TILE_XY by TileXY/TileDiffXY
tron
parents: 1977
diff changeset
   128
			start_node.node.tile = TileXY(x, y);
1777
d328484bd6f2 (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
   129
			result->addstart(result, &start_node.node, 0);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   130
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   131
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   132
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   133
	return result;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   134
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   135
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   136
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   137
// To reuse AyStar we sometimes have to clean all the memory
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   138
void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo)
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   139
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   140
	PathNode start_node;
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   141
	uint x;
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   142
	uint y;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   143
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   144
	aystar->clear(aystar);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   145
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   146
	// Set the user_data to the PathFinderInfo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   147
	aystar->user_target = PathFinderInfo;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   148
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   149
	// Set the start node
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   150
	start_node.parent = NULL;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   151
	start_node.node.direction = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   152
	start_node.node.user_data[0] = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   153
	start_node.node.tile = PathFinderInfo->start_tile_tl;
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
	// Now we add all the starting tiles
926
bd4312619522 (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
   156
	for (x = TileX(PathFinderInfo->start_tile_tl); x <= TileX(PathFinderInfo->start_tile_br); x++) {
bd4312619522 (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
   157
		for (y = TileY(PathFinderInfo->start_tile_tl); y <= TileY(PathFinderInfo->start_tile_br); y++) {
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   158
			TileIndex tile = TileXY(x, y);
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   159
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   160
			if (!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) continue;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   161
			if (!TestCanBuildStationHere(tile, TEST_STATION_NO_DIR)) continue;
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   162
			start_node.node.tile = tile;
1777
d328484bd6f2 (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
   163
			aystar->addstart(aystar, &start_node.node, 0);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   164
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   165
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   166
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   167
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   168
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   169
// The h-value, simple calculation
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   170
static int32 AyStar_AiPathFinder_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   171
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   172
	const Ai_PathFinderInfo* PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   173
	int r, r2;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   174
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   175
	if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   176
		// The station is pointing to a direction, add a tile towards that direction, so the H-value is more accurate
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   177
		r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl + TileOffsByDiagDir(PathFinderInfo->end_direction));
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   178
		r2 = DistanceManhattan(current->tile, PathFinderInfo->end_tile_br + TileOffsByDiagDir(PathFinderInfo->end_direction));
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   179
	} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   180
		// No direction, so just get the fastest route to the station
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   181
		r = DistanceManhattan(current->tile, PathFinderInfo->end_tile_tl);
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   182
		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
   183
	}
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
   184
	// 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
   185
	if (r2 < r) r = r2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   186
	return r * AI_PATHFINDER_H_MULTIPLER;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   187
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   188
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   189
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   190
// We found the end.. let's get the route back and put it in an array
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   191
static void AyStar_AiPathFinder_FoundEndNode(AyStar *aystar, OpenListNode *current)
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   192
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   193
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
959
b031d88c76f3 (svn r1451) Fix some of the signed/unsigned comparison warnings
tron
parents: 926
diff changeset
   194
	uint i = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   195
	PathNode *parent = &current->path;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   196
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   197
	do {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   198
		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
   199
		PathFinderInfo->route[i++] = parent->node.tile;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   200
		if (i > lengthof(PathFinderInfo->route)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   201
			// We ran out of space for the PathFinder
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4559
diff changeset
   202
			DEBUG(ai, 0, "No more space in pathfinder route[] array");
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   203
			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
   204
			return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   205
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   206
		parent = parent->parent;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   207
	} while (parent != NULL);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   208
	PathFinderInfo->route_length = i;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 4559
diff changeset
   209
	DEBUG(ai, 1, "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
   210
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   211
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   212
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   213
// What tiles are around us.
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   214
static void AyStar_AiPathFinder_GetNeighbours(AyStar *aystar, OpenListNode *current)
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   215
{
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   216
	CommandCost ret;
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   217
	int dir;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   218
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   219
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   220
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   221
	aystar->num_neighbours = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   222
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   223
	// Go through all surrounding tiles and check if they are within the limits
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   224
	for (DiagDirection i = DIAGDIR_BEGIN; i < DIAGDIR_END; i++) {
1716
943f28e3f2a0 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   225
		TileIndex ctile = current->path.node.tile; // Current tile
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   226
		TileIndex atile = ctile + TileOffsByDiagDir(i); // Adjacent tile
1716
943f28e3f2a0 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   227
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   228
		if (TileX(atile) > 1 && TileX(atile) < MapMaxX() - 1 &&
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   229
				TileY(atile) > 1 && TileY(atile) < MapMaxY() - 1) {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   230
			// We also directly test if the current tile can connect to this tile..
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   231
			//  We do this simply by just building the tile!
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   232
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   233
			// If the next step is a bridge, we have to enter it the right way
1716
943f28e3f2a0 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   234
			if (!PathFinderInfo->rail_or_road && IsRoad(atile)) {
943f28e3f2a0 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   235
				if (IsTileType(atile, MP_TUNNELBRIDGE)) {
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   236
					if (GetTunnelBridgeDirection(atile) != i) continue;
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   237
				}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   238
			}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   239
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   240
			if ((AI_PATHFINDER_FLAG_BRIDGE & current->path.node.user_data[0]) != 0 ||
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   241
					(AI_PATHFINDER_FLAG_TUNNEL & current->path.node.user_data[0]) != 0) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   242
				// We are a bridge/tunnel, how cool!!
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   243
				//  This means we can only point forward.. get the direction from the user_data
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   244
				if ((uint)i != (current->path.node.user_data[0] >> 8)) continue;
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   245
			}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   246
			dir = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   247
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   248
			// First, check if we have a parent
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   249
			if (current->path.parent == NULL && current->path.node.user_data[0] == 0) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   250
				// If not, this means we are at the starting station
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   251
				if (PathFinderInfo->start_direction != AI_PATHFINDER_NO_DIRECTION) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   252
					// We do need a direction?
1716
943f28e3f2a0 (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
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   254
						// We are not pointing the right way, invalid tile
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   255
						continue;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   256
					}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   257
				}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   258
			} else if (current->path.node.user_data[0] == 0) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   259
				if (PathFinderInfo->rail_or_road) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   260
					// Rail check
1716
943f28e3f2a0 (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);
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2549
diff changeset
   262
					ret = AI_DoCommand(ctile, 0, dir, DC_AUTO | DC_NO_WATER, CMD_BUILD_SINGLE_RAIL);
1714
2b8e37f8f18c (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
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   265
					if (current->path.parent->parent != NULL) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   266
						// Check if we don't make a 90degree curve
1716
943f28e3f2a0 (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
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   268
						if (_illegal_curves[dir1] == dir || _illegal_curves[dir] == dir1) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   269
							continue;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   270
						}
2b8e37f8f18c (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
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   273
				} else {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   274
					// Road check
1716
943f28e3f2a0 (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);
943f28e3f2a0 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   276
					if (IsRoad(ctile)) {
943f28e3f2a0 (svn r2220) Put two TileIndices into temporary variables to improve readability
tron
parents: 1714
diff changeset
   277
						if (IsTileType(ctile, MP_TUNNELBRIDGE)) {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   278
							// We have a bridge, how nicely! We should mark it...
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   279
							dir = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   280
						} else {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   281
							// It already has road.. check if we miss any bits!
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6574
diff changeset
   282
							if ((GetAnyRoadBits(ctile, ROADTYPE_ROAD) & dir) != dir) {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   283
								// We do miss some pieces :(
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6574
diff changeset
   284
								dir &= ~GetAnyRoadBits(ctile, ROADTYPE_ROAD);
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   285
							} else {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   286
								dir = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   287
							}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   288
						}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   289
					}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   290
					// Only destruct things if it is MP_CLEAR of MP_TREES
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   291
					if (dir != 0) {
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2549
diff changeset
   292
						ret = AI_DoCommand(ctile, dir, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   293
						if (CmdFailed(ret)) continue;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   294
					}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   295
				}
2b8e37f8f18c (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
943f28e3f2a0 (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
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   300
			aystar->neighbours[aystar->num_neighbours].user_data[0] = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   301
			aystar->neighbours[aystar->num_neighbours++].direction = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   302
		}
2b8e37f8f18c (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
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   305
	// Next step, check for bridges and tunnels
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   306
	if (current->path.parent != NULL && current->path.node.user_data[0] == 0) {
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   307
		// First we get the dir from this tile and his parent
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   308
		DiagDirection dir = AiNew_GetDirection(current->path.parent->node.tile, current->path.node.tile);
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   309
		// It means we can only walk with the track, so the bridge has to be in the same direction
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   310
		TileIndex tile = current->path.node.tile;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   311
		TileIndex new_tile = tile;
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   312
		Slope tileh = GetTileSlope(tile, NULL);
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   313
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   314
		// Bridges can only be build on land that is not flat
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   315
		//  And if there is a road or rail blocking
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   316
		if (tileh != SLOPE_FLAT ||
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   317
				(PathFinderInfo->rail_or_road && IsTileType(tile + TileOffsByDiagDir(dir), MP_ROAD)) ||
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   318
				(!PathFinderInfo->rail_or_road && IsTileType(tile + TileOffsByDiagDir(dir), MP_RAILWAY))) {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   319
			for (;;) {
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   320
				new_tile += TileOffsByDiagDir(dir);
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   321
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   322
				// Precheck, is the length allowed?
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   323
				if (!CheckBridge_Stuff(0, GetBridgeLength(tile, new_tile))) break;
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   324
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   325
				// Check if we hit the station-tile.. we don't like that!
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   326
				if (TILES_BETWEEN(new_tile, PathFinderInfo->end_tile_tl, PathFinderInfo->end_tile_br)) break;
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   327
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   328
				// Try building the bridge..
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2549
diff changeset
   329
				ret = AI_DoCommand(tile, new_tile, (0 << 8) + (MAX_BRIDGES / 2), DC_AUTO, CMD_BUILD_BRIDGE);
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   330
				if (CmdFailed(ret)) continue;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   331
				// We can build a bridge here.. add him to the neighbours
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   332
				aystar->neighbours[aystar->num_neighbours].tile = new_tile;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   333
				aystar->neighbours[aystar->num_neighbours].user_data[0] = AI_PATHFINDER_FLAG_BRIDGE + (dir << 8);
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   334
				aystar->neighbours[aystar->num_neighbours++].direction = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   335
				// 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
   336
				if (aystar->num_neighbours == 11) break;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   337
			}
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   338
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   339
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   340
		// Next, check for tunnels!
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   341
		// Tunnels can only be built on slopes corresponding to the direction
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   342
		//  For now, we check both sides for this tile.. terraforming gives fuzzy result
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   343
		if ((dir == DIAGDIR_NE && tileh == SLOPE_NE) ||
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   344
				(dir == DIAGDIR_SE && tileh == SLOPE_SE) ||
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   345
				(dir == DIAGDIR_SW && tileh == SLOPE_SW) ||
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   346
				(dir == DIAGDIR_NW && tileh == SLOPE_NW)) {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   347
			// Now simply check if a tunnel can be build
2682
94ca0b4dc53f (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2549
diff changeset
   348
			ret = AI_DoCommand(tile, (PathFinderInfo->rail_or_road?0:0x200), 0, DC_AUTO, CMD_BUILD_TUNNEL);
3055
25fd76668365 (svn r3644) Don't use FindLandscapeHeightByTile() when it's overkill - often it was just a complicated way of writing GetTileSlope(tile, NULL)
tron
parents: 3033
diff changeset
   349
			tileh = GetTileSlope(_build_tunnel_endtile, NULL);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   350
			if (CmdSucceeded(ret) && (tileh == SLOPE_SW || tileh == SLOPE_SE || tileh == SLOPE_NW || tileh == SLOPE_NE)) {
1714
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   351
				aystar->neighbours[aystar->num_neighbours].tile = _build_tunnel_endtile;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   352
				aystar->neighbours[aystar->num_neighbours].user_data[0] = AI_PATHFINDER_FLAG_TUNNEL + (dir << 8);
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   353
				aystar->neighbours[aystar->num_neighbours++].direction = 0;
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   354
			}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   355
		}
2b8e37f8f18c (svn r2218) Indentation
tron
parents: 1713
diff changeset
   356
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   357
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   358
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   359
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   360
extern Foundation GetRailFoundation(Slope tileh, TrackBits bits); // XXX function declaration in .c
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   361
extern Foundation GetRoadFoundation(Slope tileh, RoadBits bits); // XXX function declaration in .c
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   362
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   363
// The most important function: it calculates the g-value
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   364
static int32 AyStar_AiPathFinder_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent)
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   365
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   366
	Ai_PathFinderInfo *PathFinderInfo = (Ai_PathFinderInfo*)aystar->user_target;
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   367
	int res = 0;
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   368
	Slope tileh = GetTileSlope(current->tile, NULL);
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   369
	Slope parent_tileh = GetTileSlope(parent->path.node.tile, NULL);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   370
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   371
	// Check if we hit the end-tile
1729
d27130c7d9ef (svn r2233) Bracing, whitespace, indendation
tron
parents: 1716
diff changeset
   372
	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
   373
		// We are at the end-tile, check if we had a direction or something...
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   374
		if (PathFinderInfo->end_direction != AI_PATHFINDER_NO_DIRECTION && AiNew_GetDirection(current->tile, parent->path.node.tile) != PathFinderInfo->end_direction) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   375
			// 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
   376
			return AYSTAR_INVALID_NODE;
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   377
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   378
		// 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
   379
		return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   380
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   381
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   382
	// Give everything a small penalty
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   383
	res += AI_PATHFINDER_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   384
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   385
	if (!PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   386
		// 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
   387
		//  the current tile is road, and if so, give a good bonus
1047
d4c04332d837 (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
   388
		if (IsRoad(current->tile)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   389
			res -= AI_PATHFINDER_ROAD_ALREADY_EXISTS_BONUS;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   390
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   391
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   392
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   393
	// We should give a penalty when the tile is going up or down.. this is one way to do so!
1494
36882c64a497 (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
   394
	//  Too bad we have to count it from the parent.. but that is not so bad.
36882c64a497 (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
   395
	// We also dislike long routes on slopes, since they do not look too realistic
36882c64a497 (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
   396
	//  when there is a flat land all around, they are more expensive to build, and
36882c64a497 (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
   397
	//  especially they essentially block the ability to connect or cross the road
36882c64a497 (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
   398
	//  from one side.
3644
45a307767dc1 (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3484
diff changeset
   399
	if (parent_tileh != SLOPE_FLAT && parent->path.parent != NULL) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   400
		// 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
   401
		if (parent->path.node.user_data[0] == 0 && current->user_data[0] == 0) {
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   402
			static const uint32 SLOPED_TILEHS = (1 << SLOPE_NW) | (1 << SLOPE_SW) |  (1 << SLOPE_SE) | (1 << SLOPE_NE);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   403
			if (PathFinderInfo->rail_or_road) {
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   404
				Foundation f = GetRailFoundation(parent_tileh, (TrackBits)(1 << AiNew_GetRailDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile)));
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   405
				if (IsInclinedFoundation(f) || (!IsFoundation(f) && HasBit(SLOPED_TILEHS, parent_tileh))) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   406
					res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
1494
36882c64a497 (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
   407
				} else {
36882c64a497 (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
   408
					res += AI_PATHFINDER_FOUNDATION_PENALTY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   409
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   410
			} else {
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   411
				if (!IsRoad(parent->path.node.tile) || !IsTileType(parent->path.node.tile, MP_TUNNELBRIDGE)) {
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   412
					Foundation f = GetRoadFoundation(parent_tileh, (RoadBits)AiNew_GetRoadDirection(parent->path.parent->node.tile, parent->path.node.tile, current->tile));
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   413
					if (IsInclinedFoundation(f) || (!IsFoundation(f) && HasBit(SLOPED_TILEHS, parent_tileh))) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   414
						res += AI_PATHFINDER_TILE_GOES_UP_PENALTY;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   415
					} else {
1494
36882c64a497 (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;
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   417
					}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   418
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   419
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   420
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   421
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   422
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   423
	// Are we part of a tunnel?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   424
	if ((AI_PATHFINDER_FLAG_TUNNEL & current->user_data[0]) != 0) {
6743
cabfaa4a0295 (svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents: 6720
diff changeset
   425
		int r;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   426
		// 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
   427
		// Ironicly, we are using BridgeCode here ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   428
		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
   429
		res += r + (r >> 8);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   430
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   431
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   432
	// Are we part of a bridge?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   433
	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
   434
		// That means for every length a penalty
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   435
		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
   436
		// 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
   437
		// In user_data[0] is at the 8th bit the direction
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   438
		if (!HasBridgeFlatRamp(parent_tileh, (Axis)((current->user_data[0] >> 8) & 1))) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   439
		// Second for the end point
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   440
		if (!HasBridgeFlatRamp(tileh, (Axis)((current->user_data[0] >> 8) & 1))) res += AI_PATHFINDER_BRIDGE_GOES_UP_PENALTY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   441
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   442
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   443
	//  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
   444
	//    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
   445
	//  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
   446
	if (!PathFinderInfo->rail_or_road) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   447
		if (parent->path.parent != NULL &&
2366
a06b633fc6fd (svn r2892) Fix indentation
tron
parents: 2186
diff changeset
   448
				AiNew_GetDirection(current->tile, parent->path.node.tile) != AiNew_GetDirection(parent->path.node.tile, parent->path.parent->node.tile)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   449
			// When road exists, we don't like turning, but its free, so don't be to piggy about it
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   450
			if (IsRoad(parent->path.node.tile)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   451
				res += AI_PATHFINDER_DIRECTION_CHANGE_ON_EXISTING_ROAD_PENALTY;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   452
			} else {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   453
				res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY;
4000
bab1ebc37da0 (svn r5210) Many small changes which piled up: const, unsigned, variable scope, CSE for readability, DeMorgan, if cascades -> switch, whitespace, parentheses, bracing, misc.
tron
parents: 3644
diff changeset
   454
			}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   455
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   456
	} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   457
		// For rail we have 1 exeption: diagonal rail..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   458
		// 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
   459
		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
   460
			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
   461
			int dir2 = AiNew_GetRailDirection(parent->path.parent->parent->node.tile, parent->path.parent->node.tile, parent->path.node.tile);
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
   462
			// First, see if we are on diagonal path, that is better than straight path
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 2916
diff changeset
   463
			if (dir1 > 1) res -= AI_PATHFINDER_DIAGONAL_BONUS;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   464
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   465
			// First see if they are different
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   466
			if (dir1 != dir2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   467
				// 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
   468
				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
   469
					// It is not, so we changed of direction
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   470
					res += AI_PATHFINDER_DIRECTION_CHANGE_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   471
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   472
				if (parent->path.parent->parent->parent != NULL) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   473
					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
   474
					// 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
   475
					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
   476
						res += AI_PATHFINDER_CURVE_PENALTY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   477
					}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   478
				}
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
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   481
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 145
diff changeset
   482
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
   483
	return (res < 0) ? 0 : res;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 84
diff changeset
   484
}