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"
|
2011-02-07 22:08:11 +00:00
|
|
|
#include "company_func.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"
|
2015-08-04 20:24:04 +00:00
|
|
|
#include "settings_type.h"
|
2012-02-14 17:04:01 +00:00
|
|
|
#include "cmd_helper.h"
|
2015-10-25 12:47:23 +00:00
|
|
|
#include "company_base.h"
|
2013-06-09 13:55:33 +00:00
|
|
|
#include "core/sort_func.hpp"
|
2015-08-01 18:47:09 +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
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-05-01 19:14:12 +00:00
|
|
|
/**
|
|
|
|
* Change/update a particular timetable entry.
|
|
|
|
* @param v The vehicle to change the timetable of.
|
|
|
|
* @param order_number The index of the timetable in the order list.
|
2012-02-14 17:04:01 +00:00
|
|
|
* @param val The new data of the timetable entry.
|
|
|
|
* @param mtf Which part of the timetable entry to change.
|
2014-05-01 14:49:16 +00:00
|
|
|
* @param timetabled If the new value is explicitly timetabled.
|
2011-05-01 19:14:12 +00:00
|
|
|
*/
|
2014-05-01 14:49:16 +00:00
|
|
|
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf, bool timetabled)
|
2007-06-25 20:55:43 +00:00
|
|
|
{
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = v->GetOrder(order_number);
|
2014-05-01 14:49:16 +00:00
|
|
|
int total_delta = 0;
|
|
|
|
int timetable_delta = 0;
|
2012-02-14 17:04:01 +00:00
|
|
|
|
|
|
|
switch (mtf) {
|
|
|
|
case MTF_WAIT_TIME:
|
2014-05-01 14:49:16 +00:00
|
|
|
total_delta = val - order->GetWaitTime();
|
|
|
|
timetable_delta = (timetabled ? val : 0) - order->GetTimetabledWait();
|
2014-05-01 14:48:44 +00:00
|
|
|
order->SetWaitTime(val);
|
2014-05-01 14:49:16 +00:00
|
|
|
order->SetWaitTimetabled(timetabled);
|
2012-02-14 17:04:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MTF_TRAVEL_TIME:
|
2014-05-01 14:49:16 +00:00
|
|
|
total_delta = val - order->GetTravelTime();
|
|
|
|
timetable_delta = (timetabled ? val : 0) - order->GetTimetabledTravel();
|
2014-05-01 14:48:44 +00:00
|
|
|
order->SetTravelTime(val);
|
2014-05-01 14:49:16 +00:00
|
|
|
order->SetTravelTimetabled(timetabled);
|
2012-02-14 17:04:01 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-14 17:04:06 +00:00
|
|
|
case MTF_TRAVEL_SPEED:
|
2014-05-01 14:48:44 +00:00
|
|
|
order->SetMaxSpeed(val);
|
2012-02-14 17:04:06 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-14 17:04:01 +00:00
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
2014-05-01 14:49:16 +00:00
|
|
|
v->orders.list->UpdateTotalDuration(total_delta);
|
|
|
|
v->orders.list->UpdateTimetableDuration(timetable_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()) {
|
2011-01-31 20:44:15 +00:00
|
|
|
if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
|
2012-02-14 17:04:01 +00:00
|
|
|
switch (mtf) {
|
|
|
|
case MTF_WAIT_TIME:
|
2014-05-01 14:48:44 +00:00
|
|
|
v->current_order.SetWaitTime(val);
|
2014-05-01 14:49:16 +00:00
|
|
|
v->current_order.SetWaitTimetabled(timetabled);
|
2012-02-14 17:04:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MTF_TRAVEL_TIME:
|
2014-05-01 14:48:44 +00:00
|
|
|
v->current_order.SetTravelTime(val);
|
2014-05-01 14:49:16 +00:00
|
|
|
v->current_order.SetTravelTimetabled(timetabled);
|
2012-02-14 17:04:01 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-14 17:04:06 +00:00
|
|
|
case MTF_TRAVEL_SPEED:
|
2014-05-01 14:48:44 +00:00
|
|
|
v->current_order.SetMaxSpeed(val);
|
2012-02-14 17:04:06 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-14 17:04:01 +00:00
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
2008-08-30 10:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
/**
|
2012-02-14 17:04:01 +00:00
|
|
|
* Change timetable data of an order.
|
2007-06-20 19:26:25 +00:00
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Various bitstuffed elements
|
2010-08-19 20:34:51 +00:00
|
|
|
* - p1 = (bit 0-19) - Vehicle with the orders to change.
|
|
|
|
* - p1 = (bit 20-27) - Order index to modify.
|
2012-02-14 17:04:01 +00:00
|
|
|
* - p1 = (bit 28-29) - Timetable data to change (@see ModifyTimetableFlags)
|
2015-08-09 14:12:24 +00:00
|
|
|
* - p1 = (bit 30) - 0 to set timetable wait/travel time, 1 to clear it
|
2007-06-20 19:26:25 +00:00
|
|
|
* @param p2 The amount of time to wait.
|
2012-02-14 17:04:01 +00:00
|
|
|
* - p2 = (bit 0-15) - The data to modify as specified by p1 bits 28-29.
|
2015-06-20 11:28:25 +00:00
|
|
|
* 0 to clear times, UINT16_MAX to clear speed limit.
|
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
|
|
|
{
|
2010-08-19 20:34:51 +00:00
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
2007-06-20 19:26:25 +00:00
|
|
|
|
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
|
|
|
|
2010-08-19 20:34:51 +00:00
|
|
|
VehicleOrderID order_number = GB(p1, 20, 8);
|
2009-05-23 12:27:42 +00:00
|
|
|
Order *order = v->GetOrder(order_number);
|
2011-05-18 12:19:58 +00:00
|
|
|
if (order == NULL || order->IsType(OT_IMPLICIT)) return CMD_ERROR;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
2012-02-14 17:04:01 +00:00
|
|
|
ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
|
|
|
|
if (mtf >= MTF_END) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
|
2015-08-09 14:12:24 +00:00
|
|
|
bool clear_field = GB(p1, 30, 1) == 1;
|
|
|
|
|
2014-05-01 14:53:06 +00:00
|
|
|
int wait_time = order->GetWaitTime();
|
|
|
|
int travel_time = order->GetTravelTime();
|
2014-05-01 14:48:44 +00:00
|
|
|
int max_speed = order->GetMaxSpeed();
|
2012-02-14 17:04:01 +00:00
|
|
|
switch (mtf) {
|
|
|
|
case MTF_WAIT_TIME:
|
|
|
|
wait_time = GB(p2, 0, 16);
|
2015-08-09 14:12:24 +00:00
|
|
|
if (clear_field) assert(wait_time == 0);
|
2012-02-14 17:04:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MTF_TRAVEL_TIME:
|
|
|
|
travel_time = GB(p2, 0, 16);
|
2015-08-09 14:12:24 +00:00
|
|
|
if (clear_field) assert(travel_time == 0);
|
2012-02-14 17:04:01 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-14 17:04:06 +00:00
|
|
|
case MTF_TRAVEL_SPEED:
|
|
|
|
max_speed = GB(p2, 0, 16);
|
2012-03-20 13:11:20 +00:00
|
|
|
if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
|
2012-02-14 17:04:06 +00:00
|
|
|
break;
|
|
|
|
|
2012-02-14 17:04:01 +00:00
|
|
|
default:
|
|
|
|
NOT_REACHED();
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 14:53:06 +00:00
|
|
|
if (wait_time != order->GetWaitTime()) {
|
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;
|
|
|
|
|
2015-08-03 00:06:05 +00:00
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
break;
|
|
|
|
|
2008-07-20 07:41:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 14:53:06 +00:00
|
|
|
if (travel_time != order->GetTravelTime() && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
|
2014-05-01 14:48:44 +00:00
|
|
|
if (max_speed != order->GetMaxSpeed() && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
|
2008-07-20 07:41:43 +00:00
|
|
|
|
2007-06-20 19:26:25 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2015-06-20 11:28:25 +00:00
|
|
|
switch (mtf) {
|
|
|
|
case MTF_WAIT_TIME:
|
|
|
|
/* Set time if changing the value or confirming an estimated time as timetabled. */
|
2015-08-09 14:12:24 +00:00
|
|
|
if (wait_time != order->GetWaitTime() || (clear_field == order->IsWaitTimetabled())) {
|
|
|
|
ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, !clear_field);
|
2015-06-20 11:28:25 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTF_TRAVEL_TIME:
|
|
|
|
/* Set time if changing the value or confirming an estimated time as timetabled. */
|
2015-08-09 14:12:24 +00:00
|
|
|
if (travel_time != order->GetTravelTime() || (clear_field == order->IsTravelTimetabled())) {
|
|
|
|
ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME, !clear_field);
|
2015-06-20 11:28:25 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MTF_TRAVEL_SPEED:
|
|
|
|
if (max_speed != order->GetMaxSpeed()) {
|
|
|
|
ChangeTimetable(v, order_number, max_speed, MTF_TRAVEL_SPEED, max_speed != UINT16_MAX);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2014-05-01 14:53:06 +00:00
|
|
|
}
|
2007-06-20 19:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2016-03-02 22:36:21 +00:00
|
|
|
/**
|
|
|
|
* Change timetable data of all orders of a vehicle.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Various bitstuffed elements
|
|
|
|
* - p1 = (bit 0-19) - Vehicle with the orders to change.
|
|
|
|
* - p1 = (bit 20-27) - unused
|
|
|
|
* - p1 = (bit 28-29) - Timetable data to change (@see ModifyTimetableFlags)
|
|
|
|
* - p1 = (bit 30) - 0 to set timetable wait/travel time, 1 to clear it
|
|
|
|
* @param p2 The amount of time to wait.
|
|
|
|
* - p2 = (bit 0-15) - The data to modify as specified by p1 bits 28-29.
|
|
|
|
* 0 to clear times, UINT16_MAX to clear speed limit.
|
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
|
|
|
CommandCost CmdBulkChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
|
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
|
|
ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
|
|
|
|
if (mtf >= MTF_END) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (v->GetNumOrders() == 0) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
for (VehicleOrderID order_number = 0; order_number < v->GetNumOrders(); order_number++) {
|
|
|
|
Order *order = v->GetOrder(order_number);
|
|
|
|
if (order == NULL || order->IsType(OT_IMPLICIT)) continue;
|
|
|
|
|
|
|
|
uint32 new_p1 = p1;
|
|
|
|
SB(new_p1, 20, 8, order_number);
|
|
|
|
DoCommand(tile, new_p1, p2, flags, CMD_CHANGE_TIMETABLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2007-06-20 19:26:25 +00:00
|
|
|
/**
|
|
|
|
* Clear the lateness counter to make the vehicle on time.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Various bitstuffed elements
|
2010-08-19 20:34:51 +00:00
|
|
|
* - p1 = (bit 0-19) - 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
|
|
|
{
|
2010-08-19 20:34:51 +00:00
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
2007-06-20 19:26:25 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2013-07-17 18:37:13 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle() || v->orders.list == NULL) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-06-09 13:55:33 +00:00
|
|
|
/**
|
|
|
|
* Order vehicles based on their timetable. The vehicles will be sorted in order
|
|
|
|
* they would reach the first station.
|
|
|
|
*
|
|
|
|
* @param ap First Vehicle pointer.
|
|
|
|
* @param bp Second Vehicle pointer.
|
|
|
|
* @return Comparison value.
|
|
|
|
*/
|
|
|
|
static int CDECL VehicleTimetableSorter(Vehicle * const *ap, Vehicle * const *bp)
|
|
|
|
{
|
|
|
|
const Vehicle *a = *ap;
|
|
|
|
const Vehicle *b = *bp;
|
|
|
|
|
|
|
|
VehicleOrderID a_order = a->cur_real_order_index;
|
|
|
|
VehicleOrderID b_order = b->cur_real_order_index;
|
|
|
|
int j = (int)b_order - (int)a_order;
|
|
|
|
|
|
|
|
/* Are we currently at an ordered station (un)loading? */
|
|
|
|
bool a_load = a->current_order.IsType(OT_LOADING) && a->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
|
|
|
|
bool b_load = b->current_order.IsType(OT_LOADING) && b->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
|
|
|
|
|
|
|
|
/* If the current order is not loading at the ordered station, decrease the order index by one since we have
|
|
|
|
* not yet arrived at the station (and thus the timetable entry; still in the travelling of the previous one).
|
|
|
|
* Since the ?_order variables are unsigned the -1 will flow under and place the vehicles going to order #0 at
|
|
|
|
* the begin of the list with vehicles arriving at #0. */
|
|
|
|
if (!a_load) a_order--;
|
|
|
|
if (!b_load) b_order--;
|
|
|
|
|
|
|
|
/* First check the order index that accounted for loading, then just the raw one. */
|
|
|
|
int i = (int)b_order - (int)a_order;
|
|
|
|
if (i != 0) return i;
|
|
|
|
if (j != 0) return j;
|
|
|
|
|
|
|
|
/* Look at the time we spent in this order; the higher, the closer to its destination. */
|
|
|
|
i = b->current_order_time - a->current_order_time;
|
|
|
|
if (i != 0) return i;
|
|
|
|
|
|
|
|
/* If all else is equal, use some unique index to sort it the same way. */
|
|
|
|
return b->unitnumber - a->unitnumber;
|
|
|
|
}
|
|
|
|
|
2009-11-25 23:37:15 +00:00
|
|
|
/**
|
|
|
|
* Set the start date of the timetable.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
2015-11-30 19:48:44 +00:00
|
|
|
* @param p1 Various bitstuffed elements
|
|
|
|
* - p1 = (bit 0-19) - Vehicle ID.
|
|
|
|
* - p1 = (bit 20) - Set to 1 to set timetable start for all vehicles sharing this order
|
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)
|
|
|
|
{
|
2013-06-09 13:55:33 +00:00
|
|
|
bool timetable_all = HasBit(p1, 20);
|
2010-08-19 20:34:51 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
|
2013-07-17 18:37:13 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle() || v->orders.list == NULL) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-11-25 23:37:15 +00:00
|
|
|
|
2013-06-09 13:55:33 +00:00
|
|
|
if (timetable_all && !v->orders.list->IsCompleteTimetable()) return CMD_ERROR;
|
2016-01-30 15:43:31 +00:00
|
|
|
|
|
|
|
DateTicks start_date = ((DateTicks)_date * DAY_TICKS) + _date_fract + (DateTicks)(int32)p2;
|
2009-11-25 23:37:15 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2013-06-09 13:55:33 +00:00
|
|
|
SmallVector<Vehicle *, 8> vehs;
|
|
|
|
|
|
|
|
if (timetable_all) {
|
|
|
|
for (Vehicle *w = v->orders.list->GetFirstSharedVehicle(); w != NULL; w = w->NextShared()) {
|
|
|
|
*vehs.Append() = w;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*vehs.Append() = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
int total_duration = v->orders.list->GetTimetableTotalDuration();
|
|
|
|
int num_vehs = vehs.Length();
|
|
|
|
|
|
|
|
if (num_vehs >= 2) {
|
|
|
|
QSortT(vehs.Begin(), vehs.Length(), &VehicleTimetableSorter);
|
|
|
|
}
|
|
|
|
|
|
|
|
int base = vehs.FindIndex(v);
|
|
|
|
|
|
|
|
for (Vehicle **viter = vehs.Begin(); viter != vehs.End(); viter++) {
|
|
|
|
int idx = (viter - vehs.Begin()) - base;
|
|
|
|
Vehicle *w = *viter;
|
|
|
|
|
|
|
|
w->lateness_counter = 0;
|
|
|
|
ClrBit(w->vehicle_flags, VF_TIMETABLE_STARTED);
|
|
|
|
/* Do multiplication, then division to reduce rounding errors. */
|
2016-01-30 16:23:38 +00:00
|
|
|
w->timetable_start = start_date + idx * total_duration / (num_vehs * _settings_game.economy.day_length_factor);
|
|
|
|
if (w->timetable_start < (((DateTicks)_date * DAY_TICKS) + _date_fract) && idx < 0) {
|
|
|
|
w->timetable_start += (total_duration / _settings_game.economy.day_length_factor);
|
2016-01-30 15:16:35 +00:00
|
|
|
}
|
2013-06-09 13:55:33 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, w->index);
|
|
|
|
}
|
2009-11-25 23:37:15 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2010-08-19 20:34:51 +00:00
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2009-05-18 16:21:28 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
2013-07-17 18:37:13 +00:00
|
|
|
if (v == NULL || !v->IsPrimaryVehicle() || v->orders.list == NULL) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-12-29 20:20:38 +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);
|
|
|
|
}
|
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
|
2009-02-06 21:30:18 +00:00
|
|
|
}
|
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
|
|
|
|
2015-08-04 20:24:04 +00:00
|
|
|
/**
|
|
|
|
* Start or stop automatic management of timetables.
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Vehicle index.
|
|
|
|
* @param p2 Various bitstuffed elements
|
|
|
|
* - p2 = (bit 0) - Set to 1 to enable, 0 to disable automation.
|
|
|
|
* - p2 = (bit 1) - Ctrl was pressed. Used when disabling to keep times.
|
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
|
|
|
|
|
|
|
CommandCost CmdAutomateTimetable(TileIndex index, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
VehicleID veh = GB(p1, 0, 16);
|
2015-08-04 20:29:19 +00:00
|
|
|
|
2015-08-04 20:24:04 +00:00
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
|
|
|
|
if (HasBit(p2, 0)) {
|
|
|
|
/* Automated timetable. Set flags and clear current times. */
|
|
|
|
SetBit(v2->vehicle_flags, VF_AUTOMATE_TIMETABLE);
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
|
|
|
ClrBit(v2->vehicle_flags, VF_TIMETABLE_STARTED);
|
|
|
|
v2->timetable_start = 0;
|
|
|
|
v2->lateness_counter = 0;
|
|
|
|
v2->current_loading_time = 0;
|
|
|
|
v2->ClearSeparation();
|
|
|
|
} else {
|
|
|
|
/* De-automate timetable. Clear flags. */
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOMATE_TIMETABLE);
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
|
|
|
ClrBit(v2->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
|
|
|
v2->ClearSeparation();
|
|
|
|
if (!HasBit(p2, 1)) {
|
|
|
|
/* Ctrl wasn't pressed, so clear all timetabled times. */
|
|
|
|
SetBit(v2->vehicle_flags, VF_TIMETABLE_STARTED);
|
|
|
|
v2->timetable_start = 0;
|
|
|
|
v2->lateness_counter = 0;
|
|
|
|
v2->current_loading_time = 0;
|
|
|
|
OrderList *orders = v2->orders.list;
|
|
|
|
if (orders != NULL) {
|
|
|
|
for (int i = 0; i < orders->GetNumOrders(); i++) {
|
2015-11-30 19:48:44 +00:00
|
|
|
ChangeTimetable(v2, i, 0, MTF_WAIT_TIME, false);
|
|
|
|
ChangeTimetable(v2, i, 0, MTF_TRAVEL_TIME, false);
|
2015-08-04 20:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2016-05-07 00:15:46 +00:00
|
|
|
/**
|
|
|
|
* Enable or disable auto timetable separation
|
|
|
|
* @param tile Not used.
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 Vehicle index.
|
|
|
|
* @param p2 Various bitstuffed elements
|
|
|
|
* - p2 = (bit 0) - Set to 1 to enable, 0 to disable auto separatiom.
|
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
|
|
|
CommandCost CmdTimetableSeparation(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
VehicleID veh = GB(p1, 0, 20);
|
|
|
|
|
|
|
|
Vehicle *v = Vehicle::GetIfValid(veh);
|
|
|
|
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
|
|
|
|
if (HasBit(p2, 0)) {
|
|
|
|
SetBit(v2->vehicle_flags, VF_TIMETABLE_SEPARATION);
|
|
|
|
} else {
|
|
|
|
ClrBit(v2->vehicle_flags, VF_TIMETABLE_SEPARATION);
|
|
|
|
}
|
|
|
|
v2->ClearSeparation();
|
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2015-08-09 14:54:08 +00:00
|
|
|
static inline bool IsOrderUsableForSeparation(const Order *order)
|
|
|
|
{
|
|
|
|
if (order->IsType(OT_CONDITIONAL)) {
|
|
|
|
// Auto separation is unlikely to useful work at all if one of these is present, so give up
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (order->GetWaitTime() == 0 && order->IsType(OT_GOTO_STATION)) {
|
|
|
|
// non-station orders are permitted to have 0 wait times
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (order->GetTravelTime() == 0 && !order->IsTravelTimetabled()) {
|
|
|
|
// 0 travel times are permitted, if explicitly timetabled
|
|
|
|
// this is useful for depot service orders
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-04 20:24:04 +00:00
|
|
|
int TimeToFinishOrder(Vehicle *v, int n)
|
|
|
|
{
|
|
|
|
int left;
|
|
|
|
Order *order = v->GetOrder(n);
|
|
|
|
int wait_time = order->GetWaitTime();
|
|
|
|
int travel_time = order->GetTravelTime();
|
|
|
|
assert(order != NULL);
|
2015-08-09 14:54:08 +00:00
|
|
|
if (!IsOrderUsableForSeparation(order)) return -1;
|
2015-08-04 20:24:04 +00:00
|
|
|
if ((v->cur_real_order_index == n) && (v->last_station_visited == order->GetDestination())) {
|
|
|
|
if (v->current_loading_time > 0) {
|
|
|
|
left = wait_time - v->current_order_time;
|
|
|
|
} else {
|
|
|
|
left = wait_time;
|
|
|
|
}
|
|
|
|
if (left < 0) left = 0;
|
|
|
|
} else {
|
|
|
|
left = travel_time;
|
|
|
|
if (v->cur_real_order_index == n) left -= v->current_order_time;
|
|
|
|
if (left < 0) left = 0;
|
|
|
|
left +=wait_time;
|
|
|
|
}
|
|
|
|
return left;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SeparationBetween(Vehicle *v1, Vehicle *v2)
|
|
|
|
{
|
|
|
|
if (v1 == v2) return -1;
|
|
|
|
int separation = 0;
|
|
|
|
int time;
|
|
|
|
int n = v1->cur_real_order_index;
|
|
|
|
while (n != v2->cur_real_order_index) {
|
|
|
|
time = TimeToFinishOrder(v1, n);
|
|
|
|
if (time == -1) return -1;
|
|
|
|
separation += time;
|
|
|
|
n++;
|
|
|
|
if (n >= v1->GetNumOrders()) n = 0;
|
|
|
|
}
|
|
|
|
int time1 = TimeToFinishOrder(v1, n);
|
|
|
|
int time2 = TimeToFinishOrder(v2, n);
|
|
|
|
if (time1 == -1 || time2 == -1) return -1;
|
|
|
|
time = time1 - time2;
|
|
|
|
if (time < 0) {
|
|
|
|
for (n = 0; n < v1->GetNumOrders(); n++) {
|
|
|
|
Order *order = v1->GetOrder(n);
|
2015-08-09 14:54:08 +00:00
|
|
|
if (!IsOrderUsableForSeparation(order)) return -1;
|
|
|
|
time += order->GetTravelTime() + order->GetWaitTime();
|
2015-08-04 20:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
separation += time;
|
|
|
|
assert(separation >= 0);
|
|
|
|
if (separation == 0) return -1;
|
|
|
|
return separation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateSeparationOrder(Vehicle *v_start)
|
|
|
|
{
|
|
|
|
/* First check if we have a vehicle ahead, and if not search for one. */
|
|
|
|
if (v_start->AheadSeparation() == NULL) {
|
|
|
|
v_start->InitSeparation();
|
|
|
|
}
|
|
|
|
if (v_start->AheadSeparation() == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Switch positions if necessary. */
|
|
|
|
int swaps = 0;
|
|
|
|
bool done = false;
|
|
|
|
while (!done) {
|
|
|
|
done = true;
|
|
|
|
int min_sep = SeparationBetween(v_start, v_start->AheadSeparation());
|
|
|
|
Vehicle *v = v_start;
|
|
|
|
do {
|
|
|
|
if (v != v_start) {
|
|
|
|
int tmp_sep = SeparationBetween(v_start, v);
|
|
|
|
if (tmp_sep < min_sep && tmp_sep != -1) {
|
|
|
|
swaps++;
|
|
|
|
if (swaps >= 50) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
done = false;
|
|
|
|
v_start->ClearSeparation();
|
|
|
|
v_start->AddToSeparationBehind(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int separation_ahead = SeparationBetween(v, v->AheadSeparation());
|
|
|
|
int separation_behind = SeparationBetween(v->BehindSeparation(), v);
|
2015-08-11 20:20:01 +00:00
|
|
|
if (separation_ahead != -1 && separation_behind != -1) {
|
2015-10-25 12:47:23 +00:00
|
|
|
Company *owner = Company::GetIfValid(v->owner);
|
|
|
|
uint8 timetable_separation_rate = owner ? owner->settings.auto_timetable_separation_rate : 100;
|
2015-08-11 20:20:01 +00:00
|
|
|
int new_lateness = (separation_ahead - separation_behind) / 2;
|
2015-10-25 12:47:23 +00:00
|
|
|
v->lateness_counter = (new_lateness * timetable_separation_rate +
|
|
|
|
v->lateness_counter * (100 - timetable_separation_rate)) / 100;
|
2015-08-11 20:20:01 +00:00
|
|
|
}
|
2015-08-04 20:24:04 +00:00
|
|
|
v = v->AheadSeparation();
|
|
|
|
} while (v != v_start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-01 19:14:12 +00:00
|
|
|
/**
|
|
|
|
* Update the timetable for the vehicle.
|
|
|
|
* @param v The vehicle to update the timetable for.
|
|
|
|
* @param travelling Whether we just travelled or waited at a station.
|
|
|
|
*/
|
2007-06-20 19:26:25 +00:00
|
|
|
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
|
|
|
|
{
|
2015-08-04 20:24:04 +00:00
|
|
|
if (!travelling) v->current_loading_time++; // +1 because this time is one tick behind
|
2007-06-20 19:26:25 +00:00
|
|
|
uint time_taken = v->current_order_time;
|
2015-08-04 20:24:04 +00:00
|
|
|
uint time_loading = v->current_loading_time;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
|
|
|
v->current_order_time = 0;
|
2015-08-04 20:24:04 +00:00
|
|
|
v->current_loading_time = 0;
|
2007-06-20 19:26:25 +00:00
|
|
|
|
2011-05-18 12:19:58 +00:00
|
|
|
if (v->current_order.IsType(OT_IMPLICIT)) return; // no timetabling of auto orders
|
2010-12-26 09:03:19 +00:00
|
|
|
|
2015-06-20 12:04:30 +00:00
|
|
|
if (v->cur_real_order_index >= v->GetNumOrders()) return;
|
|
|
|
Order *real_current_order = v->GetOrder(v->cur_real_order_index);
|
|
|
|
|
2010-12-26 09:03:19 +00:00
|
|
|
VehicleOrderID first_manual_order = 0;
|
2011-05-18 12:19:58 +00:00
|
|
|
for (Order *o = v->GetFirstOrder(); o != NULL && o->IsType(OT_IMPLICIT); o = o->next) {
|
2010-12-26 09:03:19 +00:00
|
|
|
++first_manual_order;
|
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2008-11-18 23:53:37 +00:00
|
|
|
bool just_started = false;
|
|
|
|
|
2015-08-04 20:24:04 +00:00
|
|
|
/* Start automated timetables at first opportunity */
|
|
|
|
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED) && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
|
2015-11-30 22:28:20 +00:00
|
|
|
v->ClearSeparation();
|
2015-08-04 20:24:04 +00:00
|
|
|
SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
|
|
|
|
v->lateness_counter = 0;
|
2016-05-07 00:15:46 +00:00
|
|
|
if (HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION)) UpdateSeparationOrder(v);
|
2015-08-04 20:24:04 +00:00
|
|
|
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
|
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-25 23:37:15 +00:00
|
|
|
/* This vehicle is arriving at the first destination in the timetable. */
|
2011-01-31 20:44:15 +00:00
|
|
|
if (v->cur_real_order_index == first_manual_order && travelling) {
|
2009-11-25 23:37:15 +00:00
|
|
|
/* 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) {
|
2015-08-09 12:51:36 +00:00
|
|
|
v->lateness_counter = ((_date * DAY_TICKS) + _date_fract - v->timetable_start) * _settings_game.economy.day_length_factor + _tick_skip_counter;
|
2009-11-25 23:37:15 +00:00
|
|
|
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
|
|
|
|
2014-05-01 14:49:16 +00:00
|
|
|
bool autofilling = HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
2015-06-20 12:04:30 +00:00
|
|
|
bool remeasure_wait_time = !real_current_order->IsWaitTimetabled() ||
|
|
|
|
(autofilling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME));
|
|
|
|
|
|
|
|
if (travelling && remeasure_wait_time) {
|
|
|
|
/* We just finished travelling and want to remeasure the loading time,
|
|
|
|
* so do not apply any restrictions for the loading to finish. */
|
2014-05-01 14:49:16 +00:00
|
|
|
v->current_order.SetWaitTime(0);
|
|
|
|
}
|
2008-11-18 23:53:37 +00:00
|
|
|
|
2014-05-01 14:49:16 +00:00
|
|
|
if (just_started) return;
|
2008-11-18 23:53:37 +00:00
|
|
|
|
2015-06-20 12:04:30 +00:00
|
|
|
/* Before modifying waiting times, check whether we want to preserve bigger ones. */
|
|
|
|
if (!real_current_order->IsType(OT_CONDITIONAL) &&
|
|
|
|
(travelling || time_taken > real_current_order->GetWaitTime() || remeasure_wait_time)) {
|
2016-05-06 22:16:17 +00:00
|
|
|
/* Round the time taken up to the nearest timetable rounding factor
|
|
|
|
* (default: day), as this will avoid confusion for people who are
|
|
|
|
* timetabling in days, and can be adjusted later by people who aren't.
|
2014-05-01 14:49:16 +00:00
|
|
|
* For trains/aircraft multiple movement cycles are done in one
|
|
|
|
* tick. This makes it possible to leave the station and process
|
|
|
|
* e.g. a depot order in the same tick, causing it to not fill
|
|
|
|
* the timetable entry like is done for road vehicles/ships.
|
|
|
|
* Thus always make sure at least one tick is used between the
|
|
|
|
* processing of different orders when filling the timetable. */
|
2016-05-06 22:16:17 +00:00
|
|
|
Company *owner = Company::GetIfValid(v->owner);
|
|
|
|
uint rounding_factor = owner ? owner->settings.timetable_autofill_rounding : DAY_TICKS;
|
|
|
|
uint time_to_set = CeilDiv(max(time_taken, 1U), rounding_factor) * rounding_factor;
|
2014-05-01 14:49:16 +00:00
|
|
|
|
2015-06-20 12:04:30 +00:00
|
|
|
if (travelling && (autofilling || !real_current_order->IsTravelTimetabled())) {
|
2014-05-01 14:49:16 +00:00
|
|
|
ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_TRAVEL_TIME, autofilling);
|
2015-06-20 12:04:30 +00:00
|
|
|
} else if (!travelling && (autofilling || !real_current_order->IsWaitTimetabled())) {
|
2014-05-01 14:49:16 +00:00
|
|
|
ChangeTimetable(v, v->cur_real_order_index, time_to_set, MTF_WAIT_TIME, autofilling);
|
2007-06-25 20:55:43 +00:00
|
|
|
}
|
2008-04-18 04:54:09 +00:00
|
|
|
}
|
2007-06-25 20:55:43 +00:00
|
|
|
|
2014-05-01 14:49:16 +00:00
|
|
|
if (v->cur_real_order_index == first_manual_order && 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. */
|
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
|
|
|
|
ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autofilling) return;
|
|
|
|
|
2015-06-20 12:04:30 +00:00
|
|
|
uint timetabled = travelling ? real_current_order->GetTimetabledTravel() :
|
|
|
|
real_current_order->GetTimetabledWait();
|
2008-11-18 23:53:37 +00:00
|
|
|
|
2015-08-04 20:24:04 +00:00
|
|
|
/* Update the timetable to gradually shift order times towards the actual travel times. */
|
|
|
|
if (timetabled != 0 && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
|
|
|
|
int32 new_time;
|
|
|
|
if (travelling) {
|
|
|
|
new_time = time_taken;
|
|
|
|
} else {
|
|
|
|
new_time = time_loading;
|
|
|
|
}
|
|
|
|
|
2015-08-11 20:06:21 +00:00
|
|
|
if (new_time > (int32)timetabled * 4 && travelling) {
|
2015-08-04 20:24:04 +00:00
|
|
|
/* Possible jam, clear time and restart timetable for all vehicles.
|
|
|
|
* Otherwise we risk trains blocking 1-lane stations for long times. */
|
|
|
|
ChangeTimetable(v, v->cur_real_order_index, 0, travelling ? MTF_TRAVEL_TIME : MTF_WAIT_TIME, true);
|
|
|
|
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
|
2015-11-30 22:28:20 +00:00
|
|
|
v2->ClearSeparation();
|
2015-08-04 20:24:04 +00:00
|
|
|
ClrBit(v2->vehicle_flags, VF_TIMETABLE_STARTED);
|
|
|
|
SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
|
|
|
|
}
|
|
|
|
return;
|
2015-08-11 20:06:21 +00:00
|
|
|
} else if (new_time >= (int32)timetabled / 2) {
|
|
|
|
/* Compute running average, with sign conversion to avoid negative overflow. */
|
|
|
|
if (new_time < (int32)timetabled) {
|
|
|
|
new_time = ((int32)timetabled * 3 + new_time * 2 + 2) / 5;
|
|
|
|
} else {
|
|
|
|
new_time = ((int32)timetabled * 9 + new_time + 5) / 10;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* new time is less than hald old time, set value directly */
|
2015-08-04 20:24:04 +00:00
|
|
|
}
|
2015-08-04 20:29:19 +00:00
|
|
|
|
2015-08-04 20:24:04 +00:00
|
|
|
if (new_time < 1) new_time = 1;
|
2016-01-30 15:16:35 +00:00
|
|
|
if (new_time != (int32)timetabled) {
|
2015-08-04 20:24:04 +00:00
|
|
|
ChangeTimetable(v, v->cur_real_order_index, new_time, travelling ? MTF_TRAVEL_TIME : MTF_WAIT_TIME, true);
|
2016-01-30 15:16:35 +00:00
|
|
|
}
|
2015-08-04 20:24:04 +00:00
|
|
|
} else if (timetabled == 0 && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
|
|
|
|
/* Add times for orders that are not yet timetabled, even while not autofilling */
|
2016-01-30 15:16:35 +00:00
|
|
|
if (travelling) {
|
|
|
|
ChangeTimetable(v, v->cur_real_order_index, time_taken, MTF_TRAVEL_TIME, true);
|
|
|
|
} else {
|
|
|
|
ChangeTimetable(v, v->cur_real_order_index, time_loading, MTF_WAIT_TIME, true);
|
|
|
|
}
|
2015-08-04 20:24:04 +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
|
|
|
|
2016-05-07 00:15:46 +00:00
|
|
|
if (HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION) && HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
|
2015-08-04 20:24:04 +00:00
|
|
|
v->current_order_time = time_taken;
|
|
|
|
v->current_loading_time = time_loading;
|
|
|
|
UpdateSeparationOrder(v);
|
|
|
|
v->current_order_time = 0;
|
|
|
|
v->current_loading_time = 0;
|
|
|
|
} else {
|
|
|
|
v->lateness_counter -= (timetabled - time_taken);
|
|
|
|
}
|
2007-06-20 19:26:25 +00:00
|
|
|
|
2011-01-18 14:27:43 +00:00
|
|
|
/* When we are more late than this timetabled bit takes we (somewhat expensively)
|
|
|
|
* check how many ticks the (fully filled) timetable has. If a timetable cycle is
|
|
|
|
* shorter than the amount of ticks we are late we reduce the lateness by the
|
|
|
|
* length of a full cycle till lateness is less than the length of a timetable
|
|
|
|
* cycle. When the timetable isn't fully filled the cycle will be INVALID_TICKS. */
|
|
|
|
if (v->lateness_counter > (int)timetabled) {
|
|
|
|
Ticks cycle = v->orders.list->GetTimetableTotalDuration();
|
|
|
|
if (cycle != INVALID_TICKS && v->lateness_counter > cycle) {
|
|
|
|
v->lateness_counter %= cycle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|