1 /* 2 * This file is part of OpenTTD. 3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. 4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. 6 */ 7 8 /** @file order_base.h Base class for orders. */ 9 10 #ifndef ORDER_BASE_H 11 #define ORDER_BASE_H 12 13 #include "order_type.h" 14 #include "core/pool_type.hpp" 15 #include "core/bitmath_func.hpp" 16 #include "cargo_type.h" 17 #include "depot_type.h" 18 #include "station_type.h" 19 #include "vehicle_type.h" 20 #include "date_type.h" 21 #include "saveload/saveload.h" 22 23 typedef Pool<Order, OrderID, 256, 0xFF0000> OrderPool; 24 typedef Pool<OrderList, OrderListID, 128, 64000> OrderListPool; 25 extern OrderPool _order_pool; 26 extern OrderListPool _orderlist_pool; 27 28 /* If you change this, keep in mind that it is saved on 3 places: 29 * - Load_ORDR, all the global orders 30 * - Vehicle -> current_order 31 * - REF_ORDER (all REFs are currently limited to 16 bits!!) 32 */ 33 struct Order : OrderPool::PoolItem<&_order_pool> { 34 private: 35 friend struct VEHSChunkHandler; ///< Loading of ancient vehicles. 36 friend SaveLoadTable GetOrderDescription(); ///< Saving and loading of orders. 37 /* So we can use private/protected variables in the saveload code */ 38 friend class SlVehicleCommon; 39 friend class SlVehicleDisaster; 40 41 uint8 type; ///< The type of order + non-stop flags 42 uint8 flags; ///< Load/unload types, depot order/action types. 43 DestinationID dest; ///< The destination of the order. 44 45 CargoID refit_cargo; ///< Refit CargoID 46 47 uint16 wait_time; ///< How long in ticks to wait at the destination. 48 uint16 travel_time; ///< How long in ticks the journey to this destination should take. 49 uint16 max_speed; ///< How fast the vehicle may go on the way to the destination. 50 51 public: 52 Order *next; ///< Pointer to next order. If nullptr, end of list 53 OrderOrder54 Order() : flags(0), refit_cargo(CT_NO_REFIT), max_speed(UINT16_MAX) {} 55 ~Order(); 56 57 Order(uint32 packed); 58 59 /** 60 * Check whether this order is of the given type. 61 * @param type the type to check against. 62 * @return true if the order matches. 63 */ IsTypeOrder64 inline bool IsType(OrderType type) const { return this->GetType() == type; } 65 66 /** 67 * Get the type of order of this order. 68 * @return the order type. 69 */ GetTypeOrder70 inline OrderType GetType() const { return (OrderType)GB(this->type, 0, 4); } 71 72 void Free(); 73 74 void MakeGoToStation(StationID destination); 75 void MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type = ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS, OrderDepotActionFlags action = ODATF_SERVICE_ONLY, CargoID cargo = CT_NO_REFIT); 76 void MakeGoToWaypoint(StationID destination); 77 void MakeLoading(bool ordered); 78 void MakeLeaveStation(); 79 void MakeDummy(); 80 void MakeConditional(VehicleOrderID order); 81 void MakeImplicit(StationID destination); 82 83 /** 84 * Is this a 'goto' order with a real destination? 85 * @return True if the type is either #OT_GOTO_WAYPOINT, #OT_GOTO_DEPOT or #OT_GOTO_STATION. 86 */ IsGotoOrderOrder87 inline bool IsGotoOrder() const 88 { 89 return IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION); 90 } 91 92 /** 93 * Gets the destination of this order. 94 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION). 95 * @return the destination of the order. 96 */ GetDestinationOrder97 inline DestinationID GetDestination() const { return this->dest; } 98 99 /** 100 * Sets the destination of this order. 101 * @param destination the new destination of the order. 102 * @pre IsType(OT_GOTO_WAYPOINT) || IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION). 103 */ SetDestinationOrder104 inline void SetDestination(DestinationID destination) { this->dest = destination; } 105 106 /** 107 * Is this order a refit order. 108 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION) 109 * @return true if a refit should happen. 110 */ IsRefitOrder111 inline bool IsRefit() const { return this->refit_cargo < NUM_CARGO || this->refit_cargo == CT_AUTO_REFIT; } 112 113 /** 114 * Is this order a auto-refit order. 115 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION) 116 * @return true if a auto-refit should happen. 117 */ IsAutoRefitOrder118 inline bool IsAutoRefit() const { return this->refit_cargo == CT_AUTO_REFIT; } 119 120 /** 121 * Get the cargo to to refit to. 122 * @pre IsType(OT_GOTO_DEPOT) || IsType(OT_GOTO_STATION) 123 * @return the cargo type. 124 */ GetRefitCargoOrder125 inline CargoID GetRefitCargo() const { return this->refit_cargo; } 126 127 void SetRefit(CargoID cargo); 128 129 /** How must the consist be loaded? */ GetLoadTypeOrder130 inline OrderLoadFlags GetLoadType() const { return (OrderLoadFlags)GB(this->flags, 4, 3); } 131 /** How must the consist be unloaded? */ GetUnloadTypeOrder132 inline OrderUnloadFlags GetUnloadType() const { return (OrderUnloadFlags)GB(this->flags, 0, 3); } 133 /** At which stations must we stop? */ GetNonStopTypeOrder134 inline OrderNonStopFlags GetNonStopType() const { return (OrderNonStopFlags)GB(this->type, 6, 2); } 135 /** Where must we stop at the platform? */ GetStopLocationOrder136 inline OrderStopLocation GetStopLocation() const { return (OrderStopLocation)GB(this->type, 4, 2); } 137 /** What caused us going to the depot? */ GetDepotOrderTypeOrder138 inline OrderDepotTypeFlags GetDepotOrderType() const { return (OrderDepotTypeFlags)GB(this->flags, 0, 3); } 139 /** What are we going to do when in the depot. */ GetDepotActionTypeOrder140 inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)GB(this->flags, 4, 3); } 141 /** What variable do we have to compare? */ GetConditionVariableOrder142 inline OrderConditionVariable GetConditionVariable() const { return (OrderConditionVariable)GB(this->dest, 11, 5); } 143 /** What is the comparator to use? */ GetConditionComparatorOrder144 inline OrderConditionComparator GetConditionComparator() const { return (OrderConditionComparator)GB(this->type, 5, 3); } 145 /** Get the order to skip to. */ GetConditionSkipToOrderOrder146 inline VehicleOrderID GetConditionSkipToOrder() const { return this->flags; } 147 /** Get the value to base the skip on. */ GetConditionValueOrder148 inline uint16 GetConditionValue() const { return GB(this->dest, 0, 11); } 149 150 /** Set how the consist must be loaded. */ SetLoadTypeOrder151 inline void SetLoadType(OrderLoadFlags load_type) { SB(this->flags, 4, 3, load_type); } 152 /** Set how the consist must be unloaded. */ SetUnloadTypeOrder153 inline void SetUnloadType(OrderUnloadFlags unload_type) { SB(this->flags, 0, 3, unload_type); } 154 /** Set whether we must stop at stations or not. */ SetNonStopTypeOrder155 inline void SetNonStopType(OrderNonStopFlags non_stop_type) { SB(this->type, 6, 2, non_stop_type); } 156 /** Set where we must stop at the platform. */ SetStopLocationOrder157 inline void SetStopLocation(OrderStopLocation stop_location) { SB(this->type, 4, 2, stop_location); } 158 /** Set the cause to go to the depot. */ SetDepotOrderTypeOrder159 inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type) { SB(this->flags, 0, 3, depot_order_type); } 160 /** Set what we are going to do in the depot. */ SetDepotActionTypeOrder161 inline void SetDepotActionType(OrderDepotActionFlags depot_service_type) { SB(this->flags, 4, 3, depot_service_type); } 162 /** Set variable we have to compare. */ SetConditionVariableOrder163 inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest, 11, 5, condition_variable); } 164 /** Set the comparator to use. */ SetConditionComparatorOrder165 inline void SetConditionComparator(OrderConditionComparator condition_comparator) { SB(this->type, 5, 3, condition_comparator); } 166 /** Get the order to skip to. */ SetConditionSkipToOrderOrder167 inline void SetConditionSkipToOrder(VehicleOrderID order_id) { this->flags = order_id; } 168 /** Set the value to base the skip on. */ SetConditionValueOrder169 inline void SetConditionValue(uint16 value) { SB(this->dest, 0, 11, value); } 170 171 /* As conditional orders write their "skip to" order all over the flags, we cannot check the 172 * flags to find out if timetabling is enabled. However, as conditional orders are never 173 * autofilled we can be sure that any non-zero values for their wait_time and travel_time are 174 * explicitly set (but travel_time is actually unused for conditionals). */ 175 176 /** Does this order have an explicit wait time set? */ IsWaitTimetabledOrder177 inline bool IsWaitTimetabled() const { return this->IsType(OT_CONDITIONAL) ? this->wait_time > 0 : HasBit(this->flags, 3); } 178 /** Does this order have an explicit travel time set? */ IsTravelTimetabledOrder179 inline bool IsTravelTimetabled() const { return this->IsType(OT_CONDITIONAL) ? this->travel_time > 0 : HasBit(this->flags, 7); } 180 181 /** Get the time in ticks a vehicle should wait at the destination or 0 if it's not timetabled. */ GetTimetabledWaitOrder182 inline uint16 GetTimetabledWait() const { return this->IsWaitTimetabled() ? this->wait_time : 0; } 183 /** Get the time in ticks a vehicle should take to reach the destination or 0 if it's not timetabled. */ GetTimetabledTravelOrder184 inline uint16 GetTimetabledTravel() const { return this->IsTravelTimetabled() ? this->travel_time : 0; } 185 /** Get the time in ticks a vehicle will probably wait at the destination (timetabled or not). */ GetWaitTimeOrder186 inline uint16 GetWaitTime() const { return this->wait_time; } 187 /** Get the time in ticks a vehicle will probably take to reach the destination (timetabled or not). */ GetTravelTimeOrder188 inline uint16 GetTravelTime() const { return this->travel_time; } 189 190 /** 191 * Get the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the 192 * destination. 193 * @return maximum speed. 194 */ GetMaxSpeedOrder195 inline uint16 GetMaxSpeed() const { return this->max_speed; } 196 197 /** Set if the wait time is explicitly timetabled (unless the order is conditional). */ SetWaitTimetabledOrder198 inline void SetWaitTimetabled(bool timetabled) { if (!this->IsType(OT_CONDITIONAL)) SB(this->flags, 3, 1, timetabled ? 1 : 0); } 199 /** Set if the travel time is explicitly timetabled (unless the order is conditional). */ SetTravelTimetabledOrder200 inline void SetTravelTimetabled(bool timetabled) { if (!this->IsType(OT_CONDITIONAL)) SB(this->flags, 7, 1, timetabled ? 1 : 0); } 201 202 /** 203 * Set the time in ticks to wait at the destination. 204 * @param time Time to set as wait time. 205 */ SetWaitTimeOrder206 inline void SetWaitTime(uint16 time) { this->wait_time = time; } 207 208 /** 209 * Set the time in ticks to take for travelling to the destination. 210 * @param time Time to set as travel time. 211 */ SetTravelTimeOrder212 inline void SetTravelTime(uint16 time) { this->travel_time = time; } 213 214 /** 215 * Set the maxmimum speed in km-ish/h a vehicle is allowed to reach on the way to the 216 * destination. 217 * @param speed Speed to be set. 218 */ SetMaxSpeedOrder219 inline void SetMaxSpeed(uint16 speed) { this->max_speed = speed; } 220 221 bool ShouldStopAtStation(const Vehicle *v, StationID station) const; 222 bool CanLoadOrUnload() const; 223 bool CanLeaveWithCargo(bool has_cargo) const; 224 225 TileIndex GetLocation(const Vehicle *v, bool airport = false) const; 226 227 /** Checks if travel_time and wait_time apply to this order and if they are timetabled. */ IsCompletelyTimetabledOrder228 inline bool IsCompletelyTimetabled() const 229 { 230 if (!this->IsTravelTimetabled() && !this->IsType(OT_CONDITIONAL)) return false; 231 if (!this->IsWaitTimetabled() && this->IsType(OT_GOTO_STATION) && 232 !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) { 233 return false; 234 } 235 return true; 236 } 237 238 void AssignOrder(const Order &other); 239 bool Equals(const Order &other) const; 240 241 uint32 Pack() const; 242 uint16 MapOldOrder() const; 243 void ConvertFromOldSavegame(); 244 }; 245 246 void InsertOrder(Vehicle *v, Order *new_o, VehicleOrderID sel_ord); 247 void DeleteOrder(Vehicle *v, VehicleOrderID sel_ord); 248 249 /** 250 * Shared order list linking together the linked list of orders and the list 251 * of vehicles sharing this order list. 252 */ 253 struct OrderList : OrderListPool::PoolItem<&_orderlist_pool> { 254 private: 255 friend void AfterLoadVehicles(bool part_of_load); ///< For instantiating the shared vehicle chain 256 friend SaveLoadTable GetOrderListDescription(); ///< Saving and loading of order lists. 257 258 StationID GetBestLoadableNext(const Vehicle *v, const Order *o1, const Order *o2) const; 259 260 Order *first; ///< First order of the order list. 261 VehicleOrderID num_orders; ///< NOSAVE: How many orders there are in the list. 262 VehicleOrderID num_manual_orders; ///< NOSAVE: How many manually added orders are there in the list. 263 uint num_vehicles; ///< NOSAVE: Number of vehicles that share this order list. 264 Vehicle *first_shared; ///< NOSAVE: pointer to the first vehicle in the shared order chain. 265 266 Ticks timetable_duration; ///< NOSAVE: Total timetabled duration of the order list. 267 Ticks total_duration; ///< NOSAVE: Total (timetabled or not) duration of the order list. 268 269 public: 270 /** Default constructor producing an invalid order list. */ 271 OrderList(VehicleOrderID num_orders = INVALID_VEH_ORDER_ID) firstOrderList272 : first(nullptr), num_orders(num_orders), num_manual_orders(0), num_vehicles(0), first_shared(nullptr), 273 timetable_duration(0), total_duration(0) { } 274 275 /** 276 * Create an order list with the given order chain for the given vehicle. 277 * @param chain pointer to the first order of the order chain 278 * @param v any vehicle using this orderlist 279 */ OrderListOrderList280 OrderList(Order *chain, Vehicle *v) { this->Initialize(chain, v); } 281 282 /** Destructor. Invalidates OrderList for re-usage by the pool. */ ~OrderListOrderList283 ~OrderList() {} 284 285 void Initialize(Order *chain, Vehicle *v); 286 287 void RecalculateTimetableDuration(); 288 289 /** 290 * Get the first order of the order chain. 291 * @return the first order of the chain. 292 */ GetFirstOrderOrderList293 inline Order *GetFirstOrder() const { return this->first; } 294 295 Order *GetOrderAt(int index) const; 296 297 /** 298 * Get the last order of the order chain. 299 * @return the last order of the chain. 300 */ GetLastOrderOrderList301 inline Order *GetLastOrder() const { return this->GetOrderAt(this->num_orders - 1); } 302 303 /** 304 * Get the order after the given one or the first one, if the given one is the 305 * last one. 306 * @param curr Order to find the next one for. 307 * @return Next order. 308 */ GetNextOrderList309 inline const Order *GetNext(const Order *curr) const { return (curr->next == nullptr) ? this->GetFirstOrder() : curr->next; } 310 311 /** 312 * Get number of orders in the order list. 313 * @return number of orders in the chain. 314 */ GetNumOrdersOrderList315 inline VehicleOrderID GetNumOrders() const { return this->num_orders; } 316 317 /** 318 * Get number of manually added orders in the order list. 319 * @return number of manual orders in the chain. 320 */ GetNumManualOrdersOrderList321 inline VehicleOrderID GetNumManualOrders() const { return this->num_manual_orders; } 322 323 StationIDStack GetNextStoppingStation(const Vehicle *v, const Order *first = nullptr, uint hops = 0) const; 324 const Order *GetNextDecisionNode(const Order *next, uint hops) const; 325 326 void InsertOrderAt(Order *new_order, int index); 327 void DeleteOrderAt(int index); 328 void MoveOrder(int from, int to); 329 330 /** 331 * Is this a shared order list? 332 * @return whether this order list is shared among multiple vehicles 333 */ IsSharedOrderList334 inline bool IsShared() const { return this->num_vehicles > 1; }; 335 336 /** 337 * Get the first vehicle of this vehicle chain. 338 * @return the first vehicle of the chain. 339 */ GetFirstSharedVehicleOrderList340 inline Vehicle *GetFirstSharedVehicle() const { return this->first_shared; } 341 342 /** 343 * Return the number of vehicles that share this orders list 344 * @return the count of vehicles that use this shared orders list 345 */ GetNumVehiclesOrderList346 inline uint GetNumVehicles() const { return this->num_vehicles; } 347 348 bool IsVehicleInSharedOrdersList(const Vehicle *v) const; 349 int GetPositionInSharedOrderList(const Vehicle *v) const; 350 351 /** 352 * Adds the given vehicle to this shared order list. 353 * @note This is supposed to be called after the vehicle has been inserted 354 * into the shared vehicle chain. 355 * @param v vehicle to add to the list 356 */ AddVehicleOrderList357 inline void AddVehicle(Vehicle *v) { ++this->num_vehicles; } 358 359 void RemoveVehicle(Vehicle *v); 360 361 bool IsCompleteTimetable() const; 362 363 /** 364 * Gets the total duration of the vehicles timetable or INVALID_TICKS is the timetable is not complete. 365 * @return total timetable duration or INVALID_TICKS for incomplete timetables 366 */ GetTimetableTotalDurationOrderList367 inline Ticks GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration : INVALID_TICKS; } 368 369 /** 370 * Gets the known duration of the vehicles timetable even if the timetable is not complete. 371 * @return known timetable duration 372 */ GetTimetableDurationIncompleteOrderList373 inline Ticks GetTimetableDurationIncomplete() const { return this->timetable_duration; } 374 375 /** 376 * Gets the known duration of the vehicles orders, timetabled or not. 377 * @return known order duration. 378 */ GetTotalDurationOrderList379 inline Ticks GetTotalDuration() const { return this->total_duration; } 380 381 /** 382 * Must be called if an order's timetable is changed to update internal book keeping. 383 * @param delta By how many ticks has the timetable duration changed 384 */ UpdateTimetableDurationOrderList385 void UpdateTimetableDuration(Ticks delta) { this->timetable_duration += delta; } 386 387 /** 388 * Must be called if an order's timetable is changed to update internal book keeping. 389 * @param delta By how many ticks has the total duration changed 390 */ UpdateTotalDurationOrderList391 void UpdateTotalDuration(Ticks delta) { this->total_duration += delta; } 392 393 void FreeChain(bool keep_orderlist = false); 394 395 void DebugCheckSanity() const; 396 }; 397 398 #endif /* ORDER_BASE_H */ 399