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)
 Register the new route into the database.
function CheckRoutes ()
 Checks all routes.
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.

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
 List 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.

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::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.
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:
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::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::RegisterRoute ( route_data  ,
station_data  ,
vehtype  ,
group   
)

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