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