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" tron@3144: #include "../../road_map.h" tron@3315: #include "../../station_map.h" rubidium@8116: #include "../../command_func.h" truelight@2381: #include "trolly.h" truelight@2381: #include "../../town.h" truelight@2381: #include "../../industry.h" rubidium@8785: #include "../../station_base.h" rubidium@8786: #include "../../engine_func.h" truelight@2381: #include "../../gui.h" truelight@2381: #include "../../depot.h" rubidium@8144: #include "../../vehicle_base.h" rubidium@8144: #include "../../vehicle_func.h" rubidium@8140: #include "../../date_func.h" truelight@2682: #include "../ai.h" rubidium@8254: #include "../../player_base.h" rubidium@8254: #include "../../player_func.h" truelight@2381: rubidium@8264: #include "table/strings.h" rubidium@8264: rubidium@8229: PlayerAiNew _players_ainew[MAX_PLAYERS]; rubidium@8229: 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. rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_FIRST_TIME); truelight@2381: // We first have to init some things truelight@2381: Darkvater@4854: if (_current_player == 1) ShowErrorMessage(INVALID_STRING_ID, TEMP_AI_IN_PROGRESS, 0, 0); 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! rubidium@8229: _players_ainew[p->index].path_info.start_tile_tl = 0; rubidium@8229: _players_ainew[p->index].path_info.start_tile_br = 0; rubidium@8229: _players_ainew[p->index].path_info.end_tile_tl = 0; rubidium@8229: _players_ainew[p->index].path_info.end_tile_br = 0; rubidium@8229: _players_ainew[p->index].pathfinder = new_AyStar_AiPathFinder(12, &_players_ainew[p->index].path_info); truelight@2381: rubidium@8229: _players_ainew[p->index].idle = 0; rubidium@8229: _players_ainew[p->index].last_vehiclecheck_date = _date; truelight@2381: truelight@2381: // We ALWAYS start with a bus route.. just some basic money ;) rubidium@8229: _players_ainew[p->index].action = AI_ACTION_BUS_ROUTE; truelight@2381: truelight@2381: // Let's popup the news, and after that, start building.. rubidium@8229: _players_ainew[p->index].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: { rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_NOTHING); truelight@2381: // If we are done idling, start over again rubidium@8229: if (_players_ainew[p->index].idle == 0) _players_ainew[p->index].idle = AI_RandomRange(DAY_TICKS * 2) + DAY_TICKS; rubidium@8229: if (--_players_ainew[p->index].idle == 0) { truelight@2381: // We are done idling.. what you say? Let's do something! truelight@2381: // I mean.. the next tick ;) rubidium@8229: _players_ainew[p->index].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: int c; rubidium@8229: assert(_players_ainew[p->index].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: rubidium@6990: Money money = p->player_money - AI_MINIMUM_MONEY; truelight@2381: truelight@2381: // Let's pick an action! rubidium@8229: if (_players_ainew[p->index].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) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_REPAY_LOAN; rubidium@8229: } else if (_players_ainew[p->index].last_vehiclecheck_date + AI_DAYS_BETWEEN_VEHICLE_CHECKS < _date) { truelight@2381: // Check all vehicles once in a while rubidium@8229: _players_ainew[p->index].action = AI_ACTION_CHECK_ALL_VEHICLES; rubidium@8229: _players_ainew[p->index].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? rubidium@6259: if (GetFreeUnitNumber(VEH_ROAD) <= _patches.max_roadveh) { tron@4000: if (c < 85) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_TRUCK_ROUTE; tron@4000: } else { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_BUS_ROUTE; tron@4000: } truelight@2381: } tron@4000: #if 0 tron@4000: } else if (c < 200 && !_patches.ai_disable_veh_train) { rubidium@6259: if (GetFreeUnitNumber(VEH_TRAIN) <= _patches.max_trains) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_TRAIN_ROUTE; truelight@2381: } tron@4000: #endif tron@4000: } truelight@2381: rubidium@8229: _players_ainew[p->index].counter = 0; truelight@2381: } truelight@2381: rubidium@8229: if (_players_ainew[p->index].counter++ > AI_MAX_TRIES_FOR_SAME_ROUTE) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_NONE; truelight@2381: return; truelight@2381: } truelight@2381: truelight@2381: if (_patches.ai_disable_veh_roadveh && ( rubidium@8229: _players_ainew[p->index].action == AI_ACTION_BUS_ROUTE || rubidium@8229: _players_ainew[p->index].action == AI_ACTION_TRUCK_ROUTE truelight@2381: )) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_NONE; truelight@2381: return; truelight@2381: } truelight@2381: rubidium@8229: if (_players_ainew[p->index].action == AI_ACTION_REPAY_LOAN && truelight@2381: money > AI_MINIMUM_LOAN_REPAY_MONEY) { truelight@2381: // We start repaying some money.. rubidium@8229: _players_ainew[p->index].state = AI_STATE_REPAY_MONEY; truelight@2381: return; truelight@2381: } truelight@2381: rubidium@8229: if (_players_ainew[p->index].action == AI_ACTION_CHECK_ALL_VEHICLES) { rubidium@8229: _players_ainew[p->index].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.. rubidium@8229: if (_players_ainew[p->index].action == AI_ACTION_BUS_ROUTE && truelight@2381: money > AI_MINIMUM_BUS_ROUTE_MONEY) { rubidium@6259: if (GetFreeUnitNumber(VEH_ROAD) > _patches.max_roadveh) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_NONE; truelight@2381: return; truelight@2381: } rubidium@8229: _players_ainew[p->index].cargo = AI_NEED_CARGO; rubidium@8229: _players_ainew[p->index].state = AI_STATE_LOCATE_ROUTE; rubidium@8229: _players_ainew[p->index].tbt = AI_BUS; // Bus-route truelight@2381: return; truelight@2381: } rubidium@8229: if (_players_ainew[p->index].action == AI_ACTION_TRUCK_ROUTE && truelight@2381: money > AI_MINIMUM_TRUCK_ROUTE_MONEY) { rubidium@6259: if (GetFreeUnitNumber(VEH_ROAD) > _patches.max_roadveh) { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_NONE; truelight@2381: return; truelight@2381: } rubidium@8229: _players_ainew[p->index].cargo = AI_NEED_CARGO; rubidium@8229: _players_ainew[p->index].last_id = 0; rubidium@8229: _players_ainew[p->index].state = AI_STATE_LOCATE_ROUTE; rubidium@8229: _players_ainew[p->index].tbt = AI_TRUCK; truelight@2381: return; truelight@2381: } truelight@2381: rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; truelight@2381: } truelight@2381: truelight@2381: truelight@2381: static void AiNew_State_ActionDone(Player *p) truelight@2381: { rubidium@8229: _players_ainew[p->index].action = AI_ACTION_NONE; rubidium@8229: _players_ainew[p->index].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) { tron@4000: const Town* t = GetTown(ic); tron@4000: const 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 rubidium@6491: if (t->ratings[_current_player] < 0 && AI_CHANCE16(1, 4)) return false; truelight@2381: rubidium@6491: 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: // Do we own it? truelight@2381: if (st->owner == _current_player) { truelight@2381: // Are we talking busses? rubidium@8229: if (_players_ainew[p->index].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) { tron@4000: const Industry* i = GetIndustry(ic); tron@4000: const Station* st; truelight@2381: int count = 0; truelight@2381: int j = 0; truelight@2381: rubidium@6491: 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 glx@7645: if (i->produced_cargo[0] == CT_INVALID || i->last_month_production[0] == 0) return true; truelight@2381: rubidium@6819: if (i->last_month_production[0] - i->last_month_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: // Do we own it? truelight@2381: if (st->owner == _current_player) { truelight@2381: // Are we talking trucks? rubidium@8229: if (_players_ainew[p->index].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 glx@7645: if (i->produced_cargo[0] == CT_INVALID) continue; truelight@2381: // It does not take this cargo glx@7645: 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? glx@7645: 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.. rubidium@6819: if (count * AI_CHECKCITY_CARGO_PER_STATION > i->last_month_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: { rubidium@8229: assert(_players_ainew[p->index].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 rubidium@8229: if (_players_ainew[p->index].cargo == AI_NEED_CARGO) { rubidium@8229: _players_ainew[p->index].new_cost = 0; // No cost yet rubidium@8229: _players_ainew[p->index].temp = -1; truelight@2381: // Reset the counter rubidium@8229: _players_ainew[p->index].counter = 0; truelight@2381: rubidium@8229: _players_ainew[p->index].from_ic = -1; rubidium@8229: _players_ainew[p->index].to_ic = -1; rubidium@8229: if (_players_ainew[p->index].tbt == AI_BUS) { truelight@2381: // For now we only have a passenger route rubidium@8229: _players_ainew[p->index].cargo = CT_PASSENGERS; truelight@2381: truelight@2381: // Find a route to cities rubidium@8229: _players_ainew[p->index].from_type = AI_CITY; rubidium@8229: _players_ainew[p->index].to_type = AI_CITY; rubidium@8229: } else if (_players_ainew[p->index].tbt == AI_TRUCK) { rubidium@8229: _players_ainew[p->index].cargo = AI_NO_CARGO; truelight@2381: rubidium@8229: _players_ainew[p->index].from_type = AI_INDUSTRY; rubidium@8229: _players_ainew[p->index].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! rubidium@8229: _players_ainew[p->index].counter++; rubidium@8229: if (_players_ainew[p->index].counter > AI_LOCATE_ROUTE_MAX_COUNTER) { truelight@2381: // Switch back to doing nothing! rubidium@8229: _players_ainew[p->index].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 rubidium@8229: if (_players_ainew[p->index].from_ic == -1) { rubidium@8229: if (_players_ainew[p->index].temp == -1) { truelight@2381: // First, we pick a random spot to search from rubidium@8229: if (_players_ainew[p->index].from_type == AI_CITY) { rubidium@8229: _players_ainew[p->index].temp = AI_RandomRange(GetMaxTownIndex() + 1); tron@4000: } else { rubidium@8229: _players_ainew[p->index].temp = AI_RandomRange(GetMaxIndustryIndex() + 1); tron@4000: } truelight@2381: } truelight@2381: rubidium@8229: if (!AiNew_Check_City_or_Industry(p, _players_ainew[p->index].temp, _players_ainew[p->index].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 rubidium@8229: _players_ainew[p->index].temp++; rubidium@8229: if (_players_ainew[p->index].from_type == AI_CITY) { rubidium@8229: if (_players_ainew[p->index].temp > GetMaxTownIndex()) _players_ainew[p->index].temp = 0; truelight@2381: } else { rubidium@8229: if (_players_ainew[p->index].temp > GetMaxIndustryIndex()) _players_ainew[p->index].temp = 0; truelight@2381: } truelight@2381: truelight@2381: // Don't do an attempt if we are trying the same id as the last time... rubidium@8229: if (_players_ainew[p->index].last_id == _players_ainew[p->index].temp) return; rubidium@8229: _players_ainew[p->index].last_id = _players_ainew[p->index].temp; truelight@2381: truelight@2381: return; truelight@2381: } truelight@2381: truelight@2381: // We found a good city/industry, save the data of it rubidium@8229: _players_ainew[p->index].from_ic = _players_ainew[p->index].temp; truelight@2381: truelight@2381: // Start the next tick with finding a to-city rubidium@8229: _players_ainew[p->index].temp = -1; truelight@2381: return; truelight@2381: } truelight@2381: truelight@2381: // Find a to-city rubidium@8229: if (_players_ainew[p->index].temp == -1) { truelight@2381: // First, we pick a random spot to search to rubidium@8229: if (_players_ainew[p->index].to_type == AI_CITY) { rubidium@8229: _players_ainew[p->index].temp = AI_RandomRange(GetMaxTownIndex() + 1); tron@4000: } else { rubidium@8229: _players_ainew[p->index].temp = AI_RandomRange(GetMaxIndustryIndex() + 1); tron@4000: } truelight@2381: } truelight@2381: truelight@2381: // The same city is not allowed truelight@2381: // Also check if the city is valid rubidium@8229: if (_players_ainew[p->index].temp != _players_ainew[p->index].from_ic && AiNew_Check_City_or_Industry(p, _players_ainew[p->index].temp, _players_ainew[p->index].to_type)) { truelight@2381: // Maybe it is valid.. truelight@2381: 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@2381: rubidium@8229: if (_players_ainew[p->index].from_type == AI_CITY && _players_ainew[p->index].tbt == AI_BUS) { rubidium@8229: const Town* town_from = GetTown(_players_ainew[p->index].from_ic); rubidium@8229: const Town* town_temp = GetTown(_players_ainew[p->index].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 rubidium@8229: _players_ainew[p->index].to_ic = _players_ainew[p->index].temp; rubidium@8229: _players_ainew[p->index].state = AI_STATE_FIND_STATION; truelight@2381: Darkvater@5380: DEBUG(ai, 1, "[LocateRoute] found bus-route of %d tiles long (from %d to %d)", tron@3952: distance, rubidium@8229: _players_ainew[p->index].from_ic, rubidium@8229: _players_ainew[p->index].temp truelight@2381: ); truelight@2381: rubidium@8229: _players_ainew[p->index].from_tile = 0; rubidium@8229: _players_ainew[p->index].to_tile = 0; truelight@2381: truelight@2381: return; truelight@2381: } rubidium@8229: } else if (_players_ainew[p->index].tbt == AI_TRUCK) { rubidium@8229: const Industry* ind_from = GetIndustry(_players_ainew[p->index].from_ic); rubidium@8229: const Industry* ind_temp = GetIndustry(_players_ainew[p->index].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 glx@7645: if (ind_from->produced_cargo[0] != CT_INVALID && ind_from->last_month_production[0] != 0) { glx@7645: for (i = 0; i < lengthof(ind_temp->accepts_cargo); i++) { glx@7645: if (ind_temp->accepts_cargo[i] == CT_INVALID) break; glx@7645: if (ind_from->produced_cargo[0] == ind_temp->accepts_cargo[i]) { tron@4000: // Found a compatible industry rubidium@6819: max_cargo = ind_from->last_month_production[0] - ind_from->last_month_transported[0]; truelight@2381: found = true; rubidium@8229: _players_ainew[p->index].from_deliver = true; rubidium@8229: _players_ainew[p->index].to_deliver = false; truelight@2381: break; truelight@2381: } truelight@2381: } truelight@2381: } glx@7645: if (!found && ind_temp->produced_cargo[0] != CT_INVALID && ind_temp->last_month_production[0] != 0) { truelight@2381: // If not check if the current ic produces cargo that the from_ic accepts glx@7645: for (i = 0; i < lengthof(ind_from->accepts_cargo); i++) { glx@7645: if (ind_from->accepts_cargo[i] == CT_INVALID) break; glx@7645: if (ind_from->produced_cargo[0] == ind_from->accepts_cargo[i]) { truelight@2381: // Found a compatbiel industry truelight@2381: found = true; rubidium@6819: max_cargo = ind_temp->last_month_production[0] - ind_temp->last_month_transported[0]; rubidium@8229: _players_ainew[p->index].from_deliver = false; rubidium@8229: _players_ainew[p->index].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) { rubidium@8229: _players_ainew[p->index].to_ic = _players_ainew[p->index].temp; rubidium@8229: if (_players_ainew[p->index].from_deliver) { rubidium@8229: _players_ainew[p->index].cargo = ind_from->produced_cargo[0]; truelight@2381: } else { rubidium@8229: _players_ainew[p->index].cargo = ind_temp->produced_cargo[0]; truelight@2381: } rubidium@8229: _players_ainew[p->index].state = AI_STATE_FIND_STATION; truelight@2381: Darkvater@5380: DEBUG(ai, 1, "[LocateRoute] found truck-route of %d tiles long (from %d to %d)", tron@3952: distance, rubidium@8229: _players_ainew[p->index].from_ic, rubidium@8229: _players_ainew[p->index].temp truelight@2381: ); truelight@2381: rubidium@8229: _players_ainew[p->index].from_tile = 0; rubidium@8229: _players_ainew[p->index].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 rubidium@8229: _players_ainew[p->index].temp++; rubidium@8229: if (_players_ainew[p->index].to_type == AI_CITY) { rubidium@8229: if (_players_ainew[p->index].temp > GetMaxTownIndex()) _players_ainew[p->index].temp = 0; truelight@2381: } else { rubidium@8229: if (_players_ainew[p->index].temp > GetMaxIndustryIndex()) _players_ainew[p->index].temp = 0; truelight@2381: } truelight@2381: truelight@2381: // Don't do an attempt if we are trying the same id as the last time... rubidium@8229: if (_players_ainew[p->index].last_id == _players_ainew[p->index].temp) return; rubidium@8229: _players_ainew[p->index].last_id = _players_ainew[p->index].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) { rubidium@8836: if (order->IsType(OT_GOTO_STATION) && GetStation(order->dest) == 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; rubidium@7317: DiagDirection direction = DIAGDIR_NE; truelight@2381: Town *town = NULL; rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_FIND_STATION); truelight@2381: rubidium@8229: if (_players_ainew[p->index].from_tile == 0) { truelight@2381: // First we scan for a station in the from-city rubidium@8229: if (_players_ainew[p->index].from_type == AI_CITY) { rubidium@8229: town = GetTown(_players_ainew[p->index].from_ic); truelight@2381: tile = town->xy; truelight@2381: } else { rubidium@8229: tile = GetIndustry(_players_ainew[p->index].from_ic)->xy; truelight@2381: } rubidium@8229: } else if (_players_ainew[p->index].to_tile == 0) { truelight@2381: // Second we scan for a station in the to-city rubidium@8229: if (_players_ainew[p->index].to_type == AI_CITY) { rubidium@8229: town = GetTown(_players_ainew[p->index].to_ic); truelight@2381: tile = town->xy; truelight@2381: } else { rubidium@8229: tile = GetIndustry(_players_ainew[p->index].to_ic)->xy; truelight@2381: } truelight@2381: } else { truelight@2381: // Unsupported request truelight@2381: // Go to FIND_PATH rubidium@8229: _players_ainew[p->index].temp = -1; rubidium@8229: _players_ainew[p->index].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) { rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; tron@3885: return; tron@3885: } truelight@2381: truelight@2381: FOR_ALL_STATIONS(st) { truelight@4346: if (st->owner == _current_player) { rubidium@8229: if (_players_ainew[p->index].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 rubidium@8229: if ((int)st->goods[_players_ainew[p->index].cargo].cargo.Count() > 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@2381: } truelight@2381: } truelight@4346: count++; 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) { rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; truelight@2381: return; truelight@2381: } truelight@2381: rubidium@8229: if (new_tile == 0 && _players_ainew[p->index].tbt == AI_BUS) { truelight@2381: uint x, y, i = 0; rubidium@6943: CommandCost r; truelight@2381: uint best; truelight@2381: uint accepts[NUM_CARGO]; rubidium@6491: TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE * 4]; rubidium@6491: 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 peter1138@6924: // taking eachothers passengers 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 rubidium@8229: if (accepts[_players_ainew[p->index].cargo] >> 3 == 0) continue; truelight@2381: // See if we can build the station rubidium@8229: r = AiNew_Build_Station(p, _players_ainew[p->index].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; rubidium@8229: found_best[i++] = accepts[_players_ainew[p->index].cargo]; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: tron@4000: // If i is still zero, we did not find anything truelight@2381: if (i == 0) { rubidium@8229: _players_ainew[p->index].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: tron@4000: for (x = 0; x < i; x++) { truelight@2381: if (found_best[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... rubidium@8229: r = AiNew_Build_Station(p, _players_ainew[p->index].tbt, new_tile, 0, 0, 0, DC_QUERY_COST); rubidium@8229: _players_ainew[p->index].new_cost += r.GetCost(); truelight@2381: rubidium@7317: direction = (DiagDirection)AI_PATHFINDER_NO_DIRECTION; rubidium@8229: } else if (new_tile == 0 && _players_ainew[p->index].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; rubidium@7317: direction = (DiagDirection)AI_PATHFINDER_NO_DIRECTION; truelight@2381: } truelight@2381: rubidium@8229: if (_players_ainew[p->index].from_tile == 0) { rubidium@8229: _players_ainew[p->index].from_tile = new_tile; rubidium@8229: _players_ainew[p->index].from_direction = direction; truelight@2381: // Now we found thisone, go in for to_tile truelight@2381: return; rubidium@8229: } else if (_players_ainew[p->index].to_tile == 0) { rubidium@8229: _players_ainew[p->index].to_tile = new_tile; rubidium@8229: _players_ainew[p->index].to_direction = direction; truelight@2381: // K, done placing stations! rubidium@8229: _players_ainew[p->index].temp = -1; rubidium@8229: _players_ainew[p->index].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; rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_FIND_PATH); truelight@2381: truelight@2381: // First time, init some data rubidium@8229: if (_players_ainew[p->index].temp == -1) { truelight@2381: // Init path_info rubidium@8229: if (_players_ainew[p->index].from_tile == AI_STATION_RANGE) { rubidium@8229: const Industry* i = GetIndustry(_players_ainew[p->index].from_ic); tron@3952: truelight@2381: // For truck routes we take a range around the industry rubidium@8229: _players_ainew[p->index].path_info.start_tile_tl = i->xy - TileDiffXY(1, 1); rubidium@8229: _players_ainew[p->index].path_info.start_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1); rubidium@8229: _players_ainew[p->index].path_info.start_direction = _players_ainew[p->index].from_direction; truelight@2381: } else { rubidium@8229: _players_ainew[p->index].path_info.start_tile_tl = _players_ainew[p->index].from_tile; rubidium@8229: _players_ainew[p->index].path_info.start_tile_br = _players_ainew[p->index].from_tile; rubidium@8229: _players_ainew[p->index].path_info.start_direction = _players_ainew[p->index].from_direction; truelight@2381: } truelight@2381: rubidium@8229: if (_players_ainew[p->index].to_tile == AI_STATION_RANGE) { rubidium@8229: const Industry* i = GetIndustry(_players_ainew[p->index].to_ic); tron@3952: rubidium@8229: _players_ainew[p->index].path_info.end_tile_tl = i->xy - TileDiffXY(1, 1); rubidium@8229: _players_ainew[p->index].path_info.end_tile_br = i->xy + TileDiffXY(i->width + 1, i->height + 1); rubidium@8229: _players_ainew[p->index].path_info.end_direction = _players_ainew[p->index].to_direction; truelight@2381: } else { rubidium@8229: _players_ainew[p->index].path_info.end_tile_tl = _players_ainew[p->index].to_tile; rubidium@8229: _players_ainew[p->index].path_info.end_tile_br = _players_ainew[p->index].to_tile; rubidium@8229: _players_ainew[p->index].path_info.end_direction = _players_ainew[p->index].to_direction; truelight@2381: } truelight@2381: rubidium@8229: _players_ainew[p->index].path_info.rail_or_road = (_players_ainew[p->index].tbt == AI_TRAIN); truelight@2381: truelight@2381: // First, clean the pathfinder with our new begin and endpoints rubidium@8229: clean_AyStar_AiPathFinder(_players_ainew[p->index].pathfinder, &_players_ainew[p->index].path_info); truelight@2381: rubidium@8229: _players_ainew[p->index].temp = 0; truelight@2381: } truelight@2381: truelight@2381: // Start the pathfinder rubidium@8229: r = _players_ainew[p->index].pathfinder->main(_players_ainew[p->index].pathfinder); tron@4000: switch (r) { tron@4000: case AYSTAR_NO_PATH: Darkvater@5380: DEBUG(ai, 1, "No route found by pathfinder"); tron@4000: // Start all over again rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; tron@4000: break; tron@4000: tron@4000: case AYSTAR_FOUND_END_NODE: // We found the end-point rubidium@8229: _players_ainew[p->index].temp = -1; rubidium@8229: _players_ainew[p->index].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@2381: } 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 ;) rubidium@6943: int g, i; rubidium@6943: CommandCost r; tron@3153: DiagDirection j; truelight@2381: TileIndex tile; rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_FIND_DEPOT); truelight@2381: rubidium@8229: _players_ainew[p->index].depot_tile = 0; truelight@2381: rubidium@8229: for (i=2;i<_players_ainew[p->index].path_info.route_length-2;i++) { rubidium@8229: tile = _players_ainew[p->index].path_info.route[i]; rubidium@5587: for (j = DIAGDIR_BEGIN; j < DIAGDIR_END; j++) { Darkvater@4559: TileIndex t = tile + TileOffsByDiagDir(j); tron@3153: frosch@8563: if (IsRoadDepotTile(t) && tron@3153: IsTileOwner(t, _current_player) && tron@3179: GetRoadDepotDirection(t) == ReverseDiagDir(j)) { rubidium@8229: _players_ainew[p->index].depot_tile = t; rubidium@8229: _players_ainew[p->index].depot_direction = ReverseDiagDir(j); rubidium@8229: _players_ainew[p->index].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 :) rubidium@8229: i = _players_ainew[p->index].path_info.route_length / 2; truelight@2381: g = 1; rubidium@8229: while (i > 1 && i < _players_ainew[p->index].path_info.route_length - 2) { truelight@2381: i += g; truelight@2381: g *= -1; truelight@2381: (g < 0?g--:g++); truelight@2381: rubidium@8229: if (_players_ainew[p->index].path_info.route_extra[i] != 0 || _players_ainew[p->index].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: rubidium@8229: tile = _players_ainew[p->index].path_info.route[i]; truelight@2381: rubidium@5587: for (j = DIAGDIR_BEGIN; j < DIAGDIR_END; j++) { Darkvater@4559: TileIndex t = tile + TileOffsByDiagDir(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 :) rubidium@8229: if (t == _players_ainew[p->index].path_info.route[i - 1] || rubidium@8229: t == _players_ainew[p->index].path_info.route[i + 1]) { truelight@2381: continue; tron@3153: } truelight@2381: // Not around a bridge? rubidium@8229: if (_players_ainew[p->index].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! rubidium@8229: _players_ainew[p->index].new_cost += r.GetCost(); rubidium@8229: _players_ainew[p->index].depot_tile = t; rubidium@8229: _players_ainew[p->index].depot_direction = ReverseDiagDir(j); // Reverse direction rubidium@8229: _players_ainew[p->index].state = AI_STATE_VERIFY_ROUTE; truelight@2381: return; truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: truelight@2381: // Failed to find a depot? rubidium@8229: _players_ainew[p->index].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: { rubidium@8229: if (_players_ainew[p->index].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? rubidium@8229: length = _players_ainew[p->index].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; rubidium@8229: } else if (_players_ainew[p->index].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? rubidium@8229: length = _players_ainew[p->index].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; rubidium@8229: if (_players_ainew[p->index].from_deliver) { rubidium@8229: max_cargo = GetIndustry(_players_ainew[p->index].from_ic)->last_month_production[0]; tron@4000: } else { rubidium@8229: max_cargo = GetIndustry(_players_ainew[p->index].to_ic)->last_month_production[0]; tron@4000: } truelight@2381: truelight@2381: // This is because moving 60% is more than we can dream of! rubidium@5587: max_cargo *= 6; rubidium@5587: max_cargo /= 10; 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; rubidium@8229: assert(_players_ainew[p->index].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 rubidium@8229: _players_ainew[p->index].path_info.position = -1; truelight@2381: truelight@2381: do { rubidium@8229: _players_ainew[p->index].path_info.position++; rubidium@8229: _players_ainew[p->index].new_cost += AiNew_Build_RoutePart(p, &_players_ainew[p->index].path_info, DC_QUERY_COST).GetCost(); rubidium@8229: } while (_players_ainew[p->index].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) { rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; truelight@2381: return; truelight@2381: } rubidium@8229: _players_ainew[p->index].amount_veh = res; rubidium@8229: _players_ainew[p->index].cur_veh = 0; truelight@2381: truelight@2381: // Check how much it it going to cost us.. truelight@2381: for (i=0;iindex].new_cost += AiNew_Build_Vehicle(p, 0, DC_QUERY_COST).GetCost(); 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! rubidium@8229: if (_players_ainew[p->index].new_cost > p->player_money - AI_MINIMUM_MONEY) { truelight@2381: // Too bad.. rubidium@8229: DEBUG(ai, 1, "Insufficient funds to build route (%" OTTD_PRINTF64 "d)", (int64)_players_ainew[p->index].new_cost); rubidium@8229: _players_ainew[p->index].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! rubidium@8229: if (_players_ainew[p->index].from_direction == AI_PATHFINDER_NO_DIRECTION) { rubidium@8229: _players_ainew[p->index].from_direction = AiNew_GetDirection(_players_ainew[p->index].path_info.route[_players_ainew[p->index].path_info.route_length - 1], _players_ainew[p->index].path_info.route[_players_ainew[p->index].path_info.route_length - 2]); truelight@2381: } rubidium@8229: if (_players_ainew[p->index].to_direction == AI_PATHFINDER_NO_DIRECTION) { rubidium@8229: _players_ainew[p->index].to_direction = AiNew_GetDirection(_players_ainew[p->index].path_info.route[0], _players_ainew[p->index].path_info.route[1]); rubidium@8229: } rubidium@8229: if (_players_ainew[p->index].from_tile == AI_STATION_RANGE) rubidium@8229: _players_ainew[p->index].from_tile = _players_ainew[p->index].path_info.route[_players_ainew[p->index].path_info.route_length - 1]; rubidium@8229: if (_players_ainew[p->index].to_tile == AI_STATION_RANGE) rubidium@8229: _players_ainew[p->index].to_tile = _players_ainew[p->index].path_info.route[0]; truelight@2381: rubidium@8229: _players_ainew[p->index].state = AI_STATE_BUILD_STATION; rubidium@8229: _players_ainew[p->index].temp = 0; truelight@2381: rubidium@8229: DEBUG(ai, 1, "The route is set and buildable, building 0x%X to 0x%X...", _players_ainew[p->index].from_tile, _players_ainew[p->index].to_tile); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // Build the stations truelight@2381: static void AiNew_State_BuildStation(Player *p) truelight@2381: { rubidium@6950: CommandCost res; rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_BUILD_STATION); rubidium@8229: if (_players_ainew[p->index].temp == 0) { rubidium@8229: if (!IsTileType(_players_ainew[p->index].from_tile, MP_STATION)) rubidium@8229: res = AiNew_Build_Station(p, _players_ainew[p->index].tbt, _players_ainew[p->index].from_tile, 0, 0, _players_ainew[p->index].from_direction, DC_EXEC); truelight@2381: } else { rubidium@8229: if (!IsTileType(_players_ainew[p->index].to_tile, MP_STATION)) rubidium@8229: res = AiNew_Build_Station(p, _players_ainew[p->index].tbt, _players_ainew[p->index].to_tile, 0, 0, _players_ainew[p->index].to_direction, DC_EXEC); rubidium@8229: _players_ainew[p->index].state = AI_STATE_BUILD_PATH; truelight@2381: } peter1138@2737: if (CmdFailed(res)) { rubidium@8229: DEBUG(ai, 0, "[BuildStation] station could not be built (0x%X)", _players_ainew[p->index].to_tile); rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; truelight@2381: // If the first station _was_ build, destroy it rubidium@8229: if (_players_ainew[p->index].temp != 0) rubidium@8229: AI_DoCommand(_players_ainew[p->index].from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@2381: return; truelight@2381: } rubidium@8229: _players_ainew[p->index].temp++; truelight@2381: } truelight@2381: truelight@2381: truelight@2381: // Build the path truelight@2381: static void AiNew_State_BuildPath(Player *p) truelight@2381: { rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_BUILD_PATH); rubidium@8229: // _players_ainew[p->index].temp is set to -1 when this function is called for the first time rubidium@8229: if (_players_ainew[p->index].temp == -1) { Darkvater@5380: DEBUG(ai, 1, "Starting to build new path"); truelight@2381: // Init the counter rubidium@8229: _players_ainew[p->index].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 ++) rubidium@8229: _players_ainew[p->index].path_info.position = -1; truelight@2381: // And don't do this again rubidium@8229: _players_ainew[p->index].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 :) rubidium@8229: if (--_players_ainew[p->index].counter != 0) return; rubidium@8229: _players_ainew[p->index].counter = (4 - _opt.diff.competitor_speed) * AI_BUILDPATH_PAUSE + 1; truelight@2381: truelight@2381: // Increase the building position rubidium@8229: _players_ainew[p->index].path_info.position++; truelight@2381: // Build route rubidium@8229: AiNew_Build_RoutePart(p, &_players_ainew[p->index].path_info, DC_EXEC); rubidium@8229: if (_players_ainew[p->index].path_info.position == -2) { truelight@2381: // This means we are done building! truelight@2381: rubidium@8229: if (_players_ainew[p->index].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 rubidium@7317: DiagDirection dir1, dir2, dir3; truelight@2381: TileIndex tile; rubidium@6943: CommandCost ret; rubidium@6943: for (int i = 0; i < 2; i++) { truelight@2381: if (i == 0) { rubidium@8229: tile = _players_ainew[p->index].from_tile + TileOffsByDiagDir(_players_ainew[p->index].from_direction); rubidium@8229: dir1 = ChangeDiagDir(_players_ainew[p->index].from_direction, DIAGDIRDIFF_90LEFT); rubidium@8229: dir2 = ChangeDiagDir(_players_ainew[p->index].from_direction, DIAGDIRDIFF_90RIGHT); rubidium@8229: dir3 = _players_ainew[p->index].from_direction; truelight@2381: } else { rubidium@8229: tile = _players_ainew[p->index].to_tile + TileOffsByDiagDir(_players_ainew[p->index].to_direction); rubidium@8229: dir1 = ChangeDiagDir(_players_ainew[p->index].to_direction, DIAGDIRDIFF_90LEFT); rubidium@8229: dir2 = ChangeDiagDir(_players_ainew[p->index].to_direction, DIAGDIRDIFF_90RIGHT); rubidium@8229: dir3 = _players_ainew[p->index].to_direction; truelight@2381: } truelight@2381: rubidium@7317: ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir1)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); rubidium@6946: if (CmdSucceeded(ret)) { rubidium@7317: TileIndex offset = TileOffsByDiagDir(dir1); rubidium@7317: if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, MP_TREES)) { rubidium@7317: ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); rubidium@6946: if (CmdSucceeded(ret)) { rubidium@7317: if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, MP_TREES)) rubidium@7317: AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: rubidium@7317: ret = AI_DoCommand(tile, DiagDirToRoadBits(ReverseDiagDir(dir2)), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); rubidium@6946: if (CmdSucceeded(ret)) { rubidium@7317: TileIndex offset = TileOffsByDiagDir(dir2); rubidium@7317: if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, MP_TREES)) { rubidium@7317: ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); rubidium@6946: if (CmdSucceeded(ret)) { rubidium@7317: if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, MP_TREES)) rubidium@7317: AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: rubidium@7317: ret = AI_DoCommand(tile, DiagDirToRoadBits(dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); rubidium@6946: if (CmdSucceeded(ret)) { rubidium@7317: TileIndex offset = TileOffsByDiagDir(dir3); rubidium@7317: if (IsTileType(tile + offset, MP_CLEAR) || IsTileType(tile + offset, MP_TREES)) { rubidium@7317: ret = AI_DoCommand(tile + offset, AiNew_GetRoadDirection(tile, tile + offset, tile + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); rubidium@6946: if (CmdSucceeded(ret)) { rubidium@7317: if (IsTileType(tile + offset + offset, MP_CLEAR) || IsTileType(tile + offset + offset, MP_TREES)) rubidium@7317: AI_DoCommand(tile + offset + offset, AiNew_GetRoadDirection(tile + offset, tile + offset + offset, tile + offset + offset + offset), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: } truelight@2381: rubidium@8229: DEBUG(ai, 1, "Finished building path, cost: %" OTTD_PRINTF64 "d", (int64)_players_ainew[p->index].new_cost); rubidium@8229: _players_ainew[p->index].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: { rubidium@6950: CommandCost res; rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_BUILD_DEPOT); truelight@2381: frosch@8563: if (IsRoadDepotTile(_players_ainew[p->index].depot_tile)) { rubidium@8229: if (IsTileOwner(_players_ainew[p->index].depot_tile, _current_player)) { tron@4000: // The depot is already built rubidium@8229: _players_ainew[p->index].state = AI_STATE_BUILD_VEHICLE; truelight@2381: return; truelight@2381: } else { truelight@2381: // There is a depot, but not of our team! :( rubidium@8229: _players_ainew[p->index].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) rubidium@8229: if (!EnsureNoVehicleOnGround(_players_ainew[p->index].depot_tile + TileOffsByDiagDir(_players_ainew[p->index].depot_direction))) truelight@2381: return; truelight@2381: rubidium@8229: res = AiNew_Build_Depot(p, _players_ainew[p->index].depot_tile, _players_ainew[p->index].depot_direction, DC_EXEC); peter1138@2737: if (CmdFailed(res)) { rubidium@8229: DEBUG(ai, 0, "[BuildDepot] depot could not be built (0x%X)", _players_ainew[p->index].depot_tile); rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; truelight@2381: return; truelight@2381: } truelight@2381: rubidium@8229: _players_ainew[p->index].state = AI_STATE_BUILD_VEHICLE; rubidium@8229: _players_ainew[p->index].idle = 10; rubidium@8229: _players_ainew[p->index].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: { rubidium@6943: CommandCost res; rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_BUILD_VEHICLE); truelight@2381: truelight@2381: // Check if we need to build a vehicle rubidium@8229: if (_players_ainew[p->index].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.. :) rubidium@8229: _players_ainew[p->index].state = AI_STATE_ACTION_DONE; truelight@2381: return; truelight@2381: } rubidium@8229: if (--_players_ainew[p->index].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! rubidium@8229: _players_ainew[p->index].idle = AI_BUILD_VEHICLE_TIME_BETWEEN; truelight@2381: truelight@2381: // Build the vehicle rubidium@8229: res = AiNew_Build_Vehicle(p, _players_ainew[p->index].depot_tile, DC_EXEC); peter1138@2737: if (CmdFailed(res)) { truelight@2381: // This happens when the AI can't build any more vehicles! rubidium@8229: _players_ainew[p->index].state = AI_STATE_NOTHING; truelight@2381: return; truelight@2381: } truelight@2381: // Increase the current counter rubidium@8229: _players_ainew[p->index].cur_veh++; truelight@2381: // Decrease the total counter rubidium@8229: _players_ainew[p->index].amount_veh--; truelight@2381: // Go give some orders! rubidium@8229: _players_ainew[p->index].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: rubidium@8229: assert(_players_ainew[p->index].state == AI_STATE_GIVE_ORDERS); truelight@2381: rubidium@8229: if (_players_ainew[p->index].veh_main_id != INVALID_VEHICLE) { rubidium@8229: AI_DoCommand(0, _players_ainew[p->index].veh_id + (_players_ainew[p->index].veh_main_id << 16), CO_SHARE, DC_EXEC, CMD_CLONE_ORDER); truelight@2682: rubidium@8229: _players_ainew[p->index].state = AI_STATE_START_VEHICLE; truelight@2381: return; truelight@2381: } else { rubidium@8229: _players_ainew[p->index].veh_main_id = _players_ainew[p->index].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; rubidium@8836: order.MakeGoToDepot(GetDepotByTile(_players_ainew[p->index].depot_tile)->index, true); rubidium@8839: AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER); truelight@2682: } truelight@2682: truelight@2682: idx = 0; rubidium@8836: order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].to_tile)); rubidium@8229: if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver) rubidium@8302: order.flags |= OFB_FULL_LOAD; rubidium@8839: AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER); truelight@2682: truelight@2381: idx = 0; rubidium@8836: order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].from_tile)); rubidium@8229: if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver) rubidium@8302: order.flags |= OFB_FULL_LOAD; rubidium@8839: AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER); truelight@2381: truelight@2381: // Start the engines! rubidium@8229: _players_ainew[p->index].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: { rubidium@8229: assert(_players_ainew[p->index].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.. rubidium@8229: if (_players_ainew[p->index].cur_veh & 1) rubidium@8229: AI_DoCommand(0, _players_ainew[p->index].veh_id, 1, DC_EXEC, CMD_SKIP_TO_ORDER); truelight@2682: truelight@2381: // 3, 2, 1... go! (give START_STOP command ;)) rubidium@8229: AI_DoCommand(0, _players_ainew[p->index].veh_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH); truelight@2381: // Try to build an other vehicle (that function will stop building when needed) rubidium@8229: _players_ainew[p->index].idle = 10; rubidium@8229: _players_ainew[p->index].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: { 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: } rubidium@8229: _players_ainew[p->index].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... smatz@8614: if (v->profit_last_year + v->profit_this_year < (Money)256 * AI_MINIMUM_ROUTE_PROFIT || truelight@2381: (v->reliability * 100 >> 16) < 40) { truelight@2381: // There is a possibility that the route is fucked up... rubidium@7010: if (v->cargo.DaysInTransit() > 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) { rubidium@6259: 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: { rubidium@6950: CommandCost ret; rubidium@6259: 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->owner != p->index) continue; truelight@2381: // Currently, we only know how to handle road-vehicles rubidium@6259: if (v->type != VEH_ROAD) continue; truelight@2381: truelight@2381: AiNew_CheckVehicle(p, v); truelight@2381: } truelight@2381: rubidium@8229: _players_ainew[p->index].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: { rubidium@8229: if (_ainew_state[_players_ainew[p->index].state] != NULL) rubidium@8229: _ainew_state[_players_ainew[p->index].state](p); truelight@2381: } truelight@2381: truelight@2381: truelight@2381: void AiNewDoGameLoop(Player *p) truelight@2381: { rubidium@8229: if (_players_ainew[p->index].state == AI_STATE_STARTUP) { truelight@2381: // The AI just got alive! rubidium@8229: _players_ainew[p->index].state = AI_STATE_FIRST_TIME; rubidium@8229: _players_ainew[p->index].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 rubidium@8229: _players_ainew[p->index].tick++; truelight@2381: truelight@2381: // If we come here, we can do a tick.. do so! truelight@2381: AiNew_OnTick(p); truelight@2381: }