1
0
Fork 0

(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.

release/0.7
rubidium 2008-04-06 07:22:26 +00:00
parent e68b2088ce
commit 8cd1795fe3
7 changed files with 56 additions and 70 deletions

View File

@ -318,7 +318,7 @@ static void AiRestoreVehicleOrders(Vehicle *v, BackuppedOrders *bak)
if (bak->order == NULL) return; if (bak->order == NULL) return;
for (uint i = 0; !bak->order[i].IsType(OT_NOTHING); i++) { 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; break;
} }
} }
@ -2558,7 +2558,7 @@ handle_nocash:
if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0)) if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
order.flags |= OFB_FULL_LOAD; 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); 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)) if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
order.flags |= OFB_FULL_LOAD; 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); 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)) if (_players_ai[p->index].num_want_fullload != 0 && (is_pass || i == 0))
order.flags |= OFB_FULL_LOAD; 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); DoCommand(0, loco_id, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);

View File

@ -1185,20 +1185,20 @@ static void AiNew_State_GiveOrders(Player *p)
if (_patches.gotodepot) { if (_patches.gotodepot) {
idx = 0; idx = 0;
order.MakeGoToDepot(GetDepotByTile(_players_ainew[p->index].depot_tile)->index, true); 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; idx = 0;
order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].to_tile)); order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].to_tile));
if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver) if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].to_deliver)
order.flags |= OFB_FULL_LOAD; 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; idx = 0;
order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].from_tile)); order.MakeGoToStation(GetStationIndex(_players_ainew[p->index].from_tile));
if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver) if (_players_ainew[p->index].tbt == AI_TRUCK && _players_ainew[p->index].from_deliver)
order.flags |= OFB_FULL_LOAD; 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! // Start the engines!
_players_ainew[p->index].state = AI_STATE_START_VEHICLE; _players_ainew[p->index].state = AI_STATE_START_VEHICLE;

View File

@ -681,8 +681,8 @@ static uint32 VehicleGetVariable(const ResolverObject *object, byte variable, by
case 0x01: return MapOldSubType(v); case 0x01: return MapOldSubType(v);
case 0x04: return v->index; case 0x04: return v->index;
case 0x05: return GB(v->index, 8, 8); case 0x05: return GB(v->index, 8, 8);
case 0x0A: return PackOrder(&v->current_order); case 0x0A: return v->current_order.Pack();
case 0x0B: return GB(PackOrder(&v->current_order), 8, 8); case 0x0B: return GB(v->current_order.Pack(), 8, 8);
case 0x0C: return v->num_orders; case 0x0C: return v->num_orders;
case 0x0D: return v->cur_order_index; case 0x0D: return v->cur_order_index;
case 0x10: return v->load_unload_time_rem; case 0x10: return v->load_unload_time_rem;

View File

@ -504,7 +504,7 @@ static bool LoadOldOrder(LoadgameState *ls, int num)
{ {
if (!LoadChunk(ls, NULL, order_chunk)) return false; 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 /* 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 vehicle are behind eachother, with an invalid order (OT_NOTHING) as indication that

View File

@ -26,10 +26,6 @@ private:
friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); ///< Saving and loading the current order of vehicles. friend const struct SaveLoad *GetVehicleDescription(VehicleType vt); ///< Saving and loading the current order of vehicles.
friend void Load_VEHS(); ///< Loading of ancient vehicles. friend void Load_VEHS(); ///< Loading of ancient vehicles.
friend const struct SaveLoad *GetOrderDescription(); ///< Saving and loading of orders. 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 OrderTypeByte type; ///< The type of order
@ -48,6 +44,12 @@ public:
Order() : refit_cargo(CT_NO_REFIT) {} Order() : refit_cargo(CT_NO_REFIT) {}
~Order() { this->type = OT_NOTHING; } ~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. * Check if a Order really exists.
* @return true if the order is valid. * @return true if the order is valid.
@ -158,6 +160,14 @@ public:
* @return true if the type, flags and destination match. * @return true if the type, flags and destination match.
*/ */
bool Equals(const Order &other) const; 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() 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) #define FOR_VEHICLE_ORDERS(v, order) for (order = v->orders; order != NULL; order = order->next)
/* (Un)pack routines */ /* (Un)pack routines */
uint32 PackOrder(const Order *order);
Order UnpackOrder(uint32 packed);
Order UnpackOldOrder(uint16 packed); Order UnpackOldOrder(uint16 packed);
#endif /* ORDER_H */ #endif /* ORDER_H */

View File

@ -119,24 +119,32 @@ static bool HasOrderPoolFree(uint amount)
return false; 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; this->type = (OrderType)GB(packed, 0, 8);
order.type = (OrderType)GB(packed, 0, 8); this->flags = GB(packed, 8, 8);
order.flags = GB(packed, 8, 8); this->dest = GB(packed, 16, 16);
order.dest = GB(packed, 16, 16); this->next = NULL;
order.next = NULL; this->index = 0; // avoid compiler warning
order.index = 0; // avoid compiler warning this->refit_cargo = CT_NO_REFIT;
order.refit_cargo = CT_NO_REFIT; this->refit_subtype = 0;
order.refit_subtype = 0; this->wait_time = 0;
order.wait_time = 0; this->travel_time = 0;
order.travel_time = 0; }
return order;
/**
*
* Unpacks a order from savegames with version 4 and lower
*
*/
static Order UnpackVersion4Order(uint16 packed)
{
return Order(GB(packed, 8, 8) << 16 | GB(packed, 4, 4) << 8 | GB(packed, 0, 4));
} }
/** /**
@ -146,48 +154,19 @@ Order UnpackOrder(uint32 packed)
*/ */
Order UnpackOldOrder(uint16 packed) Order UnpackOldOrder(uint16 packed)
{ {
Order order; Order order = UnpackVersion4Order(packed);
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; * Sanity check
order.wait_time = 0; * TTD stores invalid orders as OT_NOTHING with non-zero flags/station
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)) { if (!order.IsValid() && (order.flags != 0 || order.dest != 0)) {
order.type = OT_DUMMY; order.MakeDummy();
order.flags = 0;
} }
return order; return order;
} }
/**
*
* Unpacks a order from savegames with version 4 and lower
*
*/
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.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;
}
/** /**
* *
* Updates the widgets of a vehicle which contains the order-data * Updates the widgets of a vehicle which contains the order-data
@ -276,7 +255,7 @@ CommandCost CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
Vehicle *v; Vehicle *v;
VehicleID veh = GB(p1, 0, 16); VehicleID veh = GB(p1, 0, 16);
VehicleOrderID sel_ord = GB(p1, 16, 16); VehicleOrderID sel_ord = GB(p1, 16, 16);
Order new_order = UnpackOrder(p2); Order new_order(p2);
if (!IsValidVehicleID(veh)) return CMD_ERROR; 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 * 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. */ * fails in test mode. By bypassing the test-mode, that no longer is a problem. */
for (uint i = 0; bak->order[i].IsValid(); i++) { 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)) { CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) {
break; break;
} }
@ -1589,8 +1568,7 @@ static void Load_ORDR()
SlArray(orders, len, SLE_UINT32); SlArray(orders, len, SLE_UINT32);
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
Order *order = new (i) Order(); new (i) Order(orders[i]);
order->AssignOrder(UnpackOrder(orders[i]));
} }
free(orders); free(orders);

View File

@ -373,7 +373,7 @@ static void OrdersPlaceObj(const Vehicle *v, TileIndex tile, Window *w)
cmd = GetOrderCmdFromTile(v, tile); cmd = GetOrderCmdFromTile(v, tile);
if (!cmd.IsValid()) return; 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++; if (WP(w, order_d).sel != -1) WP(w,order_d).sel++;
ResetObjectToPlace(); ResetObjectToPlace();
} }