cargodest.cpp File Reference

Implementation of cargo destinations. More...

#include "stdafx.h"
#include "cargodest_type.h"
#include "cargodest_base.h"
#include "cargodest_func.h"
#include "core/bitmath_func.hpp"
#include "core/random_func.hpp"
#include "core/pool_func.hpp"
#include "cargotype.h"
#include "settings_type.h"
#include "town.h"
#include "industry.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "station_base.h"
#include "pathfinder/yapf/yapf.h"
#include "company_base.h"

Go to the source code of this file.

Data Structures

struct  EnumRandomData
 Information for the town/industry enumerators. More...

Functions

bool CargoHasDestinations (CargoID cid)
 Are cargo destinations for this cargo type enabled?
bool CargoDestinationsDisabled ()
 Are cargo destinations for all cargo types disabled?
static bool IsTownCargo (CargoID cid)
 Should this cargo type primarily have towns as a destination?
static bool IsSymmetricCargo (CargoID cid)
 Does this cargo have a symmetric demand?
static bool IsPassengerCargo (CargoID cid)
 Is this a passenger cargo.
static bool IsNearby (TileIndex t1, TileIndex t2, uint32 dist_square)
 Test whether two tiles are nearby with map-size scaling.
static bool IsTownNearby (const Town *t, TileIndex ti)
 Test whether a tiles is near a town.
static bool IsIndustryNearby (const Industry *ind, TileIndex ti)
 Test whether a tiles is near an industry.
static bool EnumAnyDest (const CargoSourceSink *dest, EnumRandomData *erd)
 Common helper for town/industry enumeration.
static bool EnumAnyTown (const Town *t, void *data)
 Enumerate any town not already a destination and accepting a specific cargo.
static bool EnumCity (const Town *t, void *data)
 Enumerate cities.
static bool EnumBigTown (const Town *t, void *data)
 Enumerate towns with a big population.
static bool EnumNearbyTown (const Town *t, void *data)
 Enumerate nearby towns.
static bool EnumAnyIndustry (const Industry *ind, void *data)
 Enumerate any industry not already a destination and accepting a specific cargo.
static bool EnumNearbyIndustry (const Industry *ind, void *data)
 Enumerate nearby industries.
static bool EnumProducingIndustry (const Industry *ind, void *data)
 Enumerate industries that are producing cargo.
template<typename T >
static bool EnumAnySupplier (const T *css, void *data)
 Enumerate cargo sources supplying a specific cargo.
static bool EnumNearbySupplier (const Industry *ind, void *data)
 Enumerate nearby cargo sources supplying a specific cargo.
static bool EnumNearbySupplier (const Town *t, void *data)
 Enumerate nearby cargo sources supplying a specific cargo.
static CargoSourceSinkFindTownDestination (byte &weight_mod, CargoSourceSink *source, TileIndex source_xy, CargoID cid, const uint8 destclass_chance[4], TownID skip=INVALID_TOWN)
 Find a town as a destination.
static CargoSourceSinkFindIndustryDestination (byte &weight_mod, CargoSourceSink *source, TileIndex source_xy, CargoID cid, IndustryID skip=INVALID_INDUSTRY)
 Find an industry as a destination.
static CargoSourceSinkFindSupplySource (Industry *dest, CargoID cid)
 Find a supply for a cargo type.
static void RemoveLowestLink (CargoSourceSink *source, CargoID cid)
 Remove the link with the lowest weight from a cargo source.
static void CreateNewLinks (CargoSourceSink *source, TileIndex source_xy, CargoID cid, uint chance_a, uint chance_b, const uint8 town_chance[], TownID skip_town, IndustryID skip_ind)
 Create missing cargo links for a source.
static void RemoveInvalidLinks (CargoSourceSink *css)
 Remove invalid links from a cargo source/sink.
void UpdateExpectedLinks (Town *t)
 Updated the desired link count for each cargo.
void UpdateExpectedLinks (Industry *ind)
 Updated the desired link count for each cargo.
void AddMissingIndustryLinks (Industry *ind)
 Make sure an industry has at least one incoming link for each accepted cargo.
void UpdateCargoLinks (Town *t)
 Update the demand links.
void UpdateCargoLinks (Industry *ind)
 Update the demand links.
void UpdateLinkWeights (Town *t)
 Recalculate the link weights.
void UpdateLinkWeights (CargoSourceSink *css)
 Recalculate the link weights.
void RebuildCargoLinkCounts ()
 Rebuild the cached count of incoming cargo links.
void UpdateCargoLinks ()
 Update the demand links of all towns and industries.
static bool EnumAcceptingTown (const Town *t, void *data)
 Enumerate all towns accepting a specific cargo.
static bool EnumAcceptingIndustry (const Industry *ind, void *data)
 Enumerate all industries accepting a specific cargo.
bool MoveCargoWithDestinationToStationWorker (CargoID cid, uint *amount, SourceType source_type, SourceID source_id, const StationList *all_stations, TileIndex src_tile)
 Move a single packet of cargo to a station with destination information.
bool MoveCargoWithDestinationToStation (CargoID cid, uint *amount, SourceType source_type, SourceID source_id, const StationList *all_stations, TileIndex src_tile)
 Move cargo to a station with destination information.
RouteLinkFindRouteLinkForCargo (Station *st, CargoID cid, const CargoPacket *cp, StationID *next_unload, OrderID order, bool *found)
 Get the current best route link for a cargo packet at a station.
void UpdateVehicleRouteLinks (const Vehicle *v, uint32 cargos, bool clear_others, Station *from, OrderID from_oid, StationID to_id, OrderID to_oid, uint32 travel_time)
 Update or create a single route link for a specific vehicle and cargo.
void UpdateVehicleRouteLinks (const Vehicle *v, StationID arrived_at)
 Update route links after a vehicle has arrived at a station.
void PrefillRouteLinks (const Vehicle *v)
 Pre-fill the route links from the orders of a vehicle.
void InvalidateStationRouteLinks (Station *station)
 Remove all route links to and from a station.
void InvalidateOrderRouteLinks (OrderID order)
 Remove all route links referencing an order.
void AgeRouteLinks (Station *st)
 Age and expire route links of a station.

Variables

static const byte LWM_ANYWHERE = 1
 Weight modifier for undetermined destinations.
static const byte LWM_TONY_ANY = 2
 Default weight modifier for towns.
static const byte LWM_TONY_BIG = 3
 Weight modifier for big towns.
static const byte LWM_TONY_CITY = 4
 Weight modifier for cities.
static const byte LWM_TONY_NEARBY = 5
 Weight modifier for nearby towns.
static const byte LWM_INTOWN = 8
 Weight modifier for in-town links.
static const byte LWM_IND_ANY = 2
 Default weight modifier for industries.
static const byte LWM_IND_NEARBY = 3
 Weight modifier for nearby industries.
static const byte LWM_IND_PRODUCING = 4
 Weight modifier for producing industries.
static const uint MAX_EXTRA_LINKS = 2
 Number of extra links allowed.
static const uint MAX_IND_STOCKPILE = 1000
 Maximum stockpile to consider for industry link weight.
static const uint BASE_TOWN_LINKS = 0
 Index into _settings_game.economy.cargodest.base_town_links for normal cargo.
static const uint BASE_TOWN_LINKS_SYMM = 1
 Index into _settings_game.economy.cargodest.base_town_links for symmteric cargos.
static const uint BASE_IND_LINKS = 0
 Index into _settings_game.economy.cargodest.base_ind_links for normal cargo.
static const uint BASE_IND_LINKS_TOWN = 1
 Index into _settings_game.economy.cargodest.base_ind_links for town cargos.
static const uint BASE_IND_LINKS_SYMM = 2
 Index into _settings_game.economy.cargodest.base_ind_links for symmetric cargos.
static const uint BIG_TOWN_POP_MAIL = 0
 Index into _settings_game.economy.cargodest.big_town_pop for mail.
static const uint BIG_TOWN_POP_PAX = 1
 Index into _settings_game.economy.cargodest.big_town_pop for passengers.
static const uint SCALE_TOWN = 0
 Index into _settings_game.economy.cargodest.pop_scale_town/weight_scale_town for normal cargo.
static const uint SCALE_TOWN_BIG = 1
 Index into _settings_game.economy.cargodest.pop_scale_town/weight_scale_town for normal cargo of big towns.
static const uint SCALE_TOWN_PAX = 2
 Index into _settings_game.economy.cargodest.pop_scale_town/weight_scale_town for passengers.
static const uint SCALE_TOWN_BIG_PAX = 3
 Index into _settings_game.economy.cargodest.pop_scale_town/weight_scale_town for passengers of big towns.
static const uint CARGO_SCALE_IND = 0
 Index into _settings_game.economy.cargodest.cargo_scale_ind for normal cargo.
static const uint CARGO_SCALE_IND_TOWN = 1
 Index into _settings_game.economy.cargodest.cargo_scale_ind for town cargos.
static const uint MIN_WEIGHT_TOWN = 0
 Index into _settings_game.economy.cargodest.min_weight_town for normal cargo.
static const uint MIN_WEIGHT_TOWN_PAX = 1
 Index into _settings_game.economy.cargodest.min_weight_town for passengers.
static const uint WEIGHT_SCALE_IND_PROD = 0
 Index into _settings_game.economy.cargodest.weight_scale_ind for produced cargo.
static const uint WEIGHT_SCALE_IND_PILE = 1
 Index into _settings_game.economy.cargodest.weight_scale_ind for stockpiled cargo.
RouteLinkPool _routelink_pool ("RouteLink")

Detailed Description

Implementation of cargo destinations.

Definition in file cargodest.cpp.


Function Documentation

void AddMissingIndustryLinks ( Industry ind  ) 
void AgeRouteLinks ( Station st  ) 

Age and expire route links of a station.

Definition at line 1174 of file cargodest.cpp.

References _settings_game, EconomySettings::cargodest, GameSettings::economy, Station::goods, CargodestSettings::max_route_age, and GoodsEntry::routes.

static void CreateNewLinks ( CargoSourceSink source,
TileIndex  source_xy,
CargoID  cid,
uint  chance_a,
uint  chance_b,
const uint8  town_chance[],
TownID  skip_town,
IndustryID  skip_ind 
) [static]
static bool EnumAcceptingIndustry ( const Industry ind,
void *  data 
) [static]

Enumerate all industries accepting a specific cargo.

Definition at line 802 of file cargodest.cpp.

References Industry::AcceptsCargo().

Referenced by MoveCargoWithDestinationToStationWorker().

static bool EnumAcceptingTown ( const Town t,
void *  data 
) [static]

Enumerate all towns accepting a specific cargo.

Definition at line 796 of file cargodest.cpp.

References Town::AcceptsCargo().

Referenced by MoveCargoWithDestinationToStationWorker().

static bool EnumAnyDest ( const CargoSourceSink dest,
EnumRandomData erd 
) [static]

Common helper for town/industry enumeration.

Definition at line 155 of file cargodest.cpp.

References CargoSourceSink::cargo_links, CargoSourceSink::HasLinkTo(), SmallVector< T, S >::Length(), MAX_EXTRA_LINKS, and CargoSourceSink::num_links_expected.

Referenced by EnumAnyIndustry(), and EnumAnyTown().

static bool EnumAnyIndustry ( const Industry ind,
void *  data 
) [static]

Enumerate any industry not already a destination and accepting a specific cargo.

Definition at line 194 of file cargodest.cpp.

References Industry::AcceptsCargo(), and EnumAnyDest().

Referenced by EnumNearbyIndustry(), EnumProducingIndustry(), and FindIndustryDestination().

template<typename T >
static bool EnumAnySupplier ( const T *  css,
void *  data 
) [inline, static]

Enumerate cargo sources supplying a specific cargo.

Definition at line 215 of file cargodest.cpp.

Referenced by EnumNearbySupplier(), and FindSupplySource().

static bool EnumAnyTown ( const Town t,
void *  data 
) [static]

Enumerate any town not already a destination and accepting a specific cargo.

Definition at line 167 of file cargodest.cpp.

References Town::AcceptsCargo(), and EnumAnyDest().

Referenced by EnumBigTown(), EnumCity(), EnumNearbyTown(), and FindTownDestination().

static bool EnumBigTown ( const Town t,
void *  data 
) [static]
static bool EnumCity ( const Town t,
void *  data 
) [static]

Enumerate cities.

Definition at line 174 of file cargodest.cpp.

References EnumAnyTown().

Referenced by FindTownDestination().

static bool EnumNearbyIndustry ( const Industry ind,
void *  data 
) [static]

Enumerate nearby industries.

Definition at line 201 of file cargodest.cpp.

References EnumAnyIndustry(), and IsIndustryNearby().

Referenced by FindIndustryDestination().

static bool EnumNearbySupplier ( const Town t,
void *  data 
) [static]

Enumerate nearby cargo sources supplying a specific cargo.

Definition at line 228 of file cargodest.cpp.

References EnumAnySupplier(), and IsTownNearby().

static bool EnumNearbySupplier ( const Industry ind,
void *  data 
) [static]

Enumerate nearby cargo sources supplying a specific cargo.

Definition at line 221 of file cargodest.cpp.

References EnumAnySupplier(), and IsIndustryNearby().

Referenced by FindSupplySource().

static bool EnumNearbyTown ( const Town t,
void *  data 
) [static]

Enumerate nearby towns.

Definition at line 187 of file cargodest.cpp.

References EnumAnyTown(), and IsTownNearby().

Referenced by FindTownDestination().

static bool EnumProducingIndustry ( const Industry ind,
void *  data 
) [static]

Enumerate industries that are producing cargo.

Definition at line 208 of file cargodest.cpp.

References CT_INVALID, EnumAnyIndustry(), and Industry::produced_cargo.

Referenced by FindIndustryDestination().

static CargoSourceSink* FindIndustryDestination ( byte &  weight_mod,
CargoSourceSink source,
TileIndex  source_xy,
CargoID  cid,
IndustryID  skip = INVALID_INDUSTRY 
) [static]
RouteLink* FindRouteLinkForCargo ( Station st,
CargoID  cid,
const CargoPacket cp,
StationID next_unload,
OrderID  order,
bool *  found 
)

Get the current best route link for a cargo packet at a station.

Parameters:
st Station the route starts at.
cid Cargo type.
cp Cargo packet with destination information.
order Incoming order of the cargo packet.
[out] found Set to true if a route was found.
Returns:
The preferred route link or NULL if either no suitable link found or the station is the final destination.

Definition at line 962 of file cargodest.cpp.

References SmallVector< T, S >::Append(), CargoPacket::DestinationID(), CargoPacket::DestinationType(), CargoPacket::DestinationXY(), CargoPacket::Flags(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_industry_pool >::Get(), INVALID_SOURCE, ST_INDUSTRY, BaseStation::xy, and YapfChooseRouteLink().

Referenced by CargoList< Tinst >::MoveTo(), and StationCargoList::UpdateCargoNextHop().

static CargoSourceSink* FindSupplySource ( Industry dest,
CargoID  cid 
) [static]
static CargoSourceSink* FindTownDestination ( byte &  weight_mod,
CargoSourceSink source,
TileIndex  source_xy,
CargoID  cid,
const uint8  destclass_chance[4],
TownID  skip = INVALID_TOWN 
) [static]
void InvalidateOrderRouteLinks ( OrderID  order  ) 

Remove all route links referencing an order.

Parameters:
order The order being removed.

Definition at line 1155 of file cargodest.cpp.

References Station::goods, and GoodsEntry::routes.

Referenced by CmdModifyOrder(), and Order::PostDestructor().

void InvalidateStationRouteLinks ( Station station  ) 

Remove all route links to and from a station.

Parameters:
station Station being removed.

Definition at line 1123 of file cargodest.cpp.

References Station::goods, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, and GoodsEntry::routes.

Referenced by Station::~Station().

static bool IsIndustryNearby ( const Industry ind,
TileIndex  ti 
) [static]

Test whether a tiles is near an industry.

Parameters:
ind The industry.
ti The tile to test.
Returns:
True if the tiles is near the town.

Definition at line 149 of file cargodest.cpp.

References _settings_game, EconomySettings::cargodest, GameSettings::economy, CargodestSettings::ind_nearby_dist, IsNearby(), Industry::location, and TileArea::tile.

Referenced by EnumNearbyIndustry(), and EnumNearbySupplier().

static bool IsNearby ( TileIndex  t1,
TileIndex  t2,
uint32  dist_square 
) [static]

Test whether two tiles are nearby with map-size scaling.

Parameters:
t1 First tile.
t2 Second tile.
dist_square Allowed squared distance between the tiles.
Returns:
True if the tiles are nearby.

Definition at line 124 of file cargodest.cpp.

References DistanceSquare(), and ScaleByMapSize1D().

Referenced by IsIndustryNearby(), and IsTownNearby().

static bool IsPassengerCargo ( CargoID  cid  )  [static]

Is this a passenger cargo.

Definition at line 102 of file cargodest.cpp.

References CargoSpec::Get(), TE_PASSENGERS, and CargoSpec::town_effect.

Referenced by EnumBigTown(), Town::GetDestinationWeight(), and UpdateExpectedLinks().

static bool IsTownNearby ( const Town t,
TileIndex  ti 
) [static]

Test whether a tiles is near a town.

Parameters:
t The town.
ti The tile to test.
Returns:
True if the tiles is near the town.

Definition at line 138 of file cargodest.cpp.

References _settings_game, EconomySettings::cargodest, GameSettings::economy, IsNearby(), and CargodestSettings::town_nearby_dist.

Referenced by EnumNearbySupplier(), and EnumNearbyTown().

bool MoveCargoWithDestinationToStation ( CargoID  cid,
uint *  amount,
SourceType  source_type,
SourceID  source_id,
const StationList all_stations,
TileIndex  src_tile 
)

Move cargo to a station with destination information.

Parameters:
cid Cargo type.
amount[in,out] Cargo amount, return is actually moved cargo.
source_type Type of the cargo source.
source_id ID of the cargo source.
all_stations List of possible target stations.
src_tile Source tile.
Returns:
True if the cargo was handled has having destinations.

Definition at line 925 of file cargodest.cpp.

References CargoHasDestinations(), MoveCargoWithDestinationToStationWorker(), and ST_INDUSTRY.

bool MoveCargoWithDestinationToStationWorker ( CargoID  cid,
uint *  amount,
SourceType  source_type,
SourceID  source_id,
const StationList all_stations,
TileIndex  src_tile 
)

Move a single packet of cargo to a station with destination information.

Parameters:
cid Cargo type.
amount[in,out] Cargo amount, return is actually moved cargo.
source_type Type of the cargo source.
source_id ID of the cargo source.
all_stations List of possible target stations.
src_tile Source tile.
Returns:
True if the cargo was handled has having destinations.

Definition at line 817 of file cargodest.cpp.

References _settings_game, CargoLink::amount, CargoSourceSink::cargo_links, EconomySettings::cargodest, CargoLink::dest, DistanceSquare(), GameSettings::economy, SmallVector< T, S >::End(), EnumAcceptingIndustry(), EnumAcceptingTown(), SmallVector< T, S >::Find(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), SpecializedStation< Station, false >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_industry_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_town_pool >::Get(), CargoSourceSink::GetID(), RouteLink::GetOriginOrderId(), Industry::GetRandom(), Town::GetRandom(), CargoSourceSink::GetRandomLink(), StationFinder::GetStations(), CargoSourceSink::GetTileForDestination(), CargoSourceSink::GetType(), Station::goods, INVALID_ORDER, IsSymmetricCargo(), SmallVector< T, S >::Length(), LWM_ANYWHERE, CargodestSettings::max_route_penalty, TransportedCargoStat::new_act, TransportedCargoStat::new_max, ST_HEADQUARTERS, ST_INDUSTRY, ST_TOWN, TileArea::tile, and YapfChooseRouteLink().

Referenced by MoveCargoWithDestinationToStation().

void PrefillRouteLinks ( const Vehicle v  ) 
void RebuildCargoLinkCounts (  ) 

Rebuild the cached count of incoming cargo links.

Definition at line 691 of file cargodest.cpp.

References SmallVector< T, S >::Begin(), CargoSourceSink::cargo_links, SmallVector< T, S >::End(), lengthof, MemSetT(), and CargoSourceSink::num_incoming_links.

Referenced by InitializeWindowsAndCaches().

static void RemoveInvalidLinks ( CargoSourceSink css  )  [static]
static void RemoveLowestLink ( CargoSourceSink source,
CargoID  cid 
) [static]

Remove the link with the lowest weight from a cargo source.

The reverse link is removed as well if the cargo has symmetric demand.

Parameters:
source Remove the link from this cargo source.
cid Cargo type of the link to remove.

Definition at line 356 of file cargodest.cpp.

References SmallVector< T, S >::Begin(), CargoSourceSink::cargo_links, CargoLink::dest, SmallVector< T, S >::End(), SmallVector< T, S >::Erase(), SmallVector< T, S >::Find(), CargoSourceSink::HasLinkTo(), IsSymmetricCargo(), LWM_ANYWHERE, and CargoSourceSink::num_incoming_links.

Referenced by CreateNewLinks().

void UpdateCargoLinks (  ) 
void UpdateCargoLinks ( Industry ind  ) 
void UpdateCargoLinks ( Town t  ) 
void UpdateExpectedLinks ( Industry ind  ) 
void UpdateExpectedLinks ( Town t  ) 
void UpdateLinkWeights ( CargoSourceSink css  ) 
void UpdateLinkWeights ( Town t  ) 
void UpdateVehicleRouteLinks ( const Vehicle v,
StationID  arrived_at 
)
void UpdateVehicleRouteLinks ( const Vehicle v,
uint32  cargos,
bool  clear_others,
Station from,
OrderID  from_oid,
StationID  to_id,
OrderID  to_oid,
uint32  travel_time 
)

Update or create a single route link for a specific vehicle and cargo.

Parameters:
v The vehicle.
cargos Create links for the cargo types whose bit is set.
clear_others Should route links for cargo types nor carried be cleared?
from Originating station.
from_oid Originating order.
to_id Destination station ID.
to_oid Destination order.
travel_time Travel time for the route.

Definition at line 999 of file cargodest.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_routelink_pool >::CanAllocateItem(), CargoHasDestinations(), Station::goods, HasBit(), Vehicle::owner, GoodsEntry::routes, and BaseVehicle::type.

Referenced by Vehicle::BeginLoading(), PrefillRouteLinks(), and UpdateVehicleRouteLinks().


Generated on Fri Jun 3 05:19:02 2011 for OpenTTD by  doxygen 1.6.1