# HG changeset patch # User rubidium # Date 2007-09-28 21:15:45 # Node ID 8b8bc2df3a7dfe79ad774dbc32a54354192c6d86 # Parent 321948f37c1d880de3325e7019c3c992ce7f62fc (svn r11184) -Codechange: cleanup the code related to backup orders. diff --git a/src/ai/default/default.cpp b/src/ai/default/default.cpp --- a/src/ai/default/default.cpp +++ b/src/ai/default/default.cpp @@ -305,7 +305,7 @@ static void AiRestoreVehicleOrders(Vehic static void AiHandleReplaceTrain(Player *p) { const Vehicle* v = p->ai.cur_veh; - BackuppedOrders orderbak[1]; + BackuppedOrders orderbak; EngineID veh; // wait until the vehicle reaches the depot. @@ -318,13 +318,13 @@ static void AiHandleReplaceTrain(Player if (veh != INVALID_ENGINE) { TileIndex tile; - BackupVehicleOrders(v, orderbak); + BackupVehicleOrders(v, &orderbak); tile = v->tile; if (CmdSucceeded(DoCommand(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) && CmdSucceeded(DoCommand(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE))) { VehicleID veh = _new_vehicle_id; - AiRestoreVehicleOrders(GetVehicle(veh), orderbak); + AiRestoreVehicleOrders(GetVehicle(veh), &orderbak); DoCommand(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN); DoCommand(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_SERVICE_INT); diff --git a/src/aircraft_gui.cpp b/src/aircraft_gui.cpp --- a/src/aircraft_gui.cpp +++ b/src/aircraft_gui.cpp @@ -95,7 +95,7 @@ void CcBuildAircraft(bool success, TileI if (v->tile == _backup_orders_tile) { _backup_orders_tile = 0; - RestoreVehicleOrders(v, _backup_orders_data); + RestoreVehicleOrders(v); } ShowVehicleViewWindow(v); } diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -928,7 +928,7 @@ static void DepotWndProc(Window *w, Wind if (is_engine) { _backup_orders_tile = v->tile; - BackupVehicleOrders(v, _backup_orders_data); + BackupVehicleOrders(v); } switch (v->type) { diff --git a/src/order.h b/src/order.h --- a/src/order.h +++ b/src/order.h @@ -116,18 +116,18 @@ struct Order : PoolItemnum_orders >= MAX_BACKUP_ORDER_COUNT) return_cmd_error(STR_8832_TOO_MANY_ORDERS); - if (v->type == VEH_SHIP && IsHumanPlayer(v->owner) && !_patches.new_pathfinding_all) { @@ -920,6 +916,10 @@ CommandCost CmdOrderRefit(TileIndex tile */ void BackupVehicleOrders(const Vehicle *v, BackuppedOrders *bak) { + /* Make sure we always have freed the stuff */ + free(bak->order); + bak->order = NULL; + /* Save general info */ bak->orderindex = v->cur_order_index; bak->service_interval = v->service_interval; @@ -939,13 +939,21 @@ void BackupVehicleOrders(const Vehicle * bak->clone = u->index; } else { /* Else copy the orders */ - Order *order, *dest; - - dest = bak->order; /* We do not have shared orders */ bak->clone = INVALID_VEHICLE; + + /* Count the number of orders */ + uint cnt = 0; + const Order *order; + FOR_VEHICLE_ORDERS(v, order) cnt++; + + /* Allocate memory for the orders plus an end-of-orders marker */ + bak->order = MallocT(cnt + 1); + + Order *dest = bak->order; + /* Copy the orders */ FOR_VEHICLE_ORDERS(v, order) { *dest = *order; @@ -961,12 +969,10 @@ void BackupVehicleOrders(const Vehicle * * Restore vehicle orders that are backupped via BackupVehicleOrders * */ -void RestoreVehicleOrders(const Vehicle* v, const BackuppedOrders* bak) +void RestoreVehicleOrders(const Vehicle *v, const BackuppedOrders *bak) { - uint i; - /* If we have a custom name, process that */ - if (bak->name[0] != 0) { + if (!StrEmpty(bak->name)) { _cmd_text = bak->name; DoCommandP(0, v->index, 0, NULL, CMD_NAME_VEHICLE); } @@ -981,7 +987,7 @@ void RestoreVehicleOrders(const Vehicle* * order number is one more than the current amount of orders, and because * 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 (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, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)) break; } diff --git a/src/roadveh_gui.cpp b/src/roadveh_gui.cpp --- a/src/roadveh_gui.cpp +++ b/src/roadveh_gui.cpp @@ -128,7 +128,7 @@ void CcBuildRoadVeh(bool success, TileIn v = GetVehicle(_new_vehicle_id); if (v->tile == _backup_orders_tile) { _backup_orders_tile = 0; - RestoreVehicleOrders(v, _backup_orders_data); + RestoreVehicleOrders(v); } ShowVehicleViewWindow(v); } diff --git a/src/ship_gui.cpp b/src/ship_gui.cpp --- a/src/ship_gui.cpp +++ b/src/ship_gui.cpp @@ -35,7 +35,7 @@ void CcBuildShip(bool success, TileIndex v = GetVehicle(_new_vehicle_id); if (v->tile == _backup_orders_tile) { _backup_orders_tile = 0; - RestoreVehicleOrders(v, _backup_orders_data); + RestoreVehicleOrders(v); } ShowVehicleViewWindow(v); } diff --git a/src/train_gui.cpp b/src/train_gui.cpp --- a/src/train_gui.cpp +++ b/src/train_gui.cpp @@ -54,7 +54,7 @@ void CcBuildLoco(bool success, TileIndex v = GetVehicle(_new_vehicle_id); if (tile == _backup_orders_tile) { _backup_orders_tile = 0; - RestoreVehicleOrders(v, _backup_orders_data); + RestoreVehicleOrders(v); } ShowVehicleViewWindow(v); }