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