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

Define the WormRailManager class which handles trains. More...

Public Member Functions

constructor WormRailManager ()
 Create an instance of WormRailManager and initialize our variables.
function UpdateRailType ()
 Updates the current rail type of the AI based on the maximum number of cargoes transportable.
function BuildRailway ()
 Main function for building a railway which decides all the details.
function RegisterRoute (route_data, station_data, vehtype, group, train_length)
 Register the new route into the database.
function CheckRoutes ()
 Checks all routes.
function GetOptimalTrainLength (route)
 Get the optimal train length for this route.
function GetMinMaxTrainLength (vehicles)
 Get the minimum and maximum length of trains in the supplied list of vehicles.
function CheckDefaultGroup ()
 Checks ungrouped vehicles.
function CheckAddTrainToGroup (route, vehicle_count, first_vehicle)
 Check if we should add another train to the specified group.
function SelectAndAddTrain (route)
 Select optimal wagon and train engine for the specified route and buy it if possible.
function AddVehicle (route, mainvehicle, engine, wagon)
 Adds a new vehicle to an existing route.
function ReplaceVehicle (vehicle)
 Replaces an old vehicle with a newer model if it is already in the depot.
function HandleVehicleInDepot (vehicle)
 Handle a vehicle that is stopped in depot for selling, replacement or attaching wagons.
function CheckTrainProfits ()
 Check train profits and send unprofitable ones to depot to be sold.
function SendTrainToDepotForSelling (vehicle)
 Check train profits and send unprofitable ones to depot to be sold.
function SaveData (table)
 Save all data that WormRailManager needs in table.
function LoadData (table, worm_save_version)
 Load all data that WormRailManager from the table.
function AfterLoading ()
 Do any necessary processing after a savegame has been loaded.

Static Public Member Functions

static function SetGroupName (group, crg, stasrc)
 Set the name of a vehicle group.
static function IsRectangleWithinTownInfluence (tile, town_id, width, height)
 Checks whether a given rectangle is within the influence of a given town.
static function SetRailType ()
 Sets the current rail type of the AI based on the maximum number of cargoes transportable.

Public Attributes

 routes = null
 An array containing all our routes.
 groups = null
 The list of vehicle groups.
 serviced = null
 Industry/town - cargo pairs already serviced.
 railbridges = null
 The list of rail bridges.
 engine_blacklist = null
 The blacklist of train engines.
 buildingstage = null
 The current building stage.
 lastroute = null
 The date the last route was built.
 removelist = null
 An array used to continue rail removal and electrification.
 todepotlist = null
 A list of vehicles heading for the depot.
 route_without_trains = -1
 Group number or -1 of unfinished route that needs trains added.
 last_route = null
 route info table of last completed route. Needed if we still need to buy trains for this route.

Static Public Attributes

static BS_NOTHING = 0
 Building stages, needed to recover a savegame.
static BS_BUILDING = 1
static BS_REMOVING = 2
static BS_ELECTRIFYING = 3
static TD_SELL = 1
 Reasons to send a vehicle to a depot.
static TD_REPLACE = 2
static TD_ATTACH_WAGONS = 3

Detailed Description

Define the WormRailManager class which handles trains.

Constructor & Destructor Documentation

constructor WormRailManager::WormRailManager ( )
inline

Create an instance of WormRailManager and initialize our variables.

Todo:
: Needs to be initialized when loading a Save!

Member Function Documentation

function WormRailManager::AddVehicle ( route  ,
mainvehicle  ,
engine  ,
wagon   
)

Adds a new vehicle to an existing route.

Parameters
routeThe route to which the new vehicle will be added.
mainvehicleAn already existing vehicle on the route to share orders with.
engineThe EngineID of the new vehicle. In case of trains it is the EngineID of the locomotive.
wagonThe EngineID of the train wagons.
Returns
True if the action succeeded.
function WormRailManager::AfterLoading ( )

Do any necessary processing after a savegame has been loaded.

Currently recomputes the values for the _optimal_train_lengths list.

function WormRailManager::BuildRailway ( )

Main function for building a railway which decides all the details.

Todo:
replace number by a definied constant or variable depending on date and other factors
Todo:
check first if we are below max no. of trains...
function WormRailManager::CheckAddTrainToGroup ( route  ,
vehicle_count  ,
first_vehicle   
)

Check if we should add another train to the specified group.

Parameters
routeThe route info table.
vehicle_countThe current number of vehicles in this group.
first_vehicleThe first vehicle in this group.
Returns
True if we added a train, otherwise false.
Todo:
remove profit check? What if first vehicle is an older type and the others are making a profit?
function WormRailManager::CheckDefaultGroup ( )

Checks ungrouped vehicles.

Under normal conditions all vehicles should be grouped.

function WormRailManager::CheckRoutes ( )

Checks all routes.

Empty routes are removed, new vehicles are added if needed, old vehicles are replaced, vehicles are restarted if sitting in the depot for no reason, rails are electrified, short trains are lengthened.

Todo:
if vehicle count = 0 because we didn't have enough money to buy trains then
Todo:
we should (try to) add trains or wait until we have more money
Todo:
Maybe add a status to routes like [nomoneyfortrains, unprofitable, ...]

Only remove route if we're not waiting for trains to be added

                see above replacement
            if (vehicles.Count() == 1 && route.maxvehicles == 2 && (!AIVehicle.IsStoppedInDepot(vehicles.Begin()))) {
                    if (AIVehicle.GetProfitThisYear(vehicles.Begin()) <= 0) continue;
                    if (AIStation.GetCargoWaiting(route.stasrc, route.crg) > 150) {
                            local railtype = AIRail.GetCurrentRailType();
                            AIRail.SetCurrentRailType(route.railtype);
                            local wagon = WormRailBuilder.ChooseWagon(route.crg, engine_blacklist);
                            if (wagon == null) {
                                    AIRail.SetCurrentRailType(railtype);
                                    return false;
                            }
                            local platform = WormRailBuilder.GetRailStationPlatformLength(route.stasrc);
                            local engine = WormRailBuilder.ChooseTrainEngine(route.crg, AIMap.DistanceManhattan(AIStation.GetLocation(route.stasrc), 
                                    AIStation.GetLocation(route.stadst)), wagon, platform * 2 - 1, engine_blacklist);
                            if (engine == null) {
                                    AIRail.SetCurrentRailType(railtype);
                                    return false;
                            }

Check if we can afford it if (WormMoney.GetMaxBankBalance() > (WormMoney.GetMinimumCashNeeded() + AIEngine.GetPrice(engine) + 4 * AIEngine.GetPrice(wagon))) { if (WormRailManager.AddVehicle(route, vehicles.Begin(), engine, wagon)) { AILog.Info("Added train to route: " + AIStation.GetName(route.stasrc) + " - " + AIStation.GetName(route.stadst)); } } AIRail.SetCurrentRailType(railtype); } }

Todo:
Don't use platform length but compute optimal length for this group!
Todo:
maybe do this for all trains in one go instead of per group probably more efficient
function WormRailManager::CheckTrainProfits ( )

Check train profits and send unprofitable ones to depot to be sold.

Todo:
possible duplicate code with profit checking in air manager. Maybe combine in 1 function?
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:
When maintenance costs are on we should set low profit limit too at least the yearly costs.
Todo:
Don't sell all trans from the same route all at once, try selling 1 per year?
function WormRailManager::GetMinMaxTrainLength ( vehicles  )

Get the minimum and maximum length of trains in the supplied list of vehicles.

Parameters
vehiclesAIVehicleList containing the vehicles.
Returns
A table giving "min_length" and "max_length" of the shortest and longest vehicle in the list.
Note
The length is given in 1/16 of a tile, thus a one tile train has length 16.
Precondition
vehicles should not be null.
function WormRailManager::GetOptimalTrainLength ( route  )

Get the optimal train length for this route.

Parameters
routeThe route info.
Returns
The optimal length in 1/16 of a tile.
Todo:
We should get the amount of cargo our average wagon can carry. Assume 25 for now.
function WormRailManager::HandleVehicleInDepot ( vehicle  )

Handle a vehicle that is stopped in depot for selling, replacement or attaching wagons.

Parameters
vehicleThe vehicle stopped in depot.
function WormRailManager::IsRectangleWithinTownInfluence ( tile  ,
town_id  ,
width  ,
height   
)
static

Checks whether a given rectangle is within the influence of a given town.

Parameters
tileThe topmost tile of the rectangle.
town_idThe TownID of the town to be checked.
widthThe width of the rectangle.
heightThe height of the rectangle.
Returns
True if the rectangle is within the influence of the town.
Todo:
Needs to be moved to a different unit. (Town?)
Note
Taken from SimpleAI.
function WormRailManager::LoadData ( table  ,
worm_save_version   
)

Load all data that WormRailManager from the table.

Parameters
tableThe table that has all the data.
worm_save_versionWormAI save data version.
Precondition
table should be non null.
Todo:
Should we load this?
function WormRailManager::RegisterRoute ( route_data  ,
station_data  ,
vehtype  ,
group  ,
train_length   
)

Register the new route into the database.

Parameters
route_dataA WormRoute class object containing info about the route.
station_dataA WormStation class object containing info about the station.
vehtypeThe type of vehicle using this route. Currently always VT_RAIL.
groupThe vehicle group for the vehicles using this route.
train_lengthThe train_length we start with measure in 1/16th of a tile.
Returns
The new route registered.
Todo:
Also save IsSubsidy, ...
function WormRailManager::ReplaceVehicle ( vehicle  )

Replaces an old vehicle with a newer model if it is already in the depot.

Parameters
vehicleThe vehicle to be replaced.
function WormRailManager::SaveData ( table  )

Save all data that WormRailManager needs in table.

Parameters
tableThe table to store the data in.
Precondition
table should be non null.
Todo:
Should we save this?
\xrefitem todo 53.
Todo:
EventQueue, bridgesupgraded?, inauguration?, bs_building building stage
function WormRailManager::SelectAndAddTrain ( route  )

Select optimal wagon and train engine for the specified route and buy it if possible.

Parameters
routeThe route info table.
Returns
True if we added a train, otherwise false.
function WormRailManager::SendTrainToDepotForSelling ( vehicle  )

Check train profits and send unprofitable ones to depot to be sold.

Parameters
vehicleThe vehicle that should be sent to depot to be sold.
function WormRailManager::SetGroupName ( group  ,
crg  ,
stasrc   
)
static

Set the name of a vehicle group.

Parameters
groupThe GroupID of the group.
crgThe cargo transported.
stasrcThe source station.
Note
Taken from SimpleAI.
Todo:
Move to a different unit, should be accessible from other managers too.
function WormRailManager::SetRailType ( )
static

Sets the current rail type of the AI based on the maximum number of cargoes transportable.

Returns
The new railtype that has been set.
Todo:
Possible better evaluation what rail type is the most profitable.

Member Data Documentation

WormRailManager::BS_NOTHING = 0
static

Building stages, needed to recover a savegame.

WormRailManager::TD_SELL = 1
static

Reasons to send a vehicle to a depot.


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