From 34e41a2e26a154a728b37785c770a9db1d73423d Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 9 Aug 2015 15:12:24 +0100 Subject: [PATCH 1/2] Timetable GUI: Allow clearing of timetable time fields which are at 0. Allow explicitly setting timetable time fields to 0 without clearing them. --- src/timetable_cmd.cpp | 13 +++++++++---- src/timetable_gui.cpp | 6 +++--- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index 1e2165cb85..7afc20616e 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -95,6 +95,7 @@ static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, * - p1 = (bit 0-19) - Vehicle with the orders to change. * - p1 = (bit 20-27) - Order index to modify. * - 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. @@ -118,16 +119,20 @@ CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, u ModifyTimetableFlags mtf = Extract(p1); if (mtf >= MTF_END) return CMD_ERROR; + bool clear_field = GB(p1, 30, 1) == 1; + int wait_time = order->GetWaitTime(); int travel_time = order->GetTravelTime(); int max_speed = order->GetMaxSpeed(); switch (mtf) { case MTF_WAIT_TIME: wait_time = GB(p2, 0, 16); + if (clear_field) assert(wait_time == 0); break; case MTF_TRAVEL_TIME: travel_time = GB(p2, 0, 16); + if (clear_field) assert(travel_time == 0); break; case MTF_TRAVEL_SPEED: @@ -159,15 +164,15 @@ CommandCost CmdChangeTimetable(TileIndex tile, DoCommandFlag flags, uint32 p1, u switch (mtf) { case MTF_WAIT_TIME: /* Set time if changing the value or confirming an estimated time as timetabled. */ - if (wait_time != order->GetWaitTime() || (wait_time > 0 && !order->IsWaitTimetabled())) { - ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, wait_time > 0); + if (wait_time != order->GetWaitTime() || (clear_field == order->IsWaitTimetabled())) { + ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME, !clear_field); } break; case MTF_TRAVEL_TIME: /* Set time if changing the value or confirming an estimated time as timetabled. */ - if (travel_time != order->GetTravelTime() || (travel_time > 0 && !order->IsTravelTimetabled())) { - ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME, travel_time > 0); + if (travel_time != order->GetTravelTime() || (clear_field == order->IsTravelTimetabled())) { + ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME, !clear_field); } break; diff --git a/src/timetable_gui.cpp b/src/timetable_gui.cpp index 8ef81ee719..738ae3c2e6 100644 --- a/src/timetable_gui.cpp +++ b/src/timetable_gui.cpp @@ -510,14 +510,14 @@ struct TimetableWindow : Window { } } - static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected, bool speed) + static inline uint32 PackTimetableArgs(const Vehicle *v, uint selected, bool speed, bool clear = false) { uint order_number = (selected + 1) / 2; ModifyTimetableFlags mtf = (selected % 2 == 1) ? (speed ? MTF_TRAVEL_SPEED : MTF_TRAVEL_TIME) : MTF_WAIT_TIME; if (order_number >= v->GetNumOrders()) order_number = 0; - return v->index | (order_number << 20) | (mtf << 28); + return v->index | (order_number << 20) | (mtf << 28) | (clear ? 1 << 30 : 0); } virtual void OnClick(Point pt, int widget, int click_count) @@ -586,7 +586,7 @@ struct TimetableWindow : Window { } case WID_VT_CLEAR_TIME: { // Clear waiting time. - uint32 p1 = PackTimetableArgs(v, this->sel_index, false); + uint32 p1 = PackTimetableArgs(v, this->sel_index, false, true); DoCommandP(0, p1, 0, CMD_CHANGE_TIMETABLE | CMD_MSG(STR_ERROR_CAN_T_TIMETABLE_VEHICLE)); break; } From d24f7763cc7b034362a28af4cb33de3e9ebf59b2 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 9 Aug 2015 15:54:08 +0100 Subject: [PATCH 2/2] Fix auto timetable separation to handle non-station orders sensibly. --- src/timetable_cmd.cpp | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp index 7afc20616e..c26b4e73b2 100644 --- a/src/timetable_cmd.cpp +++ b/src/timetable_cmd.cpp @@ -441,6 +441,27 @@ CommandCost CmdAutomateTimetable(TileIndex index, DoCommandFlag flags, uint32 p1 return CommandCost(); } +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; +} + int TimeToFinishOrder(Vehicle *v, int n) { int left; @@ -448,8 +469,8 @@ int TimeToFinishOrder(Vehicle *v, int n) int wait_time = order->GetWaitTime(); int travel_time = order->GetTravelTime(); assert(order != NULL); + if (!IsOrderUsableForSeparation(order)) return -1; if ((v->cur_real_order_index == n) && (v->last_station_visited == order->GetDestination())) { - if (wait_time == 0) return -1; if (v->current_loading_time > 0) { left = wait_time - v->current_order_time; } else { @@ -459,7 +480,6 @@ int TimeToFinishOrder(Vehicle *v, int n) } else { left = travel_time; if (v->cur_real_order_index == n) left -= v->current_order_time; - if (travel_time == 0 || wait_time == 0) return -1; if (left < 0) left = 0; left +=wait_time; } @@ -486,10 +506,8 @@ int SeparationBetween(Vehicle *v1, Vehicle *v2) if (time < 0) { for (n = 0; n < v1->GetNumOrders(); n++) { Order *order = v1->GetOrder(n); - int wait_time = order->GetWaitTime(); - int travel_time = order->GetTravelTime(); - if (travel_time == 0 || wait_time == 0) return -1; - time += travel_time + wait_time; + if (!IsOrderUsableForSeparation(order)) return -1; + time += order->GetTravelTime() + order->GetWaitTime(); } } separation += time;