2010-03-06 12:44:30 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file ground_vehicle.cpp Implementation of GroundVehicle. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "train.h"
|
2010-03-06 12:54:42 +00:00
|
|
|
#include "roadveh.h"
|
2012-07-07 15:39:46 +00:00
|
|
|
#include "depot_map.h"
|
2017-03-05 18:05:04 +00:00
|
|
|
#include "tunnel_base.h"
|
|
|
|
#include "slope_type.h"
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-03-06 12:44:30 +00:00
|
|
|
/**
|
|
|
|
* Recalculates the cached total power of a vehicle. Should be called when the consist is changed.
|
|
|
|
*/
|
|
|
|
template <class T, VehicleType Type>
|
|
|
|
void GroundVehicle<T, Type>::PowerChanged()
|
|
|
|
{
|
|
|
|
assert(this->First() == this);
|
|
|
|
const T *v = T::From(this);
|
|
|
|
|
|
|
|
uint32 total_power = 0;
|
|
|
|
uint32 max_te = 0;
|
|
|
|
uint32 number_of_parts = 0;
|
2019-04-06 06:46:15 +00:00
|
|
|
uint16 max_track_speed = this->vcache.cached_max_speed; // Max track speed in internal units.
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
this->CalculatePower(total_power, max_te, false);
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2019-04-11 17:14:13 +00:00
|
|
|
for (const T *u = v; u != nullptr; u = u->Next()) {
|
2010-03-06 12:44:30 +00:00
|
|
|
number_of_parts++;
|
|
|
|
|
|
|
|
/* Get minimum max speed for this track. */
|
|
|
|
uint16 track_speed = u->GetMaxTrackSpeed();
|
2021-01-08 10:16:18 +00:00
|
|
|
if (track_speed > 0) max_track_speed = std::min(max_track_speed, track_speed);
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
|
|
|
|
2010-08-02 14:54:47 +00:00
|
|
|
byte air_drag;
|
|
|
|
byte air_drag_value = v->GetAirDrag();
|
|
|
|
|
|
|
|
/* If air drag is set to zero (default), the resulting air drag coefficient is dependent on max speed. */
|
|
|
|
if (air_drag_value == 0) {
|
2010-11-07 13:35:12 +00:00
|
|
|
uint16 max_speed = v->GetDisplayMaxSpeed();
|
2010-08-02 14:54:47 +00:00
|
|
|
/* Simplification of the method used in TTDPatch. It uses <= 10 to change more steadily from 128 to 196. */
|
2021-01-08 10:16:18 +00:00
|
|
|
air_drag = (max_speed <= 10) ? 192 : std::max(2048 / max_speed, 1);
|
2010-08-02 14:54:47 +00:00
|
|
|
} else {
|
|
|
|
/* According to the specs, a value of 0x01 in the air drag property means "no air drag". */
|
|
|
|
air_drag = (air_drag_value == 1) ? 0 : air_drag_value;
|
|
|
|
}
|
|
|
|
|
2010-12-14 21:28:45 +00:00
|
|
|
this->gcache.cached_air_drag = air_drag + 3 * air_drag * number_of_parts / 20;
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2010-12-14 21:28:45 +00:00
|
|
|
if (this->gcache.cached_power != total_power || this->gcache.cached_max_te != max_te) {
|
2010-03-06 12:44:30 +00:00
|
|
|
/* Stop the vehicle if it has no power. */
|
|
|
|
if (total_power == 0) this->vehstatus |= VS_STOPPED;
|
|
|
|
|
2010-12-14 21:28:45 +00:00
|
|
|
this->gcache.cached_power = total_power;
|
|
|
|
this->gcache.cached_max_te = max_te;
|
2010-03-06 12:44:30 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, this->index, WID_VV_START_STOP);
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
|
|
|
|
2010-12-14 21:28:45 +00:00
|
|
|
this->gcache.cached_max_track_speed = max_track_speed;
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
template <class T, VehicleType Type>
|
|
|
|
void GroundVehicle<T, Type>::CalculatePower(uint32& total_power, uint32& max_te, bool breakdowns) const {
|
|
|
|
|
|
|
|
total_power = 0;
|
|
|
|
max_te = 0;
|
|
|
|
|
|
|
|
const T *v = T::From(this);
|
|
|
|
|
2019-04-11 17:14:13 +00:00
|
|
|
for (const T *u = v; u != nullptr; u = u->Next()) {
|
2015-08-02 21:58:41 +00:00
|
|
|
uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u);
|
2016-01-31 22:55:25 +00:00
|
|
|
|
|
|
|
if (breakdowns && u->breakdown_ctr == 1 && u->breakdown_type == BREAKDOWN_LOW_POWER) {
|
|
|
|
current_power = current_power * u->breakdown_severity / 256;
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
total_power += current_power;
|
|
|
|
|
|
|
|
/* Only powered parts add tractive effort. */
|
|
|
|
if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort();
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:33:15 +00:00
|
|
|
max_te *= GROUND_ACCELERATION; // Tractive effort in (tonnes * 1000 * 9.8 =) N.
|
2018-05-25 17:29:17 +00:00
|
|
|
max_te /= 256; // Tractive effort is a [0-255] coefficient.
|
2015-08-02 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
2010-03-06 12:44:30 +00:00
|
|
|
/**
|
|
|
|
* Recalculates the cached weight of a vehicle and its parts. Should be called each time the cargo on
|
|
|
|
* the consist changes.
|
|
|
|
*/
|
|
|
|
template <class T, VehicleType Type>
|
|
|
|
void GroundVehicle<T, Type>::CargoChanged()
|
|
|
|
{
|
|
|
|
assert(this->First() == this);
|
|
|
|
uint32 weight = 0;
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
for (T *u = T::From(this); u != nullptr; u = u->Next()) {
|
2010-03-06 12:44:30 +00:00
|
|
|
uint32 current_weight = u->GetWeight();
|
2021-01-25 02:33:14 +00:00
|
|
|
if (Type == VEH_TRAIN) Train::From(u)->tcache.cached_veh_weight = current_weight;
|
2010-03-06 12:44:30 +00:00
|
|
|
weight += current_weight;
|
2010-11-07 13:35:07 +00:00
|
|
|
/* Slope steepness is in percent, result in N. */
|
2010-12-14 21:28:45 +00:00
|
|
|
u->gcache.cached_slope_resistance = current_weight * u->GetSlopeSteepness() * 100;
|
2020-08-25 00:26:44 +00:00
|
|
|
u->InvalidateImageCache();
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
2019-02-16 21:27:08 +00:00
|
|
|
ClrBit(this->vcache.cached_veh_flags, VCF_GV_ZERO_SLOPE_RESIST);
|
2010-03-06 12:44:30 +00:00
|
|
|
|
|
|
|
/* Store consist weight in cache. */
|
2021-01-08 10:16:18 +00:00
|
|
|
this->gcache.cached_weight = std::max(1u, weight);
|
2010-11-07 13:35:07 +00:00
|
|
|
/* Friction in bearings and other mechanical parts is 0.1% of the weight (result in N). */
|
2010-12-14 21:28:45 +00:00
|
|
|
this->gcache.cached_axle_resistance = 10 * weight;
|
2010-03-06 12:44:30 +00:00
|
|
|
|
|
|
|
/* Now update vehicle power (tractive effort is dependent on weight). */
|
|
|
|
this->PowerChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the acceleration of the vehicle under its current conditions.
|
2021-01-25 02:33:14 +00:00
|
|
|
* @return Current upper and lower bounds of acceleration of the vehicle.
|
2010-03-06 12:44:30 +00:00
|
|
|
*/
|
|
|
|
template <class T, VehicleType Type>
|
2021-01-25 02:33:14 +00:00
|
|
|
GroundVehicleAcceleration GroundVehicle<T, Type>::GetAcceleration()
|
2010-03-06 12:44:30 +00:00
|
|
|
{
|
|
|
|
/* Templated class used for function calls for performance reasons. */
|
|
|
|
const T *v = T::From(this);
|
2014-07-22 19:46:10 +00:00
|
|
|
/* Speed is used squared later on, so U16 * U16, and then multiplied by other values. */
|
|
|
|
int64 speed = v->GetCurrentSpeed(); // [km/h-ish]
|
2010-03-06 12:44:30 +00:00
|
|
|
|
|
|
|
/* Weight is stored in tonnes. */
|
2010-12-14 21:28:45 +00:00
|
|
|
int32 mass = this->gcache.cached_weight;
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2014-07-22 19:46:10 +00:00
|
|
|
/* Power is stored in HP, we need it in watts.
|
|
|
|
* Each vehicle can have U16 power, 128 vehicles, HP -> watt
|
2019-09-29 20:27:32 +00:00
|
|
|
* and km/h to m/s conversion below result in a maximum of
|
2014-07-22 19:46:10 +00:00
|
|
|
* about 1.1E11, way more than 4.3E9 of int32. */
|
|
|
|
int64 power = this->gcache.cached_power * 746ll;
|
|
|
|
|
|
|
|
/* This is constructed from:
|
|
|
|
* - axle resistance: U16 power * 10 for 128 vehicles.
|
|
|
|
* * 8.3E7
|
|
|
|
* - rolling friction: U16 power * 144 for 128 vehicles.
|
|
|
|
* * 1.2E9
|
|
|
|
* - slope resistance: U16 weight * 100 * 10 (steepness) for 128 vehicles.
|
|
|
|
* * 8.4E9
|
|
|
|
* - air drag: 28 * (U8 drag + 3 * U8 drag * 128 vehicles / 20) * U16 speed * U16 speed
|
|
|
|
* * 6.2E14 before dividing by 1000
|
|
|
|
* Sum is 6.3E11, more than 4.3E9 of int32, so int64 is needed.
|
|
|
|
*/
|
|
|
|
int64 resistance = 0;
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2021-02-04 17:20:02 +00:00
|
|
|
int acceleration_type = v->GetAccelerationType();
|
|
|
|
bool maglev = (acceleration_type == 2);
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2010-08-02 14:49:23 +00:00
|
|
|
const int area = v->GetAirDragArea();
|
2010-03-06 12:44:30 +00:00
|
|
|
if (!maglev) {
|
2010-11-07 13:35:07 +00:00
|
|
|
/* Static resistance plus rolling friction. */
|
2010-12-14 21:28:45 +00:00
|
|
|
resistance = this->gcache.cached_axle_resistance;
|
2010-11-07 13:35:07 +00:00
|
|
|
resistance += mass * v->GetRollingFriction();
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
2010-11-07 13:35:07 +00:00
|
|
|
/* Air drag; the air drag coefficient is in an arbitrary NewGRF-unit,
|
|
|
|
* so we need some magic conversion factor. */
|
2011-01-04 18:12:28 +00:00
|
|
|
resistance += (area * this->gcache.cached_air_drag * speed * speed) / 1000;
|
2010-03-06 12:44:30 +00:00
|
|
|
|
2010-03-06 12:50:55 +00:00
|
|
|
resistance += this->GetSlopeResistance();
|
2010-03-06 12:44:30 +00:00
|
|
|
|
|
|
|
/* This value allows to know if the vehicle is accelerating or braking. */
|
|
|
|
AccelStatus mode = v->GetAccelerationStatus();
|
|
|
|
|
2021-01-25 02:33:14 +00:00
|
|
|
const int braking_power = power;
|
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
/* handle breakdown power reduction */
|
2016-01-31 22:55:25 +00:00
|
|
|
uint32 max_te = this->gcache.cached_max_te; // [N]
|
2016-01-31 15:22:46 +00:00
|
|
|
if (Type == VEH_TRAIN && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) {
|
2015-08-02 21:58:41 +00:00
|
|
|
/* We'd like to cache this, but changing cached_power has too many unwanted side-effects */
|
|
|
|
uint32 power_temp;
|
|
|
|
this->CalculatePower(power_temp, max_te, true);
|
2017-07-06 20:44:17 +00:00
|
|
|
power = power_temp * 746ll;
|
2015-08-02 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-22 19:46:10 +00:00
|
|
|
/* Constructued from power, with need to multiply by 18 and assuming
|
|
|
|
* low speed, it needs to be a 64 bit integer too. */
|
|
|
|
int64 force;
|
2021-01-25 02:33:14 +00:00
|
|
|
int64 braking_force;
|
2010-03-06 12:44:30 +00:00
|
|
|
if (speed > 0) {
|
|
|
|
if (!maglev) {
|
2010-11-07 13:35:07 +00:00
|
|
|
/* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
|
|
|
|
force = power * 18 / (speed * 5);
|
2021-01-25 02:33:14 +00:00
|
|
|
braking_force = force;
|
2015-08-02 21:58:41 +00:00
|
|
|
if (mode == AS_ACCEL && force > (int)max_te) force = max_te;
|
2010-03-06 12:44:30 +00:00
|
|
|
} else {
|
|
|
|
force = power / 25;
|
2021-01-25 02:33:14 +00:00
|
|
|
braking_force = force;
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* "Kickoff" acceleration. */
|
2021-02-01 17:07:34 +00:00
|
|
|
force = (mode == AS_ACCEL && !maglev) ? std::min<uint64>(max_te, power) : power;
|
2021-01-08 10:16:18 +00:00
|
|
|
force = std::max(force, (mass * 8) + resistance);
|
2021-01-25 02:33:14 +00:00
|
|
|
braking_force = force;
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
/* If power is 0 because of a breakdown, we make the force 0 if accelerating */
|
2016-01-31 15:22:46 +00:00
|
|
|
if (Type == VEH_TRAIN && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER) && power == 0) {
|
2015-08-02 21:58:41 +00:00
|
|
|
force = 0;
|
|
|
|
}
|
|
|
|
|
2021-01-25 02:33:14 +00:00
|
|
|
if (power != braking_power) {
|
|
|
|
if (!maglev && speed > 0) {
|
|
|
|
/* Conversion factor from km/h to m/s is 5/18 to get [N] in the end. */
|
|
|
|
braking_force = braking_power * 18 / (speed * 5);
|
|
|
|
} else {
|
|
|
|
braking_force = braking_power / 25;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:58:41 +00:00
|
|
|
/* Calculate the breakdown chance */
|
|
|
|
if (_settings_game.vehicle.improved_breakdowns) {
|
|
|
|
assert(this->gcache.cached_max_track_speed > 0);
|
|
|
|
/** First, calculate (resistance / force * current speed / max speed) << 16.
|
|
|
|
* This yields a number x on a 0-1 scale, but shifted 16 bits to the left.
|
|
|
|
* We then calculate 64 + 128x, clamped to 0-255, but still shifted 16 bits to the left.
|
|
|
|
* Then we apply a correction for multiengine trains, and in the end we shift it 16 bits to the right to get a 0-255 number.
|
|
|
|
* @note A seperate correction for multiheaded engines is done in CheckVehicleBreakdown. We can't do that here because it would affect the whole consist.
|
|
|
|
*/
|
|
|
|
uint64 breakdown_factor = (uint64)abs(resistance) * (uint64)(this->cur_speed << 16);
|
2021-02-01 17:07:34 +00:00
|
|
|
breakdown_factor /= (std::max(force, (int64)100) * this->gcache.cached_max_track_speed);
|
|
|
|
breakdown_factor = std::min<uint64>((64 << 16) + (breakdown_factor * 128), 255 << 16);
|
2016-01-31 15:22:46 +00:00
|
|
|
if (Type == VEH_TRAIN && Train::From(this)->tcache.cached_num_engines > 1) {
|
2015-08-02 21:58:41 +00:00
|
|
|
/* For multiengine trains, breakdown chance is multiplied by 3 / (num_engines + 2) */
|
|
|
|
breakdown_factor *= 3;
|
|
|
|
breakdown_factor /= (Train::From(this)->tcache.cached_num_engines + 2);
|
|
|
|
}
|
|
|
|
/* breakdown_chance is at least 5 (5 / 128 = ~4% of the normal chance) */
|
2021-02-01 17:07:34 +00:00
|
|
|
this->breakdown_chance_factor = std::max(breakdown_factor >> 16, (uint64)5);
|
2015-08-02 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 02:33:14 +00:00
|
|
|
int braking_accel;
|
|
|
|
if (Type == VEH_TRAIN && _settings_game.vehicle.train_braking_model == TBM_REALISTIC) {
|
|
|
|
/* Assume that every part of a train is braked, not just the engine.
|
|
|
|
* Exceptionally heavy freight trains should still have a sensible braking distance.
|
|
|
|
* The total braking force is generally larger than the total tractive force. */
|
|
|
|
braking_accel = ClampToI32((-braking_force - resistance - (this->gcache.cached_total_length * 300)) / mass);
|
|
|
|
|
|
|
|
/* Defensive driving: prevent ridiculously fast deceleration.
|
|
|
|
* -130 corresponds to a braking distance of about 6.2 tiles from 160 km/h. */
|
2021-02-04 17:20:02 +00:00
|
|
|
braking_accel = std::max(braking_accel, -(GetTrainRealisticBrakingTargetDecelerationLimit(acceleration_type) + 10));
|
2021-01-25 02:33:14 +00:00
|
|
|
} else {
|
2021-02-01 17:07:34 +00:00
|
|
|
braking_accel = ClampToI32(std::min<int64>(-braking_force - resistance, -10000) / mass);
|
2021-01-25 02:33:14 +00:00
|
|
|
}
|
|
|
|
|
2010-03-06 12:44:30 +00:00
|
|
|
if (mode == AS_ACCEL) {
|
2011-02-06 16:45:27 +00:00
|
|
|
/* Easy way out when there is no acceleration. */
|
2021-01-25 02:33:14 +00:00
|
|
|
if (force == resistance) return { 0, braking_accel };
|
2011-02-06 16:45:27 +00:00
|
|
|
|
|
|
|
/* When we accelerate, make sure we always keep doing that, even when
|
|
|
|
* the excess force is more than the mass. Otherwise a vehicle going
|
|
|
|
* down hill will never slow down enough, and a vehicle that came up
|
|
|
|
* a hill will never speed up enough to (eventually) get back to the
|
|
|
|
* same (maximum) speed. */
|
2014-07-22 19:46:10 +00:00
|
|
|
int accel = ClampToI32((force - resistance) / (mass * 4));
|
2021-02-01 17:07:34 +00:00
|
|
|
accel = force < resistance ? std::min(-1, accel) : std::max(1, accel);
|
2016-01-31 22:55:25 +00:00
|
|
|
if (this->type == VEH_TRAIN) {
|
2015-08-02 21:58:41 +00:00
|
|
|
if(_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL &&
|
2016-01-31 15:22:46 +00:00
|
|
|
HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) {
|
2015-08-02 21:58:41 +00:00
|
|
|
/* We need to apply the power reducation for non-realistic acceleration here */
|
|
|
|
uint32 power;
|
|
|
|
CalculatePower(power, max_te, true);
|
|
|
|
accel = accel * power / this->gcache.cached_power;
|
|
|
|
accel -= this->acceleration >> 1;
|
|
|
|
}
|
|
|
|
|
2016-01-31 15:22:46 +00:00
|
|
|
if (this->IsFrontEngine() && !(this->current_order_time & 0x1FF) &&
|
|
|
|
!(this->current_order.IsType(OT_LOADING)) &&
|
|
|
|
!(Train::From(this)->flags & (VRF_IS_BROKEN | (1 << VRF_TRAIN_STUCK))) &&
|
|
|
|
this->cur_speed < 3 && accel < 5) {
|
2015-08-02 22:00:39 +00:00
|
|
|
SetBit(Train::From(this)->flags, VRF_TOO_HEAVY);
|
2019-01-25 20:17:15 +00:00
|
|
|
extern std::vector<Train *> _tick_train_too_heavy_cache;
|
|
|
|
_tick_train_too_heavy_cache.push_back(Train::From(this));
|
2015-08-02 21:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 02:33:14 +00:00
|
|
|
return { accel, braking_accel };
|
2010-03-06 12:44:30 +00:00
|
|
|
} else {
|
2021-01-25 02:33:14 +00:00
|
|
|
return { braking_accel, braking_accel };
|
2010-03-06 12:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-07 15:39:46 +00:00
|
|
|
/**
|
|
|
|
* Check whether the whole vehicle chain is in the depot.
|
|
|
|
* @return true if and only if the whole chain is in the depot.
|
|
|
|
*/
|
|
|
|
template <class T, VehicleType Type>
|
|
|
|
bool GroundVehicle<T, Type>::IsChainInDepot() const
|
|
|
|
{
|
|
|
|
const T *v = this->First();
|
|
|
|
/* Is the front engine stationary in the depot? */
|
2020-12-27 10:44:22 +00:00
|
|
|
static_assert((int)TRANSPORT_RAIL == (int)VEH_TRAIN);
|
|
|
|
static_assert((int)TRANSPORT_ROAD == (int)VEH_ROAD);
|
2012-07-07 15:39:46 +00:00
|
|
|
if (!IsDepotTypeTile(v->tile, (TransportType)Type) || v->cur_speed != 0) return false;
|
|
|
|
|
|
|
|
/* Check whether the rest is also already trying to enter the depot. */
|
2019-04-10 21:07:06 +00:00
|
|
|
for (; v != nullptr; v = v->Next()) {
|
2012-07-07 15:39:46 +00:00
|
|
|
if (!v->T::IsInDepot() || v->tile != this->tile) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 18:05:04 +00:00
|
|
|
/**
|
|
|
|
* Updates vehicle's Z inclination inside a wormhole, where applicable.
|
|
|
|
*/
|
|
|
|
template <class T, VehicleType Type>
|
|
|
|
void GroundVehicle<T, Type>::UpdateZPositionInWormhole()
|
|
|
|
{
|
|
|
|
if (!IsTunnel(this->tile)) return;
|
|
|
|
|
|
|
|
const Tunnel *t = Tunnel::GetByTile(this->tile);
|
|
|
|
if (!t->is_chunnel) return;
|
|
|
|
|
|
|
|
TileIndex pos_tile = TileVirtXY(this->x_pos, this->y_pos);
|
|
|
|
|
|
|
|
ClrBit(this->gv_flags, GVF_GOINGUP_BIT);
|
|
|
|
ClrBit(this->gv_flags, GVF_GOINGDOWN_BIT);
|
|
|
|
|
|
|
|
if (pos_tile == t->tile_n || pos_tile == t->tile_s) {
|
|
|
|
this->z_pos = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int north_coord, south_coord, pos_coord;
|
|
|
|
bool going_north;
|
|
|
|
Slope slope_north;
|
|
|
|
if (t->tile_s - t->tile_n > MapMaxX()) {
|
|
|
|
// tunnel extends along Y axis (DIAGDIR_SE from north end), has same X values
|
|
|
|
north_coord = TileY(t->tile_n);
|
|
|
|
south_coord = TileY(t->tile_s);
|
|
|
|
pos_coord = TileY(pos_tile);
|
|
|
|
going_north = (this->direction == DIR_NW);
|
|
|
|
slope_north = SLOPE_NW;
|
|
|
|
} else {
|
|
|
|
// tunnel extends along X axis (DIAGDIR_SW from north end), has same Y values
|
|
|
|
north_coord = TileX(t->tile_n);
|
|
|
|
south_coord = TileX(t->tile_s);
|
|
|
|
pos_coord = TileX(pos_tile);
|
|
|
|
going_north = (this->direction == DIR_NE);
|
|
|
|
slope_north = SLOPE_NE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Slope slope = SLOPE_FLAT;
|
|
|
|
|
|
|
|
int delta;
|
|
|
|
if ((delta = pos_coord - north_coord) <= 3) {
|
|
|
|
this->z_pos = TILE_HEIGHT * (delta == 3 ? -2 : -1);
|
|
|
|
if (delta != 2) {
|
|
|
|
slope = slope_north;
|
|
|
|
SetBit(this->gv_flags, going_north ? GVF_GOINGUP_BIT : GVF_GOINGDOWN_BIT);
|
2019-02-16 21:27:08 +00:00
|
|
|
ClrBit(this->First()->vcache.cached_veh_flags, VCF_GV_ZERO_SLOPE_RESIST);
|
2017-03-05 18:05:04 +00:00
|
|
|
}
|
|
|
|
} else if ((delta = south_coord - pos_coord) <= 3) {
|
|
|
|
this->z_pos = TILE_HEIGHT * (delta == 3 ? -2 : -1);
|
|
|
|
if (delta != 2) {
|
|
|
|
slope = SLOPE_ELEVATED ^ slope_north;
|
|
|
|
SetBit(this->gv_flags, going_north ? GVF_GOINGDOWN_BIT : GVF_GOINGUP_BIT);
|
2019-02-16 21:27:08 +00:00
|
|
|
ClrBit(this->First()->vcache.cached_veh_flags, VCF_GV_ZERO_SLOPE_RESIST);
|
2017-03-05 18:05:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slope != SLOPE_FLAT) this->z_pos += GetPartialPixelZ(this->x_pos & 0xF, this->y_pos & 0xF, slope);
|
|
|
|
}
|
|
|
|
|
2010-03-06 12:44:30 +00:00
|
|
|
/* Instantiation for Train */
|
|
|
|
template struct GroundVehicle<Train, VEH_TRAIN>;
|
2010-03-06 12:54:42 +00:00
|
|
|
/* Instantiation for RoadVehicle */
|
|
|
|
template struct GroundVehicle<RoadVehicle, VEH_ROAD>;
|