# timetable_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "command_func.h"
    00014 #include "company_func.h"
    00015 #include "date_func.h"
    00016 #include "window_func.h"
    00017 #include "vehicle_base.h"
    00018
    00019 #include "table/strings.h"
    00020
    00021 static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey)
    00022 {
    00023   Order *order = v->GetOrder(order_number);
    00024   int delta;
    00025
    00026   if (is_journey) {
    00027     delta = time - order->travel_time;
    00028     order->travel_time = time;
    00029   } else {
    00030     delta = time - order->wait_time;
    00031     order->wait_time = time;
    00032   }
    00033   v->orders.list->UpdateOrderTimetable(delta);
    00034
    00035   for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
    00036     if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
    00037       if (is_journey) {
    00038         v->current_order.travel_time = time;
    00039       } else {
    00040         v->current_order.wait_time = time;
    00041       }
    00042     }
    00043     SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00044   }
    00045 }
    00046
    00061 CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00062 {
    00063   VehicleID veh = GB(p1, 0, 20);
    00064
    00065   Vehicle *v = Vehicle::GetIfValid(veh);
    00066   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00067
    00068   CommandCost ret = CheckOwnership(v->owner);
    00069   if (ret.Failed()) return ret;
    00070
    00071   VehicleOrderID order_number = GB(p1, 20, 8);
    00072   Order *order = v->GetOrder(order_number);
    00073   if (order == NULL || order->IsType(OT_AUTOMATIC)) return CMD_ERROR;
    00074
    00075   bool is_journey = HasBit(p1, 28);
    00076
    00077   int wait_time   = order->wait_time;
    00078   int travel_time = order->travel_time;
    00079   if (is_journey) {
    00080     travel_time = GB(p2, 0, 16);
    00081   } else {
    00082     wait_time   = GB(p2, 0, 16);
    00083   }
    00084
    00085   if (wait_time != order->wait_time) {
    00086     switch (order->GetType()) {
    00087       case OT_GOTO_STATION:
    00088         if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
    00089         break;
    00090
    00091       case OT_CONDITIONAL:
    00092         break;
    00093
    00094       default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
    00095     }
    00096   }
    00097
    00098   if (travel_time != order->travel_time && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
    00099
    00100   if (flags & DC_EXEC) {
    00101     if (wait_time   != order->wait_time)   ChangeTimetable(v, order_number, wait_time,   false);
    00102     if (travel_time != order->travel_time) ChangeTimetable(v, order_number, travel_time, true);
    00103   }
    00104
    00105   return CommandCost();
    00106 }
    00107
    00118 CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00119 {
    00120   VehicleID veh = GB(p1, 0, 20);
    00121
    00122   Vehicle *v = Vehicle::GetIfValid(veh);
    00123   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00124
    00125   CommandCost ret = CheckOwnership(v->owner);
    00126   if (ret.Failed()) return ret;
    00127
    00128   if (flags & DC_EXEC) {
    00129     v->lateness_counter = 0;
    00130     SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00131   }
    00132
    00133   return CommandCost();
    00134 }
    00135
    00145 CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00146 {
    00147   Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
    00148   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00149
    00150   CommandCost ret = CheckOwnership(v->owner);
    00151   if (ret.Failed()) return ret;
    00152
    00153   /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
    00154   Date start_date = (Date)p2;
    00155   if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
    00156   if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
    00157   if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
    00158
    00159   if (flags & DC_EXEC) {
    00160     v->lateness_counter = 0;
    00161     ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
    00162     v->timetable_start = start_date;
    00163
    00164     SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00165   }
    00166
    00167   return CommandCost();
    00168 }
    00169
    00170
    00184 CommandCost CmdAutofillTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00185 {
    00186   VehicleID veh = GB(p1, 0, 20);
    00187
    00188   Vehicle *v = Vehicle::GetIfValid(veh);
    00189   if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
    00190
    00191   CommandCost ret = CheckOwnership(v->owner);
    00192   if (ret.Failed()) return ret;
    00193
    00194   if (flags & DC_EXEC) {
    00195     if (HasBit(p2, 0)) {
    00196       /* Start autofilling the timetable, which clears the
    00197        * "timetable has started" bit. Times are not cleared anymore, but are
    00198        * overwritten when the order is reached now. */
    00199       SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
    00200       ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
    00201
    00202       /* Overwrite waiting times only if they got longer */
    00203       if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
    00204
    00205       v->timetable_start = 0;
    00206       v->lateness_counter = 0;
    00207     } else {
    00208       ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
    00209       ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
    00210     }
    00211
    00212     for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
    00213       if (v2 != v) {
    00214         /* Stop autofilling; only one vehicle at a time can perform autofill */
    00215         ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
    00216         ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
    00217       }
    00218       SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
    00219     }
    00220   }
    00221
    00222   return CommandCost();
    00223 }
    00224
    00225 void UpdateVehicleTimetable(Vehicle *v, bool travelling)
    00226 {
    00227   uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time;
    00228   uint time_taken = v->current_order_time;
    00229
    00230   v->current_order_time = 0;
    00231
    00232   if (v->current_order.IsType(OT_AUTOMATIC)) return; // no timetabling of auto orders
    00233
    00234   VehicleOrderID first_manual_order = 0;
    00235   for (Order *o = v->GetFirstOrder(); o != NULL && o->IsType(OT_AUTOMATIC); o = o->next) {
    00236     ++first_manual_order;
    00237   }
    00238
    00239   bool just_started = false;
    00240
    00241   /* This vehicle is arriving at the first destination in the timetable. */
    00242   if (v->cur_real_order_index == first_manual_order && travelling) {
    00243     /* If the start date hasn't been set, or it was set automatically when
    00244      * the vehicle last arrived at the first destination, update it to the
    00245      * current time. Otherwise set the late counter appropriately to when
    00246      * the vehicle should have arrived. */
    00247     just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
    00248
    00249     if (v->timetable_start != 0) {
    00250       v->lateness_counter = (_date - v->timetable_start) * DAY_TICKS + _date_fract;
    00251       v->timetable_start = 0;
    00252     }
    00253
    00254     SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
    00255     SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00256   }
    00257
    00258   if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
    00259
    00260   if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
    00261     if (travelling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) {
    00262       /* Need to clear that now as otherwise we are not able to reduce the wait time */
    00263       v->current_order.wait_time = 0;
    00264     }
    00265
    00266     if (just_started) return;
    00267
    00268     /* Modify station waiting time only if our new value is larger (this is
    00269      * always the case when we cleared the timetable). */
    00270     if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.wait_time)) {
    00271       /* Round the time taken up to the nearest day, as this will avoid
    00272        * confusion for people who are timetabling in days, and can be
    00273        * adjusted later by people who aren't.
    00274        * For trains/aircraft multiple movement cycles are done in one
    00275        * tick. This makes it possible to leave the station and process
    00276        * e.g. a depot order in the same tick, causing it to not fill
    00277        * the timetable entry like is done for road vehicles/ships.
    00278        * Thus always make sure at least one tick is used between the
    00279        * processing of different orders when filling the timetable. */
    00280       time_taken = CeilDiv(max(time_taken, 1U), DAY_TICKS) * DAY_TICKS;
    00281
    00282       ChangeTimetable(v, v->cur_real_order_index, time_taken, travelling);
    00283     }
    00284
    00285     if (v->cur_real_order_index == first_manual_order && travelling) {
    00286       /* If we just started we would have returned earlier and have not reached
    00287        * this code. So obviously, we have completed our round: So turn autofill
    00288        * off again. */
    00289       ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
    00290       ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
    00291     }
    00292     return;
    00293   }
    00294
    00295   if (just_started) return;
    00296
    00297   /* Vehicles will wait at stations if they arrive early even if they are not
    00298    * timetabled to wait there, so make sure the lateness counter is updated
    00299    * when this happens. */
    00300   if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
    00301
    00302   v->lateness_counter -= (timetabled - time_taken);
    00303
    00304   /* When we are more late than this timetabled bit takes we (somewhat expensively)
    00305    * check how many ticks the (fully filled) timetable has. If a timetable cycle is
    00306    * shorter than the amount of ticks we are late we reduce the lateness by the
    00307    * length of a full cycle till lateness is less than the length of a timetable
    00308    * cycle. When the timetable isn't fully filled the cycle will be INVALID_TICKS. */
    00309   if (v->lateness_counter > (int)timetabled) {
    00310     Ticks cycle = v->orders.list->GetTimetableTotalDuration();
    00311     if (cycle != INVALID_TICKS && v->lateness_counter > cycle) {
    00312       v->lateness_counter %= cycle;
    00313     }
    00314   }
    00315
    00316   for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
    00317     SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
    00318   }
    00319 }