From 5350d9ba6b9e7ff6597f76f51acf48ddd0a7aa92 Mon Sep 17 00:00:00 2001 From: miloszunio Date: Mon, 30 Nov 2026 12:33:36 +0000 Subject: [PATCH] fix(driver): the flashlight after the review of 24.11: a local command, a shadow map of its own, a free key The command went over the network. flashlighttoggle was a simulation command, and those are sent to every machine of a network game: one player's key switched the flashlight of all of them. It is an entity command now, as the commands of the camera are; those stay on the machine they were given on (command_queue::is_network_target()). TCamera::OnCommand() takes it, simulation.cpp no longer does. The shadows took the budget of the scenery lamps. The flashlight is the light nearest to the camera, so it took the first of the gfx.lights.shadows.lamps slots, and as it moves with the player its map was drawn anew in nearly every frame, out of the scenery drawings a frame allows the lamps (one a frame at levels 1 and 2): the lamps kept waiting for their shadows. The flashlight now has a slot of its own past those of the lamps, made when it is first switched on, and its map is drawn when it needs it, not counted against the lamps. With gfx.lights.shadows.lamps 0 it has no shadows, as before. The key. Ctrl+Shift+L is motorconnectorsclose in the eu07_input-keyboard.ini the game ships with, and bind() gives a key two commands share to the later one in user_command, so the flashlight took it over. The default is Ctrl+Shift+F, which neither that file nor the defaults in the code use. For the files of the players that do use it: a default of the flashlight on a key their file binds to another command is dropped (keyboard_input::yield_default()), with a line in the log, so their binding keeps working; they bind the flashlight to a key of their choice. --- input/command.cpp | 3 +- input/command.h | 3 +- input/driverkeyboardinput.cpp | 5 ++- input/keyboardinput.cpp | 24 +++++++++++++ input/keyboardinput.h | 8 +++++ rendering/common/localshadows.cpp | 57 +++++++++++++++++++++++++++---- rendering/common/localshadows.h | 9 ++++- simulation/simulation.cpp | 3 -- utilities/Globals.h | 2 +- vehicle/Camera.cpp | 9 +++++ 10 files changed, 109 insertions(+), 14 deletions(-) diff --git a/input/command.cpp b/input/command.cpp index 5ca2dfe83..5e52edf30 100644 --- a/input/command.cpp +++ b/input/command.cpp @@ -459,7 +459,8 @@ commanddescription_sequence Commands_descriptions = { {"gensettoggle", command_target::vehicle, command_mode::oneoff}, {"gensetenable", command_target::vehicle, command_mode::oneoff}, {"gensetdisable", command_target::vehicle, command_mode::oneoff}, - {"flashlighttoggle", command_target::simulation, command_mode::oneoff}, + // the flashlight is the player's own: an entity command, which stays on the machine it was given on (command_queue::is_network_target()) + {"flashlighttoggle", command_target::entity, command_mode::oneoff}, }; // Maps of command and coresponding strings diff --git a/input/command.h b/input/command.h index f6dc95409..f8460fdd4 100644 --- a/input/command.h +++ b/input/command.h @@ -459,7 +459,8 @@ enum class user_command gensettoggle, gensetenable, gensetdisable, - // the flashlight of the player (Global.flashlight), at the end so the numbers of the commands before it stay as they were + // the flashlight of the player (Global.flashlight), at the end so the numbers of the commands before it stay as they were. + // an entity command, taken by the camera of the player (TCamera::OnCommand()): it isn't sent to the other machines of a network game flashlighttoggle, none = -1 }; diff --git a/input/driverkeyboardinput.cpp b/input/driverkeyboardinput.cpp index b0cb7a65c..6119adf30 100644 --- a/input/driverkeyboardinput.cpp +++ b/input/driverkeyboardinput.cpp @@ -15,6 +15,8 @@ driverkeyboard_input::init() { default_bindings(); recall_bindings(); + // the flashlight came after the players wrote their bindings: it takes no key from a command of their file + yield_default( user_command::flashlighttoggle ); bind(); return true; @@ -269,7 +271,8 @@ driverkeyboard_input::default_bindings() { // interiorlightenable, // interiorlightdisable, { interiorlightdimtoggle, {GLFW_KEY_APOSTROPHE | keymodifier::control, ""} }, - { flashlighttoggle, {GLFW_KEY_L | keymodifier::control | keymodifier::shift, ""} }, + // Ctrl+Shift+F: no command has it in the eu07_input-keyboard.ini the game ships with (Ctrl+Shift+L there is motorconnectorsclose) + { flashlighttoggle, {GLFW_KEY_F | keymodifier::control | keymodifier::shift, ""} }, // interiorlightdimenable, // interiorlightdimdisable, // compartmentlightstoggle, diff --git a/input/keyboardinput.cpp b/input/keyboardinput.cpp index 4c2fdf8f0..9a5696cf0 100644 --- a/input/keyboardinput.cpp +++ b/input/keyboardinput.cpp @@ -8,6 +8,7 @@ http://mozilla.org/MPL/2.0/. */ #include "stdafx.h" +#include #include "input/keyboardinput.h" #include "utilities/Globals.h" #include "utilities/Logs.h" @@ -217,6 +218,7 @@ keyboard_input::recall_bindings() { std::tuple binding{keycode, description}; m_bindingsetups.insert_or_assign(lookup->second, binding); + m_recalled.insert( lookup->second ); } } } @@ -225,6 +227,28 @@ keyboard_input::recall_bindings() { return true; } +void keyboard_input::yield_default( user_command const Command ) { + + auto const own { m_bindingsetups.find( Command ) }; + if( own == m_bindingsetups.end() || m_recalled.contains( Command ) ) { + // no default to yield, or the file binds the command itself: the choice of the player + return; + } + auto const key { std::get( own->second ) }; + if( key == 0 ) { + return; + } + auto const taken { std::ranges::find_if( m_bindingsetups, [this, Command, key]( auto const &Setup ) { + return Setup.first != Command && std::get( Setup.second ) == key && m_recalled.contains( Setup.first ); } ) }; + if( taken == m_bindingsetups.end() ) { + return; + } + WriteLog( + "Keyboard binding: \"" + simulation::Commands_descriptions[ static_cast( Command ) ].name + "\" is left without a key, its default one is bound to \"" + + simulation::Commands_descriptions[ static_cast( taken->first ) ].name + "\" in eu07_input-keyboard.ini" ); + std::get( own->second ) = 0; +} + void keyboard_input::dump_bindings() const { std::fstream stream("eu07_input-keyboard.ini", diff --git a/input/keyboardinput.h b/input/keyboardinput.h index 1611c9a3c..eb2886152 100644 --- a/input/keyboardinput.h +++ b/input/keyboardinput.h @@ -9,6 +9,8 @@ http://mozilla.org/MPL/2.0/. #pragma once +#include +#include #include #include #include "input/command.h" @@ -79,11 +81,17 @@ protected: default_bindings() = 0; bool recall_bindings(); + // a command added after the players wrote their bindings: where their file doesn't list it and gives the key of its default + // to another command, the command is left without a key, so that the one of the file goes on working. bind() gives a key + // two commands share to the one later in user_command, which a new command always is + void + yield_default( user_command const Command ); void bind(); // members bindingsetup_sequence m_bindingsetups; + std::set m_recalled; // the commands the file of the bindings lists private: // types diff --git a/rendering/common/localshadows.cpp b/rendering/common/localshadows.cpp index 867473ed1..c454d382a 100644 --- a/rendering/common/localshadows.cpp +++ b/rendering/common/localshadows.cpp @@ -48,6 +48,12 @@ float const CAB_RANGE_MIN{0.25f}; // walls are where they are for both float const CAB_SHARE_DISTANCE{1.0f}; +// the light is the flashlight of the player (gfx::local_lights::add_flashlight()) +bool is_flashlight(gfx::local_lights::light const &Light) +{ + return Light.owner == &Global.flashlight; +} + } // namespace local_shadows::level_setup local_shadows::setup(int const Level) @@ -346,7 +352,12 @@ void local_shadows::update_lamps(std::vector const &Li m_frame = Framestamp; m_wanted.clear(); auto const level{std::clamp(Global.gfx_lights_shadows, 0, 3)}; - auto const count{level > 0 ? Global.gfx_lights_shadows_lamps : 0}; + auto const lamps{level > 0 ? Global.gfx_lights_shadows_lamps : 0}; + // the flashlight of the player gets a map of its own, past those of the lamps, from when it is first switched on (the maps + // are made anew then, once). it goes with the player, so its map is drawn anew in nearly every frame: among the maps of the + // lamps it would take the first of them for itself, and with it the few drawings of their scenery a frame allows + m_flashlightslot = m_flashlightslot || (Global.flashlight && (lamps > 0)); + auto const count{((lamps > 0) && m_flashlightslot) ? lamps + 1 : lamps}; auto const size{count > 0 ? Global.gfx_lights_shadows_lamps_size : 0}; if ((static_cast(count) != m_slots.size()) || (size != m_lampssize)) { @@ -358,15 +369,17 @@ void local_shadows::update_lamps(std::vector const &Li } m_sceneryleft = setup(level).sceneryperframe; auto const reach{static_cast(Global.gfx_lights_shadows_lamps_range)}; + // the slots of the lamps: all of them but the last, when that is the flashlight's + auto const lampslots{m_slots.size() - (m_flashlightslot ? 1 : 0)}; // the lights come the most important first: the first ones within reach get the slots std::vector picked; for (auto const &light : Lights) { - if (picked.size() >= m_slots.size()) + if (picked.size() >= lampslots) { break; } - if ((light.owner == nullptr) || (false == light.cast_shadows) || (light.range < LAMP_RANGE_MIN) || (glm::length(light.position - Camera) > reach)) + if ((light.owner == nullptr) || is_flashlight(light) || (false == light.cast_shadows) || (light.range < LAMP_RANGE_MIN) || (glm::length(light.position - Camera) > reach)) { continue; } @@ -377,7 +390,7 @@ void local_shadows::update_lamps(std::vector const &Li for (auto const *light : picked) { auto placed{false}; - for (std::size_t idx = 0; idx < m_slots.size(); ++idx) + for (std::size_t idx = 0; idx < lampslots; ++idx) { auto &slot{m_slots[idx]}; if ((slot.owner == light->owner) && (slot.part == light->part) && (slot.wantedframe != Framestamp)) @@ -405,7 +418,7 @@ void local_shadows::update_lamps(std::vector const &Li // the others take the slots of lamps no longer picked for (auto const *light : unplaced) { - for (std::size_t idx = 0; idx < m_slots.size(); ++idx) + for (std::size_t idx = 0; idx < lampslots; ++idx) { auto &slot{m_slots[idx]}; if (slot.wantedframe == Framestamp) @@ -424,6 +437,10 @@ void local_shadows::update_lamps(std::vector const &Li break; } } + if (auto const flashlight{std::ranges::find_if(Lights, is_flashlight)}; m_flashlightslot && (flashlight != Lights.end()) && flashlight->cast_shadows) + { + place_flashlight(*flashlight, Framestamp); + } // the scene changed (events, the editor): the scenery parts are out of date for (auto &slot : m_slots) { @@ -435,6 +452,25 @@ void local_shadows::update_lamps(std::vector const &Li } } +void local_shadows::place_flashlight(gfx::local_lights::light const &Light, unsigned int const Framestamp) +{ + auto const idx{m_slots.size() - 1}; + auto &slot{m_slots[idx]}; + if ((slot.owner != Light.owner) || (glm::length(slot.position - Light.position) > LAMP_MOVED)) + { + // switched on, or carried on: its map is drawn anew + slot = lamp_slot{}; + slot.owner = Light.owner; + slot.part = Light.part; + } + slot.position = Light.position; + slot.range = Light.range; + slot.nearplane = lamp_near(Light); + slot.shade_self = Light.shade_self; + slot.wantedframe = Framestamp; + m_wanted.emplace_back(idx); +} + void local_shadows::reset_lamps() { for (auto &slot : m_slots) @@ -446,7 +482,16 @@ void local_shadows::reset_lamps() bool local_shadows::scenery_wanted(std::size_t const Slot) { - if (m_slots[Slot].scenery || (m_sceneryleft <= 0)) + if (m_slots[Slot].scenery) + { + return false; + } + // the map of the flashlight is drawn when it needs it, and not out of what the frame allows the lamps + if (m_flashlightslot && (Slot + 1 == m_slots.size())) + { + return true; + } + if (m_sceneryleft <= 0) { return false; } diff --git a/rendering/common/localshadows.h b/rendering/common/localshadows.h index b04f4501a..a52716751 100644 --- a/rendering/common/localshadows.h +++ b/rendering/common/localshadows.h @@ -34,6 +34,7 @@ namespace gfx // every few frames (for the moving parts of the cab: levers, doors, wipers; a few maps per frame) or when the light, the cab or the // level change. a light keeps its map (slot) while it's among those picked; the other lights of the cab take the map of the // closest light with one. the near plane of these maps is a few centimetres: the desk right under its lights holds their light back; +// - the flashlight of the player (Global.flashlight), in a map kept as those of the lamps are, in a slot of its own past theirs // - the lamps of scenery models closest to the camera (Global.gfx_lights_shadows_lamps of them, within // Global.gfx_lights_shadows_lamps_range). the map of such a lamp is kept in world axes, centred on the lamp, and has two parts: // the scenery (drawn once, when the lamp gets its slot or the scene changes) and a working copy of it, with the vehicles near @@ -134,7 +135,8 @@ class local_shadows { return m_slots[Slot]; } - // whether the scenery part of the map of the slot is to be drawn in this frame. a few per frame at most, by the level + // whether the scenery part of the map of the slot is to be drawn in this frame. a few per frame at most, by the level; the + // map of the flashlight of the player whenever it needs it, beside those bool scenery_wanted(std::size_t const Slot); // whether the working copy of the slot, with vehicles, is to be drawn in this frame (every few frames, by the level) bool vehicles_wanted(std::size_t const Slot, unsigned int const Framestamp) const; @@ -287,6 +289,11 @@ class local_shadows std::vector m_wanted; unsigned int m_frame{0}; // framestamp of the last update_lamps() int m_sceneryleft{0}; // scenery parts which may still be drawn in this frame + // the flashlight of the player (Global.flashlight) has a slot of its own, the last one, beside the + // Global.gfx_lights_shadows_lamps slots of the lamps: from when it is first switched on + bool m_flashlightslot{false}; + // gives the flashlight its slot for the frame; its map is drawn anew when it was switched on or moved + void place_flashlight(gfx::local_lights::light const &Light, unsigned int const Framestamp); bool m_lampstiming{false}; std::vector m_lampvehicles; std::vector m_slotvehicles; diff --git a/simulation/simulation.cpp b/simulation/simulation.cpp index 360f2544f..a63ddfc03 100644 --- a/simulation/simulation.cpp +++ b/simulation/simulation.cpp @@ -259,9 +259,6 @@ void state_manager::process_commands() { if (commanddata.command == user_command::debugtoggle) DebugModeFlag = !DebugModeFlag; - if (commanddata.command == user_command::flashlighttoggle) - Global.flashlight = !Global.flashlight; - if (commanddata.command == user_command::pausetoggle) { if( Global.iPause & 1 ) { // jeśli pauza startowa diff --git a/utilities/Globals.h b/utilities/Globals.h index 77cb66652..fa644960b 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -197,7 +197,7 @@ struct global_settings { bool RenderShadows{ true }; bool applicationQuitOrder{false}; // the flashlight of the player is on: a cone of light from the camera where it looks (gfx::local_lights), switched with - // flashlighttoggle (Ctrl+Shift+L). off at the start, not kept between the runs + // flashlighttoggle (Ctrl+Shift+F). off at the start, not kept between the runs bool flashlight{false}; int RenderCabShadowsRange{ 0 }; struct shadowtune_t { diff --git a/vehicle/Camera.cpp b/vehicle/Camera.cpp index e7346b5bb..6c3c977d0 100644 --- a/vehicle/Camera.cpp +++ b/vehicle/Camera.cpp @@ -132,6 +132,15 @@ TCamera::OnCommand( command_data const &Command ) { break; } + case user_command::flashlighttoggle: { + // the flashlight of the player, switched as the key goes up. it is this player's alone: the command is an entity one, + // so it reaches only the camera of the machine it was given on, never the other players of a network game + if( Command.action == GLFW_RELEASE ) { + Global.flashlight = !Global.flashlight; + } + break; + } + default: { iscameracommand = false;