OpenTTD Source: src/linkgraph/refresh.cpp Source File

OpenTTD Source 15.0-RC2

refresh.cpp
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
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"
15#include "refresh.h"
16#include "linkgraph.h"
17
18#include "../safeguards.h"
19
26/* static */ void LinkRefresher::Run(Vehicle *v, bool allow_merge, bool is_full_loading)
27{
28 /* If there are no orders we can't predict anything.*/
29 if (v->orders == nullptr) return;
30
31 /* Make sure the first order is a useful order. */
32 VehicleOrderID first = v->orders->GetNextDecisionNode(v->cur_implicit_order_index, 0);
33 if (first == INVALID_VEH_ORDER_ID) return;
34
35 HopSet seen_hops;
36 LinkRefresher refresher(v, &seen_hops, allow_merge, is_full_loading);
37
38 refresher.RefreshLinks(first, first, v->last_loading_station != StationID::Invalid() ? RefreshFlags{RefreshFlag::HasCargo} : RefreshFlags{});
39}
40
49LinkRefresher::LinkRefresher(Vehicle *vehicle, HopSet *seen_hops, bool allow_merge, bool is_full_loading) :
50 vehicle(vehicle), seen_hops(seen_hops), cargo(INVALID_CARGO), allow_merge(allow_merge),
51 is_full_loading(is_full_loading)
52{
53 /* Assemble list of capacities and set last loading stations to 0. */
54 for (Vehicle *v = this->vehicle; v != nullptr; v = v->Next()) {
55 this->refit_capacities.push_back(RefitDesc(v->cargo_type, v->cargo_cap, v->refit_cap));
56 if (v->refit_cap > 0) {
57 assert(v->cargo_type < NUM_CARGO);
58 this->capacities[v->cargo_type] += v->refit_cap;
59 }
60 }
61}
62
68 bool LinkRefresher::HandleRefit(CargoType refit_cargo)
69{
70 this->cargo = refit_cargo;
71 RefitList::iterator refit_it = this->refit_capacities.begin();
72 bool any_refit = false;
73 for (Vehicle *v = this->vehicle; v != nullptr; v = v->Next()) {
74 const Engine *e = Engine::Get(v->engine_type);
75 if (!HasBit(e->info.refit_mask, this->cargo)) {
76 ++refit_it;
77 continue;
78 }
79 any_refit = true;
80
81 /* Back up the vehicle's cargo type */
82 CargoType temp_cargo_type = v->cargo_type;
83 uint8_t temp_subtype = v->cargo_subtype;
84 v->cargo_type = this->cargo;
85 v->cargo_subtype = GetBestFittingSubType(v, v, this->cargo);
86
87 uint16_t mail_capacity = 0;
88 uint amount = e->DetermineCapacity(v, &mail_capacity);
89
90 /* Restore the original cargo type */
91 v->cargo_type = temp_cargo_type;
92 v->cargo_subtype = temp_subtype;
93
94 /* Skip on next refit. */
95 if (this->cargo != refit_it->cargo && refit_it->remaining > 0) {
96 this->capacities[refit_it->cargo] -= refit_it->remaining;
97 refit_it->remaining = 0;
98 } else if (amount < refit_it->remaining) {
99 this->capacities[refit_it->cargo] -= refit_it->remaining - amount;
100 refit_it->remaining = amount;
101 }
102 refit_it->capacity = amount;
103 refit_it->cargo = this->cargo;
104
105 ++refit_it;
106
107 /* Special case for aircraft with mail. */
108 if (v->type == VEH_AIRCRAFT) {
109 if (mail_capacity < refit_it->remaining) {
110 this->capacities[refit_it->cargo] -= refit_it->remaining - mail_capacity;
111 refit_it->remaining = mail_capacity;
112 }
113 refit_it->capacity = mail_capacity;
114 break; // aircraft have only one vehicle
115 }
116 }
117 return any_refit;
118}
119
123 void LinkRefresher::ResetRefit()
124{
125 for (auto &it : this->refit_capacities) {
126 if (it.remaining == it.capacity) continue;
127 this->capacities[it.cargo] += it.capacity - it.remaining;
128 it.remaining = it.capacity;
129 }
130}
131
141 VehicleOrderID LinkRefresher::PredictNextOrder(VehicleOrderID cur, VehicleOrderID next, RefreshFlags flags, uint num_hops)
142{
143 assert(this->vehicle->orders != nullptr);
144 const OrderList &orderlist = *this->vehicle->orders;
145 auto orders = orderlist.GetOrders();
146
147 /* next is good if it's either nullptr (then the caller will stop the
148 * evaluation) or if it's not conditional and the caller allows it to be
149 * chosen (by setting RefreshFlag::UseNext). */
150 while (next < orderlist.GetNumOrders() && (!flags.Test(RefreshFlag::UseNext) || orders[next].IsType(OT_CONDITIONAL))) {
151
152 /* After the first step any further non-conditional order is good,
153 * regardless of previous RefreshFlag::UseNext settings. The case of cur and next or
154 * their respective stations being equal is handled elsewhere. */
155 flags.Set(RefreshFlag::UseNext);
156
157 if (orders[next].IsType(OT_CONDITIONAL)) {
158 VehicleOrderID skip_to = orderlist.GetNextDecisionNode(orders[next].GetConditionSkipToOrder(), num_hops);
159 if (skip_to != INVALID_VEH_ORDER_ID && num_hops < orderlist.GetNumOrders()) {
160 /* Make copies of capacity tracking lists. There is potential
161 * for optimization here: If the vehicle never refits we don't
162 * need to copy anything. Also, if we've seen the branched link
163 * before we don't need to branch at all. */
164 LinkRefresher branch(*this);
165 branch.RefreshLinks(cur, skip_to, flags, num_hops + 1);
166 }
167 }
168
169 /* Reassign next with the following stop. This can be a station or a
170 * depot.*/
171 next = orderlist.GetNextDecisionNode(orderlist.GetNext(next), num_hops++);
172 }
173 return next;
174}
175
181 void LinkRefresher::RefreshStats(VehicleOrderID cur, VehicleOrderID next)
182{
183 assert(this->vehicle->orders != nullptr);
184 const OrderList &orderlist = *this->vehicle->orders;
185 auto orders = orderlist.GetOrders();
186
187 StationID next_station = orders[next].GetDestination().ToStationID();
188 Station *st = Station::GetIfValid(orders[cur].GetDestination().ToStationID());
189 if (st != nullptr && next_station != StationID::Invalid() && next_station != st->index) {
190 Station *st_to = Station::Get(next_station);
191 for (CargoType cargo = 0; cargo < NUM_CARGO; ++cargo) {
192 /* Refresh the link and give it a minimum capacity. */
193
194 uint cargo_quantity = this->capacities[cargo];
195 if (cargo_quantity == 0) continue;
196
197 if (this->vehicle->GetDisplayMaxSpeed() == 0) continue;
198
199 /* If not allowed to merge link graphs, make sure the stations are
200 * already in the same link graph. */
201 if (!this->allow_merge && st->goods[cargo].link_graph != st_to->goods[cargo].link_graph) {
202 continue;
203 }
204
205 /* A link is at least partly restricted if a vehicle can't load at its source. */
206 EdgeUpdateMode restricted_mode = orders[cur].GetLoadType() != OrderLoadType::NoLoad ?
207 EdgeUpdateMode::Unrestricted : EdgeUpdateMode::Restricted;
208 /* This estimates the travel time of the link as the time needed
209 * to travel between the stations at half the max speed of the consist.
210 * The result is in tiles/tick (= 2048 km-ish/h). */
211 uint32_t time_estimate = DistanceManhattan(st->xy, st_to->xy) * 4096U / this->vehicle->GetDisplayMaxSpeed();
212
213 /* If the vehicle is currently full loading, increase the capacities at the station
214 * where it is loading by an estimate of what it would have transported if it wasn't
215 * loading. Don't do that if the vehicle has been waiting for longer than the entire
216 * order list is supposed to take, though. If that is the case the total duration is
217 * probably far off and we'd greatly overestimate the capacity by increasing.*/
218 if (this->is_full_loading && this->vehicle->orders != nullptr &&
219 st->index == vehicle->last_station_visited &&
220 this->vehicle->orders->GetTotalDuration() > this->vehicle->current_order_time) {
221 uint effective_capacity = cargo_quantity * this->vehicle->load_unload_ticks;
222 if (effective_capacity > (uint)this->vehicle->orders->GetTotalDuration()) {
223 IncreaseStats(st, cargo, next_station, effective_capacity /
224 this->vehicle->orders->GetTotalDuration(), 0, 0,
225 {EdgeUpdateMode::Increase, restricted_mode});
226 } else if (RandomRange(this->vehicle->orders->GetTotalDuration()) < effective_capacity) {
227 IncreaseStats(st, cargo, next_station, 1, 0, 0, {EdgeUpdateMode::Increase, restricted_mode});
228 } else {
229 IncreaseStats(st, cargo, next_station, cargo_quantity, 0, time_estimate, {EdgeUpdateMode::Refresh, restricted_mode});
230 }
231 } else {
232 IncreaseStats(st, cargo, next_station, cargo_quantity, 0, time_estimate, {EdgeUpdateMode::Refresh, restricted_mode});
233 }
234 }
235 }
236}
237
249 void LinkRefresher::RefreshLinks(VehicleOrderID cur, VehicleOrderID next, RefreshFlags flags, uint num_hops)
250{
251 assert(this->vehicle->orders != nullptr);
252 const OrderList &orderlist = *this->vehicle->orders;
253 while (next < orderlist.GetNumOrders()) {
254 const Order *next_order = orderlist.GetOrderAt(next);
255
256 if ((next_order->IsType(OT_GOTO_DEPOT) || next_order->IsType(OT_GOTO_STATION)) && next_order->IsRefit()) {
257 flags.Set(RefreshFlag::WasRefit);
258 if (!next_order->IsAutoRefit()) {
259 this->HandleRefit(next_order->GetRefitCargo());
260 } else if (!flags.Test(RefreshFlag::InAutorefit)) {
261 flags.Set(RefreshFlag::InAutorefit);
262 LinkRefresher backup(*this);
263 for (CargoType cargo = 0; cargo != NUM_CARGO; ++cargo) {
264 if (CargoSpec::Get(cargo)->IsValid() && this->HandleRefit(cargo)) {
265 this->RefreshLinks(cur, next, flags, num_hops);
266 *this = backup;
267 }
268 }
269 }
270 }
271
272 /* Only reset the refit capacities if the "previous" next is a station,
273 * meaning that either the vehicle was refit at the previous station or
274 * it wasn't at all refit during the current hop. */
275 if (flags.Test(RefreshFlag::WasRefit) && (next_order->IsType(OT_GOTO_STATION) || next_order->IsType(OT_IMPLICIT))) {
276 flags.Set(RefreshFlag::ResetRefit);
277 } else {
278 flags.Reset(RefreshFlag::ResetRefit);
279 }
280
281 next = this->PredictNextOrder(cur, next, flags, num_hops);
282 if (next == INVALID_VEH_ORDER_ID) break;
283 Hop hop(cur, next, this->cargo);
284 if (this->seen_hops->find(hop) != this->seen_hops->end()) {
285 break;
286 } else {
287 this->seen_hops->insert(hop);
288 }
289
290 next_order = orderlist.GetOrderAt(next);
291
292 /* Don't use the same order again, but choose a new one in the next round. */
293 flags.Reset(RefreshFlag::UseNext);
294
295 /* Skip resetting and link refreshing if next order won't do anything with cargo. */
296 if (!next_order->IsType(OT_GOTO_STATION) && !next_order->IsType(OT_IMPLICIT)) continue;
297
298 if (flags.Test(RefreshFlag::ResetRefit)) {
299 this->ResetRefit();
300 flags.Reset({RefreshFlag::ResetRefit, RefreshFlag::WasRefit});
301 }
302
303 const Order *cur_order = orderlist.GetOrderAt(cur);
304
305 if (cur_order->IsType(OT_GOTO_STATION) || cur_order->IsType(OT_IMPLICIT)) {
306 if (cur_order->CanLeaveWithCargo(flags.Test(RefreshFlag::HasCargo))) {
307 flags.Set(RefreshFlag::HasCargo);
308 this->RefreshStats(cur, next);
309 } else {
310 flags.Reset(RefreshFlag::HasCargo);
311 }
312 }
313
314 /* "cur" is only assigned here if the stop is a station so that
315 * whenever stats are to be increased two stations can be found. */
316 cur = next;
317 }
318}
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
Engine
Definition engine_base.h:38
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:200
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
LinkRefresher
Utility to refresh links a consist will visit.
Definition refresh.h:19
LinkRefresher::allow_merge
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
Definition refresh.h:88
LinkRefresher::ResetRefit
void ResetRefit()
Restore capacities and refit_capacities as vehicle might have been able to load now.
Definition refresh.cpp:123
LinkRefresher::PredictNextOrder
VehicleOrderID PredictNextOrder(VehicleOrderID cur, VehicleOrderID next, RefreshFlags flags, uint num_hops=0)
Predict the next order the vehicle will execute and resolve conditionals by recursion and return next...
Definition refresh.cpp:141
LinkRefresher::RefreshLinks
void RefreshLinks(VehicleOrderID cur, VehicleOrderID next, RefreshFlags flags, uint num_hops=0)
Iterate over orders starting at cur and next and refresh links associated with them.
Definition refresh.cpp:249
LinkRefresher::RefreshStats
void RefreshStats(VehicleOrderID cur, VehicleOrderID next)
Refresh link stats for the given pair of orders.
Definition refresh.cpp:181
LinkRefresher::HandleRefit
bool HandleRefit(CargoType refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
Definition refresh.cpp:68
LinkRefresher::vehicle
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition refresh.h:83
LinkRefresher::RefreshFlag::UseNext
@ UseNext
There was a conditional jump. Try to use the given next order when looking for a new one.
LinkRefresher::RefreshFlag::ResetRefit
@ ResetRefit
Consist had a chance to load since the last refit and the refit capacities can be reset.
LinkRefresher::RefreshFlag::WasRefit
@ WasRefit
Consist was refit since the last stop where it could interact with cargo.
LinkRefresher::RefreshFlag::HasCargo
@ HasCargo
Consist could leave the last stop where it could interact with cargo carrying cargo (i....
LinkRefresher::RefreshFlag::InAutorefit
@ InAutorefit
Currently doing an autorefit loop. Ignore the first autorefit order.
LinkRefresher::refit_capacities
RefitList refit_capacities
Current state of capacity remaining from previous refits versus overall capacity per vehicle in the c...
Definition refresh.h:85
LinkRefresher::LinkRefresher
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
Definition refresh.cpp:49
LinkRefresher::cargo
CargoType cargo
Cargo given in last refit order.
Definition refresh.h:87
LinkRefresher::capacities
CargoArray capacities
Current added capacities per cargo type in the consist.
Definition refresh.h:84
LinkRefresher::seen_hops
HopSet * seen_hops
Hops already seen. If the same hop is seen twice we stop the algorithm. This is shared between all Re...
Definition refresh.h:86
LinkRefresher::is_full_loading
bool is_full_loading
If the vehicle is full loading.
Definition refresh.h:89
LinkRefresher::Run
static void Run(Vehicle *v, bool allow_merge=true, bool is_full_loading=false)
Refresh all links the given vehicle will visit.
Definition refresh.cpp:26
linkgraph.h
Declaration of link graph classes used for cargo distribution.
EdgeUpdateMode
EdgeUpdateMode
Special modes for updating links.
Definition linkgraph_type.h:46
EdgeUpdateMode::Refresh
@ Refresh
Refresh capacity.
EdgeUpdateMode::Unrestricted
@ Unrestricted
Use unrestricted link.
EdgeUpdateMode::Increase
@ Increase
Increase capacity.
EdgeUpdateMode::Restricted
@ Restricted
Use restricted link.
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:158
VehicleOrderID
uint8_t VehicleOrderID
The index of an order within its current vehicle (not pool related)
Definition order_type.h:18
OrderLoadType::NoLoad
@ NoLoad
Do not load anything.
INVALID_VEH_ORDER_ID
static const VehicleOrderID INVALID_VEH_ORDER_ID
Invalid vehicle order index (sentinel)
Definition order_type.h:39
RandomRange
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 random_func.hpp:88
refresh.h
Definition of link refreshing utility.
IncreaseStats
void IncreaseStats(Station *st, CargoType cargo, StationID next_station_id, uint capacity, uint usage, uint32_t time, EdgeUpdateModes modes)
Increase capacity for a link stat given by station cargo and next hop.
Definition station_cmd.cpp:4248
BaseConsist::cur_implicit_order_index
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
Definition base_consist.h:48
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition base_station_base.h:59
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
LinkRefresher::Hop
A hop the refresh algorithm might evaluate.
Definition refresh.h:58
LinkRefresher::RefitDesc
Simulated cargo type and capacity for prediction of future links.
Definition refresh.h:41
OrderList
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:274
OrderList::GetNext
VehicleOrderID GetNext(VehicleOrderID cur) const
Get the order after the given one or the first one, if the given one is the last one.
Definition order_base.h:362
OrderList::GetNumOrders
VehicleOrderID GetNumOrders() const
Get number of orders in the order list.
Definition order_base.h:372
OrderList::GetNextDecisionNode
VehicleOrderID GetNextDecisionNode(VehicleOrderID 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...
Definition order_cmd.cpp:327
OrderList::GetTotalDuration
TimerGameTick::Ticks GetTotalDuration() const
Gets the known duration of the vehicles orders, timetabled or not.
Definition order_base.h:433
OrderList::GetOrderAt
const Order * GetOrderAt(VehicleOrderID index) const
Get a certain order of the order chain.
Definition order_base.h:338
Order
Definition order_base.h:33
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
Order::GetRefitCargo
CargoType GetRefitCargo() const
Get the cargo to to refit to.
Definition order_base.h:127
Order::CanLeaveWithCargo
bool CanLeaveWithCargo(bool has_cargo) const
A vehicle can leave the current station with cargo if:
Definition order_cmd.cpp:2197
Order::IsAutoRefit
bool IsAutoRefit() const
Is this order a auto-refit order.
Definition order_base.h:120
Order::IsRefit
bool IsRefit() const
Is this order a refit order.
Definition order_base.h:113
PoolID< uint16_t, struct StationIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
SpecializedStation< Station, false >::Get
static Station * Get(auto index)
Gets station with given index.
Definition base_station_base.h:248
SpecializedStation< Station, false >::GetIfValid
static Station * GetIfValid(auto index)
Returns station if the index is a valid index for this station type.
Definition base_station_base.h:257
Station
Station data structure.
Definition station_base.h:500
Station::goods
std::array< GoodsEntry, NUM_CARGO > goods
Goods at this station.
Definition station_base.h:529
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::last_loading_station
StationID last_loading_station
Last station the vehicle has stopped at and could possibly leave from with any cargo loaded.
Definition vehicle_base.h:293
Vehicle::GetDisplayMaxSpeed
virtual int GetDisplayMaxSpeed() const
Gets the maximum speed in km-ish/h that can be sent into string parameters for string processing.
Definition vehicle_base.h:485
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition vehicle_base.h:587
Vehicle::orders
OrderList * orders
Pointer to the order list for this vehicle.
Definition vehicle_base.h:314
Vehicle::load_unload_ticks
uint16_t load_unload_ticks
Ticks to wait before starting next cycle.
Definition vehicle_base.h:307
Vehicle::last_station_visited
StationID last_station_visited
The last station we stopped at.
Definition vehicle_base.h:292
GetBestFittingSubType
uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoType dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
Definition vehicle_gui.cpp:600
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition vehicle_type.h:28