OpenTTD
refresh.cpp
1 /* $Id: refresh.cpp 26889 2014-09-21 14:22:32Z 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 #include "../stdafx.h"
13 #include "../core/bitmath_func.hpp"
14 #include "../station_func.h"
15 #include "../engine_base.h"
16 #include "../vehicle_func.h"
17 #include "refresh.h"
18 #include "linkgraph.h"
19 
20 #include "../safeguards.h"
21 
28 /* static */ void LinkRefresher::Run(Vehicle *v, bool allow_merge, bool is_full_loading)
29 {
30  /* If there are no orders we can't predict anything.*/
31  if (v->orders.list == NULL) return;
32 
33  /* Make sure the first order is a useful order. */
35  if (first == NULL) return;
36 
37  HopSet seen_hops;
38  LinkRefresher refresher(v, &seen_hops, allow_merge, is_full_loading);
39 
40  refresher.RefreshLinks(first, first, v->last_loading_station != INVALID_STATION ? 1 << HAS_CARGO : 0);
41 }
42 
48 bool LinkRefresher::Hop::operator<(const Hop &other) const
49 {
50  if (this->from < other.from) {
51  return true;
52  } else if (this->from > other.from) {
53  return false;
54  }
55  if (this->to < other.to) {
56  return true;
57  } else if (this->to > other.to) {
58  return false;
59  }
60  return this->cargo < other.cargo;
61 }
62 
72  vehicle(vehicle), seen_hops(seen_hops), cargo(CT_INVALID), allow_merge(allow_merge),
73  is_full_loading(is_full_loading)
74 {
75  /* Assemble list of capacities and set last loading stations to 0. */
76  for (Vehicle *v = this->vehicle; v != NULL; v = v->Next()) {
77  this->refit_capacities.push_back(RefitDesc(v->cargo_type, v->cargo_cap, v->refit_cap));
78  if (v->refit_cap > 0) this->capacities[v->cargo_type] += v->refit_cap;
79  }
80 }
81 
88 {
89  this->cargo = refit_cargo;
90  RefitList::iterator refit_it = this->refit_capacities.begin();
91  bool any_refit = false;
92  for (Vehicle *v = this->vehicle; v != NULL; v = v->Next()) {
93  const Engine *e = Engine::Get(v->engine_type);
94  if (!HasBit(e->info.refit_mask, this->cargo)) {
95  ++refit_it;
96  continue;
97  }
98  any_refit = true;
99 
100  /* Back up the vehicle's cargo type */
101  CargoID temp_cid = v->cargo_type;
102  byte temp_subtype = v->cargo_subtype;
103  v->cargo_type = this->cargo;
104  v->cargo_subtype = GetBestFittingSubType(v, v, this->cargo);
105 
106  uint16 mail_capacity = 0;
107  uint amount = e->DetermineCapacity(v, &mail_capacity);
108 
109  /* Restore the original cargo type */
110  v->cargo_type = temp_cid;
111  v->cargo_subtype = temp_subtype;
112 
113  /* Skip on next refit. */
114  if (this->cargo != refit_it->cargo && refit_it->remaining > 0) {
115  this->capacities[refit_it->cargo] -= refit_it->remaining;
116  refit_it->remaining = 0;
117  } else if (amount < refit_it->remaining) {
118  this->capacities[refit_it->cargo] -= refit_it->remaining - amount;
119  refit_it->remaining = amount;
120  }
121  refit_it->capacity = amount;
122  refit_it->cargo = this->cargo;
123 
124  ++refit_it;
125 
126  /* Special case for aircraft with mail. */
127  if (v->type == VEH_AIRCRAFT) {
128  if (mail_capacity < refit_it->remaining) {
129  this->capacities[refit_it->cargo] -= refit_it->remaining - mail_capacity;
130  refit_it->remaining = mail_capacity;
131  }
132  refit_it->capacity = mail_capacity;
133  break; // aircraft have only one vehicle
134  }
135  }
136  return any_refit;
137 }
138 
143 {
144  for (RefitList::iterator it(this->refit_capacities.begin()); it != this->refit_capacities.end(); ++it) {
145  if (it->remaining == it->capacity) continue;
146  this->capacities[it->cargo] += it->capacity - it->remaining;
147  it->remaining = it->capacity;
148  }
149 }
150 
160 const Order *LinkRefresher::PredictNextOrder(const Order *cur, const Order *next, uint8 flags, uint num_hops)
161 {
162  /* next is good if it's either NULL (then the caller will stop the
163  * evaluation) or if it's not conditional and the caller allows it to be
164  * chosen (by setting USE_NEXT). */
165  while (next != NULL && (!HasBit(flags, USE_NEXT) || next->IsType(OT_CONDITIONAL))) {
166 
167  /* After the first step any further non-conditional order is good,
168  * regardless of previous USE_NEXT settings. The case of cur and next or
169  * their respective stations being equal is handled elsewhere. */
170  SetBit(flags, USE_NEXT);
171 
172  if (next->IsType(OT_CONDITIONAL)) {
173  const Order *skip_to = this->vehicle->orders.list->GetNextDecisionNode(
174  this->vehicle->orders.list->GetOrderAt(next->GetConditionSkipToOrder()), num_hops);
175  if (skip_to != NULL && num_hops < this->vehicle->orders.list->GetNumOrders()) {
176  /* Make copies of capacity tracking lists. There is potential
177  * for optimization here: If the vehicle never refits we don't
178  * need to copy anything. Also, if we've seen the branched link
179  * before we don't need to branch at all. */
180  LinkRefresher branch(*this);
181  branch.RefreshLinks(cur, skip_to, flags, num_hops + 1);
182  }
183  }
184 
185  /* Reassign next with the following stop. This can be a station or a
186  * depot.*/
187  next = this->vehicle->orders.list->GetNextDecisionNode(
188  this->vehicle->orders.list->GetNext(next), num_hops++);
189  }
190  return next;
191 }
192 
198 void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
199 {
200  StationID next_station = next->GetDestination();
202  if (st != NULL && next_station != INVALID_STATION && next_station != st->index) {
203  for (CapacitiesMap::const_iterator i = this->capacities.begin(); i != this->capacities.end(); ++i) {
204  /* Refresh the link and give it a minimum capacity. */
205 
206  if (i->second == 0) continue;
207  CargoID c = i->first;
208 
209  /* If not allowed to merge link graphs, make sure the stations are
210  * already in the same link graph. */
211  if (!this->allow_merge && st->goods[c].link_graph != Station::Get(next_station)->goods[c].link_graph) {
212  continue;
213  }
214 
215  /* A link is at least partly restricted if a vehicle can't load at its source. */
216  EdgeUpdateMode restricted_mode = (cur->GetLoadType() & OLFB_NO_LOAD) == 0 ?
218 
219  /* If the vehicle is currently full loading, increase the capacities at the station
220  * where it is loading by an estimate of what it would have transported if it wasn't
221  * loading. Don't do that if the vehicle has been waiting for longer than the entire
222  * order list is supposed to take, though. If that is the case the total duration is
223  * probably far off and we'd greatly overestimate the capacity by increasing.*/
224  if (this->is_full_loading && this->vehicle->orders.list != NULL &&
226  this->vehicle->orders.list->GetTotalDuration() >
227  (Ticks)this->vehicle->current_order_time) {
228  uint effective_capacity = i->second * this->vehicle->load_unload_ticks;
229  if (effective_capacity > (uint)this->vehicle->orders.list->GetTotalDuration()) {
230  IncreaseStats(st, c, next_station, effective_capacity /
231  this->vehicle->orders.list->GetTotalDuration(), 0,
232  EUM_INCREASE | restricted_mode);
233  } else if (RandomRange(this->vehicle->orders.list->GetTotalDuration()) < effective_capacity) {
234  IncreaseStats(st, c, next_station, 1, 0, EUM_INCREASE | restricted_mode);
235  } else {
236  IncreaseStats(st, c, next_station, i->second, 0, EUM_REFRESH | restricted_mode);
237  }
238  } else {
239  IncreaseStats(st, c, next_station, i->second, 0, EUM_REFRESH | restricted_mode);
240  }
241  }
242  }
243 }
244 
256 void LinkRefresher::RefreshLinks(const Order *cur, const Order *next, uint8 flags, uint num_hops)
257 {
258  while (next != NULL) {
259 
260  if ((next->IsType(OT_GOTO_DEPOT) || next->IsType(OT_GOTO_STATION)) && next->IsRefit()) {
261  SetBit(flags, WAS_REFIT);
262  if (!next->IsAutoRefit()) {
263  this->HandleRefit(next->GetRefitCargo());
264  } else if (!HasBit(flags, IN_AUTOREFIT)) {
265  SetBit(flags, IN_AUTOREFIT);
266  LinkRefresher backup(*this);
267  for (CargoID c = 0; c != NUM_CARGO; ++c) {
268  if (CargoSpec::Get(c)->IsValid() && this->HandleRefit(c)) {
269  this->RefreshLinks(cur, next, flags, num_hops);
270  *this = backup;
271  }
272  }
273  }
274  }
275 
276  /* Only reset the refit capacities if the "previous" next is a station,
277  * meaning that either the vehicle was refit at the previous station or
278  * it wasn't at all refit during the current hop. */
279  if (HasBit(flags, WAS_REFIT) && (next->IsType(OT_GOTO_STATION) || next->IsType(OT_IMPLICIT))) {
280  SetBit(flags, RESET_REFIT);
281  } else {
282  ClrBit(flags, RESET_REFIT);
283  }
284 
285  next = this->PredictNextOrder(cur, next, flags, num_hops);
286  if (next == NULL) break;
287  Hop hop(cur->index, next->index, this->cargo);
288  if (this->seen_hops->find(hop) != this->seen_hops->end()) {
289  break;
290  } else {
291  this->seen_hops->insert(hop);
292  }
293 
294  /* Don't use the same order again, but choose a new one in the next round. */
295  ClrBit(flags, USE_NEXT);
296 
297  /* Skip resetting and link refreshing if next order won't do anything with cargo. */
298  if (!next->IsType(OT_GOTO_STATION) && !next->IsType(OT_IMPLICIT)) continue;
299 
300  if (HasBit(flags, RESET_REFIT)) {
301  this->ResetRefit();
302  ClrBit(flags, RESET_REFIT);
303  ClrBit(flags, WAS_REFIT);
304  }
305 
306  if (cur->IsType(OT_GOTO_STATION) || cur->IsType(OT_IMPLICIT)) {
307  if (cur->CanLeaveWithCargo(HasBit(flags, HAS_CARGO))) {
308  SetBit(flags, HAS_CARGO);
309  this->RefreshStats(cur, next);
310  } else {
311  ClrBit(flags, HAS_CARGO);
312  }
313  }
314 
315  /* "cur" is only assigned here if the stop is a station so that
316  * whenever stats are to be increased two stations can be found. */
317  cur = next;
318  }
319 }