changeset 15825:5a7d7a33055b draft

(svn r20504) -Codechange: Move updating of train's visual effect to separate function.
author frosch <frosch@openttd.org>
date Sun, 15 Aug 2010 19:53:53 +0000
parents 5ca623849e16
children 7f692dcca929
files src/train.h src/train_cmd.cpp
diffstat 2 files changed, 33 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/src/train.h
+++ b/src/train.h
@@ -372,6 +372,8 @@
 
 protected: // These functions should not be called outside acceleration code.
 
+	void UpdateVisualEffect();
+
 	/**
 	 * Allows to know the power value that this vehicle will use.
 	 * @return Power value from the engine in HP, or zero if the vehicle is not powered.
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -130,6 +130,35 @@
 }
 
 /**
+ * Update the cached visual effect.
+ */
+void Train::UpdateVisualEffect()
+{
+	const Engine *e = Engine::Get(this->engine_type);
+	if (e->u.rail.visual_effect != 0) {
+		this->tcache.cached_vis_effect = e->u.rail.visual_effect;
+	} else {
+		if (this->IsWagon() || this->IsArticulatedPart()) {
+			/* Wagons and articulated parts have no effect by default */
+			this->tcache.cached_vis_effect = 0x40;
+		} else if (e->u.rail.engclass == 0) {
+			/* Steam is offset by -4 units */
+			this->tcache.cached_vis_effect = 4;
+		} else {
+			/* Diesel fumes and sparks come from the centre */
+			this->tcache.cached_vis_effect = 8;
+		}
+	}
+
+	/* Check powered wagon / visual effect callback */
+	if (HasBit(e->info.callback_mask, CBM_TRAIN_WAGON_POWER)) {
+		uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, this->engine_type, this);
+
+		if (callback != CALLBACK_FAILED) this->tcache.cached_vis_effect = GB(callback, 0, 8);
+	}
+}
+
+/**
  * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
  * to/removed from the chain, and when the game is loaded.
  * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
@@ -185,27 +214,8 @@
 		/* Reset colour map */
 		u->colourmap = PAL_NONE;
 
-		if (rvi_u->visual_effect != 0) {
-			u->tcache.cached_vis_effect = rvi_u->visual_effect;
-		} else {
-			if (u->IsWagon() || u->IsArticulatedPart()) {
-				/* Wagons and articulated parts have no effect by default */
-				u->tcache.cached_vis_effect = 0x40;
-			} else if (rvi_u->engclass == 0) {
-				/* Steam is offset by -4 units */
-				u->tcache.cached_vis_effect = 4;
-			} else {
-				/* Diesel fumes and sparks come from the centre */
-				u->tcache.cached_vis_effect = 8;
-			}
-		}
-
-		/* Check powered wagon / visual effect callback */
-		if (HasBit(e_u->info.callback_mask, CBM_TRAIN_WAGON_POWER)) {
-			uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u);
-
-			if (callback != CALLBACK_FAILED) u->tcache.cached_vis_effect = GB(callback, 0, 8);
-		}
+		/* Update powered-wagon-status and visual effect */
+		u->UpdateVisualEffect();
 
 		if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
 				UsesWagonOverride(u) && !HasBit(u->tcache.cached_vis_effect, 7)) {