# order_base.h

Go to the documentation of this file.

    00001 /* $Id: order_base.h 14828 2009-01-04 15:32:25Z smatz $ */
    00002
    00005 #ifndef ORDER_BASE_H
    00006 #define ORDER_BASE_H
    00007
    00008 #include "order_type.h"
    00009 #include "oldpool.h"
    00010 #include "core/bitmath_func.hpp"
    00011 #include "cargo_type.h"
    00012 #include "depot_type.h"
    00013 #include "station_type.h"
    00014 #include "vehicle_type.h"
    00015 #include "waypoint_type.h"
    00016
    00017 DECLARE_OLD_POOL(Order, Order, 6, 1000)
    00018 DECLARE_OLD_POOL(OrderList, OrderList, 4, 4000)
    00019
    00020 /* If you change this, keep in mind that it is saved on 3 places:
    00021  * - Load_ORDR, all the global orders
    00022  * - Vehicle -> current_order
    00023  * - REF_ORDER (all REFs are currently limited to 16 bits!!)
    00024  */
    00025 struct Order : PoolItem<Order, OrderID, &_Order_pool> {
    00026 private:
    00027   friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
    00028   friend void Load_VEHS();
    00029   friend const struct SaveLoad *GetOrderDescription();
    00030
    00031   uint8 type;
    00032   uint8 flags;
    00033   DestinationID dest;
    00034
    00035   CargoID refit_cargo;
    00036   byte refit_subtype;
    00037
    00038 public:
    00039   Order *next;
    00040
    00041   uint16 wait_time;
    00042   uint16 travel_time;
    00043
    00044   Order() : refit_cargo(CT_NO_REFIT) {}
    00045   ~Order() { this->type = OT_NOTHING; }
    00046
    00051   Order(uint32 packed);
    00052
    00057   inline bool IsValid() const { return this->type != OT_NOTHING; }
    00058
    00064   inline bool IsType(OrderType type) const { return this->GetType() == type; }
    00065
    00070   inline OrderType GetType() const { return (OrderType)GB(this->type, 0, 4); }
    00071
    00076   void Free();
    00077
    00082   void MakeGoToStation(StationID destination);
    00083
    00092   void MakeGoToDepot(DepotID destination, OrderDepotTypeFlags order, OrderDepotActionFlags action = ODATF_SERVICE_ONLY, CargoID cargo = CT_NO_REFIT, byte subtype = 0);
    00093
    00098   void MakeGoToWaypoint(WaypointID destination);
    00099
    00104   void MakeLoading(bool ordered);
    00105
    00109   void MakeLeaveStation();
    00110
    00114   void MakeDummy();
    00115
    00120   void MakeConditional(VehicleOrderID order);
    00121
    00127   inline DestinationID GetDestination() const { return this->dest; }
    00128
    00134   inline void SetDestination(DestinationID destination) { this->dest = destination; }
    00135
    00141   inline bool IsRefit() const { return this->refit_cargo < NUM_CARGO; }
    00142
    00148   inline CargoID GetRefitCargo() const { return this->refit_cargo; }
    00149
    00155   inline byte GetRefitSubtype() const { return this->refit_subtype; }
    00156
    00163   void SetRefit(CargoID cargo, byte subtype = 0);
    00164
    00166   inline OrderLoadFlags GetLoadType() const { return (OrderLoadFlags)GB(this->flags, 4, 4); }
    00168   inline OrderUnloadFlags GetUnloadType() const { return (OrderUnloadFlags)GB(this->flags, 0, 4); }
    00170   inline OrderNonStopFlags GetNonStopType() const { return (OrderNonStopFlags)GB(this->type, 6, 2); }
    00172   inline OrderDepotTypeFlags GetDepotOrderType() const { return (OrderDepotTypeFlags)GB(this->flags, 0, 4); }
    00174   inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)GB(this->flags, 4, 4); }
    00176   inline OrderConditionVariable GetConditionVariable() const { return (OrderConditionVariable)GB(this->dest, 11, 5); }
    00178   inline OrderConditionComparator GetConditionComparator() const { return (OrderConditionComparator)GB(this->type, 5, 3); }
    00180   inline VehicleOrderID GetConditionSkipToOrder() const { return this->flags; }
    00182   inline uint16 GetConditionValue() const { return GB(this->dest, 0, 11); }
    00183
    00185   inline void SetLoadType(OrderLoadFlags load_type) { SB(this->flags, 4, 4, load_type); }
    00187   inline void SetUnloadType(OrderUnloadFlags unload_type) { SB(this->flags, 0, 4, unload_type); }
    00189   inline void SetNonStopType(OrderNonStopFlags non_stop_type) { SB(this->type, 6, 2, non_stop_type); }
    00191   inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type) { SB(this->flags, 0, 4, depot_order_type); }
    00193   inline void SetDepotActionType(OrderDepotActionFlags depot_service_type) { SB(this->flags, 4, 4, depot_service_type); }
    00195   inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest, 11, 5, condition_variable); }
    00197   inline void SetConditionComparator(OrderConditionComparator condition_comparator) { SB(this->type, 5, 3, condition_comparator); }
    00199   inline void SetConditionSkipToOrder(VehicleOrderID order_id) { this->flags = order_id; }
    00201   inline void SetConditionValue(uint16 value) { SB(this->dest, 0, 11, value); }
    00202
    00203   bool ShouldStopAtStation(const Vehicle *v, StationID station) const;
    00204
    00206   inline bool IsCompletelyTimetabled() const
    00207   {
    00208     if (this->travel_time == 0 && !this->IsType(OT_CONDITIONAL)) return false;
    00209     if (this->wait_time == 0 && this->IsType(OT_GOTO_STATION) && !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) return false;
    00210     return true;
    00211   }
    00212
    00217   void AssignOrder(const Order &other);
    00218
    00224   bool Equals(const Order &other) const;
    00225
    00232   uint32 Pack() const;
    00233
    00238   void ConvertFromOldSavegame();
    00239 };
    00240
    00241 static inline VehicleOrderID GetMaxOrderIndex()
    00242 {
    00243   /* TODO - This isn't the real content of the function, but
    00244    *  with the new pool-system this will be replaced with one that
    00245    *  _really_ returns the highest index. Now it just returns
    00246    *  the next safe value we are sure about everything is below.
    00247    */
    00248   return GetOrderPoolSize() - 1;
    00249 }
    00250
    00251 static inline VehicleOrderID GetNumOrders()
    00252 {
    00253   return GetOrderPoolSize();
    00254 }
    00255
    00259 struct OrderList : PoolItem<OrderList, OrderListID, &_OrderList_pool> {
    00260 private:
    00261   friend void AfterLoadVehicles(bool part_of_load);
    00262   friend const struct SaveLoad *GetOrderListDescription();
    00263
    00264   Order *first;
    00265   VehicleOrderID num_orders;
    00266   uint num_vehicles;
    00267   Vehicle *first_shared;
    00268
    00269   uint timetable_duration;
    00270
    00271 public:
    00273   OrderList()
    00274     : first(NULL), num_orders(INVALID_VEH_ORDER_ID), num_vehicles(0), first_shared(NULL),
    00275       timetable_duration(0) { }
    00276
    00281   OrderList(Order *chain, Vehicle *v);
    00282
    00284   ~OrderList() { this->num_orders = INVALID_VEH_ORDER_ID; }
    00285
    00287   inline bool IsValid() const { return this->num_orders != INVALID_VEH_ORDER_ID; }
    00288
    00293   inline Order *GetFirstOrder() const { return this->first; }
    00294
    00300   Order *GetOrderAt(int index) const;
    00301
    00306   inline Order *GetLastOrder() const { return this->GetOrderAt(this->num_orders - 1); }
    00307
    00311   inline VehicleOrderID GetNumOrders() const { return this->num_orders; }
    00312
    00317   void InsertOrderAt(Order *new_order, int index);
    00318
    00323   void DeleteOrderAt(int index);
    00324
    00329   void MoveOrder(int from, int to);
    00330
    00335   inline bool IsShared() const { return this->num_vehicles > 1; };
    00336
    00341   inline Vehicle *GetFirstSharedVehicle() const { return this->first_shared; }
    00342
    00347   inline uint GetNumVehicles() const { return this->num_vehicles; }
    00348
    00353   bool IsVehicleInSharedOrdersList(const Vehicle *v) const;
    00354
    00360   int GetPositionInSharedOrderList(const Vehicle *v) const;
    00361
    00368   inline void AddVehicle(Vehicle *v) { ++this->num_vehicles; }
    00369
    00375   void RemoveVehicle(Vehicle *v);
    00376
    00381   bool IsCompleteTimetable() const;
    00382
    00387   inline int GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? (int)this->timetable_duration : -1; }
    00388
    00393   inline int GetTimetableDurationIncomplete() const { return this->timetable_duration; }
    00394
    00399   void UpdateOrderTimetable(int delta) { this->timetable_duration += delta; }
    00400
    00404   void ResetOrderTimetable() { this->timetable_duration = 0; }
    00405
    00411   void FreeChain(bool keep_orderlist = false);
    00412
    00416   void DebugCheckSanity() const;
    00417 };
    00418
    00419 static inline bool IsValidOrderListID(uint index)
    00420 {
    00421   return index < GetOrderListPoolSize() && GetOrderList(index)->IsValid();
    00422 }
    00423
    00424 #define FOR_ALL_ORDERS_FROM(order, start) for (order = GetOrder(start); order != NULL; order = (order->index + 1U < GetOrderPoolSize()) ? GetOrder(order->index + 1U) : NULL) if (order->IsValid())
    00425 #define FOR_ALL_ORDERS(order) FOR_ALL_ORDERS_FROM(order, 0)
    00426
    00427
    00428 #define FOR_VEHICLE_ORDERS(v, order) for (order = (v->orders.list == NULL) ? NULL : v->orders.list->GetFirstOrder(); order != NULL; order = order->next)
    00429
    00430
    00431 #define FOR_ALL_ORDER_LISTS_FROM(ol, start) for (ol = GetOrderList(start); ol != NULL; ol = (ol->index + 1U < GetOrderListPoolSize()) ? GetOrderList(ol->index + 1U) : NULL) if (ol->IsValid())
    00432 #define FOR_ALL_ORDER_LISTS(ol) FOR_ALL_ORDER_LISTS_FROM(ol, 0)
    00433
    00434 #endif /* ORDER_H */