maedhros@6981: /* $Id$ */ maedhros@6981: maedhros@6981: /** @file timetable_cmd.cpp */ 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@8302: if (v->cur_order_index == order_number && HasBit(v->current_order.flags, OF_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. maedhros@6981: * @param p2 The amount of time to wait. maedhros@6981: */ maedhros@6981: CommandCost CmdChangeTimetable(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) maedhros@6981: { maedhros@6981: if (!_patches.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: skidd13@7928: bool is_journey = HasBit(p1, 24); maedhros@6981: if (!is_journey) { maedhros@6981: if (order->type != OT_GOTO_STATION) return_cmd_error(STR_TIMETABLE_ONLY_WAIT_AT_STATIONS); rubidium@8302: if (_patches.new_nonstop && (order->flags & OFB_NON_STOP)) return_cmd_error(STR_TIMETABLE_NOT_STOPPING_HERE); maedhros@6981: } maedhros@6981: maedhros@6981: if (flags & DC_EXEC) { maedhros@7066: ChangeTimetable(v, order_number, p2, is_journey); 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: { maedhros@6981: if (!_patches.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: { maedhros@7066: if (!_patches.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: maedhros@7066: if (!_patches.timetabling) return; maedhros@7066: maedhros@7066: /* Make sure the timetable only starts when the vehicle reaches the first maedhros@7066: * order, not when travelling from the depot to the first station. */ skidd13@7928: if (v->cur_order_index == 0 && !HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) { skidd13@7931: SetBit(v->vehicle_flags, VF_TIMETABLE_STARTED); maedhros@7066: return; maedhros@7066: } maedhros@7066: skidd13@7928: if (!HasBit(v->vehicle_flags, VF_TIMETABLE_STARTED)) return; maedhros@7066: skidd13@7928: 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: maedhros@7066: ChangeTimetable(v, v->cur_order_index, time_taken, travelling); 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: } maedhros@7066: } 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: }