2005-10-23 13:04:44 +00:00
|
|
|
/* $Id$ */
|
2005-07-24 14:12:37 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
/*
|
2005-10-23 13:04:44 +00:00
|
|
|
* This AI was created as a direct reaction to the big demand for some good AIs
|
|
|
|
* in OTTD. Too bad it never left alpha-stage, and it is considered dead in its
|
|
|
|
* current form.
|
|
|
|
* By the time of writing this, we, the creator of this AI and a good friend of
|
|
|
|
* mine, are designing a whole new AI-system that allows us to create AIs
|
|
|
|
* easier and without all the fuzz we encountered while I was working on this
|
|
|
|
* AI. By the time that system is finished, you can expect that this AI will
|
|
|
|
* dissapear, because it is pretty obselete and bad programmed.
|
2005-09-02 19:10:45 +00:00
|
|
|
*
|
2005-10-23 13:04:44 +00:00
|
|
|
* Meanwhile I wish you all much fun with this AI; if you are interested as
|
|
|
|
* AI-developer in this AI, I advise you not stare too long to some code, some
|
|
|
|
* things in here really are... strange ;) But in either way: enjoy :)
|
2005-09-02 19:10:45 +00:00
|
|
|
*
|
|
|
|
* -- TrueLight :: 2005-09-01
|
2004-08-22 15:56:56 +00:00
|
|
|
*/
|
|
|
|
|
2005-09-02 19:10:45 +00:00
|
|
|
#include "../../stdafx.h"
|
|
|
|
#include "../../openttd.h"
|
|
|
|
#include "../../debug.h"
|
|
|
|
#include "../../functions.h"
|
2006-03-05 10:19:33 +00:00
|
|
|
#include "../../road_map.h"
|
2006-03-24 08:55:08 +00:00
|
|
|
#include "../../station_map.h"
|
2005-09-02 19:10:45 +00:00
|
|
|
#include "../../table/strings.h"
|
|
|
|
#include "../../map.h"
|
|
|
|
#include "../../tile.h"
|
|
|
|
#include "../../command.h"
|
|
|
|
#include "trolly.h"
|
|
|
|
#include "../../town.h"
|
|
|
|
#include "../../industry.h"
|
|
|
|
#include "../../station.h"
|
|
|
|
#include "../../engine.h"
|
|
|
|
#include "../../gui.h"
|
|
|
|
#include "../../depot.h"
|
2006-04-18 05:16:13 +00:00
|
|
|
#include "../../vehicle.h"
|
2006-08-14 14:21:15 +00:00
|
|
|
#include "../../date.h"
|
2005-11-21 14:28:31 +00:00
|
|
|
#include "../ai.h"
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
// This function is called after StartUp. It is the init of an AI
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_FirstTime(Player *p)
|
|
|
|
{
|
|
|
|
// This assert is used to protect those function from misuse
|
|
|
|
// You have quickly a small mistake in the state-array
|
|
|
|
// With that, everything would go wrong. Finding that, is almost impossible
|
|
|
|
// With this assert, that problem can never happen.
|
|
|
|
assert(p->ainew.state == AI_STATE_FIRST_TIME);
|
2004-08-22 15:56:56 +00:00
|
|
|
// We first have to init some things
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2006-10-15 17:01:19 +00:00
|
|
|
if (_current_player == 1) ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 0);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
// The PathFinder (AyStar)
|
|
|
|
// TODO: Maybe when an AI goes bankrupt, this is de-init
|
|
|
|
// or when coming from a savegame.. should be checked out!
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.path_info.start_tile_tl = 0;
|
|
|
|
p->ainew.path_info.start_tile_br = 0;
|
|
|
|
p->ainew.path_info.end_tile_tl = 0;
|
|
|
|
p->ainew.path_info.end_tile_br = 0;
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.pathfinder = new_AyStar_AiPathFinder(12, &p->ainew.path_info);
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.idle = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
p->ainew.last_vehiclecheck_date = _date;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
// We ALWAYS start with a bus route.. just some basic money ;)
|
|
|
|
p->ainew.action = AI_ACTION_BUS_ROUTE;
|
|
|
|
|
|
|
|
// Let's popup the news, and after that, start building..
|
|
|
|
p->ainew.state = AI_STATE_WAKE_UP;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// This function just waste some time
|
|
|
|
// It keeps it more real. The AI can build on such tempo no normal user
|
|
|
|
// can ever keep up with that. The competitor_speed already delays a bit
|
|
|
|
// but after the AI finished a track it really needs to go to sleep.
|
|
|
|
//
|
|
|
|
// Let's say, we sleep between one and three days if the AI is put on Very Fast.
|
|
|
|
// This means that on Very Slow it will be between 16 and 48 days.. slow enough?
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_Nothing(Player *p)
|
|
|
|
{
|
|
|
|
assert(p->ainew.state == AI_STATE_NOTHING);
|
|
|
|
// If we are done idling, start over again
|
2005-11-21 14:28:31 +00:00
|
|
|
if (p->ainew.idle == 0) p->ainew.idle = AI_RandomRange(DAY_TICKS * 2) + DAY_TICKS;
|
2004-08-22 15:56:56 +00:00
|
|
|
if (--p->ainew.idle == 0) {
|
|
|
|
// We are done idling.. what you say? Let's do something!
|
|
|
|
// I mean.. the next tick ;)
|
|
|
|
p->ainew.state = AI_STATE_WAKE_UP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// This function picks out a task we are going to do.
|
|
|
|
// Currently supported:
|
|
|
|
// - Make new route
|
|
|
|
// - Check route
|
|
|
|
// - Build HQ
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_WakeUp(Player *p)
|
|
|
|
{
|
2005-03-11 22:27:49 +00:00
|
|
|
int32 money;
|
|
|
|
int c;
|
|
|
|
assert(p->ainew.state == AI_STATE_WAKE_UP);
|
2004-08-22 15:56:56 +00:00
|
|
|
// First, check if we have a HQ
|
|
|
|
if (p->location_of_house == 0) {
|
|
|
|
// We have no HQ yet, build one on a random place
|
|
|
|
// Random till we found a place for it!
|
|
|
|
// TODO: this should not be on a random place..
|
2005-11-21 14:28:31 +00:00
|
|
|
AiNew_Build_CompanyHQ(p, AI_Random() % MapSize());
|
2004-08-22 15:56:56 +00:00
|
|
|
// Enough for now, but we want to come back here the next time
|
|
|
|
// so we do not change any status
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
money = p->player_money - AI_MINIMUM_MONEY;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Let's pick an action!
|
|
|
|
if (p->ainew.action == AI_ACTION_NONE) {
|
2005-11-21 14:28:31 +00:00
|
|
|
c = AI_Random() & 0xFF;
|
2005-08-26 20:26:34 +00:00
|
|
|
if (p->current_loan > 0 &&
|
|
|
|
p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN &&
|
|
|
|
c < 10) {
|
|
|
|
p->ainew.action = AI_ACTION_REPAY_LOAN;
|
2004-08-31 16:12:52 +00:00
|
|
|
} else if (p->ainew.last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) {
|
|
|
|
// Check all vehicles once in a while
|
|
|
|
p->ainew.action = AI_ACTION_CHECK_ALL_VEHICLES;
|
|
|
|
p->ainew.last_vehiclecheck_date = _date;
|
2004-08-22 15:56:56 +00:00
|
|
|
} else if (c < 100 && !_patches.ai_disable_veh_roadveh) {
|
|
|
|
// Do we have any spots for road-vehicles left open?
|
|
|
|
if (GetFreeUnitNumber(VEH_Road) <= _patches.max_roadveh) {
|
2006-06-10 08:37:41 +00:00
|
|
|
if (c < 85) {
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.action = AI_ACTION_TRUCK_ROUTE;
|
2006-06-10 08:37:41 +00:00
|
|
|
} else {
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.action = AI_ACTION_BUS_ROUTE;
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
2006-06-10 08:37:41 +00:00
|
|
|
#if 0
|
|
|
|
} else if (c < 200 && !_patches.ai_disable_veh_train) {
|
2004-08-22 15:56:56 +00:00
|
|
|
if (GetFreeUnitNumber(VEH_Train) <= _patches.max_trains) {
|
|
|
|
p->ainew.action = AI_ACTION_TRAIN_ROUTE;
|
|
|
|
}
|
2006-06-10 08:37:41 +00:00
|
|
|
#endif
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
|
|
|
p->ainew.counter = 0;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
|
|
|
if (p->ainew.counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) {
|
|
|
|
p->ainew.action = AI_ACTION_NONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
if (_patches.ai_disable_veh_roadveh && (
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.action == AI_ACTION_BUS_ROUTE ||
|
|
|
|
p->ainew.action == AI_ACTION_TRUCK_ROUTE
|
|
|
|
)) {
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.action = AI_ACTION_NONE;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
if (p->ainew.action == AI_ACTION_REPAY_LOAN &&
|
|
|
|
money > AI_MINIMUM_LOAN_REPAY_MONEY) {
|
2004-08-22 15:56:56 +00:00
|
|
|
// We start repaying some money..
|
|
|
|
p->ainew.state = AI_STATE_REPAY_MONEY;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
|
|
|
if (p->ainew.action == AI_ACTION_CHECK_ALL_VEHICLES) {
|
|
|
|
p->ainew.state = AI_STATE_CHECK_ALL_VEHICLES;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// It is useless to start finding a route if we don't have enough money
|
|
|
|
// to build the route anyway..
|
2005-08-26 20:26:34 +00:00
|
|
|
if (p->ainew.action == AI_ACTION_BUS_ROUTE &&
|
|
|
|
money > AI_MINIMUM_BUS_ROUTE_MONEY) {
|
2004-08-22 15:56:56 +00:00
|
|
|
if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) {
|
|
|
|
p->ainew.action = AI_ACTION_NONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p->ainew.cargo = AI_NEED_CARGO;
|
|
|
|
p->ainew.state = AI_STATE_LOCATE_ROUTE;
|
|
|
|
p->ainew.tbt = AI_BUS; // Bus-route
|
|
|
|
return;
|
|
|
|
}
|
2005-08-26 20:26:34 +00:00
|
|
|
if (p->ainew.action == AI_ACTION_TRUCK_ROUTE &&
|
|
|
|
money > AI_MINIMUM_TRUCK_ROUTE_MONEY) {
|
2004-08-22 15:56:56 +00:00
|
|
|
if (GetFreeUnitNumber(VEH_Road) > _patches.max_roadveh) {
|
|
|
|
p->ainew.action = AI_ACTION_NONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p->ainew.cargo = AI_NEED_CARGO;
|
|
|
|
p->ainew.last_id = 0;
|
|
|
|
p->ainew.state = AI_STATE_LOCATE_ROUTE;
|
|
|
|
p->ainew.tbt = AI_TRUCK;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
|
|
|
static void AiNew_State_ActionDone(Player *p)
|
|
|
|
{
|
2005-03-11 22:27:49 +00:00
|
|
|
p->ainew.action = AI_ACTION_NONE;
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Check if a city or industry is good enough to start a route there
|
2005-08-26 20:26:34 +00:00
|
|
|
static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
if (type == AI_CITY) {
|
2006-06-10 08:37:41 +00:00
|
|
|
const Town* t = GetTown(ic);
|
|
|
|
const Station* st;
|
2005-02-17 10:56:19 +00:00
|
|
|
uint count = 0;
|
2004-08-22 15:56:56 +00:00
|
|
|
int j = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// We don't like roadconstructions, don't even true such a city
|
|
|
|
if (t->road_build_months != 0) return false;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Check if the rating in a city is high enough
|
|
|
|
// If not, take a chance if we want to continue
|
2005-11-21 14:28:31 +00:00
|
|
|
if (t->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !AI_CHANCE16(1,AI_CHECKCITY_CITY_CHANCE)) return false;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Check if we have build a station in this town the last 6 months
|
|
|
|
// else we don't do it. This is done, because stat updates can be slow
|
|
|
|
// and sometimes it takes up to 4 months before the stats are corectly.
|
|
|
|
// This way we don't get 12 busstations in one city of 100 population ;)
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
// Do we own it?
|
|
|
|
if (st->owner == _current_player) {
|
|
|
|
// Are we talking busses?
|
|
|
|
if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) != FACIL_BUS_STOP) continue;
|
|
|
|
// Is it the same city as we are in now?
|
|
|
|
if (st->town != t) continue;
|
|
|
|
// When was this station build?
|
|
|
|
if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false;
|
|
|
|
// Cound the amount of stations in this city that we own
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
// We do not own it, request some info about the station
|
|
|
|
// we want to know if this station gets the same good. If so,
|
|
|
|
// we want to know its rating. If it is too high, we are not going
|
|
|
|
// to build there
|
|
|
|
if (!st->goods[CT_PASSENGERS].last_speed) continue;
|
|
|
|
// Is it around our city
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceManhattan(st->xy, t->xy) > 10) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// It does take this cargo.. what is his rating?
|
|
|
|
if (st->goods[CT_PASSENGERS].rating < AI_CHECKCITY_CARGO_RATING) continue;
|
|
|
|
j++;
|
|
|
|
// When this is the first station, we build a second with no problem ;)
|
|
|
|
if (j == 1) continue;
|
|
|
|
// The rating is high.. second station...
|
|
|
|
// a little chance that we still continue
|
|
|
|
// But if there are 3 stations of this size, we never go on...
|
2005-11-21 14:28:31 +00:00
|
|
|
if (j == 2 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// We don't like this station :(
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// We are about to add one...
|
|
|
|
count++;
|
|
|
|
// Check if we the city can provide enough cargo for this amount of stations..
|
|
|
|
if (count * AI_CHECKCITY_CARGO_PER_STATION > t->max_pass) return false;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// All check are okay, so we can build here!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (type == AI_INDUSTRY) {
|
2006-06-10 08:37:41 +00:00
|
|
|
const Industry* i = GetIndustry(ic);
|
|
|
|
const Station* st;
|
2004-08-22 15:56:56 +00:00
|
|
|
int count = 0;
|
|
|
|
int j = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
if (i->town != NULL && i->town->ratings[_current_player] < 0 && AI_CHANCE16(1,4)) return false;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// No limits on delevering stations!
|
|
|
|
// Or for industry that does not give anything yet
|
2006-03-26 22:23:32 +00:00
|
|
|
if (i->produced_cargo[0] == CT_INVALID || i->total_production[0] == 0) return true;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
if (i->total_production[0] - i->total_transported[0] < AI_CHECKCITY_NEEDED_CARGO) return false;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Check if we have build a station in this town the last 6 months
|
|
|
|
// else we don't do it. This is done, because stat updates can be slow
|
|
|
|
// and sometimes it takes up to 4 months before the stats are corectly.
|
|
|
|
FOR_ALL_STATIONS(st) {
|
|
|
|
// Do we own it?
|
|
|
|
if (st->owner == _current_player) {
|
|
|
|
// Are we talking trucks?
|
|
|
|
if (p->ainew.tbt == AI_TRUCK && (FACIL_TRUCK_STOP & st->facilities) != FACIL_TRUCK_STOP) continue;
|
|
|
|
// Is it the same city as we are in now?
|
|
|
|
if (st->town != i->town) continue;
|
|
|
|
// When was this station build?
|
|
|
|
if (_date - st->build_date < AI_CHECKCITY_DATE_BETWEEN) return false;
|
|
|
|
// Cound the amount of stations in this city that we own
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
// We do not own it, request some info about the station
|
|
|
|
// we want to know if this station gets the same good. If so,
|
|
|
|
// we want to know its rating. If it is too high, we are not going
|
|
|
|
// to build there
|
2006-03-26 22:23:32 +00:00
|
|
|
if (i->produced_cargo[0] == CT_INVALID) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// It does not take this cargo
|
|
|
|
if (!st->goods[i->produced_cargo[0]].last_speed) continue;
|
|
|
|
// Is it around our industry
|
2005-01-31 07:23:15 +00:00
|
|
|
if (DistanceManhattan(st->xy, i->xy) > 5) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// It does take this cargo.. what is his rating?
|
|
|
|
if (st->goods[i->produced_cargo[0]].rating < AI_CHECKCITY_CARGO_RATING) continue;
|
|
|
|
j++;
|
|
|
|
// The rating is high.. a little chance that we still continue
|
|
|
|
// But if there are 2 stations of this size, we never go on...
|
2005-11-21 14:28:31 +00:00
|
|
|
if (j == 1 && AI_CHANCE16(1, AI_CHECKCITY_CARGO_RATING_CHANCE)) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// We don't like this station :(
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are about to add one...
|
|
|
|
count++;
|
|
|
|
// Check if we the city can provide enough cargo for this amount of stations..
|
|
|
|
if (count * AI_CHECKCITY_CARGO_PER_STATION > i->total_production[0]) return false;
|
|
|
|
|
|
|
|
// All check are okay, so we can build here!
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// This functions tries to locate a good route
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_LocateRoute(Player *p)
|
|
|
|
{
|
|
|
|
assert(p->ainew.state == AI_STATE_LOCATE_ROUTE);
|
|
|
|
// For now, we only support PASSENGERS, CITY and BUSSES
|
|
|
|
|
|
|
|
// We don't have a route yet
|
|
|
|
if (p->ainew.cargo == AI_NEED_CARGO) {
|
|
|
|
p->ainew.new_cost = 0; // No cost yet
|
|
|
|
p->ainew.temp = -1;
|
|
|
|
// Reset the counter
|
|
|
|
p->ainew.counter = 0;
|
|
|
|
|
|
|
|
p->ainew.from_ic = -1;
|
|
|
|
p->ainew.to_ic = -1;
|
|
|
|
if (p->ainew.tbt == AI_BUS) {
|
|
|
|
// For now we only have a passenger route
|
|
|
|
p->ainew.cargo = CT_PASSENGERS;
|
|
|
|
|
|
|
|
// Find a route to cities
|
|
|
|
p->ainew.from_type = AI_CITY;
|
|
|
|
p->ainew.to_type = AI_CITY;
|
2004-08-22 15:56:56 +00:00
|
|
|
} else if (p->ainew.tbt == AI_TRUCK) {
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.cargo = AI_NO_CARGO;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.from_type = AI_INDUSTRY;
|
|
|
|
p->ainew.to_type = AI_INDUSTRY;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// Now we are doing initing, we wait one tick
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increase the counter and abort if it is taking too long!
|
|
|
|
p->ainew.counter++;
|
|
|
|
if (p->ainew.counter > AI_LOCATE_ROUTE_MAX_COUNTER) {
|
|
|
|
// Switch back to doing nothing!
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// We are going to locate a city from where we are going to connect
|
|
|
|
if (p->ainew.from_ic == -1) {
|
|
|
|
if (p->ainew.temp == -1) {
|
|
|
|
// First, we pick a random spot to search from
|
2006-06-10 08:37:41 +00:00
|
|
|
if (p->ainew.from_type == AI_CITY) {
|
2006-08-22 20:41:26 +00:00
|
|
|
p->ainew.temp = AI_RandomRange(GetTownArraySize());
|
2006-06-10 08:37:41 +00:00
|
|
|
} else {
|
2006-08-22 20:41:26 +00:00
|
|
|
p->ainew.temp = AI_RandomRange(GetIndustryArraySize());
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) {
|
|
|
|
// It was not a valid city
|
|
|
|
// increase the temp with one, and return. We will come back later here
|
|
|
|
// to try again
|
|
|
|
p->ainew.temp++;
|
|
|
|
if (p->ainew.from_type == AI_CITY) {
|
2006-08-22 20:41:26 +00:00
|
|
|
if (p->ainew.temp >= GetTownArraySize()) p->ainew.temp = 0;
|
2005-08-26 20:26:34 +00:00
|
|
|
} else {
|
2006-08-22 20:41:26 +00:00
|
|
|
if (p->ainew.temp >= GetIndustryArraySize()) p->ainew.temp = 0;
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// Don't do an attempt if we are trying the same id as the last time...
|
|
|
|
if (p->ainew.last_id == p->ainew.temp) return;
|
|
|
|
p->ainew.last_id = p->ainew.temp;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// We found a good city/industry, save the data of it
|
|
|
|
p->ainew.from_ic = p->ainew.temp;
|
|
|
|
|
|
|
|
// Start the next tick with finding a to-city
|
|
|
|
p->ainew.temp = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find a to-city
|
|
|
|
if (p->ainew.temp == -1) {
|
|
|
|
// First, we pick a random spot to search to
|
2006-06-10 08:37:41 +00:00
|
|
|
if (p->ainew.to_type == AI_CITY) {
|
2006-08-22 20:41:26 +00:00
|
|
|
p->ainew.temp = AI_RandomRange(GetTownArraySize());
|
2006-06-10 08:37:41 +00:00
|
|
|
} else {
|
2006-08-22 20:41:26 +00:00
|
|
|
p->ainew.temp = AI_RandomRange(GetIndustryArraySize());
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// The same city is not allowed
|
|
|
|
// Also check if the city is valid
|
2005-08-26 20:26:34 +00:00
|
|
|
if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_type)) {
|
|
|
|
// Maybe it is valid..
|
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
/* We need to know if they are not to far apart from eachother..
|
|
|
|
* We do that by checking how much cargo we have to move and how long the
|
|
|
|
* route is.
|
|
|
|
*/
|
2005-08-26 20:26:34 +00:00
|
|
|
|
|
|
|
if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) {
|
2006-06-04 13:39:38 +00:00
|
|
|
const Town* town_from = GetTown(p->ainew.from_ic);
|
|
|
|
const Town* town_temp = GetTown(p->ainew.temp);
|
|
|
|
uint distance = DistanceManhattan(town_from->xy, town_temp->xy);
|
|
|
|
int max_cargo;
|
|
|
|
|
|
|
|
max_cargo = town_from->max_pass + town_temp->max_pass;
|
|
|
|
max_cargo -= town_from->act_pass + town_temp->act_pass;
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// max_cargo is now the amount of cargo we can move between the two cities
|
|
|
|
// If it is more than the distance, we allow it
|
2006-06-04 13:39:38 +00:00
|
|
|
if (distance <= max_cargo * AI_LOCATEROUTE_BUS_CARGO_DISTANCE) {
|
2005-08-26 20:26:34 +00:00
|
|
|
// We found a good city/industry, save the data of it
|
|
|
|
p->ainew.to_ic = p->ainew.temp;
|
|
|
|
p->ainew.state = AI_STATE_FIND_STATION;
|
|
|
|
|
|
|
|
DEBUG(ai,1)(
|
|
|
|
"[AiNew - LocateRoute] Found bus-route of %d tiles long (from %d to %d)",
|
2006-06-04 13:39:38 +00:00
|
|
|
distance,
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.from_ic,
|
|
|
|
p->ainew.temp
|
|
|
|
);
|
|
|
|
|
|
|
|
p->ainew.from_tile = 0;
|
|
|
|
p->ainew.to_tile = 0;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (p->ainew.tbt == AI_TRUCK) {
|
2006-06-04 13:39:38 +00:00
|
|
|
const Industry* ind_from = GetIndustry(p->ainew.from_ic);
|
|
|
|
const Industry* ind_temp = GetIndustry(p->ainew.temp);
|
2005-08-26 20:26:34 +00:00
|
|
|
bool found = false;
|
|
|
|
int max_cargo = 0;
|
2006-06-04 13:39:38 +00:00
|
|
|
uint i;
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// TODO: in max_cargo, also check other cargo (beside [0])
|
|
|
|
// First we check if the from_ic produces cargo that this ic accepts
|
2006-06-04 13:39:38 +00:00
|
|
|
if (ind_from->produced_cargo[0] != CT_INVALID && ind_from->total_production[0] != 0) {
|
|
|
|
for (i = 0; i < lengthof(ind_temp->accepts_cargo); i++) {
|
|
|
|
if (ind_temp->accepts_cargo[i] == CT_INVALID) break;
|
|
|
|
if (ind_from->produced_cargo[0] == ind_temp->accepts_cargo[i]) {
|
2006-06-10 08:37:41 +00:00
|
|
|
// Found a compatible industry
|
2006-06-04 13:39:38 +00:00
|
|
|
max_cargo = ind_from->total_production[0] - ind_from->total_transported[0];
|
2004-08-22 15:56:56 +00:00
|
|
|
found = true;
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.from_deliver = true;
|
|
|
|
p->ainew.to_deliver = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-04 13:39:38 +00:00
|
|
|
if (!found && ind_temp->produced_cargo[0] != CT_INVALID && ind_temp->total_production[0] != 0) {
|
2005-08-26 20:26:34 +00:00
|
|
|
// If not check if the current ic produces cargo that the from_ic accepts
|
2006-06-04 13:39:38 +00:00
|
|
|
for (i = 0; i < lengthof(ind_from->accepts_cargo); i++) {
|
|
|
|
if (ind_from->accepts_cargo[i] == CT_INVALID) break;
|
|
|
|
if (ind_temp->produced_cargo[0] == ind_from->accepts_cargo[i]) {
|
2004-08-22 15:56:56 +00:00
|
|
|
// Found a compatbiel industry
|
|
|
|
found = true;
|
2006-06-04 13:39:38 +00:00
|
|
|
max_cargo = ind_temp->total_production[0] - ind_temp->total_transported[0];
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.from_deliver = false;
|
|
|
|
p->ainew.to_deliver = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) {
|
|
|
|
// Yeah, they are compatible!!!
|
|
|
|
// Check the length against the amount of goods
|
2006-06-04 13:39:38 +00:00
|
|
|
uint distance = DistanceManhattan(ind_from->xy, ind_temp->xy);
|
|
|
|
|
|
|
|
if (distance > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE &&
|
|
|
|
distance <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) {
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.to_ic = p->ainew.temp;
|
|
|
|
if (p->ainew.from_deliver) {
|
2006-06-04 13:39:38 +00:00
|
|
|
p->ainew.cargo = ind_from->produced_cargo[0];
|
2005-08-26 20:26:34 +00:00
|
|
|
} else {
|
2006-06-04 13:39:38 +00:00
|
|
|
p->ainew.cargo = ind_temp->produced_cargo[0];
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
p->ainew.state = AI_STATE_FIND_STATION;
|
|
|
|
|
|
|
|
DEBUG(ai,1)(
|
|
|
|
"[AiNew - LocateRoute] Found truck-route of %d tiles long (from %d to %d)",
|
2006-06-04 13:39:38 +00:00
|
|
|
distance,
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.from_ic,
|
|
|
|
p->ainew.temp
|
|
|
|
);
|
|
|
|
|
|
|
|
p->ainew.from_tile = 0;
|
|
|
|
p->ainew.to_tile = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-22 15:56:56 +00:00
|
|
|
|
2005-02-01 18:32:01 +00:00
|
|
|
// It was not a valid city
|
|
|
|
// increase the temp with one, and return. We will come back later here
|
|
|
|
// to try again
|
|
|
|
p->ainew.temp++;
|
|
|
|
if (p->ainew.to_type == AI_CITY) {
|
2006-08-22 20:41:26 +00:00
|
|
|
if (p->ainew.temp >= GetTownArraySize()) p->ainew.temp = 0;
|
2005-02-01 18:32:01 +00:00
|
|
|
} else {
|
2006-08-22 20:41:26 +00:00
|
|
|
if (p->ainew.temp >= GetIndustryArraySize()) p->ainew.temp = 0;
|
2005-02-01 18:32:01 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// Don't do an attempt if we are trying the same id as the last time...
|
|
|
|
if (p->ainew.last_id == p->ainew.temp) return;
|
|
|
|
p->ainew.last_id = p->ainew.temp;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-12-29 13:13:29 +00:00
|
|
|
// Check if there are not more than a certain amount of vehicles pointed to a certain
|
2004-08-22 15:56:56 +00:00
|
|
|
// station. This to prevent 10 busses going to one station, which gives... problems ;)
|
2005-08-26 20:26:34 +00:00
|
|
|
static bool AiNew_CheckVehicleStation(Player *p, Station *st)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
int count = 0;
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
// Also check if we don't have already a lot of busses to this city...
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner == _current_player) {
|
2005-01-15 19:06:22 +00:00
|
|
|
const Order *order;
|
|
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(v, order) {
|
2006-09-03 08:25:27 +00:00
|
|
|
if (order->type == OT_GOTO_STATION && GetStation(order->dest) == st) {
|
2005-01-15 19:06:22 +00:00
|
|
|
// This vehicle has this city in its list
|
|
|
|
count++;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > AI_CHECK_MAX_VEHICLE_PER_STATION) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function finds a good spot for a station
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_FindStation(Player *p)
|
|
|
|
{
|
|
|
|
TileIndex tile;
|
|
|
|
Station *st;
|
2006-05-21 15:19:20 +00:00
|
|
|
int count = 0;
|
|
|
|
EngineID i;
|
2005-08-26 20:26:34 +00:00
|
|
|
TileIndex new_tile = 0;
|
|
|
|
byte direction = 0;
|
|
|
|
Town *town = NULL;
|
|
|
|
assert(p->ainew.state == AI_STATE_FIND_STATION);
|
|
|
|
|
|
|
|
if (p->ainew.from_tile == 0) {
|
|
|
|
// First we scan for a station in the from-city
|
|
|
|
if (p->ainew.from_type == AI_CITY) {
|
|
|
|
town = GetTown(p->ainew.from_ic);
|
|
|
|
tile = town->xy;
|
|
|
|
} else {
|
2006-06-04 13:39:38 +00:00
|
|
|
tile = GetIndustry(p->ainew.from_ic)->xy;
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
} else if (p->ainew.to_tile == 0) {
|
|
|
|
// Second we scan for a station in the to-city
|
|
|
|
if (p->ainew.to_type == AI_CITY) {
|
|
|
|
town = GetTown(p->ainew.to_ic);
|
|
|
|
tile = town->xy;
|
|
|
|
} else {
|
2006-06-04 13:39:38 +00:00
|
|
|
tile = GetIndustry(p->ainew.to_ic)->xy;
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Unsupported request
|
|
|
|
// Go to FIND_PATH
|
|
|
|
p->ainew.temp = -1;
|
|
|
|
p->ainew.state = AI_STATE_FIND_PATH;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// First, we are going to look at the stations that already exist inside the city
|
|
|
|
// If there is enough cargo left in the station, we take that station
|
|
|
|
// If that is not possible, and there are more than 2 stations in the city, abort
|
2004-08-22 15:56:56 +00:00
|
|
|
i = AiNew_PickVehicle(p);
|
|
|
|
// Euhmz, this should not happen _EVER_
|
|
|
|
// Quit finding a route...
|
2006-05-21 15:19:20 +00:00
|
|
|
if (i == INVALID_ENGINE) {
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-22 15:56:56 +00:00
|
|
|
|
|
|
|
FOR_ALL_STATIONS(st) {
|
2006-08-22 15:33:35 +00:00
|
|
|
if (st->owner == _current_player) {
|
|
|
|
if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) == FACIL_BUS_STOP) {
|
|
|
|
if (st->town == town) {
|
|
|
|
// Check how much cargo there is left in the station
|
|
|
|
if ((st->goods[p->ainew.cargo].waiting_acceptance & 0xFFF) > RoadVehInfo(i)->capacity * AI_STATION_REUSE_MULTIPLER) {
|
|
|
|
if (AiNew_CheckVehicleStation(p, st)) {
|
|
|
|
// We did found a station that was good enough!
|
|
|
|
new_tile = st->xy;
|
|
|
|
direction = GetRoadStopDir(st->xy);
|
|
|
|
break;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2006-08-22 15:33:35 +00:00
|
|
|
count++;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We are going to add a new station...
|
|
|
|
if (new_tile == 0) count++;
|
2004-12-29 13:13:29 +00:00
|
|
|
// No more than 2 stations allowed in a city
|
2004-08-22 15:56:56 +00:00
|
|
|
// This is because only the best 2 stations of one cargo do get any cargo
|
|
|
|
if (count > 2) {
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
if (new_tile == 0 && p->ainew.tbt == AI_BUS) {
|
|
|
|
uint x, y, i = 0;
|
|
|
|
int r;
|
|
|
|
uint best;
|
|
|
|
uint accepts[NUM_CARGO];
|
|
|
|
TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4];
|
|
|
|
uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4];
|
|
|
|
// To find a good spot we scan a range from the center, a get the point
|
|
|
|
// where we get the most cargo and where it is buildable.
|
|
|
|
// TODO: also check for station of myself and make sure we are not
|
|
|
|
// taking eachothers passangers away (bad result when it does not)
|
|
|
|
for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) {
|
|
|
|
for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) {
|
|
|
|
new_tile = TileXY(x, y);
|
|
|
|
if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) {
|
|
|
|
// This tile we can build on!
|
|
|
|
// Check acceptance
|
|
|
|
// XXX - Get the catchment area
|
|
|
|
GetAcceptanceAroundTiles(accepts, new_tile, 1, 1, 4);
|
|
|
|
// >> 3 == 0 means no cargo
|
|
|
|
if (accepts[p->ainew.cargo] >> 3 == 0) continue;
|
|
|
|
// See if we can build the station
|
|
|
|
r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(r)) continue;
|
2005-08-26 20:26:34 +00:00
|
|
|
// We can build it, so add it to found_spot
|
|
|
|
found_spot[i] = new_tile;
|
|
|
|
found_best[i++] = accepts[p->ainew.cargo];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
// If i is still zero, we did not find anything
|
2005-08-26 20:26:34 +00:00
|
|
|
if (i == 0) {
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through all the found_best and check which has the highest value
|
|
|
|
best = 0;
|
|
|
|
new_tile = 0;
|
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
for (x = 0; x < i; x++) {
|
2005-08-26 20:26:34 +00:00
|
|
|
if (found_best[x] > best ||
|
|
|
|
(found_best[x] == best && DistanceManhattan(tile, new_tile) > DistanceManhattan(tile, found_spot[x]))) {
|
|
|
|
new_tile = found_spot[x];
|
|
|
|
best = found_best[x];
|
|
|
|
}
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// See how much it is going to cost us...
|
|
|
|
r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST);
|
|
|
|
p->ainew.new_cost += r;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
direction = AI_PATHFINDER_NO_DIRECTION;
|
|
|
|
} else if (new_tile == 0 && p->ainew.tbt == AI_TRUCK) {
|
|
|
|
// Truck station locater works differently.. a station can be on any place
|
|
|
|
// as long as it is in range. So we give back code AI_STATION_RANGE
|
|
|
|
// so the pathfinder routine can work it out!
|
|
|
|
new_tile = AI_STATION_RANGE;
|
|
|
|
direction = AI_PATHFINDER_NO_DIRECTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->ainew.from_tile == 0) {
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.from_tile = new_tile;
|
|
|
|
p->ainew.from_direction = direction;
|
|
|
|
// Now we found thisone, go in for to_tile
|
|
|
|
return;
|
2004-08-22 15:56:56 +00:00
|
|
|
} else if (p->ainew.to_tile == 0) {
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.to_tile = new_tile;
|
|
|
|
p->ainew.to_direction = direction;
|
|
|
|
// K, done placing stations!
|
|
|
|
p->ainew.temp = -1;
|
|
|
|
p->ainew.state = AI_STATE_FIND_PATH;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// We try to find a path between 2 points
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_FindPath(Player *p)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
assert(p->ainew.state == AI_STATE_FIND_PATH);
|
|
|
|
|
|
|
|
// First time, init some data
|
|
|
|
if (p->ainew.temp == -1) {
|
|
|
|
// Init path_info
|
|
|
|
if (p->ainew.from_tile == AI_STATION_RANGE) {
|
2006-06-04 13:39:38 +00:00
|
|
|
const Industry* i = GetIndustry(p->ainew.from_ic);
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// For truck routes we take a range around the industry
|
2006-06-04 13:39:38 +00:00
|
|
|
p->ainew.path_info.start_tile_tl = i->xy - TileDiffXY(1, 1);
|
|
|
|
p->ainew.path_info.start_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1);
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.path_info.start_direction = p->ainew.from_direction;
|
|
|
|
} else {
|
|
|
|
p->ainew.path_info.start_tile_tl = p->ainew.from_tile;
|
|
|
|
p->ainew.path_info.start_tile_br = p->ainew.from_tile;
|
|
|
|
p->ainew.path_info.start_direction = p->ainew.from_direction;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->ainew.to_tile == AI_STATION_RANGE) {
|
2006-06-04 13:39:38 +00:00
|
|
|
const Industry* i = GetIndustry(p->ainew.to_ic);
|
|
|
|
|
|
|
|
p->ainew.path_info.end_tile_tl = i->xy - TileDiffXY(1, 1);
|
|
|
|
p->ainew.path_info.end_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1);
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.path_info.end_direction = p->ainew.to_direction;
|
|
|
|
} else {
|
|
|
|
p->ainew.path_info.end_tile_tl = p->ainew.to_tile;
|
|
|
|
p->ainew.path_info.end_tile_br = p->ainew.to_tile;
|
|
|
|
p->ainew.path_info.end_direction = p->ainew.to_direction;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2006-06-10 08:37:41 +00:00
|
|
|
p->ainew.path_info.rail_or_road = (p->ainew.tbt == AI_TRAIN);
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// First, clean the pathfinder with our new begin and endpoints
|
2005-08-26 20:26:34 +00:00
|
|
|
clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info);
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.temp = 0;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// Start the pathfinder
|
2004-08-22 15:56:56 +00:00
|
|
|
r = p->ainew.pathfinder->main(p->ainew.pathfinder);
|
2006-06-10 08:37:41 +00:00
|
|
|
switch (r) {
|
|
|
|
case AYSTAR_NO_PATH:
|
|
|
|
DEBUG(ai,1)("[AiNew] PathFinder found no route!");
|
|
|
|
// Start all over again
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AYSTAR_FOUND_END_NODE: // We found the end-point
|
|
|
|
p->ainew.temp = -1;
|
|
|
|
p->ainew.state = AI_STATE_FIND_DEPOT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// In any other case, we are still busy finding the route
|
|
|
|
default: break;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// This function tries to locate a good place for a depot!
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_FindDepot(Player *p)
|
|
|
|
{
|
|
|
|
// To place the depot, we walk through the route, and if we find a lovely spot (MP_CLEAR, MP_TREES), we place it there..
|
|
|
|
// Simple, easy, works!
|
|
|
|
// To make the depot stand in the middle of the route, we start from the center..
|
|
|
|
// But first we walk through the route see if we can find a depot that is ours
|
|
|
|
// this keeps things nice ;)
|
2005-11-14 08:09:57 +00:00
|
|
|
int g, i, r;
|
2006-03-06 20:28:28 +00:00
|
|
|
DiagDirection j;
|
2004-08-22 15:56:56 +00:00
|
|
|
TileIndex tile;
|
|
|
|
assert(p->ainew.state == AI_STATE_FIND_DEPOT);
|
|
|
|
|
|
|
|
p->ainew.depot_tile = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
for (i=2;i<p->ainew.path_info.route_length-2;i++) {
|
|
|
|
tile = p->ainew.path_info.route[i];
|
2005-01-05 13:32:03 +00:00
|
|
|
for (j = 0; j < 4; j++) {
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndex t = tile + TileOffsByDiagDir(j);
|
2006-03-06 20:28:28 +00:00
|
|
|
|
|
|
|
if (IsTileType(t, MP_STREET) &&
|
2006-05-09 08:25:31 +00:00
|
|
|
GetRoadTileType(t) == ROAD_TILE_DEPOT &&
|
2006-03-06 20:28:28 +00:00
|
|
|
IsTileOwner(t, _current_player) &&
|
2006-03-11 09:10:46 +00:00
|
|
|
GetRoadDepotDirection(t) == ReverseDiagDir(j)) {
|
2006-03-06 20:28:28 +00:00
|
|
|
p->ainew.depot_tile = t;
|
|
|
|
p->ainew.depot_direction = ReverseDiagDir(j);
|
|
|
|
p->ainew.state = AI_STATE_VERIFY_ROUTE;
|
|
|
|
return;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// This routine let depot finding start in the middle, and work his way to the stations
|
|
|
|
// It makes depot placing nicer :)
|
|
|
|
i = p->ainew.path_info.route_length / 2;
|
|
|
|
g = 1;
|
|
|
|
while (i > 1 && i < p->ainew.path_info.route_length - 2) {
|
|
|
|
i += g;
|
|
|
|
g *= -1;
|
|
|
|
(g < 0?g--:g++);
|
|
|
|
|
|
|
|
if (p->ainew.path_info.route_extra[i] != 0 || p->ainew.path_info.route_extra[i+1] != 0) {
|
|
|
|
// Bridge or tunnel.. we can't place a depot there
|
|
|
|
continue;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
tile = p->ainew.path_info.route[i];
|
|
|
|
|
2005-01-05 13:32:03 +00:00
|
|
|
for (j = 0; j < 4; j++) {
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndex t = tile + TileOffsByDiagDir(j);
|
2006-03-06 20:28:28 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// It may not be placed on the road/rail itself
|
|
|
|
// And because it is not build yet, we can't see it on the tile..
|
|
|
|
// So check the surrounding tiles :)
|
2006-03-06 20:28:28 +00:00
|
|
|
if (t == p->ainew.path_info.route[i - 1] ||
|
|
|
|
t == p->ainew.path_info.route[i + 1]) {
|
2005-08-26 20:26:34 +00:00
|
|
|
continue;
|
2006-03-06 20:28:28 +00:00
|
|
|
}
|
2004-08-22 15:56:56 +00:00
|
|
|
// Not around a bridge?
|
|
|
|
if (p->ainew.path_info.route_extra[i] != 0) continue;
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// Is the terrain clear?
|
2006-03-06 20:28:28 +00:00
|
|
|
if (IsTileType(t, MP_CLEAR) || IsTileType(t, MP_TREES)) {
|
2006-04-23 19:08:33 +00:00
|
|
|
// If the current tile is on a slope then we do not allow this
|
|
|
|
if (GetTileSlope(tile, NULL) != SLOPE_FLAT) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// Check if everything went okay..
|
2006-03-06 20:28:28 +00:00
|
|
|
r = AiNew_Build_Depot(p, t, ReverseDiagDir(j), 0);
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(r)) continue;
|
2004-08-22 15:56:56 +00:00
|
|
|
// Found a spot!
|
|
|
|
p->ainew.new_cost += r;
|
2006-03-06 20:28:28 +00:00
|
|
|
p->ainew.depot_tile = t;
|
|
|
|
p->ainew.depot_direction = ReverseDiagDir(j); // Reverse direction
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_VERIFY_ROUTE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Failed to find a depot?
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function calculates how many vehicles there are needed on this
|
|
|
|
// traject.
|
|
|
|
// It works pretty simple: get the length, see how much we move around
|
|
|
|
// and hussle that, and you know how many vehicles there are needed.
|
|
|
|
// It returns the cost for the vehicles
|
2005-08-26 20:26:34 +00:00
|
|
|
static int AiNew_HowManyVehicles(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
if (p->ainew.tbt == AI_BUS) {
|
2005-08-26 20:26:34 +00:00
|
|
|
// For bus-routes we look at the time before we are back in the station
|
2006-05-21 15:19:20 +00:00
|
|
|
EngineID i;
|
|
|
|
int length, tiles_a_day;
|
2004-08-22 15:56:56 +00:00
|
|
|
int amount;
|
|
|
|
i = AiNew_PickVehicle(p);
|
2006-05-21 15:19:20 +00:00
|
|
|
if (i == INVALID_ENGINE) return 0;
|
2005-08-26 20:26:34 +00:00
|
|
|
// Passenger run.. how long is the route?
|
|
|
|
length = p->ainew.path_info.route_length;
|
|
|
|
// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
|
|
|
|
tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16;
|
|
|
|
// We want a vehicle in a station once a month at least, so, calculate it!
|
|
|
|
// (the * 2 is because we have 2 stations ;))
|
|
|
|
amount = length * 2 * 2 / tiles_a_day / 30;
|
|
|
|
if (amount == 0) amount = 1;
|
|
|
|
return amount;
|
2004-08-22 15:56:56 +00:00
|
|
|
} else if (p->ainew.tbt == AI_TRUCK) {
|
2005-08-26 20:26:34 +00:00
|
|
|
// For truck-routes we look at the cargo
|
2006-05-21 15:19:20 +00:00
|
|
|
EngineID i;
|
|
|
|
int length, amount, tiles_a_day;
|
2004-08-22 15:56:56 +00:00
|
|
|
int max_cargo;
|
|
|
|
i = AiNew_PickVehicle(p);
|
2006-05-21 15:19:20 +00:00
|
|
|
if (i == INVALID_ENGINE) return 0;
|
2005-08-26 20:26:34 +00:00
|
|
|
// Passenger run.. how long is the route?
|
|
|
|
length = p->ainew.path_info.route_length;
|
|
|
|
// Calculating tiles a day a vehicle moves is not easy.. this is how it must be done!
|
|
|
|
tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16;
|
2006-06-10 08:37:41 +00:00
|
|
|
if (p->ainew.from_deliver) {
|
2005-08-26 20:26:34 +00:00
|
|
|
max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0];
|
2006-06-10 08:37:41 +00:00
|
|
|
} else {
|
2005-08-26 20:26:34 +00:00
|
|
|
max_cargo = GetIndustry(p->ainew.to_ic)->total_production[0];
|
2006-06-10 08:37:41 +00:00
|
|
|
}
|
2005-08-26 20:26:34 +00:00
|
|
|
|
|
|
|
// This is because moving 60% is more than we can dream of!
|
|
|
|
max_cargo *= 0.6;
|
|
|
|
// We want all the cargo to be gone in a month.. so, we know the cargo it delivers
|
|
|
|
// we know what the vehicle takes with him, and we know the time it takes him
|
|
|
|
// to get back here.. now let's do some math!
|
|
|
|
amount = 2 * length * max_cargo / tiles_a_day / 30 / RoadVehInfo(i)->capacity;
|
|
|
|
amount += 1;
|
|
|
|
return amount;
|
2004-08-22 15:56:56 +00:00
|
|
|
} else {
|
|
|
|
// Currently not supported
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This function checks:
|
|
|
|
// - If the route went okay
|
|
|
|
// - Calculates the amount of money needed to build the route
|
|
|
|
// - Calculates how much vehicles needed for the route
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_VerifyRoute(Player *p)
|
|
|
|
{
|
|
|
|
int res, i;
|
|
|
|
assert(p->ainew.state == AI_STATE_VERIFY_ROUTE);
|
|
|
|
|
|
|
|
// Let's calculate the cost of the path..
|
|
|
|
// new_cost already contains the cost of the stations
|
|
|
|
p->ainew.path_info.position = -1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
p->ainew.path_info.position++;
|
|
|
|
p->ainew.new_cost += AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_QUERY_COST);
|
2004-08-22 15:56:56 +00:00
|
|
|
} while (p->ainew.path_info.position != -2);
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Now we know the price of build station + path. Now check how many vehicles
|
|
|
|
// we need and what the price for that will be
|
|
|
|
res = AiNew_HowManyVehicles(p);
|
|
|
|
// If res == 0, no vehicle was found, or an other problem did occour
|
|
|
|
if (res == 0) {
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
p->ainew.amount_veh = res;
|
|
|
|
p->ainew.cur_veh = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Check how much it it going to cost us..
|
|
|
|
for (i=0;i<res;i++) {
|
|
|
|
p->ainew.new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST);
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Now we know how much the route is going to cost us
|
|
|
|
// Check if we have enough money for it!
|
|
|
|
if (p->ainew.new_cost > p->player_money - AI_MINIMUM_MONEY) {
|
|
|
|
// Too bad..
|
|
|
|
DEBUG(ai,1)("[AiNew] Can't pay for this route (%d)", p->ainew.new_cost);
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Now we can build the route, check the direction of the stations!
|
|
|
|
if (p->ainew.from_direction == AI_PATHFINDER_NO_DIRECTION) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
if (p->ainew.to_direction == AI_PATHFINDER_NO_DIRECTION) {
|
|
|
|
p->ainew.to_direction = AiNew_GetDirection(p->ainew.path_info.route[0], p->ainew.path_info.route[1]);
|
|
|
|
}
|
|
|
|
if (p->ainew.from_tile == AI_STATION_RANGE)
|
|
|
|
p->ainew.from_tile = p->ainew.path_info.route[p->ainew.path_info.route_length-1];
|
|
|
|
if (p->ainew.to_tile == AI_STATION_RANGE)
|
|
|
|
p->ainew.to_tile = p->ainew.path_info.route[0];
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_BUILD_STATION;
|
|
|
|
p->ainew.temp = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
DEBUG(ai,1)("[AiNew] The route is set and buildable.. going to build it!");
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Build the stations
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_BuildStation(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
int res = 0;
|
2005-08-26 20:26:34 +00:00
|
|
|
assert(p->ainew.state == AI_STATE_BUILD_STATION);
|
|
|
|
if (p->ainew.temp == 0) {
|
|
|
|
if (!IsTileType(p->ainew.from_tile, MP_STATION))
|
|
|
|
res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC);
|
|
|
|
} else {
|
|
|
|
if (!IsTileType(p->ainew.to_tile, MP_STATION))
|
|
|
|
res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.to_tile, 0, 0, p->ainew.to_direction, DC_EXEC);
|
|
|
|
p->ainew.state = AI_STATE_BUILD_PATH;
|
|
|
|
}
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(res)) {
|
2004-08-22 15:56:56 +00:00
|
|
|
DEBUG(ai,0)("[AiNew - BuildStation] Strange but true... station can not be build!");
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
// If the first station _was_ build, destroy it
|
|
|
|
if (p->ainew.temp != 0)
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
|
2004-08-22 15:56:56 +00:00
|
|
|
return;
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
p->ainew.temp++;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Build the path
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_BuildPath(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
assert(p->ainew.state == AI_STATE_BUILD_PATH);
|
|
|
|
// p->ainew.temp is set to -1 when this function is called for the first time
|
|
|
|
if (p->ainew.temp == -1) {
|
|
|
|
DEBUG(ai,1)("[AiNew] Starting to build the path..");
|
|
|
|
// Init the counter
|
|
|
|
p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
|
|
|
|
// Set the position to the startingplace (-1 because in a minute we do ++)
|
|
|
|
p->ainew.path_info.position = -1;
|
|
|
|
// And don't do this again
|
|
|
|
p->ainew.temp = 0;
|
|
|
|
}
|
|
|
|
// Building goes very fast on normal rate, so we are going to slow it down..
|
|
|
|
// By let the counter count from AI_BUILDPATH_PAUSE to 0, we have a nice way :)
|
|
|
|
if (--p->ainew.counter != 0) return;
|
|
|
|
p->ainew.counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Increase the building position
|
|
|
|
p->ainew.path_info.position++;
|
|
|
|
// Build route
|
|
|
|
AiNew_Build_RoutePart(p, &p->ainew.path_info, DC_EXEC);
|
|
|
|
if (p->ainew.path_info.position == -2) {
|
|
|
|
// This means we are done building!
|
|
|
|
|
|
|
|
if (p->ainew.tbt == AI_TRUCK && !_patches.roadveh_queue) {
|
2005-08-26 20:26:34 +00:00
|
|
|
// If they not queue, they have to go up and down to try again at a station...
|
|
|
|
// We don't want that, so try building some road left or right of the station
|
|
|
|
int dir1, dir2, dir3;
|
|
|
|
TileIndex tile;
|
|
|
|
int i, ret;
|
|
|
|
for (i=0;i<2;i++) {
|
|
|
|
if (i == 0) {
|
2006-09-05 23:21:41 +00:00
|
|
|
tile = p->ainew.from_tile + TileOffsByDiagDir(p->ainew.from_direction);
|
2005-08-26 20:26:34 +00:00
|
|
|
dir1 = p->ainew.from_direction - 1;
|
|
|
|
if (dir1 < 0) dir1 = 3;
|
|
|
|
dir2 = p->ainew.from_direction + 1;
|
|
|
|
if (dir2 > 3) dir2 = 0;
|
|
|
|
dir3 = p->ainew.from_direction;
|
|
|
|
} else {
|
2006-09-05 23:21:41 +00:00
|
|
|
tile = p->ainew.to_tile + TileOffsByDiagDir(p->ainew.to_direction);
|
2005-08-26 20:26:34 +00:00
|
|
|
dir1 = p->ainew.to_direction - 1;
|
|
|
|
if (dir1 < 0) dir1 = 3;
|
|
|
|
dir2 = p->ainew.to_direction + 1;
|
|
|
|
if (dir2 > 3) dir2 = 0;
|
|
|
|
dir3 = p->ainew.to_direction;
|
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(ret)) {
|
2006-09-05 23:21:41 +00:00
|
|
|
dir1 = TileOffsByDiagDir(dir1);
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) {
|
2005-11-21 14:28:31 +00:00
|
|
|
ret = AI_DoCommand(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(ret)) {
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES))
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(tile+dir1+dir1, AiNew_GetRoadDirection(tile+dir1, tile+dir1+dir1, tile+dir1+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2004-09-01 17:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(ret)) {
|
2006-09-05 23:21:41 +00:00
|
|
|
dir2 = TileOffsByDiagDir(dir2);
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) {
|
2005-11-21 14:28:31 +00:00
|
|
|
ret = AI_DoCommand(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(ret)) {
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES))
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(tile+dir2+dir2, AiNew_GetRoadDirection(tile+dir2, tile+dir2+dir2, tile+dir2+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2004-09-01 17:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(ret)) {
|
2006-09-05 23:21:41 +00:00
|
|
|
dir3 = TileOffsByDiagDir(dir3);
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) {
|
2005-11-21 14:28:31 +00:00
|
|
|
ret = AI_DoCommand(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2005-04-20 22:30:40 +00:00
|
|
|
if (!CmdFailed(ret)) {
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES))
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(tile+dir3+dir3, AiNew_GetRoadDirection(tile+dir3, tile+dir3+dir3, tile+dir3+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD);
|
2004-09-01 17:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
DEBUG(ai,1)("[AiNew] Done building the path (cost: %d)", p->ainew.new_cost);
|
|
|
|
p->ainew.state = AI_STATE_BUILD_DEPOT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Builds the depot
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_BuildDepot(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
int res = 0;
|
|
|
|
assert(p->ainew.state == AI_STATE_BUILD_DEPOT);
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2006-05-09 08:25:31 +00:00
|
|
|
if (IsTileType(p->ainew.depot_tile, MP_STREET) && GetRoadTileType(p->ainew.depot_tile) == ROAD_TILE_DEPOT) {
|
2005-06-04 11:56:32 +00:00
|
|
|
if (IsTileOwner(p->ainew.depot_tile, _current_player)) {
|
2006-06-10 08:37:41 +00:00
|
|
|
// The depot is already built
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_BUILD_VEHICLE;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
// There is a depot, but not of our team! :(
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// There is a bus on the tile we want to build road on... idle till he is gone! (BAD PERSON! :p)
|
2006-09-05 23:21:41 +00:00
|
|
|
if (!EnsureNoVehicle(p->ainew.depot_tile + TileOffsByDiagDir(p->ainew.depot_direction)))
|
2004-08-22 15:56:56 +00:00
|
|
|
return;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
res = AiNew_Build_Depot(p, p->ainew.depot_tile, p->ainew.depot_direction, DC_EXEC);
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(res)) {
|
2004-08-22 15:56:56 +00:00
|
|
|
DEBUG(ai,0)("[AiNew - BuildDepot] Strange but true... depot can not be build!");
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
2005-08-26 20:26:34 +00:00
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_BUILD_VEHICLE;
|
2005-11-21 14:28:31 +00:00
|
|
|
p->ainew.idle = 10;
|
2006-05-21 15:19:20 +00:00
|
|
|
p->ainew.veh_main_id = INVALID_VEHICLE;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Build vehicles
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_BuildVehicle(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
int res;
|
2005-08-26 20:26:34 +00:00
|
|
|
assert(p->ainew.state == AI_STATE_BUILD_VEHICLE);
|
|
|
|
|
|
|
|
// Check if we need to build a vehicle
|
|
|
|
if (p->ainew.amount_veh == 0) {
|
|
|
|
// Nope, we are done!
|
|
|
|
// This means: we are all done! The route is open.. go back to NOTHING
|
|
|
|
// He will idle some time and it will all start over again.. :)
|
|
|
|
p->ainew.state = AI_STATE_ACTION_DONE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (--p->ainew.idle != 0) return;
|
|
|
|
// It is realistic that the AI can only build 1 vehicle a day..
|
|
|
|
// This makes sure of that!
|
|
|
|
p->ainew.idle = AI_BUILD_VEHICLE_TIME_BETWEEN;
|
|
|
|
|
|
|
|
// Build the vehicle
|
|
|
|
res = AiNew_Build_Vehicle(p, p->ainew.depot_tile, DC_EXEC);
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(res)) {
|
2005-08-26 20:26:34 +00:00
|
|
|
// This happens when the AI can't build any more vehicles!
|
|
|
|
p->ainew.state = AI_STATE_NOTHING;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Increase the current counter
|
|
|
|
p->ainew.cur_veh++;
|
|
|
|
// Decrease the total counter
|
|
|
|
p->ainew.amount_veh--;
|
|
|
|
// Go give some orders!
|
2006-06-04 09:10:24 +00:00
|
|
|
p->ainew.state = AI_STATE_WAIT_FOR_BUILD;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Put the stations in the order list
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_GiveOrders(Player *p)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
Order order;
|
2004-08-22 15:56:56 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
assert(p->ainew.state == AI_STATE_GIVE_ORDERS);
|
|
|
|
|
2006-05-21 15:19:20 +00:00
|
|
|
if (p->ainew.veh_main_id != INVALID_VEHICLE) {
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER);
|
2005-08-26 20:26:34 +00:00
|
|
|
|
|
|
|
p->ainew.state = AI_STATE_START_VEHICLE;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
p->ainew.veh_main_id = p->ainew.veh_id;
|
|
|
|
}
|
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
// Very handy for AI, goto depot.. but yeah, it needs to be activated ;)
|
|
|
|
if (_patches.gotodepot) {
|
|
|
|
idx = 0;
|
|
|
|
order.type = OT_GOTO_DEPOT;
|
|
|
|
order.flags = OF_UNLOAD;
|
2006-09-03 08:25:27 +00:00
|
|
|
order.dest = GetDepotByTile(p->ainew.depot_tile)->index;
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
idx = 0;
|
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
|
order.flags = 0;
|
2006-09-03 08:25:27 +00:00
|
|
|
order.dest = GetStationIndex(p->ainew.to_tile);
|
2005-11-21 14:28:31 +00:00
|
|
|
if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver)
|
2005-08-26 20:26:34 +00:00
|
|
|
order.flags |= OF_FULL_LOAD;
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
idx = 0;
|
2005-08-26 20:26:34 +00:00
|
|
|
order.type = OT_GOTO_STATION;
|
|
|
|
order.flags = 0;
|
2006-09-03 08:25:27 +00:00
|
|
|
order.dest = GetStationIndex(p->ainew.from_tile);
|
2005-11-21 14:28:31 +00:00
|
|
|
if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver)
|
2005-08-26 20:26:34 +00:00
|
|
|
order.flags |= OF_FULL_LOAD;
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
|
2004-08-22 15:56:56 +00:00
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
// Start the engines!
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_START_VEHICLE;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Start the vehicle
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_StartVehicle(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
assert(p->ainew.state == AI_STATE_START_VEHICLE);
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2005-11-21 14:28:31 +00:00
|
|
|
// Skip the first order if it is a second vehicle
|
|
|
|
// This to make vehicles go different ways..
|
|
|
|
if (p->ainew.cur_veh & 1)
|
|
|
|
AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER);
|
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// 3, 2, 1... go! (give START_STOP command ;))
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, p->ainew.veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
|
2004-08-22 15:56:56 +00:00
|
|
|
// Try to build an other vehicle (that function will stop building when needed)
|
2005-11-21 14:28:31 +00:00
|
|
|
p->ainew.idle = 10;
|
2004-08-22 15:56:56 +00:00
|
|
|
p->ainew.state = AI_STATE_BUILD_VEHICLE;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Repays money
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_RepayMoney(Player *p)
|
|
|
|
{
|
2006-06-27 21:25:53 +00:00
|
|
|
uint i;
|
|
|
|
|
|
|
|
for (i = 0; i < AI_LOAN_REPAY; i++) {
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, 0, 0, DC_EXEC, CMD_DECREASE_LOAN);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-08-26 20:26:34 +00:00
|
|
|
p->ainew.state = AI_STATE_ACTION_DONE;
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
|
|
|
static void AiNew_CheckVehicle(Player *p, Vehicle *v)
|
|
|
|
{
|
2004-08-31 16:12:52 +00:00
|
|
|
// When a vehicle is under the 6 months, we don't check for anything
|
|
|
|
if (v->age < 180) return;
|
|
|
|
|
|
|
|
// When a vehicle is older then 1 year, it should make money...
|
|
|
|
if (v->age > 360) {
|
2004-12-29 13:13:29 +00:00
|
|
|
// If both years together are not more than AI_MINIMUM_ROUTE_PROFIT,
|
2004-08-31 16:12:52 +00:00
|
|
|
// it is not worth the line I guess...
|
|
|
|
if (v->profit_last_year + v->profit_this_year < AI_MINIMUM_ROUTE_PROFIT ||
|
2005-08-26 20:26:34 +00:00
|
|
|
(v->reliability * 100 >> 16) < 40) {
|
2004-08-31 16:12:52 +00:00
|
|
|
// There is a possibility that the route is fucked up...
|
|
|
|
if (v->cargo_days > AI_VEHICLE_LOST_DAYS) {
|
|
|
|
// The vehicle is lost.. check the route, or else, get the vehicle
|
|
|
|
// back to a depot
|
|
|
|
// TODO: make this piece of code
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// We are already sending him back
|
|
|
|
if (AiNew_GetSpecialVehicleFlag(p, v) & AI_VEHICLEFLAG_SELL) {
|
2005-02-06 22:36:08 +00:00
|
|
|
if (v->type == VEH_Road && IsTileDepotType(v->tile, TRANSPORT_ROAD) &&
|
2005-08-26 20:26:34 +00:00
|
|
|
(v->vehstatus&VS_STOPPED)) {
|
2004-08-31 16:12:52 +00:00
|
|
|
// We are at the depot, sell the vehicle
|
2005-11-21 14:28:31 +00:00
|
|
|
AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH);
|
2004-08-31 16:12:52 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AiNew_SetSpecialVehicleFlag(p, v, AI_VEHICLEFLAG_SELL)) return;
|
|
|
|
{
|
2005-04-20 22:30:40 +00:00
|
|
|
int ret = 0;
|
2004-08-31 16:12:52 +00:00
|
|
|
if (v->type == VEH_Road)
|
2005-11-21 14:28:31 +00:00
|
|
|
ret = AI_DoCommand(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT);
|
2004-08-31 16:12:52 +00:00
|
|
|
// This means we can not find a depot :s
|
2005-08-26 20:26:34 +00:00
|
|
|
// if (CmdFailed(ret))
|
2004-08-31 16:12:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-31 16:12:52 +00:00
|
|
|
// Checks all vehicles if they are still valid and make money and stuff
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_State_CheckAllVehicles(Player *p)
|
|
|
|
{
|
2004-08-31 16:12:52 +00:00
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (v->owner != p->index) continue;
|
|
|
|
// Currently, we only know how to handle road-vehicles
|
|
|
|
if (v->type != VEH_Road) continue;
|
|
|
|
|
|
|
|
AiNew_CheckVehicle(p, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
p->ainew.state = AI_STATE_ACTION_DONE;
|
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// Using the technique simular to the original AI
|
|
|
|
// Keeps things logical
|
|
|
|
// It really should be in the same order as the AI_STATE's are!
|
|
|
|
static AiNew_StateFunction* const _ainew_state[] = {
|
2005-08-26 20:26:34 +00:00
|
|
|
NULL,
|
|
|
|
AiNew_State_FirstTime,
|
|
|
|
AiNew_State_Nothing,
|
|
|
|
AiNew_State_WakeUp,
|
|
|
|
AiNew_State_LocateRoute,
|
|
|
|
AiNew_State_FindStation,
|
|
|
|
AiNew_State_FindPath,
|
|
|
|
AiNew_State_FindDepot,
|
|
|
|
AiNew_State_VerifyRoute,
|
|
|
|
AiNew_State_BuildStation,
|
|
|
|
AiNew_State_BuildPath,
|
|
|
|
AiNew_State_BuildDepot,
|
|
|
|
AiNew_State_BuildVehicle,
|
2006-06-04 09:10:24 +00:00
|
|
|
NULL,
|
2005-08-26 20:26:34 +00:00
|
|
|
AiNew_State_GiveOrders,
|
|
|
|
AiNew_State_StartVehicle,
|
|
|
|
AiNew_State_RepayMoney,
|
2004-08-31 16:12:52 +00:00
|
|
|
AiNew_State_CheckAllVehicles,
|
2005-08-26 20:26:34 +00:00
|
|
|
AiNew_State_ActionDone,
|
|
|
|
NULL,
|
2004-08-22 15:56:56 +00:00
|
|
|
};
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
static void AiNew_OnTick(Player *p)
|
|
|
|
{
|
|
|
|
if (_ainew_state[p->ainew.state] != NULL)
|
|
|
|
_ainew_state[p->ainew.state](p);
|
2004-08-22 15:56:56 +00:00
|
|
|
}
|
|
|
|
|
2005-08-26 20:26:34 +00:00
|
|
|
|
|
|
|
void AiNewDoGameLoop(Player *p)
|
|
|
|
{
|
2004-08-22 15:56:56 +00:00
|
|
|
if (p->ainew.state == AI_STATE_STARTUP) {
|
|
|
|
// The AI just got alive!
|
|
|
|
p->ainew.state = AI_STATE_FIRST_TIME;
|
|
|
|
p->ainew.tick = 0;
|
|
|
|
|
|
|
|
// Only startup the AI
|
|
|
|
return;
|
|
|
|
}
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// We keep a ticker. We use it for competitor_speed
|
|
|
|
p->ainew.tick++;
|
2004-08-31 16:12:52 +00:00
|
|
|
|
2004-08-22 15:56:56 +00:00
|
|
|
// If we come here, we can do a tick.. do so!
|
|
|
|
AiNew_OnTick(p);
|
|
|
|
}
|