10 #include "../stdafx.h"
11 #include "../core/bitmath_func.hpp"
12 #include "../station_func.h"
13 #include "../engine_base.h"
14 #include "../vehicle_func.h"
18 #include "../safeguards.h"
29 if (v->
orders ==
nullptr)
return;
33 if (first ==
nullptr)
return;
50 }
else if (this->
from > other.
from) {
53 if (this->
to < other.
to) {
55 }
else if (this->
to > other.
to) {
76 for (
Vehicle *v = this->vehicle; v !=
nullptr; v = v->
Next()) {
78 if (v->refit_cap > 0) {
80 this->
capacities[v->cargo_type] += v->refit_cap;
92 this->
cargo = refit_cargo;
94 bool any_refit =
false;
97 if (!
HasBit(e->info.refit_mask, this->cargo)) {
104 CargoID temp_cid = v->cargo_type;
105 uint8_t temp_subtype = v->cargo_subtype;
106 v->cargo_type = this->
cargo;
109 uint16_t mail_capacity = 0;
113 v->cargo_type = temp_cid;
114 v->cargo_subtype = temp_subtype;
117 if (this->
cargo != refit_it->cargo && refit_it->remaining > 0) {
118 this->
capacities[refit_it->cargo] -= refit_it->remaining;
119 refit_it->remaining = 0;
120 }
else if (amount < refit_it->remaining) {
121 this->
capacities[refit_it->cargo] -= refit_it->remaining - amount;
122 refit_it->remaining = amount;
124 refit_it->capacity = amount;
125 refit_it->cargo = this->
cargo;
131 if (mail_capacity < refit_it->remaining) {
132 this->
capacities[refit_it->cargo] -= refit_it->remaining - mail_capacity;
133 refit_it->remaining = mail_capacity;
135 refit_it->capacity = mail_capacity;
148 if (it.remaining == it.capacity)
continue;
149 this->
capacities[it.cargo] += it.capacity - it.remaining;
150 it.remaining = it.capacity;
175 if (next->
IsType(OT_CONDITIONAL)) {
205 if (st !=
nullptr && next_station != INVALID_STATION && next_station != st->
index) {
211 if (cargo_quantity == 0)
continue;
236 this->vehicle->orders->GetTotalDuration() > this->vehicle->current_order_time) {
267 while (next !=
nullptr) {
295 if (next ==
nullptr)
break;
297 if (this->
seen_hops->find(hop) != this->seen_hops->end()) {
307 if (!next->
IsType(OT_GOTO_STATION) && !next->
IsType(OT_IMPLICIT))
continue;
315 if (cur->
IsType(OT_GOTO_STATION) || cur->
IsType(OT_IMPLICIT)) {
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Utility to refresh links a consist will visit.
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
void ResetRefit()
Restore capacities and refit_capacities as vehicle might have been able to load now.
void RefreshLinks(const Order *cur, const Order *next, uint8_t flags, uint num_hops=0)
Iterate over orders starting at cur and next and refresh links associated with them.
@ HAS_CARGO
Consist could leave the last stop where it could interact with cargo carrying cargo (i....
@ RESET_REFIT
Consist had a chance to load since the last refit and the refit capacities can be reset.
@ WAS_REFIT
Consist was refit since the last stop where it could interact with cargo.
@ IN_AUTOREFIT
Currently doing an autorefit loop. Ignore the first autorefit order.
@ USE_NEXT
There was a conditional jump. Try to use the given next order when looking for a new one.
Vehicle * vehicle
Vehicle for which the links should be refreshed.
bool HandleRefit(CargoID refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
const Order * PredictNextOrder(const Order *cur, const Order *next, uint8_t flags, uint num_hops=0)
Predict the next order the vehicle will execute and resolve conditionals by recursion and return next...
CargoID cargo
Cargo given in last refit order.
RefitList refit_capacities
Current state of capacity remaining from previous refits versus overall capacity per vehicle in the c...
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
HopSet * seen_hops
Hops already seen. If the same hop is seen twice we stop the algorithm. This is shared between all Re...
uint capacities[NUM_CARGO]
Current added capacities per cargo ID in the consist.
bool is_full_loading
If the vehicle is full loading.
static void Run(Vehicle *v, bool allow_merge=true, bool is_full_loading=false)
Refresh all links the given vehicle will visit.
void RefreshStats(const Order *cur, const Order *next)
Refresh link stats for the given pair of orders.
Declaration of link graph classes used for cargo distribution.
EdgeUpdateMode
Special modes for updating links.
@ EUM_REFRESH
Refresh capacity.
@ EUM_INCREASE
Increase capacity.
@ EUM_RESTRICTED
Use restricted link.
@ EUM_UNRESTRICTED
Use unrestricted link.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
@ OLFB_NO_LOAD
Do not load anything.
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
Definition of link refreshing utility.
void IncreaseStats(Station *st, CargoID cargo, StationID next_station_id, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode)
Increase capacity for a link stat given by station cargo and next hop.
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
TileIndex xy
Base tile of the station.
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
LinkGraphID link_graph
Link graph this station belongs to.
A hop the refresh algorithm might evaluate.
OrderID from
Last order where vehicle could interact with cargo or absolute first order.
OrderID to
Next order to be processed.
CargoID cargo
Cargo the consist is probably carrying or INVALID_CARGO if unknown.
bool operator<(const Hop &other) const
Comparison operator to allow hops to be used in a std::set.
Simulated cargo type and capacity for prediction of future links.
const Order * GetNextDecisionNode(const Order *next, uint hops) const
Get the next order which will make the given vehicle stop at a station or refit at a depot or evaluat...
Order * GetOrderAt(int index) const
Get a certain order of the order chain.
const Order * GetNext(const Order *curr) const
Get the order after the given one or the first one, if the given one is the last one.
VehicleOrderID GetNumOrders() const
Get number of orders in the order list.
TimerGameTick::Ticks GetTotalDuration() const
Gets the known duration of the vehicles orders, timetabled or not.
CargoID GetRefitCargo() const
Get the cargo to to refit to.
DestinationID GetDestination() const
Gets the destination of this order.
bool IsType(OrderType type) const
Check whether this order is of the given type.
VehicleOrderID GetConditionSkipToOrder() const
Get the order to skip to.
OrderLoadFlags GetLoadType() const
How must the consist be loaded?
bool CanLeaveWithCargo(bool has_cargo) const
A vehicle can leave the current station with cargo if:
bool IsAutoRefit() const
Is this order a auto-refit order.
bool IsRefit() const
Is this order a refit order.
Tindex index
Index of this pool item.
static Titem * Get(size_t index)
Returns Titem with given index.
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
static Station * Get(size_t index)
Gets station with given index.
GoodsEntry goods[NUM_CARGO]
Goods at this station.
StationID last_loading_station
Last station the vehicle has stopped at and could possibly leave from with any cargo loaded.
virtual int GetDisplayMaxSpeed() const
Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
Order * GetOrder(int index) const
Returns order 'index' of a vehicle or nullptr when it doesn't exists.
Vehicle * Next() const
Get the next vehicle of this vehicle.
OrderList * orders
Pointer to the order list for this vehicle.
uint16_t load_unload_ticks
Ticks to wait before starting next cycle.
StationID last_station_visited
The last station we stopped at.
uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
@ VEH_AIRCRAFT
Aircraft vehicle type.