mirror of https://github.com/OpenTTD/OpenTTD
(svn r18292) -Codechange: add a command to set the start date of a timetable. Based on work by PhilSophus.
parent
1a8c203d6e
commit
a951c505bf
|
@ -198,6 +198,7 @@ DEF_COMMAND(CmdMoveOrder);
|
||||||
DEF_COMMAND(CmdChangeTimetable);
|
DEF_COMMAND(CmdChangeTimetable);
|
||||||
DEF_COMMAND(CmdSetVehicleOnTime);
|
DEF_COMMAND(CmdSetVehicleOnTime);
|
||||||
DEF_COMMAND(CmdAutofillTimetable);
|
DEF_COMMAND(CmdAutofillTimetable);
|
||||||
|
DEF_COMMAND(CmdSetTimetableStart);
|
||||||
#undef DEF_COMMAND
|
#undef DEF_COMMAND
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -343,6 +344,7 @@ static const Command _command_proc_table[] = {
|
||||||
{CmdChangeTimetable, 0}, // CMD_CHANGE_TIMETABLE
|
{CmdChangeTimetable, 0}, // CMD_CHANGE_TIMETABLE
|
||||||
{CmdSetVehicleOnTime, 0}, // CMD_SET_VEHICLE_ON_TIME
|
{CmdSetVehicleOnTime, 0}, // CMD_SET_VEHICLE_ON_TIME
|
||||||
{CmdAutofillTimetable, 0}, // CMD_AUTOFILL_TIMETABLE
|
{CmdAutofillTimetable, 0}, // CMD_AUTOFILL_TIMETABLE
|
||||||
|
{CmdSetTimetableStart, 0}, // CMD_SET_TIMETABLE_START
|
||||||
};
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
|
|
@ -293,6 +293,7 @@ enum {
|
||||||
CMD_CHANGE_TIMETABLE, ///< change the timetable for a vehicle
|
CMD_CHANGE_TIMETABLE, ///< change the timetable for a vehicle
|
||||||
CMD_SET_VEHICLE_ON_TIME, ///< set the vehicle on time feature (timetable)
|
CMD_SET_VEHICLE_ON_TIME, ///< set the vehicle on time feature (timetable)
|
||||||
CMD_AUTOFILL_TIMETABLE, ///< autofill the timetable
|
CMD_AUTOFILL_TIMETABLE, ///< autofill the timetable
|
||||||
|
CMD_SET_TIMETABLE_START, ///< set the date that a timetable should start
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -75,7 +75,7 @@ enum {
|
||||||
#define MAX_YEAR 5000000
|
#define MAX_YEAR 5000000
|
||||||
|
|
||||||
/** The number of days till the last day */
|
/** 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 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
|
typedef uint16 DateFract; ///< The fraction of a date we're in, i.e. the number of ticks since the last date changeover
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
|
|
||||||
#include "saveload_internal.h"
|
#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
|
SavegameType _savegame_type; ///< type of savegame we are loading
|
||||||
|
|
||||||
|
|
|
@ -473,6 +473,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
|
||||||
/* Timetable in current order */
|
/* Timetable in current order */
|
||||||
SLE_CONDVAR(Vehicle, current_order.wait_time, SLE_UINT16, 67, SL_MAX_VERSION),
|
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, 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_ORDER, 0, 104),
|
||||||
SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, 105, SL_MAX_VERSION),
|
SLE_CONDREF(Vehicle, orders, REF_ORDERLIST, 105, SL_MAX_VERSION),
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "command_func.h"
|
#include "command_func.h"
|
||||||
#include "functions.h"
|
#include "functions.h"
|
||||||
|
#include "date_func.h"
|
||||||
#include "window_func.h"
|
#include "window_func.h"
|
||||||
#include "vehicle_base.h"
|
#include "vehicle_base.h"
|
||||||
|
|
||||||
|
@ -137,6 +138,38 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1,
|
||||||
return CommandCost();
|
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
|
* Start or stop filling the timetable automatically from the time the vehicle
|
||||||
* actually takes to complete it. When starting to autofill the current times
|
* 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 */
|
/* Overwrite waiting times only if they got longer */
|
||||||
if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
||||||
|
|
||||||
|
v->timetable_start = 0;
|
||||||
v->lateness_counter = 0;
|
v->lateness_counter = 0;
|
||||||
} else {
|
} else {
|
||||||
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
||||||
|
@ -200,11 +234,21 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
|
||||||
|
|
||||||
bool just_started = false;
|
bool just_started = false;
|
||||||
|
|
||||||
/* Make sure the timetable only starts when the vehicle reaches the first
|
/* This vehicle is arriving at the first destination in the timetable. */
|
||||||
* order, not when travelling from the depot to the first station. */
|
if (v->cur_order_index == 0 && travelling) {
|
||||||
if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
|
/* 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);
|
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;
|
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
|
||||||
|
|
|
@ -105,6 +105,7 @@ public:
|
||||||
/* Used for timetabling. */
|
/* Used for timetabling. */
|
||||||
uint32 current_order_time; ///< How many ticks have passed since this order started.
|
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.
|
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.
|
/* Boundaries for the current position in the world and a next hash link.
|
||||||
* NOSAVE: All of those can be updated with VehiclePositionChanged() */
|
* NOSAVE: All of those can be updated with VehiclePositionChanged() */
|
||||||
|
|
Loading…
Reference in New Issue