OpenTTD Source  20241121-master-g67a0fccfad
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 
20 public:
21  static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false);
22 
23 protected:
28  enum RefreshFlags {
34  };
35 
39  struct RefitDesc {
41  uint16_t capacity;
42  uint16_t remaining;
43  RefitDesc(CargoID cargo, uint16_t capacity, uint16_t remaining) :
45  };
46 
56  struct Hop {
57  OrderID from;
58  OrderID to;
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 
82  RefitList refit_capacities;
83  HopSet *seen_hops;
85  bool allow_merge;
87 
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 */
uint8_t CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
static const CargoID NUM_CARGO
Maximum number of cargo types in a game.
Definition: cargo_type.h:74
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:85
void ResetRefit()
Restore capacities and refit_capacities as vehicle might have been able to load now.
Definition: refresh.cpp:145
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
RefreshFlags
Various flags about properties of the last examined link that might have an influence on the next one...
Definition: refresh.h:28
@ HAS_CARGO
Consist could leave the last stop where it could interact with cargo carrying cargo (i....
Definition: refresh.h:30
@ RESET_REFIT
Consist had a chance to load since the last refit and the refit capacities can be reset.
Definition: refresh.h:32
@ WAS_REFIT
Consist was refit since the last stop where it could interact with cargo.
Definition: refresh.h:31
@ IN_AUTOREFIT
Currently doing an autorefit loop. Ignore the first autorefit order.
Definition: refresh.h:33
@ USE_NEXT
There was a conditional jump. Try to use the given next order when looking for a new one.
Definition: refresh.h:29
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition: refresh.h:80
bool HandleRefit(CargoID refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
Definition: refresh.cpp:90
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
CargoID cargo
Cargo given in last refit order.
Definition: refresh.h:84
RefitList refit_capacities
Current state of capacity remaining from previous refits versus overall capacity per vehicle in the c...
Definition: refresh.h:82
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
Definition: refresh.cpp:69
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
uint capacities[NUM_CARGO]
Current added capacities per cargo ID in the consist.
Definition: refresh.h:81
bool is_full_loading
If the vehicle is full loading.
Definition: refresh.h:86
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
void RefreshStats(const Order *cur, const Order *next)
Refresh link stats for the given pair of orders.
Definition: refresh.cpp:201
A hop the refresh algorithm might evaluate.
Definition: refresh.h:56
OrderID from
Last order where vehicle could interact with cargo or absolute first order.
Definition: refresh.h:57
OrderID to
Next order to be processed.
Definition: refresh.h:58
Hop(OrderID from, OrderID to, CargoID cargo)
Real constructor, only use this one.
Definition: refresh.h:73
CargoID cargo
Cargo the consist is probably carrying or INVALID_CARGO if unknown.
Definition: refresh.h:59
Hop()
Default constructor should not be called but has to be visible for usage in std::set.
Definition: refresh.h:65
bool operator<(const Hop &other) const
Comparison operator to allow hops to be used in a std::set.
Definition: refresh.cpp:46
Simulated cargo type and capacity for prediction of future links.
Definition: refresh.h:39
CargoID cargo
Cargo type the vehicle will be carrying.
Definition: refresh.h:40
uint16_t remaining
Capacity remaining from before the previous refit.
Definition: refresh.h:42
uint16_t capacity
Capacity the vehicle will have.
Definition: refresh.h:41
Vehicle data structure.
Definition: vehicle_base.h:244