glx@9629: /* $Id$ */ glx@9629: rubidium@10455: /** @file timetable_cmd.cpp Commands related to time tabling. */ glx@9629: glx@9629: #include "stdafx.h" glx@9629: #include "openttd.h" glx@9629: #include "variables.h" rubidium@9723: #include "command_func.h" rubidium@9723: #include "functions.h" rubidium@9723: #include "window_func.h" rubidium@9723: #include "vehicle_func.h" rubidium@9723: #include "vehicle_base.h" rubidium@9724: #include "settings_type.h" glx@9629: rubidium@9724: #include "table/strings.h" glx@9629: glx@9629: static void ChangeTimetable(Vehicle *v, VehicleOrderID order_number, uint16 time, bool is_journey) glx@9629: { glx@9629: Order *order = GetVehicleOrder(v, order_number); glx@9629: glx@9629: if (is_journey) { glx@9629: order->travel_time = time; glx@9629: } else { glx@9629: order->wait_time = time; glx@9629: } glx@9629: rubidium@10142: if (v->cur_order_index == order_number && v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) { glx@9629: if (is_journey) { glx@9629: v->current_order.travel_time = time; glx@9629: } else { glx@9629: v->current_order.wait_time = time; glx@9629: } glx@9629: } glx@9629: rubidium@9722: for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { rubidium@9722: InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); rubidium@9722: } glx@9629: } glx@9629: glx@9629: /** glx@9629: * Add or remove waiting times from an order. glx@9629: * @param tile Not used. glx@9629: * @param flags Operation to perform. glx@9629: * @param p1 Various bitstuffed elements glx@9629: * - p1 = (bit 0-15) - Vehicle with the orders to change. glx@9629: * - p1 = (bit 16-23) - Order index to modify. glx@9629: * - p1 = (bit 24) - Whether to change the waiting time or the travelling glx@9629: * time. glx@9800: * - p1 = (bit 25) - Whether p2 contains waiting and travelling time. glx@9629: * @param p2 The amount of time to wait. glx@9800: * - p2 = (bit 0-15) - Waiting or travelling time as specified by p1 bit 24 if p1 bit 25 is not set, glx@9800: * Travelling time if p1 bit 25 is set. glx@9800: * - p2 = (bit 16-31) - Waiting time if p1 bit 25 is set glx@9629: */ glx@9629: CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9629: { glx@10776: if (!_settings_game.order.timetabling) return CMD_ERROR; glx@9629: glx@9629: VehicleID veh = GB(p1, 0, 16); glx@9629: if (!IsValidVehicleID(veh)) return CMD_ERROR; glx@9629: glx@9629: Vehicle *v = GetVehicle(veh); glx@9629: if (!CheckOwnership(v->owner)) return CMD_ERROR; glx@9629: glx@9629: VehicleOrderID order_number = GB(p1, 16, 8); glx@9629: Order *order = GetVehicleOrder(v, order_number); glx@9629: if (order == NULL) return CMD_ERROR; glx@9629: glx@9800: bool packed_time = HasBit(p1, 25); glx@9800: bool is_journey = HasBit(p1, 24) || packed_time; glx@9629: if (!is_journey) { rubidium@9869: if (!order->IsType(OT_GOTO_STATION)) return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS); rubidium@10142: if (order->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE); glx@9629: } glx@9629: glx@9629: if (flags & DC_EXEC) { glx@9800: ChangeTimetable(v, order_number, GB(p2, 0, 16), is_journey); glx@9800: if (packed_time) ChangeTimetable(v, order_number, GB(p2, 16, 16), false); glx@9629: } glx@9629: glx@9629: return CommandCost(); glx@9629: } glx@9629: glx@9629: /** glx@9629: * Clear the lateness counter to make the vehicle on time. glx@9629: * @param tile Not used. glx@9629: * @param flags Operation to perform. glx@9629: * @param p1 Various bitstuffed elements glx@9629: * - p1 = (bit 0-15) - Vehicle with the orders to change. glx@9629: */ glx@9629: CommandCost CmdSetVehicleOnTime(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9629: { glx@10776: if (!_settings_game.order.timetabling) return CMD_ERROR; glx@9629: glx@9629: VehicleID veh = GB(p1, 0, 16); glx@9629: if (!IsValidVehicleID(veh)) return CMD_ERROR; glx@9629: glx@9629: Vehicle *v = GetVehicle(veh); glx@9629: if (!CheckOwnership(v->owner)) return CMD_ERROR; glx@9629: glx@9629: if (flags & DC_EXEC) { glx@9629: v->lateness_counter = 0; glx@9629: } glx@9629: glx@9629: return CommandCost(); glx@9629: } glx@9629: glx@9629: /** glx@9629: * Start or stop filling the timetable automatically from the time the vehicle glx@9629: * actually takes to complete it. When starting to autofill the current times glx@9629: * are cleared and the timetable will start again from scratch. glx@9629: * @param tile Not used. glx@9629: * @param flags Operation to perform. glx@9629: * @param p1 Vehicle index. glx@9629: * @param p2 Set to 1 to enable, 0 to disable. glx@9629: */ glx@9629: CommandCost CmdAutofillTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) glx@9629: { glx@10776: if (!_settings_game.order.timetabling) return CMD_ERROR; glx@9629: glx@9629: VehicleID veh = GB(p1, 0, 16); glx@9629: if (!IsValidVehicleID(veh)) return CMD_ERROR; glx@9629: glx@9629: Vehicle *v = GetVehicle(veh); glx@9629: if (!CheckOwnership(v->owner)) return CMD_ERROR; glx@9629: glx@9629: if (flags & DC_EXEC) { glx@9629: if (p2 == 1) { glx@9629: /* Start autofilling the timetable, which clears all the current glx@9629: * timings and clears the "timetable has started" bit. */ rubidium@9722: SetBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); rubidium@9722: ClrBit(v->vehicle_flags, VF_TIMETABLE_STARTED); glx@9629: glx@9629: for (Order *order = GetVehicleOrder(v, 0); order != NULL; order = order->next) { glx@9629: order->wait_time = 0; glx@9629: order->travel_time = 0; glx@9629: } glx@9629: glx@9629: v->current_order.wait_time = 0; glx@9629: v->current_order.travel_time = 0; glx@9629: } else { rubidium@9722: ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); glx@9629: } glx@9629: } glx@9629: rubidium@9722: for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { rubidium@9722: InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); rubidium@9722: } rubidium@9722: glx@9629: return CommandCost(); glx@9629: } glx@9629: glx@9629: void UpdateVehicleTimetable(Vehicle *v, bool travelling) glx@9629: { glx@9629: uint timetabled = travelling ? v->current_order.travel_time : v->current_order.wait_time; glx@9629: uint time_taken = v->current_order_time; glx@9629: glx@9629: v->current_order_time = 0; glx@9629: glx@10776: if (!_settings_game.order.timetabling) return; glx@9629: glx@9629: /* Make sure the timetable only starts when the vehicle reaches the first rubidium@10249: * order, not when travelling from the depot to the first station. */ rubidium@10249: if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) { rubidium@10249: SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED); rubidium@10249: return; rubidium@10249: } glx@9629: rubidium@9722: if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return; glx@9629: rubidium@10249: if (HasBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE)) { glx@9629: if (timetabled == 0) { glx@9629: /* Round the time taken up to the nearest day, as this will avoid glx@9629: * confusion for people who are timetabling in days, and can be glx@9629: * adjusted later by people who aren't. */ glx@9629: time_taken = (((time_taken - 1) / DAY_TICKS) + 1) * DAY_TICKS; glx@9629: glx@9629: ChangeTimetable(v, v->cur_order_index, time_taken, travelling); glx@9629: return; glx@9629: } else if (v->cur_order_index == 0) { glx@9629: /* Otherwise if we're at the beginning and it already has a value, glx@9629: * assume that autofill is finished and turn it off again. */ rubidium@9722: ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); glx@9629: } rubidium@10249: } glx@9629: glx@9629: /* Vehicles will wait at stations if they arrive early even if they are not glx@9629: * timetabled to wait there, so make sure the lateness counter is updated glx@9629: * when this happens. */ glx@9629: if (timetabled == 0 && (travelling || v->lateness_counter >= 0)) return; glx@9629: glx@9629: v->lateness_counter -= (timetabled - time_taken); glx@9629: rubidium@9722: for (v = GetFirstVehicleFromSharedList(v); v != NULL; v = v->next_shared) { rubidium@9722: InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); rubidium@9722: } glx@9629: }