summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-04-06 07:22:26 +0000
committerrubidium <rubidium@openttd.org>2008-04-06 07:22:26 +0000
commit8cd1795fe32fc2afaa75f48b8b62ff992bf8f618 (patch)
tree94d7efcddd239260e27551baae6bc2aebe3a5f14
parente68b2088ce090c1eaf182e120195d8453f4f02c2 (diff)
downloadopenttd-8cd1795fe32fc2afaa75f48b8b62ff992bf8f618.tar.xz
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
-rw-r--r--src/ai/default/default.cpp8
-rw-r--r--src/ai/trolly/trolly.cpp6
-rw-r--r--src/newgrf_engine.cpp4
-rw-r--r--src/oldloader.cpp2
-rw-r--r--src/order_base.h20
-rw-r--r--src/order_cmd.cpp82
-rw-r--r--src/order_gui.cpp2
7 files changed, 55 insertions, 69 deletions
diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp
index dd8002d49..9436fd6ae 100644
--- a/src/ai/default/default.cpp
+++ b/src/ai/default/default.cpp
@@ -318,7 +318,7 @@ static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
if (bak->order == NULL) return;
for (uint i = 0; !bak->order[i].IsType(OT_NOTHING); i++) {
- if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
+ if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK))
break;
}
}
@@ -2558,7 +2558,7 @@ handle_nocash:
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
order.flags |= OFB_FULL_LOAD;
- DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+ DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
}
DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_TRAIN);
@@ -3293,7 +3293,7 @@ static void AiStateBuildRoadVehicles(Player *p)
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
order.flags |= OFB_FULL_LOAD;
- DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+ DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
}
DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
@@ -3572,7 +3572,7 @@ static void AiStateBuildAircraftVehicles(Player *p)
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
order.flags |= OFB_FULL_LOAD;
- DoCommand(0, loco_id + (i << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+ DoCommand(0, loco_id + (i << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
}
DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
diff --git a/src/ai/trolly/trolly.cpp b/src/ai/trolly/trolly.cpp
index 30714aed0..2876d240e 100644
--- a/src/ai/trolly/trolly.cpp
+++ b/src/ai/trolly/trolly.cpp
@@ -1185,20 +1185,20 @@ static void AiNew_State_GiveOrders(Player *p)
if (_patches.gotodepot) {
idx = 0;
order.MakeGoToDepot(GetDepotByTile(_players_ainew[p->index].depot_tile)->index, true);
- AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+ AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
}
idx = 0;
order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].to_tile));
if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver)
order.flags |= OFB_FULL_LOAD;
- AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+ AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
idx = 0;
order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].from_tile));
if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver)
order.flags |= OFB_FULL_LOAD;
- AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), PackOrder(&order), DC_EXEC, CMD_INSERT_ORDER);
+ AI_DoCommand(0, _players_ainew[p->index].veh_id + (idx << 16), order.Pack(), DC_EXEC, CMD_INSERT_ORDER);
// Start the engines!
_players_ainew[p->index].state = AI_STATE_START_VEHICLE;
diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp
index b438d838d..e56c2dda2 100644
--- a/src/newgrf_engine.cpp
+++ b/src/newgrf_engine.cpp
@@ -681,8 +681,8 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by
case 0x01: return MapOldSubType(v);
case 0x04: return v->index;
case 0x05: return GB(v->index, 8, 8);
- case 0x0A: return PackOrder(&v->current_order);
- case 0x0B: return GB(PackOrder(&v->current_order), 8, 8);
+ case 0x0A: return v->current_order.Pack();
+ case 0x0B: return GB(v->current_order.Pack(), 8, 8);
case 0x0C: return v->num_orders;
case 0x0D: return v->cur_order_index;
case 0x10: return v->load_unload_time_rem;
diff --git a/src/oldloader.cpp b/src/oldloader.cpp
index feaae2149..be09da92f 100644
--- a/src/oldloader.cpp
+++ b/src/oldloader.cpp
@@ -504,7 +504,7 @@ static bool LoadOldOrder(LoadgameState *ls, int num)
{
if (!LoadChunk(ls, NULL, order_chunk)) return false;
- (new (num) Order())->AssignOrder(UnpackOldOrder(_old_order));
+ new (num) Order(UnpackOldOrder(_old_order));
/* Relink the orders to eachother (in TTD(Patch) the orders for one
vehicle are behind eachother, with an invalid order (OT_NOTHING) as indication that
diff --git a/src/order_base.h b/src/order_base.h
index 8acea0069..9fc7514ac 100644
--- a/src/order_base.h
+++ b/src/order_base.h
@@ -26,10 +26,6 @@ private:
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); ///< Saving and loading the current order of vehicles.
friend void Load_VEHS(); ///< Loading of ancient vehicles.
friend const struct SaveLoad *GetOrderDescription(); ///< Saving and loading of orders.
- friend uint32 PackOrder(const Order *order); ///< 'Compressing' an order.
- friend Order UnpackOrder(uint32 packed); ///< 'Uncompressing' an order.
- friend Order UnpackOldOrder(uint16 packed); ///< 'Uncompressing' a loaded old order.
- friend Order UnpackVersion4Order(uint16 packed); ///< 'Uncompressing' a loaded ancient order.
OrderTypeByte type; ///< The type of order
@@ -49,6 +45,12 @@ public:
~Order() { this->type = OT_NOTHING; }
/**
+ * Create an order based on a packed representation of that order.
+ * @param packed the packed representation.
+ */
+ Order(uint32 packed);
+
+ /**
* Check if a Order really exists.
* @return true if the order is valid.
*/
@@ -158,6 +160,14 @@ public:
* @return true if the type, flags and destination match.
*/
bool Equals(const Order &other) const;
+
+ /**
+ * Pack this order into a 32 bits integer, or actually only
+ * the type, flags and destination.
+ * @return the packed representation.
+ * @note unpacking is done in the constructor.
+ */
+ uint32 Pack() const;
};
static inline VehicleOrderID GetMaxOrderIndex()
@@ -182,8 +192,6 @@ static inline VehicleOrderID GetNumOrders()
#define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)
/* (Un)pack routines */
-uint32 PackOrder(const Order *order);
-Order UnpackOrder(uint32 packed);
Order UnpackOldOrder(uint16 packed);
#endif /* ORDER_H */
diff --git a/src/order_cmd.cpp b/src/order_cmd.cpp
index 70482d453..3dc27670a 100644
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -119,72 +119,51 @@ static bool HasOrderPoolFree(uint amount)
return false;
}
-uint32 PackOrder(const Order *order)
+uint32 Order::Pack() const
{
- return order->dest << 16 | order->flags << 8 | order->type;
+ return this->dest << 16 | this->flags << 8 | this->type;
}
-Order UnpackOrder(uint32 packed)
+Order::Order(uint32 packed)
{
- Order order;
- order.type = (OrderType)GB(packed, 0, 8);
- order.flags = GB(packed, 8, 8);
- order.dest = GB(packed, 16, 16);
- order.next = NULL;
- order.index = 0; // avoid compiler warning
- order.refit_cargo = CT_NO_REFIT;
- order.refit_subtype = 0;
- order.wait_time = 0;
- order.travel_time = 0;
- return order;
+ this->type = (OrderType)GB(packed, 0, 8);
+ this->flags = GB(packed, 8, 8);
+ this->dest = GB(packed, 16, 16);
+ this->next = NULL;
+ this->index = 0; // avoid compiler warning
+ this->refit_cargo = CT_NO_REFIT;
+ this->refit_subtype = 0;
+ this->wait_time = 0;
+ this->travel_time = 0;
}
/**
*
- * Unpacks a order from savegames made with TTD(Patch)
+ * Unpacks a order from savegames with version 4 and lower
*
*/
-Order UnpackOldOrder(uint16 packed)
+static Order UnpackVersion4Order(uint16 packed)
{
- Order order;
- order.type = (OrderType)GB(packed, 0, 4);
- order.flags = GB(packed, 4, 4);
- order.dest = GB(packed, 8, 8);
- order.next = NULL;
-
- order.refit_cargo = CT_NO_REFIT;
- order.refit_subtype = 0;
- order.wait_time = 0;
- order.travel_time = 0;
- order.index = 0; // avoid compiler warning
-
- // Sanity check
- // TTD stores invalid orders as OT_NOTHING with non-zero flags/station
- if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) {
- order.type = OT_DUMMY;
- order.flags = 0;
- }
-
- return order;
+ return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
}
/**
*
- * Unpacks a order from savegames with version 4 and lower
+ * Unpacks a order from savegames made with TTD(Patch)
*
*/
-Order UnpackVersion4Order(uint16 packed)
+Order UnpackOldOrder(uint16 packed)
{
- Order order;
- order.type = (OrderType)GB(packed, 0, 4);
- order.flags = GB(packed, 4, 4);
- order.dest = GB(packed, 8, 8);
- order.next = NULL;
- order.index = 0; // avoid compiler warning
- order.refit_cargo = CT_NO_REFIT;
- order.refit_subtype = 0;
- order.wait_time = 0;
- order.travel_time = 0;
+ Order order = UnpackVersion4Order(packed);
+
+ /*
+ * Sanity check
+ * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
+ */
+ if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) {
+ order.MakeDummy();
+ }
+
return order;
}
@@ -276,7 +255,7 @@ CommandCost CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v;
VehicleID veh = GB(p1, 0, 16);
VehicleOrderID sel_ord = GB(p1, 16, 16);
- Order new_order = UnpackOrder(p2);
+ Order new_order(p2);
if (!IsValidVehicleID(veh)) return CMD_ERROR;
@@ -1099,7 +1078,7 @@ void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *bak)
* in network the commands are queued before send, the second insert always
* fails in test mode. By bypassing the test-mode, that no longer is a problem. */
for (uint i = 0; bak->order[i].IsValid(); i++) {
- if (!DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL,
+ if (!DoCommandP(0, v->index + (i << 16), bak->order[i].Pack(), NULL,
CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) {
break;
}
@@ -1589,8 +1568,7 @@ static void Load_ORDR()
SlArray(orders, len, SLE_UINT32);
for (i = 0; i < len; ++i) {
- Order *order = new (i) Order();
- order->AssignOrder(UnpackOrder(orders[i]));
+ new (i) Order(orders[i]);
}
free(orders);
diff --git a/src/order_gui.cpp b/src/order_gui.cpp
index 00d3a1fbc..1fe9149a1 100644
--- a/src/order_gui.cpp
+++ b/src/order_gui.cpp
@@ -373,7 +373,7 @@ static void OrdersPlaceObj(const Vehicle *v, TileIndex tile, Window *w)
cmd = GetOrderCmdFromTile(v, tile);
if (!cmd.IsValid()) return;
- if (DoCommandP(v->tile, v->index + (OrderGetSel(w) << 16), PackOrder(&cmd), NULL, CMD_INSERT_ORDER | CMD_MSG(STR_8833_CAN_T_INSERT_NEW_ORDER))) {
+ if (DoCommandP(v->tile, v->index + (OrderGetSel(w) << 16), cmd.Pack(), NULL, CMD_INSERT_ORDER | CMD_MSG(STR_8833_CAN_T_INSERT_NEW_ORDER))) {
if (WP(w, order_d).sel != -1) WP(w,order_d).sel++;
ResetObjectToPlace();
}