OpenTTD Source  12.0-beta2
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 #include <vector>
16 #include <map>
17 #include <set>
18 
23 public:
24  static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false);
25 
26 protected:
31  enum RefreshFlags {
37  };
38 
42  struct RefitDesc {
44  uint16 capacity;
45  uint16 remaining;
46  RefitDesc(CargoID cargo, uint16 capacity, uint16 remaining) :
48  };
49 
59  struct Hop {
60  OrderID from;
61  OrderID to;
63 
68  Hop() {NOT_REACHED();}
69 
76  Hop(OrderID from, OrderID to, CargoID cargo) : from(from), to(to), cargo(cargo) {}
77  bool operator<(const Hop &other) const;
78  };
79 
80  typedef std::vector<RefitDesc> RefitList;
81  typedef std::set<Hop> HopSet;
82 
85  RefitList refit_capacities;
86  HopSet *seen_hops;
88  bool allow_merge;
90 
92 
93  bool HandleRefit(CargoID refit_cargo);
94  void ResetRefit();
95  void RefreshStats(const Order *cur, const Order *next);
96  const Order *PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
97 
98  void RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
99 };
100 
101 #endif /* REFRESH_H */
LinkRefresher::Hop::from
OrderID from
Last order where vehicle could interact with cargo or absolute first order.
Definition: refresh.h:60
LinkRefresher::Hop::cargo
CargoID cargo
Cargo the consist is probably carrying or CT_INVALID if unknown.
Definition: refresh.h:62
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
LinkRefresher::RefreshFlags
RefreshFlags
Various flags about properties of the last examined link that might have an influence on the next one...
Definition: refresh.h:31
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
LinkRefresher::cargo
CargoID cargo
Cargo given in last refit order.
Definition: refresh.h:87
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:84
LinkRefresher::Hop::Hop
Hop()
Default constructor should not be called but has to be visible for usage in std::set.
Definition: refresh.h:68
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
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:265
LinkRefresher::vehicle
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition: refresh.h:83
LinkRefresher
Utility to refresh links a consist will visit.
Definition: refresh.h:22
LinkRefresher::Hop::Hop
Hop(OrderID from, OrderID to, CargoID cargo)
Real constructor, only use this one.
Definition: refresh.h:76
LinkRefresher::RefitDesc::capacity
uint16 capacity
Capacity the vehicle will have.
Definition: refresh.h:44
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:86
LinkRefresher::allow_merge
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
Definition: refresh.h:88
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
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:43
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::Hop
A hop the refresh algorithm might evaluate.
Definition: refresh.h:59
LinkRefresher::RefitDesc::remaining
uint16 remaining
Capacity remaining from before the previous refit.
Definition: refresh.h:45
NUM_CARGO
@ NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:65
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
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
Order
Definition: order_base.h:33
LinkRefresher::RefitDesc
Simulated cargo type and capacity for prediction of future links.
Definition: refresh.h:42
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