summaryrefslogtreecommitdiff
path: root/src/timetable_cmd.cpp
diff options
context:
space:
mode:
authormaedhros <maedhros@openttd.org>2007-06-25 20:55:43 +0000
committermaedhros <maedhros@openttd.org>2007-06-25 20:55:43 +0000
commit4af9ca5335903e72dfbc13fb00be4d747302560b (patch)
tree4840bc321f282360404faa91242a640971fe700a /src/timetable_cmd.cpp
parent2fce0506a6e898afe1045c59e4772a04011e29ab (diff)
downloadopenttd-4af9ca5335903e72dfbc13fb00be4d747302560b.tar.xz
(svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
the times from the first (or subsequent) run-throughs.
Diffstat (limited to 'src/timetable_cmd.cpp')
-rw-r--r--src/timetable_cmd.cpp108
1 files changed, 92 insertions, 16 deletions
diff --git a/src/timetable_cmd.cpp b/src/timetable_cmd.cpp
index ee333cb78..101221216 100644
--- a/src/timetable_cmd.cpp
+++ b/src/timetable_cmd.cpp
@@ -8,9 +8,32 @@
#include "variables.h"
#include "table/strings.h"
#include "command.h"
+#include "date.h"
+#include "player.h"
#include "vehicle.h"
+static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey)
+{
+ Order *order = GetVehicleOrder(v, order_number);
+
+ if (is_journey) {
+ order->travel_time = time;
+ } else {
+ order->wait_time = time;
+ }
+
+ if (v->cur_order_index == order_number && HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
+ if (is_journey) {
+ v->current_order.travel_time = time;
+ } else {
+ v->current_order.wait_time = time;
+ }
+ }
+
+ InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
+}
+
/**
* Add or remove waiting times from an order.
* @param tile Not used.
@@ -43,21 +66,7 @@ CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p
}
if (flags & DC_EXEC) {
- if (is_journey) {
- order->travel_time = p2;
- } else {
- order->wait_time = p2;
- }
-
- if (v->cur_order_index == order_number && HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
- if (is_journey) {
- v->current_order.travel_time = p2;
- } else {
- v->current_order.wait_time = p2;
- }
- }
-
- InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
+ ChangeTimetable(v, order_number, p2, is_journey);
}
return CommandCost();
@@ -87,6 +96,46 @@ CommandCost CmdSetVehicleOnTime(TileIndex tile, uint32 flags, uint32 p1, uint32
return CommandCost();
}
+/**
+ * Start or stop filling the timetable automatically from the time the vehicle
+ * actually takes to complete it. When starting to autofill the current times
+ * are cleared and the timetable will start again from scratch.
+ * @param tile Not used.
+ * @param flags Operation to perform.
+ * @param p1 Vehicle index.
+ * @param p2 Set to 1 to enable, 0 to disable.
+ */
+CommandCost CmdAutofillTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
+{
+ if (!_patches.timetabling) return CMD_ERROR;
+
+ VehicleID veh = GB(p1, 0, 16);
+ if (!IsValidVehicleID(veh)) return CMD_ERROR;
+
+ Vehicle *v = GetVehicle(veh);
+ if (!CheckOwnership(v->owner)) return CMD_ERROR;
+
+ if (flags & DC_EXEC) {
+ if (p2 == 1) {
+ /* Start autofilling the timetable, which clears all the current
+ * timings and clears the "timetable has started" bit. */
+ SETBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
+ CLRBIT(v->vehicle_flags, VF_TIMETABLE_STARTED);
+
+ for (Order *order = GetVehicleOrder(v, 0); order != NULL; order = order->next) {
+ order->wait_time = 0;
+ order->travel_time = 0;
+ }
+
+ v->current_order.wait_time = 0;
+ v->current_order.travel_time = 0;
+ } else {
+ CLRBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
+ }
+ }
+
+ return CommandCost();
+}
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
{
@@ -95,10 +144,37 @@ void UpdateVehicleTimetable(Vehicle *v, bool travelling)
v->current_order_time = 0;
+ if (!_patches.timetabling) return;
+
+ /* Make sure the timetable only starts when the vehicle reaches the first
+ * order, not when travelling from the depot to the first station. */
+ if (v->cur_order_index == 0 && !HASBIT(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
+ SETBIT(v->vehicle_flags, VF_TIMETABLE_STARTED);
+ return;
+ }
+
+ if (!HASBIT(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
+
+ if (HASBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
+ if (timetabled == 0) {
+ /* Round the time taken up to the nearest day, as this will avoid
+ * confusion for people who are timetabling in days, and can be
+ * adjusted later by people who aren't. */
+ time_taken = (((time_taken - 1) / DAY_TICKS) + 1) * DAY_TICKS;
+
+ ChangeTimetable(v, v->cur_order_index, time_taken, travelling);
+ return;
+ } else if (v->cur_order_index == 0) {
+ /* Otherwise if we're at the beginning and it already has a value,
+ * assume that autofill is finished and turn it off again. */
+ CLRBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
+ }
+ }
+
/* 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. */
- if (!_patches.timetabling || (timetabled == 0 && (travelling || v->lateness_counter >= 0))) return;
+ if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
v->lateness_counter -= (timetabled - time_taken);