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

OpenTTD Source 14.0-beta3

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 <http://www.gnu.org/licenses/>.
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 {
20 public:
21 static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false);
22
23 protected:
28 enum RefreshFlags {
29 USE_NEXT,
30 HAS_CARGO,
31 WAS_REFIT,
32 RESET_REFIT,
33 IN_AUTOREFIT,
34 };
35
39 struct RefitDesc {
40 CargoID cargo;
41 uint16_t capacity;
42 uint16_t remaining;
43 RefitDesc(CargoID cargo, uint16_t capacity, uint16_t remaining) :
44 cargo(cargo), capacity(capacity), remaining(remaining) {}
45 };
46
56 struct Hop {
57 OrderID from;
58 OrderID to;
59 CargoID cargo;
60
65 Hop() {NOT_REACHED();}
66
73 Hop(OrderID from, OrderID to, CargoID cargo) : from(from), to(to), cargo(cargo) {}
74 bool operator<(const Hop &other) const;
75 };
76
77 typedef std::vector<RefitDesc> RefitList;
78 typedef std::set<Hop> HopSet;
79
80 Vehicle *vehicle;
81 uint capacities[NUM_CARGO];
82 RefitList refit_capacities;
83 HopSet *seen_hops;
84 CargoID cargo;
85 bool allow_merge;
86 bool is_full_loading;
87
88 LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading);
89
90 bool HandleRefit(CargoID refit_cargo);
91 void ResetRefit();
92 void RefreshStats(const Order *cur, const Order *next);
93 const Order *PredictNextOrder(const Order *cur, const Order *next, uint8_t flags, uint num_hops = 0);
94
95 void RefreshLinks(const Order *cur, const Order *next, uint8_t flags, uint num_hops = 0);
96 };
97
98 #endif /* REFRESH_H */
LinkRefresher::Hop::from
OrderID from
Last order where vehicle could interact with cargo or absolute first order.
Definition: refresh.h:57
LinkRefresher::Hop::cargo
CargoID cargo
Cargo the consist is probably carrying or INVALID_CARGO if unknown.
Definition: refresh.h:59
LinkRefresher::Hop::to
OrderID to
Next order to be processed.
Definition: refresh.h:58
LinkRefresher::IN_AUTOREFIT
@ IN_AUTOREFIT
Currently doing an autorefit loop. Ignore the first autorefit order.
Definition: refresh.h:33
LinkRefresher::RefreshFlags
RefreshFlags
Various flags about properties of the last examined link that might have an influence on the next one...
Definition: refresh.h:28
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:29
LinkRefresher::RefitDesc::capacity
uint16_t capacity
Capacity the vehicle will have.
Definition: refresh.h:41
LinkRefresher::cargo
CargoID cargo
Cargo given in last refit order.
Definition: refresh.h:84
LinkRefresher::LinkRefresher
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
Definition: refresh.cpp:69
LinkRefresher::capacities
uint capacities[NUM_CARGO]
Current added capacities per cargo ID in the consist.
Definition: refresh.h:81
LinkRefresher::Hop::Hop
Hop()
Default constructor should not be called but has to be visible for usage in std::set.
Definition: refresh.h:65
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
LinkRefresher::RefreshLinks
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.
Definition: refresh.cpp:265
LinkRefresher::vehicle
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition: refresh.h:80
LinkRefresher::PredictNextOrder
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...
Definition: refresh.cpp:163
LinkRefresher
Utility to refresh links a consist will visit.
Definition: refresh.h:19
LinkRefresher::Hop::Hop
Hop(OrderID from, OrderID to, CargoID cargo)
Real constructor, only use this one.
Definition: refresh.h:73
LinkRefresher::HandleRefit
bool HandleRefit(CargoID refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
Definition: refresh.cpp:90
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:83
LinkRefresher::allow_merge
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
Definition: refresh.h:85
LinkRefresher::RefitDesc::remaining
uint16_t remaining
Capacity remaining from before the previous refit.
Definition: refresh.h:42
LinkRefresher::is_full_loading
bool is_full_loading
If the vehicle is full loading.
Definition: refresh.h:86
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
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:32
LinkRefresher::RefreshStats
void RefreshStats(const Order *cur, const Order *next)
Refresh link stats for the given pair of orders.
Definition: refresh.cpp:201
LinkRefresher::RefitDesc::cargo
CargoID cargo
Cargo type the vehicle will be carrying.
Definition: refresh.h:40
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:82
LinkRefresher::Hop
A hop the refresh algorithm might evaluate.
Definition: refresh.h:56
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
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:30
LinkRefresher::WAS_REFIT
@ WAS_REFIT
Consist was refit since the last stop where it could interact with cargo.
Definition: refresh.h:31
NUM_CARGO
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:74
Order
Definition: order_base.h:36
LinkRefresher::RefitDesc
Simulated cargo type and capacity for prediction of future links.
Definition: refresh.h:39
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