changeset 19381:f37c5813696b draft

(svn r24282) -Codechange: Add AddVehicleAdviceNewsItem function to preemptively deduplicate code.
author frosch <frosch@openttd.org>
date Sat, 26 May 2012 14:15:52 +0000
parents 5c1d868aeb06
children d28ff0d5504f
files src/aircraft_cmd.cpp src/news_func.h src/order_cmd.cpp src/train_cmd.cpp src/vehicle.cpp
diffstat 5 files changed, 20 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -1862,7 +1862,7 @@
 			if (v->owner == _local_company) {
 				/* Post a news message. */
 				SetDParam(0, v->index);
-				AddVehicleNewsItem(STR_NEWS_AIRCRAFT_DEST_TOO_FAR, NS_ADVICE, v->index);
+				AddVehicleAdviceNewsItem(STR_NEWS_AIRCRAFT_DEST_TOO_FAR, v->index);
 			}
 		}
 		return;
--- a/src/news_func.h
+++ b/src/news_func.h
@@ -27,17 +27,23 @@
 /**
  * Adds a newsitem referencing a vehicle.
  *
- * @warning
- * Be careful!
- * Vehicles are a special case, as news are kept when vehicles are autoreplaced/renewed.
- * You have to make sure, #ChangeVehicleNews catches the DParams of your message.
- * This is NOT ensured by the references.
+ * @warning The DParams may not reference the vehicle due to autoreplace stuff. See AddVehicleAdviceNewsItem for how that can be done.
  */
 static inline void AddVehicleNewsItem(StringID string, NewsSubtype subtype, VehicleID vehicle, StationID station = INVALID_STATION)
 {
 	AddNewsItem(string, subtype, NR_VEHICLE, vehicle, station == INVALID_STATION ? NR_NONE : NR_STATION, station);
 }
 
+/**
+ * Adds a vehicle-advice news item.
+ *
+ * @warning DParam 0 must reference the vehicle!
+ */
+static inline void AddVehicleAdviceNewsItem(StringID string, VehicleID vehicle)
+{
+	AddNewsItem(string, NS_ADVICE, NR_VEHICLE, vehicle);
+}
+
 static inline void AddIndustryNewsItem(StringID string, NewsSubtype subtype, IndustryID industry)
 {
 	AddNewsItem(string, subtype, NR_INDUSTRY, industry);
--- a/src/order_cmd.cpp
+++ b/src/order_cmd.cpp
@@ -1698,11 +1698,7 @@
 		//DEBUG(misc, 3, "Triggered News Item for vehicle %d", v->index);
 
 		SetDParam(0, v->index);
-		AddVehicleNewsItem(
-			message,
-			NS_ADVICE,
-			v->index
-		);
+		AddVehicleAdviceNewsItem(message, v->index);
 	}
 }
 
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -3771,11 +3771,7 @@
 				/* Show message to player. */
 				if (_settings_client.gui.lost_vehicle_warn && v->owner == _local_company) {
 					SetDParam(0, v->index);
-					AddVehicleNewsItem(
-						STR_NEWS_TRAIN_IS_STUCK,
-						NS_ADVICE,
-						v->index
-					);
+					AddVehicleAdviceNewsItem(STR_NEWS_TRAIN_IS_STUCK, v->index);
 				}
 				v->wait_counter = 0;
 			}
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -710,7 +710,7 @@
 	AI::NewEvent(this->owner, new ScriptEventVehicleLost(this->index));
 	if (_settings_client.gui.lost_vehicle_warn && this->owner == _local_company) {
 		SetDParam(0, this->index);
-		AddVehicleNewsItem(STR_NEWS_VEHICLE_IS_LOST, NS_ADVICE, this->index);
+		AddVehicleAdviceNewsItem(STR_NEWS_VEHICLE_IS_LOST, this->index);
 	}
 }
 
@@ -939,7 +939,7 @@
 
 		SetDParam(0, v->index);
 		SetDParam(1, error_message);
-		AddVehicleNewsItem(message, NS_ADVICE, v->index);
+		AddVehicleAdviceNewsItem(message, v->index);
 	}
 
 	cur_company.Restore();
@@ -1225,7 +1225,7 @@
 	}
 
 	SetDParam(0, v->index);
-	AddVehicleNewsItem(str, NS_ADVICE, v->index);
+	AddVehicleAdviceNewsItem(str, v->index);
 }
 
 /**
@@ -1362,7 +1362,7 @@
 				if (v->owner == _local_company) {
 					/* Notify the user that we stopped the vehicle */
 					SetDParam(0, v->index);
-					AddVehicleNewsItem(STR_NEWS_ORDER_REFIT_FAILED, NS_ADVICE, v->index);
+					AddVehicleAdviceNewsItem(STR_NEWS_ORDER_REFIT_FAILED, v->index);
 				}
 			} else if (cost.GetCost() != 0) {
 				v->profit_this_year -= cost.GetCost() << 8;
@@ -1383,7 +1383,7 @@
 			_vehicles_to_autoreplace[v] = false;
 			if (v->owner == _local_company) {
 				SetDParam(0, v->index);
-				AddVehicleNewsItem(STR_NEWS_TRAIN_IS_WAITING + v->type, NS_ADVICE, v->index);
+				AddVehicleAdviceNewsItem(STR_NEWS_TRAIN_IS_WAITING + v->type, v->index);
 			}
 			AI::NewEvent(v->owner, new ScriptEventVehicleWaitingInDepot(v->index));
 		}
@@ -2413,11 +2413,7 @@
 				if (_settings_client.gui.vehicle_income_warn && v->owner == _local_company) {
 					SetDParam(0, v->index);
 					SetDParam(1, profit);
-					AddVehicleNewsItem(
-						STR_NEWS_VEHICLE_IS_UNPROFITABLE,
-						NS_ADVICE,
-						v->index
-					);
+					AddVehicleAdviceNewsItem(STR_NEWS_VEHICLE_IS_UNPROFITABLE, v->index);
 				}
 				AI::NewEvent(v->owner, new ScriptEventVehicleUnprofitable(v->index));
 			}