OpenTTD Source 20250524-master-gc366e6a48e
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 <http://www.gnu.org/licenses/>.
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. */
33 if (first == INVALID_VEH_ORDER_ID) return;
34
35 HopSet seen_hops;
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
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
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
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. */
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
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() & OLFB_NO_LOAD) == 0 ?
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 &&
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
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()) {
258 if (!next_order->IsAutoRefit()) {
259 this->HandleRefit(next_order->GetRefitCargo());
260 } else if (!flags.Test(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))) {
277 } else {
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. */
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();
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))) {
308 this->RefreshStats(cur, next);
309 } else {
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}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:23
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:75
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
Enum-as-bit-set wrapper.
Utility to refresh links a consist will visit.
Definition refresh.h:19
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
Definition refresh.h:88
void ResetRefit()
Restore capacities and refit_capacities as vehicle might have been able to load now.
Definition refresh.cpp:123
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
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
void RefreshStats(VehicleOrderID cur, VehicleOrderID next)
Refresh link stats for the given pair of orders.
Definition refresh.cpp:181
bool HandleRefit(CargoType refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
Definition refresh.cpp:68
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition refresh.h:83
@ UseNext
There was a conditional jump. Try to use the given next order when looking for a new one.
@ ResetRefit
Consist had a chance to load since the last refit and the refit capacities can be reset.
@ WasRefit
Consist was refit since the last stop where it could interact with cargo.
@ HasCargo
Consist could leave the last stop where it could interact with cargo carrying cargo (i....
@ InAutorefit
Currently doing an autorefit loop. Ignore the first autorefit order.
RefitList refit_capacities
Current state of capacity remaining from previous refits versus overall capacity per vehicle in the c...
Definition refresh.h:85
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
Definition refresh.cpp:49
CargoType cargo
Cargo given in last refit order.
Definition refresh.h:87
CargoArray capacities
Current added capacities per cargo type in the consist.
Definition refresh.h:84
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
bool is_full_loading
If the vehicle is full loading.
Definition refresh.h:89
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
Declaration of link graph classes used for cargo distribution.
EdgeUpdateMode
Special modes for updating links.
@ Refresh
Refresh capacity.
@ Unrestricted
Use unrestricted link.
@ Increase
Increase capacity.
@ Restricted
Use restricted link.
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition map.cpp:142
uint8_t VehicleOrderID
The index of an order within its current vehicle (not pool related)
Definition order_type.h:18
@ OLFB_NO_LOAD
Do not load anything.
Definition order_type.h:81
static const VehicleOrderID INVALID_VEH_ORDER_ID
Invalid vehicle order index (sentinel)
Definition order_type.h:39
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, 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.
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 type.
Definition cargotype.h:137
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:201
A hop the refresh algorithm might evaluate.
Definition refresh.h:58
Simulated cargo type and capacity for prediction of future links.
Definition refresh.h:41
Shared order list linking together the linked list of orders and the list of vehicles sharing this or...
Definition order_base.h:264
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:352
VehicleOrderID GetNumOrders() const
Get number of orders in the order list.
Definition order_base.h:362
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...
TimerGameTick::Ticks GetTotalDuration() const
Gets the known duration of the vehicles orders, timetabled or not.
Definition order_base.h:423
const Order * GetOrderAt(VehicleOrderID index) const
Get a certain order of the order chain.
Definition order_base.h:328
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
CargoType GetRefitCargo() const
Get the cargo to to refit to.
Definition order_base.h:127
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.
Definition order_base.h:120
bool IsRefit() const
Is this order a refit order.
Definition order_base.h:113
static Titem * Get(auto index)
Returns Titem with given index.
Tindex index
Index of this pool item.
static Station * Get(auto index)
Gets station with given index.
static Station * GetIfValid(auto index)
Returns station if the index is a valid index for this station type.
Station data structure.
std::array< GoodsEntry, NUM_CARGO > goods
Goods at this station.
Vehicle data structure.
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 string parameters for string processing.
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, CargoType dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
@ VEH_AIRCRAFT
Aircraft vehicle type.