src/timetable_cmd.cpp
author skidd13
Sun, 02 Dec 2007 21:43:16 +0000
changeset 8004 1c54bc6f4bdf
parent 7931 b0a46cd92225
child 8116 8da76dcb3287
permissions -rw-r--r--
(svn r11563) -Codechange: Align the preprocessor code in stdafx.h with tabs
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
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     3
/** @file timetable_cmd.cpp */
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 "functions.h"
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     8
#include "variables.h"
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     9
#include "table/strings.h"
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    10
#include "command.h"
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    11
#include "date.h"
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    12
#include "player.h"
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    13
#include "vehicle.h"
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    14
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    15
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    16
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
    17
{
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    18
	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
    19
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    20
	if (is_journey) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    21
		order->travel_time = time;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    22
	} else {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    23
		order->wait_time = time;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    24
	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    25
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7809
diff changeset
    26
	if (v->cur_order_index == order_number && HasBit(v->current_order.flags, OFB_PART_OF_ORDERS)) {
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    27
		if (is_journey) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    28
			v->current_order.travel_time = time;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    29
		} else {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    30
			v->current_order.wait_time = time;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    31
		}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    32
	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    33
7809
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
    34
	for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
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.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    48
 * @param p2 The amount of time to wait.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    49
 */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    50
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
    51
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    52
	if (!_patches.timetabling) return CMD_ERROR;
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
	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
    55
	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
    56
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    57
	Vehicle *v = GetVehicle(veh);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    58
	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
    59
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    60
	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
    61
	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
    62
	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
    63
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7809
diff changeset
    64
	bool is_journey = HasBit(p1, 24);
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    65
	if (!is_journey) {
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    66
		if (order->type != OT_GOTO_STATION) return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    67
		if (_patches.new_nonstop && (order->flags & OF_NON_STOP)) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    68
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    69
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    70
	if (flags & DC_EXEC) {
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    71
		ChangeTimetable(v, order_number, p2, is_journey);
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    72
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    73
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    74
	return CommandCost();
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    75
}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    76
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    77
/**
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    78
 * 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
    79
 * @param tile Not used.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    80
 * @param flags Operation to perform.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    81
 * @param p1 Various bitstuffed elements
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    82
 * - 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
    83
 */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    84
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
    85
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    86
	if (!_patches.timetabling) return CMD_ERROR;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    87
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    88
	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
    89
	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
    90
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    91
	Vehicle *v = GetVehicle(veh);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    92
	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
    93
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    94
	if (flags & DC_EXEC) {
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    95
		v->lateness_counter = 0;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    96
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    97
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    98
	return CommandCost();
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    99
}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   100
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   101
/**
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   102
 * 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
   103
 * 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
   104
 * 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
   105
 * @param tile Not used.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   106
 * @param flags Operation to perform.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   107
 * @param p1 Vehicle index.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   108
 * @param p2 Set to 1 to enable, 0 to disable.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   109
 */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   110
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
   111
{
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   112
	if (!_patches.timetabling) return CMD_ERROR;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   113
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   114
	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
   115
	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
   116
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   117
	Vehicle *v = GetVehicle(veh);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   118
	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
   119
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   120
	if (flags & DC_EXEC) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   121
		if (p2 == 1) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   122
			/* 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
   123
			 * 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
   124
			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
   125
			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
   126
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   127
			for (Order *order = GetVehicleOrder(v, 0); order != NULL; order = order->next) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   128
				order->wait_time = 0;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   129
				order->travel_time = 0;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   130
			}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   131
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   132
			v->current_order.wait_time = 0;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   133
			v->current_order.travel_time = 0;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   134
		} 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
   135
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   136
		}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   137
	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   138
7809
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   139
	for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   140
		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
   141
	}
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   142
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   143
	return CommandCost();
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   144
}
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   145
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   146
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
   147
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   148
	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
   149
	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
   150
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   151
	v->current_order_time = 0;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   152
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   153
	if (!_patches.timetabling) return;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   154
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   155
	/* Make sure the timetable only starts when the vehicle reaches the first
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   156
 	 * order, not when travelling from the depot to the first station. */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7809
diff changeset
   157
 	if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
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
   158
 		SetBit(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
   159
 		return;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   160
 	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   161
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7809
diff changeset
   162
	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
   163
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7809
diff changeset
   164
 	if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   165
		if (timetabled == 0) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   166
			/* 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
   167
			 * 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
   168
			 * 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
   169
			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
   170
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   171
			ChangeTimetable(v, v->cur_order_index, time_taken, travelling);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   172
			return;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   173
		} else if (v->cur_order_index == 0) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   174
			/* Otherwise if we're at the beginning and it already has a value,
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   175
			 * assume that autofill is finished and turn it 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
   176
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   177
		}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   178
 	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   179
7062
6fac471b1093 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 6981
diff changeset
   180
	/* 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
   181
	 * 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
   182
	 * when this happens. */
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   183
	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
   184
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   185
	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
   186
7809
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   187
	for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
5711d0a71dba (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7066
diff changeset
   188
		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
   189
	}
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   190
}