Files
@ r19931:c8510dc6a120
Branch filter:
Location: cpp/openttd-patchpack/source/src/timetable_cmd.cpp
r19931:c8510dc6a120
12.3 KiB
text/x-c
(svn r24887) -Update from WebTranslator v3.0:
simplified_chinese - 1 changes by chenwt0315
danish - 13 changes by Elias
esperanto - 25 changes by ernie13
greek - 1 changes by Rubidium
hebrew - 19 changes by naf869
icelandic - 109 changes by Stimrol
indonesian - 26 changes by H2
korean - 6 changes by telk5093
luxembourgish - 25 changes by Phreeze
polish - 2 changes by wojteks86
serbian - 5 changes by voodoo84
swedish - 16 changes by spacejens
turkish - 17 changes by kutzun
simplified_chinese - 1 changes by chenwt0315
danish - 13 changes by Elias
esperanto - 25 changes by ernie13
greek - 1 changes by Rubidium
hebrew - 19 changes by naf869
icelandic - 109 changes by Stimrol
indonesian - 26 changes by H2
korean - 6 changes by telk5093
luxembourgish - 25 changes by Phreeze
polish - 2 changes by wojteks86
serbian - 5 changes by voodoo84
swedish - 16 changes by spacejens
turkish - 17 changes by kutzun
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | /* $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 timetable_cmd.cpp Commands related to time tabling. */
#include "stdafx.h"
#include "command_func.h"
#include "company_func.h"
#include "date_func.h"
#include "window_func.h"
#include "vehicle_base.h"
#include "cmd_helper.h"
#include "table/strings.h"
/**
* 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.
* @param val The new data of the timetable entry.
* @param mtf Which part of the timetable entry to change.
*/
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 val, ModifyTimetableFlags mtf)
{
Order *order = v->GetOrder(order_number);
int delta = 0;
switch (mtf) {
case MTF_WAIT_TIME:
delta = val - order->wait_time;
order->wait_time = val;
break;
case MTF_TRAVEL_TIME:
delta = val - order->travel_time;
order->travel_time = val;
break;
case MTF_TRAVEL_SPEED:
order->max_speed = val;
break;
default:
NOT_REACHED();
}
v->orders.list->UpdateOrderTimetable(delta);
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
if (v->cur_real_order_index == order_number && v->current_order.Equals(*order)) {
switch (mtf) {
case MTF_WAIT_TIME:
v->current_order.wait_time = val;
break;
case MTF_TRAVEL_TIME:
v->current_order.travel_time = val;
break;
case MTF_TRAVEL_SPEED:
v->current_order.max_speed = val;
break;
default:
NOT_REACHED();
}
}
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
}
}
/**
* Change timetable data of an order.
* @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) - Order index to modify.
* - p1 = (bit 28-29) - Timetable data to change (@see ModifyTimetableFlags)
* @param p2 The amount of time to wait.
* - p2 = (bit 0-15) - The data to modify as specified by p1 bits 28-29.
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdChangeTimetable(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;
VehicleOrderID order_number = GB(p1, 20, 8);
Order *order = v->GetOrder(order_number);
if (order == NULL || order->IsType(OT_IMPLICIT)) return CMD_ERROR;
ModifyTimetableFlags mtf = Extract<ModifyTimetableFlags, 28, 2>(p1);
if (mtf >= MTF_END) return CMD_ERROR;
int wait_time = order->wait_time;
int travel_time = order->travel_time;
int max_speed = order->max_speed;
switch (mtf) {
case MTF_WAIT_TIME:
wait_time = GB(p2, 0, 16);
break;
case MTF_TRAVEL_TIME:
travel_time = GB(p2, 0, 16);
break;
case MTF_TRAVEL_SPEED:
max_speed = GB(p2, 0, 16);
if (max_speed == 0) max_speed = UINT16_MAX; // Disable speed limit.
break;
default:
NOT_REACHED();
}
if (wait_time != order->wait_time) {
switch (order->GetType()) {
case OT_GOTO_STATION:
if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_ERROR_TIMETABLE_NOT_STOPPING_HERE);
break;
case OT_CONDITIONAL:
break;
default: return_cmd_error(STR_ERROR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
}
}
if (travel_time != order->travel_time && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
if (max_speed != order->max_speed && (order->IsType(OT_CONDITIONAL) || v->type == VEH_AIRCRAFT)) return CMD_ERROR;
if (flags & DC_EXEC) {
if (wait_time != order->wait_time) ChangeTimetable(v, order_number, wait_time, MTF_WAIT_TIME);
if (travel_time != order->travel_time) ChangeTimetable(v, order_number, travel_time, MTF_TRAVEL_TIME);
if (max_speed != order->max_speed) ChangeTimetable(v, order_number, max_speed, MTF_TRAVEL_SPEED);
}
return CommandCost();
}
/**
* Clear the lateness counter to make the vehicle on time.
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Various bitstuffed elements
* - p1 = (bit 0-19) - Vehicle with the orders to change.
* @param p2 unused
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdSetVehicleOnTime(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) {
v->lateness_counter = 0;
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
}
return CommandCost();
}
/**
* Set the start date of the timetable.
* @param tile Not used.
* @param flags Operation to perform.
* @param p1 Vehicle id.
* @param p2 The timetable start date.
* @param text Not used.
* @return The error or cost of the operation.
*/
CommandCost CmdSetTimetableStart(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
Vehicle *v = Vehicle::GetIfValid(GB(p1, 0, 20));
if (v == NULL || !v->IsPrimaryVehicle()) return CMD_ERROR;
CommandCost ret = CheckOwnership(v->owner);
if (ret.Failed()) return ret;
/* Don't let a timetable start more than 15 years into the future or 1 year in the past. */
Date start_date = (Date)p2;
if (start_date < 0 || start_date > MAX_DAY) return CMD_ERROR;
if (start_date - _date > 15 * DAYS_IN_LEAP_YEAR) return CMD_ERROR;
if (_date - start_date > DAYS_IN_LEAP_YEAR) return CMD_ERROR;
if (flags & DC_EXEC) {
v->lateness_counter = 0;
ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
v->timetable_start = start_date;
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
}
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 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
* @param text unused
* @return the cost of this operation or an error
*/
CommandCost CmdAutofillTimetable(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 (HasBit(p2, 0)) {
/* 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. */
SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
/* Overwrite waiting times only if they got longer */
if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
v->timetable_start = 0;
v->lateness_counter = 0;
} else {
ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
}
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);
}
}
return CommandCost();
}
/**
* 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.
*/
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
{
uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time;
uint time_taken = v->current_order_time;
v->current_order_time = 0;
if (v->current_order.IsType(OT_IMPLICIT)) return; // no timetabling of auto orders
VehicleOrderID first_manual_order = 0;
for (Order *o = v->GetFirstOrder(); o != NULL && o->IsType(OT_IMPLICIT); o = o->next) {
++first_manual_order;
}
bool just_started = false;
/* This vehicle is arriving at the first destination in the timetable. */
if (v->cur_real_order_index == first_manual_order && travelling) {
/* 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) {
v->lateness_counter = (_date - v->timetable_start) * DAY_TICKS + _date_fract;
v->timetable_start = 0;
}
SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
}
if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
if (travelling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) {
/* Need to clear that now as otherwise we are not able to reduce the wait time */
v->current_order.wait_time = 0;
}
if (just_started) return;
/* Modify station waiting time only if our new value is larger (this is
* always the case when we cleared the timetable). */
if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.wait_time)) {
/* 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.
* 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. */
time_taken = CeilDiv(max(time_taken, 1U), DAY_TICKS) * DAY_TICKS;
ChangeTimetable(v, v->cur_real_order_index, time_taken, travelling ? MTF_TRAVEL_TIME : MTF_WAIT_TIME);
}
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);
}
return;
}
if (just_started) return;
/* 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 (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
v->lateness_counter -= (timetabled - time_taken);
/* 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;
}
}
for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
SetWindowDirty(WC_VEHICLE_TIMETABLE, v->index);
}
}
|