src/ai/trolly/build.cpp
author rubidium
Wed, 19 Dec 2007 23:26:02 +0000
changeset 8108 b42a0e5c67ef
parent 7928 63e18de69e50
child 8116 8da76dcb3287
permissions -rw-r--r--
(svn r11669) -Codechange: refactor tile.h -> tile_type.h and tile_map.h
2381
9a5ed6c0a2e6 (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:
diff changeset
     1
/* $Id$ */
9a5ed6c0a2e6 (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:
diff changeset
     2
9a5ed6c0a2e6 (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:
diff changeset
     3
#include "../../stdafx.h"
9a5ed6c0a2e6 (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:
diff changeset
     4
#include "../../openttd.h"
9a5ed6c0a2e6 (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:
diff changeset
     5
#include "../../debug.h"
9a5ed6c0a2e6 (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:
diff changeset
     6
#include "../../functions.h"
9a5ed6c0a2e6 (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:
diff changeset
     7
#include "../../map.h"
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3033
diff changeset
     8
#include "../../road_map.h"
3946
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
     9
#include "../../vehicle.h"
2381
9a5ed6c0a2e6 (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:
diff changeset
    10
#include "../../command.h"
9a5ed6c0a2e6 (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:
diff changeset
    11
#include "trolly.h"
9a5ed6c0a2e6 (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:
diff changeset
    12
#include "../../engine.h"
9a5ed6c0a2e6 (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:
diff changeset
    13
#include "../../station.h"
9a5ed6c0a2e6 (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:
diff changeset
    14
#include "../../variables.h"
3359
633290bf1117 (svn r4154) -Moved MAX_BRIDGES in bridge.h and made it an enum. This makes two drops ...
celestar
parents: 3157
diff changeset
    15
#include "../../bridge.h"
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    16
#include "../ai.h"
2381
9a5ed6c0a2e6 (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:
diff changeset
    17
9a5ed6c0a2e6 (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:
diff changeset
    18
// Build HQ
9a5ed6c0a2e6 (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:
diff changeset
    19
//  Params:
9a5ed6c0a2e6 (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:
diff changeset
    20
//    tile : tile where HQ is going to be build
9a5ed6c0a2e6 (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:
diff changeset
    21
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
9a5ed6c0a2e6 (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:
diff changeset
    22
{
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    23
	if (CmdFailed(AI_DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
2381
9a5ed6c0a2e6 (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:
diff changeset
    24
		return false;
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    25
	AI_DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
2381
9a5ed6c0a2e6 (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:
diff changeset
    26
	return true;
9a5ed6c0a2e6 (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:
diff changeset
    27
}
9a5ed6c0a2e6 (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:
diff changeset
    28
9a5ed6c0a2e6 (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:
diff changeset
    29
9a5ed6c0a2e6 (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:
diff changeset
    30
// Build station
9a5ed6c0a2e6 (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:
diff changeset
    31
//  Params:
9a5ed6c0a2e6 (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:
diff changeset
    32
//    type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station
9a5ed6c0a2e6 (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:
diff changeset
    33
//    tile : tile where station is going to be build
9a5ed6c0a2e6 (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:
diff changeset
    34
//    length : in case of AI_TRAIN: length of station
9a5ed6c0a2e6 (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:
diff changeset
    35
//    numtracks : in case of AI_TRAIN: tracks of station
9a5ed6c0a2e6 (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:
diff changeset
    36
//    direction : the direction of the station
9a5ed6c0a2e6 (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:
diff changeset
    37
//    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
    38
CommandCost AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
2381
9a5ed6c0a2e6 (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:
diff changeset
    39
{
9a5ed6c0a2e6 (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:
diff changeset
    40
	if (type == AI_TRAIN)
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
    41
		return AI_DoCommand(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
2381
9a5ed6c0a2e6 (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:
diff changeset
    42
9a5ed6c0a2e6 (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:
diff changeset
    43
	if (type == AI_BUS)
6681
567445b47b68 (svn r9912) -Fix: the AIs could build any road(station)s.
rubidium
parents: 6491
diff changeset
    44
		return AI_DoCommand(tile, direction, ROADTYPES_ROAD << 2 | RoadStop::BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
2381
9a5ed6c0a2e6 (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:
diff changeset
    45
6681
567445b47b68 (svn r9912) -Fix: the AIs could build any road(station)s.
rubidium
parents: 6491
diff changeset
    46
	return AI_DoCommand(tile, direction, ROADTYPES_ROAD << 2 | RoadStop::TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
2381
9a5ed6c0a2e6 (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:
diff changeset
    47
}
9a5ed6c0a2e6 (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:
diff changeset
    48
9a5ed6c0a2e6 (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:
diff changeset
    49
9a5ed6c0a2e6 (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:
diff changeset
    50
// Builds a brdige. The second best out of the ones available for this player
9a5ed6c0a2e6 (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:
diff changeset
    51
//  Params:
9a5ed6c0a2e6 (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:
diff changeset
    52
//   tile_a : starting point
9a5ed6c0a2e6 (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:
diff changeset
    53
//   tile_b : end point
9a5ed6c0a2e6 (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:
diff changeset
    54
//   flag : flag passed to DoCommand
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
    55
CommandCost AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
2381
9a5ed6c0a2e6 (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:
diff changeset
    56
{
9a5ed6c0a2e6 (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:
diff changeset
    57
	int bridge_type, bridge_len, type, type2;
9a5ed6c0a2e6 (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:
diff changeset
    58
9a5ed6c0a2e6 (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:
diff changeset
    59
	// Find a good bridgetype (the best money can buy)
9a5ed6c0a2e6 (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:
diff changeset
    60
	bridge_len = GetBridgeLength(tile_a, tile_b);
9a5ed6c0a2e6 (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:
diff changeset
    61
	type = type2 = 0;
9a5ed6c0a2e6 (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:
diff changeset
    62
	for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) {
9a5ed6c0a2e6 (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:
diff changeset
    63
		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
9a5ed6c0a2e6 (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:
diff changeset
    64
			type2 = type;
9a5ed6c0a2e6 (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:
diff changeset
    65
			type = bridge_type;
9a5ed6c0a2e6 (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:
diff changeset
    66
			// We found two bridges, exit
9a5ed6c0a2e6 (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:
diff changeset
    67
			if (type2 != 0) break;
9a5ed6c0a2e6 (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:
diff changeset
    68
		}
9a5ed6c0a2e6 (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:
diff changeset
    69
	}
4000
4009d092b306 (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: 3946
diff changeset
    70
	// There is only one bridge that can be built
2381
9a5ed6c0a2e6 (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:
diff changeset
    71
	if (type2 == 0 && type != 0) type2 = type;
9a5ed6c0a2e6 (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:
diff changeset
    72
9a5ed6c0a2e6 (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:
diff changeset
    73
	// Now, simply, build the bridge!
4000
4009d092b306 (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: 3946
diff changeset
    74
	if (p->ainew.tbt == AI_TRAIN) {
4009d092b306 (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: 3946
diff changeset
    75
		return AI_DoCommand(tile_a, tile_b, (0x00 << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
4009d092b306 (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: 3946
diff changeset
    76
	} else {
6681
567445b47b68 (svn r9912) -Fix: the AIs could build any road(station)s.
rubidium
parents: 6491
diff changeset
    77
		return AI_DoCommand(tile_a, tile_b, ((0x80 | ROADTYPES_ROAD) << 8) + type2, flag | DC_AUTO, CMD_BUILD_BRIDGE);
4000
4009d092b306 (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: 3946
diff changeset
    78
	}
2381
9a5ed6c0a2e6 (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:
diff changeset
    79
}
9a5ed6c0a2e6 (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:
diff changeset
    80
9a5ed6c0a2e6 (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:
diff changeset
    81
9a5ed6c0a2e6 (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:
diff changeset
    82
// Build the route part by part
9a5ed6c0a2e6 (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:
diff changeset
    83
// Basicly what this function do, is build that amount of parts of the route
9a5ed6c0a2e6 (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:
diff changeset
    84
//  that go in the same direction. It sets 'part' to the last part of the route builded.
9a5ed6c0a2e6 (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:
diff changeset
    85
//  The return value is the cost for the builded parts
9a5ed6c0a2e6 (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:
diff changeset
    86
//
9a5ed6c0a2e6 (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:
diff changeset
    87
//  Params:
9a5ed6c0a2e6 (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:
diff changeset
    88
//   PathFinderInfo : Pointer to the PathFinderInfo used for AiPathFinder
9a5ed6c0a2e6 (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:
diff changeset
    89
//   part : Which part we need to build
9a5ed6c0a2e6 (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:
diff changeset
    90
//
9a5ed6c0a2e6 (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:
diff changeset
    91
// TODO: skip already builded road-pieces (e.g.: cityroad)
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
    92
CommandCost AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag)
2381
9a5ed6c0a2e6 (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:
diff changeset
    93
{
9a5ed6c0a2e6 (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:
diff changeset
    94
	int part = PathFinderInfo->position;
9a5ed6c0a2e6 (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:
diff changeset
    95
	byte *route_extra = PathFinderInfo->route_extra;
9a5ed6c0a2e6 (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:
diff changeset
    96
	TileIndex *route = PathFinderInfo->route;
9a5ed6c0a2e6 (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:
diff changeset
    97
	int dir;
9a5ed6c0a2e6 (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:
diff changeset
    98
	int old_dir = -1;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
    99
	CommandCost cost;
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
   100
	CommandCost res;
2381
9a5ed6c0a2e6 (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:
diff changeset
   101
	// We need to calculate the direction with the parent of the parent.. so we skip
9a5ed6c0a2e6 (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:
diff changeset
   102
	//  the first pieces and the last piece
9a5ed6c0a2e6 (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:
diff changeset
   103
	if (part < 1) part = 1;
9a5ed6c0a2e6 (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:
diff changeset
   104
	// When we are done, stop it
4000
4009d092b306 (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: 3946
diff changeset
   105
	if (part >= PathFinderInfo->route_length - 1) {
4009d092b306 (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: 3946
diff changeset
   106
		PathFinderInfo->position = -2;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   107
		return CommandCost();
4000
4009d092b306 (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: 3946
diff changeset
   108
	}
2381
9a5ed6c0a2e6 (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:
diff changeset
   109
9a5ed6c0a2e6 (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:
diff changeset
   110
9a5ed6c0a2e6 (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:
diff changeset
   111
	if (PathFinderInfo->rail_or_road) {
9a5ed6c0a2e6 (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:
diff changeset
   112
		// Tunnel code
9a5ed6c0a2e6 (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:
diff changeset
   113
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   114
			cost.AddCost(AI_DoCommand(route[part], 0, 0, flag, CMD_BUILD_TUNNEL));
2381
9a5ed6c0a2e6 (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:
diff changeset
   115
			PathFinderInfo->position++;
9a5ed6c0a2e6 (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:
diff changeset
   116
			// TODO: problems!
9a5ed6c0a2e6 (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:
diff changeset
   117
			if (CmdFailed(cost)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   118
				DEBUG(ai, 0, "[BuildPath] tunnel could not be built (0x%X)", route[part]);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   119
				return CommandCost();
2381
9a5ed6c0a2e6 (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:
diff changeset
   120
			}
9a5ed6c0a2e6 (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:
diff changeset
   121
			return cost;
9a5ed6c0a2e6 (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:
diff changeset
   122
		}
9a5ed6c0a2e6 (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:
diff changeset
   123
		// Bridge code
9a5ed6c0a2e6 (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:
diff changeset
   124
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   125
			cost.AddCost(AiNew_Build_Bridge(p, route[part], route[part - 1], flag));
2381
9a5ed6c0a2e6 (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:
diff changeset
   126
			PathFinderInfo->position++;
9a5ed6c0a2e6 (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:
diff changeset
   127
			// TODO: problems!
9a5ed6c0a2e6 (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:
diff changeset
   128
			if (CmdFailed(cost)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   129
				DEBUG(ai, 0, "[BuildPath] bridge could not be built (0x%X, 0x%X)", route[part], route[part - 1]);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   130
				return CommandCost();
2381
9a5ed6c0a2e6 (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:
diff changeset
   131
			}
9a5ed6c0a2e6 (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:
diff changeset
   132
			return cost;
9a5ed6c0a2e6 (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:
diff changeset
   133
		}
9a5ed6c0a2e6 (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:
diff changeset
   134
9a5ed6c0a2e6 (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:
diff changeset
   135
		// Build normal rail
9a5ed6c0a2e6 (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:
diff changeset
   136
		// Keep it doing till we go an other way
4000
4009d092b306 (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: 3946
diff changeset
   137
		if (route_extra[part - 1] == 0 && route_extra[part] == 0) {
2381
9a5ed6c0a2e6 (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:
diff changeset
   138
			while (route_extra[part] == 0) {
9a5ed6c0a2e6 (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:
diff changeset
   139
				// Get the current direction
9a5ed6c0a2e6 (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:
diff changeset
   140
				dir = AiNew_GetRailDirection(route[part-1], route[part], route[part+1]);
9a5ed6c0a2e6 (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:
diff changeset
   141
				// Is it the same as the last one?
9a5ed6c0a2e6 (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:
diff changeset
   142
				if (old_dir != -1 && old_dir != dir) break;
9a5ed6c0a2e6 (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:
diff changeset
   143
				old_dir = dir;
9a5ed6c0a2e6 (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:
diff changeset
   144
				// Build the tile
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   145
				res = AI_DoCommand(route[part], 0, dir, flag, CMD_BUILD_SINGLE_RAIL);
2381
9a5ed6c0a2e6 (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:
diff changeset
   146
				if (CmdFailed(res)) {
9a5ed6c0a2e6 (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:
diff changeset
   147
					// Problem.. let's just abort it all!
9a5ed6c0a2e6 (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:
diff changeset
   148
					p->ainew.state = AI_STATE_NOTHING;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   149
					return CommandCost();
2381
9a5ed6c0a2e6 (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:
diff changeset
   150
				}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   151
				cost.AddCost(res);
2381
9a5ed6c0a2e6 (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:
diff changeset
   152
				// Go to the next tile
9a5ed6c0a2e6 (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:
diff changeset
   153
				part++;
9a5ed6c0a2e6 (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:
diff changeset
   154
				// Check if it is still in range..
9a5ed6c0a2e6 (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:
diff changeset
   155
				if (part >= PathFinderInfo->route_length - 1) break;
9a5ed6c0a2e6 (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:
diff changeset
   156
			}
9a5ed6c0a2e6 (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:
diff changeset
   157
			part--;
9a5ed6c0a2e6 (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:
diff changeset
   158
		}
9a5ed6c0a2e6 (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:
diff changeset
   159
		// We want to return the last position, so we go back one
9a5ed6c0a2e6 (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:
diff changeset
   160
		PathFinderInfo->position = part;
9a5ed6c0a2e6 (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:
diff changeset
   161
	} else {
9a5ed6c0a2e6 (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:
diff changeset
   162
		// Tunnel code
9a5ed6c0a2e6 (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:
diff changeset
   163
		if ((AI_PATHFINDER_FLAG_TUNNEL & route_extra[part]) != 0) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   164
			cost.AddCost(AI_DoCommand(route[part], 0x200 | ROADTYPES_ROAD, 0, flag, CMD_BUILD_TUNNEL));
2381
9a5ed6c0a2e6 (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:
diff changeset
   165
			PathFinderInfo->position++;
9a5ed6c0a2e6 (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:
diff changeset
   166
			// TODO: problems!
9a5ed6c0a2e6 (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:
diff changeset
   167
			if (CmdFailed(cost)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   168
				DEBUG(ai, 0, "[BuildPath] tunnel could not be built (0x%X)", route[part]);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   169
				return CommandCost();
2381
9a5ed6c0a2e6 (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:
diff changeset
   170
			}
9a5ed6c0a2e6 (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:
diff changeset
   171
			return cost;
9a5ed6c0a2e6 (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:
diff changeset
   172
		}
9a5ed6c0a2e6 (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:
diff changeset
   173
		// Bridge code
9a5ed6c0a2e6 (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:
diff changeset
   174
		if ((AI_PATHFINDER_FLAG_BRIDGE & route_extra[part]) != 0) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   175
			cost.AddCost(AiNew_Build_Bridge(p, route[part], route[part + 1], flag));
2381
9a5ed6c0a2e6 (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:
diff changeset
   176
			PathFinderInfo->position++;
9a5ed6c0a2e6 (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:
diff changeset
   177
			// TODO: problems!
9a5ed6c0a2e6 (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:
diff changeset
   178
			if (CmdFailed(cost)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   179
				DEBUG(ai, 0, "[BuildPath] bridge could not be built (0x%X, 0x%X)", route[part], route[part + 1]);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   180
				return CommandCost();
2381
9a5ed6c0a2e6 (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:
diff changeset
   181
			}
9a5ed6c0a2e6 (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:
diff changeset
   182
			return cost;
9a5ed6c0a2e6 (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:
diff changeset
   183
		}
9a5ed6c0a2e6 (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:
diff changeset
   184
9a5ed6c0a2e6 (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:
diff changeset
   185
		// Build normal road
9a5ed6c0a2e6 (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:
diff changeset
   186
		// Keep it doing till we go an other way
7758
17ad53748c7b (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 7370
diff changeset
   187
		// EnsureNoVehicleOnGround makes sure we don't build on a tile where a vehicle is. This way
2381
9a5ed6c0a2e6 (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:
diff changeset
   188
		//  it will wait till the vehicle is gone..
7758
17ad53748c7b (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 7370
diff changeset
   189
		if (route_extra[part-1] == 0 && route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicleOnGround(route[part]))) {
17ad53748c7b (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 7370
diff changeset
   190
			while (route_extra[part] == 0 && (flag != DC_EXEC || EnsureNoVehicleOnGround(route[part]))) {
2381
9a5ed6c0a2e6 (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:
diff changeset
   191
				// Get the current direction
9a5ed6c0a2e6 (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:
diff changeset
   192
				dir = AiNew_GetRoadDirection(route[part-1], route[part], route[part+1]);
9a5ed6c0a2e6 (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:
diff changeset
   193
				// Is it the same as the last one?
9a5ed6c0a2e6 (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:
diff changeset
   194
				if (old_dir != -1 && old_dir != dir) break;
9a5ed6c0a2e6 (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:
diff changeset
   195
				old_dir = dir;
9a5ed6c0a2e6 (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:
diff changeset
   196
				// There is already some road, and it is a bridge.. don't build!!!
9a5ed6c0a2e6 (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:
diff changeset
   197
				if (!IsTileType(route[part], MP_TUNNELBRIDGE)) {
9a5ed6c0a2e6 (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:
diff changeset
   198
					// Build the tile
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   199
					res = AI_DoCommand(route[part], dir, 0, flag | DC_NO_WATER, CMD_BUILD_ROAD);
2381
9a5ed6c0a2e6 (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:
diff changeset
   200
					// Currently, we ignore CMD_ERRORs!
7758
17ad53748c7b (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 7370
diff changeset
   201
					if (CmdFailed(res) && flag == DC_EXEC && !IsTileType(route[part], MP_ROAD) && !EnsureNoVehicleOnGround(route[part])) {
2381
9a5ed6c0a2e6 (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:
diff changeset
   202
						// Problem.. let's just abort it all!
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5032
diff changeset
   203
						DEBUG(ai, 0, "[BuidPath] route building failed at tile 0x%X, aborting", route[part]);
2381
9a5ed6c0a2e6 (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:
diff changeset
   204
						p->ainew.state = AI_STATE_NOTHING;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   205
						return CommandCost();
2381
9a5ed6c0a2e6 (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:
diff changeset
   206
					}
9a5ed6c0a2e6 (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:
diff changeset
   207
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   208
					if (CmdSucceeded(res)) cost.AddCost(res);
2381
9a5ed6c0a2e6 (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:
diff changeset
   209
				}
9a5ed6c0a2e6 (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:
diff changeset
   210
				// Go to the next tile
9a5ed6c0a2e6 (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:
diff changeset
   211
				part++;
9a5ed6c0a2e6 (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:
diff changeset
   212
				// Check if it is still in range..
9a5ed6c0a2e6 (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:
diff changeset
   213
				if (part >= PathFinderInfo->route_length - 1) break;
9a5ed6c0a2e6 (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:
diff changeset
   214
			}
9a5ed6c0a2e6 (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:
diff changeset
   215
			part--;
9a5ed6c0a2e6 (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:
diff changeset
   216
			// We want to return the last position, so we go back one
9a5ed6c0a2e6 (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:
diff changeset
   217
		}
7758
17ad53748c7b (svn r11303) -Fix: EnsureNoVehicle and EnsureNoVehicleOnGround were both used to check whether there was no vehicle on the ground, except that the former didn't take care of aircraft shadows. So now we only use EnsureNoVehicleOnGround.
rubidium
parents: 7370
diff changeset
   218
		if (!EnsureNoVehicleOnGround(route[part]) && flag == DC_EXEC) part--;
2381
9a5ed6c0a2e6 (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:
diff changeset
   219
		PathFinderInfo->position = part;
9a5ed6c0a2e6 (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:
diff changeset
   220
	}
9a5ed6c0a2e6 (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:
diff changeset
   221
9a5ed6c0a2e6 (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:
diff changeset
   222
	return cost;
9a5ed6c0a2e6 (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:
diff changeset
   223
}
9a5ed6c0a2e6 (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:
diff changeset
   224
9a5ed6c0a2e6 (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:
diff changeset
   225
9a5ed6c0a2e6 (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:
diff changeset
   226
// This functions tries to find the best vehicle for this type of cargo
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   227
// It returns INVALID_ENGINE if not suitable engine is found
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   228
EngineID AiNew_PickVehicle(Player *p)
2381
9a5ed6c0a2e6 (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:
diff changeset
   229
{
9a5ed6c0a2e6 (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:
diff changeset
   230
	if (p->ainew.tbt == AI_TRAIN) {
9a5ed6c0a2e6 (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:
diff changeset
   231
		// Not supported yet
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   232
		return INVALID_ENGINE;
2381
9a5ed6c0a2e6 (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:
diff changeset
   233
	} else {
5029
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   234
		EngineID best_veh_index = INVALID_ENGINE;
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   235
		int32 best_veh_rating = 0;
5016
abe2d67ce392 (svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents: 5015
diff changeset
   236
		EngineID start = ROAD_ENGINES_INDEX;
abe2d67ce392 (svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents: 5015
diff changeset
   237
		EngineID end   = ROAD_ENGINES_INDEX + NUM_ROAD_ENGINES;
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   238
		EngineID i;
2381
9a5ed6c0a2e6 (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:
diff changeset
   239
5030
9eaeba327ddf (svn r7072) -Fix (r7070): Go up, not down, through the engines. And replace the comment too...
peter1138
parents: 5029
diff changeset
   240
		/* Loop through all road vehicles */
9eaeba327ddf (svn r7072) -Fix (r7070): Go up, not down, through the engines. And replace the comment too...
peter1138
parents: 5029
diff changeset
   241
		for (i = start; i != end; i++) {
5029
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   242
			const RoadVehicleInfo *rvi = RoadVehInfo(i);
3887
ece644bbf459 (svn r4943) uint tile -> TileIndex tile, byte player -> PlayerID player
tron
parents: 3885
diff changeset
   243
			const Engine* e = GetEngine(i);
5029
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   244
			int32 rating;
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
   245
			CommandCost ret;
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   246
5016
abe2d67ce392 (svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents: 5015
diff changeset
   247
			/* Skip vehicles which can't take our cargo type */
5031
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   248
			if (rvi->cargo_type != p->ainew.cargo && !CanRefitTo(i, p->ainew.cargo)) continue;
5016
abe2d67ce392 (svn r7057) -Codechange: Remove hardcoded lists of road vehicles for each cargo type in favour of just checking the cargo type of each vehicle.
peter1138
parents: 5015
diff changeset
   249
2381
9a5ed6c0a2e6 (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:
diff changeset
   250
			// Is it availiable?
9a5ed6c0a2e6 (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:
diff changeset
   251
			// Also, check if the reliability of the vehicle is above the AI_VEHICLE_MIN_RELIABILTY
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7758
diff changeset
   252
			if (!HasBit(e->player_avail, _current_player) || e->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
5029
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   253
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   254
			/* Rate and compare the engine by speed & capacity */
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   255
			rating = rvi->max_speed * rvi->capacity;
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   256
			if (rating <= best_veh_rating) continue;
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   257
2381
9a5ed6c0a2e6 (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:
diff changeset
   258
			// Can we build it?
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   259
			ret = AI_DoCommand(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
5029
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   260
			if (CmdFailed(ret)) continue;
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   261
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   262
			best_veh_rating = rating;
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   263
			best_veh_index = i;
2381
9a5ed6c0a2e6 (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:
diff changeset
   264
		}
5029
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   265
455ac4f14c9b (svn r7070) -Codechange: Make the AI choose road vehicles based on a rating (currently max speed * capacity) instead of either the cost or the index of the vheicle.
peter1138
parents: 5016
diff changeset
   266
		return best_veh_index;
2381
9a5ed6c0a2e6 (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:
diff changeset
   267
	}
9a5ed6c0a2e6 (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:
diff changeset
   268
}
9a5ed6c0a2e6 (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:
diff changeset
   269
9a5ed6c0a2e6 (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:
diff changeset
   270
3946
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   271
void CcAI(bool success, TileIndex tile, uint32 p1, uint32 p2)
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   272
{
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   273
	Player* p = GetPlayer(_current_player);
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   274
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   275
	if (success) {
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   276
		p->ainew.state = AI_STATE_GIVE_ORDERS;
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   277
		p->ainew.veh_id = _new_vehicle_id;
5031
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   278
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   279
		if (GetVehicle(p->ainew.veh_id)->cargo_type != p->ainew.cargo) {
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   280
			/* Cargo type doesn't match, so refit it */
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   281
			if (CmdFailed(DoCommand(tile, p->ainew.veh_id, p->ainew.cargo, DC_EXEC, CMD_REFIT_ROAD_VEH))) {
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   282
				/* Refit failed, so sell the vehicle */
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   283
				DoCommand(tile, p->ainew.veh_id, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   284
				p->ainew.state = AI_STATE_NOTHING;
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   285
			}
93e873e3ab03 (svn r7073) -Feature: Add cargo refit support to both AIs for road vehicles
peter1138
parents: 5030
diff changeset
   286
		}
3946
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   287
	} else {
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   288
		/* XXX this should be handled more gracefully */
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   289
		p->ainew.state = AI_STATE_NOTHING;
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   290
	}
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   291
}
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   292
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   293
2381
9a5ed6c0a2e6 (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:
diff changeset
   294
// Builds the best vehicle possible
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
   295
CommandCost AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
2381
9a5ed6c0a2e6 (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:
diff changeset
   296
{
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   297
	EngineID i = AiNew_PickVehicle(p);
2381
9a5ed6c0a2e6 (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:
diff changeset
   298
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3359
diff changeset
   299
	if (i == INVALID_ENGINE) return CMD_ERROR;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2549
diff changeset
   300
	if (p->ainew.tbt == AI_TRAIN) return CMD_ERROR;
2381
9a5ed6c0a2e6 (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:
diff changeset
   301
3946
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   302
	if (flag & DC_EXEC) {
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   303
		return AI_DoCommandCc(tile, i, 0, flag, CMD_BUILD_ROAD_VEH, CcAI);
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   304
	} else {
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   305
		return AI_DoCommand(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
3c8c78208dbb (svn r5092) -Fix: There was a gross race condition in the AI code which made it pretty random if the AI could give a new vehicle its orders
tron
parents: 3887
diff changeset
   306
	}
2381
9a5ed6c0a2e6 (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:
diff changeset
   307
}
9a5ed6c0a2e6 (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:
diff changeset
   308
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
   309
CommandCost AiNew_Build_Depot(Player* p, TileIndex tile, DiagDirection direction, byte flag)
2381
9a5ed6c0a2e6 (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:
diff changeset
   310
{
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6681
diff changeset
   311
	CommandCost ret, ret2;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   312
	if (p->ainew.tbt == AI_TRAIN) {
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2639
diff changeset
   313
		return AI_DoCommand(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   314
	} else {
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   315
		ret = AI_DoCommand(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   316
		if (CmdFailed(ret2)) return ret;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   317
		// Try to build the road from the depot
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4077
diff changeset
   318
		ret2 = AI_DoCommand(tile + TileOffsByDiagDir(direction), DiagDirToRoadBits(ReverseDiagDir(direction)), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   319
		// If it fails, ignore it..
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   320
		if (CmdFailed(ret2)) return ret;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   321
		ret.AddCost(ret2);
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   322
		return ret;
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
   323
	}
2381
9a5ed6c0a2e6 (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:
diff changeset
   324
}