Scheduled Dispatch feature

Code is tested and run well on small-ish train network. Not tested extensively.
pull/19/head
innocenat 7 years ago
parent 2895b1921d
commit 0b8f0e64ec

@ -338,6 +338,7 @@ screenshot.h
sdl.h
sound/sdl_s.h
video/sdl_v.h
schdispatch.h
settings_func.h
settings_gui.h
settings_internal.h
@ -519,6 +520,7 @@ plans_gui.cpp
rail_gui.cpp
road_gui.cpp
roadveh_gui.cpp
schdispatch_gui.cpp
settings_gui.cpp
ship_gui.cpp
signs_gui.cpp
@ -616,6 +618,7 @@ plans_cmd.cpp
rail_cmd.cpp
road_cmd.cpp
roadveh_cmd.cpp
schdispatch_cmd.cpp
ship_cmd.cpp
signs_cmd.cpp
station_cmd.cpp

@ -239,6 +239,14 @@ CommandProc CmdModifySignalInstruction;
CommandProc CmdRemoveSignalInstruction;
CommandProc CmdSignalProgramMgmt;
CommandProc CmdScheduledDispatch;
CommandProc CmdScheduledDispatchAdd;
CommandProc CmdScheduledDispatchRemove;
CommandProc CmdScheduledDispatchSetDuration;
CommandProc CmdScheduledDispatchSetStartDate;
CommandProc CmdScheduledDispatchSetDelay;
CommandProc CmdScheduledDispatchResetLastDispatch;
CommandProc CmdAddPlan;
CommandProc CmdAddPlanLine;
CommandProc CmdRemovePlan;
@ -443,6 +451,14 @@ static const Command _command_proc_table[] = {
DEF_CMD(CmdRemoveSignalInstruction, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNAL_INSTRUCTION
DEF_CMD(CmdSignalProgramMgmt, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_SIGNAL_PROGRAM_MGMT
DEF_CMD(CmdScheduledDispatch, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH
DEF_CMD(CmdScheduledDispatchAdd, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH_ADD
DEF_CMD(CmdScheduledDispatchRemove, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH_REMOVE
DEF_CMD(CmdScheduledDispatchSetDuration, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH_SET_DURATION
DEF_CMD(CmdScheduledDispatchSetStartDate, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH_SET_START_DATE
DEF_CMD(CmdScheduledDispatchSetDelay, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH_SET_DELAY
DEF_CMD(CmdScheduledDispatchResetLastDispatch, 0, CMDT_ROUTE_MANAGEMENT ), // CMD_SCHEDULED_DISPATCH_RESET_LAST_DISPATCH
DEF_CMD(CmdAddPlan, 0, CMDT_OTHER_MANAGEMENT ), // CMD_ADD_PLAN
DEF_CMD(CmdAddPlanLine, 0, CMDT_OTHER_MANAGEMENT ), // CMD_ADD_PLAN_LINE
DEF_CMD(CmdRemovePlan, 0, CMDT_OTHER_MANAGEMENT ), // CMD_REMOVE_PLAN

@ -382,6 +382,14 @@ enum Commands {
CMD_REMOVE_SIGNAL_INSTRUCTION, ///< removes a signal instruction
CMD_SIGNAL_PROGRAM_MGMT, ///< removes a signal program management command
CMD_SCHEDULED_DISPATCH, ///< scheduled dispatch start
CMD_SCHEDULED_DISPATCH_ADD, ///< scheduled dispatch add
CMD_SCHEDULED_DISPATCH_REMOVE, ///< scheduled dispatch remove
CMD_SCHEDULED_DISPATCH_SET_DURATION, ///< scheduled dispatch set schedule duration
CMD_SCHEDULED_DISPATCH_SET_START_DATE, ///< scheduled dispatch set start date
CMD_SCHEDULED_DISPATCH_SET_DELAY, ///< scheduled dispatch set maximum allow delay
CMD_SCHEDULED_DISPATCH_RESET_LAST_DISPATCH, ///< scheduled dispatch reset last dispatch date
CMD_ADD_PLAN,
CMD_ADD_PLAN_LINE,
CMD_REMOVE_PLAN,

@ -4607,6 +4607,9 @@ STR_TIMETABLE_AUTOMATE_TOOLTIP :{BLACK}Manage t
STR_TIMETABLE_AUTO_SEPARATION :{BLACK}Auto Separation
STR_TIMETABLE_AUTO_SEPARATION_TOOLTIP :{BLACK}Automatically adjust timetable start times to ensure vehicle separation
STR_TIMETABLE_SCHEDULED_DISPATCH :{BLACK}Scheduled Dispatch
STR_TIMETABLE_SCHEDULED_DISPATCH_TOOLTIP :{BLACK}Open scheduled dispatch windows for automatic setting of timetable start time
STR_TIMETABLE_EXPECTED :{BLACK}Expected
STR_TIMETABLE_SCHEDULED :{BLACK}Scheduled
STR_TIMETABLE_EXPECTED_TOOLTIP :{BLACK}Switch between expected and scheduled
@ -5807,3 +5810,30 @@ STR_TMPL_RPLALLGUI_BUTTON_CANCEL :{BLACK}Cancel
STR_TMPL_RPLALLGUI_USE_TIP :{BLACK}Select a vehicle type from each list and press 'Replace All'. If you are happy with the result displayed in the template list, press 'Apply' to actually apply these changes.
STR_TMPL_CANT_CREATE :{WHITE}Can't create template or virtual vehicle...
# Scheduled Dispatch
STR_SCHDISPATCH_CAPTION :{WHITE}{VEHICLE} (Scheduled Dispatch)
STR_SCHDISPATCH_ENABLED :{BLACK}Enable
STR_SCHDISPATCH_ENABLED_TOOLTIP :{BLACK}Enable scheduled dispatching for this order. Required automatic separation to be off.
STR_SCHDISPATCH_ADD :{BLACK}Add Departure Slot
STR_SCHDISPATCH_ADD_TOOLTIP :{BLACK}Add new departure slot for this schedule.
STR_SCHDISPATCH_ADD_CAPTION :{BLACK}Departure slot
STR_SCHDISPATCH_DURATION :{BLACK}Duration
STR_SCHDISPATCH_DURATION_TOOLTIP :{BLACK}Set duration of this schedule.
STR_SCHDISPATCH_DURATION_CAPTION_MINUTE :{BLACK}Duration (minute)
STR_SCHDISPATCH_DURATION_CAPTION_DAY :{BLACK}Duration (day)
STR_SCHDISPATCH_START :{BLACK}Start Date
STR_SCHDISPATCH_START_TOOLTIP :{BLACK}Select a date to start this schedule.
STR_SCHDISPATCH_START_CAPTION_MINUTE :{BLACK}Start time (hhmm)
STR_SCHDISPATCH_DELAY :{BLACK}Delay
STR_SCHDISPATCH_DELAY_TOOLTIP :{BLACK}Select a date to start this schedule.
STR_SCHDISPATCH_DELAY_CAPTION_MINUTE :{BLACK}Delay (minute)
STR_SCHDISPATCH_DELAY_CAPTION_DAY :{BLACK}Delay (day)
STR_SCHDISPATCH_RESET_LAST_DISPATCH :{BLACK}Reset Last Dispatched
STR_SCHDISPATCH_RESET_LAST_DISPATCH_TOOLTIP :{BLACK}Reset the last dispatch variable. Useful if it stuck with long-future vehicle.
STR_SCHDISPATCH_SUMMARY_L1 :{BLACK}Last departure at {DATE_WALLCLOCK_TINY}. Requires {COMMA} vehicle{P "" s}.
STR_SCHDISPATCH_SUMMARY_L2 :{BLACK}This schedule repeats every {STRING3} and began on {DATE_WALLCLOCK_TINY}.
STR_SCHDISPATCH_SUMMARY_L3 :{BLACK}Maximum delay of {STRING3} is allow before the slot is skipped.
STR_SCHDISPATCH_SUMMARY_NOT_ENABLED :{BLACK}This schedule is not active.

@ -20,6 +20,7 @@
#include "station_type.h"
#include "vehicle_type.h"
#include "date_type.h"
#include "schdispatch.h"
#include <memory>
#include <vector>
@ -493,12 +494,22 @@ private:
Ticks timetable_duration; ///< NOSAVE: Total timetabled duration of the order list.
Ticks total_duration; ///< NOSAVE: Total (timetabled or not) duration of the order list.
std::vector<uint32> scheduled_dispatch; ///< Scheduled dispatch time
uint32 scheduled_dispatch_duration; ///< Scheduled dispatch duration
Date scheduled_dispatch_start_date; ///< Scheduled dispatch start date
uint16 scheduled_dispatch_start_full_date_fract;///< Scheduled dispatch start full date fraction;
/// this count to (DAY_TICK * _settings_game.economy.day_length_factor)
int32 scheduled_dispatch_last_dispatch; ///< Last vehicle dispatched offset
int32 scheduled_dispatch_max_delay; ///< Maximum allowed delay
public:
/** Default constructor producing an invalid order list. */
OrderList(VehicleOrderID num_orders = INVALID_VEH_ORDER_ID)
: first(NULL), num_orders(num_orders), num_manual_orders(0), num_vehicles(0), first_shared(NULL),
timetable_duration(0), total_duration(0) { }
timetable_duration(0), total_duration(0), scheduled_dispatch_duration(0),
scheduled_dispatch_start_date(-1), scheduled_dispatch_start_full_date_fract(0),
scheduled_dispatch_last_dispatch(0), scheduled_dispatch_max_delay(0) { }
/**
* Create an order list with the given order chain for the given vehicle.
@ -619,6 +630,77 @@ public:
void FreeChain(bool keep_orderlist = false);
void DebugCheckSanity() const;
/**
* Get the vector of all scheduled dispatch slot
* @return first scheduled dispatch
*/
inline const std::vector<uint32> &GetScheduledDispatch() { return this->scheduled_dispatch; }
void AddScheduledDispatch(uint32 offset);
void RemoveScheduledDispatch(uint32 offset);
void UpdateScheduledDispatch();
void ResetScheduledDispatch();
/**
* Set the scheduled dispatch duration, in scaled tick
* @param duration New duration
*/
inline void SetScheduledDispatchDuration(uint32 duration) { this->scheduled_dispatch_duration = duration; }
/**
* Get the scheduled dispatch duration, in scaled tick
* @return scheduled dispatch duration
*/
inline uint32 GetScheduledDispatchDuration() { return this->scheduled_dispatch_duration; }
/**
* Set the scheduled dispatch start
* @param start New start date
* @param fract New start full date fraction, see \c CmdScheduledDispatchSetStartDate
*/
inline void SetScheduledDispatchStartDate(Date start_date, uint16 start_full_date_fract)
{
this->scheduled_dispatch_start_date = start_date;
this->scheduled_dispatch_start_full_date_fract = start_full_date_fract;
}
/**
* Get the scheduled dispatch start date, in absolute scaled tick
* @return scheduled dispatch start date
*/
inline DateTicksScaled GetScheduledDispatchStartTick() { return SchdispatchConvertToScaledTick(this->scheduled_dispatch_start_date, this->scheduled_dispatch_start_full_date_fract); }
/**
* Whether the scheduled dispatch setting is valid
* @return scheduled dispatch start date fraction
*/
inline bool IsScheduledDispatchValid() { return this->scheduled_dispatch_start_date >= 0 && this->scheduled_dispatch_duration > 0; }
/**
* Set the scheduled dispatch last dispatch offset, in scaled tick
* @param duration New last dispatch offset
*/
inline void SetScheduledDispatchLastDispatch(int32 offset) { this->scheduled_dispatch_last_dispatch = offset; }
/**
* Get the scheduled dispatch last dispatch offset, in scaled tick
* @return scheduled dispatch last dispatch
*/
inline int32 GetScheduledDispatchLastDispatch() { return this->scheduled_dispatch_last_dispatch; }
/**
* Set the scheduled dispatch maximum allowed delay, in scaled tick
* @param delay New maximum allow delay
*/
inline void SetScheduledDispatchDelay(int32 delay) { this->scheduled_dispatch_max_delay = delay; }
/**
* Get the scheduled dispatch maximum alowed delay, in scaled tick
* @return scheduled dispatch last dispatch
*/
inline int32 GetScheduledDispatchDelay() { return this->scheduled_dispatch_max_delay; }
};
#define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)

@ -1816,6 +1816,12 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
} else {
ClrBit(dst->vehicle_flags, VF_TIMETABLE_SEPARATION);
}
/* Set manual dispatch bit if target has it. */
if (HasBit(src->vehicle_flags, VF_SCHEDULED_DISPATCH)) {
SetBit(dst->vehicle_flags, VF_SCHEDULED_DISPATCH);
} else {
ClrBit(dst->vehicle_flags, VF_SCHEDULED_DISPATCH);
}
ClrBit(dst->vehicle_flags, VF_AUTOFILL_TIMETABLE);
ClrBit(dst->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
@ -1887,6 +1893,22 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
dst->orders.list = new OrderList(first, dst);
}
/* Copy over scheduled dispatch data */
for (const auto& slot : src->orders.list->GetScheduledDispatch()) {
dst->orders.list->AddScheduledDispatch(slot);
}
dst->orders.list->SetScheduledDispatchDuration(src->orders.list->GetScheduledDispatchDuration());
dst->orders.list->SetScheduledDispatchDelay(src->orders.list->GetScheduledDispatchDelay());
{
Date start_date;
uint16 start_full_date_fract;
SchdispatchConvertToFullDateFract(
src->orders.list->GetScheduledDispatchStartTick(),
&start_date, &start_full_date_fract);
dst->orders.list->SetScheduledDispatchStartDate(start_date, start_full_date_fract);
}
/* Don't copy last dispatch, leave it at 0 (default) */
/* Set automation bit if target has it. */
if (HasBit(src->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
SetBit(dst->vehicle_flags, VF_AUTOMATE_TIMETABLE);
@ -1901,6 +1923,12 @@ CommandCost CmdCloneOrder(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32
} else {
ClrBit(dst->vehicle_flags, VF_TIMETABLE_SEPARATION);
}
/* Set manual dispatch bit if target has it. */
if (HasBit(src->vehicle_flags, VF_SCHEDULED_DISPATCH)) {
SetBit(dst->vehicle_flags, VF_SCHEDULED_DISPATCH);
} else {
ClrBit(dst->vehicle_flags, VF_SCHEDULED_DISPATCH);
}
InvalidateVehicleOrder(dst, VIWD_REMOVE_ALL_ORDERS);

@ -76,6 +76,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_STATION_CATCHMENT_INC, XSCF_NULL, 1, 1, "station_catchment_inc", NULL, NULL, NULL },
{ XSLFI_CUSTOM_BRIDGE_HEADS, XSCF_NULL, 1, 1, "custom_bridge_heads", NULL, NULL, NULL },
{ XSLFI_CHUNNEL, XSCF_NULL, 1, 1, "chunnel", NULL, NULL, "TUNN" },
{ XSLFI_SCHEDULED_DISPATCH, XSCF_NULL, 1, 1, "scheduled_dispatch", NULL, NULL, NULL },
{ XSLFI_NULL, XSCF_NULL, 0, 0, NULL, NULL, NULL, NULL },// This is the end marker
};

@ -50,6 +50,7 @@ enum SlXvFeatureIndex {
XSLFI_STATION_CATCHMENT_INC, ///< Station catchment radius increase
XSLFI_CUSTOM_BRIDGE_HEADS, ///< Custom bridge heads
XSLFI_CHUNNEL, ///< Tunnels under water (channel tunnel)
XSLFI_SCHEDULED_DISPATCH, ///< Scheduled vehicle dispatching
XSLFI_RIFF_HEADER_60_BIT, ///< Size field in RIFF chunk header is 60 bit
XSLFI_HEIGHT_8_BIT, ///< Map tile height is 8 bit instead of 4 bit, but savegame version may be before this became true in trunk

@ -245,7 +245,13 @@ static void Ptrs_ORDR()
const SaveLoad *GetOrderListDescription()
{
static const SaveLoad _orderlist_desc[] = {
SLE_REF(OrderList, first, REF_ORDER),
SLE_REF(OrderList, first, REF_ORDER),
SLE_CONDVARVEC_X(OrderList, scheduled_dispatch, SLE_UINT32, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SCHEDULED_DISPATCH)),
SLE_CONDVAR_X(OrderList, scheduled_dispatch_duration, SLE_UINT32, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SCHEDULED_DISPATCH)),
SLE_CONDVAR_X(OrderList, scheduled_dispatch_start_date, SLE_INT32, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SCHEDULED_DISPATCH)),
SLE_CONDVAR_X(OrderList, scheduled_dispatch_start_full_date_fract, SLE_UINT16, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SCHEDULED_DISPATCH)),
SLE_CONDVAR_X(OrderList, scheduled_dispatch_last_dispatch, SLE_INT32, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SCHEDULED_DISPATCH)),
SLE_CONDVAR_X(OrderList, scheduled_dispatch_max_delay, SLE_INT32, 0, SL_MAX_VERSION, SlXvFeatureTest(XSLFTO_AND, XSLFI_SCHEDULED_DISPATCH)),
SLE_END()
};

@ -0,0 +1,45 @@
/* $Id$ */
/*
* 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/>.
*/
/** @file schdispatch.h Functions related to scheduled dispatch. */
#ifndef SCHDISPATCH_H
#define SCHDISPATCH_H
#include "date_type.h"
#include "vehicle_type.h"
#include "settings_type.h"
void ShowSchdispatchWindow(const Vehicle *v);
/**
* Convert date and full date fraction to DateTicksScaled
* @param date Current date
* @param full_date_fract full date fraction, the number of scaled tick in current day
* @return DateTicksScaled for ths specified date/faction
*/
inline DateTicksScaled SchdispatchConvertToScaledTick(Date date, uint16 full_date_fract)
{
return ((DateTicksScaled)date * DAY_TICKS) * _settings_game.economy.day_length_factor + full_date_fract;
}
/**
* Convert DateTicksScaled to date and full date fraction format
* @param tick DateTicksScaled to convert
* @param date Point to date, for ourput
* @param full_date_fract Pointer to uint16, for output
*/
inline void SchdispatchConvertToFullDateFract(DateTicksScaled tick, Date* date, uint16* full_date_fract)
{
const int full_date = _settings_game.economy.day_length_factor * DAY_TICKS;
*date = tick / full_date;
*full_date_fract = tick % full_date;
}
#endif /* SCHDISPATCH_H */

@ -0,0 +1,350 @@
/* $Id$ */
/*
* 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/>.
*/
/** @file schdispatch_cmd.cpp Commands related to scheduled dispatching. */
#include "stdafx.h"
#include "command_func.h"
#include "company_func.h"
#include "date_func.h"
#include "date_type.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "settings_type.h"
#include "cmd_helper.h"
#include "company_base.h"
#include "core/sort_func.hpp"
#include "settings_type.h"
#include "schdispatch.h"
#include "vehicle_gui.h"
#include <algorithm>
#include "table/strings.h"
#include "safeguards.h"
/* We squeeze this amount into 14 bit of data, so we must guarantee that
DAY_TICKS * (max_day_length_factor+1) can fit in 14-bit
See CmdScheduledDispatchSetStartDate */
assert_compile(DAY_TICKS * 126 < 16384);
/**
* Enable or disable scheduled dispatch
* @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 scheduled dispatch.
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatch(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) {
if (!v->orders.list->IsScheduledDispatchValid()) v->orders.list->ResetScheduledDispatch();
for (Vehicle *v2 = v->FirstShared(); v2 != NULL; v2 = v2->NextShared()) {
if (HasBit(p2, 0)) {
SetBit(v2->vehicle_flags, VF_SCHEDULED_DISPATCH);
} else {
ClrBit(v2->vehicle_flags, VF_SCHEDULED_DISPATCH);
}
SetWindowDirty(WC_VEHICLE_TIMETABLE, v2->index);
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v2->index);
}
}
return CommandCost();
}
/**
* Add scheduled dispatch time offset
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Vehicle index.
* @param p2 Offset time to add.
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatchAdd(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 (v->orders.list == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
v->orders.list->AddScheduledDispatch(p2);
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v->index);
}
return CommandCost();
}
/**
* Remove scheduled dispatch time offset
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Vehicle index.
* @param p2 Offset time to remove
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatchRemove(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 (v->orders.list == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
v->orders.list->RemoveScheduledDispatch(p2);
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v->index);
}
return CommandCost();
}
/**
* Set scheduled dispatch duration
*
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Vehicle index
* @param p2 Duration, in scaled tick
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatchSetDuration(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 (v->orders.list == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
v->orders.list->SetScheduledDispatchDuration(p2);
v->orders.list->UpdateScheduledDispatch();
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v->index);
}
return CommandCost();
}
/**
* Set scheduled dispatch start date
*
* The parameter is quite tricky. The default maximum of daylength factor is 125,
* and with DAY_TICKS of 74 the result (maximum scaled tick per day) fits in 14 bit.
* Vehicle index in p1 takes 20 bit, so we have 12 bit here. The MSB of the fraction is stored here.
* The 2-bit LSB is stored in MSB of p2, which is start date. The default date is stored in int32,
* which only have topmost bit available. However, if the date reached 31 bits, that means it is over 1,000,000 years,
* so I think it is safe to steal another bit here.
*
* See also the assert_compile at the top of the file.
*
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 MSB of Start Full Date Fraction || Vehicle index
* @param p2 LSB of Start Full Date Fraction || Date to add.
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatchSetStartDate(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 (v->orders.list == NULL) return CMD_ERROR;
int32 date = (int32) GB(p2, 0, 30);
uint16 full_date_fract = (GB(p1, 20, 12) << 2) + GB(p2, 30, 2);
if (flags & DC_EXEC) {
v->orders.list->SetScheduledDispatchStartDate(date, full_date_fract);
v->orders.list->UpdateScheduledDispatch();
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v->index);
}
return CommandCost();
}
/**
* Set scheduled dispatch maximum allow delay
*
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Vehicle index
* @param p2 Maximum Delay, in scaled tick
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatchSetDelay(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 (v->orders.list == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
v->orders.list->SetScheduledDispatchDelay(p2);
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v->index);
}
return CommandCost();
}
/**
* Reset scheduled dispatch last dispatch vehicle time
*
* This is useful when the current duration is high, and the vehicle get dispatched at time in far future.
* Thus, the last dispatch time stays high so no new vehicle are dispatched between now and that time.
* By resetting this you set the last dispatch time to the current timetable start time,
* allowing new vehicle to be dispatched immediately.
*
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Vehicle index
* @param p2 Not used
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdScheduledDispatchResetLastDispatch(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 (v->orders.list == NULL) return CMD_ERROR;
if (flags & DC_EXEC) {
v->orders.list->SetScheduledDispatchLastDispatch(0);
SetWindowDirty(WC_SCHDISPATCH_SLOTS, v->index);
}
return CommandCost();
}
/**
* Add new scheduled dispatch slot at offsets time.
* @param offset The offset time to add.
*/
void OrderList::AddScheduledDispatch(uint32 offset)
{
/* Maintain sorted list status */
auto insert_position = std::lower_bound(this->scheduled_dispatch.begin(), this->scheduled_dispatch.end(), offset);
if (insert_position != this->scheduled_dispatch.end() && *insert_position == offset) {
return;
}
this->scheduled_dispatch.insert(insert_position, offset);
this->UpdateScheduledDispatch();
}
/**
* Remove scheduled dispatch slot at offsets time.
* @param offset The offset time to remove.
*/
void OrderList::RemoveScheduledDispatch(uint32 offset)
{
/* Maintain sorted list status */
auto erase_position = std::lower_bound(this->scheduled_dispatch.begin(), this->scheduled_dispatch.end(), offset);
if (erase_position == this->scheduled_dispatch.end() || *erase_position != offset) {
return;
}
this->scheduled_dispatch.erase(erase_position);
}
/**
* Update the scheduled dispatch start time to be the most recent possible.
*/
void OrderList::UpdateScheduledDispatch()
{
/* Most of the time this loop does not runs. It makes sure start date in in past */
while (this->GetScheduledDispatchStartTick() > _scaled_date_ticks) {
this->scheduled_dispatch_last_dispatch += this->GetScheduledDispatchDuration();
SchdispatchConvertToFullDateFract(
this->GetScheduledDispatchStartTick() - this->GetScheduledDispatchDuration(),
&this->scheduled_dispatch_start_date, &this->scheduled_dispatch_start_full_date_fract);
InvalidateWindowClassesData(WC_SCHDISPATCH_SLOTS, VIWD_MODIFY_ORDERS);
}
/* Most of the time this loop runs once. It makes sure the start date is as close to current time as possible. */
while (this->GetScheduledDispatchStartTick() + this->GetScheduledDispatchDuration() <= _scaled_date_ticks) {
this->scheduled_dispatch_last_dispatch -= this->GetScheduledDispatchDuration();
SchdispatchConvertToFullDateFract(
this->GetScheduledDispatchStartTick() + this->GetScheduledDispatchDuration(),
&this->scheduled_dispatch_start_date, &this->scheduled_dispatch_start_full_date_fract);
InvalidateWindowClassesData(WC_SCHDISPATCH_SLOTS, VIWD_MODIFY_ORDERS);
}
}
/**
* Reset the scheduled dispatch schedule.
*
* This only occurs during initialization of the scheduled dispatch for each shared order. Basically we set
* proper default value for start time and duration
*/
void OrderList::ResetScheduledDispatch()
{
if (_settings_client.gui.time_in_minutes) {
DateTicksScaled val;
val = MINUTES_DATE(MINUTES_DAY(CURRENT_MINUTE), 0, 0);
val -= _settings_client.gui.clock_offset;
val *= _settings_client.gui.ticks_per_minute;
Date start_date;
uint16 start_full_date_fract;
SchdispatchConvertToFullDateFract(val, &start_date, &start_full_date_fract);
/* Set to 00:00 of today, and 1 day */
this->SetScheduledDispatchStartDate(start_date, start_full_date_fract);
this->SetScheduledDispatchDuration(24 * 60 * _settings_client.gui.ticks_per_minute);
} else {
/* Set Jan 1st and 365 day */
this->SetScheduledDispatchStartDate(DAYS_TILL(_cur_year), 0);
this->SetScheduledDispatchDuration(365*DAY_TICKS);
}
}

@ -0,0 +1,584 @@
/* $Id$ */
/*
* 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/>.
*/
/** @file schdispatch_gui.cpp GUI code for Scheduled Dispatch */
#include "stdafx.h"
#include "command_func.h"
#include "gui.h"
#include "window_gui.h"
#include "window_func.h"
#include "textbuf_gui.h"
#include "strings_func.h"
#include "vehicle_base.h"
#include "string_func.h"
#include "spritecache.h"
#include "gfx_func.h"
#include "company_func.h"
#include "date_func.h"
#include "date_gui.h"
#include "vehicle_gui.h"
#include "settings_type.h"
#include "viewport_func.h"
#include "zoom_func.h"
#include <vector>
#include <algorithm>
#include "table/strings.h"
#include "table/string_colours.h"
#include "table/sprites.h"
#include "safeguards.h"
enum SchdispatchWidgets {
WID_SCHDISPATCH_CAPTION, ///< Caption of window.
WID_SCHDISPATCH_MATRIX, ///< Matrix of vehicles.
WID_SCHDISPATCH_V_SCROLL, ///< Vertical scrollbar.
WID_SCHDISPATCH_SUMMARY_PANEL, ///< Summary panel
WID_SCHDISPATCH_ENABLED, ///< Enable button.
WID_SCHDISPATCH_ADD, ///< Add Departure Time button
WID_SCHDISPATCH_SET_DURATION, ///< Duration button
WID_SCHDISPATCH_SET_START_DATE, ///< Start Date button
WID_SCHDISPATCH_SET_DELAY, ///< Delat button
WID_SCHDISPATCH_RESET_DISPATCH, ///< Reset dispatch button
};
/**
* Callback for when a time has been chosen to start the schedule
* @param windex The windows index
* @param date the actually chosen date
*/
static void SetScheduleStartDateIntl(uint32 windex, DateTicksScaled date)
{
Date start_date;
uint16 start_full_date_fract;
SchdispatchConvertToFullDateFract(date, &start_date, &start_full_date_fract);
uint32 p1 = 0, p2 = 0;
SB(p1, 0, 20, windex);
SB(p1, 20, 12, GB(start_full_date_fract, 2, 12));
SB(p2, 0, 30, start_date);
SB(p2, 30, 2, GB(start_full_date_fract, 0, 2));
DoCommandP(0, p1, p2, CMD_SCHEDULED_DISPATCH_SET_START_DATE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
}
/**
* Callback for when a time has been chosen to start the schedule
* @param window the window related to the setting of the date
* @param date the actually chosen date
*/
static void SetScheduleStartDateCallback(const Window *w, DateTicksScaled date)
{
SetScheduleStartDateIntl(w->window_number, date);
}
/**
* Callback for when a time has been chosen to add to the schedule
* @param p1 The p1 parameter to send to CmdScheduledDispatchAdd
* @param date the actually chosen date
*/
static void ScheduleAddIntl(uint32 p1, DateTicksScaled date)
{
VehicleID veh = GB(p1, 0, 20);
Vehicle *v = Vehicle::GetIfValid(veh);
if (v == NULL || !v->IsPrimaryVehicle()) return;
/* Make sure the time is the closest future to the timetable start */
DateTicksScaled start_tick = v->orders.list->GetScheduledDispatchStartTick();
while (date > start_tick) date -= v->orders.list->GetScheduledDispatchDuration();
while (date < start_tick) date += v->orders.list->GetScheduledDispatchDuration();
DoCommandP(0, v->index, (uint32)(date - start_tick), CMD_SCHEDULED_DISPATCH_ADD | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
}
/**
* Callback for when a time has been chosen to add to the schedule
* @param window the window related to the setting of the date
* @param date the actually chosen date
*/
static void ScheduleAddCallback(const Window *w, DateTicksScaled date)
{
ScheduleAddIntl(w->window_number, date);
}
/**
* Calculate the maximum number of vehicle required to run this timetable according to the dispatch schedule
* @param timetable_duration timetable duration in scaled tick
* @param schedule_duration scheduled dispatch duration in scaled tick
* @param offsets list of all dispatch offsets in the schedule
* @return maxinum number of vehicle required
*/
static int CalculateMaxRequiredVehicle(Ticks timetable_duration, uint32 schedule_duration, std::vector<uint32> offsets)
{
if (timetable_duration == INVALID_TICKS) return -1;
if (offsets.size() == 0) return -1;
/* Number of time required to ensure all vehicle are counted */
int required_loop = CeilDiv(timetable_duration, schedule_duration) + 1;
/* Create indice array to count maximum overlapping range */
std::vector<std::pair<uint32, int>> indices;
for (int i = 0; i < required_loop; i++) {
for (uint32 offset : offsets) {
indices.push_back(std::make_pair(i * schedule_duration + offset, 1));
indices.push_back(std::make_pair(i * schedule_duration + offset + timetable_duration, -1));
}
}
std::sort(indices.begin(), indices.end());
int current_count = 0;
int vehicle_count = 0;
for (const auto& inc : indices) {
current_count += inc.second;
if (current_count > vehicle_count) vehicle_count = current_count;
}
return vehicle_count;
}
struct SchdispatchWindow : Window {
const Vehicle *vehicle; ///< Vehicle monitored by the window.
int clicked_widget; ///< The widget that was clicked (used to determine what to do in OnQueryTextFinished)
Scrollbar *vscroll; ///< Verticle scrollbar
uint num_columns; ///< Number of columns.
uint item_count = 0; ///< Number of scheduled item
SchdispatchWindow(WindowDesc *desc, WindowNumber window_number) :
Window(desc),
vehicle(Vehicle::Get(window_number))
{
this->CreateNestedTree();
this->vscroll = this->GetScrollbar(WID_SCHDISPATCH_V_SCROLL);
this->FinishInitNested(window_number);
this->owner = this->vehicle->owner;
}
~SchdispatchWindow()
{
if (!FocusWindowById(WC_VEHICLE_VIEW, this->window_number)) {
MarkAllRouteStepsDirty(this->vehicle);
}
}
uint base_width;
uint header_width;
uint flag_width;
uint flag_height;
virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
{
switch (widget) {
case WID_SCHDISPATCH_MATRIX: {
uint min_height = 0;
SetDParamMaxValue(0, _settings_client.gui.time_in_minutes ? 0 : MAX_YEAR * DAYS_IN_YEAR);
Dimension unumber = GetStringBoundingBox(STR_JUST_DATE_WALLCLOCK_TINY);
const Sprite *spr = GetSprite(SPR_FLAG_VEH_STOPPED, ST_NORMAL);
this->flag_width = UnScaleGUI(spr->width) + WD_FRAMERECT_RIGHT;
this->flag_height = UnScaleGUI(spr->height);
min_height = max<uint>(unumber.height + WD_MATRIX_TOP, UnScaleGUI(spr->height));
this->header_width = this->flag_width + WD_FRAMERECT_LEFT;
this->base_width = unumber.width + this->header_width + 4;
resize->height = min_height;
resize->width = base_width;
size->width = resize->width * 3;
size->height = resize->height * 3;
fill->width = resize->width;
fill->height = resize->height;
break;
}
case WID_SCHDISPATCH_SUMMARY_PANEL:
size->height = WD_FRAMERECT_TOP + 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM;
break;
}
}
/**
* Set proper item_count to number of offsets in the schedule.
*/
void CountItem()
{
this->item_count = 0;
if (this->vehicle->orders.list != NULL) {
this->item_count = this->vehicle->orders.list->GetScheduledDispatch().size();
}
}
/**
* Some data on this window has become invalid.
* @param data Information about the changed data.
* @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
*/
virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
{
switch (data) {
case VIWD_MODIFY_ORDERS:
if (!gui_scope) break;
this->ReInit();
break;
}
}
virtual void OnPaint()
{
const Vehicle *v = this->vehicle;
CountItem();
if (v->owner == _local_company) {
this->SetWidgetDisabledState(WID_SCHDISPATCH_ENABLED, HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION));
this->SetWidgetDisabledState(WID_SCHDISPATCH_ADD, !HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH) && v->orders.list != NULL);
this->SetWidgetDisabledState(WID_SCHDISPATCH_SET_DURATION, !HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH) && v->orders.list != NULL);
this->SetWidgetDisabledState(WID_SCHDISPATCH_SET_START_DATE, !HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH) && v->orders.list != NULL);
} else {
this->DisableWidget(WID_SCHDISPATCH_ENABLED);
this->DisableWidget(WID_SCHDISPATCH_ADD);
this->DisableWidget(WID_SCHDISPATCH_SET_DURATION);
this->DisableWidget(WID_SCHDISPATCH_SET_START_DATE);
}
this->vscroll->SetCount(CeilDiv(this->item_count, this->num_columns));
this->SetWidgetLoweredState(WID_SCHDISPATCH_ENABLED, HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH));
this->DrawWidgets();
}
virtual void SetStringParameters(int widget) const
{
switch (widget) {
case WID_SCHDISPATCH_CAPTION: SetDParam(0, this->vehicle->index); break;
}
}
/**
* Draw a time in the box with the top left corner at x,y.
* @param time Time to draw.
* @param left Left side of the box to draw in.
* @param right Right side of the box to draw in.
* @param y Top of the box to draw in.
*/
void DrawScheduledTime(const int time, int left, int right, int y, TextColour colour) const
{
bool rtl = _current_text_dir == TD_RTL;
uint diff_x, diff_y;
diff_x = this->flag_width + WD_FRAMERECT_LEFT;
diff_y = (this->resize.step_height - this->flag_height) / 2 - 2;
int text_left = rtl ? right - this->base_width - 1 : left + diff_x;
int text_right = rtl ? right - diff_x : left + this->base_width - 1;
DrawSprite(SPR_FLAG_VEH_STOPPED, PAL_NONE, rtl ? right - this->flag_width : left + WD_FRAMERECT_LEFT, y + diff_y);
SetDParam(0, time);
DrawString(text_left, text_right, y + 2, STR_JUST_DATE_WALLCLOCK_TINY, colour);
}
virtual void DrawWidget(const Rect &r, int widget) const
{
const Vehicle *v = this->vehicle;
switch (widget) {
case WID_SCHDISPATCH_MATRIX: {
/* If order is not initialized, don't draw */
if (v->orders.list == NULL) break;
bool rtl = _current_text_dir == TD_RTL;
/* Set the row and number of boxes in each row based on the number of boxes drawn in the matrix */
const NWidgetCore *wid = this->GetWidget<NWidgetCore>(WID_SCHDISPATCH_MATRIX);
uint16 rows_in_display = wid->current_y / wid->resize_y;
uint16 num = this->vscroll->GetPosition() * this->num_columns;
int maxval = min(this->item_count, num + (rows_in_display * this->num_columns));
int y;
auto current_schedule = v->orders.list->GetScheduledDispatch().begin();
DateTicksScaled start_tick = v->orders.list->GetScheduledDispatchStartTick();
DateTicksScaled end_tick = v->orders.list->GetScheduledDispatchStartTick() + v->orders.list->GetScheduledDispatchDuration();
for (y = r.top + 1; num < maxval; y += this->resize.step_height) { /* Draw the rows */
for (byte i = 0; i < this->num_columns && num < maxval; i++, num++) {
/* Draw all departure time in the current row */
if (current_schedule != v->orders.list->GetScheduledDispatch().end()) {
int x = r.left + (rtl ? (this->num_columns - i - 1) : i) * this->resize.step_width;
DateTicksScaled draw_time = start_tick + *current_schedule;
this->DrawScheduledTime(draw_time, x, x + this->resize.step_width - 1, y, draw_time >= end_tick ? TC_RED : TC_BLACK);
current_schedule++;
} else {
break;
}
}
}
break;
}
case WID_SCHDISPATCH_SUMMARY_PANEL: {
int y = r.top + WD_FRAMERECT_TOP;
if (!HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH) || v->orders.list == NULL) {
y += FONT_HEIGHT_NORMAL;
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SCHDISPATCH_SUMMARY_NOT_ENABLED);
} else {
int required_vehicle = CalculateMaxRequiredVehicle(v->orders.list->GetTimetableTotalDuration(), v->orders.list->GetScheduledDispatchDuration(), v->orders.list->GetScheduledDispatch());
SetDParam(0, v->orders.list->GetScheduledDispatchStartTick() + v->orders.list->GetScheduledDispatchLastDispatch());
SetDParam(1, required_vehicle);
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SCHDISPATCH_SUMMARY_L1);
y += FONT_HEIGHT_NORMAL;
SetTimetableParams(0, v->orders.list->GetScheduledDispatchDuration());
SetDParam(4, v->orders.list->GetScheduledDispatchStartTick());
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SCHDISPATCH_SUMMARY_L2);
y += FONT_HEIGHT_NORMAL;
SetTimetableParams(0, v->orders.list->GetScheduledDispatchDelay());
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, STR_SCHDISPATCH_SUMMARY_L3);
}
break;
}
}
}
/**
* Handle click in the departure time matrix.
* @param x Horizontal position in the matrix widget in pixels.
* @param y Vertical position in the matrix widget in pixels.
*/
void TimeClick(int x, int y)
{
const NWidgetCore *matrix_widget = this->GetWidget<NWidgetCore>(WID_SCHDISPATCH_MATRIX);
/* In case of RTL the widgets are swapped as a whole */
if (_current_text_dir == TD_RTL) x = matrix_widget->current_x - x;
uint xt = 0, xm = 0;
xt = x / this->resize.step_width;
xm = x % this->resize.step_width;
if (xt >= this->num_columns) return;
uint row = y / this->resize.step_height;
if (row >= this->vscroll->GetCapacity()) return;
uint pos = ((row + this->vscroll->GetPosition()) * this->num_columns) + xt;
if (pos >= this->item_count) return;
if (xm <= this->header_width) {
DoCommandP(0, this->vehicle->index, this->vehicle->orders.list->GetScheduledDispatch()[pos], CMD_SCHEDULED_DISPATCH_REMOVE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
}
}
virtual void OnClick(Point pt, int widget, int click_count)
{
const Vehicle *v = this->vehicle;
this->clicked_widget = widget;
this->DeleteChildWindows(WC_QUERY_STRING);
switch (widget) {
case WID_SCHDISPATCH_MATRIX: { /* List */
NWidgetBase *nwi = this->GetWidget<NWidgetBase>(WID_SCHDISPATCH_MATRIX);
this->TimeClick(pt.x - nwi->pos_x, pt.y - nwi->pos_y);
break;
}
case WID_SCHDISPATCH_ENABLED: {
uint32 p2 = 0;
if (!HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH)) SetBit(p2, 0);
DoCommandP(0, v->index, p2, CMD_SCHEDULED_DISPATCH | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
break;
}
case WID_SCHDISPATCH_ADD: {
if (_settings_client.gui.time_in_minutes && _settings_client.gui.timetable_start_text_entry) {
ShowQueryString(STR_EMPTY, STR_SCHDISPATCH_ADD_CAPTION, 31, this, CS_NUMERAL, QSF_NONE);
} else {
ShowSetDateWindow(this, v->index, _scaled_date_ticks, _cur_year, _cur_year + 15, ScheduleAddCallback);
}
break;
}
case WID_SCHDISPATCH_SET_DURATION: {
SetDParam(0, RoundDivSU(v->orders.list->GetScheduledDispatchDuration(), _settings_client.gui.ticks_per_minute ? _settings_client.gui.ticks_per_minute : DAY_TICKS));
ShowQueryString(STR_JUST_INT, _settings_client.gui.time_in_minutes ? STR_SCHDISPATCH_DURATION_CAPTION_MINUTE : STR_SCHDISPATCH_DURATION_CAPTION_DAY, 31, this, CS_NUMERAL, QSF_NONE);
break;
}
case WID_SCHDISPATCH_SET_START_DATE: {
if (_settings_client.gui.time_in_minutes && _settings_client.gui.timetable_start_text_entry) {
uint64 time = _scaled_date_ticks;
time /= _settings_client.gui.ticks_per_minute;
time += _settings_client.gui.clock_offset;
time %= (24 * 60);
time = (time % 60) + (((time / 60) % 24) * 100);
SetDParam(0, time);
ShowQueryString(STR_JUST_INT, STR_SCHDISPATCH_START_CAPTION_MINUTE, 31, this, CS_NUMERAL, QSF_ACCEPT_UNCHANGED);
} else {
ShowSetDateWindow(this, v->index, _scaled_date_ticks, _cur_year, _cur_year + 15, SetScheduleStartDateCallback);
}
break;
}
case WID_SCHDISPATCH_SET_DELAY: {
SetDParam(0, RoundDivSU(v->orders.list->GetScheduledDispatchDelay(), _settings_client.gui.ticks_per_minute ? _settings_client.gui.ticks_per_minute : DAY_TICKS));
ShowQueryString(STR_JUST_INT, _settings_client.gui.time_in_minutes ? STR_SCHDISPATCH_DELAY_CAPTION_MINUTE : STR_SCHDISPATCH_DELAY_CAPTION_DAY, 31, this, CS_NUMERAL, QSF_NONE);
break;
}
case WID_SCHDISPATCH_RESET_DISPATCH: {
DoCommandP(0, v->index, 0, CMD_SCHEDULED_DISPATCH_RESET_LAST_DISPATCH | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
break;
}
}
this->SetDirty();
}
virtual void OnQueryTextFinished(char *str)
{
if (str == NULL) return;
const Vehicle *v = this->vehicle;
switch (this->clicked_widget) {
default: NOT_REACHED();
case WID_SCHDISPATCH_ADD: {
int32 val = StrEmpty(str) ? -1 : strtoul(str, NULL, 10);
if (val >= 0) {
uint minutes = (val % 100) % 60;
uint hours = (val / 100) % 24;
val = MINUTES_DATE(MINUTES_DAY(CURRENT_MINUTE), hours, minutes);
val -= _settings_client.gui.clock_offset;
val *= _settings_client.gui.ticks_per_minute;
ScheduleAddIntl(v->index, val);
}
break;
}
case WID_SCHDISPATCH_SET_START_DATE: {
int32 val = StrEmpty(str) ? -1 : strtoul(str, NULL, 10);
if (val >= 0) {
uint minutes = (val % 100) % 60;
uint hours = (val / 100) % 24;
val = MINUTES_DATE(MINUTES_DAY(CURRENT_MINUTE), hours, minutes);
val -= _settings_client.gui.clock_offset;
val *= _settings_client.gui.ticks_per_minute;
SetScheduleStartDateIntl(v->index, val);
}
break;
}
case WID_SCHDISPATCH_SET_DURATION: {
int32 val = StrEmpty(str) ? 0 : strtoul(str, NULL, 10);
if (val > 0) {
if (_settings_client.gui.time_in_minutes) {
val *= _settings_client.gui.ticks_per_minute;
} else {
val *= DAY_TICKS;
}
DoCommandP(0, v->index, val, CMD_SCHEDULED_DISPATCH_SET_DURATION | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
}
break;
}
case WID_SCHDISPATCH_SET_DELAY: {
int32 val = StrEmpty(str) ? -1 : strtoul(str, NULL, 10);
if (val >= 0) {
if (_settings_client.gui.time_in_minutes) {
val *= _settings_client.gui.ticks_per_minute;
} else {
val *= DAY_TICKS;
}
DoCommandP(0, v->index, val, CMD_SCHEDULED_DISPATCH_SET_DELAY | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE));
}
break;
}
}
this->SetDirty();
}
virtual void OnResize()
{
this->vscroll->SetCapacityFromWidget(this, WID_SCHDISPATCH_MATRIX);
NWidgetCore *nwi = this->GetWidget<NWidgetCore>(WID_SCHDISPATCH_MATRIX);
this->num_columns = nwi->current_x / nwi->resize_x;
}
virtual void OnFocus(Window *previously_focused_window)
{
if (HasFocusedVehicleChanged(this->window_number, previously_focused_window)) {
MarkAllRoutePathsDirty(this->vehicle);
MarkAllRouteStepsDirty(this->vehicle);
}
}
const Vehicle *GetVehicle()
{
return this->vehicle;
}
};
static const NWidgetPart _nested_schdispatch_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(WWT_CLOSEBOX, COLOUR_GREY),
NWidget(WWT_CAPTION, COLOUR_GREY, WID_SCHDISPATCH_CAPTION), SetDataTip(STR_SCHDISPATCH_CAPTION, STR_NULL),
NWidget(WWT_SHADEBOX, COLOUR_GREY),
NWidget(WWT_DEFSIZEBOX, COLOUR_GREY),
NWidget(WWT_STICKYBOX, COLOUR_GREY),
EndContainer(),
NWidget(NWID_HORIZONTAL),
NWidget(WWT_MATRIX, COLOUR_GREY, WID_SCHDISPATCH_MATRIX), SetResize(1, 1), SetScrollbar(WID_SCHDISPATCH_V_SCROLL),
NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_SCHDISPATCH_V_SCROLL),
EndContainer(),
NWidget(WWT_PANEL, COLOUR_GREY, WID_SCHDISPATCH_SUMMARY_PANEL), SetMinimalSize(400, 22), SetResize(1, 0), EndContainer(),
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCHDISPATCH_ENABLED), SetDataTip(STR_SCHDISPATCH_ENABLED, STR_SCHDISPATCH_ENABLED_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCHDISPATCH_ADD), SetDataTip(STR_SCHDISPATCH_ADD, STR_SCHDISPATCH_ADD_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
EndContainer(),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCHDISPATCH_SET_DURATION), SetDataTip(STR_SCHDISPATCH_DURATION, STR_SCHDISPATCH_DURATION_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCHDISPATCH_SET_START_DATE), SetDataTip(STR_SCHDISPATCH_START, STR_SCHDISPATCH_START_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
EndContainer(),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCHDISPATCH_SET_DELAY), SetDataTip(STR_SCHDISPATCH_DELAY, STR_SCHDISPATCH_DELAY_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_SCHDISPATCH_RESET_DISPATCH), SetDataTip(STR_SCHDISPATCH_RESET_LAST_DISPATCH, STR_SCHDISPATCH_RESET_LAST_DISPATCH_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
EndContainer(),
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
EndContainer(),
};
static WindowDesc _schdispatch_desc(
WDP_AUTO, "scheduled_dispatch_slots", 400, 130,
WC_SCHDISPATCH_SLOTS, WC_VEHICLE_TIMETABLE,
WDF_CONSTRUCTION,
_nested_schdispatch_widgets, lengthof(_nested_schdispatch_widgets)
);
/**
* Show the slot dispatching slots
* @param v The vehicle to show the slot dispatching slots for
*/
void ShowSchdispatchWindow(const Vehicle *v)
{
AllocateWindowDescFront<SchdispatchWindow>(&_schdispatch_desc, v->index);
}

@ -1074,6 +1074,7 @@ static bool TownFoundingChanged(int32 p1)
static bool InvalidateVehTimetableWindow(int32 p1)
{
InvalidateWindowClassesData(WC_VEHICLE_TIMETABLE, VIWD_MODIFY_ORDERS);
InvalidateWindowClassesData(WC_SCHDISPATCH_SLOTS, VIWD_MODIFY_ORDERS);
return true;
}

@ -13,6 +13,7 @@
#include "command_func.h"
#include "company_func.h"
#include "date_func.h"
#include "date_type.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "settings_type.h"
@ -652,6 +653,48 @@ void UpdateSeparationOrder(Vehicle *v_start)
}
}
static bool IsVehicleAtFirstWaitingLocation(Vehicle *v)
{
/* Check if we arrive at first station */
int first_wait_index = -1;
for (int i = 0; i < v->orders.list->GetNumOrders(); ++i) {
Order* order = v->orders.list->GetOrderAt(i);
if (order->IsWaitTimetabled() && !order->IsType(OT_IMPLICIT)) {
first_wait_index = i;
break;
}
}
return v->orders.list->IsCompleteTimetable() && (v->cur_implicit_order_index == first_wait_index);
}
static DateTicksScaled GetScheduledDispatchTime(Vehicle *v)
{
DateTicksScaled first_slot = -1;
const DateTicksScaled begin_time = v->orders.list->GetScheduledDispatchStartTick();
const int32 last_dispatched_offset = v->orders.list->GetScheduledDispatchLastDispatch();
const uint32 dispatch_duration = v->orders.list->GetScheduledDispatchDuration();
const int32 max_delay = v->orders.list->GetScheduledDispatchDelay();
/* Find next available slots */
for (auto current_offset : v->orders.list->GetScheduledDispatch()) {
while (int32(current_offset) <= last_dispatched_offset) {
current_offset += dispatch_duration;
}
DateTicksScaled current_departure = begin_time + current_offset;
while (current_departure + max_delay < _scaled_date_ticks) {
current_departure += dispatch_duration;
}
if (first_slot == -1 || first_slot > current_departure) {
first_slot = current_departure;
}
}
return first_slot;
}
/**
* Update the timetable for the vehicle.
@ -679,11 +722,26 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
bool just_started = false;
/* Start scheduled dispatch at first opportunity */
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED) && HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH)) {
if (IsVehicleAtFirstWaitingLocation(v) && travelling) {
/* Update scheduled information */
v->orders.list->UpdateScheduledDispatch();
DateTicksScaled slot = GetScheduledDispatchTime(v);
if (slot > -1) {
v->lateness_counter = _scaled_date_ticks - slot;
v->orders.list->SetScheduledDispatchLastDispatch(slot - v->orders.list->GetScheduledDispatchStartTick());
}
}
}
/* Start automated timetables at first opportunity */
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED) && HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) {
v->ClearSeparation();
SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
v->lateness_counter = 0;
/* If the lateness is set by scheduled dispatch above, do not reset */
if(!HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH)) v->lateness_counter = 0;
if (HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION)) UpdateSeparationOrder(v);
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
@ -813,6 +871,21 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
UpdateSeparationOrder(v);
v->current_order_time = 0;
v->current_loading_time = 0;
} else if (HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH) && HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
if (IsVehicleAtFirstWaitingLocation(v) && travelling) {
/* Update scheduled information */
v->orders.list->UpdateScheduledDispatch();
DateTicksScaled slot = GetScheduledDispatchTime(v);
if (slot > -1) {
v->lateness_counter = _scaled_date_ticks - slot;
v->orders.list->SetScheduledDispatchLastDispatch(slot - v->orders.list->GetScheduledDispatchStartTick());
} else {
v->lateness_counter -= (timetabled - time_taken);
}
} else {
v->lateness_counter -= (timetabled - time_taken);
}
} else {
v->lateness_counter -= (timetabled - time_taken);
}

@ -25,6 +25,7 @@
#include "vehicle_gui.h"
#include "settings_type.h"
#include "viewport_func.h"
#include "schdispatch.h"
#include "widgets/timetable_widget.h"
@ -356,11 +357,11 @@ struct TimetableWindow : Window {
this->SetWidgetDisabledState(WID_VT_CLEAR_SPEED, disable_speed);
this->SetWidgetDisabledState(WID_VT_SHARED_ORDER_LIST, !v->IsOrderListShared());
this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders.list == NULL || HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION));
this->SetWidgetDisabledState(WID_VT_START_DATE, v->orders.list == NULL || HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION) || HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH));
this->SetWidgetDisabledState(WID_VT_RESET_LATENESS, v->orders.list == NULL);
this->SetWidgetDisabledState(WID_VT_AUTOFILL, v->orders.list == NULL || HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE));
this->SetWidgetDisabledState(WID_VT_AUTO_SEPARATION, HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH));
this->EnableWidget(WID_VT_AUTOMATE);
this->EnableWidget(WID_VT_AUTO_SEPARATION);
} else {
this->DisableWidget(WID_VT_START_DATE);
this->DisableWidget(WID_VT_CHANGE_TIME);
@ -377,6 +378,9 @@ struct TimetableWindow : Window {
this->SetWidgetLoweredState(WID_VT_AUTOFILL, HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE));
this->SetWidgetLoweredState(WID_VT_AUTOMATE, HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE));
this->SetWidgetLoweredState(WID_VT_AUTO_SEPARATION, HasBit(v->vehicle_flags, VF_TIMETABLE_SEPARATION));
this->SetWidgetLoweredState(WID_VT_SCHEDULED_DISPATCH, HasBit(v->vehicle_flags, VF_SCHEDULED_DISPATCH));
this->SetWidgetDisabledState(WID_VT_SCHEDULED_DISPATCH, v->orders.list == NULL);
this->DrawWidgets();
}
@ -660,6 +664,11 @@ struct TimetableWindow : Window {
break;
}
case WID_VT_SCHEDULED_DISPATCH: {
ShowSchdispatchWindow(v);
break;
}
case WID_VT_AUTOMATE: {
uint32 p2 = 0;
if (!HasBit(v->vehicle_flags, VF_AUTOMATE_TIMETABLE)) SetBit(p2, 0);
@ -787,20 +796,21 @@ static const NWidgetPart _nested_timetable_widgets[] = {
NWidget(NWID_HORIZONTAL),
NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_START_DATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_STARTING_DATE, STR_TIMETABLE_STARTING_DATE_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CHANGE_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_TIME, STR_TIMETABLE_WAIT_TIME_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CLEAR_TIME), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_TIME, STR_TIMETABLE_CLEAR_TIME_TOOLTIP),
EndContainer(),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CHANGE_SPEED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CHANGE_SPEED, STR_TIMETABLE_CHANGE_SPEED_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_CLEAR_SPEED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_CLEAR_SPEED, STR_TIMETABLE_CLEAR_SPEED_TOOLTIP),
EndContainer(),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_START_DATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_STARTING_DATE, STR_TIMETABLE_STARTING_DATE_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_SCHEDULED_DISPATCH), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_SCHEDULED_DISPATCH, STR_TIMETABLE_SCHEDULED_DISPATCH_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTO_SEPARATION), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTO_SEPARATION, STR_TIMETABLE_AUTO_SEPARATION_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_RESET_LATENESS), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_RESET_LATENESS, STR_TIMETABLE_RESET_LATENESS_TOOLTIP),
EndContainer(),
NWidget(NWID_VERTICAL, NC_EQUALSIZE),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOFILL), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOFILL, STR_TIMETABLE_AUTOFILL_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_AUTOMATE), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_TIMETABLE_AUTOMATE, STR_TIMETABLE_AUTOMATE_TOOLTIP),
NWidget(NWID_SELECTION, INVALID_COLOUR, WID_VT_EXPECTED_SELECTION),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_VT_EXPECTED), SetResize(1, 0), SetFill(1, 1), SetDataTip(STR_BLACK_STRING, STR_TIMETABLE_EXPECTED_TOOLTIP),

@ -931,6 +931,7 @@ void Vehicle::PreDestructor()
DeleteWindowById(WC_VEHICLE_REFIT, this->index);
DeleteWindowById(WC_VEHICLE_DETAILS, this->index);
DeleteWindowById(WC_VEHICLE_TIMETABLE, this->index);
DeleteWindowById(WC_SCHDISPATCH_SLOTS, this->index);
DeleteWindowById(WC_VEHICLE_CARGO_TYPE_LOAD_ORDERS, this->index);
DeleteWindowById(WC_VEHICLE_CARGO_TYPE_UNLOAD_ORDERS, this->index);
SetWindowDirty(WC_COMPANY, this->owner);

@ -58,6 +58,7 @@ enum VehicleFlags {
// Additional flags not in trunk are added at the end to avoid clashing with any new
// flags which get added in future trunk, and to avoid re-ordering flags which are in trunk already,
// as this breaks savegame compatibility.
VF_SCHEDULED_DISPATCH = 12, ///< Whether the vehicle should follow a timetabled dispatching schedule
VF_LAST_LOAD_ST_SEP = 13, ///< Each vehicle of this chain has its last_loading_station field set separately
VF_TIMETABLE_SEPARATION = 14,///< Whether the vehicle should manage the timetable automatically.
VF_AUTOMATE_TIMETABLE = 15, ///< Whether the vehicle should manage the timetable automatically.

@ -34,6 +34,7 @@ enum VehicleTimetableWidgets {
WID_VT_EXPECTED_SELECTION, ///< Disable/hide the expected selection button.
WID_VT_CHANGE_SPEED, ///< Change speed limit button.
WID_VT_CLEAR_SPEED, ///< Clear speed limit button.
WID_VT_SCHEDULED_DISPATCH, ///< Scheduled Dispatch button.
};
#endif /* WIDGETS_TIMETABLE_WIDGET_H */

@ -728,6 +728,11 @@ enum WindowClass {
*/
WC_DEPARTURES_BOARD,
/**
* Vehicle scheduled dispatch - departure slots
*/
WC_SCHDISPATCH_SLOTS,
/**
* Plans window.
*/

Loading…
Cancel
Save