src/timetable_cmd.cpp
author translators
Thu, 18 Dec 2008 18:47:39 +0000
changeset 10437 6d64230b9fb9
parent 10341 840daea3d9a2
permissions -rw-r--r--
(svn r14691) -Update: WebTranslator2 update to 2008-12-18 18:47:25
arabic_egypt - 132 fixed, 1 changed by khaloofah (133)
czech - 10 fixed, 25 changed by Hadez (35)
esperanto - 15 fixed by Athaba (15)
greek - 7 fixed by ouranogrammi (7)
indonesian - 88 changed by fanioz (88)
latvian - 117 fixed, 16 changed by peerer (83), marismols (50)
lithuanian - 15 fixed by Zogg (15)
malay - 38 fixed by tombakemas (5), Syed (33)
serbian - 39 fixed, 1 changed by Jenraux (40)
thai - 105 fixed by vetbook (105)
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     1
/* $Id$ */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8970
diff changeset
     3
/** @file timetable_cmd.cpp Commands related to time tabling. */
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     4
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     5
#include "stdafx.h"
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     6
#include "openttd.h"
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     7
#include "variables.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 7931
diff changeset
     8
#include "command_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8116
diff changeset
     9
#include "functions.h"
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8116
diff changeset
    10
#include "window_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    11
#include "vehicle_func.h"
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    12
#include "vehicle_base.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8264
diff changeset
    13
#include "settings_type.h"
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    14
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8254
diff changeset
    15
#include "table/strings.h"
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    16
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    17
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey)
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    18
{
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    19
	Order *order = GetVehicleOrder(v, order_number);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    20
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    21
	if (is_journey) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    22
		order->travel_time = time;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    23
	} else {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    24
		order->wait_time = time;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    25
	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    26
10033
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    27
	for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    28
		if (v->cur_order_index == order_number && v->current_order.Equals(*order)) {
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    29
			if (is_journey) {
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    30
				v->current_order.travel_time = time;
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    31
			} else {
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    32
				v->current_order.wait_time = time;
0336744fb8a6 (svn r14192) -Fix [FS#2236]: properly update the current timetable's travel/wait times instead of only doing it for one vehicle in the shared order chain and only when some bit has not been set (PhilSophus)
rubidium
parents: 9941
diff changeset
    33
			}
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    34
		}
7809
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
    35
		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
    36
	}
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    37
}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    38
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    39
/**
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    40
 * Add or remove waiting times from an order.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    41
 * @param tile Not used.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    42
 * @param flags Operation to perform.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    43
 * @param p1 Various bitstuffed elements
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    44
 * - p1 = (bit  0-15) - Vehicle with the orders to change.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    45
 * - p1 = (bit 16-23) - Order index to modify.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    46
 * - p1 = (bit    24) - Whether to change the waiting time or the travelling
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    47
 *                      time.
8650
9a9c905deaa5 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 8302
diff changeset
    48
 * - p1 = (bit    25) - Whether p2 contains waiting and travelling time.
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    49
 * @param p2 The amount of time to wait.
8650
9a9c905deaa5 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 8302
diff changeset
    50
 * - p2 = (bit  0-15) - Waiting or travelling time as specified by p1 bit 24 if p1 bit 25 is not set,
9a9c905deaa5 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 8302
diff changeset
    51
 *                      Travelling time if p1 bit 25 is set.
9a9c905deaa5 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 8302
diff changeset
    52
 * - p2 = (bit 16-31) - Waiting time if p1 bit 25 is set
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    53
 */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    54
CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    55
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
    56
	if (!_settings_game.order.timetabling) return CMD_ERROR;
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    57
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    58
	VehicleID veh = GB(p1, 0, 16);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    59
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    60
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    61
	Vehicle *v = GetVehicle(veh);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    62
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    63
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    64
	VehicleOrderID order_number = GB(p1, 16, 8);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    65
	Order *order = GetVehicleOrder(v, order_number);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    66
	if (order == NULL) return CMD_ERROR;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    67
8650
9a9c905deaa5 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 8302
diff changeset
    68
	bool packed_time = HasBit(p1, 25);
9a9c905deaa5 (svn r12296) -Fix [FS#1549]: restore timetable from backupped orders and add group ID to the backup
glx
parents: 8302
diff changeset
    69
	bool is_journey = HasBit(p1, 24) || packed_time;
9665
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    70
10227
2119781291ba (svn r14450) -Fix [FS#2337]: time table restoring did check too restrictively causing order backups not to be properly restored.
rubidium
parents: 10033
diff changeset
    71
	int wait_time   = order->wait_time;
2119781291ba (svn r14450) -Fix [FS#2337]: time table restoring did check too restrictively causing order backups not to be properly restored.
rubidium
parents: 10033
diff changeset
    72
	int travel_time = order->travel_time;
9665
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    73
	if (packed_time) {
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    74
		travel_time = GB(p2, 0, 16);
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    75
		wait_time   = GB(p2, 16, 16);;
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    76
	} else if (is_journey) {
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    77
		travel_time = GB(p2, 0, 16);
9641
a854c7cf6af5 (svn r13704) -Fix: make timetables work more sensible when used in conjunction with conditional orders, i.e. make it possibly to tell how long to travel to the next destination if you jump.
rubidium
parents: 9413
diff changeset
    78
	} else {
9665
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    79
		wait_time   = GB(p2, 0, 16);
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    80
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    81
10227
2119781291ba (svn r14450) -Fix [FS#2337]: time table restoring did check too restrictively causing order backups not to be properly restored.
rubidium
parents: 10033
diff changeset
    82
	if (wait_time != order->wait_time) {
9665
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    83
		switch (order->GetType()) {
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    84
			case OT_GOTO_STATION:
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    85
				if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE);
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    86
				break;
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    87
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    88
			case OT_CONDITIONAL:
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    89
				break;
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    90
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    91
			default: return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    92
		}
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    93
	}
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    94
10227
2119781291ba (svn r14450) -Fix [FS#2337]: time table restoring did check too restrictively causing order backups not to be properly restored.
rubidium
parents: 10033
diff changeset
    95
	if (travel_time != order->travel_time && order->IsType(OT_CONDITIONAL)) return CMD_ERROR;
9665
dd6c07db9d32 (svn r13752) -Fix [FS#2130]: correctly restore conditional orders when they are put 'into' backup.
rubidium
parents: 9641
diff changeset
    96
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    97
	if (flags & DC_EXEC) {
10227
2119781291ba (svn r14450) -Fix [FS#2337]: time table restoring did check too restrictively causing order backups not to be properly restored.
rubidium
parents: 10033
diff changeset
    98
		if (wait_time   != order->wait_time)   ChangeTimetable(v, order_number, wait_time,   false);
2119781291ba (svn r14450) -Fix [FS#2337]: time table restoring did check too restrictively causing order backups not to be properly restored.
rubidium
parents: 10033
diff changeset
    99
		if (travel_time != order->travel_time) ChangeTimetable(v, order_number, travel_time, true);
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   100
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   101
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   102
	return CommandCost();
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   103
}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   104
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   105
/**
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   106
 * Clear the lateness counter to make the vehicle on time.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   107
 * @param tile Not used.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   108
 * @param flags Operation to perform.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   109
 * @param p1 Various bitstuffed elements
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   110
 * - p1 = (bit  0-15) - Vehicle with the orders to change.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   111
 */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   112
CommandCost CmdSetVehicleOnTime(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   113
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   114
	if (!_settings_game.order.timetabling) return CMD_ERROR;
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   115
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   116
	VehicleID veh = GB(p1, 0, 16);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   117
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   118
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   119
	Vehicle *v = GetVehicle(veh);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   120
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   121
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   122
	if (flags & DC_EXEC) {
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   123
		v->lateness_counter = 0;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   124
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   125
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   126
	return CommandCost();
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   127
}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   128
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   129
/**
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   130
 * Start or stop filling the timetable automatically from the time the vehicle
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   131
 * actually takes to complete it. When starting to autofill the current times
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   132
 * are cleared and the timetable will start again from scratch.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   133
 * @param tile Not used.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   134
 * @param flags Operation to perform.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   135
 * @param p1 Vehicle index.
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   136
 * @param p2 Various bitstuffed elements
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   137
 * - p2 = (bit 0) - Set to 1 to enable, 0 to disable autofill.
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   138
 * - p2 = (bit 1) - Set to 1 to preserve waiting times in non-destructive mode
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   139
 */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   140
CommandCost CmdAutofillTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   141
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   142
	if (!_settings_game.order.timetabling) return CMD_ERROR;
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   143
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   144
	VehicleID veh = GB(p1, 0, 16);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   145
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   146
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   147
	Vehicle *v = GetVehicle(veh);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   148
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   149
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   150
	if (flags & DC_EXEC) {
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   151
		if (HasBit(p2, 0)) {
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   152
			/* Start autofilling the timetable, which clears all the current
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   153
			 * timings and clears the "timetable has started" bit. */
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
   154
			SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   155
			ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   156
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   157
			if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   158
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   159
			v->lateness_counter = 0;
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   160
		} else {
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   161
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   162
			ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   163
		}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   164
	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   165
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9829
diff changeset
   166
	for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
7809
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   167
		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   168
	}
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   169
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   170
	return CommandCost();
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   171
}
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   172
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   173
void UpdateVehicleTimetable(Vehicle *v, bool travelling)
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   174
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   175
	uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   176
	uint time_taken = v->current_order_time;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   177
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   178
	v->current_order_time = 0;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   179
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9354
diff changeset
   180
	if (!_settings_game.order.timetabling) return;
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   181
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   182
	bool just_started = false;
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   183
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   184
	/* Make sure the timetable only starts when the vehicle reaches the first
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8853
diff changeset
   185
	 * order, not when travelling from the depot to the first station. */
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8853
diff changeset
   186
	if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8853
diff changeset
   187
		SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   188
		just_started = true;
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8853
diff changeset
   189
	}
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   190
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7809
diff changeset
   191
	if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   192
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8853
diff changeset
   193
	if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   194
		if (travelling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) {
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   195
			/* Need to clear that now as otherwise we are not able to reduce the wait time */
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   196
			v->current_order.wait_time = 0;
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   197
		}
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   198
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   199
		if (just_started) return;
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   200
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   201
		/* Modify station waiting time only if our new value is larger (this is
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   202
		 * always the case when we cleared the timetable). */
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   203
		if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.wait_time)) {
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   204
			/* Round the time taken up to the nearest day, as this will avoid
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   205
			 * confusion for people who are timetabling in days, and can be
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   206
			 * adjusted later by people who aren't. */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   207
			time_taken = (((time_taken - 1) / DAY_TICKS) + 1) * DAY_TICKS;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   208
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   209
			ChangeTimetable(v, v->cur_order_index, time_taken, travelling);
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   210
		}
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   211
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   212
		if (v->cur_order_index == 0 && travelling) {
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   213
			/* If we just started we would have returned earlier and have not reached
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   214
			 * this code. So obviously, we have completed our round: So turn autofill
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   215
			 * off again. */
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   216
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   217
			ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME);
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   218
		}
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   219
		return;
8970
da7261f48b7e (svn r12762) -Fix: tabs after the first non-tab character are generally not okay (or lines starting with a space and then tabs).
rubidium
parents: 8853
diff changeset
   220
	}
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   221
10341
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   222
	if (just_started) return;
840daea3d9a2 (svn r14592) -Feature [FS#1124]: non-destructive autofill with option to keep waiting times (PhilSophus)
rubidium
parents: 10227
diff changeset
   223
7062
6fac471b1093 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 6981
diff changeset
   224
	/* Vehicles will wait at stations if they arrive early even if they are not
6fac471b1093 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 6981
diff changeset
   225
	 * timetabled to wait there, so make sure the lateness counter is updated
6fac471b1093 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 6981
diff changeset
   226
	 * when this happens. */
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   227
	if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   228
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   229
	v->lateness_counter -= (timetabled - time_taken);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   230
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9829
diff changeset
   231
	for (v = v->FirstShared(); v != NULL; v = v->NextShared()) {
7809
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   232
		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   233
	}
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   234
}