WormAIDev  Worm SELF_VERSION
 All Classes Files Functions Variables Pages
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
WormAirManager Class Reference

Define the WormAirManager class which handles airports and airplanes. More...

Public Member Functions

constructor WormAirManager ()
 Create an instance of WormAirManager and initialize our variables.
function AfterLoading ()
 Do any necessary processing after a savegame has been loaded.
function LoadFromScratch ()
 Try to load existing air routes and towns used from scratch.
function UpdateAirportUpgradeList ()
 Update the upgrade_wanted list of towns with airports that can and should be upgraded.
function SendAirplanesOffAirport (town_id, station_id)
 Send all airplanes that are currently on this (assumed closed) airport to their next order.
function UpgradeLargeToMetropolitan (nearest_town, station_id, station_tile)
 Tries to upgrade airport from large to metropolitan in the same location since they are the same size.
function IsAirportEmpty (station_id)
 Check whether the airport (including depots) is empty, meaning no airplanes.
function UpgradeSmall (nearest_town, station_id, station_tile, airport_type, other_station_tile)
 Tries to upgrade airport from small to either large or metropolitan.
function TryToBuildAirport (tile_1, tile_2, is_first_airport, airport_type)
 Try to build an airport.
function ComputeDistances ()
 Compute squared min and max distances based on our AI settings.
function FindAirportSpotInTown (town, airport_type, airport_width, airport_height, coverage_radius, center_tile, minimum_acceptance, add_to_blacklist=true, old_airport_type=AIAirport.AT_INVALID)
 Find a candidate spot in the specified town to build an airport of the specified type.
Debugging output functions
function DebugListTownsUsed ()
 List of towns used and stations near those towns.
function DebugListTowns (towns_list)
 List all towns in the supplied list.
function DebugListRoutes ()
 List all routes: per route all stations and all vehicles on that route with relevant info.
function DebugListRoute (route)
 List all route info in the supplied list.
function DebugListRouteInfo ()
 List all our air routes.
function DebugListVehiclesSentToDepot ()
 List all vehicles that were sent to depot to be sold.
function DebugVehicleInfo (veh)
 Show info about the specified vehicle.
Airport handling functions
function RemoveAirport (tile)
 Remove airport at specified tile.
function GetOptimalAvailableAirportType ()
 Get the optimal type of airport that is available.
function GetAiportTileOtherEndOfRoute (town_id, station_tile)
 Get tile of airport at the other end of the route.
function UpdateAirportTileInfo (town_idx, station_id, old_tile)
 Update the airport station tile info in our lists after upgrading airport.
function ReplaceAirportTileInfo (old_town_idx, old_tile, new_tile, other_end_of_route_tile)
 Replace the airport town and station tile info in our lists and update orders.
function IsWithinNoiseLimit (tile, old_airport_type, new_airport_type)
 Determines whether an airport at a given tile is allowed by the town authorities because of the noise level.
function CheckForAirportsNeedingToBeUpgraded ()
 Checks all airports to see if they should be upgraded.
function BuildAirportRoute ()
 Build an airport route.
function FindSuitableAirportSpot (airport_type, center_tile)
 Find a suitable spot for an airport, walking all towns hoping to find one.
function SellAirports (airport_1_tile, airport_2_tile)
 Sells the airports at tile_1 and tile_2.
Order handling
function IsTownFirstOrder (town_id)
 Check whether the airport at a certain town is used as the first order of a route.
function ReplaceOrders (veh, is_first_order, breakdowns, station_tile)
 Replace orders of a vehicle, either the first station or last station is replaced.
function InsertGotoStationOrder (veh, order_pos, station_tile)
 Insert go to station order for airport at station_tile.
function InsertMaintenanceOrder (veh, order_pos, station_tile)
 Insert Maintenance order for airport at station_tile.
function ReplaceGotoStationOrder (veh, order_pos, station_tile)
 Replace go to station order for airport at station_tile.
Aircraft handling
function GetAircraftMinimumPrice (IsForSmallAirport)
 Get the minimum price of an aircraft.
function GetMaximumDistance (engine)
 Get the maximum distance this aircraft can safely fly without landing.
function BuildAircraft (tile_1, tile_2, start_tile)
 Build an aircraft with orders from tile_1 to tile_2.
function SendAllVehiclesOfStationToDepot (station_id, sell_reason)
 Send all vehicles belonging to a station to depot for selling.
function SendToDepotForSelling (vehicle, sell_reason)
 Send a vehicle to depot to be sold when it arrives.
function RemoveVehicleFromLists (vehicle)
 Remove a vehicle from our route lists and to depot list.
function SellVehicleInDepot (vehicle)
 Sell the vehicle provided it's in depot.
function SellVehiclesInDepot ()
 Sell all vehicles in depot that are marked to be sold.
Task related functions
function ManageVehicleRenewal ()
 Check all vehicles for being old or needing upgrading to a newer type.
function CheckAirportsWithoutVehicles ()
 Check for airports that don't have any vehicles anymore and delete them.
function UpdateBlacklists ()
 Remove towns from the blacklists where blacklisting has expired.
function ManageAirRoutes ()
 

Manage air routes:


function HandleEvents ()
 Callback that handles events.
function CheckAirplanePrices (engine_list)
 Get the lowest prices of the current available big and small airplanes.
function EvaluateAircraft (clear_warning_shown_flag)
 Task that evaluates all available aircraft for how suited they are for our purposes.
function BuildHQ ()
 Build the company headquarters if there isn't one yet.
function BuildStatues ()
 Build statues in towns where we have a station as long as we have a reasonable amount of money.
General functions
function GetTownFromStationTile (st_tile)
 Get Town id in our towns_used list based on tile of station built near it.
function IsValidStationFromTile (st_tile)
 Determine if a station is valid based on the station tile.
function IsValidFirstStation (veh)
 Determine if the first station of the route of a vehicle is valid.
function IsValidLastStation (veh)
 Determine if the last station of the route of a vehicle is valid.
Valuator functions
function GetCostFactor (engine, costfactor_list)
 Valuator function to get the cost factor of an aircraft.

Public Attributes

 towns_used = null
 town id, airport station tile
 route_1 = null
 vehicle id, station_tile of first station in an order
 route_2 = null
 vehicle id, station_tile of last station in an order
 distance_of_route = {}
 vehicle id, distance between first/last order stations
 vehicle_to_depot = {}
 vehicle id, boolean always true currently
 engine_usefulness = null
 acceptance_limit = 0
 Starting limit for passenger acceptance for airport finding.
 passenger_cargo_id = -1
 no_aircraft_warning_shown = false
 Whether we have shown the no available aircraft warning.
 route_without_aircraft = false
 True if we have built airports but failed to buy airplanes due to lack of money.
 incomplete_route_tile1 = 0
 Tile of first airport of incomplete air route without aircraft.
 incomplete_route_tile2 = 0
 Tile of second airport of incomplete air route without aircraft.
 towns_blacklist = null
 List of towns where we already tried to build an airport.
 upgrade_blacklist = null
 List of towns where we failed to upgrade the airport.
 low_price_small = 0
 Lowest price of a small airplane.
 low_price_big = 0
 Lowest price of a big airplane.
 max_costfactor = 0
 upgrade_wanted = null
 List of airports we would like to upgrade.
 min_distance_squared = 0
 Precomputed minimum distance squared based on current settings.
 max_distance_squared = 0
 Precomputed maximum distance squared based on current settings.
 max_aircraft_distance = 0
 Highest distance that an aircraft can fly.

Static Public Attributes

static DEFAULT_MAX_COSTFACTOR = 350
 We don't want airplanes that have a higher costfactor than this unless we have nothing but planes to play with.
static DEFAULT_ANY_COSTFACTOR = 10000
 Used in case we want to accept any costfactor.
static BUILD_SUCCESS = 1
 Status code: Building airport succeeded.
static BUILD_FAILED = 2
 Building airport failed.
static BUILD_REBUILD_FAILED = 3
 Rebuilding airport failed.
static BUILD_REMOVE_FAILED = 4
 Removing airport failed.
static BUILD_AIRPORT_NOT_EMPTY = 5
 Airport is not empty, removing impossible.
static BUILD_NO_NEW_LOCATION = 6
 We could not find a good location for the replacement airport.

Detailed Description

Define the WormAirManager class which handles airports and airplanes.

Constructor & Destructor Documentation

constructor WormAirManager::WormAirManager ( )
inline

Create an instance of WormAirManager and initialize our variables.

Member Function Documentation

function WormAirManager::AfterLoading ( )

Do any necessary processing after a savegame has been loaded.

Currently recomputes the values for the distance_of_route table.

function WormAirManager::BuildAircraft ( tile_1  ,
tile_2  ,
start_tile   
)

Build an aircraft with orders from tile_1 to tile_2.

The best available aircraft will be bought.

Parameters
tile_1Airport tile that should be used as the first order.
tile_2Airport tile that should be used as the last order.
start_tileThe Airport tile where the airplane should start. If this is 0 then it will start at tile_1. To let it start at tile_2 use the same value as tile_2.
Todo:
We need to revise depot orders: both should probably go to depot on same airport as the order before it!
Todo:
But this will have implications in several other places that depend on the order of the orders.
function WormAirManager::BuildAirportRoute ( )

Build an airport route.

Find 2 cities that are big enough and try to build airport in both cities. Then we can build an aircraft and try to make some money. We limit our amount of airports to max aircraft / AIRPORT_LIMIT_FACTOR * 2. (2 airports for a route, and AIRPORT_LIMIT_FACTOR planes per route)

function WormAirManager::BuildHQ ( )

Build the company headquarters if there isn't one yet.

Note
Adapted from the version in AdmiralAI.
function WormAirManager::BuildStatues ( )

Build statues in towns where we have a station as long as we have a reasonable amount of money.

We limit the amount of statues we build at any one time.

function WormAirManager::CheckAirplanePrices ( engine_list  )

Get the lowest prices of the current available big and small airplanes.

Parameters
engine_listList of airplanes that can transport passengers.
function WormAirManager::CheckForAirportsNeedingToBeUpgraded ( )

Checks all airports to see if they should be upgraded.

If they can it tries to upgrade the airport. If it fails after removing the old airport it will first try to replace it with another airport at another spot. If that also fails it will send the aircraft on the now crippled route to depot to be sold. After all aircraft are sold the remaining airport will be sold too.

Todo:
Should probably be moved somewhere elese (main.nut?)
Todo:
Maybe only upgrade if we have enought money
Todo:
CLOSED airports should not be checked once a month but as often as possible because we don't want them to be closed for a long time, loosing profits.

Since METROPOLITAN is the same size as LARGE we will try to rebuild it in the same spot!

function WormAirManager::DebugListRoute ( route  )

List all route info in the supplied list.

function WormAirManager::DebugListRouteInfo ( )

List all our air routes.

function WormAirManager::DebugListRoutes ( )

List all routes: per route all stations and all vehicles on that route with relevant info.

function WormAirManager::DebugListTowns ( towns_list  )

List all towns in the supplied list.

function WormAirManager::DebugListTownsUsed ( )

List of towns used and stations near those towns.

function WormAirManager::DebugListVehiclesSentToDepot ( )

List all vehicles that were sent to depot to be sold.

function WormAirManager::DebugVehicleInfo ( veh  )

Show info about the specified vehicle.

It's start and end town and distance between them.

Parameters
veh= Vehicle id
function WormAirManager::EvaluateAircraft ( clear_warning_shown_flag  )

Task that evaluates all available aircraft for how suited they are for our purposes.

The suitedness values for aircraft which we can use are saved in engine_usefulness.

Parameters
clear_warning_shown_flagWhether to clear the no_aircraft_warning_shown flag.
function WormAirManager::FindAirportSpotInTown ( town  ,
airport_type  ,
airport_width  ,
airport_height  ,
coverage_radius  ,
center_tile  ,
minimum_acceptance  ,
add_to_blacklist  = true,
old_airport_type  = AIAirport.AT_INVALID 
)

Find a candidate spot in the specified town to build an airport of the specified type.

Parameters
townThe town id of the town where we should search.
airport_typeFor which type of airport.
airport_widthThe width of the airport.
airport_heightThe height of the airport.
coverageradiusThe coverage radius of the airport.
center_tileThe tile of the airport at the other end of the route or 0 if this is the first airport on the route.
minimum_acceptanceThe minimum cargo acceptance we should allow for suitable spots.
add_to_blacklistBoolean (default true) If true adds town to blacklist if no suitable spot could be found.
old_airport_type(default=AIAirport.AT_INVALID) If not invalid noise limits are checked for replacing old_airport_type with airport type.
Returns
The tile where an airport can be built or ERROR_FIND_AIRPORT1 or ERROR_FIND_AIRPORT2.
Todo:
Is this really useful. Does it matter what tile we build on in same town?
Todo:
In early games with low maximum speeds we may need to adjust maximum and maybe even minimum distance to get a round trip within a year.
function WormAirManager::FindSuitableAirportSpot ( airport_type  ,
center_tile   
)

Find a suitable spot for an airport, walking all towns hoping to find one.

When a town is used, it is marked as such and not re-used.

Parameters
airport_typeThe type of airport we want to build.
center_tileThe tile around which we will search for a spot for the airport.
Returns
tile where we can build the airport or an error code.
function WormAirManager::GetAiportTileOtherEndOfRoute ( town_id  ,
station_tile   
)

Get tile of airport at the other end of the route.

Parameters
town_idTown id of town at this end of route.
station_tiletile of station at this end of route.
Returns
tile of airport or -1 if not found.
function WormAirManager::GetAircraftMinimumPrice ( IsForSmallAirport  )

Get the minimum price of an aircraft.

Parameters
IsForSmallAirportBoolean True if only aircraft that can land on small airports should be considered.
Returns
The lowest price of an aircraft.
function WormAirManager::GetCostFactor ( engine  ,
costfactor_list   
)

Valuator function to get the cost factor of an aircraft.

Parameters
engineThe engine id of the aircraft.
costfactor_listThe list (usually engine_usefulness) that holds the cost factors.
Returns
The cost factor.
function WormAirManager::GetMaximumDistance ( engine  )

Get the maximum distance this aircraft can safely fly without landing.

Parameters
engineThe engine id for which we want to know the maximum distance.
Returns
The maximum distance.
function WormAirManager::GetOptimalAvailableAirportType ( )

Get the optimal type of airport that is available.

Note
For now we only choose between small, large and metropolitan. Larger ones would only be useful for very high cargo/passenger amounts with many airplanes.
Returns
The optimal AirportType or null if no suitable airport is available.
function WormAirManager::GetTownFromStationTile ( st_tile  )

Get Town id in our towns_used list based on tile of station built near it.

Returns
id of town or null if not found.
function WormAirManager::HandleEvents ( )

Callback that handles events.

Currently only AIEvent.ET_VEHICLE_CRASHED is handled.

function WormAirManager::InsertGotoStationOrder ( veh  ,
order_pos  ,
station_tile   
)

Insert go to station order for airport at station_tile.

Parameters
vehVehicle to set the order for.
order_posPosition in the order list where order should be inserted.
station_tileTile for the Airport of the to be inserted order.
Returns
true if order got inserted; false in case of failure to insert.
function WormAirManager::InsertMaintenanceOrder ( veh  ,
order_pos  ,
station_tile   
)

Insert Maintenance order for airport at station_tile.

Parameters
vehVehicle to set the order for
order_posPosition in the order list where order should be inserted
station_tileTile for the Airport (not the hangar) of the order to be inserted.
Returns
true if order got inserted; false in case of failure to insert.
function WormAirManager::IsAirportEmpty ( station_id  )

Check whether the airport (including depots) is empty, meaning no airplanes.

Returns
true if it is empty, otherwise false
function WormAirManager::IsTownFirstOrder ( town_id  )

Check whether the airport at a certain town is used as the first order of a route.

Parameters
town_idThe id of the town to check if it's the first order.
Returns
true if it is the first order, else false if it is the last order.
function WormAirManager::IsValidFirstStation ( veh  )

Determine if the first station of the route of a vehicle is valid.

Parameters
vehVehicle to determine the validity of the station for.
Returns
true if station is valid, otherwise false.
function WormAirManager::IsValidLastStation ( veh  )

Determine if the last station of the route of a vehicle is valid.

Parameters
vehVehicle to determine the validity of the station for.
Returns
true if station is valid, otherwise false.
function WormAirManager::IsValidStationFromTile ( st_tile  )

Determine if a station is valid based on the station tile.

Parameters
st_tileThe tile of the station.
Returns
true if station is valid, otherwise false.
function WormAirManager::IsWithinNoiseLimit ( tile  ,
old_airport_type  ,
new_airport_type   
)

Determines whether an airport at a given tile is allowed by the town authorities because of the noise level.

Parameters
tileThe tile where the aiport would be built.
old_airport_typeThe type of the current airport or AT_INVALID if there is no airport yet.
new_airport_typeThe type of the proposed replacement airport.
Returns
True if the construction would be allowed. If the noise setting is off, it defaults to true.
Note
Adapted from SimpleAI.
function WormAirManager::LoadFromScratch ( )

Try to load existing air routes and towns used from scratch.

This can be needed when we get loaded into a savegame from a different AI.

Todo:
code from AdmiralAI Aircraftmanager suggests that airport_id may be same as airport_tile!
Todo:
What to do if multiple airports have the same nearest town?
Todo:
Add vehicles if there are airports available.

Second station in orders

Todo:
figure out what to do if there are more stations in orders...
Todo:
If problems.Count() > 0 then randomly add them over the available routes. Since it's not that likely we will leave that for another time.
function WormAirManager::ManageAirRoutes ( )

Manage air routes:

  • Checks for airports without vehicles.
  • Send unprofitable aircraft to depot for selling.
  • Add aircraft to routes that have a lot of waiting cargo.
    Returns
    Error code if something went wrong or ok.
    Todo:
    Refactor the parts of this function into separate functions.
  • Checks for airports without vehicles.
  • Send unprofitable aircraft to depot for selling.
  • Add aircraft to routes that have a lot of waiting cargo.
    Returns
    Error code if something went wrong or ok.
    Todo:
    Refactor the parts of this function into separate functions.
Todo:
  1. Make groups for each route
  2. When we have max aircraft/airports:
    • Evaluate total profit per group, remove bad groups/airports or reduce # planes
    • Favor bigger/faster aircraft over cost more when high amount waiting passengers
  3. Upgrade aircraft when they are old or when newer ones would be more profitable
  4. Upgrade airports only when it's needed
  5. Check reliability when breakdowns are on
Todo:
Do this on a per group basis (each route a group).
Todo:
That way we can decide to not check a group wich was created less than x years ago.
Todo:
When maintenance costs are on we should set low profit limit too at least the yearly costs.
Todo:
Don't sell all aircraft from the same route all at once, try selling 1 per year?
function WormAirManager::ManageVehicleRenewal ( )

Check all vehicles for being old or needing upgrading to a newer type.

It will send all vehicles that are non optimal to depot for selling.

function WormAirManager::RemoveAirport ( tile  )

Remove airport at specified tile.

If removing fails then give a warning.

Note
Note that using Sleep(x) here and trying again doesn't work for some reason (removing still fails)
Parameters
tileThe tile of the airport that should be removed.
function WormAirManager::RemoveVehicleFromLists ( vehicle  )

Remove a vehicle from our route lists and to depot list.

Note
If this is the last vehicle serving a certain route then after selling the vehicle we will also sell the airports.
Parameters
vehicleThe vehicle id that should be removed from the lists.
function WormAirManager::ReplaceAirportTileInfo ( old_town_idx  ,
old_tile  ,
new_tile  ,
other_end_of_route_tile   
)

Replace the airport town and station tile info in our lists and update orders.

Parameters
old_town_idxIndex into towns_used list of town/station being replaced
old_tileThe old tile for the airport being replaced.
new_tileThe tile of the new airport.
other_end_of_route_tileTile of other end of route (needed to access vehicles of route)
function WormAirManager::ReplaceGotoStationOrder ( veh  ,
order_pos  ,
station_tile   
)

Replace go to station order for airport at station_tile.

Parameters
vehVehicle to set the order for.
order_posPosition in the order list where order should be inserted.
station_tileTile for the Airport of the new to be inserted order.
function WormAirManager::ReplaceOrders ( veh  ,
is_first_order  ,
breakdowns  ,
station_tile   
)

Replace orders of a vehicle, either the first station or last station is replaced.

Parameters
vehVehicle to replace the orders for.
is_first_orderWhether to replace the orders for the first or last station.
breakdownsWhether breakdowns are on; if they are on we will add maintenance orders.
station_tileTile of station for the new order.
function WormAirManager::SellAirports ( airport_1_tile  ,
airport_2_tile   
)

Sells the airports at tile_1 and tile_2.

Removes towns from towns_used list too.

Parameters
airport_1_tileThe tile of the first airport to remove
airport_2_tileThe tile of the other airport to remove
Note
The airport tiles are allowed to be invalid. Removal will be ignored in that case but the towns_used will be updated.
Todo:
Make a list of removed airports/tiles so that we don't build a new airport in the same spot soon after we have removed it!
function WormAirManager::SellVehicleInDepot ( vehicle  )

Sell the vehicle provided it's in depot.

If it's not yet in depot it will fail silently.

Parameters
vehicleThe id of the vehicle that should be sold.
function WormAirManager::SendAirplanesOffAirport ( town_id  ,
station_id   
)

Send all airplanes that are currently on this (assumed closed) airport to their next order.

Parameters
town_idThe id of the town this airport belongs to.
station_idThe id of the airport station.
function WormAirManager::SendAllVehiclesOfStationToDepot ( station_id  ,
sell_reason   
)

Send all vehicles belonging to a station to depot for selling.

Parameters
station_idThe id of the station.
sell_reasonThe reason for selling. Valid reasons are VEH_OLD_AGE, VEH_LOW_PROFIT, VEH_STATION_REMOVAL
function WormAirManager::SendToDepotForSelling ( vehicle  ,
sell_reason   
)

Send a vehicle to depot to be sold when it arrives.

Parameters
vehicleThe vehicle id of the vehicle to be sold.
sell_reasonThe reason for selling. Valid reasons are VEH_OLD_AGE, VEH_LOW_PROFIT, VEH_STATION_REMOVAL
function WormAirManager::TryToBuildAirport ( tile_1  ,
tile_2  ,
is_first_airport  ,
airport_type   
)

Try to build an airport.

Parameters
tile_1The tile of the proposed first airport on a route.
tile_2The tile of the proposed second airport on a route.
is_first_airportBoolean: true if it's the first airport we want to build, false if the second should be built.
airport_typeThe type of airport to build.
Returns
The actual tile where the airport was built or ERROR_BUILD_AIRPORT1.
Note
The actual tile where the airport got built can be different than tile_1/tile_2 because if building there fails we will try to find a second spot to build. If that succeeds the tile of that spot is returned.
function WormAirManager::UpdateAirportTileInfo ( town_idx  ,
station_id  ,
old_tile   
)

Update the airport station tile info in our lists after upgrading airport.

Expects a valid station_id.

Parameters
town_idxIndex into towns_used list.
station_idId of the Airport station that got upgraded.
old_tileThe old tile for the airport before upgrading.
function WormAirManager::UpgradeLargeToMetropolitan ( nearest_town  ,
station_id  ,
station_tile   
)

Tries to upgrade airport from large to metropolitan in the same location since they are the same size.

Parameters
nearest_townThe nearest town according to town influence.
station_idThe id of the airport to upgrade.
station_tileThe tile of the airport.
Returns
BUILD_SUCCESS if we succeed, or else one of the BUILD_XXX error codes.
Precondition
Noise and Town allowance already checked, enough money, ...
Todo:
Can we use RemoveAirport too or does that make it impossible to reuse station_id?
function WormAirManager::UpgradeSmall ( nearest_town  ,
station_id  ,
station_tile  ,
airport_type  ,
other_station_tile   
)

Tries to upgrade airport from small to either large or metropolitan.

Parameters
nearest_townThe nearest town according to town influence.
station_idThe id of the airport to upgrade.
station_tileThe tile of the airport.
Returns
BUILD_SUCCESS if we succeed, or else one of the BUILD_XXX error codes.
Todo:
Ideally we should include the tiles of the current airport in our search for a new spot!
Todo:
Maybe add as an extra parameter an optional list (default null) with the tiles of the airport
Todo:
Set acceptance to maybe 50 lower than acceptance of current airport? On the other hand it will already go for the highest acceptance spot, maybe do acceptance = 50?
Todo:
Can we use RemoveAirport too or does that make it impossible to reuse station_id?

Member Data Documentation

WormAirManager::distance_of_route = {}

vehicle id, distance between first/last order stations

Todo:
Change distance_of_route to not register distance PER VEHICLE but only once PER ROUTE

The documentation for this class was generated from the following file: