src/timetable_cmd.cpp
author skidd13
Tue, 20 Nov 2007 13:35:54 +0000
changeset 8427 143b0be22af1
parent 8425 72a71d480c5f
child 8612 6414fc21c2f3
permissions -rw-r--r--
(svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     1
/* $Id$ */
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     2
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     3
/** @file timetable_cmd.cpp */
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     4
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     5
#include "stdafx.h"
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     6
#include "openttd.h"
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     7
#include "functions.h"
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     8
#include "variables.h"
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
     9
#include "table/strings.h"
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    10
#include "command.h"
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    11
#include "date.h"
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    12
#include "player.h"
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    13
#include "vehicle.h"
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    14
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    15
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    16
static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey)
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    17
{
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    18
	Order *order = GetVehicleOrder(v, order_number);
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    19
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    20
	if (is_journey) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    21
		order->travel_time = time;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    22
	} else {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    23
		order->wait_time = time;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    24
	}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    25
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8305
diff changeset
    26
	if (v->cur_order_index == order_number && HasBit(v->current_order.flags, OFB_PART_OF_ORDERS)) {
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    27
		if (is_journey) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    28
			v->current_order.travel_time = time;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    29
		} else {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    30
			v->current_order.wait_time = time;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    31
		}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    32
	}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    33
8305
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
    34
	for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
    35
		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
    36
	}
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    37
}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    38
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    39
/**
1f22e188617e (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.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    41
 * @param tile Not used.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    42
 * @param flags Operation to perform.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    43
 * @param p1 Various bitstuffed elements
1f22e188617e (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.
1f22e188617e (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.
1f22e188617e (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
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    47
 *                      time.
1f22e188617e (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.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    49
 */
1f22e188617e (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)
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    51
{
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    53
1f22e188617e (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);
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    56
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    57
	Vehicle *v = GetVehicle(veh);
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    59
1f22e188617e (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);
1f22e188617e (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);
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    63
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8305
diff changeset
    64
	bool is_journey = HasBit(p1, 24);
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    65
	if (!is_journey) {
1f22e188617e (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);
1f22e188617e (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);
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    68
	}
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    69
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    70
	if (flags & DC_EXEC) {
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
    71
		ChangeTimetable(v, order_number, p2, is_journey);
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    72
	}
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    73
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    74
	return CommandCost();
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    75
}
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    76
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    77
/**
1f22e188617e (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.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    79
 * @param tile Not used.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    80
 * @param flags Operation to perform.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    81
 * @param p1 Various bitstuffed elements
1f22e188617e (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.
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    83
 */
1f22e188617e (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)
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    85
{
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    87
1f22e188617e (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);
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    90
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    91
	Vehicle *v = GetVehicle(veh);
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    93
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    94
	if (flags & DC_EXEC) {
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    95
		v->lateness_counter = 0;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    96
	}
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    97
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    98
	return CommandCost();
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
    99
}
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   100
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   101
/**
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   102
 * Start or stop filling the timetable automatically from the time the vehicle
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   103
 * actually takes to complete it. When starting to autofill the current times
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   104
 * are cleared and the timetable will start again from scratch.
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   105
 * @param tile Not used.
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   106
 * @param flags Operation to perform.
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   107
 * @param p1 Vehicle index.
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   108
 * @param p2 Set to 1 to enable, 0 to disable.
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   109
 */
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   110
CommandCost CmdAutofillTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   111
{
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   112
	if (!_patches.timetabling) return CMD_ERROR;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   113
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   114
	VehicleID veh = GB(p1, 0, 16);
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   115
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   116
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   117
	Vehicle *v = GetVehicle(veh);
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   118
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   119
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   120
	if (flags & DC_EXEC) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   121
		if (p2 == 1) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   122
			/* Start autofilling the timetable, which clears all the current
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   123
			 * timings and clears the "timetable has started" bit. */
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
   124
			SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
   125
			ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   126
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   127
			for (Order *order = GetVehicleOrder(v, 0); order != NULL; order = order->next) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   128
				order->wait_time = 0;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   129
				order->travel_time = 0;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   130
			}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   131
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   132
			v->current_order.wait_time = 0;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   133
			v->current_order.travel_time = 0;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   134
		} else {
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
   135
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   136
		}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   137
	}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   138
8305
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   139
	for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   140
		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   141
	}
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   142
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   143
	return CommandCost();
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   144
}
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   145
1f22e188617e (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)
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   147
{
1f22e188617e (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;
1f22e188617e (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;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   150
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   151
	v->current_order_time = 0;
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   152
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   153
	if (!_patches.timetabling) return;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   154
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   155
	/* Make sure the timetable only starts when the vehicle reaches the first
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   156
 	 * order, not when travelling from the depot to the first station. */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8305
diff changeset
   157
 	if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) {
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
   158
 		SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED);
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   159
 		return;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   160
 	}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   161
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8305
diff changeset
   162
	if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return;
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   163
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8305
diff changeset
   164
 	if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) {
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   165
		if (timetabled == 0) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   166
			/* Round the time taken up to the nearest day, as this will avoid
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   167
			 * confusion for people who are timetabling in days, and can be
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   168
			 * adjusted later by people who aren't. */
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   169
			time_taken = (((time_taken - 1) / DAY_TICKS) + 1) * DAY_TICKS;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   170
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   171
			ChangeTimetable(v, v->cur_order_index, time_taken, travelling);
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   172
			return;
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   173
		} else if (v->cur_order_index == 0) {
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   174
			/* Otherwise if we're at the beginning and it already has a value,
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   175
			 * assume that autofill is finished and turn it off again. */
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
   176
			ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE);
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   177
		}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   178
 	}
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   179
7558
ca45ff710b35 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 7477
diff changeset
   180
	/* Vehicles will wait at stations if they arrive early even if they are not
ca45ff710b35 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 7477
diff changeset
   181
	 * timetabled to wait there, so make sure the lateness counter is updated
ca45ff710b35 (svn r10327) -Fix (r10236) [FS#934]: Vehicles wait at stations when they arrive early even
maedhros
parents: 7477
diff changeset
   182
	 * when this happens. */
7562
f6f62814907b (svn r10331) -Feature: Add the possibility of automatically filling in timetables based on
maedhros
parents: 7558
diff changeset
   183
	if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return;
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   184
1f22e188617e (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);
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   186
8305
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   187
	for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) {
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   188
		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
c8b7592ae94b (svn r11359) -Fix [FS#1381]: shared timetables were not properly refreshed with autofill.
rubidium
parents: 7562
diff changeset
   189
	}
7477
1f22e188617e (svn r10237) -Fix (r10236): It helps if you use svn add with new files...
maedhros
parents:
diff changeset
   190
}