changeset 20053:d49381b79636 draft

(svn r24986) -Change: Cleanup goals and cargo monitors of companies when they go bankrupt or are taken over.
author zuu <zuu@openttd.org>
date Sun, 10 Feb 2013 19:49:04 +0000
parents 7ecdc7b2f7ef
children b4d32dc27cfe
files src/cargomonitor.cpp src/cargomonitor.h src/economy.cpp src/script/api/game_changelog.hpp src/script/api/script_cargomonitor.hpp src/script/api/script_goal.hpp
diffstat 6 files changed, 60 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/cargomonitor.cpp
+++ b/src/cargomonitor.cpp
@@ -16,16 +16,48 @@
 CargoMonitorMap _cargo_pickups;    ///< Map of monitored pick-ups   to the amount since last query/activation.
 CargoMonitorMap _cargo_deliveries; ///< Map of monitored deliveries to the amount since last query/activation.
 
-/** Clear all pick-up cargo monitors. */
-void ClearCargoPickupMonitoring()
+/**
+ * Helper method for ClearCargoPickupMonitoring and ClearCargoDeliveryMonitoring.
+ * Clears all monitors that belong to the specified company or all if INVALID_OWNER
+ * is specified as company.
+ * @param cargo_monitor_map reference to the cargo monitor map to operate on.
+ * @param company company to clear cargo monitors for or INVALID_OWNER if all cargo monitors should be cleared.
+ */
+static void ClearCargoMonitoring(CargoMonitorMap &cargo_monitor_map, CompanyID company = INVALID_OWNER)
 {
-	_cargo_pickups.clear();
+	if (company == INVALID_OWNER) {
+		cargo_monitor_map.clear();
+		return;
+	}
+
+	CargoMonitorMap::iterator next;
+	for (CargoMonitorMap::iterator it = cargo_monitor_map.begin(); it != cargo_monitor_map.end(); it = next) {
+		next = it;
+		next++;
+		if (DecodeMonitorCompany(it->first) == company) {
+			cargo_monitor_map.erase(it);
+		}
+	}
 }
 
-/** Clear all delivery cargo monitors. */
-void ClearCargoDeliveryMonitoring()
+/**
+ * Clear all pick-up cargo monitors.
+ * @param company clear all pick-up monitors for this company or if INVALID_OWNER
+ * is passed, all pick-up monitors are cleared regardless of company.
+ */
+void ClearCargoPickupMonitoring(CompanyID company)
 {
-	_cargo_deliveries.clear();
+	ClearCargoMonitoring(_cargo_pickups, company);
+}
+
+/**
+ * Clear all delivery cargo monitors.
+ * @param company clear all delivery monitors for this company or if INVALID_OWNER
+ * is passed, all delivery monitors are cleared regardless of company.
+ */
+void ClearCargoDeliveryMonitoring(CompanyID company)
+{
+	ClearCargoMonitoring(_cargo_deliveries, company);
 }
 
 /**
--- a/src/cargomonitor.h
+++ b/src/cargomonitor.h
@@ -139,8 +139,8 @@
 	return GB(num, CCB_TOWN_IND_NUMBER_START, CCB_TOWN_IND_NUMBER_LENGTH);
 }
 
-void ClearCargoPickupMonitoring();
-void ClearCargoDeliveryMonitoring();
+void ClearCargoPickupMonitoring(CompanyID company = INVALID_OWNER);
+void ClearCargoDeliveryMonitoring(CompanyID company = INVALID_OWNER);
 uint32 GetDeliveryAmount(CargoMonitorID monitor, bool keep_monitoring);
 uint32 GetPickupAmount(CargoMonitorID monitor, bool keep_monitoring);
 void AddCargoDelivery(CargoID cargo_type, CompanyID company, uint32 amount, SourceType src_type, SourceID src, const Station *st);
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -45,6 +45,7 @@
 #include "water.h"
 #include "game/game.hpp"
 #include "cargomonitor.h"
+#include "goal_base.h"
 
 #include "table/strings.h"
 #include "table/pricebase.h"
@@ -508,6 +509,15 @@
 		if (si->owner == old_owner) si->owner = new_owner == INVALID_OWNER ? OWNER_NONE : new_owner;
 	}
 
+	/* Remove Game Script created Goals and CargoMonitors. */
+	Goal *g;
+	FOR_ALL_GOALS(g) {
+		if (g->company == old_owner) delete g;
+	}
+
+	ClearCargoPickupMonitoring(old_owner);
+	ClearCargoDeliveryMonitoring(old_owner);
+
 	/* Change colour of existing windows */
 	if (new_owner != INVALID_OWNER) ChangeWindowOwner(old_owner, new_owner);
 
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -31,6 +31,9 @@
  * \li GSController::Break
  * \li GSIndustryType::BuildIndustry, GSIndustryType::CanBuildIndustry, GSIndustryType::ProspectIndustry and GSIndustryType::CanProspectIndustry when outside GSCompanyMode scope
  *
+ * Other changes:
+ * \li Company specific goals are now removed when a company goes bankrupt or is taken over.
+ *
  * \b 1.2.3
  *
  * No changes
--- a/src/script/api/script_cargomonitor.hpp
+++ b/src/script/api/script_cargomonitor.hpp
@@ -37,7 +37,8 @@
  * The latter get added at the moment the cargo is delivered. This prevents users from getting credit for
  * picking up cargo without delivering it.
  *
- * The active monitors are saved and loaded. You can reset to the empty state with #StopAllMonitoring.
+ * The active monitors are saved and loaded. Upon bankruptcy or company takeover, the cargo monitors are
+ * automatically stopped for that company. You can reset to the empty state with #StopAllMonitoring.
  *
  * @api game
  */
--- a/src/script/api/script_goal.hpp
+++ b/src/script/api/script_goal.hpp
@@ -17,6 +17,11 @@
 
 /**
  * Class that handles some goal related functions.
+ *
+ * Goals are saved and loaded. Upon bankruptcy or company takeover, all company
+ * specific goals are removed for that company. You can also remove individual
+ * goals using #Remove.
+ *
  * @api game
  */
 class ScriptGoal : public ScriptObject {