src/timetable_cmd.cpp
changeset 6981 9af49f50fe33
child 7062 6fac471b1093
equal deleted inserted replaced
6980:c7c4f3bf5901 6981:9af49f50fe33
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file timetable_cmd.cpp */
       
     4 
       
     5 #include "stdafx.h"
       
     6 #include "openttd.h"
       
     7 #include "functions.h"
       
     8 #include "variables.h"
       
     9 #include "table/strings.h"
       
    10 #include "command.h"
       
    11 #include "vehicle.h"
       
    12 
       
    13 
       
    14 /**
       
    15  * Add or remove waiting times from an order.
       
    16  * @param tile Not used.
       
    17  * @param flags Operation to perform.
       
    18  * @param p1 Various bitstuffed elements
       
    19  * - p1 = (bit  0-15) - Vehicle with the orders to change.
       
    20  * - p1 = (bit 16-23) - Order index to modify.
       
    21  * - p1 = (bit    24) - Whether to change the waiting time or the travelling
       
    22  *                      time.
       
    23  * @param p2 The amount of time to wait.
       
    24  */
       
    25 CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
    26 {
       
    27 	if (!_patches.timetabling) return CMD_ERROR;
       
    28 
       
    29 	VehicleID veh = GB(p1, 0, 16);
       
    30 	if (!IsValidVehicleID(veh)) return CMD_ERROR;
       
    31 
       
    32 	Vehicle *v = GetVehicle(veh);
       
    33 	if (!CheckOwnership(v->owner)) return CMD_ERROR;
       
    34 
       
    35 	VehicleOrderID order_number = GB(p1, 16, 8);
       
    36 	Order *order = GetVehicleOrder(v, order_number);
       
    37 	if (order == NULL) return CMD_ERROR;
       
    38 
       
    39 	bool is_journey = HASBIT(p1, 24);
       
    40 	if (!is_journey) {
       
    41 		if (order->type != OT_GOTO_STATION) return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS);
       
    42 		if (_patches.new_nonstop && (order->flags & OF_NON_STOP)) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE);
       
    43 	}
       
    44 
       
    45 	if (flags & DC_EXEC) {
       
    46 		if (is_journey) {
       
    47 			order->travel_time = p2;
       
    48 		} else {
       
    49 			order->wait_time = p2;
       
    50 		}
       
    51 
       
    52 		if (v->cur_order_index == order_number && HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
       
    53 			if (is_journey) {
       
    54 				v->current_order.travel_time = p2;
       
    55 			} else {
       
    56 				v->current_order.wait_time = p2;
       
    57 			}
       
    58 		}
       
    59 
       
    60 		InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
       
    61 	}
       
    62 
       
    63 	return CommandCost();
       
    64 }
       
    65 
       
    66 /**
       
    67  * Clear the lateness counter to make the vehicle on time.
       
    68  * @param tile Not used.
       
    69  * @param flags Operation to perform.
       
    70  * @param p1 Various bitstuffed elements
       
    71  * - p1 = (bit  0-15) - Vehicle with the orders to change.
       
    72  */
       
    73 CommandCost CmdSetVehicleOnTime(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
    74 {
       
    75 	if (!_patches.timetabling) return CMD_ERROR;
       
    76 
       
    77 	VehicleID veh = GB(p1, 0, 16);
       
    78 	if (!IsValidVehicleID(veh)) return CMD_ERROR;
       
    79 
       
    80 	Vehicle *v = GetVehicle(veh);
       
    81 	if (!CheckOwnership(v->owner)) return CMD_ERROR;
       
    82 
       
    83 	if (flags & DC_EXEC) {
       
    84 		v->lateness_counter = 0;
       
    85 	}
       
    86 
       
    87 	return CommandCost();
       
    88 }
       
    89 
       
    90 
       
    91 void UpdateVehicleTimetable(Vehicle *v, bool travelling)
       
    92 {
       
    93 	uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time;
       
    94 	uint time_taken = v->current_order_time;
       
    95 
       
    96 	v->current_order_time = 0;
       
    97 
       
    98 	if (!_patches.timetabling || timetabled == 0) return;
       
    99 
       
   100 	v->lateness_counter -= (timetabled - time_taken);
       
   101 
       
   102 	InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index);
       
   103 }