From 4ae7226cfab64309cf035f84665e5ed56cf979cc Mon Sep 17 00:00:00 2001 From: lcddisplay Date: Thu, 5 Nov 2026 22:32:59 +0100 Subject: [PATCH] Moths fly about the lit lamps of the scenery A part of the lamps of the scenery models which shine downwards (moths.share, picked by the places of the lamps) gets a swarm of moths: an instance of animals/cmy.t3d (animated by textures, shader vat) put at the light point when the lamp first comes within moths.range of the camera. The swarm is shown while its lamp shines (the light points gathered by the local lights in the frame), not in the winter and not below moths.temperature. The swarms are made by the game (basic_node::m_generated): not saved with the scenery, not edited. Settings (eu07.ini): moths yes/no, moths.share 0-1 (0.4), moths.range metres (80), moths.temperature degrees (8). Also in the debug window of the lights. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01GXgG7F4L42c2F9sDSAJbT2 --- CMakeLists.txt | 1 + lang/editor_pl.po | 5 ++ rendering/common/locallights.cpp | 11 ++- rendering/common/moths.cpp | 140 +++++++++++++++++++++++++++++++ rendering/common/moths.h | 63 ++++++++++++++ scene/scenelayers.cpp | 8 ++ scene/scenenode.cpp | 2 + scene/scenenode.h | 1 + utilities/Globals.cpp | 8 ++ utilities/Globals.h | 6 ++ 10 files changed, 244 insertions(+), 1 deletion(-) create mode 100644 rendering/common/moths.cpp create mode 100644 rendering/common/moths.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d9ef586f4..37d0b9650 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -360,6 +360,7 @@ set(SOURCES "rendering/common/gpuparticles.cpp" "rendering/common/windscreenwater.cpp" "rendering/common/locallights.cpp" +"rendering/common/moths.cpp" "rendering/common/skylight.cpp" "rendering/common/weatherstate.cpp" "rendering/common/exposure.cpp" diff --git a/lang/editor_pl.po b/lang/editor_pl.po index e7332801e..7499dfc73 100644 --- a/lang/editor_pl.po +++ b/lang/editor_pl.po @@ -15867,6 +15867,11 @@ msgstr "Nie udało się zapisać sieci trakcyjnej: jej plik \"%s\" jest plikiem msgid "it's a part of the catenary, which is changed with the Catenary tool (F6)" msgstr "to część sieci trakcyjnej, którą zmienia się narzędziem Sieć trakcyjna (F6)" +# scenery editor: moths at the lamps, made by the game + +msgid "it's made by the game while it runs (the moths at a lamp), not a part of the scenery" +msgstr "to robi sama gra podczas jazdy (ćmy przy lampie), nie jest częścią scenerii" + msgid "A part of the catenary: it's changed with the Catenary tool (F6)" msgstr "Część sieci trakcyjnej: zmienia się ją narzędziem Sieć trakcyjna (F6)" diff --git a/rendering/common/locallights.cpp b/rendering/common/locallights.cpp index 9f042afe0..3edfbbd7d 100644 --- a/rendering/common/locallights.cpp +++ b/rendering/common/locallights.cpp @@ -10,6 +10,7 @@ http://mozilla.org/MPL/2.0/. #include "stdafx.h" #include "rendering/common/locallights.h" +#include "rendering/common/moths.h" #include #include @@ -260,6 +261,9 @@ void gfx::local_lights::gather(glm::dvec3 const &Camera, scene::basic_region &Re }); } + // the moths fly about the lamps of the scenery which shine + Moths.update(m_lights, Camera, Framestamp); + auto const elapsed{std::chrono::duration(std::chrono::steady_clock::now() - start).count()}; m_gathertime = (m_gathertime == 0.0 ? elapsed : m_gathertime * 0.95 + elapsed * 0.05); } @@ -274,6 +278,7 @@ void gfx::local_lights::reset() m_sections.clear(); m_models.clear(); m_scenerycount = m_vehiclecount = m_compartmentcount = m_placed = m_dropped = 0; + Moths.reset(); } void gfx::local_lights::gather_instance(TAnimModel *Instance, glm::dvec3 const &Camera, float const Brightness, unsigned int const Framestamp) @@ -726,7 +731,7 @@ std::string gfx::local_lights::info() const { return "local lights: " + std::to_string(m_lights.size()) + " (scenery " + std::to_string(m_scenerycount) + ", vehicles " + std::to_string(m_vehiclecount) + ", compartments " + std::to_string(m_compartmentcount) + ")" + (m_cablampon ? " + cab lights " + std::to_string(m_cablights.size()) : "") + ", placed " + std::to_string(m_placed) + ", over capacity " + std::to_string(m_dropped) + - ", gather " + to_string(m_gathertime, 2) + " ms"; + ", gather " + to_string(m_gathertime, 2) + " ms, moth swarms " + std::to_string(Moths.shown()) + "/" + std::to_string(Moths.count()); } void gfx::local_lights::draw_debug_ui() @@ -740,6 +745,10 @@ void gfx::local_lights::draw_debug_ui() ImGui::SliderFloat("cab lamp brightness", &Global.gfx_lights_cab_intensity, 0.f, 10.f); ImGui::SliderFloat("cab ambient kept", &Global.gfx_lights_cab_bounce, 0.f, 1.f); ImGui::SliderFloat("range, m", &Global.gfx_lights_range, 0.f, 1000.f); + ImGui::Checkbox("moths at the lamps of the scenery", &Global.moths); + ImGui::SliderFloat("moths: part of the lamps", &Global.moths_share, 0.f, 1.f); + ImGui::SliderFloat("moths: range, m", &Global.moths_range, 0.f, 300.f); + ImGui::Text("moth swarms shown %u, made %u", static_cast(Moths.shown()), static_cast(Moths.count())); ImGui::Text("gathered %u: scenery %u, vehicles %u, compartments %u, cab %u", static_cast(m_lights.size()), m_scenerycount, m_vehiclecount, m_compartmentcount, static_cast(m_cablights.size())); ImGui::Text("in the light buffer %u, over capacity %u", m_placed, m_dropped); diff --git a/rendering/common/moths.cpp b/rendering/common/moths.cpp new file mode 100644 index 000000000..16399b6c7 --- /dev/null +++ b/rendering/common/moths.cpp @@ -0,0 +1,140 @@ +/* +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 "rendering/common/moths.h" +#include +#include +#include +#include "utilities/Globals.h" +#include "model/AnimModel.h" +#include "scene/scenenode.h" +#include "scene/scenelayers.h" +#include "simulation/simulation.h" + +namespace +{ + +// the model of a swarm, models/ and the extension left out +char const *const Swarmmodel{"animals/cmy"}; + +// a number 0-1 for the place, the same every time for the same place (whole half metres) +double place_number(glm::dvec3 const &Position) +{ + auto const x{static_cast(std::floor(Position.x * 2.0))}; + auto const y{static_cast(std::floor(Position.y * 2.0))}; + auto const z{static_cast(std::floor(Position.z * 2.0))}; + auto hash{static_cast(x) * 0x9E3779B97F4A7C15ull ^ static_cast(y) * 0xC2B2AE3D27D4EB4Full ^ static_cast(z) * 0x165667B19E3779F9ull}; + hash ^= hash >> 33; + hash *= 0xff51afd7ed558ccdull; + hash ^= hash >> 33; + hash *= 0xc4ceb9fe1a85ec53ull; + hash ^= hash >> 33; + return static_cast(hash >> 11) / static_cast(1ull << 53); +} + +// moths fly in the warm part of the year +bool moths_fly() +{ + return Global.moths && (Global.Season != "winter:") && (Global.AirTemperature >= Global.moths_temperature); +} + +} // namespace + +gfx::moths gfx::Moths; + +void gfx::moths::update(std::vector const &Lights, glm::dvec3 const &Camera, unsigned int const Framestamp) +{ + auto const flying{moths_fly() && (Global.moths_share > 0.f) && (Global.moths_range > 0.f)}; + if (flying) + { + auto const range{static_cast(Global.moths_range)}; + for (auto const &light : Lights) + { + // the lamps of the scenery models, shining downwards (street and platform lamps; not the signals) + if ((light.owner == nullptr) || (light.part == nullptr) || (light.direction.y > -0.5f)) + { + continue; + } + auto const key{std::make_pair(light.owner, light.part)}; + auto lookup{m_lamps.find(key)}; + if ((lookup != m_lamps.end()) && (glm::length(lookup->second.position - light.position) > 0.5)) + { + // another lamp in the place of the one met before (the editor took it away and put a new one) + if (lookup->second.model != nullptr) + { + lookup->second.model->visible(false); + } + lookup = m_lamps.end(); + m_lamps.erase(key); + } + if (lookup == m_lamps.end()) + { + if (glm::length(light.position - Camera) > range) + { + // decided when the lamp comes closer + continue; + } + swarm entry; + entry.position = light.position; + auto chosen{place_number(light.position) < static_cast(Global.moths_share)}; + for (auto const &place : m_places) + { + if (glm::length(place - light.position) < 1.0) + { + // the other light point of the same lamp has a swarm already + chosen = false; + break; + } + } + if (chosen) + { + auto const definition{"node " + std::to_string(static_cast(std::ceil(range))) + " 0 none model 0 0 0 0 " + std::string{Swarmmodel} + ".t3d none endmodel"}; + auto *model{simulation::State.create_model(definition, "none", light.position)}; + if (model != nullptr) + { + // not a part of the scenery: no layer of it, not saved, not edited + scene::Layers.count(model->layer(), scene::layer_item::model, -1); + model->layer(null_handle); + model->m_dirty = false; + model->m_generated = true; + model->visible(false); + ++scene::node_state_version; + entry.model = model; + m_places.emplace_back(light.position); + ++m_count; + } + } + lookup = m_lamps.emplace(key, entry).first; + } + lookup->second.seen = Framestamp; + } + } + // the swarms of the lamps lit in this frame are shown + m_shown = 0; + for (auto &lamp : m_lamps) + { + auto &entry{lamp.second}; + if (entry.model == nullptr) + { + continue; + } + auto const shown{flying && (entry.seen == Framestamp)}; + entry.model->visible(shown); + m_shown += (shown ? 1 : 0); + } +} + +void gfx::moths::reset() +{ + // the instances go with the scenery + m_lamps.clear(); + m_places.clear(); + m_count = m_shown = 0; +} diff --git a/rendering/common/moths.h b/rendering/common/moths.h new file mode 100644 index 000000000..ca2e71643 --- /dev/null +++ b/rendering/common/moths.h @@ -0,0 +1,63 @@ +/* +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/. +*/ + +#pragma once + +#include +#include +#include +#include "rendering/common/locallights.h" + +class TAnimModel; + +// moths flying about the lit lamps of the scenery. a part of the lamps of the scenery models which shine downwards (Global.moths_share, +// picked by the places of the lamps, so always the same ones) gets a swarm: an instance of the model animals/cmy (animated by textures, +// shader vat: the swarms differ in the moment and the clip of the animation by their places) put in the scenery at the light point. +// the swarm is shown while its lamp shines (the light points the local lights gathered in the frame), in the warm part of the year +// (not in the winter, the air not colder than Global.moths_temperature), to Global.moths_range from the camera. the swarms are made +// when their lamps first come within the range, aren't parts of the scenery (not saved, not edited) and go with it +namespace gfx +{ + +class moths +{ + public: + // after the lights of the frame were gathered: makes the swarms of the lamps which came within the range, shows the swarms of the + // lit lamps and hides the others + void update(std::vector const &Lights, glm::dvec3 const &Camera, unsigned int const Framestamp); + // forgets the swarms: the scenery they were put in is about to be replaced + void reset(); + // swarms made, and shown in the last frame + std::size_t count() const + { + return m_count; + } + std::size_t shown() const + { + return m_shown; + } + + private: + struct swarm + { + glm::dvec3 position{0.0}; // of the light point the swarm was decided for + TAnimModel *model{nullptr}; // nullptr: the lamp has no swarm + unsigned int seen{0}; // the frame the lamp shone last in + }; + // the lamps met: the instance and the submodel of the light point + std::map, swarm> m_lamps; + std::vector m_places; // of the swarms made, so two light points of one lamp don't both get one + std::size_t m_count{0}; + std::size_t m_shown{0}; +}; + +// the swarms of the scenery, kept by the local lights (local_lights::gather(), reset()) +extern moths Moths; + +} // namespace gfx diff --git a/scene/scenelayers.cpp b/scene/scenelayers.cpp index 9e075d063..e729fac7b 100644 --- a/scene/scenelayers.cpp +++ b/scene/scenelayers.cpp @@ -393,6 +393,14 @@ bool node_layers::editable(basic_node const *Node, std::string *Reason) const { return false; } + if (Node->m_generated) + { + if (Reason != nullptr) + { + *Reason = STR_C("it's made by the game while it runs (the moths at a lamp), not a part of the scenery"); + } + return false; + } if (Node->m_catenary) { // moved, copied or deleted by itself it would part from the catenary which keeps it (and is rebuilt from its own file) diff --git a/scene/scenenode.cpp b/scene/scenenode.cpp index 426f0e077..c5fe662c8 100644 --- a/scene/scenenode.cpp +++ b/scene/scenenode.cpp @@ -764,6 +764,8 @@ basic_node::deserialize( std::istream &Input ) { void basic_node::export_as_text( std::ostream &Output ) const { + // what the game made while it runs isn't a part of the scenery + if( m_generated ) { return; } Output // header << "node" diff --git a/scene/scenenode.h b/scene/scenenode.h index f10ac0d16..d389c3a9e 100644 --- a/scene/scenenode.h +++ b/scene/scenenode.h @@ -423,6 +423,7 @@ public: bool m_layerhidden { false }; // the node was visible until the editor hid its scenery layer, or the include which defines it bool m_preview { false }; // the node was made by the editor to show an include it placed or changed, and is replaced when the include changes bool m_catenary { false }; // a part the catenary assembled (world/catenary_scene.cpp): it's changed by the catenary tool, not edited as a node + bool m_generated { false }; // made by the game while it runs (the moths at the lamps, rendering/common/moths.cpp): not edited, not saved scene::layer_handle m_layer { null_handle }; // scenery layer this node belongs to, if any // include of an *.inc template which defines the node; 0 if the node is defined directly by a scenery layer file. // such nodes can't be rewritten one by one on scenery save. set only for scenery opened for editing diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 71ab7987c..5c6ea7231 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -2128,6 +2128,10 @@ global_settings::ConfigParse_gfx_sky( cParser &Parser, std::string_view const To else if( Token == "gfx.skyvisibility.tunnels" ) { readswitch( gfx_skyvisibility_tunnels ); } else if( Token == "gfx.tunnelflags" ) { readswitch( gfx_tunnelflags ); } else if( Token == "gfx.lights.range" ) { readvalue( gfx_lights_range, 0.f, 2000.f ); } + else if( Token == "moths" ) { readswitch( moths ); } + else if( Token == "moths.share" ) { readvalue( moths_share, 0.f, 1.f ); } + else if( Token == "moths.range" ) { readvalue( moths_range, 0.f, 500.f ); } + else if( Token == "moths.temperature" ) { readvalue( moths_temperature, -50.f, 50.f ); } else if( Token == "gfx.lights.shadows" ) { // no/low/medium/high, or the number of the level Parser.getTokens( 1 ); @@ -2681,6 +2685,10 @@ global_settings::export_as_text( std::ostream &Output ) const { export_as_text( Output, "gfx.skyvisibility.tunnels", gfx_skyvisibility_tunnels ); export_as_text( Output, "gfx.tunnelflags", gfx_tunnelflags ); export_as_text( Output, "gfx.lights.range", gfx_lights_range ); + export_as_text( Output, "moths", moths ); + export_as_text( Output, "moths.share", moths_share ); + export_as_text( Output, "moths.range", moths_range ); + export_as_text( Output, "moths.temperature", moths_temperature ); export_as_text( Output, "gfx.lights.shadows", std::string{ gfx_lights_shadows == 3 ? "high" : gfx_lights_shadows == 2 ? "medium" : gfx_lights_shadows == 1 ? "low" : "no" } ); export_as_text( Output, "gfx.lights.shadows.cab", gfx_lights_shadows_cab ); export_as_text( Output, "gfx.lights.shadows.lamps", gfx_lights_shadows_lamps ); diff --git a/utilities/Globals.h b/utilities/Globals.h index f292a6b4e..a30744173 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -600,6 +600,12 @@ struct global_settings { // the sound and the driver bool gfx_tunnelflags { false }; float gfx_lights_range { 300.f }; // distance from the camera the lights of models and vehicles are taken from, metres + // moths flying about the lit lamps of the scenery (rendering/common/moths.cpp): a swarm (the model animals/cmy, animated by + // textures) at a part of the lamps shining downwards, picked by their places, in the warm months, within the range of the camera + bool moths { true }; + float moths_share { 0.4f }; // part of the lamps with a swarm, 0-1 + float moths_range { 80.f }; // distance from the camera the swarms are shown at, metres + float moths_temperature { 8.f }; // the lowest temperature of the air moths fly at, degrees Celsius // shadows of the lamps (cube shadow maps), 0 none, 1 low, 2 medium, 3 high; see opengl46_local_shadows. for now the lamp of the // occupied cab, its shadows cast by the cab int gfx_lights_shadows { 2 };