src/train_cmd.cpp
changeset 6986 168d3add1f13
parent 6980 6b5dee376733
child 6987 b0f13039bda2
equal deleted inserted replaced
6985:6fd300b20503 6986:168d3add1f13
    61  * Recalculates the cached total power of a train. Should be called when the consist is changed
    61  * Recalculates the cached total power of a train. Should be called when the consist is changed
    62  * @param v First vehicle of the consist.
    62  * @param v First vehicle of the consist.
    63  */
    63  */
    64 void TrainPowerChanged(Vehicle* v)
    64 void TrainPowerChanged(Vehicle* v)
    65 {
    65 {
    66 	uint32 power = 0;
    66 	uint32 total_power = 0;
    67 	uint32 max_te = 0;
    67 	uint32 max_te = 0;
    68 
    68 
    69 	for (const Vehicle *u = v; u != NULL; u = u->next) {
    69 	for (const Vehicle *u = v; u != NULL; u = u->next) {
    70 		/* Power is not added for articulated parts */
    70 		/* Power is not added for articulated parts */
    71 		if (IsArticulatedPart(u)) continue;
    71 		if (IsArticulatedPart(u)) continue;
    74 		bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype);
    74 		bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype);
    75 		bool wagon_has_power  = HasPowerOnRail(v->u.rail.railtype, railtype);
    75 		bool wagon_has_power  = HasPowerOnRail(v->u.rail.railtype, railtype);
    76 
    76 
    77 		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
    77 		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
    78 
    78 
    79 		if (engine_has_power && rvi_u->power != 0) {
    79 		if (engine_has_power) {
    80 			power += rvi_u->power;
    80 			uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power);
    81 			/* Tractive effort in (tonnes * 1000 * 10 =) N */
    81 			if (power != 0) {
    82 			max_te += (u->u.rail.cached_veh_weight * 10000 * rvi_u->tractive_effort) / 256;
    82 				total_power += power;
       
    83 				/* Tractive effort in (tonnes * 1000 * 10 =) N */
       
    84 				max_te += (u->u.rail.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256;
       
    85 			}
    83 		}
    86 		}
    84 
    87 
    85 		if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) {
    88 		if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) {
    86 			power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power;
    89 			total_power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power;
    87 		}
    90 		}
    88 	}
    91 	}
    89 
    92 
    90 	if (v->u.rail.cached_power != power || v->u.rail.cached_max_te != max_te) {
    93 	if (v->u.rail.cached_power != total_power || v->u.rail.cached_max_te != max_te) {
    91 		v->u.rail.cached_power = power;
    94 		v->u.rail.cached_power = total_power;
    92 		v->u.rail.cached_max_te = max_te;
    95 		v->u.rail.cached_max_te = max_te;
    93 		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
    96 		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
    94 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
    97 		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
    95 	}
    98 	}
    96 }
    99 }
   206 				u->u.rail.railtype = RAILTYPE_RAIL;
   209 				u->u.rail.railtype = RAILTYPE_RAIL;
   207 				u->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL);
   210 				u->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL);
   208 			}
   211 			}
   209 
   212 
   210 			/* max speed is the minimum of the speed limits of all vehicles in the consist */
   213 			/* max speed is the minimum of the speed limits of all vehicles in the consist */
   211 			if ((rvi_u->railveh_type != RAILVEH_WAGON || _patches.wagon_speed_limits) &&
   214 			if ((rvi_u->railveh_type != RAILVEH_WAGON || _patches.wagon_speed_limits) && !UsesWagonOverride(u)) {
   212 				rvi_u->max_speed != 0 && !UsesWagonOverride(u))
   215 				uint16 speed = GetVehicleProperty(u, 0x09, rvi_u->max_speed);
   213 				max_speed = min(rvi_u->max_speed, max_speed);
   216 				if (speed != 0) max_speed = min(speed, max_speed);
       
   217 			}
   214 		}
   218 		}
   215 
   219 
   216 		/* check the vehicle length (callback) */
   220 		/* check the vehicle length (callback) */
   217 		uint16 veh_len = CALLBACK_FAILED;
   221 		uint16 veh_len = CALLBACK_FAILED;
   218 		if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) {
   222 		if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) {
  3471 {
  3475 {
  3472 	int32 cost = 0;
  3476 	int32 cost = 0;
  3473 
  3477 
  3474 	do {
  3478 	do {
  3475 		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
  3479 		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
  3476 		if (rvi->running_cost_base > 0)
  3480 
  3477 			cost += rvi->running_cost_base * _price.running_rail[rvi->running_cost_class];
  3481 		byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost_base);
       
  3482 		if (cost_factor == 0) continue;
       
  3483 
       
  3484 		cost += cost_factor * _price.running_rail[rvi->running_cost_class];
  3478 	} while ((v = GetNextVehicle(v)) != NULL);
  3485 	} while ((v = GetNextVehicle(v)) != NULL);
  3479 
  3486 
  3480 	return cost;
  3487 	return cost;
  3481 }
  3488 }
  3482 
  3489