truelight@110: /* truelight@110: * Next part is in Dutch, and only here for me, TrueLight, the maker of this new AI truelight@110: */ truelight@110: truelight@110: // TODO: als iemand een vehicle stil zet op een weg waar de AI wil bouwen truelight@110: // doet de AI helemaal niets meer truelight@110: // TODO: depot rondjes rijden stom iets dingus truelight@110: // TODO: jezelf afvragen of competitor_intelligence op niveau 2 wel meer geld moet opleverne... truelight@110: // TODO: als er iets in path komt, bouwt AI gewoon verder :( truelight@110: // TODO: mail routes truelight@110: truelight@110: /* truelight@110: * End of Dutch part truelight@110: */ truelight@110: truelight@110: #include "stdafx.h" truelight@110: #include "ttd.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" truelight@110: #include "command.h" truelight@110: #include "ai.h" truelight@110: #include "town.h" truelight@110: #include "industry.h" truelight@110: #include "station.h" truelight@110: #include "engine.h" truelight@110: #include "gui.h" truelight@110: truelight@110: // This function is called after StartUp. It is the init of an AI truelight@110: static void AiNew_State_FirstTime(Player *p) { truelight@110: // This assert is used to protect those function from misuse truelight@110: // You have quickly a small mistake in the state-array truelight@110: // With that, everything would go wrong. Finding that, is almost impossible truelight@110: // With this assert, that problem can never happen. truelight@110: assert(p->ainew.state == AI_STATE_FIRST_TIME); truelight@110: // We first have to init some things truelight@145: truelight@110: if (_current_player == 1) { truelight@110: ShowErrorMessage(-1, TEMP_AI_IN_PROGRESS, 0, 0); truelight@110: } 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! truelight@110: p->ainew.path_info.start_tile_tl = 0; truelight@110: p->ainew.path_info.start_tile_br = 0; truelight@110: p->ainew.path_info.end_tile_tl = 0; truelight@110: 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: 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? truelight@110: static void AiNew_State_Nothing(Player *p) { truelight@110: assert(p->ainew.state == AI_STATE_NOTHING); truelight@110: // If we are done idling, start over again darkvater@165: if (p->ainew.idle == 0) p->ainew.idle = 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: 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 truelight@110: static void AiNew_State_WakeUp(Player *p) { truelight@110: int32 money; truelight@110: int c; truelight@110: 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@110: while (!AiNew_Build_CompanyHQ(p, (Random()&0xFFFF))) { } 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@110: c = Random() & 0xFF; truelight@110: if (p->current_loan > 0 && p->old_economy[1].income > AI_MINIMUM_INCOME_FOR_LOAN && truelight@110: c < 10) { truelight@110: 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? truelight@110: if (GetFreeUnitNumber(VEH_Road) <= _patches.max_roadveh) { truelight@145: if (c < 85) p->ainew.action = AI_ACTION_TRUCK_ROUTE; truelight@110: else p->ainew.action = AI_ACTION_BUS_ROUTE; truelight@110: } truelight@110: }/* else if (c < 200 && !_patches.ai_disable_veh_train) { truelight@110: if (GetFreeUnitNumber(VEH_Train) <= _patches.max_trains) { truelight@110: p->ainew.action = AI_ACTION_TRAIN_ROUTE; truelight@110: } truelight@110: }*/ 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 && ( truelight@110: p->ainew.action == AI_ACTION_BUS_ROUTE || p->ainew.action == AI_ACTION_TRUCK_ROUTE)) { truelight@110: p->ainew.action = AI_ACTION_NONE; truelight@110: return; truelight@110: } truelight@145: truelight@110: if (_patches.ai_disable_veh_roadveh && ( truelight@110: p->ainew.action == AI_ACTION_BUS_ROUTE || p->ainew.action == AI_ACTION_TRUCK_ROUTE)) { truelight@110: p->ainew.action = AI_ACTION_NONE; truelight@110: return; truelight@110: } truelight@110: truelight@110: if (p->ainew.action == AI_ACTION_REPAY_LOAN && 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.. truelight@110: if (p->ainew.action == AI_ACTION_BUS_ROUTE && money > AI_MINIMUM_BUS_ROUTE_MONEY) { truelight@110: 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: } truelight@110: if (p->ainew.action == AI_ACTION_TRUCK_ROUTE && money > AI_MINIMUM_TRUCK_ROUTE_MONEY) { truelight@110: 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: truelight@110: static void AiNew_State_ActionDone(Player *p) { truelight@110: p->ainew.action = AI_ACTION_NONE; truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: } truelight@110: truelight@110: // Check if a city or industry is good enough to start a route there truelight@110: static bool AiNew_Check_City_or_Industry(Player *p, int ic, byte type) { truelight@110: if (type == AI_CITY) { truelight@919: Town *t = GetTown(ic); truelight@110: Station *st; truelight@110: int 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 truelight@110: if (t->ratings[_current_player] < 0 && CHANCE16(1,4)) return false; truelight@145: truelight@110: if (t->max_pass - t->act_pass < AI_CHECKCITY_NEEDED_CARGO && !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: // Is it an active station truelight@110: if (st->xy == 0) continue; 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 truelight@110: if (GetTileDist(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@110: if (j == 2 && 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) { truelight@919: Industry *i = GetIndustry(ic); truelight@110: Station *st; truelight@110: int count = 0; truelight@110: int j = 0; truelight@145: truelight@110: if (i->town != NULL && i->town->ratings[_current_player] < 0 && 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 truelight@110: if (i->produced_cargo[0] == 0xFF || 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: // Is it an active station truelight@110: if (st->xy == 0) continue; truelight@110: 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 truelight@110: if (i->produced_cargo[0] == 0xFF) continue; truelight@110: // It does not take this cargo truelight@110: if (!st->goods[i->produced_cargo[0]].last_speed) continue; truelight@110: // Is it around our industry truelight@110: if (GetTileDist(st->xy, i->xy) > 5) continue; truelight@110: // It does take this cargo.. what is his rating? truelight@110: if (st->goods[i->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@110: if (j == 1 && 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@110: // This functions tries to locate a good route truelight@110: static void AiNew_State_LocateRoute(Player *p) { truelight@110: assert(p->ainew.state == AI_STATE_LOCATE_ROUTE); truelight@110: // For now, we only support PASSENGERS, CITY and BUSSES truelight@145: truelight@110: // We don't have a route yet truelight@110: if (p->ainew.cargo == AI_NEED_CARGO) { truelight@110: p->ainew.new_cost = 0; // No cost yet truelight@110: p->ainew.temp = -1; truelight@110: // Reset the counter truelight@110: p->ainew.counter = 0; truelight@145: truelight@110: p->ainew.from_ic = -1; truelight@110: p->ainew.to_ic = -1; truelight@110: if (p->ainew.tbt == AI_BUS) { truelight@110: // For now we only have a passenger route truelight@110: p->ainew.cargo = CT_PASSENGERS; truelight@110: truelight@110: // Find a route to cities truelight@110: p->ainew.from_type = AI_CITY; truelight@110: p->ainew.to_type = AI_CITY; truelight@110: } else if (p->ainew.tbt == AI_TRUCK) { truelight@110: p->ainew.cargo = AI_NO_CARGO; truelight@110: truelight@110: p->ainew.from_type = AI_INDUSTRY; truelight@110: p->ainew.to_type = AI_INDUSTRY; truelight@110: } truelight@110: truelight@110: // Now we are doing initing, we wait one tick truelight@110: return; truelight@110: } truelight@145: truelight@110: // Increase the counter and abort if it is taking too long! truelight@110: p->ainew.counter++; truelight@110: if (p->ainew.counter > AI_LOCATE_ROUTE_MAX_COUNTER) { truelight@110: // Switch back to doing nothing! truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return; truelight@110: } truelight@145: truelight@110: // We are going to locate a city from where we are going to connect truelight@110: if (p->ainew.from_ic == -1) { truelight@110: if (p->ainew.temp == -1) { truelight@110: // First, we pick a random spot to search from truelight@110: if (p->ainew.from_type == AI_CITY) truelight@110: p->ainew.temp = RandomRange(_total_towns); truelight@110: else truelight@110: p->ainew.temp = RandomRange(_total_industries); truelight@110: } truelight@145: truelight@110: if (!AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.from_type)) { truelight@110: // It was not a valid city truelight@110: // increase the temp with one, and return. We will come back later here truelight@110: // to try again truelight@110: p->ainew.temp++; truelight@110: if (p->ainew.from_type == AI_CITY) { truelight@110: if (p->ainew.temp >= _total_towns) p->ainew.temp = 0; truelight@110: } else { truelight@110: if (p->ainew.temp >= _total_industries) p->ainew.temp = 0; truelight@110: } truelight@145: truelight@110: // Don't do an attempt if we are trying the same id as the last time... truelight@110: if (p->ainew.last_id == p->ainew.temp) return; truelight@110: p->ainew.last_id = p->ainew.temp; truelight@145: truelight@110: return; truelight@110: } truelight@145: truelight@110: // We found a good city/industry, save the data of it truelight@110: p->ainew.from_ic = p->ainew.temp; truelight@145: truelight@110: // Start the next tick with finding a to-city truelight@110: p->ainew.temp = -1; truelight@110: return; truelight@110: } truelight@145: truelight@110: // Find a to-city truelight@110: if (p->ainew.temp == -1) { truelight@110: // First, we pick a random spot to search to truelight@110: if (p->ainew.to_type == AI_CITY) truelight@110: p->ainew.temp = RandomRange(_total_towns); truelight@110: else truelight@110: p->ainew.temp = RandomRange(_total_industries); truelight@110: } truelight@145: truelight@110: // The same city is not allowed truelight@110: // Also check if the city is valid truelight@110: if (p->ainew.temp != p->ainew.from_ic && AiNew_Check_City_or_Industry(p, p->ainew.temp, p->ainew.to_type)) { truelight@110: // Maybe it is valid.. truelight@145: truelight@110: // We need to know if they are not to far apart from eachother.. truelight@110: // We do that by checking how much cargo we have to move and how long the route truelight@110: // is. truelight@145: truelight@110: if (p->ainew.from_type == AI_CITY && p->ainew.tbt == AI_BUS) { truelight@919: int max_cargo = GetTown(p->ainew.from_ic)->max_pass + GetTown(p->ainew.temp)->max_pass; truelight@919: max_cargo -= GetTown(p->ainew.from_ic)->act_pass + GetTown(p->ainew.temp)->act_pass; truelight@110: // max_cargo is now the amount of cargo we can move between the two cities miham@826: // If it is more than the distance, we allow it truelight@919: if (GetTileDist(GetTown(p->ainew.from_ic)->xy, GetTown(p->ainew.temp)->xy) <= max_cargo * AI_LOCATEROUTE_BUS_CARGO_DISTANCE) { truelight@110: // We found a good city/industry, save the data of it truelight@110: p->ainew.to_ic = p->ainew.temp; truelight@110: p->ainew.state = AI_STATE_FIND_STATION; truelight@110: truelight@919: DEBUG(ai,1)("[AiNew - LocateRoute] Found bus-route of %d tiles long (from %d to %d)",GetTileDist(GetTown(p->ainew.from_ic)->xy, GetTown(p->ainew.temp)->xy), p->ainew.from_ic, p->ainew.temp); truelight@110: truelight@110: p->ainew.from_tile = 0; truelight@110: p->ainew.to_tile = 0; truelight@145: truelight@110: return; truelight@110: } truelight@110: } else if (p->ainew.tbt == AI_TRUCK) { truelight@110: bool found = false; truelight@110: int max_cargo = 0; truelight@110: int i; truelight@110: // TODO: in max_cargo, also check other cargo (beside [0]) truelight@110: // First we check if the from_ic produces cargo that this ic accepts truelight@919: if (GetIndustry(p->ainew.from_ic)->produced_cargo[0] != 0xFF && GetIndustry(p->ainew.from_ic)->total_production[0] != 0) { truelight@110: for (i=0;i<3;i++) { truelight@919: if (GetIndustry(p->ainew.temp)->accepts_cargo[i] == 0xFF) break; truelight@919: if (GetIndustry(p->ainew.from_ic)->produced_cargo[0] == GetIndustry(p->ainew.temp)->accepts_cargo[i]) { truelight@110: // Found a compatbiel industry truelight@919: max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0] - GetIndustry(p->ainew.from_ic)->total_transported[0]; truelight@110: found = true; truelight@110: p->ainew.from_deliver = true; truelight@110: p->ainew.to_deliver = false; truelight@110: break; truelight@110: } truelight@110: } truelight@110: } truelight@919: if (!found && GetIndustry(p->ainew.temp)->produced_cargo[0] != 0xFF && GetIndustry(p->ainew.temp)->total_production[0] != 0) { truelight@110: // If not check if the current ic produces cargo that the from_ic accepts truelight@110: for (i=0;i<3;i++) { truelight@919: if (GetIndustry(p->ainew.from_ic)->accepts_cargo[i] == 0xFF) break; truelight@919: if (GetIndustry(p->ainew.temp)->produced_cargo[0] == GetIndustry(p->ainew.from_ic)->accepts_cargo[i]) { truelight@110: // Found a compatbiel industry truelight@110: found = true; truelight@919: max_cargo = GetIndustry(p->ainew.temp)->total_production[0] - GetIndustry(p->ainew.from_ic)->total_transported[0]; truelight@110: p->ainew.from_deliver = false; truelight@110: p->ainew.to_deliver = true; truelight@110: break; truelight@110: } truelight@110: } truelight@110: } truelight@110: if (found) { truelight@110: // Yeah, they are compatible!!! truelight@110: // Check the length against the amount of goods truelight@919: if (GetTileDist(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy) > AI_LOCATEROUTE_TRUCK_MIN_DISTANCE && truelight@919: GetTileDist(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy) <= max_cargo * AI_LOCATEROUTE_TRUCK_CARGO_DISTANCE) { truelight@110: p->ainew.to_ic = p->ainew.temp; truelight@110: if (p->ainew.from_deliver) { truelight@919: p->ainew.cargo = GetIndustry(p->ainew.from_ic)->produced_cargo[0]; truelight@110: } else { truelight@919: p->ainew.cargo = GetIndustry(p->ainew.temp)->produced_cargo[0]; truelight@110: } truelight@110: p->ainew.state = AI_STATE_FIND_STATION; truelight@110: truelight@919: DEBUG(ai,1)("[AiNew - LocateRoute] Found truck-route of %d tiles long (from %d to %d)",GetTileDist(GetIndustry(p->ainew.from_ic)->xy, GetIndustry(p->ainew.temp)->xy), p->ainew.from_ic, p->ainew.temp); truelight@110: truelight@110: p->ainew.from_tile = 0; truelight@110: p->ainew.to_tile = 0; truelight@110: truelight@110: return; truelight@110: } truelight@110: } truelight@110: } truelight@110: } truelight@110: truelight@110: // It was not a valid city truelight@110: // increase the temp with one, and return. We will come back later here truelight@110: // to try again truelight@110: p->ainew.temp++; truelight@110: if (p->ainew.to_type == AI_CITY) { truelight@110: if (p->ainew.temp >= _total_towns) p->ainew.temp = 0; truelight@110: } else { truelight@110: if (p->ainew.temp >= _total_industries) p->ainew.temp = 0; truelight@110: } truelight@145: truelight@110: // Don't do an attempt if we are trying the same id as the last time... truelight@110: if (p->ainew.last_id == p->ainew.temp) return; truelight@110: p->ainew.last_id = p->ainew.temp; truelight@110: } truelight@110: 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 ;) truelight@110: static bool AiNew_CheckVehicleStation(Player *p, Station *st) { 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) { truelight@1024: if (order->type == OT_GOTO_STATION && GetStation(order->station) == 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 truelight@110: static void AiNew_State_FindStation(Player *p) { truelight@110: TileIndex tile; truelight@110: Station *st; truelight@110: int i, count = 0; truelight@110: TileIndex new_tile = 0; truelight@110: byte direction = 0; truelight@110: Town *town = NULL; truelight@110: Industry *industry = NULL; truelight@110: assert(p->ainew.state == AI_STATE_FIND_STATION); truelight@145: truelight@110: if (p->ainew.from_tile == 0) { truelight@110: // First we scan for a station in the from-city truelight@110: if (p->ainew.from_type == AI_CITY) { truelight@919: town = GetTown(p->ainew.from_ic); truelight@110: tile = town->xy; truelight@110: } else { truelight@919: industry = GetIndustry(p->ainew.from_ic); truelight@110: tile = industry->xy; truelight@110: } truelight@110: } else if (p->ainew.to_tile == 0) { truelight@110: // Second we scan for a station in the to-city truelight@110: if (p->ainew.to_type == AI_CITY) { truelight@919: town = GetTown(p->ainew.to_ic); truelight@110: tile = town->xy; truelight@110: } else { truelight@919: industry = GetIndustry(p->ainew.to_ic); truelight@110: tile = industry->xy; truelight@110: } truelight@110: } else { truelight@110: // Unsupported request truelight@110: // Go to FIND_PATH truelight@110: p->ainew.temp = -1; truelight@110: p->ainew.state = AI_STATE_FIND_PATH; truelight@110: return; truelight@110: } truelight@110: truelight@110: // First, we are going to look at the stations that already exist inside the city truelight@110: // If there is enough cargo left in the station, we take that station miham@826: // 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... truelight@110: if (i == -1) { p->ainew.state = AI_STATE_NOTHING; return; } truelight@110: truelight@110: FOR_ALL_STATIONS(st) { truelight@110: if (st->xy != 0) { truelight@110: if (st->owner == _current_player) { truelight@110: if (p->ainew.tbt == AI_BUS && (FACIL_BUS_STOP & st->facilities) == FACIL_BUS_STOP) { truelight@110: if (st->town == town) { truelight@110: // Check how much cargo there is left in the station tron@538: if ((st->goods[p->ainew.cargo].waiting_acceptance & 0xFFF) > RoadVehInfo(i)->capacity * AI_STATION_REUSE_MULTIPLER) { truelight@110: if (AiNew_CheckVehicleStation(p, st)) { truelight@110: // We did found a station that was good enough! truelight@110: new_tile = st->xy; truelight@110: // Cheap way to get the direction of the station... truelight@110: // Bus stations save it as 0x47 .. 0x4A, so decrease it with 0x47, and tada! truelight@110: direction = _map5[st->xy] - 0x47; truelight@110: break; truelight@110: } truelight@110: } truelight@110: count++; truelight@110: } 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: truelight@110: if (new_tile == 0 && p->ainew.tbt == AI_BUS) { truelight@110: uint x, y, i = 0; truelight@110: int r; truelight@110: uint best; truelight@110: uint accepts[NUM_CARGO]; truelight@110: TileIndex found_spot[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4]; truelight@110: uint found_best[AI_FINDSTATION_TILE_RANGE*AI_FINDSTATION_TILE_RANGE*4]; truelight@110: // To find a good spot we scan a range from the center, a get the point truelight@110: // where we get the most cargo and where it is buildable. truelight@110: // TODO: also check for station of myself and make sure we are not truelight@110: // taking eachothers passangers away (bad result when it does not) tron@926: for (x = TileX(tile) - AI_FINDSTATION_TILE_RANGE; x <= TileX(tile) + AI_FINDSTATION_TILE_RANGE; x++) { tron@926: for (y = TileY(tile) - AI_FINDSTATION_TILE_RANGE; y <= TileY(tile) + AI_FINDSTATION_TILE_RANGE; y++) { truelight@110: new_tile = TILE_XY(x,y); tron@1035: if (IsTileType(new_tile, MP_CLEAR) || IsTileType(new_tile, MP_TREES)) { truelight@110: // This tile we can build on! truelight@110: // Check acceptance Celestar@568: // XXX - Get the catchment area Celestar@568: GetAcceptanceAroundTiles(accepts, new_tile, 1, 1, 4); truelight@110: // >> 3 == 0 means no cargo truelight@110: if (accepts[p->ainew.cargo] >> 3 == 0) continue; truelight@110: // See if we can build the station truelight@110: r = AiNew_Build_Station(p, p->ainew.tbt, new_tile, 0, 0, 0, DC_QUERY_COST); truelight@110: if (r == CMD_ERROR) continue; truelight@110: // We can build it, so add it to found_spot truelight@110: found_spot[i] = new_tile; truelight@110: found_best[i++] = accepts[p->ainew.cargo]; truelight@110: } truelight@110: } truelight@110: } truelight@110: truelight@110: // If i is still zero, we did not found anything :( truelight@110: if (i == 0) { truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return; truelight@110: } truelight@110: truelight@110: // Go through all the found_best and check which has the highest value truelight@110: best = 0; truelight@110: new_tile = 0; truelight@110: truelight@110: for (x=0;x best || truelight@110: (found_best[x] == best && GetTileDist(tile, new_tile) > GetTileDist(tile, found_spot[x]))) { truelight@110: new_tile = found_spot[x]; truelight@110: best = found_best[x]; truelight@110: } truelight@110: } 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) { truelight@110: p->ainew.from_tile = new_tile; truelight@110: p->ainew.from_direction = direction; truelight@110: // Now we found thisone, go in for to_tile truelight@110: return; truelight@110: } else if (p->ainew.to_tile == 0) { truelight@110: p->ainew.to_tile = new_tile; truelight@110: p->ainew.to_direction = direction; truelight@110: // K, done placing stations! truelight@110: p->ainew.temp = -1; truelight@110: p->ainew.state = AI_STATE_FIND_PATH; truelight@110: return; truelight@110: } truelight@110: } truelight@110: truelight@110: // We try to find a path between 2 points truelight@110: static void AiNew_State_FindPath(Player *p) { truelight@110: int r; truelight@110: assert(p->ainew.state == AI_STATE_FIND_PATH); truelight@145: truelight@110: // First time, init some data truelight@110: if (p->ainew.temp == -1) { truelight@110: // Init path_info truelight@110: if (p->ainew.from_tile == AI_STATION_RANGE) { truelight@110: // For truck routes we take a range around the industry truelight@919: p->ainew.path_info.start_tile_tl = GetIndustry(p->ainew.from_ic)->xy - TILE_XY(1,1); truelight@919: p->ainew.path_info.start_tile_br = GetIndustry(p->ainew.from_ic)->xy + TILE_XY(GetIndustry(p->ainew.from_ic)->width, GetIndustry(p->ainew.from_ic)->height) + TILE_XY(1,1); truelight@110: p->ainew.path_info.start_direction = p->ainew.from_direction; truelight@110: } else { truelight@110: p->ainew.path_info.start_tile_tl = p->ainew.from_tile; truelight@110: p->ainew.path_info.start_tile_br = p->ainew.from_tile; truelight@110: p->ainew.path_info.start_direction = p->ainew.from_direction; truelight@110: } truelight@110: truelight@110: if (p->ainew.to_tile == AI_STATION_RANGE) { truelight@919: p->ainew.path_info.end_tile_tl = GetIndustry(p->ainew.to_ic)->xy - TILE_XY(1,1); truelight@919: p->ainew.path_info.end_tile_br = GetIndustry(p->ainew.to_ic)->xy + TILE_XY(GetIndustry(p->ainew.to_ic)->width, GetIndustry(p->ainew.to_ic)->height) + TILE_XY(1,1); truelight@110: p->ainew.path_info.end_direction = p->ainew.to_direction; truelight@110: } else { truelight@110: p->ainew.path_info.end_tile_tl = p->ainew.to_tile; truelight@110: p->ainew.path_info.end_tile_br = p->ainew.to_tile; truelight@110: p->ainew.path_info.end_direction = p->ainew.to_direction; truelight@110: } truelight@145: truelight@110: if (p->ainew.tbt == AI_TRAIN) truelight@110: p->ainew.path_info.rail_or_road = true; truelight@110: else truelight@110: p->ainew.path_info.rail_or_road = false; truelight@145: truelight@110: // First, clean the pathfinder with our new begin and endpoints truelight@110: clean_AyStar_AiPathFinder(p->ainew.pathfinder, &p->ainew.path_info); truelight@145: truelight@110: p->ainew.temp = 0; truelight@110: } truelight@145: truelight@110: // Start the pathfinder truelight@110: r = p->ainew.pathfinder->main(p->ainew.pathfinder); truelight@110: // If it return: no match, stop it... truelight@110: if (r == AYSTAR_NO_PATH) { truelight@110: DEBUG(ai,1)("[AiNew] PathFinder found no route!"); truelight@110: // Start all over again... truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return; truelight@110: } truelight@110: if (r == AYSTAR_FOUND_END_NODE) { truelight@110: // We found the end-point truelight@110: p->ainew.temp = -1; truelight@110: p->ainew.state = AI_STATE_FIND_DEPOT; truelight@110: return; truelight@110: } truelight@110: // In any other case, we are still busy finding the route... truelight@110: } truelight@110: truelight@110: // This function tries to locate a good place for a depot! truelight@110: static void AiNew_State_FindDepot(Player *p) { truelight@110: // 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@110: // Simple, easy, works! truelight@110: // To make the depot stand in the middle of the route, we start from the center.. truelight@110: // But first we walk through the route see if we can find a depot that is ours truelight@110: // this keeps things nice ;) truelight@110: int g, i, j, r; 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]; tron@900: for (j = 0; j < 4; j++) { tron@1035: if (IsTileType(tile + TileOffsByDir(j), MP_STREET)) { truelight@110: // Its a street, test if it is a depot tron@900: if (_map5[tile + TileOffsByDir(j)] & 0x20) { truelight@110: // We found a depot, is it ours? (TELL ME!!!) tron@900: if (_map_owner[tile + TileOffsByDir(j)] == _current_player) { truelight@110: // Now, is it pointing to the right direction......... tron@900: if ((_map5[tile + TileOffsByDir(j)] & 3) == (j ^ 2)) { truelight@110: // Yeah!!! tron@900: p->ainew.depot_tile = tile + TileOffsByDir(j); truelight@110: p->ainew.depot_direction = j ^ 2; // Reverse direction truelight@110: p->ainew.state = AI_STATE_VERIFY_ROUTE; truelight@110: return; truelight@110: } truelight@110: } truelight@110: } 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: tron@900: for (j = 0; j < 4; j++) { 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@900: if (tile + TileOffsByDir(j) == p->ainew.path_info.route[i-1] || tron@900: tile + TileOffsByDir(j) == p->ainew.path_info.route[i+1]) continue; 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@1035: if (IsTileType(tile + TileOffsByDir(j), MP_CLEAR) || tron@1035: IsTileType(tile + TileOffsByDir(j), MP_TREES)) { truelight@110: TileInfo ti; truelight@110: FindLandscapeHeightByTile(&ti, tile); truelight@110: // If the current tile is on a slope (tileh != 0) then we do not allow this truelight@110: if (ti.tileh != 0) continue; truelight@110: // Check if everything went okay.. tron@900: r = AiNew_Build_Depot(p, tile + TileOffsByDir(j), j ^ 2, 0); truelight@110: if (r == CMD_ERROR) continue; truelight@110: // Found a spot! truelight@110: p->ainew.new_cost += r; tron@900: p->ainew.depot_tile = tile + TileOffsByDir(j); truelight@110: p->ainew.depot_direction = j ^ 2; // 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 truelight@110: static int AiNew_HowManyVehicles(Player *p) { truelight@110: if (p->ainew.tbt == AI_BUS) { truelight@110: // For bus-routes we look at the time before we are back in the station truelight@110: int i, length, tiles_a_day; truelight@110: int amount; truelight@110: i = AiNew_PickVehicle(p); truelight@110: if (i == -1) return 0; truelight@110: // Passenger run.. how long is the route? truelight@110: length = p->ainew.path_info.route_length; truelight@110: // Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! tron@538: tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; truelight@110: // We want a vehicle in a station once a month at least, so, calculate it! truelight@110: // (the * 2 is because we have 2 stations ;)) truelight@543: amount = length * 2 * 2 / tiles_a_day / 30; truelight@110: if (amount == 0) amount = 1; truelight@110: return amount; truelight@110: } else if (p->ainew.tbt == AI_TRUCK) { truelight@110: // For truck-routes we look at the cargo truelight@110: int i, length, amount, tiles_a_day; truelight@110: int max_cargo; truelight@110: i = AiNew_PickVehicle(p); truelight@110: if (i == -1) return 0; truelight@110: // Passenger run.. how long is the route? truelight@110: length = p->ainew.path_info.route_length; truelight@110: // Calculating tiles a day a vehicle moves is not easy.. this is how it must be done! tron@538: tiles_a_day = RoadVehInfo(i)->max_speed * DAY_TICKS / 256 / 16; truelight@110: if (p->ainew.from_deliver) truelight@919: max_cargo = GetIndustry(p->ainew.from_ic)->total_production[0]; truelight@110: else truelight@919: max_cargo = GetIndustry(p->ainew.to_ic)->total_production[0]; truelight@145: miham@826: // This is because moving 60% is more than we can dream of! truelight@110: max_cargo *= 0.6; truelight@110: // We want all the cargo to be gone in a month.. so, we know the cargo it delivers truelight@110: // we know what the vehicle takes with him, and we know the time it takes him truelight@110: // to get back here.. now let's do some math! truelight@543: amount = 2 * length * max_cargo / tiles_a_day / 30 / RoadVehInfo(i)->capacity; truelight@110: amount += 1; truelight@110: 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 truelight@110: static void AiNew_State_VerifyRoute(Player *p) { truelight@110: int res, i; truelight@110: assert(p->ainew.state == AI_STATE_VERIFY_ROUTE); truelight@145: truelight@110: // Let's calculate the cost of the path.. truelight@110: // new_cost already contains the cost of the stations truelight@110: p->ainew.path_info.position = -1; truelight@145: truelight@110: do { truelight@110: p->ainew.path_info.position++; truelight@110: 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.. truelight@110: DEBUG(ai,1)("[AiNew] Can't pay for this 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) { truelight@110: 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) truelight@110: 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: truelight@110: DEBUG(ai,1)("[AiNew] The route is set and buildable.. going to build it!"); truelight@110: } truelight@110: truelight@110: // Build the stations truelight@110: static void AiNew_State_BuildStation(Player *p) { truelight@110: int res = 0; truelight@110: assert(p->ainew.state == AI_STATE_BUILD_STATION); truelight@110: if (p->ainew.temp == 0) { tron@1035: if (!IsTileType(p->ainew.from_tile, MP_STATION)) truelight@110: res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.from_tile, 0, 0, p->ainew.from_direction, DC_EXEC); truelight@110: } truelight@110: else { tron@1035: if (!IsTileType(p->ainew.to_tile, MP_STATION)) truelight@110: res = AiNew_Build_Station(p, p->ainew.tbt, p->ainew.to_tile, 0, 0, p->ainew.to_direction, DC_EXEC); truelight@110: p->ainew.state = AI_STATE_BUILD_PATH; truelight@110: } truelight@110: if (res == CMD_ERROR) { truelight@110: DEBUG(ai,0)("[AiNew - BuildStation] Strange but true... station can not be build!"); 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@110: DoCommandByTile(p->ainew.from_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@110: return; truelight@110: } truelight@110: p->ainew.temp++; truelight@110: } truelight@110: truelight@110: // Build the path truelight@110: static void AiNew_State_BuildPath(Player *p) { 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) { truelight@110: DEBUG(ai,1)("[AiNew] Starting to build the 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) { truelight@110: static const byte _roadbits_by_dir[4] = {2,1,8,4}; truelight@110: // If they not queue, they have to go up and down to try again at a station... truelight@110: // We don't want that, so try building some road left or right of the station truelight@145: int dir1, dir2, dir3; truelight@110: TileIndex tile; truelight@110: int i, r; truelight@110: for (i=0;i<2;i++) { truelight@110: if (i == 0) { tron@900: tile = p->ainew.from_tile + TileOffsByDir(p->ainew.from_direction); truelight@110: dir1 = p->ainew.from_direction - 1; truelight@110: if (dir1 < 0) dir1 = 3; truelight@110: dir2 = p->ainew.from_direction + 1; truelight@110: if (dir2 > 3) dir2 = 0; truelight@110: dir3 = p->ainew.from_direction; truelight@110: } else { tron@900: tile = p->ainew.to_tile + TileOffsByDir(p->ainew.to_direction); truelight@110: dir1 = p->ainew.to_direction - 1; truelight@110: if (dir1 < 0) dir1 = 3; truelight@110: dir2 = p->ainew.to_direction + 1; truelight@110: if (dir2 > 3) dir2 = 0; truelight@110: dir3 = p->ainew.to_direction; truelight@110: } truelight@145: truelight@146: r = DoCommandByTile(tile, _roadbits_by_dir[dir1], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@146: if (r != CMD_ERROR) { tron@900: dir1 = TileOffsByDir(dir1); tron@1035: if (IsTileType(tile + dir1, MP_CLEAR) || IsTileType(tile + dir1, MP_TREES)) { truelight@146: r = DoCommandByTile(tile+dir1, AiNew_GetRoadDirection(tile, tile+dir1, tile+dir1+dir1), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@146: if (r != CMD_ERROR) { tron@1035: if (IsTileType(tile + dir1 + dir1, MP_CLEAR) || IsTileType(tile + dir1 + dir1, MP_TREES)) truelight@146: DoCommandByTile(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: truelight@146: r = DoCommandByTile(tile, _roadbits_by_dir[dir2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@146: if (r != CMD_ERROR) { tron@900: dir2 = TileOffsByDir(dir2); tron@1035: if (IsTileType(tile + dir2, MP_CLEAR) || IsTileType(tile + dir2, MP_TREES)) { truelight@146: r = DoCommandByTile(tile+dir2, AiNew_GetRoadDirection(tile, tile+dir2, tile+dir2+dir2), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@146: if (r != CMD_ERROR) { tron@1035: if (IsTileType(tile + dir2 + dir2, MP_CLEAR) || IsTileType(tile + dir2 + dir2, MP_TREES)) truelight@146: DoCommandByTile(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: truelight@146: r = DoCommandByTile(tile, _roadbits_by_dir[dir3^2], 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@146: if (r != CMD_ERROR) { tron@900: dir3 = TileOffsByDir(dir3); tron@1035: if (IsTileType(tile + dir3, MP_CLEAR) || IsTileType(tile + dir3, MP_TREES)) { truelight@146: r = DoCommandByTile(tile+dir3, AiNew_GetRoadDirection(tile, tile+dir3, tile+dir3+dir3), 0, DC_EXEC | DC_NO_WATER, CMD_BUILD_ROAD); truelight@146: if (r != CMD_ERROR) { tron@1035: if (IsTileType(tile + dir3 + dir3, MP_CLEAR) || IsTileType(tile + dir3 + dir3, MP_TREES)) truelight@146: DoCommandByTile(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: } truelight@110: } truelight@110: } truelight@145: truelight@145: truelight@110: DEBUG(ai,1)("[AiNew] Done building the path (cost: %d)", p->ainew.new_cost); truelight@110: p->ainew.state = AI_STATE_BUILD_DEPOT; truelight@110: } truelight@110: } truelight@110: truelight@110: // Builds the depot truelight@110: static void AiNew_State_BuildDepot(Player *p) { truelight@110: int res = 0; truelight@110: assert(p->ainew.state == AI_STATE_BUILD_DEPOT); truelight@145: tron@1035: if (IsTileType(p->ainew.depot_tile, MP_STREET) && _map5[p->ainew.depot_tile] & 0x20) { truelight@110: if (_map_owner[p->ainew.depot_tile] == _current_player) { truelight@110: // The depot is already builded! 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) tron@900: if (!EnsureNoVehicle(p->ainew.depot_tile + TileOffsByDir(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); truelight@110: if (res == CMD_ERROR) { truelight@110: DEBUG(ai,0)("[AiNew - BuildDepot] Strange but true... depot can not be build!"); truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return; truelight@110: } truelight@145: truelight@110: p->ainew.state = AI_STATE_BUILD_VEHICLE; truelight@110: p->ainew.idle = 1; truelight@110: p->ainew.veh_main_id = (VehicleID)-1; truelight@110: } truelight@110: truelight@110: // Build vehicles truelight@110: static void AiNew_State_BuildVehicle(Player *p) { truelight@110: int res; truelight@110: assert(p->ainew.state == AI_STATE_BUILD_VEHICLE); truelight@145: truelight@110: // Check if we need to build a vehicle truelight@110: if (p->ainew.amount_veh == 0) { truelight@110: // Nope, we are done! truelight@110: // This means: we are all done! The route is open.. go back to NOTHING truelight@110: // He will idle some time and it will all start over again.. :) truelight@110: p->ainew.state = AI_STATE_ACTION_DONE; truelight@110: return; truelight@110: } truelight@110: if (--p->ainew.idle != 0) return; truelight@110: // It is realistic that the AI can only build 1 vehicle a day.. truelight@110: // This makes sure of that! truelight@110: p->ainew.idle = AI_BUILD_VEHICLE_TIME_BETWEEN; truelight@145: truelight@110: // Build the vehicle truelight@110: res = AiNew_Build_Vehicle(p, p->ainew.depot_tile, DC_EXEC); truelight@110: if (res == CMD_ERROR) { truelight@110: // This happens when the AI can't build any more vehicles! truelight@110: p->ainew.state = AI_STATE_NOTHING; truelight@110: return; truelight@110: } truelight@110: // Increase the current counter truelight@110: p->ainew.cur_veh++; truelight@110: // Decrease the total counter truelight@110: p->ainew.amount_veh--; truelight@110: // Get the new ID truelight@110: if (p->ainew.tbt == AI_TRAIN) { truelight@110: } else { truelight@110: p->ainew.veh_id = _new_roadveh_id; truelight@110: } truelight@110: // Go give some orders! truelight@110: p->ainew.state = AI_STATE_GIVE_ORDERS; truelight@110: } truelight@110: truelight@110: // Put the stations in the order list truelight@110: static void AiNew_State_GiveOrders(Player *p) { darkvater@558: int idx; darkvater@558: Order order; darkvater@558: truelight@110: assert(p->ainew.state == AI_STATE_GIVE_ORDERS); truelight@145: truelight@110: if (p->ainew.veh_main_id != (VehicleID)-1) { truelight@110: DoCommandByTile(0, p->ainew.veh_id + (p->ainew.veh_main_id << 16), 0, DC_EXEC, CMD_CLONE_ORDER); truelight@145: truelight@110: // Skip the first order if it is a second vehicle truelight@110: // This to make vehicles go different ways.. truelight@110: if (p->ainew.veh_id & 1) truelight@110: DoCommandByTile(0, p->ainew.veh_id, 0, DC_EXEC, CMD_SKIP_ORDER); truelight@110: p->ainew.state = AI_STATE_START_VEHICLE; truelight@110: return; truelight@110: } else { truelight@110: p->ainew.veh_main_id = p->ainew.veh_id; truelight@110: } truelight@145: miham@826: // When more than 1 vehicle, we send them to different directions darkvater@558: idx = 0; darkvater@558: order.type = OT_GOTO_STATION; darkvater@558: order.flags = 0; darkvater@558: order.station = _map2[p->ainew.from_tile]; truelight@110: if (p->ainew.tbt == AI_TRUCK && p->ainew.from_deliver) darkvater@558: order.flags |= OF_FULL_LOAD; darkvater@558: DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); truelight@110: darkvater@558: idx = 1; darkvater@558: order.type = OT_GOTO_STATION; darkvater@558: order.flags = 0; darkvater@558: order.station = _map2[p->ainew.to_tile]; truelight@110: if (p->ainew.tbt == AI_TRUCK && p->ainew.to_deliver) darkvater@558: order.flags |= OF_FULL_LOAD; darkvater@558: DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); truelight@110: truelight@110: // Very handy for AI, goto depot.. but yeah, it needs to be activated ;) truelight@110: if (_patches.gotodepot) { darkvater@558: idx = 2; darkvater@558: order.type = OT_GOTO_DEPOT; darkvater@558: order.flags = OF_UNLOAD; darkvater@558: order.station = GetDepotByTile(p->ainew.depot_tile); darkvater@558: DoCommandByTile(0, p->ainew.veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER); truelight@110: } truelight@110: truelight@110: // Start the engines! truelight@110: p->ainew.state = AI_STATE_START_VEHICLE; truelight@110: } truelight@110: truelight@110: // Start the vehicle truelight@110: static void AiNew_State_StartVehicle(Player *p) { truelight@110: assert(p->ainew.state == AI_STATE_START_VEHICLE); truelight@145: truelight@110: // 3, 2, 1... go! (give START_STOP command ;)) truelight@110: DoCommandByTile(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@110: p->ainew.state = AI_STATE_BUILD_VEHICLE; truelight@110: } truelight@110: truelight@110: // Repays money truelight@110: static void AiNew_State_RepayMoney(Player *p) { truelight@110: int i; truelight@110: for (i=0;iainew.state = AI_STATE_ACTION_DONE; truelight@110: } truelight@110: truelight@145: static void AiNew_CheckVehicle(Player *p, Vehicle *v) { 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 || truelight@145: (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) { truelight@145: if (v->type == VEH_Road && IsRoadDepotTile(v->tile) && truelight@145: (v->vehstatus&VS_STOPPED)) { truelight@145: // We are at the depot, sell the vehicle truelight@145: DoCommandByTile(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: { truelight@145: int res = 0; truelight@145: if (v->type == VEH_Road) truelight@145: res = DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SEND_ROADVEH_TO_DEPOT); truelight@145: // This means we can not find a depot :s truelight@145: // if (res == CMD_ERROR) truelight@145: } truelight@145: } truelight@145: } truelight@145: } truelight@145: truelight@145: // Checks all vehicles if they are still valid and make money and stuff truelight@145: static void AiNew_State_CheckAllVehicles(Player *p) { truelight@145: Vehicle *v; truelight@145: truelight@145: FOR_ALL_VEHICLES(v) { truelight@145: if (v->type == 0) continue; truelight@145: if (v->owner != p->index) continue; truelight@145: // Currently, we only know how to handle road-vehicles truelight@145: 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: 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[] = { truelight@110: NULL, truelight@110: AiNew_State_FirstTime, truelight@110: AiNew_State_Nothing, truelight@110: AiNew_State_WakeUp, truelight@110: AiNew_State_LocateRoute, truelight@110: AiNew_State_FindStation, truelight@110: AiNew_State_FindPath, truelight@110: AiNew_State_FindDepot, truelight@110: AiNew_State_VerifyRoute, truelight@110: AiNew_State_BuildStation, truelight@110: AiNew_State_BuildPath, truelight@110: AiNew_State_BuildDepot, truelight@110: AiNew_State_BuildVehicle, truelight@110: AiNew_State_GiveOrders, truelight@110: AiNew_State_StartVehicle, truelight@110: AiNew_State_RepayMoney, truelight@145: AiNew_State_CheckAllVehicles, truelight@110: AiNew_State_ActionDone, truelight@110: NULL, truelight@110: }; truelight@110: truelight@110: static void AiNew_OnTick(Player *p) { truelight@110: if (_ainew_state[p->ainew.state] != NULL) truelight@110: _ainew_state[p->ainew.state](p); truelight@110: } truelight@110: truelight@110: void AiNewDoGameLoop(Player *p) { truelight@110: // If it is a human player, it is not an AI, so bubye! truelight@110: if (IS_HUMAN_PLAYER(_current_player)) truelight@110: return; truelight@145: 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: // See what the speed is truelight@110: switch (_opt.diff.competitor_speed) { truelight@110: case 0: // Very slow truelight@110: if (!(p->ainew.tick&8)) return; truelight@110: break; truelight@110: case 1: // Slow truelight@110: if (!(p->ainew.tick&4)) return; truelight@110: break; truelight@110: case 2: truelight@110: if (!(p->ainew.tick&2)) return; truelight@110: break; truelight@110: case 3: truelight@110: if (!(p->ainew.tick&1)) return; truelight@110: break; truelight@110: case 4: // Very fast truelight@110: default: // Cool, a new speed setting.. ;) VERY fast ;) truelight@110: break; truelight@110: } truelight@110: truelight@110: // If we come here, we can do a tick.. do so! truelight@110: AiNew_OnTick(p); truelight@110: }