Mercurial > hg > openttd
changeset 8839:ebb630745ebc draft
(svn r12587) -Codechange: unduplicate some code in the Unpack*Order functions and move the 'normal' case Pack/Unpack to Order.
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sun, 06 Apr 2008 07:22:26 +0000 |
parents | db9c6b0c9f07 |
children | c3d2f0eb69a1 |
files | src/ai/default/default.cpp src/ai/trolly/trolly.cpp src/newgrf_engine.cpp src/oldloader.cpp src/order_base.h src/order_cmd.cpp src/order_gui.cpp |
diffstat | 7 files changed, 56 insertions(+), 70 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -318,7 +318,7 @@ 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 @@ 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 @@ 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 @@ 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);
--- a/src/ai/trolly/trolly.cpp +++ b/src/ai/trolly/trolly.cpp @@ -1185,20 +1185,20 @@ 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;
--- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -681,8 +681,8 @@ 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;
--- a/src/oldloader.cpp +++ b/src/oldloader.cpp @@ -504,7 +504,7 @@ { 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
--- a/src/order_base.h +++ b/src/order_base.h @@ -26,10 +26,6 @@ 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 @@ ~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 @@ * @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 @@ #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 */
--- a/src/order_cmd.cpp +++ b/src/order_cmd.cpp @@ -119,24 +119,32 @@ 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 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,23 +154,14 @@ */ 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 order = UnpackVersion4Order(packed); - 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 + /* + * 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; + order.MakeDummy(); } return order; @@ -170,26 +169,6 @@ /** * - * 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 * */ @@ -276,7 +255,7 @@ 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 @@ * 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 @@ 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);
--- a/src/order_gui.cpp +++ b/src/order_gui.cpp @@ -373,7 +373,7 @@ 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(); }