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

OpenTTD Source 13.1

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. */
32 const Order *first = v->orders->GetNextDecisionNode(v->GetOrder(v->cur_implicit_order_index), 0);
33 if (first == nullptr) 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 != INVALID_STATION ? 1 << HAS_CARGO : 0);
39 }
40
46 bool LinkRefresher::Hop::operator<(const Hop &other) const
47 {
48 if (this->from < other.from) {
49 return true;
50 } else if (this->from > other.from) {
51 return false;
52 }
53 if (this->to < other.to) {
54 return true;
55 } else if (this->to > other.to) {
56 return false;
57 }
58 return this->cargo < other.cargo;
59 }
60
69 LinkRefresher::LinkRefresher(Vehicle *vehicle, HopSet *seen_hops, bool allow_merge, bool is_full_loading) :
70 vehicle(vehicle), seen_hops(seen_hops), cargo(CT_INVALID), allow_merge(allow_merge),
71 is_full_loading(is_full_loading)
72 {
73 memset(this->capacities, 0, sizeof(this->capacities));
74
75 /* Assemble list of capacities and set last loading stations to 0. */
76 for (Vehicle *v = this->vehicle; v != nullptr; v = v->Next()) {
77 this->refit_capacities.push_back(RefitDesc(v->cargo_type, v->cargo_cap, v->refit_cap));
78 if (v->refit_cap > 0) {
79 assert(v->cargo_type < NUM_CARGO);
80 this->capacities[v->cargo_type] += v->refit_cap;
81 }
82 }
83 }
84
90 bool LinkRefresher::HandleRefit(CargoID refit_cargo)
91 {
92 this->cargo = refit_cargo;
93 RefitList::iterator refit_it = this->refit_capacities.begin();
94 bool any_refit = false;
95 for (Vehicle *v = this->vehicle; v != nullptr; v = v->Next()) {
96 const Engine *e = Engine::Get(v->engine_type);
97 if (!HasBit(e->info.refit_mask, this->cargo)) {
98 ++refit_it;
99 continue;
100 }
101 any_refit = true;
102
103 /* Back up the vehicle's cargo type */
104 CargoID temp_cid = v->cargo_type;
105 byte temp_subtype = v->cargo_subtype;
106 v->cargo_type = this->cargo;
107 v->cargo_subtype = GetBestFittingSubType(v, v, this->cargo);
108
109 uint16 mail_capacity = 0;
110 uint amount = e->DetermineCapacity(v, &mail_capacity);
111
112 /* Restore the original cargo type */
113 v->cargo_type = temp_cid;
114 v->cargo_subtype = temp_subtype;
115
116 /* Skip on next refit. */
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;
123 }
124 refit_it->capacity = amount;
125 refit_it->cargo = this->cargo;
126
127 ++refit_it;
128
129 /* Special case for aircraft with mail. */
130 if (v->type == VEH_AIRCRAFT) {
131 if (mail_capacity < refit_it->remaining) {
132 this->capacities[refit_it->cargo] -= refit_it->remaining - mail_capacity;
133 refit_it->remaining = mail_capacity;
134 }
135 refit_it->capacity = mail_capacity;
136 break; // aircraft have only one vehicle
137 }
138 }
139 return any_refit;
140 }
141
145 void LinkRefresher::ResetRefit()
146 {
147 for (RefitList::iterator it(this->refit_capacities.begin()); it != this->refit_capacities.end(); ++it) {
148 if (it->remaining == it->capacity) continue;
149 this->capacities[it->cargo] += it->capacity - it->remaining;
150 it->remaining = it->capacity;
151 }
152 }
153
163 const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops)
164 {
165 /* next is good if it's either nullptr (then the caller will stop the
166 * evaluation) or if it's not conditional and the caller allows it to be
167 * chosen (by setting USE_NEXT). */
168 while (next != nullptr && (!HasBit(flags, USE_NEXT) || next->IsType(OT_CONDITIONAL))) {
169
170 /* After the first step any further non-conditional order is good,
171 * regardless of previous USE_NEXT settings. The case of cur and next or
172 * their respective stations being equal is handled elsewhere. */
173 SetBit(flags, USE_NEXT);
174
175 if (next->IsType(OT_CONDITIONAL)) {
176 const Order *skip_to = this->vehicle->orders->GetNextDecisionNode(
177 this->vehicle->orders->GetOrderAt(next->GetConditionSkipToOrder()), num_hops);
178 if (skip_to != nullptr && num_hops < this->vehicle->orders->GetNumOrders()) {
179 /* Make copies of capacity tracking lists. There is potential
180 * for optimization here: If the vehicle never refits we don't
181 * need to copy anything. Also, if we've seen the branched link
182 * before we don't need to branch at all. */
183 LinkRefresher branch(*this);
184 branch.RefreshLinks(cur, skip_to, flags, num_hops + 1);
185 }
186 }
187
188 /* Reassign next with the following stop. This can be a station or a
189 * depot.*/
190 next = this->vehicle->orders->GetNextDecisionNode(
191 this->vehicle->orders->GetNext(next), num_hops++);
192 }
193 return next;
194 }
195
201 void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
202 {
203 StationID next_station = next->GetDestination();
204 Station *st = Station::GetIfValid(cur->GetDestination());
205 if (st != nullptr && next_station != INVALID_STATION && next_station != st->index) {
206 Station *st_to = Station::Get(next_station);
207 for (CargoID c = 0; c < NUM_CARGO; c++) {
208 /* Refresh the link and give it a minimum capacity. */
209
210 uint cargo_quantity = this->capacities[c];
211 if (cargo_quantity == 0) continue;
212
213 if (this->vehicle->GetDisplayMaxSpeed() == 0) continue;
214
215 /* If not allowed to merge link graphs, make sure the stations are
216 * already in the same link graph. */
217 if (!this->allow_merge && st->goods[c].link_graph != st_to->goods[c].link_graph) {
218 continue;
219 }
220
221 /* A link is at least partly restricted if a vehicle can't load at its source. */
222 EdgeUpdateMode restricted_mode = (cur->GetLoadType() & OLFB_NO_LOAD) == 0 ?
223 EUM_UNRESTRICTED : EUM_RESTRICTED;
224 /* This estimates the travel time of the link as the time needed
225 * to travel between the stations at half the max speed of the consist.
226 * The result is in tiles/tick (= 2048 km-ish/h). */
227 uint32 time_estimate = DistanceManhattan(st->xy, st_to->xy) * 4096U / this->vehicle->GetDisplayMaxSpeed();
228
229 /* If the vehicle is currently full loading, increase the capacities at the station
230 * where it is loading by an estimate of what it would have transported if it wasn't
231 * loading. Don't do that if the vehicle has been waiting for longer than the entire
232 * order list is supposed to take, though. If that is the case the total duration is
233 * probably far off and we'd greatly overestimate the capacity by increasing.*/
234 if (this->is_full_loading && this->vehicle->orders != nullptr &&
235 st->index == vehicle->last_station_visited &&
236 this->vehicle->orders->GetTotalDuration() >
237 (Ticks)this->vehicle->current_order_time) {
238 uint effective_capacity = cargo_quantity * this->vehicle->load_unload_ticks;
239 if (effective_capacity > (uint)this->vehicle->orders->GetTotalDuration()) {
240 IncreaseStats(st, c, next_station, effective_capacity /
241 this->vehicle->orders->GetTotalDuration(), 0, 0,
242 EUM_INCREASE | restricted_mode);
243 } else if (RandomRange(this->vehicle->orders->GetTotalDuration()) < effective_capacity) {
244 IncreaseStats(st, c, next_station, 1, 0, 0, EUM_INCREASE | restricted_mode);
245 } else {
246 IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, EUM_REFRESH | restricted_mode);
247 }
248 } else {
249 IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, EUM_REFRESH | restricted_mode);
250 }
251 }
252 }
253 }
254
266 void LinkRefresher::RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops)
267 {
268 while (next != nullptr) {
269
270 if ((next->IsType(OT_GOTO_DEPOT) || next->IsType(OT_GOTO_STATION)) && next->IsRefit()) {
271 SetBit(flags, WAS_REFIT);
272 if (!next->IsAutoRefit()) {
273 this->HandleRefit(next->GetRefitCargo());
274 } else if (!HasBit(flags, IN_AUTOREFIT)) {
275 SetBit(flags, IN_AUTOREFIT);
276 LinkRefresher backup(*this);
277 for (CargoID c = 0; c != NUM_CARGO; ++c) {
278 if (CargoSpec::Get(c)->IsValid() && this->HandleRefit(c)) {
279 this->RefreshLinks(cur, next, flags, num_hops);
280 *this = backup;
281 }
282 }
283 }
284 }
285
286 /* Only reset the refit capacities if the "previous" next is a station,
287 * meaning that either the vehicle was refit at the previous station or
288 * it wasn't at all refit during the current hop. */
289 if (HasBit(flags, WAS_REFIT) && (next->IsType(OT_GOTO_STATION) || next->IsType(OT_IMPLICIT))) {
290 SetBit(flags, RESET_REFIT);
291 } else {
292 ClrBit(flags, RESET_REFIT);
293 }
294
295 next = this->PredictNextOrder(cur, next, flags, num_hops);
296 if (next == nullptr) break;
297 Hop hop(cur->index, next->index, this->cargo);
298 if (this->seen_hops->find(hop) != this->seen_hops->end()) {
299 break;
300 } else {
301 this->seen_hops->insert(hop);
302 }
303
304 /* Don't use the same order again, but choose a new one in the next round. */
305 ClrBit(flags, USE_NEXT);
306
307 /* Skip resetting and link refreshing if next order won't do anything with cargo. */
308 if (!next->IsType(OT_GOTO_STATION) && !next->IsType(OT_IMPLICIT)) continue;
309
310 if (HasBit(flags, RESET_REFIT)) {
311 this->ResetRefit();
312 ClrBit(flags, RESET_REFIT);
313 ClrBit(flags, WAS_REFIT);
314 }
315
316 if (cur->IsType(OT_GOTO_STATION) || cur->IsType(OT_IMPLICIT)) {
317 if (cur->CanLeaveWithCargo(HasBit(flags, HAS_CARGO))) {
318 SetBit(flags, HAS_CARGO);
319 this->RefreshStats(cur, next);
320 } else {
321 ClrBit(flags, HAS_CARGO);
322 }
323 }
324
325 /* "cur" is only assigned here if the stop is a station so that
326 * whenever stats are to be increased two stations can be found. */
327 cur = next;
328 }
329 }
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
LinkRefresher::Hop::from
OrderID from
Last order where vehicle could interact with cargo or absolute first order.
Definition: refresh.h:60
BaseConsist::cur_implicit_order_index
VehicleOrderID cur_implicit_order_index
The index to the current implicit order.
Definition: base_consist.h:29
Order::IsRefit
bool IsRefit() const
Is this order a refit order.
Definition: order_base.h:118
Station::goods
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Definition: station_base.h:483
LinkRefresher::Hop::cargo
CargoID cargo
Cargo the consist is probably carrying or CT_INVALID if unknown.
Definition: refresh.h:62
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:71
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
LinkRefresher::Hop::to
OrderID to
Next order to be processed.
Definition: refresh.h:61
LinkRefresher::IN_AUTOREFIT
@ IN_AUTOREFIT
Currently doing an autorefit loop. Ignore the first autorefit order.
Definition: refresh.h:36
OrderList::GetTotalDuration
Ticks GetTotalDuration() const
Gets the known duration of the vehicles orders, timetabled or not.
Definition: order_base.h:386
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:610
Vehicle::GetDisplayMaxSpeed
virtual int GetDisplayMaxSpeed() const
Gets the maximum speed in km-ish/h that can be sent into SetDParam for string processing.
Definition: vehicle_base.h:513
Station
Station data structure.
Definition: station_base.h:454
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:104
LinkRefresher::USE_NEXT
@ USE_NEXT
There was a conditional jump. Try to use the given next order when looking for a new one.
Definition: refresh.h:32
LinkRefresher::PredictNextOrder
const Order * PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops=0)
Predict the next order the vehicle will execute and resolve conditionals by recursion and return next...
Definition: refresh.cpp:163
EUM_UNRESTRICTED
@ EUM_UNRESTRICTED
Use unrestricted link.
Definition: linkgraph_type.h:56
Vehicle::load_unload_ticks
uint16 load_unload_ticks
Ticks to wait before starting next cycle.
Definition: vehicle_base.h:340
LinkRefresher::cargo
CargoID cargo
Cargo given in last refit order.
Definition: refresh.h:87
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:118
LinkRefresher::LinkRefresher
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
Definition: refresh.cpp:69
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
LinkRefresher::capacities
uint capacities[NUM_CARGO]
Current added capacities per cargo ID in the consist.
Definition: refresh.h:84
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
EUM_INCREASE
@ EUM_INCREASE
Increase capacity.
Definition: linkgraph_type.h:53
SpecializedStation< Station, false >::Get
static Station * Get(size_t index)
Gets station with given index.
Definition: base_station_base.h:218
RandomRange
static uint32 RandomRange(uint32 limit)
Pick a random number between 0 and limit - 1, inclusive.
Definition: random_func.hpp:81
Engine
Definition: engine_base.h:36
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
LinkRefresher::RefreshLinks
void RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops=0)
Iterate over orders starting at cur and next and refresh links associated with them.
Definition: refresh.cpp:266
LinkRefresher::vehicle
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition: refresh.h:83
EdgeUpdateMode
EdgeUpdateMode
Special modes for updating links.
Definition: linkgraph_type.h:52
OrderList::GetNextDecisionNode
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...
Definition: order_cmd.cpp:380
LinkRefresher
Utility to refresh links a consist will visit.
Definition: refresh.h:22
LinkRefresher::HandleRefit
bool HandleRefit(CargoID refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
Definition: refresh.cpp:90
DistanceManhattan
uint DistanceManhattan(TileIndex t0, TileIndex t1)
Gets the Manhattan distance between the two given tiles.
Definition: map.cpp:157
Order::GetRefitCargo
CargoID GetRefitCargo() const
Get the cargo to to refit to.
Definition: order_base.h:132
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::allow_merge
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
Definition: refresh.h:88
Vehicle::last_station_visited
StationID last_station_visited
The last station we stopped at.
Definition: vehicle_base.h:316
IncreaseStats
void IncreaseStats(Station *st, CargoID cargo, StationID next_station_id, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode)
Increase capacity for a link stat given by station cargo and next hop.
Definition: station_cmd.cpp:3704
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:317
Ticks
int32 Ticks
The type to store ticks in.
Definition: date_type.h:16
LinkRefresher::is_full_loading
bool is_full_loading
If the vehicle is full loading.
Definition: refresh.h:89
LinkRefresher::RESET_REFIT
@ RESET_REFIT
Consist had a chance to load since the last refit and the refit capacities can be reset.
Definition: refresh.h:35
Order::GetConditionSkipToOrder
VehicleOrderID GetConditionSkipToOrder() const
Get the order to skip to.
Definition: order_base.h:153
GoodsEntry::link_graph
LinkGraphID link_graph
Link graph this station belongs to.
Definition: station_base.h:254
EUM_REFRESH
@ EUM_REFRESH
Refresh capacity.
Definition: linkgraph_type.h:54
OrderList::GetOrderAt
Order * GetOrderAt(int index) const
Get a certain order of the order chain.
Definition: order_cmd.cpp:357
LinkRefresher::RefreshStats
void RefreshStats(const Order *cur, const Order *next)
Refresh link stats for the given pair of orders.
Definition: refresh.cpp:201
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
refresh.h
LinkRefresher::Hop
A hop the refresh algorithm might evaluate.
Definition: refresh.h:59
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
linkgraph.h
Order::IsAutoRefit
bool IsAutoRefit() const
Is this order a auto-refit order.
Definition: order_base.h:125
BaseStation::xy
TileIndex xy
Base tile of the station.
Definition: base_station_base.h:53
EUM_RESTRICTED
@ EUM_RESTRICTED
Use restricted link.
Definition: linkgraph_type.h:55
OrderList::GetNext
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.
Definition: order_base.h:316
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
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
SpecializedStation< Station, false >::GetIfValid
static Station * GetIfValid(size_t index)
Returns station if the index is a valid index for this station type.
Definition: base_station_base.h:227
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:197
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
LinkRefresher::ResetRefit
void ResetRefit()
Restore capacities and refit_capacities as vehicle might have been able to load now.
Definition: refresh.cpp:145
LinkRefresher::HAS_CARGO
@ HAS_CARGO
Consist could leave the last stop where it could interact with cargo carrying cargo (i....
Definition: refresh.h:33
LinkRefresher::WAS_REFIT
@ WAS_REFIT
Consist was refit since the last stop where it could interact with cargo.
Definition: refresh.h:34
Vehicle::GetOrder
Order * GetOrder(int index) const
Returns order 'index' of a vehicle or nullptr when it doesn't exists.
Definition: vehicle_base.h:890
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
Order::CanLeaveWithCargo
bool CanLeaveWithCargo(bool has_cargo) const
A vehicle can leave the current station with cargo if:
Definition: order_cmd.cpp:2213
Vehicle::orders
OrderList * orders
Pointer to the order list for this vehicle.
Definition: vehicle_base.h:336
Order
Definition: order_base.h:36
GetBestFittingSubType
byte GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_cargo_type)
Get the best fitting subtype when 'cloning'/'replacing' v_from with v_for.
Definition: vehicle_gui.cpp:439
OrderList::GetNumOrders
VehicleOrderID GetNumOrders() const
Get number of orders in the order list.
Definition: order_base.h:322
LinkRefresher::RefitDesc
Simulated cargo type and capacity for prediction of future links.
Definition: refresh.h:42
OLFB_NO_LOAD
@ OLFB_NO_LOAD
Do not load anything.
Definition: order_type.h:66
LinkRefresher::Hop::operator<
bool operator<(const Hop &other) const
Comparison operator to allow hops to be used in a std::set.
Definition: refresh.cpp:46
Order::GetLoadType
OrderLoadFlags GetLoadType() const
How must the consist be loaded?
Definition: order_base.h:137