# order_base.h

Go to the documentation of this file.

    00001 /* $Id: order_base.h 19223 2010-02-23 23:26:37Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef ORDER_BASE_H
    00013 #define ORDER_BASE_H
    00014
    00015 #include "order_type.h"
    00016 #include "core/pool_type.hpp"
    00017 #include "core/bitmath_func.hpp"
    00018 #include "cargo_type.h"
    00019 #include "depot_type.h"
    00020 #include "station_type.h"
    00021 #include "vehicle_type.h"
    00022 #include "date_type.h"
    00023
    00024 typedef Pool<Order, OrderID, 256, 64000> OrderPool;
    00025 typedef Pool<OrderList, OrderListID, 128, 64000> OrderListPool;
    00026 extern OrderPool _order_pool;
    00027 extern OrderListPool _orderlist_pool;
    00028
    00029 /* If you change this, keep in mind that it is saved on 3 places:
    00030  * - Load_ORDR, all the global orders
    00031  * - Vehicle -> current_order
    00032  * - REF_ORDER (all REFs are currently limited to 16 bits!!)
    00033  */
    00034 struct Order : OrderPool::PoolItem<&_order_pool> {
    00035 private:
    00036   friend const struct SaveLoad *GetVehicleDescription(VehicleType vt);
    00037   friend void Load_VEHS();
    00038   friend const struct SaveLoad *GetOrderDescription();
    00039
    00040   uint8 type;
    00041   uint8 flags;
    00042   DestinationID dest;
    00043
    00044   CargoID refit_cargo;
    00045   byte refit_subtype;
    00046
    00047 public:
    00048   Order *next;
    00049
    00050   uint16 wait_time;
    00051   uint16 travel_time;
    00052
    00053   Order() : refit_cargo(CT_NO_REFIT) {}
    00054   ~Order() {}
    00055
    00060   Order(uint32 packed);
    00061
    00067   inline bool IsType(OrderType type) const { return this->GetType() == type; }
    00068
    00073   inline OrderType GetType() const { return (OrderType)GB(this->type, 0, 4); }
    00074
    00079   void Free();
    00080
    00085   void MakeGoToStation(StationID destination);
    00086
    00096   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, byte subtype = 0);
    00097
    00102   void MakeGoToWaypoint(StationID destination);
    00103
    00108   void MakeLoading(bool ordered);
    00109
    00113   void MakeLeaveStation();
    00114
    00118   void MakeDummy();
    00119
    00124   void MakeConditional(VehicleOrderID order);
    00125
    00131   inline DestinationID GetDestination() const { return this->dest; }
    00132
    00138   inline void SetDestination(DestinationID destination) { this->dest = destination; }
    00139
    00145   inline bool IsRefit() const { return this->refit_cargo < NUM_CARGO; }
    00146
    00152   inline CargoID GetRefitCargo() const { return this->refit_cargo; }
    00153
    00159   inline byte GetRefitSubtype() const { return this->refit_subtype; }
    00160
    00167   void SetRefit(CargoID cargo, byte subtype = 0);
    00168
    00170   inline OrderLoadFlags GetLoadType() const { return (OrderLoadFlags)GB(this->flags, 4, 4); }
    00172   inline OrderUnloadFlags GetUnloadType() const { return (OrderUnloadFlags)GB(this->flags, 0, 4); }
    00174   inline OrderNonStopFlags GetNonStopType() const { return (OrderNonStopFlags)GB(this->type, 6, 2); }
    00176   inline OrderStopLocation GetStopLocation() const { return (OrderStopLocation)GB(this->type, 4, 2); }
    00178   inline OrderDepotTypeFlags GetDepotOrderType() const { return (OrderDepotTypeFlags)GB(this->flags, 0, 4); }
    00180   inline OrderDepotActionFlags GetDepotActionType() const { return (OrderDepotActionFlags)GB(this->flags, 4, 4); }
    00182   inline OrderConditionVariable GetConditionVariable() const { return (OrderConditionVariable)GB(this->dest, 11, 5); }
    00184   inline OrderConditionComparator GetConditionComparator() const { return (OrderConditionComparator)GB(this->type, 5, 3); }
    00186   inline VehicleOrderID GetConditionSkipToOrder() const { return this->flags; }
    00188   inline uint16 GetConditionValue() const { return GB(this->dest, 0, 11); }
    00189
    00191   inline void SetLoadType(OrderLoadFlags load_type) { SB(this->flags, 4, 4, load_type); }
    00193   inline void SetUnloadType(OrderUnloadFlags unload_type) { SB(this->flags, 0, 4, unload_type); }
    00195   inline void SetNonStopType(OrderNonStopFlags non_stop_type) { SB(this->type, 6, 2, non_stop_type); }
    00197   inline void SetStopLocation(OrderStopLocation stop_location) { SB(this->type, 4, 2, stop_location); }
    00199   inline void SetDepotOrderType(OrderDepotTypeFlags depot_order_type) { SB(this->flags, 0, 4, depot_order_type); }
    00201   inline void SetDepotActionType(OrderDepotActionFlags depot_service_type) { SB(this->flags, 4, 4, depot_service_type); }
    00203   inline void SetConditionVariable(OrderConditionVariable condition_variable) { SB(this->dest, 11, 5, condition_variable); }
    00205   inline void SetConditionComparator(OrderConditionComparator condition_comparator) { SB(this->type, 5, 3, condition_comparator); }
    00207   inline void SetConditionSkipToOrder(VehicleOrderID order_id) { this->flags = order_id; }
    00209   inline void SetConditionValue(uint16 value) { SB(this->dest, 0, 11, value); }
    00210
    00211   bool ShouldStopAtStation(const Vehicle *v, StationID station) const;
    00212   TileIndex GetLocation(const Vehicle *v) const;
    00213
    00215   inline bool IsCompletelyTimetabled() const
    00216   {
    00217     if (this->travel_time == 0 && !this->IsType(OT_CONDITIONAL)) return false;
    00218     if (this->wait_time == 0 && this->IsType(OT_GOTO_STATION) && !(this->GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION)) return false;
    00219     return true;
    00220   }
    00221
    00226   void AssignOrder(const Order &other);
    00227
    00233   bool Equals(const Order &other) const;
    00234
    00241   uint32 Pack() const;
    00242
    00248   uint16 MapOldOrder() const;
    00249
    00254   void ConvertFromOldSavegame();
    00255 };
    00256
    00260 struct OrderList : OrderListPool::PoolItem<&_orderlist_pool> {
    00261 private:
    00262   friend void AfterLoadVehicles(bool part_of_load);
    00263   friend const struct SaveLoad *GetOrderListDescription();
    00264
    00265   Order *first;
    00266   VehicleOrderID num_orders;
    00267   uint num_vehicles;
    00268   Vehicle *first_shared;
    00269
    00270   Ticks timetable_duration;
    00271
    00272 public:
    00274   OrderList(VehicleOrderID num_orders = INVALID_VEH_ORDER_ID)
    00275     : first(NULL), num_orders(num_orders), num_vehicles(0), first_shared(NULL),
    00276       timetable_duration(0) { }
    00277
    00282   OrderList(Order *chain, Vehicle *v) { this->Initialize(chain, v); }
    00283
    00285   ~OrderList() {}
    00286
    00292   void Initialize(Order *chain, Vehicle *v);
    00293
    00298   inline Order *GetFirstOrder() const { return this->first; }
    00299
    00305   Order *GetOrderAt(int index) const;
    00306
    00311   inline Order *GetLastOrder() const { return this->GetOrderAt(this->num_orders - 1); }
    00312
    00316   inline VehicleOrderID GetNumOrders() const { return this->num_orders; }
    00317
    00322   void InsertOrderAt(Order *new_order, int index);
    00323
    00328   void DeleteOrderAt(int index);
    00329
    00334   void MoveOrder(int from, int to);
    00335
    00340   inline bool IsShared() const { return this->num_vehicles > 1; };
    00341
    00346   inline Vehicle *GetFirstSharedVehicle() const { return this->first_shared; }
    00347
    00352   inline uint GetNumVehicles() const { return this->num_vehicles; }
    00353
    00358   bool IsVehicleInSharedOrdersList(const Vehicle *v) const;
    00359
    00365   int GetPositionInSharedOrderList(const Vehicle *v) const;
    00366
    00373   inline void AddVehicle(Vehicle *v) { ++this->num_vehicles; }
    00374
    00380   void RemoveVehicle(Vehicle *v);
    00381
    00386   bool IsCompleteTimetable() const;
    00387
    00392   inline Ticks GetTimetableTotalDuration() const { return this->IsCompleteTimetable() ? this->timetable_duration : INVALID_TICKS; }
    00393
    00398   inline Ticks GetTimetableDurationIncomplete() const { return this->timetable_duration; }
    00399
    00404   void UpdateOrderTimetable(Ticks delta) { this->timetable_duration += delta; }
    00405
    00411   void FreeChain(bool keep_orderlist = false);
    00412
    00416   void DebugCheckSanity() const;
    00417 };
    00418
    00419 #define FOR_ALL_ORDERS_FROM(var, start) FOR_ALL_ITEMS_FROM(Order, order_index, var, start)
    00420 #define FOR_ALL_ORDERS(var) FOR_ALL_ORDERS_FROM(var, 0)
    00421
    00422
    00423 #define FOR_VEHICLE_ORDERS(v, order) for (order = (v->orders.list == NULL) ? NULL : v->orders.list->GetFirstOrder(); order != NULL; order = order->next)
    00424
    00425
    00426 #define FOR_ALL_ORDER_LISTS_FROM(var, start) FOR_ALL_ITEMS_FROM(OrderList, orderlist_index, var, start)
    00427 #define FOR_ALL_ORDER_LISTS(var) FOR_ALL_ORDER_LISTS_FROM(var, 0)
    00428
    00429 #endif /* ORDER_H */