ai_new.c
author tron
Fri, 26 Aug 2005 20:26:34 +0000
changeset 2365 d3f8baf5b10d
parent 2186 461a2aff3486
permissions -rw-r--r--
(svn r2891) Fix indentation
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     3
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     4
 * Next part is in Dutch, and only here for me, TrueLight, the maker of this new AI
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     5
 */
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     6
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     7
// TODO: als iemand een vehicle stil zet op een weg waar de AI wil bouwen
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     8
//         doet de AI helemaal niets meer
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
     9
// TODO: depot rondjes rijden stom iets dingus
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    10
// TODO: jezelf afvragen of competitor_intelligence op niveau 2 wel meer geld moet opleverne...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    11
// TODO: als er iets in path komt, bouwt AI gewoon verder :(
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    12
// TODO: mail routes
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    13
1490
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
    14
// FIXME: This code is horrible. Indisputably from the style POV, at least. --pasky
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
    15
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    16
/*
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    17
 * End of Dutch part
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    18
 */
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    19
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    20
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1767
diff changeset
    21
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1260
diff changeset
    22
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2096
diff changeset
    23
#include "functions.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 374
diff changeset
    24
#include "table/strings.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 568
diff changeset
    25
#include "map.h"
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1035
diff changeset
    26
#include "tile.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    27
#include "command.h"
2096
3043c62401d0 (svn r2606) -Codechange: renamed ai.c to ai_old.c, and ai.h to ai_new.h to make room
truelight
parents: 2049
diff changeset
    28
#include "ai_new.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    29
#include "town.h"
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    30
#include "industry.h"
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    31
#include "station.h"
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    32
#include "engine.h"
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    33
#include "gui.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
    34
#include "depot.h"
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    35
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    36
// This function is called after StartUp. It is the init of an AI
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    37
static void AiNew_State_FirstTime(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    38
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    39
	// This assert is used to protect those function from misuse
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    40
	//   You have quickly a small mistake in the state-array
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    41
	//   With that, everything would go wrong. Finding that, is almost impossible
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    42
	//   With this assert, that problem can never happen.
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    43
	assert(p->ainew.state == AI_STATE_FIRST_TIME);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    44
	// We first have to init some things
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    45
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    46
	if (_current_player == 1) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    47
		ShowErrorMessage(-1, TEMP_AI_IN_PROGRESS, 0, 0);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    48
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    49
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    50
	// The PathFinder (AyStar)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    51
	// TODO: Maybe when an AI goes bankrupt, this is de-init
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    52
	//  or when coming from a savegame.. should be checked out!
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    53
	p->ainew.path_info.start_tile_tl = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    54
	p->ainew.path_info.start_tile_br = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    55
	p->ainew.path_info.end_tile_tl = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    56
	p->ainew.path_info.end_tile_br = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    57
	p->ainew.pathfinder = new_AyStar_AiPathFinder(12, &p->ainew.path_info);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    58
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    59
	p->ainew.idle = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
    60
	p->ainew.last_vehiclecheck_date = _date;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    61
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    62
	// We ALWAYS start with a bus route.. just some basic money ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    63
	p->ainew.action = AI_ACTION_BUS_ROUTE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    64
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    65
	// Let's popup the news, and after that, start building..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    66
	p->ainew.state = AI_STATE_WAKE_UP;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    67
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    68
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    69
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    70
// This function just waste some time
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    71
//  It keeps it more real. The AI can build on such tempo no normal user
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    72
//  can ever keep up with that. The competitor_speed already delays a bit
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    73
//  but after the AI finished a track it really needs to go to sleep.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    74
//
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    75
// Let's say, we sleep between one and three days if the AI is put on Very Fast.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    76
//  This means that on Very Slow it will be between 16 and 48 days.. slow enough?
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    77
static void AiNew_State_Nothing(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    78
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    79
	assert(p->ainew.state == AI_STATE_NOTHING);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    80
	// If we are done idling, start over again
165
f81fa8c27236 (svn r166) -Codechange: change 74 for constant DAY_TICKS
darkvater
parents: 146
diff changeset
    81
	if (p->ainew.idle == 0) p->ainew.idle = RandomRange(DAY_TICKS * 2) + DAY_TICKS;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    82
	if (--p->ainew.idle == 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    83
		// We are done idling.. what you say? Let's do something!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    84
		// I mean.. the next tick ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    85
		p->ainew.state = AI_STATE_WAKE_UP;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    86
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    87
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    88
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    89
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    90
// This function picks out a task we are going to do.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    91
//  Currently supported:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    92
//    - Make new route
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    93
//    - Check route
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
    94
//    - Build HQ
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    95
static void AiNew_State_WakeUp(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
    96
{
1490
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
    97
	int32 money;
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
    98
	int c;
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
    99
	assert(p->ainew.state == AI_STATE_WAKE_UP);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   100
	// First, check if we have a HQ
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   101
	if (p->location_of_house == 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   102
		// We have no HQ yet, build one on a random place
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   103
		// Random till we found a place for it!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   104
		// TODO: this should not be on a random place..
1490
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
   105
		AiNew_Build_CompanyHQ(p, Random() % MapSize());
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   106
		// Enough for now, but we want to come back here the next time
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   107
		//  so we do not change any status
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   108
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   109
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   110
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   111
	money = p->player_money - AI_MINIMUM_MONEY;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   112
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   113
	// Let's pick an action!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   114
	if (p->ainew.action == AI_ACTION_NONE) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   115
		c = Random() & 0xFF;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   116
		if (p->current_loan > 0 &&
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   117
				p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN &&
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   118
				c < 10) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   119
			p->ainew.action = AI_ACTION_REPAY_LOAN;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   120
		} else if (p->ainew.last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   121
			// Check all vehicles once in a while
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   122
			p->ainew.action = AI_ACTION_CHECK_ALL_VEHICLES;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   123
			p->ainew.last_vehiclecheck_date = _date;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   124
		} else if (c < 100 && !_patches.ai_disable_veh_roadveh) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   125
			// Do we have any spots for road-vehicles left open?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   126
			if (GetFreeUnitNumber(VEH_Road) <= _patches.max_roadveh) {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   127
				if (c < 85)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   128
					p->ainew.action = AI_ACTION_TRUCK_ROUTE;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   129
				else
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   130
					p->ainew.action = AI_ACTION_BUS_ROUTE;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   131
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   132
		}/* else if (c < 200 && !_patches.ai_disable_veh_train) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   133
			if (GetFreeUnitNumber(VEH_Train) <= _patches.max_trains) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   134
				p->ainew.action = AI_ACTION_TRAIN_ROUTE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   135
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   136
		}*/
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   137
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   138
		p->ainew.counter = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   139
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   140
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   141
	if (p->ainew.counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   142
		p->ainew.action = AI_ACTION_NONE;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   143
		return;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   144
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   145
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   146
	if (_patches.ai_disable_veh_roadveh && (
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   147
				p->ainew.action == AI_ACTION_BUS_ROUTE ||
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   148
				p->ainew.action == AI_ACTION_TRUCK_ROUTE
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   149
			)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   150
		p->ainew.action = AI_ACTION_NONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   151
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   152
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   153
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   154
	if (_patches.ai_disable_veh_roadveh && (
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   155
				p->ainew.action == AI_ACTION_BUS_ROUTE ||
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   156
				p->ainew.action == AI_ACTION_TRUCK_ROUTE
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   157
			)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   158
		p->ainew.action = AI_ACTION_NONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   159
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   160
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   161
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   162
	if (p->ainew.action == AI_ACTION_REPAY_LOAN &&
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   163
			money > AI_MINIMUM_LOAN_REPAY_MONEY) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   164
		// We start repaying some money..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   165
		p->ainew.state = AI_STATE_REPAY_MONEY;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   166
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   167
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   168
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   169
	if (p->ainew.action == AI_ACTION_CHECK_ALL_VEHICLES) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   170
		p->ainew.state = AI_STATE_CHECK_ALL_VEHICLES;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   171
		return;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   172
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   173
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   174
	// It is useless to start finding a route if we don't have enough money
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   175
	//  to build the route anyway..
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   176
	if (p->ainew.action == AI_ACTION_BUS_ROUTE &&
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   177
			money > AI_MINIMUM_BUS_ROUTE_MONEY) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   178
		if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   179
			p->ainew.action = AI_ACTION_NONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   180
			return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   181
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   182
		p->ainew.cargo = AI_NEED_CARGO;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   183
		p->ainew.state = AI_STATE_LOCATE_ROUTE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   184
		p->ainew.tbt = AI_BUS; // Bus-route
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   185
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   186
	}
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   187
	if (p->ainew.action == AI_ACTION_TRUCK_ROUTE &&
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   188
			money > AI_MINIMUM_TRUCK_ROUTE_MONEY) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   189
		if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   190
			p->ainew.action = AI_ACTION_NONE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   191
			return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   192
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   193
		p->ainew.cargo = AI_NEED_CARGO;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   194
		p->ainew.last_id = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   195
		p->ainew.state = AI_STATE_LOCATE_ROUTE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   196
		p->ainew.tbt = AI_TRUCK;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   197
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   198
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   199
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   200
	p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   201
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   202
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   203
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   204
static void AiNew_State_ActionDone(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   205
{
1490
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
   206
	p->ainew.action = AI_ACTION_NONE;
6a13118e99f5 (svn r1994) Fix: 1159456 - hopefully prevent the new AI to get stuck in an infinite loop forlorny trying to build its haven. Unintentionally bundled with trivial tidyup.
pasky
parents: 1377
diff changeset
   207
	p->ainew.state = AI_STATE_NOTHING;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   208
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   209
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   210
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   211
// Check if a city or industry is good enough to start a route there
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   212
static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   213
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   214
	if (type == AI_CITY) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 900
diff changeset
   215
		Town *t = GetTown(ic);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   216
		Station *st;
1377
2a418162176e (svn r1881) -Fix: [ 1119308 ] Max passengers / mail variables are now 32 bit
celestar
parents: 1330
diff changeset
   217
		uint count = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   218
		int j = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   219
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   220
		// We don't like roadconstructions, don't even true such a city
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   221
		if (t->road_build_months != 0) return false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   222
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   223
		// Check if the rating in a city is high enough
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   224
		//  If not, take a chance if we want to continue
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   225
		if (t->ratings[_current_player] < 0 && CHANCE16(1,4)) return false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   226
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   227
		if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !CHANCE16(1,AI_CHECKCITY_CITY_CHANCE)) return false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   228
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   229
		// Check if we have build a station in this town the last 6 months
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   230
		//  else we don't do it. This is done, because stat updates can be slow
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   231
		//  and sometimes it takes up to 4 months before the stats are corectly.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   232
		//  This way we don't get 12 busstations in one city of 100 population ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   233
		FOR_ALL_STATIONS(st) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   234
			// Is it an active station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   235
			if (st->xy == 0) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   236
			// Do we own it?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   237
			if (st->owner == _current_player) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   238
				// Are we talking busses?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   239
				if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) != FACIL_BUS_STOP) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   240
				// Is it the same city as we are in now?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   241
				if (st->town != t) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   242
				// When was this station build?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   243
				if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   244
				// Cound the amount of stations in this city that we own
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   245
				count++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   246
			} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   247
				// We do not own it, request some info about the station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   248
				//  we want to know if this station gets the same good. If so,
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   249
				//  we want to know its rating. If it is too high, we are not going
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   250
				//  to build there
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   251
				if (!st->goods[CT_PASSENGERS].last_speed) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   252
				// Is it around our city
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   253
				if (DistanceManhattan(st->xy, t->xy) > 10) continue;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   254
				// It does take this cargo.. what is his rating?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   255
				if (st->goods[CT_PASSENGERS].rating < AI_CHECKCITY_CARGO_RATING) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   256
				j++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   257
				// When this is the first station, we build a second with no problem ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   258
				if (j == 1) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   259
				// The rating is high.. second station...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   260
				//  a little chance that we still continue
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   261
				//  But if there are 3 stations of this size, we never go on...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   262
				if (j == 2 && CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   263
				// We don't like this station :(
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   264
				return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   265
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   266
		}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   267
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   268
		// We are about to add one...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   269
		count++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   270
		// Check if we the city can provide enough cargo for this amount of stations..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   271
		if (count * AI_CHECKCITY_CARGO_PER_STATION > t->max_pass) return false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   272
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   273
		// All check are okay, so we can build here!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   274
		return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   275
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   276
	if (type == AI_INDUSTRY) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 900
diff changeset
   277
		Industry *i = GetIndustry(ic);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   278
		Station *st;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   279
		int count = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   280
		int j = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   281
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   282
		if (i->town != NULL && i->town->ratings[_current_player] < 0 && CHANCE16(1,4)) return false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   283
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   284
		// No limits on delevering stations!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   285
		//  Or for industry that does not give anything yet
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   286
		if (i->produced_cargo[0] == 0xFF || i->total_production[0] == 0) return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   287
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   288
		if (i->total_production[0] - i->total_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   289
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   290
		// Check if we have build a station in this town the last 6 months
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   291
		//  else we don't do it. This is done, because stat updates can be slow
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   292
		//  and sometimes it takes up to 4 months before the stats are corectly.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   293
		FOR_ALL_STATIONS(st) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   294
			// Is it an active station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   295
			if (st->xy == 0) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   296
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   297
			// Do we own it?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   298
			if (st->owner == _current_player) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   299
				// Are we talking trucks?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   300
				if (p->ainew.tbt == AI_TRUCK && (FACIL_TRUCK_STOP & st->facilities) != FACIL_TRUCK_STOP) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   301
				// Is it the same city as we are in now?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   302
				if (st->town != i->town) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   303
				// When was this station build?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   304
				if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   305
				// Cound the amount of stations in this city that we own
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   306
				count++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   307
			} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   308
				// We do not own it, request some info about the station
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   309
				//  we want to know if this station gets the same good. If so,
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   310
				//  we want to know its rating. If it is too high, we are not going
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   311
				//  to build there
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   312
				if (i->produced_cargo[0] == 0xFF) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   313
				// It does not take this cargo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   314
				if (!st->goods[i->produced_cargo[0]].last_speed) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   315
				// Is it around our industry
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1209
diff changeset
   316
				if (DistanceManhattan(st->xy, i->xy) > 5) continue;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   317
				// It does take this cargo.. what is his rating?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   318
				if (st->goods[i->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   319
				j++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   320
				// The rating is high.. a little chance that we still continue
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   321
				//  But if there are 2 stations of this size, we never go on...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   322
				if (j == 1 && CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   323
				// We don't like this station :(
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   324
				return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   325
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   326
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   327
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   328
		// We are about to add one...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   329
		count++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   330
		// Check if we the city can provide enough cargo for this amount of stations..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   331
		if (count * AI_CHECKCITY_CARGO_PER_STATION > i->total_production[0]) return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   332
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   333
		// All check are okay, so we can build here!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   334
		return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   335
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   336
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   337
	return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   338
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   339
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   340
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   341
// This functions tries to locate a good route
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   342
static void AiNew_State_LocateRoute(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   343
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   344
	assert(p->ainew.state == AI_STATE_LOCATE_ROUTE);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   345
	// For now, we only support PASSENGERS, CITY and BUSSES
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   346
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   347
	// We don't have a route yet
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   348
	if (p->ainew.cargo == AI_NEED_CARGO) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   349
		p->ainew.new_cost = 0; // No cost yet
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   350
		p->ainew.temp = -1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   351
		// Reset the counter
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   352
		p->ainew.counter = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   353
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   354
		p->ainew.from_ic = -1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   355
		p->ainew.to_ic = -1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   356
		if (p->ainew.tbt == AI_BUS) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   357
			// For now we only have a passenger route
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   358
			p->ainew.cargo = CT_PASSENGERS;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   359
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   360
			// Find a route to cities
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   361
			p->ainew.from_type = AI_CITY;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   362
			p->ainew.to_type = AI_CITY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   363
		} else if (p->ainew.tbt == AI_TRUCK) {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   364
			p->ainew.cargo = AI_NO_CARGO;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   365
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   366
			p->ainew.from_type = AI_INDUSTRY;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   367
			p->ainew.to_type = AI_INDUSTRY;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   368
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   369
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   370
		// Now we are doing initing, we wait one tick
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   371
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   372
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   373
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   374
	// Increase the counter and abort if it is taking too long!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   375
	p->ainew.counter++;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   376
	if (p->ainew.counter > AI_LOCATE_ROUTE_MAX_COUNTER) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   377
		// Switch back to doing nothing!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   378
		p->ainew.state = AI_STATE_NOTHING;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   379
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   380
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   381
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   382
	// We are going to locate a city from where we are going to connect
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   383
	if (p->ainew.from_ic == -1) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   384
		if (p->ainew.temp == -1) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   385
			// First, we pick a random spot to search from
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   386
			if (p->ainew.from_type == AI_CITY)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   387
				p->ainew.temp = RandomRange(_total_towns);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   388
			else
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   389
				p->ainew.temp = RandomRange(_total_industries);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   390
		}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   391
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   392
		if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   393
			// It was not a valid city
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   394
			//  increase the temp with one, and return. We will come back later here
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   395
			//  to try again
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   396
			p->ainew.temp++;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   397
			if (p->ainew.from_type == AI_CITY) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   398
				if (p->ainew.temp >= (int)_total_towns) p->ainew.temp = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   399
			} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   400
				if (p->ainew.temp >= _total_industries) p->ainew.temp = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   401
			}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   402
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   403
			// Don't do an attempt if we are trying the same id as the last time...
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   404
			if (p->ainew.last_id == p->ainew.temp) return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   405
			p->ainew.last_id = p->ainew.temp;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   406
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   407
			return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   408
		}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   409
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   410
		// We found a good city/industry, save the data of it
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   411
		p->ainew.from_ic = p->ainew.temp;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   412
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   413
		// Start the next tick with finding a to-city
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   414
		p->ainew.temp = -1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   415
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   416
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   417
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   418
	// Find a to-city
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   419
	if (p->ainew.temp == -1) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   420
		// First, we pick a random spot to search to
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   421
		if (p->ainew.to_type == AI_CITY)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   422
			p->ainew.temp = RandomRange(_total_towns);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   423
		else
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   424
			p->ainew.temp = RandomRange(_total_industries);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   425
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   426
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   427
	// The same city is not allowed
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   428
	// Also check if the city is valid
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   429
	if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_type)) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   430
		// Maybe it is valid..
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   431
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   432
		// We need to know if they are not to far apart from eachother..
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   433
		// We do that by checking how much cargo we have to move and how long the route
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   434
		//   is.
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   435
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   436
		if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   437
			int max_cargo = GetTown(p->ainew.from_ic)->max_pass + GetTown(p->ainew.temp)->max_pass;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   438
			max_cargo -= GetTown(p->ainew.from_ic)->act_pass + GetTown(p->ainew.temp)->act_pass;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   439
			// max_cargo is now the amount of cargo we can move between the two cities
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   440
			// If it is more than the distance, we allow it
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   441
			if (DistanceManhattan(GetTown(p->ainew.from_ic)->xy, GetTown(p->ainew.temp)->xy) <= max_cargo * AI_LOCATEROUTE_BUS_CARGO_DISTANCE) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   442
				// We found a good city/industry, save the data of it
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   443
				p->ainew.to_ic = p->ainew.temp;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   444
				p->ainew.state = AI_STATE_FIND_STATION;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   445
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   446
				DEBUG(ai,1)(
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   447
					"[AiNew - LocateRoute] Found bus-route of %d tiles long (from %d to %d)",
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   448
					DistanceManhattan(GetTown(p->ainew.from_ic)->xy, GetTown(p->ainew.temp)->xy),
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   449
					p->ainew.from_ic,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   450
					p->ainew.temp
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   451
				);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   452
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   453
				p->ainew.from_tile = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   454
				p->ainew.to_tile = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   455
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   456
				return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   457
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   458
		} else if (p->ainew.tbt == AI_TRUCK) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   459
			bool found = false;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   460
			int max_cargo = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   461
			int i;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   462
			// TODO: in max_cargo, also check other cargo (beside [0])
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   463
			// First we check if the from_ic produces cargo that this ic accepts
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   464
			if (GetIndustry(p->ainew.from_ic)->produced_cargo[0] != 0xFF && GetIndustry(p->ainew.from_ic)->total_production[0] != 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   465
				for (i=0;i<3;i++) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   466
					if (GetIndustry(p->ainew.temp)->accepts_cargo[i] == 0xFF) break;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 900
diff changeset
   467
					if (GetIndustry(p->ainew.from_ic)->produced_cargo[0] == GetIndustry(p->ainew.temp)->accepts_cargo[i]) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   468
						// Found a compatbiel industry
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 900
diff changeset
   469
						max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0] - GetIndustry(p->ainew.from_ic)->total_transported[0];
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   470
						found = true;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   471
						p->ainew.from_deliver = true;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   472
						p->ainew.to_deliver = false;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   473
						break;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   474
					}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   475
				}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   476
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   477
			if (!found && GetIndustry(p->ainew.temp)->produced_cargo[0] != 0xFF && GetIndustry(p->ainew.temp)->total_production[0] != 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   478
				// If not check if the current ic produces cargo that the from_ic accepts
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   479
				for (i=0;i<3;i++) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   480
					if (GetIndustry(p->ainew.from_ic)->accepts_cargo[i] == 0xFF) break;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 900
diff changeset
   481
					if (GetIndustry(p->ainew.temp)->produced_cargo[0] == GetIndustry(p->ainew.from_ic)->accepts_cargo[i]) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   482
						// Found a compatbiel industry
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   483
						found = true;
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 900
diff changeset
   484
						max_cargo = GetIndustry(p->ainew.temp)->total_production[0] - GetIndustry(p->ainew.from_ic)->total_transported[0];
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   485
						p->ainew.from_deliver = false;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   486
						p->ainew.to_deliver = true;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   487
						break;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   488
					}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   489
				}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   490
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   491
			if (found) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   492
				// Yeah, they are compatible!!!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   493
				// Check the length against the amount of goods
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   494
				if (DistanceManhattan(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy) > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE &&
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   495
						DistanceManhattan(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy) <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   496
					p->ainew.to_ic = p->ainew.temp;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   497
					if (p->ainew.from_deliver) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   498
						p->ainew.cargo = GetIndustry(p->ainew.from_ic)->produced_cargo[0];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   499
					} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   500
						p->ainew.cargo = GetIndustry(p->ainew.temp)->produced_cargo[0];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   501
					}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   502
					p->ainew.state = AI_STATE_FIND_STATION;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   503
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   504
					DEBUG(ai,1)(
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   505
						"[AiNew - LocateRoute] Found truck-route of %d tiles long (from %d to %d)",
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   506
						DistanceManhattan(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy),
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   507
						p->ainew.from_ic,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   508
						p->ainew.temp
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   509
					);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   510
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   511
					p->ainew.from_tile = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   512
					p->ainew.to_tile = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   513
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   514
					return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   515
				}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   516
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   517
		}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   518
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   519
1260
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   520
	// It was not a valid city
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   521
	//  increase the temp with one, and return. We will come back later here
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   522
	//  to try again
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   523
	p->ainew.temp++;
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   524
	if (p->ainew.to_type == AI_CITY) {
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   525
		if (p->ainew.temp >= (int)_total_towns) p->ainew.temp = 0;
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   526
	} else {
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   527
		if (p->ainew.temp >= _total_industries) p->ainew.temp = 0;
c60e76928e5c (svn r1764) -Add: dynamic towns, you can now have up to 64k towns (let me know when
truelight
parents: 1253
diff changeset
   528
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   529
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   530
	// Don't do an attempt if we are trying the same id as the last time...
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   531
	if (p->ainew.last_id == p->ainew.temp) return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   532
	p->ainew.last_id = p->ainew.temp;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   533
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   534
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   535
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
   536
// Check if there are not more than a certain amount of vehicles pointed to a certain
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   537
//  station. This to prevent 10 busses going to one station, which gives... problems ;)
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   538
static bool AiNew_CheckVehicleStation(Player *p, Station *st)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   539
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   540
	int count = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   541
	Vehicle *v;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   542
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   543
	// Also check if we don't have already a lot of busses to this city...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   544
	FOR_ALL_VEHICLES(v) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   545
		if (v->owner == _current_player) {
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   546
			const Order *order;
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   547
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   548
			FOR_VEHICLE_ORDERS(v, order) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   549
				if (order->type == OT_GOTO_STATION && GetStation(order->station) == st) {
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   550
					// This vehicle has this city in its list
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   551
					count++;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   552
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   553
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   554
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   555
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   556
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   557
	if (count > AI_CHECK_MAX_VEHICLE_PER_STATION) return false;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   558
	return true;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   559
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   560
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   561
// This function finds a good spot for a station
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   562
static void AiNew_State_FindStation(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   563
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   564
	TileIndex tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   565
	Station *st;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   566
	int i, count = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   567
	TileIndex new_tile = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   568
	byte direction = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   569
	Town *town = NULL;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   570
	Industry *industry = NULL;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   571
	assert(p->ainew.state == AI_STATE_FIND_STATION);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   572
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   573
	if (p->ainew.from_tile == 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   574
		// First we scan for a station in the from-city
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   575
		if (p->ainew.from_type == AI_CITY) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   576
			town = GetTown(p->ainew.from_ic);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   577
			tile = town->xy;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   578
		} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   579
			industry = GetIndustry(p->ainew.from_ic);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   580
			tile = industry->xy;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   581
		}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   582
	} else if (p->ainew.to_tile == 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   583
		// Second we scan for a station in the to-city
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   584
		if (p->ainew.to_type == AI_CITY) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   585
			town = GetTown(p->ainew.to_ic);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   586
			tile = town->xy;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   587
		} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   588
			industry = GetIndustry(p->ainew.to_ic);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   589
			tile = industry->xy;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   590
		}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   591
	} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   592
		// Unsupported request
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   593
		// Go to FIND_PATH
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   594
		p->ainew.temp = -1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   595
		p->ainew.state = AI_STATE_FIND_PATH;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   596
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   597
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   598
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   599
	// First, we are going to look at the stations that already exist inside the city
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   600
	//  If there is enough cargo left in the station, we take that station
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   601
	//  If that is not possible, and there are more than 2 stations in the city, abort
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   602
	i = AiNew_PickVehicle(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   603
	// Euhmz, this should not happen _EVER_
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   604
	// Quit finding a route...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   605
	if (i == -1) { p->ainew.state = AI_STATE_NOTHING; return; }
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   606
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   607
	FOR_ALL_STATIONS(st) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   608
		if (st->xy != 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   609
			if (st->owner == _current_player) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   610
				if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) == FACIL_BUS_STOP) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   611
					if (st->town == town) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   612
						// Check how much cargo there is left in the station
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 507
diff changeset
   613
						if ((st->goods[p->ainew.cargo].waiting_acceptance & 0xFFF) > RoadVehInfo(i)->capacity * AI_STATION_REUSE_MULTIPLER) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   614
							if (AiNew_CheckVehicleStation(p, st)) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   615
								// We did found a station that was good enough!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   616
								new_tile = st->xy;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   617
								// Cheap way to get the direction of the station...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   618
								//  Bus stations save it as 0x47 .. 0x4A, so decrease it with 0x47, and tada!
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
   619
								direction = _m[st->xy].m5 - 0x47;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   620
								break;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   621
							}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   622
						}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   623
						count++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   624
					}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   625
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   626
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   627
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   628
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   629
	// We are going to add a new station...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   630
	if (new_tile == 0) count++;
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
   631
	// No more than 2 stations allowed in a city
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   632
	//  This is because only the best 2 stations of one cargo do get any cargo
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   633
	if (count > 2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   634
		p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   635
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   636
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   637
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   638
	if (new_tile == 0 && p->ainew.tbt == AI_BUS) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   639
		uint x, y, i = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   640
		int r;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   641
		uint best;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   642
		uint accepts[NUM_CARGO];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   643
		TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   644
		uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   645
		// To find a good spot we scan a range from the center, a get the point
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   646
		//  where we get the most cargo and where it is buildable.
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   647
		// TODO: also check for station of myself and make sure we are not
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   648
		//   taking eachothers passangers away (bad result when it does not)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   649
		for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   650
			for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   651
				new_tile = TileXY(x, y);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   652
				if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   653
					// This tile we can build on!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   654
					// Check acceptance
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   655
					// XXX - Get the catchment area
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   656
					GetAcceptanceAroundTiles(accepts, new_tile, 1, 1, 4);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   657
					// >> 3 == 0 means no cargo
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   658
					if (accepts[p->ainew.cargo] >> 3 == 0) continue;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   659
					// See if we can build the station
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   660
					r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   661
					if (r == CMD_ERROR) continue;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   662
					// We can build it, so add it to found_spot
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   663
					found_spot[i] = new_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   664
					found_best[i++] = accepts[p->ainew.cargo];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   665
				}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   666
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   667
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   668
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   669
		// If i is still zero, we did not found anything :(
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   670
		if (i == 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   671
			p->ainew.state = AI_STATE_NOTHING;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   672
			return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   673
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   674
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   675
		// Go through all the found_best and check which has the highest value
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   676
		best = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   677
		new_tile = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   678
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   679
		for (x=0;x<i;x++) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   680
			if (found_best[x] > best ||
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   681
					(found_best[x] == best && DistanceManhattan(tile, new_tile) > DistanceManhattan(tile, found_spot[x]))) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   682
				new_tile = found_spot[x];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   683
				best = found_best[x];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   684
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   685
		}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   686
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   687
		// See how much it is going to cost us...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   688
		r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   689
		p->ainew.new_cost += r;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   690
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   691
		direction = AI_PATHFINDER_NO_DIRECTION;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   692
	} else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   693
		// Truck station locater works differently.. a station can be on any place
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   694
		//  as long as it is in range. So we give back code AI_STATION_RANGE
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   695
		//  so the pathfinder routine can work it out!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   696
		new_tile = AI_STATION_RANGE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   697
		direction = AI_PATHFINDER_NO_DIRECTION;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   698
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   699
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   700
	if (p->ainew.from_tile == 0) {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   701
		p->ainew.from_tile = new_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   702
		p->ainew.from_direction = direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   703
		// Now we found thisone, go in for to_tile
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   704
		return;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   705
	} else if (p->ainew.to_tile == 0) {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   706
		p->ainew.to_tile = new_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   707
		p->ainew.to_direction = direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   708
		// K, done placing stations!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   709
		p->ainew.temp = -1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   710
		p->ainew.state = AI_STATE_FIND_PATH;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   711
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   712
	}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   713
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   714
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   715
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   716
// We try to find a path between 2 points
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   717
static void AiNew_State_FindPath(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   718
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   719
	int r;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   720
	assert(p->ainew.state == AI_STATE_FIND_PATH);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   721
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   722
	// First time, init some data
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   723
	if (p->ainew.temp == -1) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   724
		// Init path_info
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   725
		if (p->ainew.from_tile == AI_STATION_RANGE) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   726
			// For truck routes we take a range around the industry
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   727
			p->ainew.path_info.start_tile_tl = GetIndustry(p->ainew.from_ic)->xy - TileDiffXY(1, 1);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   728
			p->ainew.path_info.start_tile_br = GetIndustry(p->ainew.from_ic)->xy + TileDiffXY(GetIndustry(p->ainew.from_ic)->width, GetIndustry(p->ainew.from_ic)->height) + TileDiffXY(1, 1);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   729
			p->ainew.path_info.start_direction = p->ainew.from_direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   730
		} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   731
			p->ainew.path_info.start_tile_tl = p->ainew.from_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   732
			p->ainew.path_info.start_tile_br = p->ainew.from_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   733
			p->ainew.path_info.start_direction = p->ainew.from_direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   734
		}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   735
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   736
		if (p->ainew.to_tile == AI_STATION_RANGE) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   737
			p->ainew.path_info.end_tile_tl = GetIndustry(p->ainew.to_ic)->xy - TileDiffXY(1, 1);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   738
			p->ainew.path_info.end_tile_br = GetIndustry(p->ainew.to_ic)->xy + TileDiffXY(GetIndustry(p->ainew.to_ic)->width, GetIndustry(p->ainew.to_ic)->height) + TileDiffXY(1, 1);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   739
			p->ainew.path_info.end_direction = p->ainew.to_direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   740
		} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   741
			p->ainew.path_info.end_tile_tl = p->ainew.to_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   742
			p->ainew.path_info.end_tile_br = p->ainew.to_tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   743
			p->ainew.path_info.end_direction = p->ainew.to_direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   744
		}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   745
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   746
		if (p->ainew.tbt == AI_TRAIN)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   747
			p->ainew.path_info.rail_or_road = true;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   748
		else
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   749
			p->ainew.path_info.rail_or_road = false;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   750
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   751
		// First, clean the pathfinder with our new begin and endpoints
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   752
		clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   753
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   754
		p->ainew.temp = 0;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   755
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   756
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   757
	// Start the pathfinder
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   758
	r = p->ainew.pathfinder->main(p->ainew.pathfinder);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   759
	// If it return: no match, stop it...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   760
	if (r == AYSTAR_NO_PATH) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   761
		DEBUG(ai,1)("[AiNew] PathFinder found no route!");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   762
		// Start all over again...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   763
		p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   764
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   765
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   766
	if (r == AYSTAR_FOUND_END_NODE) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   767
		// We found the end-point
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   768
		p->ainew.temp = -1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   769
		p->ainew.state = AI_STATE_FIND_DEPOT;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   770
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   771
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   772
	// In any other case, we are still busy finding the route...
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   773
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   774
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   775
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   776
// This function tries to locate a good place for a depot!
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   777
static void AiNew_State_FindDepot(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   778
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   779
	// To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there..
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   780
	// Simple, easy, works!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   781
	// To make the depot stand in the middle of the route, we start from the center..
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   782
	// But first we walk through the route see if we can find a depot that is ours
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   783
	//  this keeps things nice ;)
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   784
	int g, i, j, r;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   785
	TileIndex tile;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   786
	assert(p->ainew.state == AI_STATE_FIND_DEPOT);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   787
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   788
	p->ainew.depot_tile = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   789
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   790
	for (i=2;i<p->ainew.path_info.route_length-2;i++) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   791
		tile = p->ainew.path_info.route[i];
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
   792
		for (j = 0; j < 4; j++) {
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
   793
			if (IsTileType(tile + TileOffsByDir(j), MP_STREET)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   794
				// Its a street, test if it is a depot
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
   795
				if (_m[tile + TileOffsByDir(j)].m5 & 0x20) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   796
					// We found a depot, is it ours? (TELL ME!!!)
1901
fb05044cf5c3 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1891
diff changeset
   797
					if (IsTileOwner(tile + TileOffsByDir(j), _current_player)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   798
						// Now, is it pointing to the right direction.........
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
   799
						if ((_m[tile + TileOffsByDir(j)].m5 & 3) == (j ^ 2)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   800
							// Yeah!!!
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
   801
							p->ainew.depot_tile = tile + TileOffsByDir(j);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   802
							p->ainew.depot_direction = j ^ 2; // Reverse direction
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   803
							p->ainew.state = AI_STATE_VERIFY_ROUTE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   804
							return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   805
						}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   806
					}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   807
				}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   808
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   809
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   810
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   811
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   812
	// This routine let depot finding start in the middle, and work his way to the stations
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   813
	// It makes depot placing nicer :)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   814
	i = p->ainew.path_info.route_length / 2;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   815
	g = 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   816
	while (i > 1 && i < p->ainew.path_info.route_length - 2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   817
		i += g;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   818
		g *= -1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   819
		(g < 0?g--:g++);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   820
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   821
		if (p->ainew.path_info.route_extra[i] != 0 || p->ainew.path_info.route_extra[i+1] != 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   822
			// Bridge or tunnel.. we can't place a depot there
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   823
			continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   824
		}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   825
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   826
		tile = p->ainew.path_info.route[i];
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   827
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
   828
		for (j = 0; j < 4; j++) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   829
			// It may not be placed on the road/rail itself
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   830
			// And because it is not build yet, we can't see it on the tile..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   831
			// So check the surrounding tiles :)
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
   832
			if (tile + TileOffsByDir(j) == p->ainew.path_info.route[i-1] ||
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   833
					tile + TileOffsByDir(j) == p->ainew.path_info.route[i+1])
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   834
				continue;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   835
			// Not around a bridge?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   836
			if (p->ainew.path_info.route_extra[i] != 0) continue;
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
   837
			if (IsTileType(tile, MP_TUNNELBRIDGE)) continue;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   838
			// Is the terrain clear?
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
   839
			if (IsTileType(tile + TileOffsByDir(j), MP_CLEAR) ||
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
   840
					IsTileType(tile + TileOffsByDir(j), MP_TREES)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   841
				TileInfo ti;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   842
				FindLandscapeHeightByTile(&ti, tile);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   843
				// If the current tile is on a slope (tileh != 0) then we do not allow this
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   844
				if (ti.tileh != 0) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   845
				// Check if everything went okay..
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
   846
				r = AiNew_Build_Depot(p, tile + TileOffsByDir(j), j ^ 2, 0);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   847
				if (r == CMD_ERROR) continue;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   848
				// Found a spot!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   849
				p->ainew.new_cost += r;
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
   850
				p->ainew.depot_tile = tile + TileOffsByDir(j);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   851
				p->ainew.depot_direction = j ^ 2; // Reverse direction
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   852
				p->ainew.state = AI_STATE_VERIFY_ROUTE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   853
				return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   854
			}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   855
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   856
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   857
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   858
	// Failed to find a depot?
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   859
	p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   860
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   861
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   862
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   863
// This function calculates how many vehicles there are needed on this
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   864
//  traject.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   865
// It works pretty simple: get the length, see how much we move around
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   866
//  and hussle that, and you know how many vehicles there are needed.
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   867
// It returns the cost for the vehicles
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   868
static int AiNew_HowManyVehicles(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   869
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   870
	if (p->ainew.tbt == AI_BUS) {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   871
		// For bus-routes we look at the time before we are back in the station
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   872
		int i, length, tiles_a_day;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   873
		int amount;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   874
		i = AiNew_PickVehicle(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   875
		if (i == -1) return 0;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   876
		// Passenger run.. how long is the route?
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   877
		length = p->ainew.path_info.route_length;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   878
		// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   879
		tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   880
		// We want a vehicle in a station once a month at least, so, calculate it!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   881
		// (the * 2 is because we have 2 stations ;))
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   882
		amount = length * 2 * 2 / tiles_a_day / 30;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   883
		if (amount == 0) amount = 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   884
		return amount;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   885
	} else if (p->ainew.tbt == AI_TRUCK) {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   886
		// For truck-routes we look at the cargo
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   887
		int i, length, amount, tiles_a_day;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   888
		int max_cargo;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   889
		i = AiNew_PickVehicle(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   890
		if (i == -1) return 0;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   891
		// Passenger run.. how long is the route?
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   892
		length = p->ainew.path_info.route_length;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   893
		// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   894
		tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   895
		if (p->ainew.from_deliver)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   896
			max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0];
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   897
		else
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   898
			max_cargo = GetIndustry(p->ainew.to_ic)->total_production[0];
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   899
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   900
		// This is because moving 60% is more than we can dream of!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   901
		max_cargo *= 0.6;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   902
		// We want all the cargo to be gone in a month.. so, we know the cargo it delivers
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   903
		//  we know what the vehicle takes with him, and we know the time it takes him
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   904
		//  to get back here.. now let's do some math!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   905
		amount = 2 * length * max_cargo / tiles_a_day / 30 / RoadVehInfo(i)->capacity;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   906
		amount += 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   907
		return amount;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   908
	} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   909
		// Currently not supported
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   910
		return 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   911
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   912
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   913
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   914
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   915
// This function checks:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   916
//   - If the route went okay
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   917
//   - Calculates the amount of money needed to build the route
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   918
//   - Calculates how much vehicles needed for the route
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   919
static void AiNew_State_VerifyRoute(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   920
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   921
	int res, i;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   922
	assert(p->ainew.state == AI_STATE_VERIFY_ROUTE);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   923
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   924
	// Let's calculate the cost of the path..
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   925
	//  new_cost already contains the cost of the stations
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   926
	p->ainew.path_info.position = -1;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   927
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   928
	do {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   929
		p->ainew.path_info.position++;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   930
		p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   931
	} while (p->ainew.path_info.position != -2);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   932
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   933
	// Now we know the price of build station + path. Now check how many vehicles
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   934
	//  we need and what the price for that will be
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   935
	res = AiNew_HowManyVehicles(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   936
	// If res == 0, no vehicle was found, or an other problem did occour
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   937
	if (res == 0) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   938
		p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   939
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   940
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   941
	p->ainew.amount_veh = res;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   942
	p->ainew.cur_veh = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   943
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   944
	// Check how much it it going to cost us..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   945
	for (i=0;i<res;i++) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   946
		p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   947
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   948
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   949
	// Now we know how much the route is going to cost us
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   950
	//  Check if we have enough money for it!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   951
	if (p->ainew.new_cost > p->player_money - AI_MINIMUM_MONEY) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   952
		// Too bad..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   953
		DEBUG(ai,1)("[AiNew] Can't pay for this route (%d)", p->ainew.new_cost);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   954
		p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   955
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   956
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   957
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   958
	// Now we can build the route, check the direction of the stations!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   959
	if (p->ainew.from_direction == AI_PATHFINDER_NO_DIRECTION) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   960
		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]);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   961
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   962
	if (p->ainew.to_direction == AI_PATHFINDER_NO_DIRECTION) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   963
		p->ainew.to_direction = AiNew_GetDirection(p->ainew.path_info.route[0], p->ainew.path_info.route[1]);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   964
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   965
	if (p->ainew.from_tile == AI_STATION_RANGE)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   966
		p->ainew.from_tile = p->ainew.path_info.route[p->ainew.path_info.route_length-1];
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   967
	if (p->ainew.to_tile == AI_STATION_RANGE)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   968
		p->ainew.to_tile = p->ainew.path_info.route[0];
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   969
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   970
	p->ainew.state = AI_STATE_BUILD_STATION;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   971
	p->ainew.temp = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
   972
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   973
	DEBUG(ai,1)("[AiNew] The route is set and buildable.. going to build it!");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   974
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   975
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   976
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   977
// Build the stations
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   978
static void AiNew_State_BuildStation(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   979
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   980
	int res = 0;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   981
	assert(p->ainew.state == AI_STATE_BUILD_STATION);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   982
	if (p->ainew.temp == 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   983
		if (!IsTileType(p->ainew.from_tile, MP_STATION))
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   984
			res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   985
	} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   986
		if (!IsTileType(p->ainew.to_tile, MP_STATION))
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   987
			res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.to_tile, 0, 0, p->ainew.to_direction, DC_EXEC);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   988
		p->ainew.state = AI_STATE_BUILD_PATH;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   989
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   990
	if (res == CMD_ERROR) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   991
		DEBUG(ai,0)("[AiNew - BuildStation] Strange but true... station can not be build!");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   992
		p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   993
		// If the first station _was_ build, destroy it
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   994
		if (p->ainew.temp != 0)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   995
			DoCommandByTile(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   996
		return;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   997
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
   998
	p->ainew.temp++;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
   999
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1000
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1001
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1002
// Build the path
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1003
static void AiNew_State_BuildPath(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1004
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1005
	assert(p->ainew.state == AI_STATE_BUILD_PATH);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1006
	// p->ainew.temp is set to -1 when this function is called for the first time
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1007
	if (p->ainew.temp == -1) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1008
		DEBUG(ai,1)("[AiNew] Starting to build the path..");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1009
		// Init the counter
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1010
		p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1011
		// Set the position to the startingplace (-1 because in a minute we do ++)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1012
		p->ainew.path_info.position = -1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1013
		// And don't do this again
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1014
		p->ainew.temp = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1015
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1016
	// Building goes very fast on normal rate, so we are going to slow it down..
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1017
	//  By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1018
	if (--p->ainew.counter != 0) return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1019
	p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1020
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1021
	// Increase the building position
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1022
	p->ainew.path_info.position++;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1023
	// Build route
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1024
	AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_EXEC);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1025
	if (p->ainew.path_info.position == -2) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1026
		// This means we are done building!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1027
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1028
		if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1029
			static const byte _roadbits_by_dir[4] = {2,1,8,4};
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1030
			// If they not queue, they have to go up and down to try again at a station...
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1031
			// We don't want that, so try building some road left or right of the station
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1032
			int dir1, dir2, dir3;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1033
			TileIndex tile;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1034
			int i, ret;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1035
			for (i=0;i<2;i++) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1036
				if (i == 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1037
					tile = p->ainew.from_tile + TileOffsByDir(p->ainew.from_direction);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1038
					dir1 = p->ainew.from_direction - 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1039
					if (dir1 < 0) dir1 = 3;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1040
					dir2 = p->ainew.from_direction + 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1041
					if (dir2 > 3) dir2 = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1042
					dir3 = p->ainew.from_direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1043
				} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1044
					tile = p->ainew.to_tile + TileOffsByDir(p->ainew.to_direction);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1045
					dir1 = p->ainew.to_direction - 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1046
					if (dir1 < 0) dir1 = 3;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1047
					dir2 = p->ainew.to_direction + 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1048
					if (dir2 > 3) dir2 = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1049
					dir3 = p->ainew.to_direction;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1050
				}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1051
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1052
				ret = DoCommandByTile(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1053
				if (!CmdFailed(ret)) {
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
  1054
					dir1 = TileOffsByDir(dir1);
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1055
					if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1056
						ret = DoCommandByTile(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1057
						if (!CmdFailed(ret)) {
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1058
							if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES))
146
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1059
								DoCommandByTile(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1060
						}
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1061
					}
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1062
				}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1063
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1064
				ret = DoCommandByTile(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1065
				if (!CmdFailed(ret)) {
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
  1066
					dir2 = TileOffsByDir(dir2);
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1067
					if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1068
						ret = DoCommandByTile(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1069
						if (!CmdFailed(ret)) {
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1070
							if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES))
146
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1071
								DoCommandByTile(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1072
						}
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1073
					}
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1074
				}
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1075
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1076
				ret = DoCommandByTile(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1077
				if (!CmdFailed(ret)) {
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
  1078
					dir3 = TileOffsByDir(dir3);
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1079
					if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1080
						ret = DoCommandByTile(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1081
						if (!CmdFailed(ret)) {
1035
0a170deb6e33 (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1082
							if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES))
146
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1083
								DoCommandByTile(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1084
						}
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1085
					}
7632a1ddadc0 (svn r147) -Fix [NewAI]: small fix for road-building when RoadQueueing is disabled
truelight
parents: 145
diff changeset
  1086
				}
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1087
			}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1088
		}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1089
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1090
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1091
		DEBUG(ai,1)("[AiNew] Done building the path (cost: %d)", p->ainew.new_cost);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1092
		p->ainew.state = AI_STATE_BUILD_DEPOT;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1093
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1094
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1095
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1096
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1097
// Builds the depot
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1098
static void AiNew_State_BuildDepot(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1099
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1100
	int res = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1101
	assert(p->ainew.state == AI_STATE_BUILD_DEPOT);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1102
2049
ad0d49c916d4 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 1981
diff changeset
  1103
	if (IsTileType(p->ainew.depot_tile, MP_STREET) && _m[p->ainew.depot_tile].m5 & 0x20) {
1901
fb05044cf5c3 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1891
diff changeset
  1104
		if (IsTileOwner(p->ainew.depot_tile, _current_player)) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1105
			// The depot is already builded!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1106
			p->ainew.state = AI_STATE_BUILD_VEHICLE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1107
			return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1108
		} else {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1109
			// There is a depot, but not of our team! :(
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1110
			p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1111
			return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1112
		}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1113
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1114
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1115
	// There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p)
900
feed1801fd35 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 826
diff changeset
  1116
	if (!EnsureNoVehicle(p->ainew.depot_tile + TileOffsByDir(p->ainew.depot_direction)))
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1117
		return;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1118
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1119
	res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC);
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1120
	if (res == CMD_ERROR) {
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1121
		DEBUG(ai,0)("[AiNew - BuildDepot] Strange but true... depot can not be build!");
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1122
		p->ainew.state = AI_STATE_NOTHING;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1123
		return;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1124
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1125
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1126
	p->ainew.state = AI_STATE_BUILD_VEHICLE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1127
	p->ainew.idle = 1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1128
	p->ainew.veh_main_id = (VehicleID)-1;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1129
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1130
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1131
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1132
// Build vehicles
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1133
static void AiNew_State_BuildVehicle(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1134
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1135
	int res;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1136
	assert(p->ainew.state == AI_STATE_BUILD_VEHICLE);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1137
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1138
	// Check if we need to build a vehicle
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1139
	if (p->ainew.amount_veh == 0) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1140
		// Nope, we are done!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1141
		// This means: we are all done! The route is open.. go back to NOTHING
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1142
		//  He will idle some time and it will all start over again.. :)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1143
		p->ainew.state = AI_STATE_ACTION_DONE;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1144
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1145
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1146
	if (--p->ainew.idle != 0) return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1147
	// It is realistic that the AI can only build 1 vehicle a day..
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1148
	// This makes sure of that!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1149
	p->ainew.idle = AI_BUILD_VEHICLE_TIME_BETWEEN;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1150
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1151
	// Build the vehicle
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1152
	res = AiNew_Build_Vehicle(p, p->ainew.depot_tile, DC_EXEC);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1153
	if (res == CMD_ERROR) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1154
		// This happens when the AI can't build any more vehicles!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1155
		p->ainew.state = AI_STATE_NOTHING;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1156
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1157
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1158
	// Increase the current counter
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1159
	p->ainew.cur_veh++;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1160
	// Decrease the total counter
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1161
	p->ainew.amount_veh--;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1162
	// Get the new ID
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1163
	if (p->ainew.tbt == AI_TRAIN) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1164
	} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1165
		p->ainew.veh_id = _new_roadveh_id;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1166
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1167
	// Go give some orders!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1168
	p->ainew.state = AI_STATE_GIVE_ORDERS;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1169
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1170
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1171
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1172
// Put the stations in the order list
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1173
static void AiNew_State_GiveOrders(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1174
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1175
	int idx;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1176
	Order order;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1177
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1178
	assert(p->ainew.state == AI_STATE_GIVE_ORDERS);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1179
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1180
	if (p->ainew.veh_main_id != (VehicleID)-1) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1181
		DoCommandByTile(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1182
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1183
		// Skip the first order if it is a second vehicle
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1184
		//  This to make vehicles go different ways..
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1185
		if (p->ainew.veh_id & 1)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1186
			DoCommandByTile(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1187
		p->ainew.state = AI_STATE_START_VEHICLE;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1188
		return;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1189
	} else {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1190
		p->ainew.veh_main_id = p->ainew.veh_id;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1191
	}
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1192
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1193
	// When more than 1 vehicle, we send them to different directions
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1194
	idx = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1195
	order.type = OT_GOTO_STATION;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1196
	order.flags = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1197
	order.station = _m[p->ainew.from_tile].m2;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1198
	if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1199
		order.flags |= OF_FULL_LOAD;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1200
	DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1201
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1202
	idx = 1;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1203
	order.type = OT_GOTO_STATION;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1204
	order.flags = 0;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1205
	order.station = _m[p->ainew.to_tile].m2;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1206
	if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1207
		order.flags |= OF_FULL_LOAD;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1208
	DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1209
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1210
	// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1211
	if (_patches.gotodepot) {
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1212
		idx = 2;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1213
		order.type = OT_GOTO_DEPOT;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1214
		order.flags = OF_UNLOAD;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1215
		order.station = GetDepotByTile(p->ainew.depot_tile)->index;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1216
		DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1217
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1218
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1219
	// Start the engines!
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1220
	p->ainew.state = AI_STATE_START_VEHICLE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1221
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1222
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1223
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1224
// Start the vehicle
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1225
static void AiNew_State_StartVehicle(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1226
{
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1227
	assert(p->ainew.state == AI_STATE_START_VEHICLE);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1228
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1229
	// 3, 2, 1... go! (give START_STOP command ;))
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1230
	DoCommandByTile(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1231
	// Try to build an other vehicle (that function will stop building when needed)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1232
	p->ainew.state = AI_STATE_BUILD_VEHICLE;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1233
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1234
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1235
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1236
// Repays money
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1237
static void AiNew_State_RepayMoney(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1238
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1239
	int i;
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1240
	for (i=0;i<AI_LOAN_REPAY;i++)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1241
		DoCommandByTile(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1242
	p->ainew.state = AI_STATE_ACTION_DONE;
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1243
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1244
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1245
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1246
static void AiNew_CheckVehicle(Player *p, Vehicle *v)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1247
{
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1248
	// When a vehicle is under the 6 months, we don't check for anything
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1249
	if (v->age < 180) return;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1250
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1251
	// When a vehicle is older then 1 year, it should make money...
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1252
	if (v->age > 360) {
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 679
diff changeset
  1253
		// If both years together are not more than AI_MINIMUM_ROUTE_PROFIT,
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1254
		//  it is not worth the line I guess...
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1255
		if (v->profit_last_year + v->profit_this_year < AI_MINIMUM_ROUTE_PROFIT ||
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1256
				(v->reliability * 100 >> 16) < 40) {
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1257
			// There is a possibility that the route is fucked up...
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1258
			if (v->cargo_days > AI_VEHICLE_LOST_DAYS) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1259
				// The vehicle is lost.. check the route, or else, get the vehicle
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1260
				//  back to a depot
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1261
				// TODO: make this piece of code
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1262
			}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1263
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1264
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1265
			// We are already sending him back
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1266
			if (AiNew_GetSpecialVehicleFlag(p, v) & AI_VEHICLEFLAG_SELL) {
1330
8a67d04016ce (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1267
				if (v->type == VEH_Road && IsTileDepotType(v->tile, TRANSPORT_ROAD) &&
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1268
						(v->vehstatus&VS_STOPPED)) {
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1269
					// We are at the depot, sell the vehicle
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1270
					DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1271
				}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1272
				return;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1273
			}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1274
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1275
			if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1276
			{
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1277
				int ret = 0;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1278
				if (v->type == VEH_Road)
1713
d970350410b2 (svn r2217) - Fix: [ 1184201 ] AI orders its vehicles to a competitor's truck stop. Added a CmdFailed() check to all command returns of the AI instead of the simple == / != CMD_ERROR check. This should fix the problem.
Darkvater
parents: 1490
diff changeset
  1279
					ret = DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1280
				// This means we can not find a depot :s
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1281
				//				if (CmdFailed(ret))
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1282
			}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1283
		}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1284
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1285
}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1286
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1287
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1288
// Checks all vehicles if they are still valid and make money and stuff
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1289
static void AiNew_State_CheckAllVehicles(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1290
{
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1291
	Vehicle *v;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1292
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1293
	FOR_ALL_VEHICLES(v) {
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1294
		if (v->type == 0) continue;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1295
		if (v->owner != p->index) continue;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1296
		// Currently, we only know how to handle road-vehicles
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1297
		if (v->type != VEH_Road) continue;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1298
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1299
		AiNew_CheckVehicle(p, v);
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1300
	}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1301
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1302
	p->ainew.state = AI_STATE_ACTION_DONE;
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1303
}
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1304
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1305
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1306
// Using the technique simular to the original AI
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1307
//   Keeps things logical
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1308
// It really should be in the same order as the AI_STATE's are!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1309
static AiNew_StateFunction* const _ainew_state[] = {
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1310
	NULL,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1311
	AiNew_State_FirstTime,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1312
	AiNew_State_Nothing,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1313
	AiNew_State_WakeUp,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1314
	AiNew_State_LocateRoute,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1315
	AiNew_State_FindStation,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1316
	AiNew_State_FindPath,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1317
	AiNew_State_FindDepot,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1318
	AiNew_State_VerifyRoute,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1319
	AiNew_State_BuildStation,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1320
	AiNew_State_BuildPath,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1321
	AiNew_State_BuildDepot,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1322
	AiNew_State_BuildVehicle,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1323
	AiNew_State_GiveOrders,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1324
	AiNew_State_StartVehicle,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1325
	AiNew_State_RepayMoney,
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1326
	AiNew_State_CheckAllVehicles,
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1327
	AiNew_State_ActionDone,
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1328
	NULL,
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1329
};
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1330
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1331
static void AiNew_OnTick(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1332
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1333
	if (_ainew_state[p->ainew.state] != NULL)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1334
		_ainew_state[p->ainew.state](p);
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1335
}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1336
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1337
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1338
void AiNewDoGameLoop(Player *p)
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1339
{
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1340
	// If it is a human player, it is not an AI, so bubye!
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1341
	if (IS_HUMAN_PLAYER(_current_player)) return;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1342
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1343
	if (p->ainew.state == AI_STATE_STARTUP) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1344
		// The AI just got alive!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1345
		p->ainew.state = AI_STATE_FIRST_TIME;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1346
		p->ainew.tick = 0;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1347
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1348
		// Only startup the AI
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1349
		return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1350
	}
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1351
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1352
	// We keep a ticker. We use it for competitor_speed
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1353
	p->ainew.tick++;
145
6e5468217504 (svn r146) -Fix [AI]: Tunnel/bridge bug
truelight
parents: 110
diff changeset
  1354
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1355
	// See what the speed is
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1356
	switch (_opt.diff.competitor_speed) {
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1357
		case 0: // Very slow
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1358
			if (!(p->ainew.tick&8)) return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1359
			break;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1360
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1361
		case 1: // Slow
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1362
			if (!(p->ainew.tick&4)) return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1363
			break;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1364
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1365
		case 2:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1366
			if (!(p->ainew.tick&2)) return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1367
			break;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1368
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1369
		case 3:
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1370
			if (!(p->ainew.tick&1)) return;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1371
			break;
2365
d3f8baf5b10d (svn r2891) Fix indentation
tron
parents: 2186
diff changeset
  1372
110
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1373
		case 4: // Very fast
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1374
		default: // Cool, a new speed setting.. ;) VERY fast ;)
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1375
			break;
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1376
	}
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1377
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1378
	// If we come here, we can do a tick.. do so!
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1379
	AiNew_OnTick(p);
a22a6b07904b (svn r111) -Fix: converted all linebreaks to UNIX-linebreak (\n)
truelight
parents: 95
diff changeset
  1380
}