18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 08:04:57 +01:00

Merge branch 'master' into feat/renderers-cleanup

This commit is contained in:
2026-11-16 21:44:48 +01:00
264 changed files with 41203 additions and 10041 deletions

View File

@@ -18,10 +18,16 @@ http://mozilla.org/MPL/2.0/.
#include "vehicle/lightsprogrammer.h"
#include "simulation/simulation.h"
#include "simulation/blackboxrecorder.h"
#include "rendering/lightarray.h"
#include "vehicle/Camera.h"
#include "vehicle/Train.h"
#include "vehicle/odometer.h"
#include "vehicle/Driver.h"
#include <algorithm>
#include <iterator>
#include <unordered_set>
#include "utilities/Globals.h"
#include "utilities/Timer.h"
#include "utilities/workerpool.h"
@@ -244,6 +250,13 @@ void TAnim::Parovoz(){
void
material_data::assign( std::string const &Replacableskin ) {
// a template puts its parameter after a folder ("tabl/(p6)"): a texture made by a script given there keeps its scheme
if( auto const scheme { Replacableskin.find( "/make:" ) };
scheme != std::string::npos
&& false == contains( Replacableskin, '|' ) ) {
assign( Replacableskin.substr( scheme + 1 ) );
return;
}
// check for the pipe method first
if( contains( Replacableskin, '|' ) ) {
cParser nameparser( Replacableskin );
@@ -2044,6 +2057,8 @@ TDynamicObject::Init(std::string Name, // nazwa pojazdu, np. "EU07-424"
*/
// utworzenie parametrów fizyki
MoverParameters = new TMoverParameters(iDirection ? fVel : -fVel, Type_Name, asName, Cab);
// the vehicle carries on with the odometer state of its earlier sessions (eu07/maszyna#72)
MoverParameters->Odometer = odometer::starting_state( odometer::states(), asName, asBaseDir );
// McZapkie: TypeName musi byc nazwą CHK/MMD pojazdu
if (!MoverParameters->LoadFIZ(asBaseDir))
{ // jak wczytanie CHK się nie uda, to błąd
@@ -8891,6 +8906,112 @@ TDynamicObject::powertrain_sounds::render( TMoverParameters const &Vehicle, doub
}
namespace {
// a train on the same track this close to the end of the player's train is taken into the physical coupler model [m]
constexpr double coupler_physics_reach { 200.0 };
// and let go of once none of its vehicles is this close to the player's train [m]; farther than the reach, so that
// the trains don't switch to and fro between the models
constexpr double coupler_physics_release { 500.0 };
// the vehicle and those coupled with it, one after the other
std::vector<TDynamicObject *>
coupled_train( TDynamicObject *Vehicle ) {
std::vector<TDynamicObject *> train { Vehicle };
for( int direction = end::front; direction <= end::rear; ++direction ) {
auto *vehicle { Vehicle };
auto side { direction };
// a train of more vehicles than are on the tracks would be one coupled into a ring
while( train.size() < 10000 ) {
auto const &mover { *vehicle->MoverParameters };
auto *next { mover.Neighbours[ side ].vehicle };
if( next == nullptr
|| ( mover.Couplers[ side ].CouplingFlag & coupling::coupler ) == 0
|| std::ranges::find( train, next ) != train.end() ) {
break;
}
train.emplace_back( next );
// onwards from the end of the next vehicle away from this one
side = 1 - mover.Neighbours[ side ].vehicle_end;
vehicle = next;
}
}
return train;
}
// the trains on the same track the player's train is closing in on: those found at its free ends, near enough
std::vector<TDynamicObject *>
trains_closing_in( std::vector<TDynamicObject *> const &Train ) {
std::vector<TDynamicObject *> trains;
for( auto *vehicle : Train ) {
for( int side = end::front; side <= end::rear; ++side ) {
auto const &mover { *vehicle->MoverParameters };
auto const &neighbour { mover.Neighbours[ side ] };
if( neighbour.vehicle == nullptr
|| ( mover.Couplers[ side ].CouplingFlag & coupling::coupler ) != 0
|| neighbour.distance > coupler_physics_reach
|| std::ranges::find( Train, neighbour.vehicle ) != Train.end() ) {
continue;
}
std::ranges::copy( coupled_train( neighbour.vehicle ), std::back_inserter( trains ) );
}
}
return trains;
}
// whether any vehicle of the train is within the distance of any vehicle of the other one
bool
trains_within( std::vector<TDynamicObject *> const &Train, std::vector<TDynamicObject *> const &Other, double const Distance ) {
return std::ranges::any_of( Train, [&]( TDynamicObject const *Vehicle ) {
return std::ranges::any_of( Other, [&]( TDynamicObject const *Othervehicle ) {
return glm::distance( Vehicle->GetPosition(), Othervehicle->GetPosition() ) <= Distance; } ); } );
}
} // namespace
// the physical coupler model covers the player's train and the trains it closes in on, on the same track; the others
// keep the legacy model, as the AI drives them in it
void
vehicle_table::update_coupler_physics() const {
if( false == Global.coupler_physics
|| true == Global.coupler_physics_all ) {
return;
}
auto *player { simulation::Train != nullptr ? simulation::Train->Dynamic() : nullptr };
std::vector<TDynamicObject *> train;
if( player != nullptr ) {
train = coupled_train( player );
}
std::unordered_set<TDynamicObject *> covered { train.begin(), train.end() };
for( auto *vehicle : trains_closing_in( train ) ) {
covered.insert( vehicle );
}
std::unordered_set<TDynamicObject *> checked;
for( auto *vehicle : m_items ) {
if( covered.contains( vehicle ) ) {
vehicle->MoverParameters->CouplerPhysics = true;
continue;
}
if( false == vehicle->MoverParameters->CouplerPhysics
|| checked.contains( vehicle ) ) {
continue;
}
// a train taken in before stays in the model until it's far from the player's one, all of it alike
auto const other { coupled_train( vehicle ) };
checked.insert( other.begin(), other.end() );
if( false == trains_within( other, train, coupler_physics_release ) ) {
for( auto *othervehicle : other ) {
othervehicle->MoverParameters->CouplerPhysics = false;
}
}
}
}
// legacy method, calculates changes in simulation state over specified time
void
vehicle_table::update( double Deltatime, int Iterationcount ) {
@@ -8967,6 +9088,7 @@ vehicle_table::update( double Deltatime, int Iterationcount ) {
vehicle->MoverParameters->ComputeConstans();
vehicle->update_neighbours();
}
update_coupler_physics();
Timer::subsystem.vehicles_prepare.stop();
Timer::subsystem.vehicles_forces.start();
auto const totaltime { Deltatime * Iterationcount }; // całkowity czas
@@ -9050,6 +9172,26 @@ vehicle_table::update( double Deltatime, int Iterationcount ) {
// jeśli jest coś do usunięcia z listy, to trzeba na końcu
erase_disabled();
// the odometers are saved now and then, a crash doesn't lose more than the last minute
m_odometersavetimer += totaltime;
if( m_odometersavetimer >= 60.0 ) {
save_odometers();
}
}
// records the odometer states of the vehicles and saves them for the next sessions (eu07/maszyna#72)
void
vehicle_table::save_odometers() {
m_odometersavetimer = 0.0;
auto &odometers { odometer::states() };
for( auto const *vehicle : m_items ) {
if( vehicle->MoverParameters != nullptr ) {
odometer::record( odometers, vehicle->name(), vehicle->MoverParameters->HasDriverCab(), vehicle->MoverParameters->Odometer );
}
}
odometer::save();
}
// the steps of the physics of vehicle_table::update() (UpdateForce() and FastUpdate() of each vehicle, Iterationcount times) on up
@@ -9306,6 +9448,11 @@ vehicle_table::erase_disabled() {
++vehicleiter;
}
else {
// the distance the vehicle ran goes in the odometer store now, it doesn't depend on the vehicle being
// kept on the list until the next save (eu07/maszyna#72)
if( vehicle->MoverParameters != nullptr ) {
odometer::record( odometer::states(), vehicle->name(), vehicle->MoverParameters->HasDriverCab(), vehicle->MoverParameters->Odometer );
}
if( vehicle->MyTrack != nullptr
&& true == vehicle->MyTrack->RemoveDynamicObject(vehicle) ) {
vehicle->MyTrack = nullptr;
@@ -9319,6 +9466,7 @@ vehicle_table::erase_disabled() {
simulation::Trains.purge(vehicle->name());
// remove potential entries in the light array
simulation::Lights.remove( vehicle );
simulation::Blackbox.vehicle_removed( vehicle );
/*
// finally get rid of the vehicle and its record themselves
// BUG: deleting the vehicle leaves dangling pointers in event->Activator and potentially elsewhere