mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 06:35:21 +01:00
ed8708c5e2
* feat(simulation): text format of simulation snapshots
A snapshot of a running simulation is a text file in the syntax of the scenery
files: entries of the form <type> <name> <key> <value> ... end<type>, the first
one a header which names the scenery. This is the text alone: entries, writer,
reader, the names of the things of a scenery, the plan of coupling changes and
the command line switches (--load-snapshot, --snapshot-at), in the core library
with unit tests. Nothing reads or writes a snapshot yet.
* feat(simulation): snapshot of time, weather, cells, switches and events (L1)
--snapshot-at <seconds> <file> writes a snapshot when the simulation has run
that long, --load-snapshot <file> puts one over the scenery once it has loaded,
before the first frame. This layer keeps the clock and the simulation timer,
the weather with the wind, the random engine, the memory cells, the switches,
the tracks an event can change and the ones made on the run, the traction
power sources, the event queue, the timers of the launchers and the lamps of
the signals.
While a snapshot is put back the event manager is suspended, so nothing in the
scenery reacts to it. Existing classes only get accessors for what was private
(the fraction of the second, launcher timers, the speed limit of a track, the
wind, the state of a power source, the event queue). Without the switches
nothing changes.
* feat(simulation): state of a vehicle's physics for snapshots (L3, core)
What TMoverParameters holds of a running vehicle, to and from an entry of a
snapshot: speed, load, controls, switches and relays, pantographs, doors,
engine and its cooling, the air of the pipes, of every reservoir of the brake
valve (with the flow it has yet to take in) and of the chambers of the driver's
valves, the vigilance device, the power circuits. In the core library, tested
on the real vehicle definitions: a
vehicle restored on a fresh one saves the same, and runs on next to the one it
was saved from.
The brake valves, the driver's valves and the vigilance device get accessors
for their private state (reservoirs(), latches(), pressures(), state()), a
reservoir one for the flow it has yet to take in; nothing else in the physics
changes. This comes before the vehicles of the scenery (L2), whose
speed and load are kept by it.
* feat(simulation): snapshot of vehicles and consists (L2)
Every vehicle is kept with the place of both its axles (track, distance,
direction, path of a switch, events still to set off), the way it faces, its
couplings with their flags, destination and, through its physics, speed and
load. Loaded, the vehicles are put straight where they were
(TTrackFollower::Place(), which trails no switch and fires no event), then
parted and joined until they are coupled as saved, whatever trainsets the
scenery made of them.
A vehicle of the snapshot the scenery does not have is skipped and reported,
one the snapshot does not know stays as the scenery made it, one which had left
the simulation is taken out again.
* feat(simulation): pantographs and passenger exchange in snapshots (L3)
What a vehicle keeps of its machinery beside its physics: the arms of the
pantographs as they stand under the wire, the time without voltage, the
passengers getting on and off, the lighting of the compartments, the headlight
toggles, the last force asked of the electrodynamic brake.
* feat(simulation): snapshot of the drivers and of the player's vehicle (L4)
A driver is kept with its orders and the one it is on, the name of its
timetable and its place in it, the consist it has, what it holds to (speeds of
signals and limits, the reason it stands, its timers and flags), its table of
what is ahead on the track with the point limits it has latched, its tables of
what the brake of the consist gives, and whether the AI drives. A driver who
had moved to another vehicle of its train is moved there.
A run loaded in ghost view (the test mode) is ready before its first frame, as
the run it goes on from was.
The snapshot also names the vehicle the player was in, which --load-snapshot
starts in unless -v says otherwise.
* feat(simulation): snapshotsave command, on [Ctrl]+[Shift]+[F9]
Writes a snapshot of the simulation to snapshots/<scenery>_<date>_<time>.snapshot
in the game folder. The command is the last one of the list, so the numbers of
the others stay.
* test: scenario cases which load a simulation snapshot and compare the run
A case with a "snapshot" section runs its scenery to a time, taking snapshots
on the way and at the end, then again from the first snapshot, and compares the
two ends: positions, speeds, pressures, couplings, cells, switches, drivers.
The snapshot saved right after loading has to be the one loaded.
tests/scenarios/snapshots.py reads the format and tells two snapshots apart.
The switches and the format are described in tests/scenarios/README.md.
* fix(simulation): a vehicle gone in the snapshot leaves its isolated sections
The scenery counts the axles of every vehicle in the isolated sections of
the tracks it puts it on. A vehicle the snapshot has as removed was only
taken off the list of its track, so its two axles stayed counted there and
the section never went free again: the next train on it went 4-6-4 axles
instead of 0-2-0, and its busy and free events never fired.
- A removed vehicle counts both axles out of the sections of their tracks,
is taken off the list of its track only if it's on it, and has no owner.
- The drivers a snapshot doesn't have are deleted as before, and no
vehicle keeps its owner pointing at one of them.
- The snapshot keeps the count of every isolated section and sets it after
the vehicles are placed: the vehicles a consist leaves behind as it goes
through a portal stay counted where they were, in the game too. A count
the placing gives other than the snapshot has is told in the log.
- A vehicle with two entries is placed once.
- TIsolated::axles() reads and sets the count.
- The harness compares the counts of the sections; a new case,
snapshot_td_portal_isolated, has a train leave through a portal from the
track of a section before the snapshot.
* fix(simulation): every value of a snapshot file is checked before it's used
A damaged or edited snapshot could put an index past a table, a count
which takes forever or a number out of the range of its type into the
simulation, and a file cut short was put back half way.
- Reading a value: a whole number has to be whole and fit its type (an
enum its underlying one), a float can't be past its largest value; what
doesn't fit is refused. Every refusal names the entry, the key and the
value, and goes to the log (errors.txt), up to 100 lines and a count.
- Ranges: coupler ends 0 or 1 and the flags of a coupling; couplings which
would close a ring of vehicles; the count of the points of a driver's
table and of its point limits (not past the keys of the entry), the
indices into the table (tablelast, semnext, semnextstop); the order
stack and its position, the timetable station, the driver's enums and
directions; the path, events and distance of an axle, facing and first
axle; the date and time; switch states, made track ends, crossing
segments, event siblings and times, launcher timers, the weather; the
load (amount, and a name which isn't a path); the whole numbers inside
number lists (flags of a table point, the stop of a coupler pair, the
track flags under a vehicle, the fuse count of a power source, the cab
of the vigilance device).
- The positions of the controllers are brought within the ranges of the
vehicle: master and secondary controller (set and actual), reverser,
cabs, light, compressor and wiper switches, brake handle, hand brake,
dimmer.
- A document with problems isn't loaded unless --load-snapshot-force says
so; a test run ends with the error code then. The header counts the
entries, so a file cut between two entries is found too. A file of a
newer version is a warning, not a problem.
- A snapshot is written to <file>.tmp and renamed over the file only when
the stream didn't fail.
- Unit tests of the reader on damaged input.
* feat(simulation): the pullee mode and the holding brake timer in snapshots
Of what #419 and #424 added to TMoverParameters, two are state: PulleeMode
(prepared for being hauled dead; the scenery sets it, PulleeModeSwitch()
can change it) and StopBrakeReleaseTimer (how long power has been asked
for, which lets the holding brake of a unit go after its delay). Both are
kept now, as "pulleemode" and "stopbrakereleasetimer". The others
(PressureSwitchBrakeCylinder, EmergencyBrakeMainOffPressure,
StopBrakeReleaseDelay) come from the vehicle definition and aren't.
* fix(simulation): the snapshot code without Sonar issues
No declaration shadows an enumerator, the variables used only by a
condition are declared in it, write_file takes its writer as a template,
the temporary file is a std::filesystem::path, the isolated sections are
looked up by a transparent hash, apply_couplings is split into helpers,
and the copies of the simulation's random engine are marked NOSONAR.
The test data has no http:// in it. No snapshot key or format changes.
* fix(brakes): TSt113 lists the control pressure once
TSt113::pressures() added CP on top of TH14K1::pressures(), which
already has it since #366 removed the CP of TSt113 that hid it. A
snapshot held the St113's control chamber twice. The second entry is
removed.
Tests: every driver's handle lists each pressure once, and the St113
pressures of an EN57 saved through a snapshot come back on a fresh one.
* fix(simulation): snapshots skip road traffic, refuse changed sceneries, keep misplaced vehicles apart
Cars of the road spawn points which a snapshot has on the road are no longer put back: the spawn point owns
them and the snapshot would leave them frozen on the track. They're counted in the log. The pooled cars keep
their AI drivers, which the snapshot used to delete.
A snapshot whose scenery fingerprint differs is refused unless --load-snapshot-force is given: tracks named
"#N" shift when a track is added. Each vehicle is checked against the x/y/z of its entry after placing. One
more than 1 m off, or one which can't be placed, goes back where the scenery put it, isn't coupled to the
placed vehicles, and keeps its scenery state and driver.
Couplings set from a snapshot keep to CompatibleCoupling, so no MU control between an electric and a diesel
loco (#520).
The snapshotsave key writes to snapshotsdir of eu07.ini (snapshots by default, ini only like screenshotsdir),
adds _2, _3... to a name that is taken, and shows where the file went.
* refactor(simulation): the snapshot reads and writes the private state of the vehicles through their own members
Since #552 the state of physics::Vehicle and of its parts is private, and since #531 the vehicle, the AI
and the cab live in other files under other names. The snapshot code is ported to that:
- the state of a vehicle's physics is saved and loaded by physics::Vehicle::SnapshotSave() and
SnapshotLoad() (physics/components/snapshot.cpp), with the tables of the variables in a nested
snapshot_members; capture_mover() and apply_mover() only hand it the entry. The parts with private
state (low voltage, line breaker, line contactors, protective relays, converter, air of the
pantographs, compressor, air supply) give a plain Snapshot of it and take it back
(TakeSnapshot(), RestoreSnapshot()); the rest goes through their queries and setters
- the keys, their order and the numbers in them are unchanged, so the snapshots written before load:
the fields that moved into the parts are mapped there (Mains to lineBreaker, PipePress to brakePipe,
BrakeCtrlPos to brakes, dizel_* to diesel, hydro_* to torqueConverter and retarder, eimv to
inductionMotor.state in the order of the old table, EngDmgFlag to equipmentFaults, ...)
- TDynamicObject: the machinery of a vehicle (sections, rail joints, the passenger exchange) is saved
by members of its own, as these are private now; MED_oldFED is the last ED demand of the consist's
brake blending (ConsistBrakeBlending::LastElectrodynamicDemand())
- TController: the driving flags are saved as the bits of iDrivigFlags they were, the running brake
test as the number of its stage, the uphill start and the braking shortfall from their structs;
StaticBrakeTest, which is gone, is written as 0 and not read
- the couplings take Coupling, CouplingSet and VehicleEnd; orders are DriverOrders
- the unit tests of the snapshot of a vehicle use the queries and the parts
* fix(simulation): carry the driver's coupling set in the snapshot as the bits of the set
iCoupler is a CouplingSet since 2098614f, which record::set()/get() can't take. It goes
through the converted values as a whole number, as the driving flags do: Bits() on save,
CouplingSet::FromBits() on load. The key and its values are the same as before.
* fix(simulation): a loaded snapshot counts the vehicles it left where they stand
apply_isolated() set every isolated section to the count of the snapshot, also where a
vehicle the load left at its scenery place (misplaced, forced load, road traffic not put
back) stands, or where the snapshot had it. The axles of those vehicles are now counted in
the sections they stand in and out of the ones the snapshot has them in, and the memory
cells of those sections say whether they are taken by that count.
An AI consist doesn't end on a vehicle the load left: the ends are then found along what is
coupled now, as CheckVehicles() finds them.
The player entry no longer writes the cab, which nothing read: the cab is the one the
vehicle has occupied (caboccupied of its entry).
* feat(simulation): the snapshot keeps a derailment and the brakes of a sleeping vehicle
A derailed vehicle came back on the rails. Its entry now keeps what derailed it (outcome,
cause, speed, the cause and side of the motion off the track, the acceleration, the
collision derailment) and the distance its criteria were last checked over; the
derailment the scenery and the test mode were told of (new_derailment()), so it isn't told
again; and with physics.derailment new the motion of the body off the track
(derail_motion::body::state()) with the timers of the vehicle off the track. The terrain
under it is gathered anew around it when it loads.
sleep_brakes, what a sleeping vehicle's brake force came from, is kept too.
All keys are new: an entry without them leaves the vehicle as the scenery made it, so files
written before still load. New scenario case snapshot_td_off_track.
* fix(simulation): the snapshot keeps the sanded start of an AI driver
#494 (in staging since the last runs of the snapshot cases) made a driver whose start
slipped keep sanding under power until the train is under way, and take the power down a
step at a time. Neither the flag nor the time of the last step down was kept, so on
linia61 the loaded run sanded one frame later than the run which went on and parted from
it 0.06 s after loading (suwnica2, 9 mm and 0.08 km/h at 600 s). Both are kept now
(sandedstart, sandedstarttime); an entry without them leaves the driver as it is.
* fix(simulation): the snapshot keeps a train running forked on a switch thrown under it
Staging's switch fork running (#496) gave the axles and the vehicles state of their own: the way of a
switch an axle ran onto (TTrackFollower::iPath, which it leaves the switch by after a throw), the ways it
remembers of the switches behind it (path_memory, read when the axle behind it comes onto one of them),
and per vehicle the switch its bogies or its coupled ends run forked on with whether that was told
(m_forkbogies, m_forkends). None of it was in a snapshot: a loaded train on a switch thrown under it
would have left it by the way it is set now and never derailed running forked.
- Place() sets the way of the switch from the path the axle is put on, as SetCurrentTrack() does, and
clears the facing entry the vehicle hasn't looked at (there is none between frames).
- A vehicle entry has the remembered ways of each axle, oldest first (axle0switch0, axle0way0...),
recorded again into an empty memory on load (path_memory::entries()), and the forked watches
(forkbogies, fork0, fork1 with their "told"). A name which isn't a switch is refused.
- New case snapshot_td_switch_thrown: the snapshot is taken while the second coach of
eu07_td_switch_thrown_facing_behind_derails runs forked and hasn't derailed yet; the loaded run derails
it the same way and the two files at 150 s are identical.
1467 lines
44 KiB
C++
1467 lines
44 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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "application/drivermode.h"
|
|
#include "application/driveruilayer.h"
|
|
|
|
#include "utilities/Globals.h"
|
|
#include "application/application.h"
|
|
#include "utilities/translation.h"
|
|
#include "simulation/simulation.h"
|
|
#include "simulation/simulationtime.h"
|
|
#include "simulation/simulationenvironment.h"
|
|
#include "simulation/simulationtest.h"
|
|
#include "simulation/simulationsnapshotworld.h"
|
|
#include "simulation/blackboxrecorder.h"
|
|
#include "scene/scene.h"
|
|
#include "rendering/lightarray.h"
|
|
#include "rendering/particles.h"
|
|
#include "vehicle/Train.h"
|
|
#include "vehicle/Driver.h"
|
|
#include "vehicle/DynObj.h"
|
|
#include "model/Model3d.h"
|
|
#include "world/Event.h"
|
|
#include "world/deer.h"
|
|
#include "input/messaging.h"
|
|
#include "utilities/Timer.h"
|
|
#include "rendering/renderer.h"
|
|
#include "utilities/Logs.h"
|
|
#include "physics/vehicle.h"
|
|
|
|
#include <algorithm>
|
|
|
|
/*
|
|
namespace input {
|
|
|
|
user_command command; // currently issued control command, if any
|
|
|
|
}
|
|
*/
|
|
void driver_mode::drivermode_input::poll()
|
|
{
|
|
if (telemetry)
|
|
telemetry->update();
|
|
keyboard.poll();
|
|
if (true == Global.InputMouse)
|
|
{
|
|
mouse.poll();
|
|
}
|
|
if (true == Global.InputGamepad)
|
|
{
|
|
gamepad.poll();
|
|
}
|
|
#ifdef WITH_UART
|
|
if (uart != nullptr)
|
|
{
|
|
uart->poll();
|
|
}
|
|
#endif
|
|
#ifdef WITH_ZMQ
|
|
if (zmq != nullptr)
|
|
{
|
|
zmq->poll();
|
|
}
|
|
#endif
|
|
/*
|
|
// TBD, TODO: wrap current command in object, include other input sources?
|
|
input::command = (
|
|
mouse.command() != user_command::none ?
|
|
mouse.command() :
|
|
keyboard.command() );
|
|
*/
|
|
}
|
|
|
|
bool driver_mode::drivermode_input::init()
|
|
{
|
|
|
|
// initialize input devices
|
|
auto result = keyboard.init() && mouse.init();
|
|
if (true == Global.InputGamepad)
|
|
{
|
|
gamepad.init();
|
|
}
|
|
#ifdef WITH_UART
|
|
if (true == Global.uart_conf.enable)
|
|
{
|
|
uart = std::make_unique<uart_input>();
|
|
uart->init();
|
|
}
|
|
#endif
|
|
#ifdef WITH_ZMQ
|
|
if (!Global.zmq_address.empty())
|
|
{
|
|
zmq = std::make_unique<zmq_input>();
|
|
}
|
|
#endif
|
|
if (Global.motiontelemetry_conf.enable)
|
|
telemetry = std::make_unique<motiontelemetry>();
|
|
|
|
#ifdef _WIN32
|
|
Console::On(); // włączenie konsoli
|
|
#endif
|
|
|
|
return result;
|
|
}
|
|
|
|
std::string driver_mode::drivermode_input::binding_hints(std::pair<user_command, user_command> const &Commands) const
|
|
{
|
|
|
|
auto const inputhintleft{keyboard.binding_hint(Commands.first)};
|
|
auto const inputhintright{keyboard.binding_hint(Commands.second)};
|
|
std::string inputhints = inputhintleft + (inputhintright.empty() ? "" : inputhintleft.empty() ? inputhintright : "] [" + inputhintright);
|
|
|
|
return inputhints;
|
|
}
|
|
|
|
std::unordered_map<user_command, std::pair<user_command, user_command>> commandfallbacks = {
|
|
{user_command::mastercontrollerset, {user_command::mastercontrollerincrease, user_command::mastercontrollerdecrease}},
|
|
{user_command::secondcontrollerset, {user_command::secondcontrollerincrease, user_command::secondcontrollerdecrease}},
|
|
{user_command::trainbrakeset, {user_command::trainbrakeincrease, user_command::trainbrakedecrease}},
|
|
{user_command::independentbrakeset, {user_command::independentbrakeincrease, user_command::independentbrakedecrease}},
|
|
{user_command::linebreakeropen, {user_command::linebreakertoggle, user_command::none}},
|
|
{user_command::linebreakerclose, {user_command::linebreakertoggle, user_command::none}},
|
|
{user_command::pantographlowerfront, {user_command::pantographtogglefront, user_command::none}},
|
|
{user_command::pantographlowerrear, {user_command::pantographtogglerear, user_command::none}},
|
|
{user_command::compartmentlightsenable, {user_command::compartmentlightstoggle, user_command::none}},
|
|
{user_command::compartmentlightsdisable, {user_command::compartmentlightstoggle, user_command::none}},
|
|
{user_command::batteryenable, {user_command::batterytoggle, user_command::none}},
|
|
{user_command::batterydisable, {user_command::batterytoggle, user_command::none}},
|
|
};
|
|
|
|
std::pair<user_command, user_command> driver_mode::drivermode_input::command_fallback(user_command const Command) const
|
|
{
|
|
|
|
if (Command == user_command::none)
|
|
{
|
|
return {user_command::none, user_command::none};
|
|
}
|
|
|
|
auto const lookup{commandfallbacks.find(Command)};
|
|
|
|
if (lookup == commandfallbacks.end())
|
|
{
|
|
return {user_command::none, user_command::none};
|
|
}
|
|
|
|
return lookup->second;
|
|
}
|
|
|
|
driver_mode::driver_mode()
|
|
{
|
|
|
|
m_userinterface = std::make_shared<driver_ui>();
|
|
}
|
|
|
|
// initializes internal data structures of the mode. returns: true on success, false otherwise
|
|
bool driver_mode::init()
|
|
{
|
|
|
|
return m_input.init();
|
|
}
|
|
|
|
// mode-specific update of simulation data. returns: false on error, true otherwise
|
|
namespace {
|
|
|
|
// the camera as the black box tells it: where it is and where it looks, as the heading of a vehicle
|
|
blackbox::camera_state blackbox_camera(TCamera const &Camera, std::uint8_t const Mode, int const View) {
|
|
blackbox::camera_state state;
|
|
state.mode = Mode;
|
|
state.view = static_cast<std::uint8_t>(View);
|
|
state.position = Camera.Pos;
|
|
state.pitch = Camera.Angle.x;
|
|
if (Camera.m_owner != nullptr && false == DebugCameraFlag) {
|
|
// it looks at a point, then turns by its yaw from there
|
|
auto const where{Camera.LookAt - Camera.Pos};
|
|
state.heading = static_cast<float>(std::atan2(where.x, where.z)) + Camera.Angle.y;
|
|
}
|
|
else {
|
|
// the yaw alone: 0 looks towards -z
|
|
state.heading = static_cast<float>(std::atan2(-std::sin(Camera.Angle.y), -std::cos(Camera.Angle.y)));
|
|
}
|
|
state.heading = std::remainder(state.heading, 2.0f * glm::pi<float>());
|
|
return state;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
bool driver_mode::update()
|
|
{
|
|
|
|
Timer::UpdateTimers(Global.iPause != 0);
|
|
Timer::subsystem.sim_total.start();
|
|
|
|
double const deltatime = Timer::GetDeltaTime(); // 0.0 gdy pauza
|
|
|
|
Timer::subsystem.sim_scripting.start();
|
|
simulation::State.update_clocks();
|
|
simulation::State.update_scripting_interface();
|
|
Timer::subsystem.sim_scripting.stop();
|
|
Timer::subsystem.sim_environment.start();
|
|
simulation::Environment.update();
|
|
Timer::subsystem.sim_environment.stop();
|
|
|
|
if (deltatime != 0.0 || false == simulation::is_ready)
|
|
{
|
|
// jak pauza, to nie ma po co tego przeliczać
|
|
simulation::Time.update(deltatime);
|
|
|
|
// fixed step, simulation time based updates
|
|
// m_primaryupdateaccumulator += dt; // unused for the time being
|
|
m_secondaryupdateaccumulator += deltatime;
|
|
/*
|
|
// NOTE: until we have no physics state interpolation during render, we need to rely on the old code,
|
|
// as doing fixed step calculations but flexible step render results in ugly mini jitter
|
|
// core routines (physics)
|
|
int updatecount = 0;
|
|
while( ( m_primaryupdateaccumulator >= m_primaryupdaterate )
|
|
&&( updatecount < 20 ) ) {
|
|
// no more than 20 updates per single pass, to keep physics from hogging up all run time
|
|
Ground.Update( m_primaryupdaterate, 1 );
|
|
++updatecount;
|
|
m_primaryupdateaccumulator -= m_primaryupdaterate;
|
|
}
|
|
*/
|
|
int updatecount = 1;
|
|
if (deltatime > m_primaryupdaterate) // normalnie 0.01s
|
|
{
|
|
/*
|
|
// NOTE: experimentally disabled physics update cap
|
|
auto const iterations = std::ceil(dt / m_primaryupdaterate);
|
|
updatecount = std::min( 20, static_cast<int>( iterations ) );
|
|
*/
|
|
updatecount = std::ceil(deltatime / m_primaryupdaterate);
|
|
/*
|
|
// NOTE: changing dt wrecks things further down the code. re-acquire proper value later or cleanup here
|
|
dt = dt / iterations; // Ra: fizykę lepiej by było przeliczać ze stałym krokiem
|
|
*/
|
|
}
|
|
auto const stepdeltatime{deltatime / updatecount};
|
|
// NOTE: updates are limited to 20, but dt is distributed over potentially many more iterations
|
|
// this means at count > 20 simulation and render are going to desync. is that right?
|
|
// NOTE: experimentally changing this to prevent the desync.
|
|
// TODO: test what happens if we hit more than 20 * 0.01 sec slices, i.e. less than 5 fps
|
|
Timer::subsystem.sim_dynamics.start();
|
|
if (true == Global.FullPhysics)
|
|
{
|
|
// mixed calculation mode, steps calculated in ~0.05s chunks
|
|
while (updatecount >= 5)
|
|
{
|
|
simulation::State.update(stepdeltatime, 5);
|
|
updatecount -= 5;
|
|
}
|
|
if (updatecount)
|
|
{
|
|
simulation::State.update(stepdeltatime, updatecount);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// simplified calculation mode; faster but can lead to errors
|
|
simulation::State.update(stepdeltatime, updatecount);
|
|
}
|
|
Timer::subsystem.sim_dynamics.stop();
|
|
|
|
// secondary fixed step simulation time routines
|
|
Timer::subsystem.sim_secondary.start();
|
|
while (m_secondaryupdateaccumulator >= m_secondaryupdaterate)
|
|
{
|
|
|
|
// awaria PoKeys mogła włączyć pauzę - przekazać informację
|
|
if (Global.iMultiplayer) // dajemy znać do serwera o wykonaniu
|
|
if (iPause != Global.iPause)
|
|
{ // przesłanie informacji o pauzie do programu nadzorującego
|
|
multiplayer::WyslijParam(5, 3); // ramka 5 z czasem i stanem zapauzowania
|
|
iPause = Global.iPause;
|
|
}
|
|
|
|
// TODO: generic shake update pass for vehicles within view range
|
|
if (Camera.m_owner != nullptr)
|
|
{
|
|
Camera.m_owner->update_shake(m_secondaryupdaterate);
|
|
}
|
|
|
|
m_secondaryupdateaccumulator -= m_secondaryupdaterate; // these should be inexpensive enough we have no cap
|
|
}
|
|
Timer::subsystem.sim_secondary.stop();
|
|
|
|
// variable step simulation time routines
|
|
|
|
if (false == Global.editor_enter_vehicle.empty())
|
|
{
|
|
auto const name{std::move(Global.editor_enter_vehicle)};
|
|
Global.editor_enter_vehicle.clear();
|
|
if (auto *dynamic{simulation::Vehicles.find(name)})
|
|
{
|
|
if (false == FreeFlyModeFlag)
|
|
InOutKey();
|
|
m_relay.post(user_command::entervehicle, 0.0, simulation::Train ? simulation::Train->id() : 0, GLFW_PRESS, 0, dynamic->GetPosition(), &dynamic->name());
|
|
change_train = dynamic->name();
|
|
}
|
|
}
|
|
|
|
if (!change_train.empty())
|
|
{
|
|
TTrain *train = simulation::Trains.find(change_train);
|
|
if (train)
|
|
{
|
|
Global.local_start_vehicle = change_train;
|
|
simulation::Train = train;
|
|
InOutKey();
|
|
m_relay.post(user_command::aidriverdisable, 0.0, 0.0, GLFW_PRESS, 0);
|
|
change_train.clear();
|
|
}
|
|
}
|
|
|
|
if (simulation::Train == nullptr && false == FreeFlyModeFlag)
|
|
{
|
|
// intercept cases when the driven train got removed after entering portal
|
|
InOutKey();
|
|
}
|
|
|
|
if (!FreeFlyModeFlag && simulation::Train->Dynamic() != Camera.m_owner)
|
|
{
|
|
// fixup camera after vehicle switch
|
|
CabView();
|
|
}
|
|
|
|
if (simulation::Train != nullptr)
|
|
TSubModel::iInstance = reinterpret_cast<std::uintptr_t>(simulation::Train->Dynamic());
|
|
else
|
|
TSubModel::iInstance = 0;
|
|
|
|
Timer::subsystem.sim_trains.start();
|
|
if (Global.trainThreads > 0)
|
|
simulation::Trains.updateAsync(deltatime);
|
|
else
|
|
simulation::Trains.update(deltatime);
|
|
Timer::subsystem.sim_trains.stop();
|
|
Timer::subsystem.sim_events.start();
|
|
simulation::Events.update();
|
|
simulation::Region->update_events();
|
|
Timer::subsystem.sim_events.stop();
|
|
Timer::subsystem.sim_lights.start();
|
|
simulation::Lights.update();
|
|
Timer::subsystem.sim_lights.stop();
|
|
simulation::Deer.update(deltatime);
|
|
{
|
|
// where the user looks from, for the black box
|
|
auto const &camera{DebugCameraFlag ? DebugCamera : Camera};
|
|
auto const mode{DebugCameraFlag ? blackbox::camera_modes::debug :
|
|
false == FreeFlyModeFlag ? blackbox::camera_modes::cab :
|
|
m_externalview ? blackbox::camera_modes::external :
|
|
blackbox::camera_modes::free};
|
|
simulation::Blackbox.camera(blackbox_camera(camera, mode, mode == blackbox::camera_modes::external ? m_externalviewmode : 0),
|
|
DebugCameraFlag ? nullptr : camera.m_owner);
|
|
}
|
|
simulation::Blackbox.update(deltatime);
|
|
|
|
if (Global.test.enabled && simulation::is_ready)
|
|
{
|
|
simulation::test().update(deltatime);
|
|
}
|
|
// the frame is complete: the snapshots asked for at this time are taken (--snapshot-at)
|
|
simulation::snapshot::update();
|
|
}
|
|
|
|
// render time routines follow:
|
|
|
|
auto const deltarealtime = Timer::GetDeltaRenderTime(); // nie uwzględnia pauzowania ani mnożenia czasu
|
|
Timer::subsystem.sim_commands.start();
|
|
simulation::State.process_commands();
|
|
Timer::subsystem.sim_commands.stop();
|
|
|
|
// fixed step render time routines
|
|
|
|
fTime50Hz += deltarealtime; // w pauzie też trzeba zliczać czas, bo przy dużym FPS będzie problem z odczytem ramek
|
|
bool runonce{false};
|
|
Timer::subsystem.sim_ui.start();
|
|
while (fTime50Hz >= 1.0 / 50.0)
|
|
{
|
|
#ifdef _WIN32
|
|
Console::Update(); // to i tak trzeba wywoływać
|
|
#endif
|
|
ui::Transcripts.Update(); // obiekt obsługujący stenogramy dźwięków na ekranie
|
|
m_userinterface->update();
|
|
// decelerate camera
|
|
Camera.Velocity *= 0.65;
|
|
if (std::abs(Camera.Velocity.x) < 0.01)
|
|
{
|
|
Camera.Velocity.x = 0.0;
|
|
}
|
|
if (std::abs(Camera.Velocity.y) < 0.01)
|
|
{
|
|
Camera.Velocity.y = 0.0;
|
|
}
|
|
if (std::abs(Camera.Velocity.z) < 0.01)
|
|
{
|
|
Camera.Velocity.z = 0.0;
|
|
}
|
|
// decelerate debug camera too
|
|
DebugCamera.Velocity *= 0.65;
|
|
if (std::abs(DebugCamera.Velocity.x) < 0.01)
|
|
{
|
|
DebugCamera.Velocity.x = 0.0;
|
|
}
|
|
if (std::abs(DebugCamera.Velocity.y) < 0.01)
|
|
{
|
|
DebugCamera.Velocity.y = 0.0;
|
|
}
|
|
if (std::abs(DebugCamera.Velocity.z) < 0.01)
|
|
{
|
|
DebugCamera.Velocity.z = 0.0;
|
|
}
|
|
|
|
if (false == runonce)
|
|
{
|
|
// tooltip update
|
|
set_tooltip("");
|
|
auto const *train{simulation::Train};
|
|
if (train != nullptr && false == FreeFlyModeFlag)
|
|
{
|
|
if (false == DebugModeFlag)
|
|
{
|
|
// in regular mode show control functions, for defined controls
|
|
auto const controlname{train->GetLabel(GfxRenderer->Pick_Control())};
|
|
if (false == controlname.empty())
|
|
{
|
|
auto const mousecommands{m_input.mouse.bindings(controlname)};
|
|
auto inputhints{m_input.binding_hints(mousecommands)};
|
|
// if the commands bound with the control don't have any assigned keys try potential fallbacks
|
|
if (inputhints.empty())
|
|
{
|
|
inputhints = m_input.binding_hints(m_input.command_fallback(mousecommands.first));
|
|
}
|
|
if (inputhints.empty())
|
|
{
|
|
inputhints = m_input.binding_hints(m_input.command_fallback(mousecommands.second));
|
|
}
|
|
// ready or not, here we go
|
|
if (inputhints.empty())
|
|
{
|
|
set_tooltip(Translations.label_cab_control(controlname));
|
|
}
|
|
else
|
|
{
|
|
set_tooltip(Translations.label_cab_control(controlname) + " [" + inputhints + "]");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// in debug mode show names of submodels, to help with cab setup and/or debugging
|
|
auto const cabcontrol = GfxRenderer->Pick_Control();
|
|
set_tooltip(cabcontrol ? cabcontrol->pName : "");
|
|
}
|
|
}
|
|
if (Global.ControlPicking && FreeFlyModeFlag && DebugModeFlag)
|
|
{
|
|
const auto sceneryNode = GfxRenderer->Pick_Node();
|
|
const std::string content = sceneryNode ? sceneryNode->tooltip() : "";
|
|
set_tooltip(content);
|
|
}
|
|
|
|
runonce = true;
|
|
}
|
|
|
|
fTime50Hz -= 1.0 / 50.0;
|
|
}
|
|
Timer::subsystem.sim_ui.stop();
|
|
|
|
// variable step render time routines
|
|
|
|
Timer::subsystem.sim_camera.start();
|
|
update_camera(deltarealtime);
|
|
Timer::subsystem.sim_camera.stop();
|
|
|
|
Timer::subsystem.sim_precipitation.start();
|
|
simulation::Environment.update_precipitation(); // has to be launched after camera step to work properly
|
|
Timer::subsystem.sim_precipitation.stop();
|
|
|
|
Timer::subsystem.sim_total.stop();
|
|
// the parts of the vehicle update made in the frame, none when paused
|
|
Timer::subsystem.commit_frame();
|
|
Timer::subsystem.switching.update();
|
|
|
|
Timer::subsystem.loop_audio.start();
|
|
// the test mode has no sound: the sounds of the scenery and of the vehicles aren't gone through. they draw from the local
|
|
// engine of Random() only (LocalRandom()), and clear the sound flags of the vehicles, which nothing else reads
|
|
if (false == Global.test.enabled)
|
|
simulation::Region->update_sounds();
|
|
audio::renderer.update(Global.iPause ? 0.0 : deltarealtime);
|
|
Timer::subsystem.loop_audio.stop();
|
|
|
|
// NOTE: particle system runs on simulation time, but needs actual camera position to determine how to update each particle source
|
|
// nor smoke in the test mode, which draws nothing: the particles draw from the local engine of Random() only
|
|
Timer::subsystem.loop_particles.start();
|
|
if (false == Global.test.enabled)
|
|
simulation::Particles.update();
|
|
Timer::subsystem.loop_particles.stop();
|
|
|
|
Timer::subsystem.loop_renderer_update.start();
|
|
GfxRenderer->Update(deltarealtime);
|
|
Timer::subsystem.loop_renderer_update.stop();
|
|
|
|
simulation::is_ready = simulation::is_ready || (simulation::Train != nullptr && simulation::Train->is_cab_initialized) || Global.local_start_vehicle == "ghostview";
|
|
|
|
if (m_editorpending)
|
|
{
|
|
// edit session starts in the editor. the switch is made after the initial update on purpose, so the
|
|
// simulation state is set up the same way as when the editor is entered by hand right after the load
|
|
m_editorpending = false;
|
|
Application.push_mode(eu07_application::mode::editor);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// maintenance method, called when the mode is activated
|
|
void driver_mode::enter()
|
|
{
|
|
|
|
TDynamicObject *nPlayerTrain{(Global.local_start_vehicle != "ghostview" ? simulation::Vehicles.find(Global.local_start_vehicle) : nullptr)};
|
|
|
|
Camera.Init(Global.FreeCameraInit[0], Global.FreeCameraInitAngle[0], nullptr);
|
|
Global.pCamera = Camera;
|
|
Global.pDebugCamera = DebugCamera;
|
|
|
|
FreeFlyModeFlag = true;
|
|
DebugCamera = Camera;
|
|
|
|
if (nPlayerTrain)
|
|
{
|
|
WriteLog("Trying to enter player train, \"" + Global.local_start_vehicle + "\"");
|
|
|
|
m_relay.post(user_command::entervehicle, 0.0, 0.0, GLFW_PRESS, 0, nPlayerTrain->GetPosition(), &Global.local_start_vehicle);
|
|
change_train = nPlayerTrain->name();
|
|
}
|
|
else if (Global.local_start_vehicle != "ghostview")
|
|
{
|
|
Global.local_start_vehicle = "ghostview";
|
|
Error("Bad scenario: failed to locate player train, \"" + Global.local_start_vehicle + "\"");
|
|
}
|
|
|
|
// if (!Global.bMultiplayer) //na razie włączone
|
|
{ // eventy aktywowane z klawiatury tylko dla jednego użytkownika
|
|
KeyEvents[0] = simulation::Events.FindEvent("keyctrl00");
|
|
KeyEvents[1] = simulation::Events.FindEvent("keyctrl01");
|
|
KeyEvents[2] = simulation::Events.FindEvent("keyctrl02");
|
|
KeyEvents[3] = simulation::Events.FindEvent("keyctrl03");
|
|
KeyEvents[4] = simulation::Events.FindEvent("keyctrl04");
|
|
KeyEvents[5] = simulation::Events.FindEvent("keyctrl05");
|
|
KeyEvents[6] = simulation::Events.FindEvent("keyctrl06");
|
|
KeyEvents[7] = simulation::Events.FindEvent("keyctrl07");
|
|
KeyEvents[8] = simulation::Events.FindEvent("keyctrl08");
|
|
KeyEvents[9] = simulation::Events.FindEvent("keyctrl09");
|
|
}
|
|
|
|
Timer::ResetTimers();
|
|
|
|
// the scenery is loaded and nothing runs yet: the state saved in a snapshot goes over it (--load-snapshot)
|
|
simulation::snapshot::start();
|
|
|
|
if (Global.test.enabled)
|
|
{
|
|
// the test runs as fast as it can compute, in steps of fixed length, so that runs on different machines agree
|
|
Timer::set_fixed_step(simulation::test_manager::frame_step);
|
|
simulation::test().start();
|
|
}
|
|
|
|
set_picking(!Global.captureonstart);
|
|
|
|
m_editorpending = Global.editor_session;
|
|
}
|
|
|
|
// maintenance method, called when the mode is deactivated
|
|
void driver_mode::exit() {}
|
|
|
|
void driver_mode::on_key(int const Key, int const Scancode, int const Action, int const Mods)
|
|
{
|
|
|
|
#ifndef __unix__
|
|
Global.shiftState = Mods & GLFW_MOD_SHIFT ? true : false;
|
|
Global.ctrlState = Mods & GLFW_MOD_CONTROL ? true : false;
|
|
Global.altState = Mods & GLFW_MOD_ALT ? true : false;
|
|
#endif
|
|
|
|
bool anyModifier = Mods & (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT);
|
|
|
|
// give the ui first shot at the input processing...
|
|
if (!anyModifier && true == m_userinterface->on_key(Key, Action))
|
|
{
|
|
return;
|
|
}
|
|
if (Key == GLFW_KEY_F12 && Global.shiftState && Action == GLFW_PRESS)
|
|
{
|
|
m_userinterface->showDebugUI();
|
|
return;
|
|
}
|
|
// ...if the input is left untouched, pass it on
|
|
if (true == m_input.keyboard.key(Key, Action))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (true == Global.InputMouse && (Key == GLFW_KEY_LEFT_ALT || Key == GLFW_KEY_RIGHT_ALT))
|
|
{
|
|
// if the alt key was pressed toggle control picking mode and set matching cursor behaviour
|
|
if (Action == GLFW_PRESS)
|
|
{
|
|
// toggle picking mode
|
|
set_picking(Global.shiftState ? true : Global.ctrlState ? false : !Global.ControlPicking);
|
|
}
|
|
}
|
|
|
|
if (Action != GLFW_RELEASE)
|
|
{
|
|
OnKeyDown(Key);
|
|
}
|
|
}
|
|
|
|
void driver_mode::on_cursor_pos(double const Horizontal, double const Vertical)
|
|
{
|
|
|
|
// give the ui first shot at the input processing...
|
|
if (true == m_userinterface->on_cursor_pos(Horizontal, Vertical))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (false == Global.ControlPicking)
|
|
{
|
|
// in regular view mode keep cursor on screen
|
|
Application.set_cursor_pos(0, 0);
|
|
}
|
|
// give the potential event recipient a shot at it, in the virtual z order
|
|
m_input.mouse.move(Horizontal, Vertical);
|
|
}
|
|
|
|
void driver_mode::on_mouse_button(int const Button, int const Action, int const Mods)
|
|
{
|
|
|
|
// give the ui first shot at the input processing...
|
|
if (true == m_userinterface->on_mouse_button(Button, Action))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// give the potential event recipient a shot at it, in the virtual z order
|
|
m_input.mouse.button(Button, Action);
|
|
}
|
|
|
|
void driver_mode::on_scroll(double const Xoffset, double const Yoffset)
|
|
{
|
|
|
|
m_input.mouse.scroll(Xoffset, Yoffset);
|
|
}
|
|
|
|
void driver_mode::on_event_poll()
|
|
{
|
|
|
|
m_input.poll();
|
|
}
|
|
|
|
bool driver_mode::is_command_processor() const
|
|
{
|
|
|
|
return true;
|
|
}
|
|
|
|
double driver_mode::shake_fraction() const
|
|
{
|
|
// how far the simulation time has got from the last step of the shake towards the next one
|
|
return std::clamp(m_secondaryupdateaccumulator / m_secondaryupdaterate, 0.0, 1.0);
|
|
}
|
|
|
|
void driver_mode::update_camera(double const Deltatime)
|
|
{
|
|
|
|
auto *controlled = simulation::Train ? simulation::Train->Dynamic() : nullptr;
|
|
|
|
if (false == Global.ControlPicking)
|
|
{
|
|
|
|
if (m_input.mouse.button(GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
|
{
|
|
Camera.Reset(); // likwidacja obrotów - patrzy horyzontalnie na południe
|
|
if (Camera.m_owner == nullptr)
|
|
{
|
|
if (controlled && glm::length2(controlled->GetPosition() - Camera.Pos) < sq(1500)) // length2 is better than length for comparing because it does not require sqrt function
|
|
{
|
|
// gdy bliżej niż 1.5km
|
|
Camera.LookAt = controlled->GetPosition() + 0.4 * controlled->VectorUp() * controlled->physics->Dim.H;
|
|
}
|
|
else
|
|
{
|
|
TDynamicObject *d = std::get<TDynamicObject *>(simulation::Region->find_vehicle(Camera.Pos, 300, false, false));
|
|
if (!d)
|
|
d = std::get<TDynamicObject *>(simulation::Region->find_vehicle(Camera.Pos, 1000, false, false)); // dalej szukanie, jesli bliżej nie ma
|
|
|
|
if (d && pDynamicNearest)
|
|
{
|
|
// jeśli jakiś jest znaleziony wcześniej
|
|
if (sq(10.0) * glm::length2(d->GetPosition() - Camera.Pos) > glm::length2(pDynamicNearest->GetPosition() - Camera.Pos)) // length2 is better than length for comparing because it does not require sqrt function
|
|
{
|
|
d = pDynamicNearest; // jeśli najbliższy nie jest 10 razy bliżej niż
|
|
}
|
|
}
|
|
// poprzedni najbliższy, zostaje poprzedni
|
|
if (d)
|
|
pDynamicNearest = d; // zmiana na nowy, jeśli coś znaleziony niepusty
|
|
if (pDynamicNearest)
|
|
Camera.LookAt = pDynamicNearest->GetPosition() + 0.4 * pDynamicNearest->VectorUp() * pDynamicNearest->physics->Dim.H;
|
|
}
|
|
Camera.RaLook(); // jednorazowe przestawienie kamery
|
|
}
|
|
else
|
|
{
|
|
if (false == FreeFlyModeFlag && simulation::Train)
|
|
{
|
|
// reset cached view angle in the cab
|
|
simulation::Train->pMechViewAngle = {Camera.Angle.x, Camera.Angle.y};
|
|
}
|
|
}
|
|
}
|
|
else if (m_input.mouse.button(GLFW_MOUSE_BUTTON_RIGHT) == GLFW_PRESS)
|
|
{
|
|
CabView();
|
|
}
|
|
}
|
|
|
|
if (m_input.mouse.button(GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS)
|
|
{
|
|
// middle mouse button controls zoom.
|
|
Global.ZoomFactor = std::min(4.5f, Global.ZoomFactor + 15.0f * static_cast<float>(Deltatime));
|
|
}
|
|
else if (m_input.mouse.button(GLFW_MOUSE_BUTTON_MIDDLE) != GLFW_PRESS)
|
|
{
|
|
// reset zoom level if the button is no longer held down.
|
|
// NOTE: yes, this is terrible way to go about it. it'll do for now.
|
|
Global.ZoomFactor = std::max(1.0f, Global.ZoomFactor - 15.0f * static_cast<float>(Deltatime));
|
|
}
|
|
|
|
// uwzględnienie ruchu wywołanego klawiszami
|
|
if (false == DebugCameraFlag)
|
|
{
|
|
// regular camera
|
|
if (simulation::Train != nullptr && false == FreeFlyModeFlag && false == Global.CabWindowOpen)
|
|
{
|
|
// if in cab potentially alter camera placement based on changes in train object
|
|
Camera.m_owneroffset = simulation::Train->pMechOffset;
|
|
Camera.Angle.x = simulation::Train->pMechViewAngle.x;
|
|
Camera.Angle.y = simulation::Train->pMechViewAngle.y;
|
|
}
|
|
|
|
Camera.Update();
|
|
|
|
if (simulation::Train != nullptr && false == FreeFlyModeFlag)
|
|
{
|
|
// keep the camera within cab boundaries
|
|
Camera.m_owneroffset = simulation::Train->clamp_inside(Camera.m_owneroffset);
|
|
}
|
|
|
|
if (simulation::Train != nullptr && false == FreeFlyModeFlag && false == Global.CabWindowOpen)
|
|
{
|
|
// cache cab camera in case of view type switch
|
|
simulation::Train->pMechViewAngle = {Camera.Angle.x, Camera.Angle.y};
|
|
simulation::Train->pMechOffset = Camera.m_owneroffset;
|
|
}
|
|
|
|
if (true == FreeFlyModeFlag && Camera.m_owner != nullptr)
|
|
{
|
|
// cache external view config
|
|
auto &externalviewconfig{m_externalviewconfigs[m_externalviewmode]};
|
|
externalviewconfig.owner = Camera.m_owner;
|
|
externalviewconfig.offset = Camera.m_owneroffset;
|
|
externalviewconfig.angle = Camera.Angle;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// debug camera
|
|
DebugCamera.Update();
|
|
}
|
|
|
|
// reset window state, it'll be set again if applicable in a check below
|
|
Global.CabWindowOpen = false;
|
|
|
|
if (consist_view_active())
|
|
{
|
|
// the orbit, shunting, coupling and trackside views place the camera themselves
|
|
update_consist_view(Deltatime);
|
|
}
|
|
else if (simulation::Train != nullptr && controlled != nullptr && Camera.m_owner != nullptr && false == DebugCameraFlag)
|
|
{
|
|
// jeśli jazda w kabinie, przeliczyć trzeba parametry kamery
|
|
/*
|
|
auto tempangle = controlled->VectorFront() * ( controlled->physics->CabOccupied() == -1 ? -1 : 1 );
|
|
double modelrotate = atan2( -tempangle.x, tempangle.z );
|
|
*/
|
|
if (false == FreeFlyModeFlag && true == Global.ctrlState && (m_input.keyboard.key(GLFW_KEY_LEFT) != GLFW_RELEASE || m_input.keyboard.key(GLFW_KEY_RIGHT) != GLFW_RELEASE))
|
|
{
|
|
// jeśli lusterko lewe albo prawe (bez rzucania na razie)
|
|
Global.CabWindowOpen = true;
|
|
|
|
auto const lr{m_input.keyboard.key(GLFW_KEY_LEFT) != GLFW_RELEASE};
|
|
// Camera.Yaw powinno być wyzerowane, aby po powrocie patrzeć do przodu
|
|
Camera.Pos = controlled->GetPosition() + simulation::Train->MirrorPosition(lr); // pozycja lusterka // NOSONAR
|
|
Camera.Angle.y = 0; // odchylenie na bok od Camera.LookAt
|
|
if (simulation::Train->Occupied()->CabOccupied() == 0)
|
|
{
|
|
// gdy w korytarzu
|
|
Camera.LookAt = Camera.Pos - simulation::Train->GetDirection();
|
|
}
|
|
else if (Global.shiftState)
|
|
{
|
|
// patrzenie w bok przez szybę
|
|
Camera.LookAt = Camera.Pos - (lr ? -1.0 : 1.0) * controlled->VectorLeft() * (double)simulation::Train->Occupied()->CabOccupied();
|
|
}
|
|
else
|
|
{ // patrzenie w kierunku osi pojazdu, z uwzględnieniem kabiny - jakby z lusterka,
|
|
// ale bez odbicia
|
|
Camera.LookAt = Camera.Pos - simulation::Train->GetDirection() * (double)simulation::Train->Occupied()->CabOccupied(); //-1 albo 1
|
|
}
|
|
auto const *dynamic{simulation::Train->Dynamic()};
|
|
auto const shakeangles{dynamic->shake_angles(dynamic->shake_between(shake_fraction()))};
|
|
Camera.Angle.x = 0.5 * shakeangles.second; // hustanie kamery przod tyl
|
|
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
|
|
/*
|
|
Camera.Roll = std::atan( simulation::Train->pMechShake.x * simulation::Train->BaseShake.angle_scale.x ); // hustanie kamery na boki
|
|
Camera.Pitch = 0.5 * std::atan( simulation::Train->vMechVelocity.z * simulation::Train->BaseShake.angle_scale.z ); // hustanie kamery przod tyl
|
|
*/
|
|
Camera.vUp = controlled->VectorUp();
|
|
}
|
|
else
|
|
{
|
|
// patrzenie standardowe
|
|
if (false == FreeFlyModeFlag)
|
|
{
|
|
// potentially restore view angle after returning from external view
|
|
// TODO: mirror view toggle as separate method
|
|
Camera.Angle.x = simulation::Train->pMechViewAngle.x;
|
|
Camera.Angle.y = simulation::Train->pMechViewAngle.y;
|
|
}
|
|
|
|
float const shakescale{FreeFlyModeFlag ? 5.0f : 1.0f};
|
|
// the shake steps at a fixed rate, the frames come at their own: taken at the frame's moment between the steps, it moves as smoothly as the vehicle
|
|
auto const shake{Camera.m_owner->shake_between(shake_fraction())};
|
|
auto shakencamerapos{Camera.m_owneroffset + shakescale * glm::vec3(1.5 * shake.offset.x, 2.0 * shake.offset.y, 1.5 * shake.offset.z)};
|
|
|
|
Camera.Pos = Camera.m_owner->GetWorldPosition(FreeFlyModeFlag ? glm::dvec3(shakencamerapos) : // TODO: vehicle collision box for the external vehicle camera
|
|
simulation::Train->clamp_inside(shakencamerapos));
|
|
|
|
if (!Global.iPause)
|
|
{
|
|
// podczas pauzy nie przeliczać kątów przypadkowymi wartościami
|
|
auto const shakeangles{Camera.m_owner->shake_angles(shake)};
|
|
Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl
|
|
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
|
|
/*
|
|
Camera.Roll = std::atan( simulation::Train->vMechVelocity.x * simulation::Train->BaseShake.angle_scale.x ); // hustanie kamery na boki
|
|
Camera.Pitch -= 0.5 * atan( simulation::Train->vMechVelocity.z * simulation::Train->BaseShake.angle_scale.z ); // hustanie kamery przod tyl
|
|
*/
|
|
}
|
|
/*
|
|
// ABu011104: rzucanie pudlem
|
|
vector3 temp;
|
|
if( abs( Train->pMechShake.y ) < 0.25 )
|
|
temp = vector3( 0, 0, 6 * Train->pMechShake.y );
|
|
else if( ( Train->pMechShake.y ) > 0 )
|
|
temp = vector3( 0, 0, 6 * 0.25 );
|
|
else
|
|
temp = vector3( 0, 0, -6 * 0.25 );
|
|
if( Controlled )
|
|
Controlled->ABuSetModelShake( temp );
|
|
// ABu: koniec rzucania
|
|
*/
|
|
if (simulation::Train->Occupied()->CabOccupied() == 0)
|
|
{
|
|
// gdy w korytarzu
|
|
Camera.LookAt = Camera.m_owner->GetWorldPosition(Camera.m_owneroffset) + Camera.m_owner->VectorFront() * 5.0;
|
|
}
|
|
else
|
|
{
|
|
// patrzenie w kierunku osi pojazdu, z uwzględnieniem kabiny
|
|
Camera.LookAt = Camera.m_owner->GetWorldPosition(Camera.m_owneroffset) + Camera.m_owner->VectorFront() * 5.0 * (double)simulation::Train->Occupied()->CabOccupied(); //-1 albo 1
|
|
}
|
|
Camera.vUp = simulation::Train->GetUp();
|
|
}
|
|
}
|
|
// all done, update camera position to the new value
|
|
Global.pCamera = Camera;
|
|
}
|
|
|
|
void driver_mode::OnKeyDown(int cKey)
|
|
{
|
|
// dump keypress info in the log
|
|
// podczas pauzy klawisze nie działają
|
|
std::string keyinfo;
|
|
auto keyname = glfwGetKeyName(cKey, 0);
|
|
if (keyname != nullptr)
|
|
{
|
|
keyinfo += std::string(keyname);
|
|
}
|
|
else
|
|
{
|
|
switch (cKey)
|
|
{
|
|
|
|
case GLFW_KEY_SPACE:
|
|
{
|
|
keyinfo += "Space";
|
|
break;
|
|
}
|
|
case GLFW_KEY_ENTER:
|
|
{
|
|
keyinfo += "Enter";
|
|
break;
|
|
}
|
|
case GLFW_KEY_ESCAPE:
|
|
{
|
|
keyinfo += "Esc";
|
|
break;
|
|
}
|
|
case GLFW_KEY_TAB:
|
|
{
|
|
keyinfo += "Tab";
|
|
break;
|
|
}
|
|
case GLFW_KEY_INSERT:
|
|
{
|
|
keyinfo += "Insert";
|
|
break;
|
|
}
|
|
case GLFW_KEY_DELETE:
|
|
{
|
|
keyinfo += "Delete";
|
|
break;
|
|
}
|
|
case GLFW_KEY_HOME:
|
|
{
|
|
keyinfo += "Home";
|
|
break;
|
|
}
|
|
case GLFW_KEY_END:
|
|
{
|
|
keyinfo += "End";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F1:
|
|
{
|
|
keyinfo += "F1";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F2:
|
|
{
|
|
keyinfo += "F2";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F3:
|
|
{
|
|
keyinfo += "F3";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F4:
|
|
{
|
|
keyinfo += "F4";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F5:
|
|
{
|
|
keyinfo += "F5";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F6:
|
|
{
|
|
keyinfo += "F6";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F7:
|
|
{
|
|
keyinfo += "F7";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F8:
|
|
{
|
|
keyinfo += "F8";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F9:
|
|
{
|
|
keyinfo += "F9";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F10:
|
|
{
|
|
keyinfo += "F10";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F11:
|
|
{
|
|
keyinfo += "F11";
|
|
break;
|
|
}
|
|
case GLFW_KEY_F12:
|
|
{
|
|
keyinfo += "F12";
|
|
break;
|
|
}
|
|
case GLFW_KEY_PAUSE:
|
|
{
|
|
keyinfo += "Pause";
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (keyinfo.empty() == false)
|
|
{
|
|
|
|
std::string keymodifiers;
|
|
if (Global.shiftState)
|
|
keymodifiers += "[Shift]+";
|
|
if (Global.ctrlState)
|
|
keymodifiers += "[Ctrl]+";
|
|
|
|
WriteLog("Key pressed: " + keymodifiers + "[" + keyinfo + "]");
|
|
}
|
|
|
|
// actual key processing
|
|
// TODO: redo the input system
|
|
if (cKey >= GLFW_KEY_0 && cKey <= GLFW_KEY_9)
|
|
{
|
|
// klawisze cyfrowe
|
|
int i = cKey - GLFW_KEY_0; // numer klawisza
|
|
if (Global.shiftState)
|
|
{
|
|
// z [Shift] uruchomienie eventu
|
|
if (Global.iPause == 0 // podczas pauzy klawisze nie działają
|
|
&& KeyEvents[i] != nullptr)
|
|
{
|
|
m_relay.post(user_command::queueevent, 0.0, 0.0, GLFW_PRESS, 0, glm::vec3(0.0f), &KeyEvents[i]->name());
|
|
}
|
|
}
|
|
else if (Global.ctrlState)
|
|
{
|
|
// zapamiętywanie kamery może działać podczas pauzy
|
|
if (FreeFlyModeFlag)
|
|
{
|
|
// w trybie latania można przeskakiwać do ustawionych kamer
|
|
if (Global.FreeCameraInit[i].x == 0.0 && Global.FreeCameraInit[i].y == 0.0 && Global.FreeCameraInit[i].z == 0.0)
|
|
{
|
|
// jeśli kamera jest w punkcie zerowym, zapamiętanie współrzędnych i kątów
|
|
Global.FreeCameraInit[i] = Camera.Pos;
|
|
Global.FreeCameraInitAngle[i] = Camera.Angle;
|
|
// logowanie, żeby można było do scenerii przepisać
|
|
WriteLog("camera " + std::to_string(Global.FreeCameraInit[i].x) + " " + std::to_string(Global.FreeCameraInit[i].y) + " " + std::to_string(Global.FreeCameraInit[i].z) + " " +
|
|
std::to_string(glm::degrees(Global.FreeCameraInitAngle[i].x)) + " " + std::to_string(glm::degrees(Global.FreeCameraInitAngle[i].y)) + " " +
|
|
std::to_string(glm::degrees(Global.FreeCameraInitAngle[i].z)) + " " + std::to_string(i) + " endcamera");
|
|
}
|
|
else // również przeskakiwanie
|
|
{ // Ra: to z tą kamerą (Camera.Pos i Global.pCameraPosition) jest trochę bez sensu
|
|
Global.pCamera.Pos = Global.FreeCameraInit[i]; // nowa pozycja dla generowania obiektów
|
|
Camera.Init(Global.FreeCameraInit[i], Global.FreeCameraInitAngle[i], nullptr); // przestawienie
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
switch (cKey)
|
|
{
|
|
|
|
case GLFW_KEY_F4:
|
|
{
|
|
if (Global.shiftState)
|
|
{
|
|
ExternalView();
|
|
} // with Shift, cycle through external views
|
|
else
|
|
{
|
|
InOutKey();
|
|
} // without, step out of the cab or return to it
|
|
break;
|
|
}
|
|
case GLFW_KEY_F5:
|
|
{
|
|
// przesiadka do innego pojazdu
|
|
if (!FreeFlyModeFlag)
|
|
// only available in free fly mode
|
|
break;
|
|
|
|
TDynamicObject *dynamic = std::get<TDynamicObject *>(simulation::Region->find_vehicle(Global.pCamera.Pos, 50, false, false));
|
|
if (dynamic)
|
|
{
|
|
m_relay.post(user_command::entervehicle, Global.ctrlState ? GLFW_MOD_CONTROL : 0, simulation::Train ? simulation::Train->id() : 0, GLFW_PRESS, 0, dynamic->GetPosition(),
|
|
&dynamic->name());
|
|
|
|
change_train = dynamic->name();
|
|
}
|
|
|
|
break;
|
|
}
|
|
case GLFW_KEY_F6:
|
|
{
|
|
// przyspieszenie symulacji do testowania scenerii... uwaga na FPS!
|
|
if (DebugModeFlag)
|
|
{
|
|
|
|
if (Global.ctrlState)
|
|
{
|
|
Global.fTimeSpeed = Global.shiftState ? 60.0 : 20.0;
|
|
}
|
|
else
|
|
{
|
|
Global.fTimeSpeed = Global.shiftState ? 5.0 : Global.default_timespeed;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case GLFW_KEY_F7:
|
|
{
|
|
// debug mode functions
|
|
if (DebugModeFlag)
|
|
{
|
|
|
|
if (Global.ctrlState && Global.shiftState)
|
|
{
|
|
// shift + ctrl + f7 toggles between debug and regular camera
|
|
DebugCameraFlag = !DebugCameraFlag;
|
|
}
|
|
else if (Global.ctrlState)
|
|
{
|
|
// ctrl + f7 toggles static daylight
|
|
Global.FakeLight = !Global.FakeLight;
|
|
simulation::Environment.on_daylight_change();
|
|
break;
|
|
}
|
|
else if (Global.shiftState)
|
|
{
|
|
// shift + f7 is currently unused
|
|
}
|
|
else
|
|
{
|
|
// f7: wireframe toggle
|
|
Global.bWireFrame = !Global.bWireFrame;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case GLFW_KEY_F11:
|
|
{
|
|
// editor mode
|
|
if (false == Global.ctrlState && false == Global.shiftState)
|
|
{
|
|
Application.push_mode(eu07_application::mode::editor);
|
|
}
|
|
break;
|
|
}
|
|
case GLFW_KEY_LEFT_BRACKET:
|
|
case GLFW_KEY_RIGHT_BRACKET:
|
|
{
|
|
// in the orbit, shunting, coupling and trackside views: the vehicle (coupling) towards the front, or the rear,
|
|
// of the train. [Ctrl] with them moves the train in the debug mode, bound in the keyboard settings
|
|
if (false == Global.ctrlState && consist_view_active())
|
|
{
|
|
m_consistviews.select(cKey == GLFW_KEY_LEFT_BRACKET ? -1 : 1);
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return; // nie są przekazywane do pojazdu wcale
|
|
}
|
|
|
|
bool driver_mode::consist_view_active() const
|
|
{
|
|
return FreeFlyModeFlag && m_externalview && m_externalviewmode >= view::orbit && Camera.m_owner != nullptr && false == DebugCameraFlag;
|
|
}
|
|
|
|
void driver_mode::update_consist_view(double const Deltatime)
|
|
{
|
|
auto *controlled{simulation::Train != nullptr ? simulation::Train->Dynamic() : nullptr};
|
|
// the camera goes with the vehicle picked, or stays where it is once the train is gone
|
|
Camera.m_owner = m_consistviews.update(Camera, controlled, Deltatime);
|
|
}
|
|
|
|
// places camera outside the controlled vehicle, or nearest if nothing is under control
|
|
// depending on provided switch the view is placed right outside, or at medium distance
|
|
void driver_mode::DistantView(bool const Near)
|
|
{
|
|
|
|
TDynamicObject const *vehicle = {(simulation::Train != nullptr ? simulation::Train->Dynamic() : pDynamicNearest)};
|
|
|
|
if (vehicle == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
auto const cab = vehicle->physics->CabOccupied() == 0 ? 1 : vehicle->physics->CabOccupied();
|
|
auto const left = vehicle->VectorLeft() * (double)cab;
|
|
|
|
if (true == Near)
|
|
{
|
|
|
|
Camera.Pos = glm::dvec3(Camera.Pos.x, vehicle->GetPosition().y, Camera.Pos.z) + left * vehicle->GetWidth() + glm::dvec3(1.25 * left.x, 1.6, 1.25 * left.z);
|
|
}
|
|
else
|
|
{
|
|
|
|
Camera.Pos = vehicle->GetPosition() + vehicle->VectorFront() * (double)vehicle->physics->CabOccupied() * 50.0 + glm::dvec3(-10.0 * left.x, 1.6, -10.0 * left.z);
|
|
}
|
|
|
|
Camera.m_owner = nullptr;
|
|
Camera.LookAt = vehicle->GetPosition();
|
|
Camera.RaLook(); // jednorazowe przestawienie kamery
|
|
}
|
|
|
|
void driver_mode::ExternalView()
|
|
{
|
|
|
|
auto *train{simulation::Train};
|
|
if (train == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
auto *vehicle{train->Dynamic()};
|
|
|
|
// disable detailed cab in external view modes
|
|
vehicle->bDisplayCab = false;
|
|
|
|
if (true == m_externalview)
|
|
{
|
|
// we're already in some external view mode, so select next one on the list
|
|
// the views of the train's vehicles come after the four older ones, unless they're turned off in eu07.ini
|
|
m_externalviewmode = clamp_circular(++m_externalviewmode, static_cast<int>(Global.ConsistViews ? view::count_ : view::orbit));
|
|
}
|
|
|
|
FreeFlyModeFlag = true;
|
|
m_externalview = true;
|
|
|
|
Camera.Reset();
|
|
// configure camera placement for the selected view mode
|
|
switch (m_externalviewmode)
|
|
{
|
|
case view::consistfront:
|
|
{
|
|
// bind camera with the vehicle
|
|
auto *owner{vehicle->Mechanik->Vehicle(VehicleEnd::front)};
|
|
|
|
Camera.m_owner = owner;
|
|
|
|
auto const &viewconfig{m_externalviewconfigs[m_externalviewmode]};
|
|
if (owner == viewconfig.owner)
|
|
{
|
|
// restore view config for previous owner
|
|
Camera.m_owneroffset = viewconfig.offset;
|
|
Camera.Angle = viewconfig.angle;
|
|
}
|
|
else
|
|
{
|
|
// default view setup
|
|
auto const offsetflip{(vehicle->physics->CabOccupied() == 0 ? 1 : vehicle->physics->CabOccupied()) *
|
|
(vehicle->physics->DirActive == 0 ? 1 : vehicle->physics->DirActive)};
|
|
|
|
Camera.m_owneroffset = {1.5 * owner->physics->Dim.W * offsetflip, std::max(5.0, 1.25 * owner->physics->Dim.H), -0.4 * owner->physics->Dim.L * offsetflip};
|
|
|
|
Camera.Angle.y = glm::radians(vehicle->physics->DirActive < 0 ? 180.0 : 0.0);
|
|
}
|
|
auto const shakeangles{owner->shake_angles()};
|
|
Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl
|
|
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
|
|
|
|
break;
|
|
}
|
|
case view::consistrear:
|
|
{
|
|
// bind camera with the vehicle
|
|
auto *owner{vehicle->Mechanik->Vehicle(VehicleEnd::rear)};
|
|
|
|
Camera.m_owner = owner;
|
|
|
|
auto const &viewconfig{m_externalviewconfigs[m_externalviewmode]};
|
|
if (owner == viewconfig.owner)
|
|
{
|
|
// restore view config for previous owner
|
|
Camera.m_owneroffset = viewconfig.offset;
|
|
Camera.Angle = viewconfig.angle;
|
|
}
|
|
else
|
|
{
|
|
// default view setup
|
|
auto const offsetflip{(vehicle->physics->CabOccupied() == 0 ? 1 : vehicle->physics->CabOccupied()) *
|
|
(vehicle->physics->DirActive == 0 ? 1 : vehicle->physics->DirActive) * -1};
|
|
|
|
Camera.m_owneroffset = {1.5 * owner->physics->Dim.W * offsetflip, std::max(5.0, 1.25 * owner->physics->Dim.H), 0.2 * owner->physics->Dim.L * offsetflip};
|
|
|
|
Camera.Angle.y = glm::radians(vehicle->physics->DirActive < 0 ? 0.0 : 180.0);
|
|
}
|
|
auto const shakeangles{owner->shake_angles()};
|
|
Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl
|
|
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
|
|
break;
|
|
}
|
|
case view::bogie:
|
|
{
|
|
auto *owner{vehicle->Mechanik->Vehicle(VehicleEnd::front)};
|
|
|
|
Camera.m_owner = owner;
|
|
|
|
auto const &viewconfig{m_externalviewconfigs[m_externalviewmode]};
|
|
if (owner == viewconfig.owner)
|
|
{
|
|
// restore view config for previous owner
|
|
Camera.m_owneroffset = viewconfig.offset;
|
|
Camera.Angle = viewconfig.angle;
|
|
}
|
|
else
|
|
{
|
|
// default view setup
|
|
auto const offsetflip{(vehicle->physics->CabOccupied() == 0 ? 1 : vehicle->physics->CabOccupied()) *
|
|
(vehicle->physics->DirActive == 0 ? 1 : vehicle->physics->DirActive)};
|
|
|
|
Camera.m_owneroffset = {-0.65 * owner->physics->Dim.W * offsetflip, 0.90, 0.15 * owner->physics->Dim.L * offsetflip};
|
|
|
|
Camera.Angle.y = glm::radians(vehicle->physics->DirActive < 0 ? 180.0 : 0.0);
|
|
}
|
|
auto const shakeangles{owner->shake_angles()};
|
|
Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl
|
|
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
|
|
|
|
break;
|
|
}
|
|
case view::driveby:
|
|
{
|
|
DistantView(false);
|
|
break;
|
|
}
|
|
case view::orbit:
|
|
case view::shunting:
|
|
case view::coupling:
|
|
case view::trackside:
|
|
{
|
|
// the camera goes with a vehicle of the train, placed by update_consist_view() from the next frame on
|
|
auto const kind{static_cast<consist_views::kind>(m_externalviewmode - view::orbit)};
|
|
Camera.m_owner = m_consistviews.enter(kind, vehicle);
|
|
Camera.m_owneroffset = {};
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// ustawienie śledzenia pojazdu
|
|
void driver_mode::CabView()
|
|
{
|
|
|
|
// TODO: configure owner and camera placement depending on the view mode
|
|
if (true == FreeFlyModeFlag)
|
|
{
|
|
return;
|
|
}
|
|
|
|
auto *train{simulation::Train};
|
|
if (train == nullptr)
|
|
{
|
|
return;
|
|
}
|
|
|
|
m_externalview = false;
|
|
|
|
// likwidacja obrotów - patrzy horyzontalnie na południe
|
|
Camera.Reset();
|
|
|
|
// bind camera with the vehicle
|
|
Camera.m_owner = train->Dynamic();
|
|
// potentially restore cached camera setup
|
|
Camera.m_owneroffset = train->pMechSittingPosition;
|
|
Camera.Angle.x = train->pMechViewAngle.x;
|
|
Camera.Angle.y = train->pMechViewAngle.y;
|
|
|
|
auto const shakeangles{Camera.m_owner->shake_angles()};
|
|
Camera.Angle.x -= 0.5 * shakeangles.second; // hustanie kamery przod tyl
|
|
Camera.Angle.z = shakeangles.first; // hustanie kamery na boki
|
|
|
|
if (train->Occupied()->CabOccupied() == 0)
|
|
{
|
|
Camera.LookAt = Camera.m_owner->GetWorldPosition(Camera.m_owneroffset) + Camera.m_owner->VectorFront() * 5.0;
|
|
}
|
|
else
|
|
{
|
|
// patrz w strone wlasciwej kabiny
|
|
Camera.LookAt = Camera.m_owner->GetWorldPosition(Camera.m_owneroffset) + Camera.m_owner->VectorFront() * 5.0 * (double)Camera.m_owner->physics->CabOccupied();
|
|
}
|
|
train->pMechOffset = Camera.m_owneroffset;
|
|
}
|
|
|
|
void driver_mode::InOutKey()
|
|
{ // przełączenie widoku z kabiny na zewnętrzny i odwrotnie
|
|
FreeFlyModeFlag = !FreeFlyModeFlag; // zmiana widoku
|
|
|
|
auto *train{simulation::Train};
|
|
|
|
if (train == nullptr)
|
|
{
|
|
FreeFlyModeFlag = true; // nadal poza kabiną
|
|
Camera.m_owner = nullptr; // detach camera from the vehicle
|
|
return;
|
|
}
|
|
|
|
auto *vehicle{train->Dynamic()};
|
|
|
|
if (FreeFlyModeFlag)
|
|
{
|
|
// jeżeli poza kabiną, przestawiamy w jej okolicę - OK
|
|
// cache current cab position so there's no need to set it all over again after each out-in switch
|
|
train->pMechSittingPosition = train->pMechOffset;
|
|
|
|
vehicle->bDisplayCab = false;
|
|
DistantView(true);
|
|
|
|
DebugCamera = Camera;
|
|
}
|
|
else
|
|
{
|
|
// jazda w kabinie
|
|
// zerowanie przesunięcia przed powrotem?
|
|
vehicle->ABuSetModelShake({0, 0, 0});
|
|
vehicle->bDisplayCab = true;
|
|
CabView(); // na pozycję mecha
|
|
}
|
|
// update window title to reflect the situation
|
|
Application.set_title(Global.AppName + " (" + (train != nullptr ? train->Occupied()->Name : "") + " @ " + Global.SceneryFile + ")");
|
|
}
|
|
|
|
void driver_mode::set_picking(bool const Picking)
|
|
{
|
|
|
|
if (Picking)
|
|
{
|
|
// enter picking mode
|
|
Application.set_cursor_pos(m_input.mouse_pickmodepos.x, m_input.mouse_pickmodepos.y);
|
|
Application.set_cursor(GLFW_CURSOR_NORMAL);
|
|
}
|
|
else
|
|
{
|
|
// switch off
|
|
m_input.mouse_pickmodepos = glm::dvec2(Global.cursor_pos);
|
|
Application.set_cursor(GLFW_CURSOR_DISABLED);
|
|
Application.set_cursor_pos(0, 0);
|
|
}
|
|
// actually toggle the mode
|
|
Global.ControlPicking = Picking;
|
|
}
|