summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-08-18 18:52:16 +0000
committerrubidium <rubidium@openttd.org>2010-08-18 18:52:16 +0000
commit019878118d11a6596eeab608c361dfea06c3f1ff (patch)
tree4675f56eaa3968827b3a98a1dd924f1de7c4bd13
parente2422945301452421ede848aa67a2ff47ed03d13 (diff)
downloadopenttd-019878118d11a6596eeab608c361dfea06c3f1ff.tar.xz
(svn r20545) -Codechange: make sure an OrderBackup gets cleared when the depot it belongs to gets removed, the depot window gets closed or when another vehicle gets sold in a depot
-rw-r--r--src/depot.cpp2
-rw-r--r--src/depot_gui.cpp8
-rw-r--r--src/order_backup.cpp23
-rw-r--r--src/order_backup.h27
-rw-r--r--src/station_cmd.cpp2
-rw-r--r--src/vehicle_gui.cpp3
6 files changed, 41 insertions, 24 deletions
diff --git a/src/depot.cpp b/src/depot.cpp
index f5c134b4b..f53a51ff0 100644
--- a/src/depot.cpp
+++ b/src/depot.cpp
@@ -11,6 +11,7 @@
#include "stdafx.h"
#include "depot_base.h"
+#include "order_backup.h"
#include "order_func.h"
#include "window_func.h"
#include "core/pool_func.hpp"
@@ -31,6 +32,7 @@ Depot::~Depot()
/* Delete the depot-window */
DeleteWindowById(WC_VEHICLE_DEPOT, this->xy);
+ OrderBackup::Reset(this->xy);
/* Delete the depot list */
WindowNumber wno = (this->index << 16) | VLW_DEPOT_LIST | GetTileOwner(this->xy);
diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp
index ff976fe77..e9ba9143f 100644
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -261,6 +261,7 @@ struct DepotWindow : Window {
~DepotWindow()
{
DeleteWindowById(WC_BUILD_VEHICLE, this->window_number);
+ OrderBackup::Reset(this->window_number);
}
/**
@@ -978,12 +979,9 @@ struct DepotWindow : Window {
bool is_engine = (v->type != VEH_TRAIN || Train::From(v)->IsFrontEngine());
- if (is_engine) {
- OrderBackup::Reset();
- new OrderBackup(v);
- }
+ if (is_engine) OrderBackup::Backup(v);
- if (!DoCommandP(v->tile, v->index | sell_cmd << 16, 0, GetCmdSellVeh(v->type)) && is_engine) OrderBackup::Reset();
+ if (!DoCommandP(v->tile, v->index | sell_cmd << 16, 0, GetCmdSellVeh(v->type)) && is_engine) OrderBackup::Reset(this->window_number);
break;
}
diff --git a/src/order_backup.cpp b/src/order_backup.cpp
index ccbe93abe..1518b2bde 100644
--- a/src/order_backup.cpp
+++ b/src/order_backup.cpp
@@ -61,7 +61,7 @@ OrderBackup::OrderBackup(const Vehicle *v)
}
}
-void OrderBackup::RestoreTo(const Vehicle *v)
+void OrderBackup::DoRestore(const Vehicle *v)
{
/* If we have a custom name, process that */
if (this->name != NULL) DoCommandP(0, v->index, 0, CMD_RENAME_VEHICLE, NULL, this->name);
@@ -109,22 +109,31 @@ void OrderBackup::RestoreTo(const Vehicle *v)
/* Restore vehicle group */
DoCommandP(0, this->group, v->index, CMD_ADD_VEHICLE_GROUP);
+}
- delete this;
+/* static */ void OrderBackup::Backup(const Vehicle *v)
+{
+ OrderBackup::Reset();
+ new OrderBackup(v);
}
-/* static */ OrderBackup *OrderBackup::GetByTile(TileIndex t)
+/* static */ void OrderBackup::Restore(const Vehicle *v)
{
OrderBackup *ob;
FOR_ALL_ORDER_BACKUPS(ob) {
- if (ob->tile == t) return ob;
+ if (v->tile != ob->tile) continue;
+
+ ob->DoRestore(v);
+ delete ob;
}
- return NULL;
}
-/* static */ void OrderBackup::Reset()
+/* static */ void OrderBackup::Reset(TileIndex t)
{
- _order_backup_pool.CleanPool();
+ OrderBackup *ob;
+ FOR_ALL_ORDER_BACKUPS(ob) {
+ if (t == INVALID_TILE || t == ob->tile) delete ob;
+ }
}
/* static */ void OrderBackup::ClearGroup(GroupID group)
diff --git a/src/order_backup.h b/src/order_backup.h
index 04913c2ac..3d4f81f92 100644
--- a/src/order_backup.h
+++ b/src/order_backup.h
@@ -43,34 +43,41 @@ private:
VehicleOrderID orderindex; ///< The order-index the vehicle had.
Order *orders; ///< The actual orders if the vehicle was not a clone.
-public:
/**
* Create an order backup for the given vehicle.
* @param v The vehicle to make a backup of.
*/
OrderBackup(const Vehicle *v);
+ /**
+ * Restore the data of this order to the given vehicle.
+ * @param v The vehicle to restore to.
+ */
+ void DoRestore(const Vehicle *v);
+
+public:
/** Free everything that is allocated. */
~OrderBackup();
/**
- * Restore the data of this order to the given vehicle.
- * @param v The vehicle to restore to.
- * @note After restoration the backup will automatically be removed.
+ * Create an order backup for the given vehicle.
+ * @param v The vehicle to make a backup of.
+ * @note Will automatically remove any previous backups of this user.
*/
- void RestoreTo(const Vehicle *v);
+ static void Backup(const Vehicle *v);
/**
- * Get the order backup associated with a given tile.
- * @param t The tile to get the order backup for.
- * @return The order backup, or NULL if it doesn't exist.
+ * Restore the data of this order to the given vehicle.
+ * @param v The vehicle to restore to.
+ * @note After restoration the backup will automatically be removed.
*/
- static OrderBackup *GetByTile(TileIndex t);
+ static void Restore(const Vehicle *v);
/**
* Reset the OrderBackups.
+ * @param tile The tile of the order backup.
*/
- static void Reset();
+ static void Reset(TileIndex tile = INVALID_TILE);
/**
* Clear the group of all backups having this group ID.
diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp
index 77d8619d2..aa77f2327 100644
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -49,6 +49,7 @@
#include "newgrf.h"
#include "table/airporttile_ids.h"
#include "newgrf_airporttiles.h"
+#include "order_backup.h"
#include "table/strings.h"
@@ -2287,6 +2288,7 @@ static CommandCost RemoveAirport(TileIndex tile, DoCommandFlag flags)
cost.AddCost(_price[PR_CLEAR_STATION_AIRPORT]);
if (flags & DC_EXEC) {
+ if (IsHangarTile(tile_cur)) OrderBackup::Reset(tile_cur);
DeleteAnimatedTile(tile_cur);
DoClearSquare(tile_cur);
DeleteNewGRFInspectWindow(GSF_AIRPORTTILES, tile_cur);
diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp
index 7d62184aa..a38a91492 100644
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -2373,7 +2373,6 @@ void CcBuildPrimaryVehicle(const CommandCost &result, TileIndex tile, uint32 p1,
if (result.Failed()) return;
const Vehicle *v = Vehicle::Get(_new_vehicle_id);
- OrderBackup *ob = OrderBackup::GetByTile(v->tile);
- if (ob != NULL) ob->RestoreTo(v);
+ OrderBackup::Restore(v);
ShowVehicleViewWindow(v);
}