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

Generated on Sun Jan 23 01:49:11 2011 for OpenTTD by  doxygen 1.6.1