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