src/ai/trolly/trolly.cpp
author rubidium
Wed, 26 Dec 2007 13:50:40 +0000
changeset 8140 0d0d8c94f84b
parent 8139 4e91c448c409
child 8144 65cec0877b78
permissions -rw-r--r--
(svn r11702) -Codechange: move all date related stuff to date*.
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     1
/* $Id$ */
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
     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
/*
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     4
 * This AI was created as a direct reaction to the big demand for some good AIs
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     5
 * in OTTD. Too bad it never left alpha-stage, and it is considered dead in its
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     6
 * current form.
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     7
 * By the time of writing this, we, the creator of this AI and a good friend of
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     8
 * mine, are designing a whole new AI-system that allows us to create AIs
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
     9
 * easier and without all the fuzz we encountered while I was working on this
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
    10
 * AI. By the time that system is finished, you can expect that this AI will
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
    11
 * dissapear, because it is pretty obselete and bad programmed.
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
    12
 *
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
    13
 * Meanwhile I wish you all much fun with this AI; if you are interested as
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
    14
 * AI-developer in this AI, I advise you not stare too long to some code, some
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2381
diff changeset
    15
 * things in here really are... strange ;) But in either way: enjoy :)
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
    16
 *
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
 *  -- TrueLight :: 2005-09-01
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
 */
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
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
#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
    21
#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
    22
#include "../../debug.h"
3144
33e42feae531 (svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents: 3071
diff changeset
    23
#include "../../road_map.h"
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3179
diff changeset
    24
#include "../../station_map.h"
5656
7917bf321f97 (svn r8116) Undo r8113
tron
parents: 5654
diff changeset
    25
#include "table/strings.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8108
diff changeset
    26
#include "../../command_func.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
    27
#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
    28
#include "../../town.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
    29
#include "../../industry.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
    30
#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
    31
#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
    32
#include "../../gui.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
    33
#include "../../depot.h"
3577
c6532e0bee4c (svn r4463) -Codechange. Include vehicle.h directly instead of implicitly via station.h in a number of source files
celestar
parents: 3404
diff changeset
    34
#include "../../vehicle.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    35
#include "../../date_func.h"
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
    36
#include "../ai.h"
8064
fef5cd447a5a (svn r11625) -Codechange: add CO_* enum at some places, add includes of order.h too
smatz
parents: 7763
diff changeset
    37
#include "../../order.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
    38
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
// This function is called after StartUp. It is the init of an AI
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
static void AiNew_State_FirstTime(Player *p)
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
    41
{
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
	// This assert is used to protect those function from misuse
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
	//   You have quickly a small mistake in the state-array
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
    44
	//   With that, everything would go wrong. Finding that, is almost impossible
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
	//   With this assert, that problem can never happen.
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
    46
	assert(p->ainew.state == AI_STATE_FIRST_TIME);
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
	// We first have to init some things
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
4854
151cdb683187 (svn r6780) -Codechange: Remove GPMI leftovers (-b impersonisation of AI in MP).
Darkvater
parents: 4559
diff changeset
    49
	if (_current_player == 1) ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 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
    50
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
	// The PathFinder (AyStar)
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
	// TODO: Maybe when an AI goes bankrupt, this is de-init
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
	//  or when coming from a savegame.. should be checked out!
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
	p->ainew.path_info.start_tile_tl = 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
    55
	p->ainew.path_info.start_tile_br = 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
    56
	p->ainew.path_info.end_tile_tl = 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
    57
	p->ainew.path_info.end_tile_br = 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
    58
	p->ainew.pathfinder = new_AyStar_AiPathFinder(12, &p->ainew.path_info);
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
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
	p->ainew.idle = 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
    61
	p->ainew.last_vehiclecheck_date = _date;
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
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
	// We ALWAYS start with a bus route.. just some basic money ;)
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
	p->ainew.action = AI_ACTION_BUS_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
    65
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
	// Let's popup the news, and after that, start building..
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
	p->ainew.state = AI_STATE_WAKE_UP;
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
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
    70
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
// This function just waste some time
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
//  It keeps it more real. The AI can build on such tempo no normal user
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
//  can ever keep up with that. The competitor_speed already delays a bit
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
    74
//  but after the AI finished a track it really needs to go to sleep.
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
    75
//
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
    76
// Let's say, we sleep between one and three days if the AI is put on Very Fast.
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
    77
//  This means that on Very Slow it will be between 16 and 48 days.. slow enough?
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
    78
static void AiNew_State_Nothing(Player *p)
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
	assert(p->ainew.state == AI_STATE_NOTHING);
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
	// If we are done idling, start over again
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
    82
	if (p->ainew.idle == 0) p->ainew.idle = AI_RandomRange(DAY_TICKS * 2) + DAY_TICKS;
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
    83
	if (--p->ainew.idle == 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
    84
		// We are done idling.. what you say? Let's do something!
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
		// I mean.. the next tick ;)
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
		p->ainew.state = AI_STATE_WAKE_UP;
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
	}
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
}
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
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
// This function picks out a task we are going to do.
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
    92
//  Currently supported:
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
//    - Make new 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
    94
//    - Check 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
    95
//    - 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
    96
static void AiNew_State_WakeUp(Player *p)
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
{
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 c;
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
    99
	assert(p->ainew.state == AI_STATE_WAKE_UP);
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
   100
	// First, check if we have a 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
   101
	if (p->location_of_house == 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
   102
		// We have no HQ yet, build one on a random place
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
		// Random till we found a place for it!
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
		// TODO: this should not be on a random place..
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
   105
		AiNew_Build_CompanyHQ(p, AI_Random() % MapSize());
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
   106
		// Enough for now, but we want to come back here the next time
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
   107
		//  so we do not change any status
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
   108
		return;
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
6990
136a08baf0ed (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 6950
diff changeset
   111
	Money money = p->player_money - AI_MINIMUM_MONEY;
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
   112
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
	// Let's pick an action!
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
   114
	if (p->ainew.action == AI_ACTION_NONE) {
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
   115
		c = AI_Random() & 0xFF;
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
   116
		if (p->current_loan > 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
   117
				p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN &&
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
   118
				c < 10) {
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
   119
			p->ainew.action = AI_ACTION_REPAY_LOAN;
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
		} else if (p->ainew.last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) {
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
			// Check all vehicles once in a while
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
			p->ainew.action = AI_ACTION_CHECK_ALL_VEHICLES;
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
			p->ainew.last_vehiclecheck_date = _date;
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
		} else if (c < 100 && !_patches.ai_disable_veh_roadveh) {
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
   125
			// Do we have any spots for road-vehicles left open?
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
   126
			if (GetFreeUnitNumber(VEH_ROAD) <= _patches.max_roadveh) {
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: 3952
diff changeset
   127
				if (c < 85) {
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
   128
					p->ainew.action = AI_ACTION_TRUCK_ROUTE;
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: 3952
diff changeset
   129
				} else {
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
   130
					p->ainew.action = AI_ACTION_BUS_ROUTE;
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: 3952
diff changeset
   131
				}
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
   132
			}
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: 3952
diff changeset
   133
#if 0
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: 3952
diff changeset
   134
		} else if (c < 200 && !_patches.ai_disable_veh_train) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
   135
			if (GetFreeUnitNumber(VEH_TRAIN) <= _patches.max_trains) {
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
   136
				p->ainew.action = AI_ACTION_TRAIN_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
   137
			}
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: 3952
diff changeset
   138
#endif
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: 3952
diff changeset
   139
		}
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
   140
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
		p->ainew.counter = 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
   142
	}
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
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
	if (p->ainew.counter++ > AI_MAX_TRIES_FOR_SAME_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
   145
		p->ainew.action = AI_ACTION_NONE;
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
		return;
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
	}
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
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
   149
	if (_patches.ai_disable_veh_roadveh && (
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
				p->ainew.action == AI_ACTION_BUS_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
   151
				p->ainew.action == AI_ACTION_TRUCK_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
   152
			)) {
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
		p->ainew.action = AI_ACTION_NONE;
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
		return;
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
	}
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
	if (p->ainew.action == AI_ACTION_REPAY_LOAN &&
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
			money > AI_MINIMUM_LOAN_REPAY_MONEY) {
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 start repaying some money..
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
		p->ainew.state = AI_STATE_REPAY_MONEY;
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
		return;
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
	}
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
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
   164
	if (p->ainew.action == AI_ACTION_CHECK_ALL_VEHICLES) {
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
		p->ainew.state = AI_STATE_CHECK_ALL_VEHICLES;
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
		return;
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
	}
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
   168
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
   169
	// It is useless to start finding a route if we don't have enough money
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
	//  to build the route anyway..
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
	if (p->ainew.action == AI_ACTION_BUS_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
   172
			money > AI_MINIMUM_BUS_ROUTE_MONEY) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
   173
		if (GetFreeUnitNumber(VEH_ROAD) > _patches.max_roadveh) {
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
   174
			p->ainew.action = AI_ACTION_NONE;
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
   175
			return;
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
		}
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
		p->ainew.cargo = AI_NEED_CARGO;
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
		p->ainew.state = AI_STATE_LOCATE_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
   179
		p->ainew.tbt = AI_BUS; // Bus-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
   180
		return;
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
	if (p->ainew.action == AI_ACTION_TRUCK_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
   183
			money > AI_MINIMUM_TRUCK_ROUTE_MONEY) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
   184
		if (GetFreeUnitNumber(VEH_ROAD) > _patches.max_roadveh) {
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
   185
			p->ainew.action = AI_ACTION_NONE;
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
			return;
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
   187
		}
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
		p->ainew.cargo = AI_NEED_CARGO;
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
   189
		p->ainew.last_id = 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
   190
		p->ainew.state = AI_STATE_LOCATE_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
   191
		p->ainew.tbt = AI_TRUCK;
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
		return;
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
	}
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
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
	p->ainew.state = AI_STATE_NOTHING;
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
}
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
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
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
   199
static void AiNew_State_ActionDone(Player *p)
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
{
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
   201
	p->ainew.action = AI_ACTION_NONE;
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
	p->ainew.state = AI_STATE_NOTHING;
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
   203
}
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
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
   205
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
// Check if a city or industry is good enough to start a route there
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
static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte 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
   208
{
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
	if (type == AI_CITY) {
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: 3952
diff changeset
   210
		const Town* t = GetTown(ic);
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: 3952
diff changeset
   211
		const Station* st;
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
   212
		uint count = 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
   213
		int j = 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
   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
		// We don't like roadconstructions, don't even true such a city
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
		if (t->road_build_months != 0) return false;
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
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
   218
		// Check if the rating in a city is high enough
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
		//  If not, take a chance if we want to continue
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   220
		if (t->ratings[_current_player] < 0 && AI_CHANCE16(1, 4)) return false;
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
   221
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   222
		if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !AI_CHANCE16(1, AI_CHECKCITY_CITY_CHANCE)) return false;
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
   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
		// Check if we have build a station in this town the last 6 months
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
		//  else we don't do it. This is done, because stat updates can be slow
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
		//  and sometimes it takes up to 4 months before the stats are corectly.
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
   227
		//  This way we don't get 12 busstations in one city of 100 population ;)
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
   228
		FOR_ALL_STATIONS(st) {
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
			// Do we own it?
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 (st->owner == _current_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
   231
				// Are we talking busses?
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
   232
				if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) != FACIL_BUS_STOP) continue;
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
				// Is it the same city as we are in now?
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
   234
				if (st->town != t) continue;
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
   235
				// When was this station 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
   236
				if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false;
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
   237
				// Cound the amount of stations in this city that we own
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
   238
				count++;
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
			} 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
   240
				// We do not own it, request some info about 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
   241
				//  we want to know if this station gets the same good. If so,
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
   242
				//  we want to know its rating. If it is too high, we are not going
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
   243
				//  to build there
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
   244
				if (!st->goods[CT_PASSENGERS].last_speed) continue;
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
   245
				// Is it around our city
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
   246
				if (DistanceManhattan(st->xy, t->xy) > 10) continue;
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
   247
				// It does take this cargo.. what is his rating?
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
   248
				if (st->goods[CT_PASSENGERS].rating < AI_CHECKCITY_CARGO_RATING) continue;
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
   249
				j++;
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
				// When this is the first station, we build a second with no problem ;)
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
				if (j == 1) continue;
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
   252
				// The rating is high.. second 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
   253
				//  a little chance that we still continue
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
   254
				//  But if there are 3 stations of this size, we never go on...
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
   255
				if (j == 2 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
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
   256
				// We don't like this 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
   257
				return false;
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
			}
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
   259
		}
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
   260
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
   261
		// We are about to add 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
   262
		count++;
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
   263
		// Check if we the city can provide enough cargo for this amount of stations..
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
		if (count * AI_CHECKCITY_CARGO_PER_STATION > t->max_pass) return false;
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
   265
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
   266
		// All check are okay, so we can build here!
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
		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
   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
	if (type == AI_INDUSTRY) {
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: 3952
diff changeset
   270
		const Industry* i = GetIndustry(ic);
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: 3952
diff changeset
   271
		const Station* st;
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
   272
		int count = 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
   273
		int j = 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
   274
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   275
		if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1, 4)) return false;
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
   276
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
   277
		// No limits on delevering stations!
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
   278
		//  Or for industry that does not give anything yet
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   279
		if (i->produced_cargo[0] == CT_INVALID || i->last_month_production[0] == 0) return true;
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
   280
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6794
diff changeset
   281
		if (i->last_month_production[0] - i->last_month_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false;
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
   282
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
   283
		// Check if we have build a station in this town the last 6 months
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
   284
		//  else we don't do it. This is done, because stat updates can be slow
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
   285
		//  and sometimes it takes up to 4 months before the stats are corectly.
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
   286
		FOR_ALL_STATIONS(st) {
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
   287
			// Do we own it?
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
   288
			if (st->owner == _current_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
   289
				// Are we talking trucks?
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
   290
				if (p->ainew.tbt == AI_TRUCK && (FACIL_TRUCK_STOP & st->facilities) != FACIL_TRUCK_STOP) continue;
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
   291
				// Is it the same city as we are in now?
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
   292
				if (st->town != i->town) continue;
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
   293
				// When was this station 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
   294
				if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false;
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
   295
				// Cound the amount of stations in this city that we own
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
				count++;
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
   297
			} 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
   298
				// We do not own it, request some info about 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
   299
				//  we want to know if this station gets the same good. If so,
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
   300
				//  we want to know its rating. If it is too high, we are not going
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
				//  to build there
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   302
				if (i->produced_cargo[0] == CT_INVALID) continue;
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
   303
				// It does not take this cargo
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   304
				if (!st->goods[i->produced_cargo[0]].last_speed) continue;
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
   305
				// Is it around our industry
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
   306
				if (DistanceManhattan(st->xy, i->xy) > 5) continue;
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
				// It does take this cargo.. what is his rating?
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   308
				if (st->goods[i->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue;
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
   309
				j++;
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
				// The rating is high.. a little chance that we still continue
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
   311
				//  But if there are 2 stations of this size, we never go on...
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
   312
				if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
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
   313
				// We don't like this 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
   314
				return false;
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
   315
			}
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
   316
		}
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
   317
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
   318
		// We are about to add 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
   319
		count++;
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
   320
		// Check if we the city can provide enough cargo for this amount of stations..
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6794
diff changeset
   321
		if (count * AI_CHECKCITY_CARGO_PER_STATION > i->last_month_production[0]) return false;
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
   322
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
   323
		// All check are okay, so we can build here!
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
		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
   325
	}
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
   326
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
   327
	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
   328
}
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
   329
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
   330
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
   331
// This functions tries to locate a good 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
   332
static void AiNew_State_LocateRoute(Player *p)
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
   333
{
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
   334
	assert(p->ainew.state == AI_STATE_LOCATE_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
   335
	// For now, we only support PASSENGERS, CITY and BUSSES
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
   336
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
   337
	// We don't have a route yet
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
   338
	if (p->ainew.cargo == AI_NEED_CARGO) {
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
   339
		p->ainew.new_cost = 0; // No cost yet
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
   340
		p->ainew.temp = -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
   341
		// Reset the counter
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
   342
		p->ainew.counter = 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
   343
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
   344
		p->ainew.from_ic = -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
   345
		p->ainew.to_ic = -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
   346
		if (p->ainew.tbt == AI_BUS) {
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
   347
			// For now we only have a passenger 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
   348
			p->ainew.cargo = CT_PASSENGERS;
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
   349
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
   350
			// Find a route to cities
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
   351
			p->ainew.from_type = AI_CITY;
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
   352
			p->ainew.to_type = AI_CITY;
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
   353
		} else if (p->ainew.tbt == AI_TRUCK) {
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
   354
			p->ainew.cargo = AI_NO_CARGO;
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
   355
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
   356
			p->ainew.from_type = AI_INDUSTRY;
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
   357
			p->ainew.to_type = AI_INDUSTRY;
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
   358
		}
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
   359
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
   360
		// Now we are doing initing, we wait one tick
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
   361
		return;
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
   362
	}
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
   363
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
   364
	// Increase the counter and abort if it is taking too long!
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
   365
	p->ainew.counter++;
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
   366
	if (p->ainew.counter > AI_LOCATE_ROUTE_MAX_COUNTER) {
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
   367
		// Switch back to doing nothing!
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
   368
		p->ainew.state = AI_STATE_NOTHING;
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
   369
		return;
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
   370
	}
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
   371
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
   372
	// We are going to locate a city from where we are going to connect
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
   373
	if (p->ainew.from_ic == -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
   374
		if (p->ainew.temp == -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
   375
			// First, we pick a random spot to search from
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: 3952
diff changeset
   376
			if (p->ainew.from_type == AI_CITY) {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   377
				p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1);
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: 3952
diff changeset
   378
			} else {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   379
				p->ainew.temp = AI_RandomRange(GetMaxIndustryIndex() + 1);
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: 3952
diff changeset
   380
			}
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
   381
		}
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
   382
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
   383
		if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_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
   384
			// It was not a valid city
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
   385
			//  increase the temp with one, and return. We will come back later here
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
   386
			//  to try again
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
   387
			p->ainew.temp++;
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
   388
			if (p->ainew.from_type == AI_CITY) {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   389
				if (p->ainew.temp > GetMaxTownIndex()) p->ainew.temp = 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
   390
			} else {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   391
				if (p->ainew.temp > GetMaxIndustryIndex()) p->ainew.temp = 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
   392
			}
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
   393
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
   394
			// Don't do an attempt if we are trying the same id as the last time...
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
   395
			if (p->ainew.last_id == p->ainew.temp) return;
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
   396
			p->ainew.last_id = p->ainew.temp;
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
   397
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
   398
			return;
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
   399
		}
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
   400
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
   401
		// We found a good city/industry, save the data of it
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
   402
		p->ainew.from_ic = p->ainew.temp;
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
   403
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
   404
		// Start the next tick with finding a to-city
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
   405
		p->ainew.temp = -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
   406
		return;
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
   407
	}
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
   408
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
   409
	// Find a to-city
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
   410
	if (p->ainew.temp == -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
   411
		// First, we pick a random spot to search to
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: 3952
diff changeset
   412
		if (p->ainew.to_type == AI_CITY) {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   413
			p->ainew.temp = AI_RandomRange(GetMaxTownIndex() + 1);
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: 3952
diff changeset
   414
		} else {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   415
			p->ainew.temp = AI_RandomRange(GetMaxIndustryIndex() + 1);
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: 3952
diff changeset
   416
		}
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
   417
	}
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
   418
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
   419
	// The same city is not allowed
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
   420
	// Also check if the city is valid
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
   421
	if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_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
   422
		// Maybe it is valid..
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
   423
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: 3952
diff changeset
   424
		/* We need to know if they are not to far apart from eachother..
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: 3952
diff changeset
   425
		 * We do that by checking how much cargo we have to move and how long the
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: 3952
diff changeset
   426
		 * route is.
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: 3952
diff changeset
   427
		 */
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
   428
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
   429
		if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) {
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   430
			const Town* town_from = GetTown(p->ainew.from_ic);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   431
			const Town* town_temp = GetTown(p->ainew.temp);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   432
			uint distance = DistanceManhattan(town_from->xy, town_temp->xy);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   433
			int max_cargo;
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   434
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   435
			max_cargo  = town_from->max_pass + town_temp->max_pass;
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   436
			max_cargo -= town_from->act_pass + town_temp->act_pass;
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   437
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
   438
			// max_cargo is now the amount of cargo we can move between the two cities
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
   439
			// If it is more than the distance, we allow it
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   440
			if (distance <= max_cargo * AI_LOCATEROUTE_BUS_CARGO_DISTANCE) {
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
   441
				// We found a good city/industry, save the data of it
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
   442
				p->ainew.to_ic = p->ainew.temp;
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
   443
				p->ainew.state = AI_STATE_FIND_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
   444
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
   445
				DEBUG(ai, 1, "[LocateRoute] found bus-route of %d tiles long (from %d to %d)",
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   446
					distance,
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
   447
					p->ainew.from_ic,
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
   448
					p->ainew.temp
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
   449
				);
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
   450
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
   451
				p->ainew.from_tile = 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
   452
				p->ainew.to_tile = 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
   453
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
   454
				return;
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
   455
			}
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
   456
		} else if (p->ainew.tbt == AI_TRUCK) {
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   457
			const Industry* ind_from = GetIndustry(p->ainew.from_ic);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   458
			const Industry* ind_temp = GetIndustry(p->ainew.temp);
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
   459
			bool found = false;
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
   460
			int max_cargo = 0;
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   461
			uint i;
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   462
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
   463
			// TODO: in max_cargo, also check other cargo (beside [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
   464
			// First we check if the from_ic produces cargo that this ic accepts
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   465
			if (ind_from->produced_cargo[0] != CT_INVALID && ind_from->last_month_production[0] != 0) {
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   466
				for (i = 0; i < lengthof(ind_temp->accepts_cargo); i++) {
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   467
					if (ind_temp->accepts_cargo[i] == CT_INVALID) break;
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   468
					if (ind_from->produced_cargo[0] == ind_temp->accepts_cargo[i]) {
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: 3952
diff changeset
   469
						// Found a compatible industry
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6794
diff changeset
   470
						max_cargo = ind_from->last_month_production[0] - ind_from->last_month_transported[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
   471
						found = 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
   472
						p->ainew.from_deliver = 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
   473
						p->ainew.to_deliver = false;
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
   474
						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
   475
					}
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
   476
				}
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
   477
			}
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   478
			if (!found && ind_temp->produced_cargo[0] != CT_INVALID && ind_temp->last_month_production[0] != 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
   479
				// If not check if the current ic produces cargo that the from_ic accepts
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   480
				for (i = 0; i < lengthof(ind_from->accepts_cargo); i++) {
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   481
					if (ind_from->accepts_cargo[i] == CT_INVALID) break;
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   482
					if (ind_from->produced_cargo[0] == ind_from->accepts_cargo[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
   483
						// Found a compatbiel industry
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
   484
						found = true;
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6794
diff changeset
   485
						max_cargo = ind_temp->last_month_production[0] - ind_temp->last_month_transported[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
   486
						p->ainew.from_deliver = false;
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
   487
						p->ainew.to_deliver = 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
   488
						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
   489
					}
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
   490
				}
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
   491
			}
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
   492
			if (found) {
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
   493
				// Yeah, they are compatible!!!
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
   494
				// Check the length against the amount of goods
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   495
				uint distance = DistanceManhattan(ind_from->xy, ind_temp->xy);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   496
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   497
				if (distance > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE &&
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   498
						distance <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) {
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
   499
					p->ainew.to_ic = p->ainew.temp;
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
   500
					if (p->ainew.from_deliver) {
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   501
						p->ainew.cargo = ind_from->produced_cargo[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
   502
					} else {
7645
efa25fcf7917 (svn r11176) -Revert (r9867): as it is needed for newgrf callbacks 14B and 14C
glx
parents: 7370
diff changeset
   503
						p->ainew.cargo = ind_temp->produced_cargo[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
   504
					}
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
   505
					p->ainew.state = AI_STATE_FIND_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
   506
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
   507
					DEBUG(ai, 1, "[LocateRoute] found truck-route of %d tiles long (from %d to %d)",
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   508
						distance,
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
   509
						p->ainew.from_ic,
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
   510
						p->ainew.temp
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
   511
					);
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
   512
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
   513
					p->ainew.from_tile = 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
   514
					p->ainew.to_tile = 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
   515
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
   516
					return;
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
   517
				}
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
   518
			}
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
   519
		}
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
   520
	}
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
   521
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
   522
	// It was not a valid city
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
   523
	//  increase the temp with one, and return. We will come back later here
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
   524
	//  to try again
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
   525
	p->ainew.temp++;
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
   526
	if (p->ainew.to_type == AI_CITY) {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   527
		if (p->ainew.temp > GetMaxTownIndex()) p->ainew.temp = 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
   528
	} else {
5247
1f982de55b88 (svn r7372) - CodeChange: Rename all GetXXXArraySize() functions to GetNumXXX() and add GetMaxXXXIndex() functions. This prepares for the new pool interface.
matthijs
parents: 5015
diff changeset
   529
		if (p->ainew.temp > GetMaxIndustryIndex()) p->ainew.temp = 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
   530
	}
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
   531
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
   532
	// Don't do an attempt if we are trying the same id as the last time...
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
   533
	if (p->ainew.last_id == p->ainew.temp) return;
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
   534
	p->ainew.last_id = p->ainew.temp;
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
   535
}
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
   536
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
   537
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
   538
// Check if there are not more than a certain amount of vehicles pointed to a certain
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
   539
//  station. This to prevent 10 busses going to one station, which gives... 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
   540
static bool AiNew_CheckVehicleStation(Player *p, Station *st)
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
   541
{
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
   542
	int count = 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
   543
	Vehicle *v;
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
   544
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
   545
	// Also check if we don't have already a lot of busses to this city...
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
   546
	FOR_ALL_VEHICLES(v) {
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
   547
		if (v->owner == _current_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
   548
			const Order *order;
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
   549
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
   550
			FOR_VEHICLE_ORDERS(v, order) {
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4389
diff changeset
   551
				if (order->type == OT_GOTO_STATION && GetStation(order->dest) == st) {
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
   552
					// This vehicle has this city in its list
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
   553
					count++;
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
   554
				}
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
   555
			}
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
   556
		}
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
   557
	}
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
   558
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
   559
	if (count > AI_CHECK_MAX_VEHICLE_PER_STATION) return false;
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
   560
	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
   561
}
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
   562
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
   563
// This function finds a good spot for a 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
   564
static void AiNew_State_FindStation(Player *p)
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
   565
{
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
   566
	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
   567
	Station *st;
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   568
	int count = 0;
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   569
	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
   570
	TileIndex new_tile = 0;
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
   571
	DiagDirection direction = DIAGDIR_NE;
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
   572
	Town *town = NULL;
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
   573
	assert(p->ainew.state == AI_STATE_FIND_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
   574
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
   575
	if (p->ainew.from_tile == 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
   576
		// First we scan for a station in the from-city
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
   577
		if (p->ainew.from_type == AI_CITY) {
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
   578
			town = GetTown(p->ainew.from_ic);
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
   579
			tile = town->xy;
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
   580
		} else {
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   581
			tile = GetIndustry(p->ainew.from_ic)->xy;
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
   582
		}
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
   583
	} else if (p->ainew.to_tile == 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
   584
		// Second we scan for a station in the to-city
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
   585
		if (p->ainew.to_type == AI_CITY) {
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
   586
			town = GetTown(p->ainew.to_ic);
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
   587
			tile = town->xy;
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
   588
		} else {
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   589
			tile = GetIndustry(p->ainew.to_ic)->xy;
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
   590
		}
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
   591
	} 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
   592
		// Unsupported request
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
   593
		// Go to FIND_PATH
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
   594
		p->ainew.temp = -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
   595
		p->ainew.state = AI_STATE_FIND_PATH;
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
   596
		return;
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
   597
	}
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
   598
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
   599
	// First, we are going to look at the stations that already exist inside the city
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
   600
	//  If there is enough cargo left in the station, we take that 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
   601
	//  If that is not possible, and there are more than 2 stations in the city, abort
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
   602
	i = AiNew_PickVehicle(p);
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
   603
	// Euhmz, this should not happen _EVER_
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
   604
	// Quit finding a route...
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   605
	if (i == INVALID_ENGINE) {
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   606
		p->ainew.state = AI_STATE_NOTHING;
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   607
		return;
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   608
	}
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
   609
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
   610
	FOR_ALL_STATIONS(st) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   611
		if (st->owner == _current_player) {
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   612
			if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) == FACIL_BUS_STOP) {
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   613
				if (st->town == town) {
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   614
					// Check how much cargo there is left in the station
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6990
diff changeset
   615
					if ((int)st->goods[p->ainew.cargo].cargo.Count() > RoadVehInfo(i)->capacity * AI_STATION_REUSE_MULTIPLER) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   616
						if (AiNew_CheckVehicleStation(p, st)) {
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   617
							// We did found a station that was good enough!
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   618
							new_tile = st->xy;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   619
							direction = GetRoadStopDir(st->xy);
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   620
							break;
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
   621
						}
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
   622
					}
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4261
diff changeset
   623
					count++;
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
   624
				}
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
   625
			}
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
   626
		}
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
   627
	}
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
   628
	// We are going to add a new 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
   629
	if (new_tile == 0) count++;
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
   630
	// No more than 2 stations allowed in a city
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
   631
	//  This is because only the best 2 stations of one cargo do get any cargo
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
   632
	if (count > 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
   633
		p->ainew.state = AI_STATE_NOTHING;
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
   634
		return;
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
   635
	}
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
   636
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
   637
	if (new_tile == 0 && p->ainew.tbt == AI_BUS) {
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
   638
		uint x, y, i = 0;
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   639
		CommandCost r;
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
   640
		uint best;
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
   641
		uint accepts[NUM_CARGO];
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   642
		TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4];
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   643
		uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4];
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
   644
		// To find a good spot we scan a range from the center, a get the 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
   645
		//  where we get the most cargo and where it is buildable.
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
   646
		// TODO: also check for station of myself and make sure we are not
6924
f2ac2b1f5d06 (svn r10177) -Fix: Passengers has one A and two Es (ln)
peter1138
parents: 6819
diff changeset
   647
		//   taking eachothers passengers away (bad result when it does not)
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
   648
		for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) {
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
   649
			for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) {
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
   650
				new_tile = TileXY(x, y);
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
   651
				if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) {
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
   652
					// This tile we can build on!
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
   653
					// Check acceptance
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
   654
					// XXX - Get the catchment area
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
   655
					GetAcceptanceAroundTiles(accepts, new_tile, 1, 1, 4);
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
   656
					// >> 3 == 0 means no cargo
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
   657
					if (accepts[p->ainew.cargo] >> 3 == 0) continue;
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
   658
					// See if we can build 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
   659
					r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
2737
fb1556b8f5e0 (svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents: 2684
diff changeset
   660
					if (CmdFailed(r)) continue;
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
   661
					// We can build it, so add it to found_spot
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
   662
					found_spot[i] = new_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
   663
					found_best[i++] = accepts[p->ainew.cargo];
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
   664
				}
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
   665
			}
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
   666
		}
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
   667
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: 3952
diff changeset
   668
		// If i is still zero, we did not find anything
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
   669
		if (i == 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
   670
			p->ainew.state = AI_STATE_NOTHING;
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
   671
			return;
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
   672
		}
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
   673
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
   674
		// Go through all the found_best and check which has the highest value
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
   675
		best = 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
   676
		new_tile = 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
   677
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: 3952
diff changeset
   678
		for (x = 0; x < i; x++) {
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
   679
			if (found_best[x] > best ||
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
   680
					(found_best[x] == best && DistanceManhattan(tile, new_tile) > DistanceManhattan(tile, found_spot[x]))) {
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
   681
				new_tile = found_spot[x];
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
   682
				best = found_best[x];
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
   683
			}
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
   684
		}
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
   685
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
   686
		// See how much it is going to cost us...
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
   687
		r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   688
		p->ainew.new_cost += r.GetCost();
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
   689
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
   690
		direction = (DiagDirection)AI_PATHFINDER_NO_DIRECTION;
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
   691
	} else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) {
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
   692
		// Truck station locater works differently.. a station can be on any place
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
   693
		//  as long as it is in range. So we give back code AI_STATION_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
   694
		//  so the pathfinder routine can work it out!
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
   695
		new_tile = AI_STATION_RANGE;
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
   696
		direction = (DiagDirection)AI_PATHFINDER_NO_DIRECTION;
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
   697
	}
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
   698
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
   699
	if (p->ainew.from_tile == 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
   700
		p->ainew.from_tile = new_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
   701
		p->ainew.from_direction = 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
   702
		// Now we found thisone, go in for to_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
   703
		return;
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
   704
	} else if (p->ainew.to_tile == 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
   705
		p->ainew.to_tile = new_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
   706
		p->ainew.to_direction = 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
   707
		// K, done placing stations!
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
   708
		p->ainew.temp = -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
   709
		p->ainew.state = AI_STATE_FIND_PATH;
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
   710
		return;
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
   711
	}
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
   712
}
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
   713
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
   714
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
   715
// We try to find a path between 2 points
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
   716
static void AiNew_State_FindPath(Player *p)
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
   717
{
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
   718
	int r;
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
   719
	assert(p->ainew.state == AI_STATE_FIND_PATH);
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
   720
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
   721
	// First time, init some data
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
   722
	if (p->ainew.temp == -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
   723
		// Init path_info
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
   724
		if (p->ainew.from_tile == AI_STATION_RANGE) {
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   725
			const Industry* i = GetIndustry(p->ainew.from_ic);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   726
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
   727
			// For truck routes we take a range around the industry
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   728
			p->ainew.path_info.start_tile_tl = i->xy - TileDiffXY(1, 1);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   729
			p->ainew.path_info.start_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1);
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
   730
			p->ainew.path_info.start_direction = p->ainew.from_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
   731
		} 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
   732
			p->ainew.path_info.start_tile_tl = p->ainew.from_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
   733
			p->ainew.path_info.start_tile_br = p->ainew.from_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
   734
			p->ainew.path_info.start_direction = p->ainew.from_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
   735
		}
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
   736
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
   737
		if (p->ainew.to_tile == AI_STATION_RANGE) {
3952
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   738
			const Industry* i = GetIndustry(p->ainew.to_ic);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   739
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   740
			p->ainew.path_info.end_tile_tl = i->xy - TileDiffXY(1, 1);
e96de3e61c9b (svn r5098) Do some manual CSE and replace a few magic numbers to improve the readability of the trolly AI
tron
parents: 3951
diff changeset
   741
			p->ainew.path_info.end_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1);
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
   742
			p->ainew.path_info.end_direction = p->ainew.to_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
   743
		} 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
   744
			p->ainew.path_info.end_tile_tl = p->ainew.to_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
   745
			p->ainew.path_info.end_tile_br = p->ainew.to_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
   746
			p->ainew.path_info.end_direction = p->ainew.to_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
   747
		}
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
   748
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: 3952
diff changeset
   749
		p->ainew.path_info.rail_or_road = (p->ainew.tbt == AI_TRAIN);
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
   750
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
   751
		// First, clean the pathfinder with our new begin and endpoints
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
   752
		clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info);
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
   753
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
   754
		p->ainew.temp = 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
   755
	}
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
   756
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
   757
	// Start the pathfinder
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
   758
	r = p->ainew.pathfinder->main(p->ainew.pathfinder);
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: 3952
diff changeset
   759
	switch (r) {
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: 3952
diff changeset
   760
		case AYSTAR_NO_PATH:
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
   761
			DEBUG(ai, 1, "No route found by pathfinder");
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: 3952
diff changeset
   762
			// Start all over again
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: 3952
diff changeset
   763
			p->ainew.state = AI_STATE_NOTHING;
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: 3952
diff changeset
   764
			break;
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: 3952
diff changeset
   765
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: 3952
diff changeset
   766
		case AYSTAR_FOUND_END_NODE: // We found the end-point
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: 3952
diff changeset
   767
			p->ainew.temp = -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: 3952
diff changeset
   768
			p->ainew.state = AI_STATE_FIND_DEPOT;
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: 3952
diff changeset
   769
			break;
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: 3952
diff changeset
   770
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: 3952
diff changeset
   771
		// In any other case, we are still busy finding the route
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: 3952
diff changeset
   772
		default: break;
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
   773
	}
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
   774
}
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
   775
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
   776
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
   777
// This function tries to locate a good place for a depot!
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
   778
static void AiNew_State_FindDepot(Player *p)
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
   779
{
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
   780
	// To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there..
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
   781
	// Simple, easy, works!
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
   782
	// To make the depot stand in the middle of the route, we start from the center..
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
   783
	// But first we walk through the route see if we can find a depot that is ours
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
   784
	//  this keeps things nice ;)
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   785
	int g, i;
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
   786
	CommandCost r;
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   787
	DiagDirection j;
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
   788
	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
   789
	assert(p->ainew.state == AI_STATE_FIND_DEPOT);
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
   790
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
   791
	p->ainew.depot_tile = 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
   792
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
   793
	for (i=2;i<p->ainew.path_info.route_length-2;i++) {
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
   794
		tile = p->ainew.path_info.route[i];
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   795
		for (j = DIAGDIR_BEGIN; j < DIAGDIR_END; j++) {
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
   796
			TileIndex t = tile + TileOffsByDiagDir(j);
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   797
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7317
diff changeset
   798
			if (IsTileType(t, MP_ROAD) &&
3793
7fe24e10ea63 (svn r4789) - Codechange: rename RoadType to RoadTileType and ROAD_{NORMAL,CROSSING,DEPOT} to ROAD_TILE_* for consistency
rubidium
parents: 3644
diff changeset
   799
					GetRoadTileType(t) == ROAD_TILE_DEPOT &&
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   800
					IsTileOwner(t, _current_player) &&
3179
8ef3e8028af5 (svn r3816) Use existing accessors
tron
parents: 3157
diff changeset
   801
					GetRoadDepotDirection(t) == ReverseDiagDir(j)) {
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   802
				p->ainew.depot_tile = t;
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   803
				p->ainew.depot_direction = ReverseDiagDir(j);
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   804
				p->ainew.state = AI_STATE_VERIFY_ROUTE;
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   805
				return;
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
   806
			}
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
   807
		}
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
   808
	}
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
   809
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
   810
	// This routine let depot finding start in the middle, and work his way to the stations
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
   811
	// It makes depot placing nicer :)
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
   812
	i = p->ainew.path_info.route_length / 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
   813
	g = 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
   814
	while (i > 1 && i < p->ainew.path_info.route_length - 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
   815
		i += g;
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
   816
		g *= -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
   817
		(g < 0?g--:g++);
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
   818
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
   819
		if (p->ainew.path_info.route_extra[i] != 0 || p->ainew.path_info.route_extra[i+1] != 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
   820
			// Bridge or tunnel.. we can't place a depot there
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
   821
			continue;
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
   822
		}
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
   823
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
   824
		tile = p->ainew.path_info.route[i];
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
   825
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   826
		for (j = DIAGDIR_BEGIN; j < DIAGDIR_END; j++) {
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
   827
			TileIndex t = tile + TileOffsByDiagDir(j);
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   828
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
   829
			// It may not be placed on the road/rail itself
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
   830
			// And because it is not build yet, we can't see it on the 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
   831
			// So check the surrounding tiles :)
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   832
			if (t == p->ainew.path_info.route[i - 1] ||
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   833
					t == p->ainew.path_info.route[i + 1]) {
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
   834
				continue;
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   835
			}
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
   836
			// Not around a bridge?
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
   837
			if (p->ainew.path_info.route_extra[i] != 0) continue;
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
   838
			if (IsTileType(tile, MP_TUNNELBRIDGE)) continue;
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
   839
			// Is the terrain clear?
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   840
			if (IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)) {
3644
7c9a6a91873b (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3577
diff changeset
   841
				// If the current tile is on a slope then we do not allow this
7c9a6a91873b (svn r4553) int and magic numbers -> Slope and DiagDirection
tron
parents: 3577
diff changeset
   842
				if (GetTileSlope(tile, NULL) != SLOPE_FLAT) continue;
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
   843
				// Check if everything went okay..
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   844
				r = AiNew_Build_Depot(p, t, ReverseDiagDir(j), 0);
2737
fb1556b8f5e0 (svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents: 2684
diff changeset
   845
				if (CmdFailed(r)) continue;
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
   846
				// Found a spot!
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   847
				p->ainew.new_cost += r.GetCost();
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   848
				p->ainew.depot_tile = t;
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   849
				p->ainew.depot_direction = ReverseDiagDir(j); // Reverse direction
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
   850
				p->ainew.state = AI_STATE_VERIFY_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
   851
				return;
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
   852
			}
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
   853
		}
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
   854
	}
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
   855
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
   856
	// Failed to find a depot?
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
   857
	p->ainew.state = AI_STATE_NOTHING;
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
   858
}
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
   859
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
   860
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
   861
// This function calculates how many vehicles there are needed on this
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
   862
//  traject.
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
   863
// It works pretty simple: get the length, see how much we move around
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
   864
//  and hussle that, and you know how many vehicles there are needed.
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
   865
// It returns the cost for the vehicles
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
   866
static int AiNew_HowManyVehicles(Player *p)
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
   867
{
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
   868
	if (p->ainew.tbt == AI_BUS) {
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
   869
		// For bus-routes we look at the time before we are back in the station
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   870
		EngineID i;
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   871
		int length, tiles_a_day;
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
   872
		int amount;
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
   873
		i = AiNew_PickVehicle(p);
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   874
		if (i == INVALID_ENGINE) return 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
   875
		// Passenger run.. how long is 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
   876
		length = p->ainew.path_info.route_length;
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
   877
		// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
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
   878
		tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16;
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
   879
		// We want a vehicle in a station once a month at least, so, calculate it!
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
   880
		// (the * 2 is because we have 2 stations ;))
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
   881
		amount = length * 2 * 2 / tiles_a_day / 30;
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
   882
		if (amount == 0) amount = 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
   883
		return amount;
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
   884
	} else if (p->ainew.tbt == AI_TRUCK) {
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
   885
		// For truck-routes we look at the cargo
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   886
		EngineID i;
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   887
		int length, amount, tiles_a_day;
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
   888
		int max_cargo;
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
   889
		i = AiNew_PickVehicle(p);
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
   890
		if (i == INVALID_ENGINE) return 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
   891
		// Passenger run.. how long is 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
   892
		length = p->ainew.path_info.route_length;
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
   893
		// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
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
   894
		tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16;
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: 3952
diff changeset
   895
		if (p->ainew.from_deliver) {
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6794
diff changeset
   896
			max_cargo = GetIndustry(p->ainew.from_ic)->last_month_production[0];
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: 3952
diff changeset
   897
		} else {
6819
f986f54e2660 (svn r10058) -Codechange: give some industry variable sensible names (like not telling "last_mo_production" when it is the production of the current month).
rubidium
parents: 6794
diff changeset
   898
			max_cargo = GetIndustry(p->ainew.to_ic)->last_month_production[0];
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: 3952
diff changeset
   899
		}
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
   900
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
   901
		// This is because moving 60% is more than we can dream of!
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   902
		max_cargo *= 6;
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   903
		max_cargo /= 10;
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
   904
		// We want all the cargo to be gone in a month.. so, we know the cargo it delivers
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
   905
		//  we know what the vehicle takes with him, and we know the time it takes him
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
   906
		//  to get back here.. now let's do some math!
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
   907
		amount = 2 * length * max_cargo / tiles_a_day / 30 / RoadVehInfo(i)->capacity;
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
   908
		amount += 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
   909
		return amount;
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
   910
	} 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
   911
		// Currently not supported
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
   912
		return 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
   913
	}
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
   914
}
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
   915
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
   916
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
   917
// This function checks:
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
   918
//   - If the route went okay
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
   919
//   - Calculates the amount of money needed to build 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
   920
//   - Calculates how much vehicles needed for 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
   921
static void AiNew_State_VerifyRoute(Player *p)
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
   922
{
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
   923
	int res, i;
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
   924
	assert(p->ainew.state == AI_STATE_VERIFY_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
   925
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
   926
	// Let's calculate the cost of the path..
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
   927
	//  new_cost already contains the cost of the stations
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
   928
	p->ainew.path_info.position = -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
   929
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
   930
	do {
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
   931
		p->ainew.path_info.position++;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   932
		p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST).GetCost();
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
   933
	} while (p->ainew.path_info.position != -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
   934
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
   935
	// Now we know the price of build station + path. Now check how many vehicles
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
   936
	//  we need and what the price for that will be
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
   937
	res = AiNew_HowManyVehicles(p);
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
   938
	// If res == 0, no vehicle was found, or an other problem did occour
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
   939
	if (res == 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
   940
		p->ainew.state = AI_STATE_NOTHING;
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
   941
		return;
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
   942
	}
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
   943
	p->ainew.amount_veh = 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
   944
	p->ainew.cur_veh = 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
   945
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
   946
	// Check how much it it going to cost us..
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
   947
	for (i=0;i<res;i++) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   948
		p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST).GetCost();
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
   949
	}
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
   950
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
   951
	// Now we know how much the route is going to cost us
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
   952
	//  Check if we have enough money for it!
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
   953
	if (p->ainew.new_cost > p->player_money - AI_MINIMUM_MONEY) {
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
   954
		// Too bad..
7763
342e5357b592 (svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents: 7758
diff changeset
   955
		DEBUG(ai, 1, "Insufficient funds to build route (%" OTTD_PRINTF64 "d)", (int64)p->ainew.new_cost);
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
   956
		p->ainew.state = AI_STATE_NOTHING;
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
   957
		return;
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
   958
	}
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
   959
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
   960
	// Now we can build the route, check the direction of the stations!
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
   961
	if (p->ainew.from_direction == AI_PATHFINDER_NO_DIRECTION) {
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   962
		p->ainew.from_direction = AiNew_GetDirection(p->ainew.path_info.route[p->ainew.path_info.route_length - 1], p->ainew.path_info.route[p->ainew.path_info.route_length - 2]);
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
   963
	}
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
   964
	if (p->ainew.to_direction == AI_PATHFINDER_NO_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
   965
		p->ainew.to_direction = AiNew_GetDirection(p->ainew.path_info.route[0], p->ainew.path_info.route[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
   966
	}
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
   967
	if (p->ainew.from_tile == AI_STATION_RANGE)
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6259
diff changeset
   968
		p->ainew.from_tile = p->ainew.path_info.route[p->ainew.path_info.route_length - 1];
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
   969
	if (p->ainew.to_tile == AI_STATION_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
   970
		p->ainew.to_tile = p->ainew.path_info.route[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
   971
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
   972
	p->ainew.state = AI_STATE_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
   973
	p->ainew.temp = 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
   974
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
   975
	DEBUG(ai, 1, "The route is set and buildable, building 0x%X to 0x%X...", p->ainew.from_tile, p->ainew.to_tile);
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
   976
}
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
   977
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
   978
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
   979
// Build the stations
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
   980
static void AiNew_State_BuildStation(Player *p)
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
   981
{
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
   982
	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
   983
	assert(p->ainew.state == AI_STATE_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
   984
	if (p->ainew.temp == 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
   985
		if (!IsTileType(p->ainew.from_tile, MP_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
   986
			res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC);
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
   987
	} 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
   988
		if (!IsTileType(p->ainew.to_tile, MP_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
   989
			res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.to_tile, 0, 0, p->ainew.to_direction, DC_EXEC);
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
   990
		p->ainew.state = AI_STATE_BUILD_PATH;
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
   991
	}
2737
fb1556b8f5e0 (svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents: 2684
diff changeset
   992
	if (CmdFailed(res)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
   993
		DEBUG(ai, 0, "[BuildStation] station could not be built (0x%X)", p->ainew.to_tile);
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
   994
		p->ainew.state = AI_STATE_NOTHING;
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
   995
		// If the first station _was_ build, destroy it
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
   996
		if (p->ainew.temp != 0)
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
   997
			AI_DoCommand(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
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
   998
		return;
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
   999
	}
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
  1000
	p->ainew.temp++;
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
  1001
}
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
  1002
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
  1003
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
  1004
// Build the path
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
  1005
static void AiNew_State_BuildPath(Player *p)
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
  1006
{
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
  1007
	assert(p->ainew.state == AI_STATE_BUILD_PATH);
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
  1008
	// p->ainew.temp is set to -1 when this function is called for the first time
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
  1009
	if (p->ainew.temp == -1) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
  1010
		DEBUG(ai, 1, "Starting to build new path");
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
  1011
		// Init the counter
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
  1012
		p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 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
  1013
		// Set the position to the startingplace (-1 because in a minute we do ++)
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
  1014
		p->ainew.path_info.position = -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
  1015
		// And don't do this again
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
  1016
		p->ainew.temp = 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
  1017
	}
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
  1018
	// Building goes very fast on normal rate, so we are going to slow it down..
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
  1019
	//  By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :)
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
  1020
	if (--p->ainew.counter != 0) return;
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
  1021
	p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 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
  1022
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
  1023
	// Increase the building 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
  1024
	p->ainew.path_info.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
  1025
	// Build 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
  1026
	AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_EXEC);
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
  1027
	if (p->ainew.path_info.position == -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
  1028
		// This means we are done building!
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
  1029
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
  1030
		if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
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
  1031
			// If they not queue, they have to go up and down to try again at a 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
  1032
			// We don't want that, so try building some road left or right of the station
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1033
			DiagDirection dir1, dir2, dir3;
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
  1034
			TileIndex tile;
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1035
			CommandCost ret;
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1036
			for (int i = 0; i < 2; 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
  1037
				if (i == 0) {
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  1038
					tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction);
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1039
					dir1 = ChangeDiagDir(p->ainew.from_direction, DIAGDIRDIFF_90LEFT);
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1040
					dir2 = ChangeDiagDir(p->ainew.from_direction, DIAGDIRDIFF_90RIGHT);
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
  1041
					dir3 = p->ainew.from_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
  1042
				} else {
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4527
diff changeset
  1043
					tile = p->ainew.to_tile + TileOffsByDiagDir(p->ainew.to_direction);
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1044
					dir1 = ChangeDiagDir(p->ainew.to_direction, DIAGDIRDIFF_90LEFT);
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1045
					dir2 = ChangeDiagDir(p->ainew.to_direction, DIAGDIRDIFF_90RIGHT);
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
  1046
					dir3 = p->ainew.to_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
  1047
				}
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
  1048
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1049
				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1050
				if (CmdSucceeded(ret)) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1051
					TileIndex offset = TileOffsByDiagDir(dir1);
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1052
					if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, MP_TREES)) {
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1053
						ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1054
						if (CmdSucceeded(ret)) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1055
							if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, MP_TREES))
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1056
								AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | 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
  1057
						}
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
  1058
					}
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
  1059
				}
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
  1060
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1061
				ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1062
				if (CmdSucceeded(ret)) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1063
					TileIndex offset = TileOffsByDiagDir(dir2);
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1064
					if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, MP_TREES)) {
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1065
						ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1066
						if (CmdSucceeded(ret)) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1067
							if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, MP_TREES))
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1068
								AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | 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
  1069
						}
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
  1070
					}
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
  1071
				}
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
  1072
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1073
				ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1074
				if (CmdSucceeded(ret)) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1075
					TileIndex offset = TileOffsByDiagDir(dir3);
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1076
					if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, MP_TREES)) {
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1077
						ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1078
						if (CmdSucceeded(ret)) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1079
							if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, MP_TREES))
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7010
diff changeset
  1080
								AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | 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
  1081
						}
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
  1082
					}
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
  1083
				}
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
  1084
			}
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
  1085
		}
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
  1086
7763
342e5357b592 (svn r11312) -Codechange: implement a overflow safe integer and use that for money and don't misuses CommandCost to have a overflow safe integer. Based on a patch by Noldo.
rubidium
parents: 7758
diff changeset
  1087
		DEBUG(ai, 1, "Finished building path, cost: %" OTTD_PRINTF64 "d", (int64)p->ainew.new_cost);
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
  1088
		p->ainew.state = AI_STATE_BUILD_DEPOT;
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
  1089
	}
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
  1090
}
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
  1091
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
  1092
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
  1093
// Builds the depot
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
  1094
static void AiNew_State_BuildDepot(Player *p)
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
  1095
{
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1096
	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
  1097
	assert(p->ainew.state == AI_STATE_BUILD_DEPOT);
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
  1098
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7317
diff changeset
  1099
	if (IsTileType(p->ainew.depot_tile, MP_ROAD) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) {
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
  1100
		if (IsTileOwner(p->ainew.depot_tile, _current_player)) {
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: 3952
diff changeset
  1101
			// The depot is already 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
  1102
			p->ainew.state = AI_STATE_BUILD_VEHICLE;
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
  1103
			return;
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
  1104
		} 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
  1105
			// There is a depot, but not of our team! :(
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
  1106
			p->ainew.state = AI_STATE_NOTHING;
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
  1107
			return;
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
  1108
		}
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
  1109
	}
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
  1110
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
  1111
	// There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p)
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: 7645
diff changeset
  1112
	if (!EnsureNoVehicleOnGround(p->ainew.depot_tile + TileOffsByDiagDir(p->ainew.depot_direction)))
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
  1113
		return;
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
  1114
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
  1115
	res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC);
2737
fb1556b8f5e0 (svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents: 2684
diff changeset
  1116
	if (CmdFailed(res)) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5247
diff changeset
  1117
		DEBUG(ai, 0, "[BuildDepot] depot could not be built (0x%X)", p->ainew.depot_tile);
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
  1118
		p->ainew.state = AI_STATE_NOTHING;
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
  1119
		return;
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
  1120
	}
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
  1121
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
  1122
	p->ainew.state = AI_STATE_BUILD_VEHICLE;
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1123
	p->ainew.idle = 10;
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
  1124
	p->ainew.veh_main_id = INVALID_VEHICLE;
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
  1125
}
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
  1126
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
  1127
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
  1128
// Build vehicles
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
  1129
static void AiNew_State_BuildVehicle(Player *p)
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
  1130
{
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6924
diff changeset
  1131
	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
  1132
	assert(p->ainew.state == AI_STATE_BUILD_VEHICLE);
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
  1133
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
  1134
	// Check if we need to build a vehicle
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
  1135
	if (p->ainew.amount_veh == 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
  1136
		// Nope, we are done!
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
  1137
		// This means: we are all done! The route is open.. go back to NOTHING
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
  1138
		//  He will idle some time and it will all start over again.. :)
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
  1139
		p->ainew.state = AI_STATE_ACTION_DONE;
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
  1140
		return;
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
  1141
	}
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
  1142
	if (--p->ainew.idle != 0) return;
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
  1143
	// It is realistic that the AI can only build 1 vehicle a day..
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
  1144
	// This makes sure of that!
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
  1145
	p->ainew.idle = AI_BUILD_VEHICLE_TIME_BETWEEN;
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
  1146
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
  1147
	// Build the vehicle
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
  1148
	res = AiNew_Build_Vehicle(p, p->ainew.depot_tile, DC_EXEC);
2737
fb1556b8f5e0 (svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents: 2684
diff changeset
  1149
	if (CmdFailed(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
  1150
		// This happens when the AI can't build any more vehicles!
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
  1151
		p->ainew.state = AI_STATE_NOTHING;
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
  1152
		return;
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
  1153
	}
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
  1154
	// Increase the current counter
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
  1155
	p->ainew.cur_veh++;
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
  1156
	// Decrease the total counter
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
  1157
	p->ainew.amount_veh--;
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
  1158
	// Go give some orders!
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: 3945
diff changeset
  1159
	p->ainew.state = AI_STATE_WAIT_FOR_BUILD;
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
  1160
}
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
  1161
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
  1162
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
  1163
// Put the stations in the order list
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
  1164
static void AiNew_State_GiveOrders(Player *p)
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
  1165
{
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
  1166
	int idx;
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
  1167
	Order order;
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
  1168
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
  1169
	assert(p->ainew.state == AI_STATE_GIVE_ORDERS);
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
  1170
3885
e67faccf52f5 (svn r4941) Replace some ints and magic numbers by proper types and enums
tron
parents: 3793
diff changeset
  1171
	if (p->ainew.veh_main_id != INVALID_VEHICLE) {
8064
fef5cd447a5a (svn r11625) -Codechange: add CO_* enum at some places, add includes of order.h too
smatz
parents: 7763
diff changeset
  1172
		AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), CO_SHARE, DC_EXEC, CMD_CLONE_ORDER);
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1173
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
  1174
		p->ainew.state = AI_STATE_START_VEHICLE;
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
  1175
		return;
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
  1176
	} 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
  1177
		p->ainew.veh_main_id = p->ainew.veh_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
  1178
	}
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
  1179
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1180
	// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1181
	if (_patches.gotodepot) {
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1182
		idx = 0;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1183
		order.type = OT_GOTO_DEPOT;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1184
		order.flags = OF_UNLOAD;
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4389
diff changeset
  1185
		order.dest = GetDepotByTile(p->ainew.depot_tile)->index;
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1186
		AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1187
	}
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1188
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1189
	idx = 0;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1190
	order.type = OT_GOTO_STATION;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1191
	order.flags = 0;
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4389
diff changeset
  1192
	order.dest = GetStationIndex(p->ainew.to_tile);
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1193
	if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver)
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1194
		order.flags |= OF_FULL_LOAD;
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1195
	AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1196
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
  1197
	idx = 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
  1198
	order.type = OT_GOTO_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
  1199
	order.flags = 0;
4527
fa30d19685c2 (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4389
diff changeset
  1200
	order.dest = GetStationIndex(p->ainew.from_tile);
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
  1201
	if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver)
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
  1202
		order.flags |= OF_FULL_LOAD;
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1203
	AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
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
  1204
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
  1205
	// Start the engines!
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
  1206
	p->ainew.state = AI_STATE_START_VEHICLE;
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
  1207
}
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
  1208
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
  1209
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
  1210
// Start the vehicle
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
  1211
static void AiNew_State_StartVehicle(Player *p)
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
  1212
{
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
  1213
	assert(p->ainew.state == AI_STATE_START_VEHICLE);
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
  1214
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1215
	// Skip the first order if it is a second vehicle
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1216
	//  This to make vehicles go different ways..
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1217
	if (p->ainew.cur_veh & 1)
6794
feec0fe8e824 (svn r10033) -Feature [FS#760]: skip to the selected order in the order list when clicking on the "skip" button while pressing CTRL.
rubidium
parents: 6636
diff changeset
  1218
		AI_DoCommand(0, p->ainew.veh_id, 1, DC_EXEC, CMD_SKIP_TO_ORDER);
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1219
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
  1220
	// 3, 2, 1... go! (give START_STOP command ;))
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1221
	AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
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
  1222
	// Try to build an other vehicle (that function will stop building when needed)
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1223
	p->ainew.idle  = 10;
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
  1224
	p->ainew.state = AI_STATE_BUILD_VEHICLE;
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
  1225
}
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
  1226
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
  1227
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
  1228
// Repays money
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
  1229
static void AiNew_State_RepayMoney(Player *p)
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
  1230
{
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1231
	uint i;
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1232
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1233
	for (i = 0; i < AI_LOAN_REPAY; i++) {
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1234
		AI_DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 4000
diff changeset
  1235
	}
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
  1236
	p->ainew.state = AI_STATE_ACTION_DONE;
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
  1237
}
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
  1238
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
  1239
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
  1240
static void AiNew_CheckVehicle(Player *p, Vehicle *v)
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
  1241
{
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
  1242
	// When a vehicle is under the 6 months, we don't check for anything
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
  1243
	if (v->age < 180) return;
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
  1244
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
  1245
	// When a vehicle is older then 1 year, it should make money...
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
  1246
	if (v->age > 360) {
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
  1247
		// If both years together are not more than AI_MINIMUM_ROUTE_PROFIT,
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
  1248
		//  it is not worth the line I guess...
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
  1249
		if (v->profit_last_year + v->profit_this_year < AI_MINIMUM_ROUTE_PROFIT ||
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
  1250
				(v->reliability * 100 >> 16) < 40) {
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
  1251
			// There is a possibility that the route is fucked up...
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6990
diff changeset
  1252
			if (v->cargo.DaysInTransit() > AI_VEHICLE_LOST_DAYS) {
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
  1253
				// The vehicle is lost.. check the route, or else, get the vehicle
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
  1254
				//  back to a depot
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
  1255
				// TODO: make this piece of 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
  1256
			}
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
  1257
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
  1258
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
  1259
			// We are already sending him back
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
  1260
			if (AiNew_GetSpecialVehicleFlag(p, v) & AI_VEHICLEFLAG_SELL) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
  1261
				if (v->type == VEH_ROAD && IsTileDepotType(v->tile, TRANSPORT_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
  1262
						(v->vehstatus&VS_STOPPED)) {
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
  1263
					// We are at the depot, sell the vehicle
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1264
					AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
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
  1265
				}
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
  1266
				return;
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
  1267
			}
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
  1268
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
  1269
			if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return;
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
  1270
			{
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1271
				CommandCost ret;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
  1272
				if (v->type == VEH_ROAD)
2682
7fa4b202b9f0 (svn r3224) -Add: Allow the NewAI to work in Multiplayer Games (switchable via patch
truelight
parents: 2635
diff changeset
  1273
					ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
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
  1274
				// This means we can not find a depot :s
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
  1275
				//				if (CmdFailed(ret))
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
  1276
			}
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
  1277
		}
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
  1278
	}
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
  1279
}
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
  1280
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
  1281
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
  1282
// Checks all vehicles if they are still valid and make money and stuff
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
  1283
static void AiNew_State_CheckAllVehicles(Player *p)
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
  1284
{
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
  1285
	Vehicle *v;
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
  1286
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
  1287
	FOR_ALL_VEHICLES(v) {
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
  1288
		if (v->owner != p->index) continue;
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
  1289
		// Currently, we only know how to handle road-vehicles
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 5656
diff changeset
  1290
		if (v->type != VEH_ROAD) continue;
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
  1291
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
  1292
		AiNew_CheckVehicle(p, v);
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
  1293
	}
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
  1294
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
  1295
	p->ainew.state = AI_STATE_ACTION_DONE;
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
  1296
}
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
  1297
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
  1298
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
  1299
// Using the technique simular to the original AI
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
  1300
//   Keeps things logical
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
  1301
// It really should be in the same order as the AI_STATE's are!
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
  1302
static AiNew_StateFunction* const _ainew_state[] = {
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
  1303
	NULL,
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
  1304
	AiNew_State_FirstTime,
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
  1305
	AiNew_State_Nothing,
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
  1306
	AiNew_State_WakeUp,
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
  1307
	AiNew_State_LocateRoute,
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
  1308
	AiNew_State_FindStation,
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
  1309
	AiNew_State_FindPath,
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
  1310
	AiNew_State_FindDepot,
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
  1311
	AiNew_State_VerifyRoute,
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
  1312
	AiNew_State_BuildStation,
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
  1313
	AiNew_State_BuildPath,
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
  1314
	AiNew_State_BuildDepot,
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
  1315
	AiNew_State_BuildVehicle,
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: 3945
diff changeset
  1316
	NULL,
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
  1317
	AiNew_State_GiveOrders,
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
  1318
	AiNew_State_StartVehicle,
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
  1319
	AiNew_State_RepayMoney,
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
  1320
	AiNew_State_CheckAllVehicles,
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
  1321
	AiNew_State_ActionDone,
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
  1322
	NULL,
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
  1323
};
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
  1324
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
  1325
static void AiNew_OnTick(Player *p)
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
  1326
{
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
  1327
	if (_ainew_state[p->ainew.state] != NULL)
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
  1328
		_ainew_state[p->ainew.state](p);
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
  1329
}
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
  1330
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
  1331
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
  1332
void AiNewDoGameLoop(Player *p)
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
  1333
{
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
  1334
	if (p->ainew.state == AI_STATE_STARTUP) {
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
  1335
		// The AI just got alive!
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
  1336
		p->ainew.state = AI_STATE_FIRST_TIME;
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
  1337
		p->ainew.tick = 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
  1338
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
  1339
		// Only startup the AI
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
  1340
		return;
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
  1341
	}
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
  1342
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
  1343
	// We keep a ticker. We use it for competitor_speed
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
  1344
	p->ainew.tick++;
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
  1345
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
  1346
	// If we come here, we can do a tick.. do so!
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
  1347
	AiNew_OnTick(p);
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
  1348
}