From a951c505bff54ea4365895126e72a89a09f9fe2d Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 25 Nov 2009 23:37:15 +0000 Subject: (svn r18292) -Codechange: add a command to set the start date of a timetable. Based on work by PhilSophus. --- src/command.cpp | 2 ++ src/command_type.h | 1 + src/date_type.h | 2 +- src/saveload/saveload.cpp | 2 +- src/saveload/vehicle_sl.cpp | 1 + src/timetable_cmd.cpp | 52 +++++++++++++++++++++++++++++++++++++++++---- src/vehicle_base.h | 1 + 7 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/command.cpp b/src/command.cpp index cb97886d2..35e27577b 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -198,6 +198,7 @@ DEF_COMMAND(CmdMoveOrder); DEF_COMMAND(CmdChangeTimetable); DEF_COMMAND(CmdSetVehicleOnTime); DEF_COMMAND(CmdAutofillTimetable); +DEF_COMMAND(CmdSetTimetableStart); #undef DEF_COMMAND /** @@ -343,6 +344,7 @@ static const Command _command_proc_table[] = { {CmdChangeTimetable, 0}, // CMD_CHANGE_TIMETABLE {CmdSetVehicleOnTime, 0}, // CMD_SET_VEHICLE_ON_TIME {CmdAutofillTimetable, 0}, // CMD_AUTOFILL_TIMETABLE + {CmdSetTimetableStart, 0}, // CMD_SET_TIMETABLE_START }; /*! diff --git a/src/command_type.h b/src/command_type.h index a5a7bf1a6..1a48f287a 100644 --- a/src/command_type.h +++ b/src/command_type.h @@ -293,6 +293,7 @@ enum { CMD_CHANGE_TIMETABLE, ///< change the timetable for a vehicle CMD_SET_VEHICLE_ON_TIME, ///< set the vehicle on time feature (timetable) CMD_AUTOFILL_TIMETABLE, ///< autofill the timetable + CMD_SET_TIMETABLE_START, ///< set the date that a timetable should start }; /** diff --git a/src/date_type.h b/src/date_type.h index ae92b1d27..5afc925e4 100644 --- a/src/date_type.h +++ b/src/date_type.h @@ -75,7 +75,7 @@ enum { #define MAX_YEAR 5000000 /** The number of days till the last day */ -#define MAX_DAY DAYS_TILL(MAX_YEAR + 1) - 1 +#define MAX_DAY (DAYS_TILL(MAX_YEAR + 1) - 1) typedef int32 Date; ///< The type to store our dates in typedef uint16 DateFract; ///< The fraction of a date we're in, i.e. the number of ticks since the last date changeover diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index cbf24248b..90be4172e 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -47,7 +47,7 @@ #include "saveload_internal.h" -extern const uint16 SAVEGAME_VERSION = 128; +extern const uint16 SAVEGAME_VERSION = 129; SavegameType _savegame_type; ///< type of savegame we are loading diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 0e1023909..e08e533fc 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -473,6 +473,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt) /* Timetable in current order */ SLE_CONDVAR(Vehicle, current_order.wait_time, SLE_UINT16, 67, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, current_order.travel_time, SLE_UINT16, 67, SL_MAX_VERSION), + SLE_CONDVAR(Vehicle, timetable_start, SLE_INT32, 129, SL_MAX_VERSION), SLE_CONDREF(Vehicle, orders, REF_ORDER, 0, 104), SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, 105, SL_MAX_VERSION), diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index e8018a0ca..4cde4b06f 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -12,6 +12,7 @@ #include "stdafx.h" #include "command_func.h" #include "functions.h" +#include "date_func.h" #include "window_func.h" #include "vehicle_base.h" @@ -137,6 +138,38 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, return CommandCost(); } +/** + * Set the start date of the timetable. + * @param tile Not used. + * @param flags Operation to perform. + * @param p1 Vehicle id. + * @param p2 The timetable start date in ticks. + */ +CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) +{ + if (!_settings_game.order.timetabling) return CMD_ERROR; + + Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 16)); + if (v == NULL || !CheckOwnership(v->owner) || !v->IsPrimaryVehicle()) return CMD_ERROR; + + /* Don't let a timetable start more than 15 years into the future or 1 year in the past. */ + Date start_date = (Date)p2; + if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR; + if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR; + if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR; + + if (flags & DC_EXEC) { + v->lateness_counter = 0; + ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED); + v->timetable_start = start_date; + + SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index); + } + + return CommandCost(); +} + + /** * Start or stop filling the timetable automatically from the time the vehicle * actually takes to complete it. When starting to autofill the current times @@ -170,6 +203,7 @@ CommandCost CmdAutofillTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, /* Overwrite waiting times only if they got longer */ if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME); + v->timetable_start = 0; v->lateness_counter = 0; } else { ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); @@ -200,11 +234,21 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling) bool just_started = false; - /* Make sure the timetable only starts when the vehicle reaches the first - * order, not when travelling from the depot to the first station. */ - if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) { + /* This vehicle is arriving at the first destination in the timetable. */ + if (v->cur_order_index == 0 && travelling) { + /* If the start date hasn't been set, or it was set automatically when + * the vehicle last arrived at the first destination, update it to the + * current time. Otherwise set the late counter appropriately to when + * the vehicle should have arrived. */ + just_started = !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED); + + if (v->timetable_start != 0) { + v->lateness_counter = (_date - v->timetable_start) * DAY_TICKS + _date_fract; + v->timetable_start = 0; + } + SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED); - just_started = true; + SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index); } if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return; diff --git a/src/vehicle_base.h b/src/vehicle_base.h index 10ab41047..2672a656a 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -105,6 +105,7 @@ public: /* Used for timetabling. */ uint32 current_order_time; ///< How many ticks have passed since this order started. int32 lateness_counter; ///< How many ticks late (or early if negative) this vehicle is. + Date timetable_start; ///< When the vehicle is supposed to start the timetable. /* Boundaries for the current position in the world and a next hash link. * NOSAVE: All of those can be updated with VehiclePositionChanged() */ -- cgit v1.2.3-54-g00ecf