mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-01 22:07:55 +01:00
d0387753f8
The physics of a vehicle loses the name it had from the Pascal of 2001. Done by
tools/rename/mover_to_physics.py, committed with it:
physics/mover.h physics/vehicle.h
TMoverParameters physics::Vehicle (in namespace physics)
TDynamicObject::MoverParameters physics
mvOccupied, mvControlled occupied, controlled (TTrain, TController)
mvSecond, mvThird secondUnit, thirdUnit
mvPantographUnit pantographUnit
mover, Mover, movers physics, Physics, vehicles (locals, params)
m_mover, othermover, currentMover, from_movers, renderMoverEventSounds
m_physics, otherPhysics, currentPhysics,
from_vehicles, renderPhysicsEventSounds
The code and the comments are renamed, the string literals are not: no text
the game shows, logs or looks up has changed (none had the names). The movers
of the editor, the tracks a slide moves, keep their name.
By hand: two member functions of TDynamicObject (gather_off_track_terrain,
begin_off_track) had a local mover aliasing their own MoverParameters, which
the rename made physics { *physics }; the alias is gone, they use physics->.
No other function has a renamed local meeting a renamed member (checked over
all the functions of the code before the rename).
For the branches made before it: run the script on the branch (python
tools/rename/mover_to_physics.py, from the root of the repository) before
merging it with this one, and the names don't conflict. It can be run again;
the second time it changes nothing.
The class is the same as it was; breaking it into its parts (braking,
electrics, controls, load) and making its data private are the next stages.
No change in behaviour: 1149/1149 tests, golden traces exact against staging
62f7a29b.
307 lines
12 KiB
C++
307 lines
12 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
/*
|
|
MaSzyna EU07 locomotive simulator
|
|
Copyright (C) 2001-2004 Marcin Wozniak, Maciej Czapkiewicz and others
|
|
|
|
*/
|
|
|
|
// the load of a vehicle, its exchange at the stops, how it shows
|
|
|
|
#include "stdafx.h"
|
|
#include "vehicle/DynObj.h"
|
|
#include "physics/vehicle.h"
|
|
#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/pulleemode.h"
|
|
#include "vehicle/Driver.h"
|
|
#include <algorithm>
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <format>
|
|
#include <random>
|
|
#include <iterator>
|
|
#include <numbers>
|
|
#include <optional>
|
|
#include <string_view>
|
|
#include <unordered_set>
|
|
#include <utility>
|
|
#include <vector>
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/Timer.h"
|
|
#include "utilities/workerpool.h"
|
|
#include "utilities/Logs.h"
|
|
#include "utilities/glmHelpers.h"
|
|
#include "Console.h"
|
|
#include "world/Traction.h"
|
|
#include "audio/sound.h"
|
|
#include "model/MdlMngr.h"
|
|
#include "model/Model3d.h"
|
|
#include "vehicle/doorwings.h"
|
|
#include "rendering/renderer.h"
|
|
#include "editor/editorTerrainStreamer.hpp"
|
|
#include "application/uitranscripts.h"
|
|
#include "input/messaging.h"
|
|
#include "application/application.h"
|
|
#include "vehicle/Driver.h"
|
|
#include "vehicle/dynamic/internal.h"
|
|
|
|
|
|
// initiates load change by specified amounts, with a platform on specified side
|
|
void TDynamicObject::LoadExchange( int const Disembark, int const Embark, int const Platforms ) {
|
|
/*
|
|
if( ( physics->Doors.open_control == control_t::passenger )
|
|
|| ( physics->Doors.open_control == control_t::mixed ) ) {
|
|
// jeśli jedzie do tyłu, to drzwi otwiera odwrotnie
|
|
auto const lewe = ( DirectionGet() > 0 ) ? 1 : 2;
|
|
auto const prawe = 3 - lewe;
|
|
if( Platform & lewe ) {
|
|
physics->OperateDoors( VehicleSide::left, true, range_t::local );
|
|
}
|
|
if( Platform & prawe ) {
|
|
physics->OperateDoors( VehicleSide::right, true, range_t::local );
|
|
}
|
|
}
|
|
*/
|
|
if( Platforms == 0 ) { return; } // edge case, if there's no accessible platforms discard the request
|
|
|
|
m_exchange.unload_count += Disembark;
|
|
m_exchange.load_count += Embark;
|
|
m_exchange.platforms = Platforms;
|
|
m_exchange.time = 0.0;
|
|
}
|
|
|
|
// calculates time needed to complete current load change
|
|
float TDynamicObject::LoadExchangeTime( int const Platforms ) {
|
|
|
|
m_exchange.platforms = Platforms;
|
|
return LoadExchangeTime();
|
|
}
|
|
|
|
float TDynamicObject::LoadExchangeTime() const {
|
|
|
|
if( m_exchange.unload_count < 0.01 && m_exchange.load_count < 0.01 ) { return 0.f; }
|
|
|
|
auto const baseexchangetime { m_exchange.unload_count / physics->UnLoadSpeed + m_exchange.load_count / physics->LoadSpeed };
|
|
auto const nominalexchangespeedfactor { ( m_exchange.platforms == 3 ? 2.f : 1.f ) };
|
|
auto const actualexchangespeedfactor { LoadExchangeSpeed() };
|
|
|
|
return baseexchangetime / ( actualexchangespeedfactor > 0.f ? actualexchangespeedfactor : nominalexchangespeedfactor );
|
|
}
|
|
|
|
// calculates current load exchange rate
|
|
float TDynamicObject::LoadExchangeSpeed() const {
|
|
// platforms (1:left, 2:right, 3:both)
|
|
// with exchange performed on both sides waiting times are halved
|
|
auto exchangespeedfactor { 0.f };
|
|
auto const lewe { DirectionGet() > 0 ? 1 : 2 };
|
|
auto const prawe { 3 - lewe };
|
|
if( m_exchange.platforms & lewe ) {
|
|
exchangespeedfactor += physics->Doors.instances[VehicleSide::left].is_open ? 1.f : 0.f;
|
|
}
|
|
if( m_exchange.platforms & prawe ) {
|
|
exchangespeedfactor += physics->Doors.instances[VehicleSide::right].is_open ? 1.f : 0.f;
|
|
}
|
|
|
|
return exchangespeedfactor;
|
|
}
|
|
|
|
// update state of load exchange operation
|
|
void TDynamicObject::update_exchange( double const Deltatime ) {
|
|
|
|
// TODO: move offset calculation after initial check, when the loading system is all unified
|
|
update_load_offset();
|
|
|
|
if( m_exchange.unload_count < 0.01 && m_exchange.load_count < 0.01 ) { return; }
|
|
|
|
if( physics->Vel < 2.0 ) {
|
|
|
|
auto const exchangespeed { LoadExchangeSpeed() };
|
|
|
|
if( exchangespeed < ( m_exchange.platforms == 3 ? 2.f : 1.f ) ) {
|
|
// the exchange isn't performed at optimal rate, or at all. try to open viable vehicle doors to speed it up
|
|
auto const lewe { DirectionGet() > 0 ? 1 : 2 };
|
|
auto const prawe { 3 - lewe };
|
|
if( m_exchange.platforms & lewe
|
|
&& false == (physics->Doors.instances[VehicleSide::left].is_open || physics->Doors.instances[VehicleSide::left].is_opening) ) {
|
|
// try to open left door
|
|
physics->OperateDoors( VehicleSide::left, true, range_t::local );
|
|
}
|
|
if( m_exchange.platforms & prawe
|
|
&& false == (physics->Doors.instances[VehicleSide::right].is_open || physics->Doors.instances[VehicleSide::right].is_opening) ) {
|
|
// try to open right door
|
|
physics->OperateDoors( VehicleSide::right, true, range_t::local );
|
|
}
|
|
}
|
|
|
|
if( exchangespeed > 0.f ) {
|
|
|
|
m_exchange.time += Deltatime;
|
|
while( m_exchange.unload_count > 0.01
|
|
&& m_exchange.time >= 1.0 ) {
|
|
|
|
m_exchange.time -= 1.0;
|
|
auto const exchangesize = std::min( m_exchange.unload_count, physics->UnLoadSpeed * exchangespeed );
|
|
m_exchange.unload_count -= exchangesize;
|
|
physics->LoadStatus = 1;
|
|
physics->LoadAmount = std::max( 0.f, physics->LoadAmount - exchangesize );
|
|
physics->ComputeMass();
|
|
update_load_visibility();
|
|
}
|
|
if( m_exchange.unload_count < 0.01 ) {
|
|
// finish any potential unloading operation before adding new load
|
|
// don't load more than can fit
|
|
m_exchange.load_count = std::min( m_exchange.load_count, physics->MaxLoad - physics->LoadAmount );
|
|
while( m_exchange.load_count > 0.01
|
|
&& m_exchange.time >= 1.0 ) {
|
|
|
|
m_exchange.time -= 1.0;
|
|
auto const exchangesize = std::min( m_exchange.load_count, physics->LoadSpeed * exchangespeed );
|
|
m_exchange.load_count -= exchangesize;
|
|
physics->LoadStatus = 2;
|
|
physics->LoadAmount = std::min( physics->MaxLoad, physics->LoadAmount + exchangesize ); // std::max not strictly needed but, eh
|
|
physics->ComputeMass();
|
|
update_load_visibility();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if( physics->Vel > 2.0 ) {
|
|
// if the vehicle moves too fast cancel the exchange
|
|
m_exchange.unload_count = 0;
|
|
m_exchange.load_count = 0;
|
|
}
|
|
|
|
if( m_exchange.unload_count < 0.01
|
|
&& m_exchange.load_count < 0.01 ) {
|
|
|
|
physics->LoadStatus = 4;
|
|
// if the exchange is completed (or canceled) close the door, if applicable
|
|
if( physics->Doors.close_control == control_t::passenger
|
|
|| physics->Doors.close_control == control_t::mixed ) {
|
|
|
|
if( physics->Vel > 2.0
|
|
|| Random() < (
|
|
// remotely controlled door are more likely to be left open
|
|
physics->Doors.close_control == control_t::passenger ? 0.75 : 0.50) ) {
|
|
|
|
physics->OperateDoors( VehicleSide::left, false, range_t::local );
|
|
physics->OperateDoors( VehicleSide::right, false, range_t::local );
|
|
}
|
|
}
|
|
// if the vehicle was emptied potentially switch load visualization model
|
|
if( physics->LoadAmount == 0 ) {
|
|
physics->AssignLoad( "" );
|
|
}
|
|
}
|
|
}
|
|
|
|
void TDynamicObject::LoadUpdate() {
|
|
|
|
physics->LoadStatus &= 1 | 2; // po zakończeniu będzie równe zero
|
|
// przeładowanie modelu ładunku
|
|
if( physics->LoadTypeChange ) {
|
|
// whether we succeed or not don't try more than once
|
|
physics->LoadTypeChange = false;
|
|
// bieżąca ścieżka do tekstur to dynamic/...
|
|
Global.asCurrentTexturePath = asBaseDir;
|
|
mdLoad = LoadMMediaFile_mdload( physics->LoadType.name );
|
|
// TODO: discern from vehicle component which merely uses vehicle directory and has no animations, so it can be initialized outright
|
|
// and actual vehicles which get their initialization after their animations are set up
|
|
if( mdLoad != nullptr ) {
|
|
mdLoad->Init();
|
|
}
|
|
// update bindings between lowpoly sections and potential load chunks placed inside them
|
|
update_load_sections();
|
|
// z powrotem defaultowa sciezka do tekstur
|
|
Global.asCurrentTexturePath = std::string( paths::textures );
|
|
}
|
|
}
|
|
|
|
void
|
|
TDynamicObject::update_load_sections() {
|
|
|
|
SectionLoadOrder.clear();
|
|
|
|
for( auto §ion : Sections ) {
|
|
|
|
section.load = GetSubmodelFromName( mdLoad, section.compartment->pName );
|
|
|
|
if( section.load != nullptr ) {
|
|
// create entry for each load model chunk assigned to the section
|
|
// TBD, TODO: store also pointer to chunk submodel and control its visibility more directly, instead of per-section visibility flag?
|
|
auto loadchunkcount { section.load->count_children() };
|
|
while( loadchunkcount-- ) {
|
|
SectionLoadOrder.push_back( §ion );
|
|
}
|
|
// HACK: disable automatic self-illumination threshold, at least until 3d model update
|
|
if( physics->CompartmentLights.start_type == start_t::manual ) {
|
|
section.load->SetSelfIllum( 2.0f, true, false );
|
|
}
|
|
}
|
|
}
|
|
shuffle_load_order();
|
|
}
|
|
|
|
void
|
|
TDynamicObject::update_load_visibility() {
|
|
// start with clean load chunk visibility slate
|
|
for( auto §ion : Sections ) {
|
|
section.load_chunks_visible = 0;
|
|
}
|
|
// each entry in load order sequence matches a single chunk of the section it points to
|
|
// the length of load order sequence matches total number of load chunks
|
|
auto const loadpercentage { (
|
|
physics->MaxLoad == 0.f ?
|
|
0.0 :
|
|
physics->LoadAmount / physics->MaxLoad ) };
|
|
auto visiblechunkcount { (
|
|
SectionLoadOrder.empty() ?
|
|
0 :
|
|
static_cast<int>( std::ceil( loadpercentage * SectionLoadOrder.size() - 0.001f ) ) ) };
|
|
for( auto *section : SectionLoadOrder ) {
|
|
if( visiblechunkcount == 0 ) { break; }
|
|
section->load_chunks_visible++;
|
|
--visiblechunkcount;
|
|
}
|
|
}
|
|
|
|
void
|
|
TDynamicObject::update_load_offset() {
|
|
|
|
if( physics->LoadType.offset_min == 0.f ) { return; }
|
|
|
|
auto const loadpercentage { (
|
|
physics->MaxLoad == 0.f ?
|
|
0.0 :
|
|
100.0 * physics->LoadAmount / physics->MaxLoad ) };
|
|
|
|
LoadOffset = std::lerp( physics->LoadType.offset_min, 0.f, safe_clamp( 0.0, 1.0, loadpercentage * 0.01 ) );
|
|
}
|
|
|
|
void
|
|
TDynamicObject::shuffle_load_order() {
|
|
|
|
// order of visible load chunks, nothing security related -- see the engines in Globals.h
|
|
std::shuffle( std::begin( SectionLoadOrder ), std::end( SectionLoadOrder ), Global.random_engine ); // NOSONAR
|
|
// shift chunks assigned to corridors to the end of the list, so they show up last
|
|
std::stable_partition(
|
|
std::begin( SectionLoadOrder ), std::end( SectionLoadOrder ),
|
|
[]( vehicle_section const *section ) {
|
|
return section->compartment->pName.find("compartment") == 0 || section->compartment->pName.find("przedzial") == 0; } );
|
|
// NOTE: potentially we're left with a mix of corridor and external section loads
|
|
// but that's not necessarily a wrong outcome, so we leave it this way for the time being
|
|
}
|