OpenTTD

refresh.h
Go to the documentation of this file.
1 /* $Id: refresh.h 27614 2016-07-10 12:17:00Z fonsinchen $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef REFRESH_H
13 #define REFRESH_H
14
15 #include "../cargo_type.h"
16 #include "../vehicle_base.h"
17 #include <vector>
18 #include <map>
19 #include <set>
20
24 class LinkRefresher {
25 public:
26 static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false);
27
28 protected:
33 enum RefreshFlags {
34 USE_NEXT,
35 HAS_CARGO,
36 WAS_REFIT,
37 RESET_REFIT,
38 IN_AUTOREFIT,
39 };
40
44 struct RefitDesc {
45 CargoID cargo;
46 uint16 capacity;
47 uint16 remaining;
48 RefitDesc(CargoID cargo, uint16 capacity, uint16 remaining) :
49 cargo(cargo), capacity(capacity), remaining(remaining) {}
50 };
51
61 struct Hop {
62 OrderID from;
63 OrderID to;
64 CargoID cargo;
65
70 Hop() {NOT_REACHED();}
71
78 Hop(OrderID from, OrderID to, CargoID cargo) : from(from), to(to), cargo(cargo) {}
79 bool operator<(const Hop &other) const;
80 };
81
82 typedef std::vector<RefitDesc> RefitList;
83 typedef std::set<Hop> HopSet;
84
85 Vehicle *vehicle;
86 uint capacities[NUM_CARGO];
87 RefitList refit_capacities;
88 HopSet *seen_hops;
89 CargoID cargo;
90 bool allow_merge;
91 bool is_full_loading;
92
93 LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading);
94
95 bool HandleRefit(CargoID refit_cargo);
96 void ResetRefit();
97 void RefreshStats(const Order *cur, const Order *next);
98 const Order *PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
99
100 void RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
101 };
102
103 #endif /* REFRESH_H */
LinkRefresher::Hop::cargo
CargoID cargo
Cargo the consist is probably carrying or CT_INVALID if unknown.
Definition: refresh.h:64
LinkRefresher::Hop::from
OrderID from
Last order where vehicle could interact with cargo or absolute first order.
Definition: refresh.h:62
LinkRefresher::RESET_REFIT
Consist had a chance to load since the last refit and the refit capacities can be reset...
Definition: refresh.h:37
LinkRefresher::RefreshStats
void RefreshStats(const Order *cur, const Order *next)
Refresh link stats for the given pair of orders.
Definition: refresh.cpp:203
LinkRefresher::cargo
CargoID cargo
Cargo given in last refit order.
Definition: refresh.h:89
LinkRefresher::is_full_loading
bool is_full_loading
If the vehicle is full loading.
Definition: refresh.h:91
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:212
LinkRefresher::Hop::Hop
Hop()
Default constructor should not be called but has to be visible for usage in std::set.
Definition: refresh.h:70
LinkRefresher::RefitDesc::capacity
uint16 capacity
Capacity the vehicle will have.
Definition: refresh.h:46
Order
Definition: order_base.h:34
LinkRefresher::ResetRefit
void ResetRefit()
Restore capacities and refit_capacities as vehicle might have been able to load now.
Definition: refresh.cpp:147
LinkRefresher::RefitDesc::cargo
CargoID cargo
Cargo type the vehicle will be carrying.
Definition: refresh.h:45
LinkRefresher::HAS_CARGO
Consist could leave the last stop where it could interact with cargo carrying cargo (i...
Definition: refresh.h:35
LinkRefresher::vehicle
Vehicle * vehicle
Vehicle for which the links should be refreshed.
Definition: refresh.h:85
LinkRefresher::capacities
uint capacities[NUM_CARGO]
Current added capacities per cargo ID in the consist.
Definition: refresh.h:86
LinkRefresher::allow_merge
bool allow_merge
If the refresher is allowed to merge or extend link graphs.
Definition: refresh.h:90
LinkRefresher::Hop::Hop
Hop(OrderID from, OrderID to, CargoID cargo)
Real constructor, only use this one.
Definition: refresh.h:78
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:28
LinkRefresher::Hop::to
OrderID to
Next order to be processed.
Definition: refresh.h:63
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:88
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:165
LinkRefresher::IN_AUTOREFIT
Currently doing an autorefit loop. Ignore the first autorefit order.
Definition: refresh.h:38
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:261
LinkRefresher::Hop
A hop the refresh algorithm might evaluate.
Definition: refresh.h:61
LinkRefresher::LinkRefresher
LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading)
Constructor for link refreshing algorithm.
Definition: refresh.cpp:71
NUM_CARGO
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
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:87
LinkRefresher::RefreshFlags
RefreshFlags
Various flags about properties of the last examined link that might have an influence on the next one...
Definition: refresh.h:33
LinkRefresher::HandleRefit
bool HandleRefit(CargoID refit_cargo)
Handle refit orders by updating capacities and refit_capacities.
Definition: refresh.cpp:92
LinkRefresher::USE_NEXT
There was a conditional jump. Try to use the given next order when looking for a new one...
Definition: refresh.h:34
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
LinkRefresher::RefitDesc
Simulated cargo type and capacity for prediction of future links.
Definition: refresh.h:44
LinkRefresher::RefitDesc::remaining
uint16 remaining
Capacity remaining from before the previous refit.
Definition: refresh.h:47
LinkRefresher::WAS_REFIT
Consist was refit since the last stop where it could interact with cargo.
Definition: refresh.h:36
LinkRefresher
Utility to refresh links a consist will visit.
Definition: refresh.h:24