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