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@10033: for (v = v->FirstShared(); v != NULL; v = v->NextShared()) { rubidium@10033: if (v->cur_order_index == order_number && v->current_order.Equals(*order)) { rubidium@10033: if (is_journey) { rubidium@10033: v->current_order.travel_time = time; rubidium@10033: } else { rubidium@10033: v->current_order.wait_time = time; rubidium@10033: } maedhros@7066: } 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@10227: int wait_time = order->wait_time; rubidium@10227: int travel_time = order->travel_time; 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@10227: if (wait_time != order->wait_time) { 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@10227: if (travel_time != order->travel_time && order->IsType(OT_CONDITIONAL)) return CMD_ERROR; rubidium@9665: maedhros@6981: if (flags & DC_EXEC) { rubidium@10227: if (wait_time != order->wait_time) ChangeTimetable(v, order_number, wait_time, false); rubidium@10227: if (travel_time != order->travel_time) 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. rubidium@10341: * @param p2 Various bitstuffed elements rubidium@10341: * - p2 = (bit 0) - Set to 1 to enable, 0 to disable autofill. rubidium@10341: * - p2 = (bit 1) - Set to 1 to preserve waiting times in non-destructive mode 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) { rubidium@10341: if (HasBit(p2, 0)) { 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: rubidium@10341: if (HasBit(p2, 1)) SetBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME); maedhros@7066: rubidium@10341: v->lateness_counter = 0; maedhros@7066: } else { skidd13@7929: ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); rubidium@10341: ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME); maedhros@7066: } maedhros@7066: } maedhros@7066: rubidium@9941: for (v = v->FirstShared(); v != NULL; v = v->NextShared()) { 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: rubidium@10341: bool just_started = false; rubidium@10341: 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@10341: just_started = true; 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)) { rubidium@10341: if (travelling && !HasBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME)) { rubidium@10341: /* Need to clear that now as otherwise we are not able to reduce the wait time */ rubidium@10341: v->current_order.wait_time = 0; rubidium@10341: } rubidium@10341: rubidium@10341: if (just_started) return; rubidium@10341: rubidium@10341: /* Modify station waiting time only if our new value is larger (this is rubidium@10341: * always the case when we cleared the timetable). */ rubidium@10341: if (!v->current_order.IsType(OT_CONDITIONAL) && (travelling || time_taken > v->current_order.wait_time)) { 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@10341: ChangeTimetable(v, v->cur_order_index, time_taken, travelling); rubidium@10341: } rubidium@10341: rubidium@10341: if (v->cur_order_index == 0 && travelling) { rubidium@10341: /* If we just started we would have returned earlier and have not reached rubidium@10341: * this code. So obviously, we have completed our round: So turn autofill rubidium@10341: * off again. */ skidd13@7929: ClrBit(v->vehicle_flags, VF_AUTOFILL_TIMETABLE); rubidium@10341: ClrBit(v->vehicle_flags, VF_AUTOFILL_PRES_WAIT_TIME); maedhros@7066: } rubidium@10341: return; rubidium@8970: } maedhros@7066: rubidium@10341: if (just_started) return; rubidium@10341: 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@9941: for (v = v->FirstShared(); v != NULL; v = v->NextShared()) { rubidium@7809: InvalidateWindow(WC_VEHICLE_TIMETABLE, v->index); rubidium@7809: } maedhros@6981: }