2007-06-20 19:26:25 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
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"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2009-11-25 23:37:15 +00:00
|
|
|
#include "date_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_base.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)
|
|
|
|
{
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = v->GetOrder(order_number);
|
2009-01-03 13:52:06 +00:00
|
|
|
int delta;
|
2007-06-25 20:55:43 +00:00
|
|
|
|
|
|
|
if (is_journey) {
|
2009-01-03 13:52:06 +00:00
|
|
|
delta = time - order->travel_time;
|
2007-06-25 20:55:43 +00:00
|
|
|
order->travel_time = time;
|
|
|
|
} else {
|
2009-01-03 13:52:06 +00:00
|
|
|
delta = time - order->wait_time;
|
2007-06-25 20:55:43 +00:00
|
|
|
order->wait_time = time;
|
|
|
|
}
|
2009-01-03 13:52:06 +00:00
|
|
|
v->orders.list->UpdateOrderTimetable(delta);
|
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()) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
|
2007-10-30 17:57:51 +00:00
|
|
|
}
|
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
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-06-20 19:26:25 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-06-20 19:26:25 +00:00
|
|
|
{
|
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);
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
VehicleOrderID order_number = GB(p1, 16, 8);
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = v->GetOrder(order_number);
|
2007-06-20 19:26:25 +00:00
|
|
|
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);
|
2010-02-14 16:31:35 +00:00
|
|
|
wait_time = GB(p2, 16, 16);
|
2008-07-20 07:41:43 +00:00
|
|
|
} 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:
|
2009-08-05 17:59:21 +00:00
|
|
|
if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
|
2008-07-20 07:41:43 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_CONDITIONAL:
|
|
|
|
break;
|
|
|
|
|
2009-08-05 17:59:21 +00:00
|
|
|
default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
|
2008-07-20 07:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param p2 unused
|
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-06-20 19:26:25 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSetVehicleOnTime(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-06-20 19:26:25 +00:00
|
|
|
{
|
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);
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->lateness_counter = 0;
|
2009-11-25 22:58:28 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2009-11-25 23:37:15 +00:00
|
|
|
/**
|
|
|
|
* Set the start date of the timetable.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Vehicle id.
|
2010-08-18 22:52:02 +00:00
|
|
|
* @param p2 The timetable start date.
|
|
|
|
* @param text Not used.
|
|
|
|
* @return The error or cost of the operation.
|
2009-11-25 23:37:15 +00:00
|
|
|
*/
|
|
|
|
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));
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-11-25 23:37:15 +00:00
|
|
|
|
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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.
|
2008-11-18 23:53:37 +00:00
|
|
|
* @param p2 Various bitstuffed elements
|
|
|
|
* - p2 = (bit 0) - Set to 1 to enable, 0 to disable autofill.
|
|
|
|
* - p2 = (bit 1) - Set to 1 to preserve waiting times in non-destructive mode
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2007-06-25 20:55:43 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdAutofillTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2007-06-25 20:55:43 +00:00
|
|
|
{
|
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);
|
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2007-06-25 20:55:43 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-11-18 23:53:37 +00:00
|
|
|
if (HasBit(p2, 0)) {
|
2009-01-10 23:21:46 +00:00
|
|
|
/* Start autofilling the timetable, which clears the
|
|
|
|
* "timetable has started" bit. Times are not cleared anymore, but are
|
|
|
|
* overwritten when the order is reached now. */
|
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
|
|
|
|
2009-01-10 23:21:46 +00:00
|
|
|
/* Overwrite waiting times only if they got longer */
|
2008-11-18 23:53:37 +00:00
|
|
|
if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2009-11-25 23:37:15 +00:00
|
|
|
v->timetable_start = 0;
|
2008-11-18 23:53:37 +00:00
|
|
|
v->lateness_counter = 0;
|
2007-06-25 20:55:43 +00:00
|
|
|
} else {
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
2008-11-18 23:53:37 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-06 21:30:18 +00:00
|
|
|
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
|
|
|
|
if (v2 != v) {
|
|
|
|
/* Stop autofilling; only one vehicle at a time can perform autofill */
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
|
|
|
}
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
|
2007-10-30 17:57:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-11-18 23:53:37 +00:00
|
|
|
bool just_started = false;
|
|
|
|
|
2009-11-25 23:37:15 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2008-04-18 04:54:09 +00:00
|
|
|
SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
|
2009-11-25 23:37:15 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
|
2008-04-18 04:54:09 +00:00
|
|
|
}
|
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)) {
|
2008-11-18 23:53:37 +00:00
|
|
|
if (travelling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) {
|
|
|
|
/* Need to clear that now as otherwise we are not able to reduce the wait time */
|
|
|
|
v->current_order.wait_time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (just_started) return;
|
|
|
|
|
|
|
|
/* Modify station waiting time only if our new value is larger (this is
|
|
|
|
* always the case when we cleared the timetable). */
|
|
|
|
if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.wait_time)) {
|
2007-06-25 20:55:43 +00:00
|
|
|
/* 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. */
|
2010-04-18 14:56:05 +00:00
|
|
|
time_taken = CeilDiv(time_taken, DAY_TICKS) * DAY_TICKS;
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2008-11-18 23:53:37 +00:00
|
|
|
ChangeTimetable(v, v->cur_order_index, time_taken, travelling);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->cur_order_index == 0 && travelling) {
|
|
|
|
/* If we just started we would have returned earlier and have not reached
|
|
|
|
* this code. So obviously, we have completed our round: So turn autofill
|
|
|
|
* off again. */
|
2007-11-19 21:32:20 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
2008-11-18 23:53:37 +00:00
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
2008-11-18 23:53:37 +00:00
|
|
|
return;
|
2008-04-18 04:54:09 +00:00
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2008-11-18 23:53:37 +00:00
|
|
|
if (just_started) return;
|
|
|
|
|
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()) {
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
|
2007-10-30 17:57:51 +00:00
|
|
|
}
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|