/* 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 */ // coupling and uncoupling a vehicle, the adapters of its couplers #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 #include #include #include #include #include #include #include #include #include #include #include #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" int TDynamicObject::DettachStatus(int dir) { // sprawdzenie odległości sprzęgów // rzeczywistych od strony (dir): // 0=przód,1=tył // Ra: dziwne, że ta funkcja nie jest używana if( physics->Couplers[ dir ].CouplingFlag == Coupling::none ) { return 0; // jeśli nic nie podłączone, to jest OK } return physics->DettachStatus(dir); // czy jest w odpowiedniej odległości? } int TDynamicObject::Dettach(int dir) { // rozłączenie sprzęgów rzeczywistych od // strony (dir): 0=przód,1=tył // zwraca maskę bitową aktualnych sprzegów (0 jeśli rozłączony) if (ctOwner) { // jeśli pojazd ma przypisany obiekt nadzorujący skład, to póki // są wskaźniki TDynamicObject *d = this; while (d) { d->ctOwner = nullptr; // usuwanie właściciela d = d->Prev(); } d = Next(); while (d) { d->ctOwner = nullptr; // usuwanie właściciela d = d->Next(); // i w drugą stronę } } if( physics->Couplers[ dir ].CouplingFlag ) { // odczepianie, o ile coś podłączone physics->Dettach( dir ); /* if( true == physics->Dettach( dir ) ) { auto *othervehicle { dir ? NextConnected : PrevConnected }; auto const othercoupler { dir ? NextConnectedNo() : PrevConnectedNo() }; ( othercoupler ? othervehicle->NextConnected : othervehicle->PrevConnected ) = nullptr; ( dir ? NextConnected : PrevConnected ) = nullptr; }; */ } // sprzęg po rozłączaniu (czego się nie da odpiąć return physics->Couplers[dir].CouplingFlag.Bits(); } void TDynamicObject::couple( VehicleEnd const Side ) { auto const &neighbour { physics->Neighbours[ Side ] }; if( neighbour.vehicle == nullptr ) { return; } auto const &coupler { physics->Couplers[ Side ] }; auto *othervehicle { neighbour.vehicle }; auto *othervehicleparams{ othervehicle->physics }; auto const &othercoupler { othervehicleparams->Couplers[ neighbour.vehicle_end ] }; if( coupler.CouplingFlag == Coupling::none ) { // najpierw hak if( ( coupler.AllowedFlag & othercoupler.AllowedFlag & Coupling::coupler ) == Coupling::coupler ) { if( physics->Attach( Side, neighbour.FacingEnd(), othervehicleparams, Coupling::coupler ) ) { // one coupling type per key press return; } else { WriteLog( "Mechanical coupling failed." ); } } } if( false == physics->Couplers[ Side ].CouplingFlag.HasAll(Coupling::brakeHose) ) { // pneumatyka if( ( coupler.AllowedFlag & othercoupler.AllowedFlag & Coupling::brakeHose ) == Coupling::brakeHose ) { if( physics->Attach( Side, neighbour.FacingEnd(), othervehicleparams, coupler.CouplingFlag | Coupling::brakeHose ) ) { SetPneumatic( Side != VehicleEnd::front, true ); othervehicle->SetPneumatic( Side != VehicleEnd::front, true ); // one coupling type per key press return; } } } if( false == physics->Couplers[ Side ].CouplingFlag.HasAll(Coupling::mainHose) ) { // zasilajacy if( ( coupler.AllowedFlag & othercoupler.AllowedFlag & Coupling::mainHose ) == Coupling::mainHose ) { if( physics->Attach( Side, neighbour.FacingEnd(), othervehicleparams, coupler.CouplingFlag | Coupling::mainHose ) ) { SetPneumatic( Side != VehicleEnd::front, false ); othervehicle->SetPneumatic( Side != VehicleEnd::front, false ); // one coupling type per key press return; } } } if( false == physics->Couplers[ Side ].CouplingFlag.HasAll(Coupling::control) ) { // ukrotnionko if( (coupler.AllowedFlag & othercoupler.AllowedFlag & Coupling::control) == Coupling::control && coupler.control_type == othercoupler.control_type && physics->CompatibleCoupling( Side, *othervehicleparams, neighbour.FacingEnd(), coupler.CouplingFlag | Coupling::control ).HasAny( Coupling::control ) ) { if( physics->Attach( Side, neighbour.FacingEnd(), othervehicleparams, coupler.CouplingFlag | Coupling::control ) ) { // one coupling type per key press return; } } } if( false == physics->Couplers[ Side ].CouplingFlag.HasAll(Coupling::gangway) ) { // mostek if( ( coupler.AllowedFlag & othercoupler.AllowedFlag & Coupling::gangway ) == Coupling::gangway ) { if( physics->Attach( Side, neighbour.FacingEnd(), othervehicleparams, coupler.CouplingFlag | Coupling::gangway ) ) { // one coupling type per key press return; } } } if( false == physics->Couplers[ Side ].CouplingFlag.HasAll(Coupling::heating) ) { // heating if( ( coupler.AllowedFlag & othercoupler.AllowedFlag & Coupling::heating ) == Coupling::heating ) { if( physics->Attach( Side, neighbour.FacingEnd(), othervehicleparams, coupler.CouplingFlag | Coupling::heating ) ) { // one coupling type per key press return; } } } } int TDynamicObject::uncouple( VehicleEnd const Side ) { if( DettachStatus( NumberOf( Side ) ) >= 0 || true == physics->Couplers[Side].CouplingFlag.HasAll(Coupling::permanent) ) { // can't uncouple, return existing coupling state return physics->Couplers[ Side ].CouplingFlag.Bits(); } // jeżeli sprzęg niezablokowany, jest co odczepić i się da auto const couplingflag { Dettach( NumberOf( Side ) ) }; return couplingflag; } bool TDynamicObject::attach_coupler_adapter( VehicleEnd const Side, bool const Enforce ) { auto &coupler { physics->Couplers[ Side ] }; // sanity check(s) if( coupler.type() == TCouplerType::Automatic ) { return false; } auto const *neighbour { physics->Neighbours[ Side ].vehicle }; if( neighbour == nullptr || physics->Neighbours[Side].distance > 25.0 ) { // can only acquire the adapter from a nearby enough vehicle return false; } // TBD: empty struct instead of fallback defaults, to allow vehicles without adapter? auto adapterdata { coupleradapter_data { { 0.085f, 0.95f }, "tabor/polsprzeg" } }; if( false == neighbour->m_coupleradapter.model.empty() ) { // explicit coupler adapter definition overrides default parameters adapterdata = neighbour->m_coupleradapter; } if( physics->Neighbours[Side].distance - adapterdata.position.x < 0.5 && false == Enforce ) { // arbitrary amount of free room required to install the adapter // NOTE: this also covers cases with established physical connection return false; } coupler.adapter_type = TCouplerType::Automatic; coupler.adapter_length = adapterdata.position.x; coupler.adapter_height = adapterdata.position.y; // audio flag, visuals update coupler.sounds |= sound::attachadapter; m_coupleradapters[ Side ] = TModelsManager::GetModel( adapterdata.model ); return true; } bool TDynamicObject::remove_coupler_adapter( VehicleEnd const Side ) { auto &coupler{ physics->Couplers[ Side ] }; if( coupler.adapter_type == TCouplerType::NoCoupler ) { return false; } // TODO: sanity check(s) if( coupler.Connected != nullptr ) { // TBD: disallow instead adapter removal if it's coupled with another vehicle? uncouple( Side ); } coupler.adapter_type = TCouplerType::NoCoupler; coupler.adapter_length = 0.0; coupler.adapter_height = 0.0; // audio flag, visuals update coupler.sounds |= sound::removeadapter; m_coupleradapters[ Side ] = nullptr; return true; } void TDynamicObject::AttachNext(TDynamicObject *Object, int iType) { // Ra: doczepia Object na końcu składu (nazwa funkcji może być myląca) // Ra: używane tylko przy wczytywaniu scenerii auto const vehicleend { iDirection }; auto const othervehicleend { Object->iDirection ^ 1 }; physics->Attach( vehicleend, othervehicleend, Object->physics, CouplingSet::FromBits( iType ), true, false ); // update neighbour data for both affected vehicles update_neighbours(); Object->update_neighbours(); // potentially attach automatic coupler adapter to allow the connection // HACK: we're doing it after establishin actual connection, as the method needs valid neighbour data auto &coupler { physics->Couplers[ vehicleend ] }; auto &othercoupler { Object->physics->Couplers[ ( othervehicleend != 2 ? othervehicleend : coupler.ConnectedNr ) ] }; if( coupler.type() != othercoupler.type() ) { if( othercoupler.type() == TCouplerType::Automatic ) { // try to attach adapter to the vehicle attach_coupler_adapter( EndOf( vehicleend ), true ); } else if( coupler.type() == TCouplerType::Automatic ) { // try to attach adapter to the other vehicle Object->attach_coupler_adapter( EndOf( othervehicleend != 2 ? othervehicleend : coupler.ConnectedNr ), true ); } // update distance to neighbours on account of potentially attached adapter update_neighbours(); Object->update_neighbours(); } // potentially adjust vehicle position to avoid collision at the simulation start if( physics->Neighbours[ vehicleend ].distance > -0.001 ) { return; } Object->Move( physics->Neighbours[ vehicleend ].distance * Object->DirectionGet() ); // HACK: manually update vehicle position as it's used by neighbour distance update we do next Object->physics->Loc = { -Object->vPosition.x, Object->vPosition.z, Object->vPosition.y }; // update neighbour distance data after moving our vehicle update_neighbours(); Object->update_neighbours(); }