Remove costly recalculation of a date format we already have.
[openttd-joker.git] / src / linkgraph / refresh.h
blob9d04f8f605f8766e53f821a6dac416a8177c8138
1 /* $Id: refresh.h 26283 2014-01-28 19:49:43Z fonsinchen $ */
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 */
10 /** @file refresh.h Declaration of link refreshing utility. */
12 #ifndef REFRESH_H
13 #define REFRESH_H
15 #include "../cargo_type.h"
16 #include "../vehicle_base.h"
17 #include "../3rdparty/cpp-btree/btree_set.h"
18 #include <vector>
19 #include <map>
21 /**
22 * Utility to refresh links a consist will visit.
24 class LinkRefresher {
25 public:
26 static void Run(Vehicle *v, bool allow_merge = true, bool is_full_loading = false, uint32 cargo_mask = ~0);
28 protected:
29 /**
30 * Various flags about properties of the last examined link that might have
31 * an influence on the next one.
33 enum RefreshFlags {
34 USE_NEXT, ///< There was a conditional jump. Try to use the given next order when looking for a new one.
35 HAS_CARGO, ///< Consist could leave the last stop where it could interact with cargo carrying cargo (i.e. not an "unload all" + "no loading" order).
36 WAS_REFIT, ///< Consist was refit since the last stop where it could interact with cargo.
37 RESET_REFIT, ///< Consist had a chance to load since the last refit and the refit capacities can be reset.
38 IN_AUTOREFIT, ///< Currently doing an autorefit loop. Ignore the first autorefit order.
41 /**
42 * Simulated cargo type and capacity for prediction of future links.
44 struct RefitDesc {
45 CargoID cargo; ///< Cargo type the vehicle will be carrying.
46 uint16 capacity; ///< Capacity the vehicle will have.
47 uint16 remaining; ///< Capacity remaining from before the previous refit.
48 RefitDesc(CargoID cargo, uint16 capacity, uint16 remaining) :
49 cargo(cargo), capacity(capacity), remaining(remaining) {}
52 /**
53 * A hop the refresh algorithm might evaluate. If the same hop is seen again
54 * the evaluation is stopped. This of course is a fairly simple heuristic.
55 * Sequences of refit orders can produce vehicles with all kinds of
56 * different cargoes and remembering only one can lead to early termination
57 * of the algorithm. However, as the order language is Turing complete, we
58 * are facing the halting problem here. At some point we have to draw the
59 * line.
61 struct Hop {
62 OrderID from; ///< Last order where vehicle could interact with cargo or absolute first order.
63 OrderID to; ///< Next order to be processed.
64 CargoID cargo; ///< Cargo the consist is probably carrying or CT_INVALID if unknown.
66 /**
67 * Default constructor should not be called but has to be visible for
68 * usage in btree::btree_set.
70 Hop() {}
72 /**
73 * Real constructor, only use this one.
74 * @param from First order of the hop.
75 * @param to Second order of the hop.
76 * @param cargo Cargo the consist is probably carrying when passing the hop.
78 Hop(OrderID from, OrderID to, CargoID cargo) : from(from), to(to), cargo(cargo) {}
79 bool operator<(const Hop &other) const;
82 typedef std::vector<RefitDesc> RefitList;
83 typedef btree::btree_set<Hop> HopSet;
85 Vehicle *vehicle; ///< Vehicle for which the links should be refreshed.
86 uint capacities[NUM_CARGO]; ///< Current added capacities per cargo ID in the consist.
87 RefitList refit_capacities; ///< Current state of capacity remaining from previous refits versus overall capacity per vehicle in the consist.
88 HopSet *seen_hops; ///< Hops already seen. If the same hop is seen twice we stop the algorithm. This is shared between all Refreshers of the same run.
89 CargoID cargo; ///< Cargo given in last refit order.
90 bool allow_merge; ///< If the refresher is allowed to merge or extend link graphs.
91 bool is_full_loading; ///< If the vehicle is full loading.
92 uint32 cargo_mask; ///< Bit-mask of cargo IDs to refresh.
94 LinkRefresher(Vehicle *v, HopSet *seen_hops, bool allow_merge, bool is_full_loading, uint32 cargo_mask);
96 bool HandleRefit(CargoID refit_cargo);
97 void ResetRefit();
98 void RefreshStats(const Order *cur, const Order *next);
99 const Order *PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
101 void RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops = 0);
104 #endif /* REFRESH_H */