changeset 8467:284a76aa2868 draft

(svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
author glx <glx@openttd.org>
date Fri, 01 Feb 2008 22:02:14 +0000
parents 728f11b3fc9c
children d862bcc2b67b
files src/aircraft.h src/aircraft_cmd.cpp src/date.cpp src/disaster_cmd.cpp src/roadveh.h src/roadveh_cmd.cpp src/ship.h src/ship_cmd.cpp src/train.h src/train_cmd.cpp src/vehicle_base.h
diffstat 11 files changed, 84 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/src/aircraft.h
+++ b/src/aircraft.h
@@ -124,6 +124,7 @@
 	Money GetRunningCost() const { return AircraftVehInfo(this->engine_type)->running_cost * _price.aircraft_running; }
 	bool IsInDepot() const { return (this->vehstatus & VS_HIDDEN) != 0 && IsHangarTile(this->tile); }
 	void Tick();
+	void OnNewDay();
 };
 
 #endif /* AIRCRAFT_H */
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -723,27 +723,27 @@
 	}
 }
 
-void OnNewDay_Aircraft(Vehicle *v)
+void Aircraft::OnNewDay()
 {
-	if (!IsNormalAircraft(v)) return;
+	if (!IsNormalAircraft(this)) return;
 
-	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
+	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
 
-	CheckOrders(v);
+	CheckOrders(this);
 
-	CheckVehicleBreakdown(v);
-	AgeVehicle(v);
-	CheckIfAircraftNeedsService(v);
+	CheckVehicleBreakdown(this);
+	AgeVehicle(this);
+	CheckIfAircraftNeedsService(this);
 
-	if (v->vehstatus & VS_STOPPED) return;
+	if (this->vehstatus & VS_STOPPED) return;
 
-	CommandCost cost = CommandCost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(v, 0x0E, AircraftVehInfo(v->engine_type)->running_cost) * _price.aircraft_running / 364);
+	CommandCost cost = CommandCost(EXPENSES_AIRCRAFT_RUN, GetVehicleProperty(this, 0x0E, AircraftVehInfo(this->engine_type)->running_cost) * _price.aircraft_running / 364);
 
-	v->profit_this_year -= cost.GetCost() >> 8;
+	this->profit_this_year -= cost.GetCost() >> 8;
 
-	SubtractMoneyFromPlayerFract(v->owner, cost);
+	SubtractMoneyFromPlayerFract(this->owner, cost);
 
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
+	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
 	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
 }
 
--- a/src/date.cpp
+++ b/src/date.cpp
@@ -165,24 +165,6 @@
 
 /** Functions used by the IncreaseDate function */
 
-extern void OnNewDay_Train(Vehicle *v);
-extern void OnNewDay_RoadVeh(Vehicle *v);
-extern void OnNewDay_Aircraft(Vehicle *v);
-extern void OnNewDay_Ship(Vehicle *v);
-static void OnNewDay_EffectVehicle(Vehicle *v) { /* empty */ }
-extern void OnNewDay_DisasterVehicle(Vehicle *v);
-
-typedef void OnNewVehicleDayProc(Vehicle *v);
-
-static OnNewVehicleDayProc * _on_new_vehicle_day_proc[] = {
-	OnNewDay_Train,
-	OnNewDay_RoadVeh,
-	OnNewDay_Ship,
-	OnNewDay_Aircraft,
-	OnNewDay_EffectVehicle,
-	OnNewDay_DisasterVehicle,
-};
-
 extern void WaypointsDailyLoop();
 extern void ChatMessageDailyLoop();
 extern void EnginesDailyLoop();
@@ -225,7 +207,7 @@
 		if (v->IsValid()) {
 			/* Call the 32-day callback if needed */
 			CheckVehicle32Day(v);
-			_on_new_vehicle_day_proc[v->type](v);
+			v->OnNewDay();
 		}
 	}
 }
--- a/src/disaster_cmd.cpp
+++ b/src/disaster_cmd.cpp
@@ -749,12 +749,6 @@
 	_disastervehicle_tick_procs[this->subtype](this);
 }
 
-
-void OnNewDay_DisasterVehicle(Vehicle *v)
-{
-	// not used
-}
-
 typedef void DisasterInitProc();
 
 
--- a/src/roadveh.h
+++ b/src/roadveh.h
@@ -75,6 +75,7 @@
 	Money GetRunningCost() const { return RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running; }
 	bool IsInDepot() const { return this->u.road.state == RVSB_IN_DEPOT; }
 	void Tick();
+	void OnNewDay();
 };
 
 byte GetRoadVehLength(const Vehicle *v);
--- a/src/roadveh_cmd.cpp
+++ b/src/roadveh_cmd.cpp
@@ -2004,33 +2004,33 @@
 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
 }
 
-void OnNewDay_RoadVeh(Vehicle *v)
+void RoadVehicle::OnNewDay()
 {
 	CommandCost cost(EXPENSES_ROADVEH_RUN);
 
-	if (!IsRoadVehFront(v)) return;
+	if (!IsRoadVehFront(this)) return;
 
-	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
-	if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
+	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
+	if (this->u.road.blocked_ctr == 0) CheckVehicleBreakdown(this);
 
-	AgeVehicle(v);
-	CheckIfRoadVehNeedsService(v);
+	AgeVehicle(this);
+	CheckIfRoadVehNeedsService(this);
 
-	CheckOrders(v);
+	CheckOrders(this);
 
 	/* Current slot has expired */
-	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) {
+	if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot != NULL && this->u.road.slot_age-- == 0) {
 		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
-			v->unitnumber, v->index, v->u.road.slot->xy);
-		ClearSlot(v);
+			this->unitnumber, this->index, this->u.road.slot->xy);
+		ClearSlot(this);
 	}
 
-	if (v->vehstatus & VS_STOPPED) return;
+	if (this->vehstatus & VS_STOPPED) return;
 
 	/* update destination */
-	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) {
-		Station *st = GetStation(v->current_order.dest);
-		RoadStop *rs = st->GetPrimaryRoadStop(v);
+	if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
+		Station *st = GetStation(this->current_order.dest);
+		RoadStop *rs = st->GetPrimaryRoadStop(this);
 		RoadStop *best = NULL;
 
 		if (rs != NULL) {
@@ -2040,16 +2040,16 @@
 			 * 2) we're somewhere close to the station rectangle (to make sure we do assign
 			 *    slots even if the station and its road stops are incredibly spread out)
 			 */
-			if (DistanceManhattan(v->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(v->tile), TileY(v->tile), 2)) {
+			if (DistanceManhattan(this->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(this->tile), TileY(this->tile), 2)) {
 				uint dist, badness;
 				uint minbadness = UINT_MAX;
 
 				DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
-					v->unitnumber, v->index, st->index, st->xy
+					this->unitnumber, this->index, st->index, st->xy
 				);
 				/* Now we find the nearest road stop that has a free slot */
-				for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
-					dist = RoadFindPathToStop(v, rs->xy);
+				for (; rs != NULL; rs = rs->GetNextRoadStop(this)) {
+					dist = RoadFindPathToStop(this, rs->xy);
 					if (dist == UINT_MAX) {
 						DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
 						continue;
@@ -2070,29 +2070,29 @@
 					best->num_vehicles++;
 					DEBUG(ms, 3, "Assigned to stop 0x%X", best->xy);
 
-					v->u.road.slot = best;
-					v->dest_tile = best->xy;
-					v->u.road.slot_age = 14;
+					this->u.road.slot = best;
+					this->dest_tile = best->xy;
+					this->u.road.slot_age = 14;
 				} else {
 					DEBUG(ms, 3, "Could not find a suitable stop");
 				}
 			} else {
 				DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)",
-						v->unitnumber, v->index, st->index, st->xy);
+						this->unitnumber, this->index, st->index, st->xy);
 			}
 		} else {
 			DEBUG(ms, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)",
-					v->unitnumber, v->index, st->index, st->xy);
+					this->unitnumber, this->index, st->index, st->xy);
 		}
 	}
 
-	cost = CommandCost(EXPENSES_ROADVEH_RUN, RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364);
+	cost = CommandCost(EXPENSES_ROADVEH_RUN, RoadVehInfo(this->engine_type)->running_cost * _price.roadveh_running / 364);
 
-	v->profit_this_year -= cost.GetCost() >> 8;
+	this->profit_this_year -= cost.GetCost() >> 8;
 
-	SubtractMoneyFromPlayerFract(v->owner, cost);
+	SubtractMoneyFromPlayerFract(this->owner, cost);
 
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
+	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
 	InvalidateWindowClasses(WC_ROADVEH_LIST);
 }
 
--- a/src/ship.h
+++ b/src/ship.h
@@ -41,6 +41,7 @@
 	Money GetRunningCost() const { return ShipVehInfo(this->engine_type)->running_cost * _price.ship_running; }
 	bool IsInDepot() const { return this->u.ship.state == 0x80; }
 	void Tick();
+	void OnNewDay();
 };
 
 #endif /* SHIP_H */
--- a/src/ship_cmd.cpp
+++ b/src/ship_cmd.cpp
@@ -170,27 +170,27 @@
 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
 }
 
-void OnNewDay_Ship(Vehicle *v)
+void Ship::OnNewDay()
 {
 	CommandCost cost(EXPENSES_SHIP_RUN);
 
-	if ((++v->day_counter & 7) == 0)
-		DecreaseVehicleValue(v);
+	if ((++this->day_counter & 7) == 0)
+		DecreaseVehicleValue(this);
 
-	CheckVehicleBreakdown(v);
-	AgeVehicle(v);
-	CheckIfShipNeedsService(v);
+	CheckVehicleBreakdown(this);
+	AgeVehicle(this);
+	CheckIfShipNeedsService(this);
 
-	CheckOrders(v);
+	CheckOrders(this);
 
-	if (v->vehstatus & VS_STOPPED) return;
+	if (this->vehstatus & VS_STOPPED) return;
 
-	cost.AddCost(GetVehicleProperty(v, 0x0F, ShipVehInfo(v->engine_type)->running_cost) * _price.ship_running / 364);
-	v->profit_this_year -= cost.GetCost() >> 8;
+	cost.AddCost(GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running / 364);
+	this->profit_this_year -= cost.GetCost() >> 8;
 
-	SubtractMoneyFromPlayerFract(v->owner, cost);
+	SubtractMoneyFromPlayerFract(this->owner, cost);
 
-	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
+	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
 	/* we need this for the profit */
 	InvalidateWindowClasses(WC_SHIPS_LIST);
 }
--- a/src/train.h
+++ b/src/train.h
@@ -304,6 +304,7 @@
 	bool IsInDepot() const { return CheckTrainInDepot(this, false) != -1; }
 	bool IsStoppedInDepot() const { return CheckTrainStoppedInDepot(this) >= 0; }
 	void Tick();
+	void OnNewDay();
 };
 
 #endif /* TRAIN_H */
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -3625,38 +3625,38 @@
 	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
 }
 
-void OnNewDay_Train(Vehicle *v)
+void Train::OnNewDay()
 {
-	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
-
-	if (IsFrontEngine(v)) {
-		CheckVehicleBreakdown(v);
-		AgeVehicle(v);
-
-		CheckIfTrainNeedsService(v);
-
-		CheckOrders(v);
+	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
+
+	if (IsFrontEngine(this)) {
+		CheckVehicleBreakdown(this);
+		AgeVehicle(this);
+
+		CheckIfTrainNeedsService(this);
+
+		CheckOrders(this);
 
 		/* update destination */
-		if (v->current_order.type == OT_GOTO_STATION) {
-			TileIndex tile = GetStation(v->current_order.dest)->train_tile;
-			if (tile != 0) v->dest_tile = tile;
+		if (this->current_order.type == OT_GOTO_STATION) {
+			TileIndex tile = GetStation(this->current_order.dest)->train_tile;
+			if (tile != 0) this->dest_tile = tile;
 		}
 
-		if ((v->vehstatus & VS_STOPPED) == 0) {
+		if ((this->vehstatus & VS_STOPPED) == 0) {
 			/* running costs */
-			CommandCost cost(EXPENSES_TRAIN_RUN, v->GetRunningCost() / 364);
-
-			v->profit_this_year -= cost.GetCost() >> 8;
-
-			SubtractMoneyFromPlayerFract(v->owner, cost);
-
-			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
+			CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() / 364);
+
+			this->profit_this_year -= cost.GetCost() >> 8;
+
+			SubtractMoneyFromPlayerFract(this->owner, cost);
+
+			InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
 			InvalidateWindowClasses(WC_TRAINS_LIST);
 		}
-	} else if (IsTrainEngine(v)) {
+	} else if (IsTrainEngine(this)) {
 		/* Also age engines that aren't front engines */
-		AgeVehicle(v);
+		AgeVehicle(this);
 	}
 }
 
--- a/src/vehicle_base.h
+++ b/src/vehicle_base.h
@@ -426,6 +426,11 @@
 	virtual void Tick() {};
 
 	/**
+	 * Calls the new day handler of the vehicle
+	 */
+	virtual void OnNewDay() {};
+
+	/**
 	 * Gets the running cost of a vehicle  that can be sent into SetDParam for string processing.
 	 * @return the vehicle's running cost
 	 */