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

Public Member Functions

function GetAuthor ()
function GetName ()
function GetShortName ()
function GetDescription ()
function GetVersion ()
function GetDate ()
function MinVersionToLoad ()
function CreateInstance ()
function GetAPIVersion ()
function GetURL ()
function GetSettings ()
constructor WormAI ()
Implementation of base class functions
function Start ()
function Save ()
function Load (version, data)
Utility functions
function decToHex (number)
function WriteTile (tile)
function GetAircraftTypeAsText (airplane_id)
function GetAgeString (AgeInDays)
function SafeAddRectangle (list, tile, radius)
Debugging output functions
function DebugListTownsUsed ()
function DebugListTowns (towns_list)
function DebugListRoutes ()
function DebugListRoute (route)
function DebugListRouteInfo ()
function DebugListVehiclesSentToDepot ()
function DebugVehicleInfo (veh)
Money related functions
function HasMoney (money)
function GetMoney (money)
Airport handling functions
function RemoveAirport (tile)
function GetOptimalAvailableAirportType ()
function GetAiportTileOtherEndOfRoute (town_id, station_tile)
function UpdateAirportTileInfo (town_idx, station_id, old_tile)
function ReplaceAirportTileInfo (old_town_idx, old_tile, new_tile, other_end_of_route_tile)
function CheckForAirportsNeedingToBeUpgraded ()
function BuildAirportRoute ()
function FindSuitableAirportSpot (airport_type, center_tile)
function SellAirports (airport_1_tile, airport_2_tile)
Order handling
function IsTownFirstOrder (town_id)
function ReplaceOrders (veh, is_first_order, breakdowns, station_tile)
function InsertGotoStationOrder (veh, order_pos, station_tile)
function InsertMaintenanceOrder (veh, order_pos, station_tile)
function ReplaceGotoStationOrder (veh, order_pos, station_tile)
Aircraft handling
function GetMaximumDistance (engine)
function BuildAircraft (tile_1, tile_2, start_tile)
function SendAllVehiclesOfStationToDepot (station_id, sell_reason)
function SendToDepotForSelling (vehicle, sell_reason)
function RemoveVehicleFromLists (vehicle)
function SellVehicleInDepot (vehicle)
function SellVehiclesInDepot ()
function CanBuildAircraft ()
Task related functions
function ManageVehicleRenewal (age_limit)
function CheckAirportsWithoutVehicles ()
function ManageAirRoutes ()
function HandleEvents ()
function EvaluateAircraft ()
General functions
function GetTownFromStationTile (st_tile)
function IsValidStationFromTile (st_tile)
function IsValidFirstStation (veh)
function IsValidLastStation (veh)
Valuator functions
function GetCostFactor (engine, costfactor_list)
Initialization functions
function InitSettings ()
function Welcome ()

Public Attributes

 name = null
 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
 ai_speed_factor = 1
 speed factor for our ai actions (1=fast..3=slow)
 delay_build_airport_route = 0
 passenger_cargo_id = -1
 loaded_from_save = false
 engine_usefulness = null
 acceptance_limit = 0
 Starting limit for passenger acceptance for airport finding.
 aircraft_disabled_shown = 0
 Has the aircraft disabled in game settings message been shown (1) or not (0).
 aircraft_max0_shown = 0
 Has the max aircraft is 0 in game settings message been shown.

Detailed Description

Define the main class of our AI WormAI.

Constructor & Destructor Documentation

constructor WormAI::WormAI ( )
inline

Create an instance of WormAI.

Member Function Documentation

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

Build an aircraft with orders from tile_1 to tile_2. The best available aircraft of that time will be bought. start_tile is the tile where the airplane should start, or 0 to start at the first tile.

function WormAI::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 WormAI::CanBuildAircraft ( )

Checks if we can build an aircraft and if not outputs a string with the reason.

function WormAI::CheckAirportsWithoutVehicles ( )

Check for airports that don't have any vehicles anymore and delete them.

function WormAI::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.

function WormAI::DebugListRoute ( route  )

List all route info in the supplied list.

function WormAI::DebugListRouteInfo ( )

List all our air routes.

function WormAI::DebugListRoutes ( )

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

function WormAI::DebugListTowns ( towns_list  )

List all towns in the supplied list.

function WormAI::DebugListTownsUsed ( )

List of towns used and stations near those towns.

function WormAI::DebugListVehiclesSentToDepot ( )

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

function WormAI::DebugVehicleInfo ( veh  )

Show info about the specified vehicle. It's start and end town and distance between them.

Parameters
veh= Vehicle id
function WormAI::decToHex ( number  )

Convert a number to its hexadecimal string representation.

Parameters
numberThe number to convert.
Returns
The hexadecimal string.
Note
Since it seems that Squirrel string library (with format and regexp) is not enabled in OpenTTD I used the following function to get a hexadecimal string. Source: http://forum.iv-multiplayer.com/index.php?topic=914.60
function WormAI::EvaluateAircraft ( )

Task that once in a while evaluates all available aircraft for how suited they are for our purposes.

function WormAI::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 WormAI::GetAgeString ( AgeInDays  )

Rough year/month age estimation string where year = 365 days and month = 30 days.

Parameters
AgeInDaysThe age in days.
Returns
Text string saying how many years and months.
function WormAI::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 WormAI::GetAircraftTypeAsText ( airplane_id  )

Returns aircraft type as text.

Parameters
airplane_idThe id of the airplane
Returns
The airplane type as a text string
function WormAI::GetCostFactor ( engine  ,
costfactor_list   
)

Get the cost factor of an aircraft.

function WormAI::GetMaximumDistance ( engine  )

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

function WormAI::GetMoney ( money  )

Get the amount of money requested, loan if needed.

Parameters
moneyThe amount of money we need.
Returns
Boolean saying if we got the needed money or not.
function WormAI::GetOptimalAvailableAirportType ( )

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 WormAI::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 WormAI::HandleEvents ( )

Callback that handles events.

function WormAI::HasMoney ( money  )

Check if we have enough money (via loan and on bank).

Parameters
moneyThe amount of money we need.
Returns
Boolean saying if we do or don't have enough money.
function WormAI::InitSettings ( )

InitSettings initializes a number of required variables based on the game settings of our AI.

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

Insert go to station order for airport at station_tile veh Vehicle to set the order for order_pos Position in the order list where order should be inserted station_tile Tile for the Airport

function WormAI::InsertMaintenanceOrder ( veh  ,
order_pos  ,
station_tile   
)

Insert Maintenance order for airport at station_tile veh Vehicle to set the order for order_pos Position in the order list where order should be inserted station_tile Tile for the Airport (not the hangar)

function WormAI::IsTownFirstOrder ( town_id  )

IsTownFirstOrder returns true if the airport near this town is used as the first order, if false then it is used as the last/second order.

function WormAI::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 WormAI::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 WormAI::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 WormAI::Load ( version  ,
data   
)

Load previously saved information.

Parameters
versionWhich version of our AI saved the information.
dataThe data that was saved.
function WormAI::ManageAirRoutes ( )

Manage air routes:

  • Send unprofitable aircraft to depot for selling
  • Add aircraft to routes that have a lot of waiting cargo
function WormAI::ManageVehicleRenewal ( age_limit  )

ManageVehicleRenewal will 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. Parameters: age_limit - the age in days left limit below which we send to depot for selling

function WormAI::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 WormAI::RemoveVehicleFromLists ( vehicle  )

Remove vehicle from route lists and to depot list.

function WormAI::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 WormAI::ReplaceGotoStationOrder ( veh  ,
order_pos  ,
station_tile   
)

Insert go to station order for airport at station_tile veh Vehicle to set the order for order_pos Position in the order list where order should be inserted station_tile Tile for the Airport

function WormAI::ReplaceOrders ( veh  ,
is_first_order  ,
breakdowns  ,
station_tile   
)

Replace orders of vehicle, either the first station or last station is replaced veh Vehicle to replace the orders for. is_first_order Whether to replace the orders for the first or last station breakdowns Whether breakdowns are on, if on then add maintenance orders station_tile Tile of new station

function WormAI::SafeAddRectangle ( list  ,
tile  ,
radius   
)

Add a rectangular area to an AITileList containing tiles that are within /radius/ tiles from the center tile, taking the edges of the map into account.

Note
This function was taken from Rondje.
Parameters
listThe AITileList in which the valid tiles will be returned.
tileThe center tile.
radiusThe radius of tiles.
function WormAI::Save ( )

Save all data that WormAI uses.

Returns
The data to be saved.
function WormAI::SellAirports ( airport_1_tile  ,
airport_2_tile   
)

Sells the airports from tile_1 and tile_2 Removes towns from towns_used list too

function WormAI::SellVehicleInDepot ( vehicle  )

Sell the vehicle provided it's in depot. If it's not yet in depot it will fail silently.

function WormAI::SellVehiclesInDepot ( )

Sell all vehicles in depot that are marked to be sold.

function WormAI::SendAllVehiclesOfStationToDepot ( station_id  ,
sell_reason   
)

Send all vehicles belonging to station to depot for selling sell_reason Reason for selling

function WormAI::SendToDepotForSelling ( vehicle  ,
sell_reason   
)

Send the vehicle to depot to be sold when it arrives. Vehicle - the vehicle id of the vehicle to be sold sell_reason - Reason for selling vehicle

function WormAI::Start ( )

Start the main loop of WormAI.

function WormAI::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 WormAI::Welcome ( )

Welcome says hello to the user and prints out it's current AI gamesettings.

function WormAI::WriteTile ( tile  )

Writes a tile as a hexadecimal number.

Parameters
tileThe tile to convert.
Returns
The hexadecimal string.

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