author | truebrain |
Thu, 12 Jun 2008 21:07:25 +0000 | |
branch | noai |
changeset 10943 | 5f5a5dd407d8 |
parent 10776 | 07203fc29812 |
permissions | -rw-r--r-- |
9629 | 1 |
/* $Id$ */ |
2 |
||
10455
22c441f5adf9
(svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents:
10249
diff
changeset
|
3 |
/** @file timetable_cmd.cpp Commands related to time tabling. */ |
9629 | 4 |
|
5 |
#include "stdafx.h" |
|
6 |
#include "openttd.h" |
|
7 |
#include "variables.h" |
|
9723
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
8 |
#include "command_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
9 |
#include "functions.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
10 |
#include "window_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
11 |
#include "vehicle_func.h" |
eee46cb39750
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents:
9722
diff
changeset
|
12 |
#include "vehicle_base.h" |
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
13 |
#include "settings_type.h" |
9629 | 14 |
|
9724
b39bc69bb2f2
(svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents:
9723
diff
changeset
|
15 |
#include "table/strings.h" |
9629 | 16 |
|
17 |
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey) |
|
18 |
{ |
|
19 |
Order *order = GetVehicleOrder(v, order_number); |
|
20 |
||
21 |
if (is_journey) { |
|
22 |
order->travel_time = time; |
|
23 |
} else { |
|
24 |
order->wait_time = time; |
|
25 |
} |
|
26 |
||
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
27 |
if (v->cur_order_index == order_number && v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) { |
9629 | 28 |
if (is_journey) { |
29 |
v->current_order.travel_time = time; |
|
30 |
} else { |
|
31 |
v->current_order.wait_time = time; |
|
32 |
} |
|
33 |
} |
|
34 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
35 |
for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
36 |
InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
37 |
} |
9629 | 38 |
} |
39 |
||
40 |
/** |
|
41 |
* Add or remove waiting times from an order. |
|
42 |
* @param tile Not used. |
|
43 |
* @param flags Operation to perform. |
|
44 |
* @param p1 Various bitstuffed elements |
|
45 |
* - p1 = (bit 0-15) - Vehicle with the orders to change. |
|
46 |
* - p1 = (bit 16-23) - Order index to modify. |
|
47 |
* - p1 = (bit 24) - Whether to change the waiting time or the travelling |
|
48 |
* time. |
|
9800 | 49 |
* - p1 = (bit 25) - Whether p2 contains waiting and travelling time. |
9629 | 50 |
* @param p2 The amount of time to wait. |
9800 | 51 |
* - p2 = (bit 0-15) - Waiting or travelling time as specified by p1 bit 24 if p1 bit 25 is not set, |
52 |
* Travelling time if p1 bit 25 is set. |
|
53 |
* - p2 = (bit 16-31) - Waiting time if p1 bit 25 is set |
|
9629 | 54 |
*/ |
55 |
CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
56 |
{ |
|
10776 | 57 |
if (!_settings_game.order.timetabling) return CMD_ERROR; |
9629 | 58 |
|
59 |
VehicleID veh = GB(p1, 0, 16); |
|
60 |
if (!IsValidVehicleID(veh)) return CMD_ERROR; |
|
61 |
||
62 |
Vehicle *v = GetVehicle(veh); |
|
63 |
if (!CheckOwnership(v->owner)) return CMD_ERROR; |
|
64 |
||
65 |
VehicleOrderID order_number = GB(p1, 16, 8); |
|
66 |
Order *order = GetVehicleOrder(v, order_number); |
|
67 |
if (order == NULL) return CMD_ERROR; |
|
68 |
||
9800 | 69 |
bool packed_time = HasBit(p1, 25); |
70 |
bool is_journey = HasBit(p1, 24) || packed_time; |
|
9629 | 71 |
if (!is_journey) { |
9869
6404afe43575
(svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents:
9800
diff
changeset
|
72 |
if (!order->IsType(OT_GOTO_STATION)) return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS); |
10142
56ee7da4ad56
(svn r12673) [NoAI] -Sync: with trunk r12596:12672. Note that due to the order rewrite AIOrder.ChangeOrder does currently not work as expected.
rubidium
parents:
9869
diff
changeset
|
73 |
if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE); |
9629 | 74 |
} |
75 |
||
76 |
if (flags & DC_EXEC) { |
|
9800 | 77 |
ChangeTimetable(v, order_number, GB(p2, 0, 16), is_journey); |
78 |
if (packed_time) ChangeTimetable(v, order_number, GB(p2, 16, 16), false); |
|
9629 | 79 |
} |
80 |
||
81 |
return CommandCost(); |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Clear the lateness counter to make the vehicle on time. |
|
86 |
* @param tile Not used. |
|
87 |
* @param flags Operation to perform. |
|
88 |
* @param p1 Various bitstuffed elements |
|
89 |
* - p1 = (bit 0-15) - Vehicle with the orders to change. |
|
90 |
*/ |
|
91 |
CommandCost CmdSetVehicleOnTime(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
92 |
{ |
|
10776 | 93 |
if (!_settings_game.order.timetabling) return CMD_ERROR; |
9629 | 94 |
|
95 |
VehicleID veh = GB(p1, 0, 16); |
|
96 |
if (!IsValidVehicleID(veh)) return CMD_ERROR; |
|
97 |
||
98 |
Vehicle *v = GetVehicle(veh); |
|
99 |
if (!CheckOwnership(v->owner)) return CMD_ERROR; |
|
100 |
||
101 |
if (flags & DC_EXEC) { |
|
102 |
v->lateness_counter = 0; |
|
103 |
} |
|
104 |
||
105 |
return CommandCost(); |
|
106 |
} |
|
107 |
||
108 |
/** |
|
109 |
* Start or stop filling the timetable automatically from the time the vehicle |
|
110 |
* actually takes to complete it. When starting to autofill the current times |
|
111 |
* are cleared and the timetable will start again from scratch. |
|
112 |
* @param tile Not used. |
|
113 |
* @param flags Operation to perform. |
|
114 |
* @param p1 Vehicle index. |
|
115 |
* @param p2 Set to 1 to enable, 0 to disable. |
|
116 |
*/ |
|
117 |
CommandCost CmdAutofillTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
|
118 |
{ |
|
10776 | 119 |
if (!_settings_game.order.timetabling) return CMD_ERROR; |
9629 | 120 |
|
121 |
VehicleID veh = GB(p1, 0, 16); |
|
122 |
if (!IsValidVehicleID(veh)) return CMD_ERROR; |
|
123 |
||
124 |
Vehicle *v = GetVehicle(veh); |
|
125 |
if (!CheckOwnership(v->owner)) return CMD_ERROR; |
|
126 |
||
127 |
if (flags & DC_EXEC) { |
|
128 |
if (p2 == 1) { |
|
129 |
/* Start autofilling the timetable, which clears all the current |
|
130 |
* timings and clears the "timetable has started" bit. */ |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
131 |
SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
132 |
ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED); |
9629 | 133 |
|
134 |
for (Order *order = GetVehicleOrder(v, 0); order != NULL; order = order->next) { |
|
135 |
order->wait_time = 0; |
|
136 |
order->travel_time = 0; |
|
137 |
} |
|
138 |
||
139 |
v->current_order.wait_time = 0; |
|
140 |
v->current_order.travel_time = 0; |
|
141 |
} else { |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
142 |
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); |
9629 | 143 |
} |
144 |
} |
|
145 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
146 |
for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
147 |
InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
148 |
} |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
149 |
|
9629 | 150 |
return CommandCost(); |
151 |
} |
|
152 |
||
153 |
void UpdateVehicleTimetable(Vehicle *v, bool travelling) |
|
154 |
{ |
|
155 |
uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time; |
|
156 |
uint time_taken = v->current_order_time; |
|
157 |
||
158 |
v->current_order_time = 0; |
|
159 |
||
10776 | 160 |
if (!_settings_game.order.timetabling) return; |
9629 | 161 |
|
162 |
/* Make sure the timetable only starts when the vehicle reaches the first |
|
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
163 |
* order, not when travelling from the depot to the first station. */ |
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
164 |
if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) { |
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
165 |
SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED); |
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
166 |
return; |
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
167 |
} |
9629 | 168 |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
169 |
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return; |
9629 | 170 |
|
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
171 |
if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) { |
9629 | 172 |
if (timetabled == 0) { |
173 |
/* Round the time taken up to the nearest day, as this will avoid |
|
174 |
* confusion for people who are timetabling in days, and can be |
|
175 |
* adjusted later by people who aren't. */ |
|
176 |
time_taken = (((time_taken - 1) / DAY_TICKS) + 1) * DAY_TICKS; |
|
177 |
||
178 |
ChangeTimetable(v, v->cur_order_index, time_taken, travelling); |
|
179 |
return; |
|
180 |
} else if (v->cur_order_index == 0) { |
|
181 |
/* Otherwise if we're at the beginning and it already has a value, |
|
182 |
* assume that autofill is finished and turn it off again. */ |
|
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
183 |
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); |
9629 | 184 |
} |
10249
58810805030e
(svn r12781) [NoAI] -Sync: with trunk r12711:12780.
rubidium
parents:
10142
diff
changeset
|
185 |
} |
9629 | 186 |
|
187 |
/* Vehicles will wait at stations if they arrive early even if they are not |
|
188 |
* timetabled to wait there, so make sure the lateness counter is updated |
|
189 |
* when this happens. */ |
|
190 |
if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return; |
|
191 |
||
192 |
v->lateness_counter -= (timetabled - time_taken); |
|
193 |
||
9722
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
194 |
for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
195 |
InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); |
ebf0ece7d8f6
(svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents:
9629
diff
changeset
|
196 |
} |
9629 | 197 |
} |