2007-06-20 19:26:25 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file timetable_cmd.cpp Commands related to time tabling. */
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "variables.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
|
|
|
#include "vehicle_base.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2007-06-20 19:26:25 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2007-06-20 19:26:25 +00:00
|
|
|
|
2007-06-25 20:55:43 +00:00
|
|
|
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey)
|
|
|
|
{
|
|
|
|
Order *order = GetVehicleOrder(v, order_number);
|
|
|
|
|
|
|
|
if (is_journey) {
|
|
|
|
order->travel_time = time;
|
|
|
|
} else {
|
|
|
|
order->wait_time = time;
|
|
|
|
}
|
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
|
2008-08-30 10:26:16 +00:00
|
|
|
if (v->cur_order_index == order_number && v->current_order.Equals(*order)) {
|
|
|
|
if (is_journey) {
|
|
|
|
v->current_order.travel_time = time;
|
|
|
|
} else {
|
|
|
|
v->current_order.wait_time = time;
|
|
|
|
}
|
|
|
|
}
|
2007-10-30 17:57:51 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
|
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
|
|
|
|
2007-06-20 19:26:25 +00:00
|
|
|
/**
|
|
|
|
* Add or remove waiting times from an order.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Various bitstuffed elements
|
|
|
|
* - p1 = (bit 0-15) - Vehicle with the orders to change.
|
|
|
|
* - p1 = (bit 16-23) - Order index to modify.
|
|
|
|
* - p1 = (bit 24) - Whether to change the waiting time or the travelling
|
|
|
|
* time.
|
2008-02-27 21:46:57 +00:00
|
|
|
* - p1 = (bit 25) - Whether p2 contains waiting and travelling time.
|
2007-06-20 19:26:25 +00:00
|
|
|
* @param p2 The amount of time to wait.
|
2008-02-27 21:46:57 +00:00
|
|
|
* - p2 = (bit 0-15) - Waiting or travelling time as specified by p1 bit 24 if p1 bit 25 is not set,
|
|
|
|
* Travelling time if p1 bit 25 is set.
|
|
|
|
* - p2 = (bit 16-31) - Waiting time if p1 bit 25 is set
|
2007-06-20 19:26:25 +00:00
|
|
|
*/
|
|
|
|
CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (!_settings_game.order.timetabling) return CMD_ERROR;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
VehicleID veh = GB(p1, 0, 16);
|
|
|
|
if (!IsValidVehicleID(veh)) return CMD_ERROR;
|
|
|
|
|
|
|
|
Vehicle *v = GetVehicle(veh);
|
|
|
|
if (!CheckOwnership(v->owner)) return CMD_ERROR;
|
|
|
|
|
|
|
|
VehicleOrderID order_number = GB(p1, 16, 8);
|
|
|
|
Order *order = GetVehicleOrder(v, order_number);
|
|
|
|
if (order == NULL) return CMD_ERROR;
|
|
|
|
|
2008-02-27 21:46:57 +00:00
|
|
|
bool packed_time = HasBit(p1, 25);
|
|
|
|
bool is_journey = HasBit(p1, 24) || packed_time;
|
2008-07-20 07:41:43 +00:00
|
|
|
|
2008-10-08 11:29:17 +00:00
|
|
|
int wait_time = order->wait_time;
|
|
|
|
int travel_time = order->travel_time;
|
2008-07-20 07:41:43 +00:00
|
|
|
if (packed_time) {
|
|
|
|
travel_time = GB(p2, 0, 16);
|
|
|
|
wait_time = GB(p2, 16, 16);;
|
|
|
|
} else if (is_journey) {
|
|
|
|
travel_time = GB(p2, 0, 16);
|
2008-07-14 21:01:49 +00:00
|
|
|
} else {
|
2008-07-20 07:41:43 +00:00
|
|
|
wait_time = GB(p2, 0, 16);
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|
|
|
|
|
2008-10-08 11:29:17 +00:00
|
|
|
if (wait_time != order->wait_time) {
|
2008-07-20 07:41:43 +00:00
|
|
|
switch (order->GetType()) {
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_CONDITIONAL:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:29:17 +00:00
|
|
|
if (travel_time != order->travel_time && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
|
2007-06-20 19:26:25 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-10-08 11:29:17 +00:00
|
|
|
if (wait_time != order->wait_time) ChangeTimetable(v, order_number, wait_time, false);
|
|
|
|
if (travel_time != order->travel_time) ChangeTimetable(v, order_number, travel_time, true);
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the lateness counter to make the vehicle on time.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Various bitstuffed elements
|
|
|
|
* - p1 = (bit 0-15) - Vehicle with the orders to change.
|
|
|
|
*/
|
|
|
|
CommandCost CmdSetVehicleOnTime(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (!_settings_game.order.timetabling) return CMD_ERROR;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
VehicleID veh = GB(p1, 0, 16);
|
|
|
|
if (!IsValidVehicleID(veh)) return CMD_ERROR;
|
|
|
|
|
|
|
|
Vehicle *v = GetVehicle(veh);
|
|
|
|
if (!CheckOwnership(v->owner)) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->lateness_counter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2007-06-25 20:55:43 +00:00
|
|
|
/**
|
|
|
|
* Start or stop filling the timetable automatically from the time the vehicle
|
|
|
|
* actually takes to complete it. When starting to autofill the current times
|
|
|
|
* are cleared and the timetable will start again from scratch.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Vehicle index.
|
|
|
|
* @param p2 Set to 1 to enable, 0 to disable.
|
|
|
|
*/
|
|
|
|
CommandCost CmdAutofillTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2008-05-29 15:13:28 +00:00
|
|
|
if (!_settings_game.order.timetabling) return CMD_ERROR;
|
2007-06-25 20:55:43 +00:00
|
|
|
|
|
|
|
VehicleID veh = GB(p1, 0, 16);
|
|
|
|
if (!IsValidVehicleID(veh)) return CMD_ERROR;
|
|
|
|
|
|
|
|
Vehicle *v = GetVehicle(veh);
|
|
|
|
if (!CheckOwnership(v->owner)) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (p2 == 1) {
|
|
|
|
/* Start autofilling the timetable, which clears all the current
|
|
|
|
* timings and clears the "timetable has started" bit. */
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
|
2007-06-25 20:55:43 +00:00
|
|
|
|
|
|
|
for (Order *order = GetVehicleOrder(v, 0); order != NULL; order = order->next) {
|
|
|
|
order->wait_time = 0;
|
|
|
|
order->travel_time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->current_order.wait_time = 0;
|
|
|
|
v->current_order.travel_time = 0;
|
|
|
|
} else {
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
|
2007-10-30 17:57:51 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
|
|
|
|
}
|
|
|
|
|
2007-06-25 20:55:43 +00:00
|
|
|
return CommandCost();
|
|
|
|
}
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
|
|
|
|
{
|
|
|
|
uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time;
|
|
|
|
uint time_taken = v->current_order_time;
|
|
|
|
|
|
|
|
v->current_order_time = 0;
|
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
if (!_settings_game.order.timetabling) return;
|
2007-06-25 20:55:43 +00:00
|
|
|
|
|
|
|
/* Make sure the timetable only starts when the vehicle reaches the first
|
2008-04-18 04:54:09 +00:00
|
|
|
* order, not when travelling from the depot to the first station. */
|
|
|
|
if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
|
|
|
|
SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
|
|
|
|
return;
|
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2008-04-18 04:54:09 +00:00
|
|
|
if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
|
2007-06-25 20:55:43 +00:00
|
|
|
if (timetabled == 0) {
|
|
|
|
/* Round the time taken up to the nearest day, as this will avoid
|
|
|
|
* confusion for people who are timetabling in days, and can be
|
|
|
|
* adjusted later by people who aren't. */
|
|
|
|
time_taken = (((time_taken - 1) / DAY_TICKS) + 1) * DAY_TICKS;
|
|
|
|
|
2008-07-14 21:01:49 +00:00
|
|
|
if (!v->current_order.IsType(OT_CONDITIONAL)) {
|
|
|
|
ChangeTimetable(v, v->cur_order_index, time_taken, travelling);
|
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
return;
|
|
|
|
} else if (v->cur_order_index == 0) {
|
|
|
|
/* Otherwise if we're at the beginning and it already has a value,
|
|
|
|
* assume that autofill is finished and turn it off again. */
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
2008-04-18 04:54:09 +00:00
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2007-06-25 17:17:40 +00:00
|
|
|
/* Vehicles will wait at stations if they arrive early even if they are not
|
|
|
|
* timetabled to wait there, so make sure the lateness counter is updated
|
|
|
|
* when this happens. */
|
2007-06-25 20:55:43 +00:00
|
|
|
if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
v->lateness_counter -= (timetabled - time_taken);
|
|
|
|
|
2008-08-17 19:56:17 +00:00
|
|
|
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
|
2007-10-30 17:57:51 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
|
|
|
|
}
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|