changeset 15821:67634d829518 draft

(svn r20500) -Fix: When converting rail all trains with a part on the converted rails need updating. Not only engines, which have power afterwards. Also update Acceleration after updating Power.
author frosch <frosch@openttd.org>
date Sun, 15 Aug 2010 15:47:07 +0000
parents 59fd8293b2ee
children 942de975cc28
files src/rail_cmd.cpp
diffstat 1 files changed, 19 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/rail_cmd.cpp
+++ b/src/rail_cmd.cpp
@@ -43,6 +43,9 @@
 #include "table/railtypes.h"
 #include "table/track_land.h"
 
+/** Helper type for lists/vectors of trains */
+typedef SmallVector<Train *, 16> TrainList;
+
 RailtypeInfo _railtypes[RAILTYPE_END];
 
 assert_compile(sizeof(_original_railtypes) <= sizeof(_railtypes));
@@ -1378,12 +1381,8 @@
 	if (v->type != VEH_TRAIN) return NULL;
 
 	/* Similar checks as in Train::PowerChanged() */
-
-	Train *t = Train::From(v);
-	if (t->IsArticulatedPart()) return NULL;
-
-	const RailVehicleInfo *rvi = RailVehInfo(t->engine_type);
-	if (GetVehicleProperty(t, PROP_TRAIN_POWER, rvi->power) != 0) t->First()->PowerChanged();
+	TrainList *affected_trains = static_cast<TrainList*>(data);
+	affected_trains->Include(Train::From(v)->First());
 
 	return NULL;
 }
@@ -1414,6 +1413,8 @@
 	if (ex < sx) Swap(ex, sx);
 	if (ey < sy) Swap(ey, sy);
 
+	TrainList affected_trains;
+
 	CommandCost cost(EXPENSES_CONSTRUCTION);
 	CommandCost error = CommandCost(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK); // by default, there is no track to convert.
 	for (uint x = sx; x <= ex; ++x) {
@@ -1480,8 +1481,8 @@
 
 					SetRailType(tile, totype);
 					MarkTileDirtyByTile(tile);
-					/* update power of train engines on this tile */
-					FindVehicleOnPos(tile, NULL, &UpdateTrainPowerProc);
+					/* update power of train on this tile */
+					FindVehicleOnPos(tile, &affected_trains, &UpdateTrainPowerProc);
 				}
 			}
 
@@ -1543,8 +1544,8 @@
 						SetRailType(tile, totype);
 						SetRailType(endtile, totype);
 
-						FindVehicleOnPos(tile, NULL, &UpdateTrainPowerProc);
-						FindVehicleOnPos(endtile, NULL, &UpdateTrainPowerProc);
+						FindVehicleOnPos(tile, &affected_trains, &UpdateTrainPowerProc);
+						FindVehicleOnPos(endtile, &affected_trains, &UpdateTrainPowerProc);
 
 						YapfNotifyTrackLayoutChange(tile, track);
 						YapfNotifyTrackLayoutChange(endtile, track);
@@ -1579,6 +1580,14 @@
 		}
 	}
 
+	if (flags & DC_EXEC) {
+		/* Railtype changed, update trains as when entering different track */
+		for (Train **v = affected_trains.Begin(); v != affected_trains.End(); v++) {
+			(*v)->PowerChanged();
+			(*v)->UpdateAcceleration();
+		}
+	}
+
 	return (cost.GetCost() == 0) ? error : cost;
 }