summaryrefslogtreecommitdiff
path: root/train_cmd.c
diff options
context:
space:
mode:
Diffstat (limited to 'train_cmd.c')
-rw-r--r--train_cmd.c95
1 files changed, 54 insertions, 41 deletions
diff --git a/train_cmd.c b/train_cmd.c
index 5a4148f92..c1c5746bb 100644
--- a/train_cmd.c
+++ b/train_cmd.c
@@ -55,41 +55,6 @@ byte FreightWagonMult(CargoID cargo)
/**
- * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
- * the consist changes.
- * @param v First vehicle of the consist.
- */
-static void TrainCargoChanged(Vehicle* v)
-{
- Vehicle *u;
- uint32 weight = 0;
-
- for (u = v; u != NULL; u = u->next) {
- const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
- uint32 vweight = (_cargoc.weights[u->cargo_type] * u->cargo_count * FreightWagonMult(u->cargo_type)) / 16;
-
- // Vehicle weight is not added for articulated parts.
- if (!IsArticulatedPart(u)) {
- // vehicle weight is the sum of the weight of the vehicle and the weight of its cargo
- vweight += rvi->weight;
-
- // powered wagons have extra weight added
- if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON))
- vweight += RailVehInfo(u->u.rail.first_engine)->pow_wag_weight;
- }
-
- // consist weight is the sum of the weight of all vehicles in the consist
- weight += vweight;
-
- // store vehicle weight in cache
- u->u.rail.cached_veh_weight = vweight;
- };
-
- // store consist weight in cache
- v->u.rail.cached_weight = weight;
-}
-
-/**
* Recalculates the cached total power of a train. Should be called when the consist is changed
* @param v First vehicle of the consist.
*/
@@ -97,6 +62,7 @@ void TrainPowerChanged(Vehicle* v)
{
Vehicle* u;
uint32 power = 0;
+ uint32 max_te = 0;
for (u = v; u != NULL; u = u->next) {
const RailVehicleInfo *rvi_u;
@@ -116,19 +82,65 @@ void TrainPowerChanged(Vehicle* v)
rvi_u = RailVehInfo(u->engine_type);
- if (engine_has_power) power += rvi_u->power;
+ if (engine_has_power && rvi_u->power != 0) {
+ power += rvi_u->power;
+ /* Tractive effort in (tonnes * 1000 * 10 =) N */
+ max_te += (u->u.rail.cached_veh_weight * 10000 * rvi_u->tractive_effort) / 256;
+ }
+
if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) {
power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power;
}
}
- if (v->u.rail.cached_power != power) {
+ if (v->u.rail.cached_power != power || v->u.rail.cached_max_te != max_te) {
v->u.rail.cached_power = power;
+ v->u.rail.cached_max_te = max_te;
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
}
}
+
+/**
+ * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
+ * the consist changes.
+ * @param v First vehicle of the consist.
+ */
+static void TrainCargoChanged(Vehicle* v)
+{
+ Vehicle *u;
+ uint32 weight = 0;
+
+ for (u = v; u != NULL; u = u->next) {
+ const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
+ uint32 vweight = (_cargoc.weights[u->cargo_type] * u->cargo_count * FreightWagonMult(u->cargo_type)) / 16;
+
+ // Vehicle weight is not added for articulated parts.
+ if (!IsArticulatedPart(u)) {
+ // vehicle weight is the sum of the weight of the vehicle and the weight of its cargo
+ vweight += rvi->weight;
+
+ // powered wagons have extra weight added
+ if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON))
+ vweight += RailVehInfo(u->u.rail.first_engine)->pow_wag_weight;
+ }
+
+ // consist weight is the sum of the weight of all vehicles in the consist
+ weight += vweight;
+
+ // store vehicle weight in cache
+ u->u.rail.cached_veh_weight = vweight;
+ };
+
+ // store consist weight in cache
+ v->u.rail.cached_weight = weight;
+
+ /* Now update train power (tractive effort is dependent on weight) */
+ TrainPowerChanged(v);
+}
+
+
/**
* 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.
@@ -231,9 +243,7 @@ void TrainConsistChanged(Vehicle* v)
// store consist weight/max speed in cache
v->u.rail.cached_max_speed = max_speed;
- TrainPowerChanged(v);
-
- // recalculate cached weights too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added)
+ // recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added)
TrainCargoChanged(v);
}
@@ -307,6 +317,7 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
int curvecount[2] = {0, 0};
int sum = 0;
int numcurve = 0;
+ int max_te = v->u.rail.cached_max_te; // [N]
speed *= 10;
speed /= 16;
@@ -407,6 +418,7 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
force = power / speed; //[N]
force *= 22;
force /= 10;
+ if (mode == AM_ACCEL && force > max_te) force = max_te;
break;
case RAILTYPE_MAGLEV:
@@ -415,7 +427,8 @@ static int GetTrainAcceleration(Vehicle *v, bool mode)
}
} else {
//"kickoff" acceleration
- force = max(power, (mass * 8) + resistance);
+ force = (mode == AM_ACCEL && v->u.rail.railtype != RAILTYPE_MAGLEV) ? min(max_te, power) : power;
+ force = max(force, (mass * 8) + resistance);
}
if (force <= 0) force = 10000;