OpenTTD
timetable_cmd.cpp
Go to the documentation of this file.
1 /* $Id: timetable_cmd.cpp 26574 2014-05-11 09:59:30Z 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 "command_func.h"
14 #include "company_func.h"
15 #include "date_func.h"
16 #include "window_func.h"
17 #include "vehicle_base.h"
18 #include "cmd_helper.h"
19 #include "core/sort_func.hpp"
20 
21 #include "table/strings.h"
22 
23 #include "safeguards.h"
24 
33 static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled)
34 {
35  Order *order = v->GetOrder(order_number);
36  int total_delta = 0;
37  int timetable_delta = 0;
38 
39  switch (mtf) {
40  case MTF_WAIT_TIME:
41  total_delta = val - order->GetWaitTime();
42  timetable_delta = (timetabled ? val : 0) - order->GetTimetabledWait();
43  order->SetWaitTime(val);
44  order->SetWaitTimetabled(timetabled);
45  break;
46 
47  case MTF_TRAVEL_TIME:
48  total_delta = val - order->GetTravelTime();
49  timetable_delta = (timetabled ? val : 0) - order->GetTimetabledTravel();
50  order->SetTravelTime(val);
51  order->SetTravelTimetabled(timetabled);
52  break;
53 
54  case MTF_TRAVEL_SPEED:
55  order->SetMaxSpeed(val);
56  break;
57 
58  default:
59  NOT_REACHED();
60  }
61  v->orders.list->UpdateTotalDuration(total_delta);
62  v->orders.list->UpdateTimetableDuration(timetable_delta);
63 
64  for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
65  if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
66  switch (mtf) {
67  case MTF_WAIT_TIME:
68  v->current_order.SetWaitTime(val);
69  v->current_order.SetWaitTimetabled(timetabled);
70  break;
71 
72  case MTF_TRAVEL_TIME:
74  v->current_order.SetTravelTimetabled(timetabled);
75  break;
76 
77  case MTF_TRAVEL_SPEED:
78  v->current_order.SetMaxSpeed(val);
79  break;
80 
81  default:
82  NOT_REACHED();
83  }
84  }
86  }
87 }
88 
102 CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
103 {
104  VehicleID veh = GB(p1, 0, 20);
105 
106  Vehicle *v = Vehicle::GetIfValid(veh);
107  if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
108 
109  CommandCost ret = CheckOwnership(v->owner);
110  if (ret.Failed()) return ret;
111 
112  VehicleOrderID order_number = GB(p1, 20, 8);
113  Order *order = v->GetOrder(order_number);
114  if (order == NULL || order->IsType(OT_IMPLICIT)) return CMD_ERROR;
115 
116  ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
117  if (mtf >= MTF_END) return CMD_ERROR;
118 
119  int wait_time = order->GetWaitTime();
120  int travel_time = order->GetTravelTime();
121  int max_speed = order->GetMaxSpeed();
122  switch (mtf) {
123  case MTF_WAIT_TIME:
124  wait_time = GB(p2, 0, 16);
125  break;
126 
127  case MTF_TRAVEL_TIME:
128  travel_time = GB(p2, 0, 16);
129  break;
130 
131  case MTF_TRAVEL_SPEED:
132  max_speed = GB(p2, 0, 16);
133  if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
134  break;
135 
136  default:
137  NOT_REACHED();
138  }
139 
140  if (wait_time != order->GetWaitTime()) {
141  switch (order->GetType()) {
142  case OT_GOTO_STATION:
143  if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
144  break;
145 
146  case OT_CONDITIONAL:
147  break;
148 
149  default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
150  }
151  }
152 
153  if (travel_time != order->GetTravelTime() && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
154  if (max_speed != order->GetMaxSpeed() && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
155 
156  if (flags & DC_EXEC) {
157  if (wait_time != order->GetWaitTime() || (wait_time > 0 && !order->IsWaitTimetabled())) {
158  ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, wait_time > 0);
159  }
160  if (travel_time != order->GetTravelTime() || (travel_time > 0 && !order->IsTravelTimetabled())) {
161  ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME, travel_time > 0);
162  }
163  if (max_speed != order->GetMaxSpeed()) {
164  ChangeTimetable(v, order_number, max_speed, MTF_TRAVEL_SPEED, max_speed != UINT16_MAX);
165  }
166  }
167 
168  return CommandCost();
169 }
170 
181 CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
182 {
183  VehicleID veh = GB(p1, 0, 20);
184 
185  Vehicle *v = Vehicle::GetIfValid(veh);
186  if (v == NULL || !v->IsPrimaryVehicle() || v->orders.list == NULL) return CMD_ERROR;
187 
188  CommandCost ret = CheckOwnership(v->owner);
189  if (ret.Failed()) return ret;
190 
191  if (flags & DC_EXEC) {
192  v->lateness_counter = 0;
194  }
195 
196  return CommandCost();
197 }
198 
207 static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp)
208 {
209  const Vehicle *a = *ap;
210  const Vehicle *b = *bp;
211 
214  int j = (int)b_order - (int)a_order;
215 
216  /* Are we currently at an ordered station (un)loading? */
217  bool a_load = a->current_order.IsType(OT_LOADING) && a->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
218  bool b_load = b->current_order.IsType(OT_LOADING) && b->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
219 
220  /* If the current order is not loading at the ordered station, decrease the order index by one since we have
221  * not yet arrived at the station (and thus the timetable entry; still in the travelling of the previous one).
222  * Since the ?_order variables are unsigned the -1 will flow under and place the vehicles going to order #0 at
223  * the begin of the list with vehicles arriving at #0. */
224  if (!a_load) a_order--;
225  if (!b_load) b_order--;
226 
227  /* First check the order index that accounted for loading, then just the raw one. */
228  int i = (int)b_order - (int)a_order;
229  if (i != 0) return i;
230  if (j != 0) return j;
231 
232  /* Look at the time we spent in this order; the higher, the closer to its destination. */
234  if (i != 0) return i;
235 
236  /* If all else is equal, use some unique index to sort it the same way. */
237  return b->unitnumber - a->unitnumber;
238 }
239 
251 CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
252 {
253  bool timetable_all = HasBit(p1, 20);
254  Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
255  if (v == NULL || !v->IsPrimaryVehicle() || v->orders.list == NULL) return CMD_ERROR;
256 
257  CommandCost ret = CheckOwnership(v->owner);
258  if (ret.Failed()) return ret;
259 
260  /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
261  Date start_date = (Date)p2;
262  if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
263  if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
264  if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
265  if (timetable_all && !v->orders.list->IsCompleteTimetable()) return CMD_ERROR;
266 
267  if (flags & DC_EXEC) {
269 
270  if (timetable_all) {
271  for (Vehicle *w = v->orders.list->GetFirstSharedVehicle(); w != NULL; w = w->NextShared()) {
272  *vehs.Append() = w;
273  }
274  } else {
275  *vehs.Append() = v;
276  }
277 
278  int total_duration = v->orders.list->GetTimetableTotalDuration();
279  int num_vehs = vehs.Length();
280 
281  if (num_vehs >= 2) {
282  QSortT(vehs.Begin(), vehs.Length(), &VehicleTimetableSorter);
283  }
284 
285  int base = vehs.FindIndex(v);
286 
287  for (Vehicle **viter = vehs.Begin(); viter != vehs.End(); viter++) {
288  int idx = (viter - vehs.Begin()) - base;
289  Vehicle *w = *viter;
290 
291  w->lateness_counter = 0;
293  /* Do multiplication, then division to reduce rounding errors. */
294  w->timetable_start = start_date + idx * total_duration / num_vehs / DAY_TICKS;
296  }
297 
298  }
299 
300  return CommandCost();
301 }
302 
303 
317 CommandCost CmdAutofillTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
318 {
319  VehicleID veh = GB(p1, 0, 20);
320 
321  Vehicle *v = Vehicle::GetIfValid(veh);
322  if (v == NULL || !v->IsPrimaryVehicle() || v->orders.list == NULL) return CMD_ERROR;
323 
324  CommandCost ret = CheckOwnership(v->owner);
325  if (ret.Failed()) return ret;
326 
327  if (flags & DC_EXEC) {
328  if (HasBit(p2, 0)) {
329  /* Start autofilling the timetable, which clears the
330  * "timetable has started" bit. Times are not cleared anymore, but are
331  * overwritten when the order is reached now. */
334 
335  /* Overwrite waiting times only if they got longer */
337 
338  v->timetable_start = 0;
339  v->lateness_counter = 0;
340  } else {
343  }
344 
345  for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
346  if (v2 != v) {
347  /* Stop autofilling; only one vehicle at a time can perform autofill */
348  ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
349  ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
350  }
352  }
353  }
354 
355  return CommandCost();
356 }
357 
363 void UpdateVehicleTimetable(Vehicle *v, bool travelling)
364 {
365  uint time_taken = v->current_order_time;
366 
367  v->current_order_time = 0;
368 
369  if (v->current_order.IsType(OT_IMPLICIT)) return; // no timetabling of auto orders
370 
371  VehicleOrderID first_manual_order = 0;
372  for (Order *o = v->GetFirstOrder(); o != NULL && o->IsType(OT_IMPLICIT); o = o->next) {
373  ++first_manual_order;
374  }
375 
376  bool just_started = false;
377 
378  /* This vehicle is arriving at the first destination in the timetable. */
379  if (v->cur_real_order_index == first_manual_order && travelling) {
380  /* If the start date hasn't been set, or it was set automatically when
381  * the vehicle last arrived at the first destination, update it to the
382  * current time. Otherwise set the late counter appropriately to when
383  * the vehicle should have arrived. */
384  just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
385 
386  if (v->timetable_start != 0) {
388  v->timetable_start = 0;
389  }
390 
393  }
394 
395  if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
396 
397  bool autofilling = HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
398  if (travelling && (!v->current_order.IsWaitTimetabled() ||
399  (autofilling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)))) {
400  /* Need to clear that now as otherwise we are not able to reduce the wait time */
402  }
403 
404  if (just_started) return;
405 
406  /* Modify station waiting time only if our new value is larger (this is
407  * always the case when we cleared the timetable). */
408  if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.GetWaitTime())) {
409  /* Round the time taken up to the nearest day, as this will avoid
410  * confusion for people who are timetabling in days, and can be
411  * adjusted later by people who aren't.
412  * For trains/aircraft multiple movement cycles are done in one
413  * tick. This makes it possible to leave the station and process
414  * e.g. a depot order in the same tick, causing it to not fill
415  * the timetable entry like is done for road vehicles/ships.
416  * Thus always make sure at least one tick is used between the
417  * processing of different orders when filling the timetable. */
418  uint time_to_set = CeilDiv(max(time_taken, 1U), DAY_TICKS) * DAY_TICKS;
419 
420  if (travelling && (autofilling || !v->current_order.IsTravelTimetabled())) {
421  ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_TRAVEL_TIME, autofilling);
422  } else if (!travelling && (autofilling || !v->current_order.IsWaitTimetabled())) {
423  ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_WAIT_TIME, autofilling);
424  }
425  }
426 
427  if (v->cur_real_order_index == first_manual_order && travelling) {
428  /* If we just started we would have returned earlier and have not reached
429  * this code. So obviously, we have completed our round: So turn autofill
430  * off again. */
433  }
434 
435  if (autofilling) return;
436 
437  uint timetabled = travelling ? v->current_order.GetTimetabledTravel() :
439 
440  /* Vehicles will wait at stations if they arrive early even if they are not
441  * timetabled to wait there, so make sure the lateness counter is updated
442  * when this happens. */
443  if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
444 
445  v->lateness_counter -= (timetabled - time_taken);
446 
447  /* When we are more late than this timetabled bit takes we (somewhat expensively)
448  * check how many ticks the (fully filled) timetable has. If a timetable cycle is
449  * shorter than the amount of ticks we are late we reduce the lateness by the
450  * length of a full cycle till lateness is less than the length of a timetable
451  * cycle. When the timetable isn't fully filled the cycle will be INVALID_TICKS. */
452  if (v->lateness_counter > (int)timetabled) {
454  if (cycle != INVALID_TICKS && v->lateness_counter > cycle) {
455  v->lateness_counter %= cycle;
456  }
457  }
458 
459  for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
461  }
462 }