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

OpenTTD Source 15.3

refresh.h
Go to the documentation of this file.
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#ifndef REFRESH_H
11#define REFRESH_H
12
13#include "../cargo_type.h"
14#include "../vehicle_base.h"
15
19 class LinkRefresher {
20public:
21 static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false);
22
23protected:
28 enum class RefreshFlag : uint8_t {
29 UseNext,
30 HasCargo,
31 WasRefit,
32 ResetRefit,
33 InAutorefit,
34 };
35
36 using RefreshFlags = EnumBitSet<RefreshFlag, uint8_t>;
37
41 struct RefitDesc {
42 CargoType cargo;
43 uint16_t capacity;
44 uint16_t remaining;
45 RefitDesc(CargoType cargo, uint16_t capacity, uint16_t remaining) :
46 cargo(cargo), capacity(capacity), remaining(remaining) {}
47 };
48
58 struct Hop {
59 VehicleOrderID from;
60 VehicleOrderID to;
61 CargoType cargo;
62
67 Hop() {NOT_REACHED();}
68
75 Hop(VehicleOrderID from, VehicleOrderID to, CargoType cargo) : from(from), to(to), cargo(cargo) {}
76
77 constexpr auto operator<=>(const Hop &) const noexcept = default;
78 };
79
80 typedef std::vector<RefitDesc> RefitList;
81 typedef std::set<Hop> HopSet;
82
83 Vehicle *vehicle;
84 CargoArray capacities{};
85 RefitList refit_capacities;
86 HopSet *seen_hops;
87 CargoType cargo;
88 bool allow_merge;
89 bool is_full_loading;
90
91 LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading);
92
93 bool HandleRefit(CargoType refit_cargo);
94 void ResetRefit();
95 void RefreshStats(VehicleOrderID cur, VehicleOrderID next);
96 VehicleOrderID PredictNextOrder(VehicleOrderID cur, VehicleOrderID next, RefreshFlags flags, uint num_hops = 0);
97
98 void RefreshLinks(VehicleOrderID cur, VehicleOrderID next, RefreshFlags flags, uint num_hops = 0);
99};
100
101#endif /* REFRESH_H */
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
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
RefreshFlag
Various flags about properties of the last examined link that might have an influence on the next one...
Definition refresh.h:28
LinkRefresher::RefreshFlag::UseNext
@ UseNext
There was a conditional jump. Try to use the given next order when looking for a new one.
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::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
VehicleOrderID
uint8_t VehicleOrderID
The index of an order within its current vehicle (not pool related)
Definition order_type.h:18
CargoArray
Class for storing amounts of cargo.
Definition cargo_type.h:111
LinkRefresher::Hop
A hop the refresh algorithm might evaluate.
Definition refresh.h:58
LinkRefresher::Hop::from
VehicleOrderID from
Last order where vehicle could interact with cargo or absolute first order.
Definition refresh.h:59
LinkRefresher::Hop::Hop
Hop()
Default constructor should not be called but has to be visible for usage in std::set.
Definition refresh.h:67
LinkRefresher::Hop::Hop
Hop(VehicleOrderID from, VehicleOrderID to, CargoType cargo)
Real constructor, only use this one.
Definition refresh.h:75
LinkRefresher::Hop::cargo
CargoType cargo
Cargo the consist is probably carrying or INVALID_CARGO if unknown.
Definition refresh.h:61
LinkRefresher::Hop::to
VehicleOrderID to
Next order to be processed.
Definition refresh.h:60
LinkRefresher::RefitDesc
Simulated cargo type and capacity for prediction of future links.
Definition refresh.h:41
LinkRefresher::RefitDesc::cargo
CargoType cargo
Cargo type the vehicle will be carrying.
Definition refresh.h:42
LinkRefresher::RefitDesc::remaining
uint16_t remaining
Capacity remaining from before the previous refit.
Definition refresh.h:44
LinkRefresher::RefitDesc::capacity
uint16_t capacity
Capacity the vehicle will have.
Definition refresh.h:43
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205