/* 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/. */ // the doors and the exchange of the load #include "stdafx.h" #include "physics/vehicle.h" #include "vehicle/DynObj.h" #include "physics/oerlikonest.h" #include "utilities/Globals.h" #include "utilities/Logs.h" #include "utilities/Timer.h" #include "utilities/parser.h" #include "simulation/simulation.h" #include "vehicle/fieldshunting.h" #include #include #include #include #include #include #include #include #include "physics/components/internal.h" bool physics::Vehicle::AssignLoad(std::string const &Loadname, float const Amount) { if (Loadname == "pantstate") { if (EnginePowerSource.SourceType == TPowerSource::CurrentCollector) { // wartość niby "pantstate" - nazwa dla formalności, ważna jest ilość auto const pantographsetup{static_cast(Amount)}; if (pantographsetup & 1 << 2) { DoubleTr = -1; } if (pantographsetup & 1 << 0) { if (DoubleTr == 1) { OperatePantographValve(VehicleEnd::front, operation_t::enable, range_t::local); } else { OperatePantographValve(VehicleEnd::rear, operation_t::enable, range_t::local); } } if (pantographsetup & 1 << 1) { if (DoubleTr == 1) { OperatePantographValve(VehicleEnd::rear, operation_t::enable, range_t::local); } else { OperatePantographValve(VehicleEnd::front, operation_t::enable, range_t::local); } } return true; } else { return false; } } if (Loadname.empty()) { // empty the vehicle if requested loadTypeChange = LoadType.name != Loadname; LoadType = load_attributes(); loadAmount = 0.f; return true; } // can't mix load types, at least for the time being if (loadAmount > 0 && LoadType.name != Loadname) { return false; } auto const loadattributes = std::ranges::find_if(LoadAttributes, [&](auto const &attributes) { return attributes.name == Loadname; }); if (loadattributes == std::end(LoadAttributes)) { // didn't find matching load configuration, this type is unsupported return false; } loadTypeChange = LoadType.name != Loadname; LoadType = *loadattributes; loadAmount = safe_clamp(Amount, 0.f, maxLoad); ComputeMass(); return true; } // a step of the exchange of the load: so much taken out, the mass with it void physics::Vehicle::UnloadStep(float const Amount) { loadStatus = 1; loadAmount = std::max(0.f, loadAmount - Amount); ComputeMass(); } // a step of the exchange of the load: so much put in, up to the capacity, the mass with it void physics::Vehicle::LoadStep(float const Amount) { loadStatus = 2; loadAmount = std::min(maxLoad, loadAmount + Amount); // std::max not strictly needed but, eh ComputeMass(); } // ************************************************************************************************* // Q: 20160713 // Test zakończenia załadunku / rozładunku // ************************************************************************************************* bool physics::Vehicle::LoadingDone(double const LSpeed, std::string_view Loadname) { if (LSpeed == 0.0) { // zerowa prędkość zmiany, to koniec loadStatus = 4; return true; } if (Loadname.empty()) { return loadStatus >= 4; } if (Loadname != LoadType.name) { return loadStatus >= 4; } // test zakończenia załadunku/rozładunku // load exchange speed is reduced if the wagon is overloaded auto const loadchange{static_cast(std::abs(LSpeed * LastLoadChangeTime) * (loadAmount > maxLoad ? 0.5 : 1.0))}; if (LSpeed < 0) { // gdy rozładunek loadStatus = 2; // trwa rozładunek (włączenie naliczania czasu) if (loadchange > 0) // jeśli coś przeładowano { LastLoadChangeTime = 0; // naliczony czas został zużyty loadAmount -= loadchange; // zmniejszenie ilości ładunku CommandIn.Value1 -= loadchange; // zmniejszenie ilości do rozładowania if (loadAmount <= 0 || CommandIn.Value1 <= 0) { // pusto lub rozładowano żądaną ilość loadStatus = 4; // skończony rozładunek loadAmount = safe_clamp(loadAmount, 0.f, maxLoad); // ładunek nie może być ujemny } if (loadAmount == 0.f) { AssignLoad(""); // jak nic nie ma, to nie ma też nazwy } ComputeMass(); } } else if (LSpeed > 0) { // gdy załadunek loadStatus = 1; // trwa załadunek (włączenie naliczania czasu) if (loadchange > 0) // jeśli coś przeładowano { LastLoadChangeTime = 0; // naliczony czas został zużyty loadAmount += loadchange; // zwiększenie ładunku CommandIn.Value1 -= loadchange; if (loadAmount >= maxLoad * (1.0 + OverLoadFactor) || CommandIn.Value1 <= 0) { loadStatus = 4; // skończony załadunek loadAmount = std::min(static_cast(maxLoad * (1.0 + OverLoadFactor)), loadAmount); } ComputeMass(); } } return loadStatus >= 4; } bool physics::Vehicle::ChangeDoorPermitPreset(int const Change, range_t const Notify) { auto const initialstate{Doors.permit_preset}; if (false == Doors.permit_presets.empty()) { Doors.permit_preset = safe_clamp(Doors.permit_preset + Change, 0, static_cast(Doors.permit_presets.size() - 1)); auto const doors{Doors.permit_presets[Doors.permit_preset]}; auto const permitleft{((doors & 1) != 0)}; auto const permitright{((doors & 2) != 0)}; PermitDoors(CabActive > 0 ? VehicleSide::left : VehicleSide::right, permitleft, Notify); PermitDoors(CabActive > 0 ? VehicleSide::right : VehicleSide::left, permitright, Notify); } return Doors.permit_preset != initialstate; } bool physics::Vehicle::PermitDoorStep(bool const State, range_t const Notify) { auto const initialstate{Doors.step_enabled}; Doors.step_enabled = State; if (Notify != range_t::local) { // wysłanie wyłączenia do pozostałych? SendCtrlToNext(VehicleCommand::doorStep, State == true ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return Doors.step_enabled != initialstate; } bool physics::Vehicle::PermitDoors(VehicleSide const Door, bool const State, range_t const Notify) { bool const initialstate{Doors.instances[Door].open_permit}; PermitDoors_(Door, State); if (Notify != range_t::local) { SendCtrlToNext(VehicleCommand::doorPermit, (State ? 1 : -1) // positive: grant, negative: revoke * (Door == (CabActive > 0 ? VehicleSide::left : VehicleSide::right) ? // 1=lewe, 2=prawe (swap if reversed) 1 : 2), CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return Doors.instances[Door].open_permit != initialstate; } void physics::Vehicle::PermitDoors_(VehicleSide const Door, bool const State) { if (State && State != Doors.instances[Door].open_permit) { SetFlag(soundFlag, sound::doorpermit); } Doors.instances[Door].open_permit = State; } bool physics::Vehicle::ChangeDoorControlMode(bool const State, range_t const Notify) { auto const initialstate{Doors.remote_only}; Doors.remote_only = State; if (Notify != range_t::local) { // wysłanie wyłączenia do pozostałych? SendCtrlToNext(VehicleCommand::doorMode, State == true ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } if (true == State) { // when door are put in remote control mode they're automatically open // TBD, TODO: make it dependant on config switch? OperateDoors(VehicleSide::left, true); OperateDoors(VehicleSide::right, true); } return Doors.remote_only != initialstate; } bool physics::Vehicle::OperateDoors(VehicleSide const Door, bool const State, range_t const Notify) { auto &door{Doors.instances[Door]}; /* if( ( State == true ? door.is_open : door.is_closed ) ) { // TBD: should the command be passed to other vehicles regardless of whether it affected the primary target? // (for the time being no, methods are often invoked blindly which would lead to commands spam) return false; } */ bool result{false}; if (Notify == range_t::local) { door.local_open = State; door.local_close = false == State; result = true; } else { // remote door operation signals require power to propagate if (lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable()) { door.remote_open = State; door.remote_close = false == State; result = true; } } if (Notify != range_t::local) { SendCtrlToNext(State == true ? VehicleCommand::doorOpen : VehicleCommand::doorClose, Door == (CabActive > 0 ? VehicleSide::left : VehicleSide::right) ? // 1=lewe, 2=prawe (swap if reversed) 1 : 2, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return result; } // toggle door lock bool physics::Vehicle::LockDoors(bool const State, range_t const Notify) { auto const initialstate{Doors.lock_enabled}; Doors.lock_enabled = State; if (Notify != range_t::local) { // wysłanie wyłączenia do pozostałych? SendCtrlToNext(VehicleCommand::doorLock, State == true ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return Doors.lock_enabled != initialstate; } // toggles departure warning bool physics::Vehicle::signal_departure(bool const State, range_t const Notify) { if (DepartureSignal == State) { // TBD: should the command be passed to other vehicles regardless of whether it affected the primary target? return false; } DepartureSignal = State; if (Notify != range_t::local) { // wysłanie wyłączenia do pozostałych? SendCtrlToNext(VehicleCommand::departureSignal, State == true ? 1 : 0, CabActive, Notify == range_t::unit ? Coupling::control | Coupling::permanent : Coupling::control); } return true; } // state of the door closure signalling (SOD) of this vehicle: open while a door isn't fully closed, released while // the closed doors can be opened (a permit given, or, for doors that need none, no lock engaged), locked otherwise. // doors the crew doesn't close (autonomous) aren't signalled, the same as for the door lights of the consist door_closure_t physics::door_data::Closure() const { if (range == 0.f || close_control == control_t::autonomous) { return door_closure_t::none; } if (std::ranges::any_of(instances, [](basic_door const &Door) { return false == Door.is_closed; })) { return door_closure_t::open; } if (is_locked) { return door_closure_t::locked; } if (permit_needed && std::ranges::none_of(instances, [](basic_door const &Door) { return Door.open_permit; })) { return door_closure_t::locked; } return door_closure_t::released; } door_closure_t physics::Vehicle::door_closure() const { return Doors.Closure(); } bool physics::Vehicle::door_closure_lamp_lit(door_closure_t const Consist, bool const BlinkLit) const { return Doors.closure_signal ? door_closure_lamp(Consist, BlinkLit) : Doors.is_locked; } bool door_closure_lamp(door_closure_t const State, bool const BlinkLit) { switch (State) { case door_closure_t::locked: return true; case door_closure_t::released: return BlinkLit; default: return false; } } // automatic door controller update void physics::Vehicle::update_doors(double const Deltatime) { door_data::StepInputs inputs; inputs.velocity = Vel; inputs.powered = doors_powered(); inputs.multipleUnit = is_multiple_unit(); inputs.exchangingLoad = (loadStatus & (2 | 1)) != 0; Doors.Update(inputs, Deltatime); } // whether the doors have the voltage they work on bool physics::Vehicle::doors_powered() const { if (Doors.voltage == 0) { return true; } if (Doors.voltage == 24) { return lowVoltage.Is24vAvailable() || lowVoltage.Is110vAvailable(); } return Doors.voltage == 110 && lowVoltage.Is110vAvailable(); } // the step of the doors: the lock, the requests, the movement of the doors and their steps, the timers void physics::door_data::Update(StepInputs const &Inputs, double const Deltatime) { if (range == 0.f) { return; } // HACK: crude way to distinguish vehicles with actual doors // NBMX Obsluga drzwi, MC: zuniwersalnione auto const localopencontrol{false == remote_only && (open_control == control_t::passenger || open_control == control_t::mixed)}; auto const remoteopencontrol{open_control == control_t::driver || open_control == control_t::conductor || open_control == control_t::mixed}; auto const localclosecontrol{false == remote_only && (close_control == control_t::passenger || close_control == control_t::mixed)}; auto const remoteclosecontrol{close_control == control_t::driver || close_control == control_t::conductor || close_control == control_t::mixed}; is_locked = true == has_lock && true == lock_enabled && Inputs.velocity >= doorLockSpeed; for (auto &door : instances) { UpdateRequests(door, Inputs, localopencontrol, remoteopencontrol, localclosecontrol, remoteclosecontrol); Move(door, Inputs, Deltatime); } if (false == instances[VehicleSide::right].is_open && false == instances[VehicleSide::left].is_open) { return; } if (auto_duration > 0.f) { UpdateTimers(Inputs, Deltatime); } /* // the door are closed if their timer goes below 0, or if the vehicle is moving faster than defined threshold std::array const doorids { VehicleSide::right, VehicleSide::left }; for( auto const doorid : doorids ) { auto const &door { instances[ doorid ] }; if( true == door.is_open ) { if( ( ( auto_velocity != -1.f ) && ( Inputs.velocity > auto_velocity ) ) || ( ( door.auto_timer != -1.f ) && ( door.auto_timer <= 0.f ) ) ) { // close the door and set the timer to expired state (closing may happen sooner if vehicle starts moving) OperateDoors( doorid, false, range_t::local ); } } } */ } // the permit, the state and the requests of a door, and whether it and its step open or close void physics::door_data::UpdateRequests(basic_door &door, StepInputs const &Inputs, bool const localopencontrol, bool const remoteopencontrol, bool const localclosecontrol, bool const remoteclosecontrol) { // revoke permit if... door.open_permit = true == door.open_permit // ...we already have one... && (false == permit_presets.empty() // ...there's no preset switch controlling permit state... || (false == is_locked // ...and the door lock is engaged... && false == door.remote_close)); // ...or the door is about to be closed door.is_open = door.position >= range && (false == step_enabled || door.step_position >= (step_range != 0.f ? 1.f : 0.f)); door.is_closed = door.position <= 0.f && door.step_position <= 0.f; door.is_door_closed = door.position <= 0.f; door.local_open = door.local_open && false == door.is_open && (false == permit_needed || door.open_permit); door.remote_open = (door.remote_open || remote_only) && false == door.is_open && (false == permit_needed || door.open_permit); door.local_close = door.local_close && false == door.is_closed && (false == remoteopencontrol || false == door.remote_open); door.remote_close = door.remote_close && false == door.is_closed && (false == localopencontrol || false == door.local_open); auto const openrequest{OpenRequest(door, localopencontrol, remoteopencontrol)}; auto const closerequest{CloseRequest(door, Inputs, localclosecontrol, remoteclosecontrol)}; auto const ispowered{Inputs.powered}; door.is_opening = false == door.is_open && true == ispowered && false == closerequest && (true == door.is_opening || (true == openrequest && false == is_locked)); door.is_closing = false == door.is_closed && true == ispowered && false == openrequest && (door.is_closing || closerequest); door.step_unfolding = step_range != 0.f && step_enabled && false == is_locked && door.step_position < 1.f && door.is_opening; door.step_folding = StepFolding(door); if (true == door.is_opening) { door.auto_timer = OpenDuration(door, localopencontrol, remoteopencontrol); } } // whether the door is asked to open, by the passengers, from afar or by itself bool physics::door_data::OpenRequest(basic_door const &door, bool const localopencontrol, bool const remoteopencontrol) const { auto const autoopenrequest{open_control == control_t::autonomous && (false == permit_needed || door.open_permit)}; return (localopencontrol && door.local_open) || (remoteopencontrol && door.remote_open) || (autoopenrequest && false == door.is_open); } // whether the door is asked to close, by the passengers, from afar, or by itself: over the speed, after its time or // with its permit taken back bool physics::door_data::CloseRequest(basic_door const &door, StepInputs const &Inputs, bool const localclosecontrol, bool const remoteclosecontrol) const { auto const autocloserequest{(auto_velocity != -1.f && Inputs.velocity > auto_velocity) || (door.auto_timer != -1.f && door.auto_timer <= 0.f) || (permit_needed && false == door.open_permit)}; return (door.remote_close && remoteclosecontrol) || (door.local_close && localclosecontrol) || (autocloserequest && door.is_open); } // whether the unfolded step folds: without the permission to stay out, or with the door closing, once it's about to // close or closed bool physics::door_data::StepFolding(basic_door const &door) const { auto const unfolded{door.step_position > 0.f}; auto const mayfold{false == step_enabled || (permit_needed ? false == door.open_permit : door.is_closing)}; return unfolded && mayfold && (door.close_delay > close_delay || door.position <= 0.f); } // how long the opening door stays open: the time of the doors, for the passengers' opening and, if it counts, the // opening from afar; none otherwise float physics::door_data::OpenDuration(basic_door const &door, bool const localopencontrol, bool const remoteopencontrol) const { if (localopencontrol && door.local_open) { return auto_duration; } if (remoteopencontrol && door.remote_open && auto_include_remote) { return auto_duration; } return -1.f; } // a door and its step moved as they open or close void physics::door_data::Move(basic_door &door, StepInputs const &Inputs, double const Deltatime) { // doors if (true == door.is_opening) { // open door if (false == door.step_unfolding // no wait if no doorstep || step_type == 2) { // no wait for rotating doorstep door.open_delay += static_cast(Deltatime); if (door.open_delay > open_delay) { door.position = std::min(range, static_cast(door.position + open_rate * Deltatime)); } } door.close_delay = 0.f; } if (true == door.is_closing) { // close door door.close_delay += static_cast(Deltatime); if (door.close_delay > close_delay) { door.position = std::max(0.f, static_cast(door.position - close_rate * Deltatime)); } door.open_delay = 0.f; } // doorsteps if (door.step_unfolding) { // unfold left doorstep door.step_position = std::min(1.f, static_cast(door.step_position + step_rate * Deltatime)); } if (door.step_folding) { // fold left doorstep if (Inputs.multipleUnit) { // multi-unit vehicles typically fold the doorstep only after closing the door if (door.position <= 0.f) { door.step_position = std::max(0.f, static_cast(door.step_position - step_rate * Deltatime)); } } else { door.step_position = std::max(0.f, static_cast(door.step_position - step_rate * Deltatime)); } } } // the timers of the open doors which close after a while, kept running while the load is exchanged void physics::door_data::UpdateTimers(StepInputs const &Inputs, double const Deltatime) { // update door timers if the door close after defined time for (auto &door : instances) { if (false == door.is_open) { continue; } if (door.auto_timer > 0.f) { door.auto_timer -= static_cast(Deltatime); } // if there's load exchange in progress, reset the timer(s) for already open doors if (door.auto_timer != -1.f && Inputs.exchangingLoad) { door.auto_timer = auto_duration; } } }