src/timetable_cmd.cpp
author truelight
Mon, 23 Jul 2007 16:39:27 +0000
changeset 7310 eed5036fee1f
parent 7066 e5d16aa8d6ca
child 7809 5711d0a71dba
permissions -rw-r--r--
(svn r10662) -Add: added 'V' as new shortcut for new viewport (bilbo)
-Add: added 'M' as new shortcut for smallmap (bilbo)
-Add: added '+' and '-' as shortcuts to zoom in the mainview (bilbo)
-Add: added support for other keycodes to be possible used in the future (bilbo)
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
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    26
	if (v->cur_order_index == order_number && HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
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
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    34
	InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    35
}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    36
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    37
/**
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    38
 * 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
    39
 * @param tile Not used.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    40
 * @param flags Operation to perform.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    41
 * @param p1 Various bitstuffed elements
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    42
 * - 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
    43
 * - 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
    44
 * - 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
    45
 *                      time.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    46
 * @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
    47
 */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    48
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
    49
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    50
	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
    51
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    52
	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
    53
	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
    54
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    55
	Vehicle *v = GetVehicle(veh);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    56
	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
    57
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    58
	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
    59
	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
    60
	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
    61
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    62
	bool is_journey = HASBIT(p1, 24);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    63
	if (!is_journey) {
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    64
		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
    65
		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
    66
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    67
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    68
	if (flags & DC_EXEC) {
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    69
		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
    70
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    71
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    72
	return CommandCost();
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
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
 * 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
    77
 * @param tile Not used.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    78
 * @param flags Operation to perform.
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    79
 * @param p1 Various bitstuffed elements
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    80
 * - 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
    81
 */
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    82
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
    83
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    84
	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
    85
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    86
	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
    87
	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
    88
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    89
	Vehicle *v = GetVehicle(veh);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    90
	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
    91
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    92
	if (flags & DC_EXEC) {
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    93
		v->lateness_counter = 0;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    94
	}
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    95
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    96
	return CommandCost();
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
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
    99
/**
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   100
 * 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
   101
 * 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
   102
 * 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
   103
 * @param tile Not used.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   104
 * @param flags Operation to perform.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   105
 * @param p1 Vehicle index.
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   106
 * @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
   107
 */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   108
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
   109
{
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   110
	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
   111
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   112
	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
   113
	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
   114
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   115
	Vehicle *v = GetVehicle(veh);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   116
	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
   117
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   118
	if (flags & DC_EXEC) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   119
		if (p2 == 1) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   120
			/* 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
   121
			 * timings and clears the "timetable has started" bit. */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   122
			SETBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   123
			CLRBIT(v->vehicle_flags, VF_TIMETABLE_STARTED);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   124
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   125
			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
   126
				order->wait_time = 0;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   127
				order->travel_time = 0;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   128
			}
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
			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
   131
			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
   132
		} else {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   133
			CLRBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   134
		}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   135
	}
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
	return CommandCost();
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   138
}
6981
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   139
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   140
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
   141
{
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   142
	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
   143
	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
   144
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   145
	v->current_order_time = 0;
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   146
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   147
	if (!_patches.timetabling) return;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   148
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   149
	/* 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
   150
 	 * order, not when travelling from the depot to the first station. */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   151
 	if (v->cur_order_index == 0 && !HASBIT(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   152
 		SETBIT(v->vehicle_flags, VF_TIMETABLE_STARTED);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   153
 		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
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   156
	if (!HASBIT(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   157
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   158
 	if (HASBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   159
		if (timetabled == 0) {
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   160
			/* 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
   161
			 * 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
   162
			 * 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
   163
			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
   164
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   165
			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
   166
			return;
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   167
		} 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
   168
			/* 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
   169
			 * assume that autofill is finished and turn it off again. */
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   170
			CLRBIT(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   171
		}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   172
 	}
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   173
7062
6fac471b1093 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 6981
diff changeset
   174
	/* 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
   175
	 * 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
   176
	 * when this happens. */
7066
e5d16aa8d6ca (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7062
diff changeset
   177
	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
   178
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   179
	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
   180
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   181
	InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
9af49f50fe33 (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   182
}