diff --git a/CMakeLists.txt b/CMakeLists.txt index 1358ac24c..d574465a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,6 +136,7 @@ set(SOURCES "world/TractionPower.cpp" "vehicle/Train.cpp" "world/TrkFoll.cpp" +"world/deer.cpp" "vehicle/AirCoupler.cpp" "model/AnimModel.cpp" "vehicle/Button.cpp" diff --git a/application/drivermode.cpp b/application/drivermode.cpp index 71a343d6b..b9afe24fb 100644 --- a/application/drivermode.cpp +++ b/application/drivermode.cpp @@ -26,6 +26,7 @@ http://mozilla.org/MPL/2.0/. #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" @@ -318,6 +319,7 @@ bool driver_mode::update() Timer::subsystem.sim_lights.start(); simulation::Lights.update(); Timer::subsystem.sim_lights.stop(); + simulation::Deer.update(deltatime); if (Global.test.enabled && simulation::is_ready) { diff --git a/model/AnimModel.cpp b/model/AnimModel.cpp index b63f5c22d..7c1d9f572 100644 --- a/model/AnimModel.cpp +++ b/model/AnimModel.cpp @@ -553,6 +553,15 @@ void TAnimModel::update_instanceable_flag() { ++s_instanceable_total; } +void TAnimModel::draw_alone() { + + if( m_instanceable ) { + ++s_instanceable_revoked; + m_instanceable = false; + ++scene::node_state_version; + } +} + std::shared_ptr TAnimModel::AddContainer(std::string const &Name) { // dodanie sterowania submodelem dla egzemplarza if (!pModel) diff --git a/model/AnimModel.h b/model/AnimModel.h index f1a204303..ad512a770 100644 --- a/model/AnimModel.h +++ b/model/AnimModel.h @@ -123,6 +123,9 @@ public: // Angles, Scale: content of the optional blocks of the definition, nullptr for a block which isn't there void Load( std::string const &Name, std::string const &Texture, TAnimModel const *Twin, glm::vec3 const *Angles, glm::vec3 const *Scale, bool const Transition ); std::shared_ptr AddContainer(std::string const &Name); + // the instance drawn on its own from now on, not with the others of its model: one the game moves (the deer at the tracks), + // whose place the renderer takes anew in every frame + void draw_alone(); std::shared_ptr GetContainer(std::string const &Name = ""); void LightSet( int const n, float const v ); void SkinSet( int const Index, material_handle const Material ); diff --git a/rendering/common/scenewalkindirect.cpp b/rendering/common/scenewalkindirect.cpp index 1406e1815..7f1a1e9a3 100644 --- a/rendering/common/scenewalkindirect.cpp +++ b/rendering/common/scenewalkindirect.cpp @@ -282,10 +282,10 @@ bool scene_walk::Queue_Model(TAnimModel *Instance, bool const Inrange) auto &records{m_modelrecords[Instance]}; auto &bucket{records.bucket}; - if ((bucket.recordsversion != m_instancerecordsversion) || (bucket.records.size() != 1)) + if ((bucket.recordsversion != m_instancerecordsversion) || (bucket.records.size() != 1) || (records.area.center != Instance->location())) { // the key, the instance and the record anew: the editor may have given the instance another model or skins, or - // another instance may have taken the place of a deleted one + // another instance may have taken the place of a deleted one, or the game moved it (world/deer.cpp) auto &key{records.key}; key = {}; key.pModel = Instance->Model(); diff --git a/shaders/ogl46/mat_vat.vert b/shaders/ogl46/mat_vat.vert index a06143220..aacf58bdd 100644 --- a/shaders/ogl46/mat_vat.vert +++ b/shaders/ogl46/mat_vat.vert @@ -19,6 +19,9 @@ // right after another (vertex Frame * Vertices + Vertex of the whole image, row by row), else each in its own rows // the textures go with the trait r (texture_vatpos_hi: name:r), which keeps them as they are (never scaled down nor compressed) // param vat_speed (1 if not given): how fast the animation plays +// param vat_random (1 if not given): 1 every model its own moment from its place; 0 the moment of the clock alone, for a model the game +// moves (its place changes the whole time, the moment taken from it would jump); -1 the game picks the clip and the moment, by the +// fraction of ten times the turn of the model about the vertical in degrees: (clip + 0.25 + 0.5 * moment) / clips (world/deer.cpp) layout(location = 0) in vec3 v_vert; layout(location = 1) in vec3 v_normal; layout(location = 2) in vec2 v_coord; @@ -31,6 +34,7 @@ layout(location = 4) in vec4 v_userdata; #pragma texture(vatpos_lo, 2, RGBA) #pragma texture(vatnormal, 3, RGBA) #pragma param(vat_speed, 2, 0, 1, one) +#pragma param(vat_random, 2, 1, 1, one) layout(binding = 1) uniform sampler2D vatpos_hi; layout(binding = 2) uniform sampler2D vatpos_lo; layout(binding = 3) uniform sampler2D vatnormal; @@ -143,8 +147,18 @@ void vat_animate(mat4 mv, out vec3 position, out vec3 normal) int bin = int(mod(floor(yaw * 0.5 + 0.315), 180.0)); uint seed = uint(cell.x) * 73856093u ^ uint(cell.y) * 19349663u ^ uint(cell.z) * 83492791u ^ uint(bin) * 2654435761u; seed ^= seed >> 16; seed *= 0x7feb352du; seed ^= seed >> 15; seed *= 0x846ca68bu; seed ^= seed >> 16; - float phase = float(seed & 0xffffu) / 65536.0; - float pick = float(seed >> 16) / 65536.0; + float random = clamp(param[2].y, 0.0, 1.0); + float phase = random * float(seed & 0xffffu) / 65536.0; + float pick = random * float(seed >> 16) / 65536.0; + if (param[2].y < -0.5) + { + // picked by the game: the fraction of the turn in tenths of a degree (the turn is -yaw), a quarter of a clip's span kept + // clear of its edges + float spin = fract(-yaw * 10.0) * float(h.clips); + float slot = floor(spin); + pick = (slot + 0.5) / float(h.clips); + phase = clamp((spin - slot - 0.25) * 2.0, 0.0, 1.0); + } int clipframes = max(h.frames / h.clips, 1); int clip = min(int(pick * float(h.clips)), h.clips - 1); float t = (time * h.fps * max(param[2].x, 0.0)) + phase * float(clipframes); diff --git a/shaders/vk14/mat_vat.vert b/shaders/vk14/mat_vat.vert index d830b93c1..e3e2fd662 100644 --- a/shaders/vk14/mat_vat.vert +++ b/shaders/vk14/mat_vat.vert @@ -21,6 +21,9 @@ // right after another (vertex Frame * Vertices + Vertex of the whole image, row by row), else each in its own rows // the textures go with the trait r (texture_vatpos_hi: name:r), which keeps them as they are (never scaled down nor compressed) // param vat_speed (1 if not given): how fast the animation plays +// param vat_random (1 if not given): 1 every model its own moment from its place; 0 the moment of the clock alone, for a model the game +// moves (its place changes the whole time, the moment taken from it would jump); -1 the game picks the clip and the moment, by the +// fraction of ten times the turn of the model about the vertical in degrees: (clip + 0.25 + 0.5 * moment) / clips (world/deer.cpp) layout(location = 0) in vec3 v_vert; layout(location = 1) in vec3 v_normal; layout(location = 2) in vec2 v_coord; @@ -31,6 +34,7 @@ layout(location = 4) in vec4 v_userdata; #pragma texture(vatpos_lo, 2, RGBA) #pragma texture(vatnormal, 3, RGBA) #pragma param(vat_speed, 2, 0, 1, one) +#pragma param(vat_random, 2, 1, 1, one) layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 1) uniform sampler2D vatpos_hi; layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 2) uniform sampler2D vatpos_lo; layout(set = MATERIAL_SET, binding = MATERIAL_TEXTURE_FIRST + 3) uniform sampler2D vatnormal; @@ -143,8 +147,18 @@ void vat_animate(mat4 mv, out vec3 position, out vec3 normal) int bin = int(mod(floor(yaw * 0.5 + 0.315), 180.0)); uint seed = uint(cell.x) * 73856093u ^ uint(cell.y) * 19349663u ^ uint(cell.z) * 83492791u ^ uint(bin) * 2654435761u; seed ^= seed >> 16; seed *= 0x7feb352du; seed ^= seed >> 15; seed *= 0x846ca68bu; seed ^= seed >> 16; - float phase = float(seed & 0xffffu) / 65536.0; - float pick = float(seed >> 16) / 65536.0; + float random = clamp(param[2].y, 0.0, 1.0); + float phase = random * float(seed & 0xffffu) / 65536.0; + float pick = random * float(seed >> 16) / 65536.0; + if (param[2].y < -0.5) + { + // picked by the game: the fraction of the turn in tenths of a degree (the turn is -yaw), a quarter of a clip's span kept + // clear of its edges + float spin = fract(-yaw * 10.0) * float(h.clips); + float slot = floor(spin); + pick = (slot + 0.5) / float(h.clips); + phase = clamp((spin - slot - 0.25) * 2.0, 0.0, 1.0); + } int clipframes = max(h.frames / h.clips, 1); int clip = min(int(pick * float(h.clips)), h.clips - 1); float t = (time * h.fps * max(param[2].x, 0.0)) + phase * float(clipframes); diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 83db0862d..96b13f9a3 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -2035,6 +2035,12 @@ global_settings::ConfigParse_gfx_sky( cParser &Parser, std::string_view const To 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 == "deer" ) { readswitch( deer ); } + else if( Token == "deer.interval" ) { readvalue( deer_interval, 0.0f, 100000.0f ); } + else if( Token == "deer.distance" ) { readvalue( deer_distance, 100.0f, 10000.0f ); } + else if( Token == "deer.chance" ) { readvalue( deer_chance, 0.0f, 1.0f ); } + else if( Token == "deer.hornrange" ) { readvalue( deer_hornrange, 0.0f, 5000.0f ); } + else if( Token == "deer.fleedistance" ) { readvalue( deer_fleedistance, 0.0f, 1000.0f ); } else if( Token == "gfx.lights.shadows" ) { // no/low/medium/high, or the number of the level Parser.getTokens( 1 ); @@ -2545,6 +2551,12 @@ global_settings::export_as_text( std::ostream &Output ) const { 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, "deer", deer ); + export_as_text( Output, "deer.interval", deer_interval ); + export_as_text( Output, "deer.distance", deer_distance ); + export_as_text( Output, "deer.chance", deer_chance ); + export_as_text( Output, "deer.hornrange", deer_hornrange ); + export_as_text( Output, "deer.fleedistance", deer_fleedistance ); 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 9f187a00b..2ac04a510 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -559,6 +559,14 @@ struct global_settings { 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 + // deer at the tracks (world/deer.cpp): put on the path ahead of the train of the player, grazing, they run away from the horn + // or the train coming close. off unless turned on + bool deer { false }; + float deer_interval { 2000.0f }; // metres the train covers between the deer, 0 none + float deer_distance { 2000.0f }; // metres ahead of the train a deer is put at + float deer_chance { 1.0f }; // of a deer at each interval, 0-1 + float deer_hornrange { 600.0f }; // the horn sends away the deer within this many metres + float deer_fleedistance { 150.0f }; // a deer runs away from the train come this close, metres // 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 }; diff --git a/world/deer.cpp b/world/deer.cpp new file mode 100644 index 000000000..e6c3749be --- /dev/null +++ b/world/deer.cpp @@ -0,0 +1,502 @@ +/* +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 "world/deer.h" +#include +#include +#include +#include +#include +#include +#include "utilities/Globals.h" +#include "utilities/utilities.h" +#include "model/AnimModel.h" +#include "scene/scene.h" +#include "scene/scenenode.h" +#include "scene/scenelayers.h" +#include "simulation/simulation.h" +#include "rendering/renderer.h" +#include "vehicle/DynObj.h" +#include "McZapkie/MOVER.h" +#include "world/Track.h" +#include "utilities/Timer.h" + +namespace +{ + +// the animations of the deer (textures of the vertices, shader vat with vat_random -1: the game picks the clip and the moment of every +// instance, see vat_turn()). made by jelen_anim2.py; the numbers below are the ones it bakes with +struct vat_clip +{ + std::string_view model; // models/ and the extension left out + double fps; // frames a second + double frames; // of one clip + int clips; +}; +// grazing, 12 s: the head at the grass, raised about 4.5-9.3 s +constexpr vat_clip Grazing{"animals/jelen_je", 20.0, 240.0, 1}; +// startled: from the moments k * Window of the grazing (clip k) to the alarm, in Startletime; two more frames hold the end +constexpr vat_clip Startle{"animals/jelen_zryw", 30.0, 15.0, 16}; +// the flight from the alarm: looks at the train (clip 0 to the left, 1 to the right), bounds away and stops in the grazing pose of the +// moment Grazeafter, in Flighttime; two more frames hold the end +constexpr vat_clip Flight{"animals/jelen_skok", 30.0, 168.0, 2}; +constexpr double Window{0.75}; +constexpr double Startletime{0.4}; +constexpr double Flighttime{5.5}; +constexpr double Grazeafter{5.6}; +// the way it covers in the flight: the speed rises from 0 to Flightspeed in Flight0-Flight1 and falls back in Flight2-Flight3 +// (smoothstep), as the animation moves its hooves +constexpr double Flightspeed{7.0}; +constexpr double Flight0{0.80}; +constexpr double Flight1{1.20}; +constexpr double Flight2{3.95}; +constexpr double Flight3{4.85}; +// the frame an instance starts from at the moment of the change, a little after the frame proper: never the end of the one before +constexpr double Startframe{0.02}; +// the ground under the way of the deer is looked for up to this much above the place it starts from, metres +constexpr double Groundceiling{3.0}; +// the train past the deer: it came this close, and is this much farther away now, metres +constexpr double Passedclose{300.0}; +constexpr double Passedmargin{200.0}; + +double fraction(double const Value) +{ + return Value - std::floor(Value); +} + +// the integral of smoothstep((T - A) / (B - A)) from minus infinity to T +double ramp_integral(double const T, double const A, double const B) +{ + if (T <= A) + { + return 0.0; + } + if (T >= B) + { + return (B - A) * 0.5 + (T - B); + } + auto const x{(T - A) / (B - A)}; + return (B - A) * (x * x * x - 0.5 * x * x * x * x); +} + +// metres covered in the flight Time seconds after its start +double flight_way(double const Time) +{ + return Flightspeed * (ramp_integral(Time, Flight0, Flight1) - ramp_integral(Time, Flight2, Flight3)); +} + +// the height of the triangle at (X, Z), false when the point isn't in it +bool triangle_height(glm::dvec3 const &A, glm::dvec3 const &B, glm::dvec3 const &C, double const X, double const Z, double &Height) +{ + auto const d{(B.z - C.z) * (A.x - C.x) + (C.x - B.x) * (A.z - C.z)}; + if (std::abs(d) < 1e-9) + { + return false; + } + auto const a{((B.z - C.z) * (X - C.x) + (C.x - B.x) * (Z - C.z)) / d}; + auto const b{((C.z - A.z) * (X - C.x) + (A.x - C.x) * (Z - C.z)) / d}; + auto const c{1.0 - a - b}; + if ((a < 0.0) || (b < 0.0) || (c < 0.0)) + { + return false; + } + Height = a * A.y + b * B.y + c * C.y; + return true; +} + +// what's looked for under a point: its place, the highest ground found so far +struct ground_probe +{ + glm::dvec3 point{0.0}; + double ceiling{0.0}; + bool found{false}; + double height{0.0}; + + void triangle(glm::dvec3 const &A, glm::dvec3 const &B, glm::dvec3 const &C) + { + if (double y{0.0}; triangle_height(A, B, C, point.x, point.z, y) && (y <= ceiling) && ((false == found) || (y > height))) + { + found = true; + height = y; + } + } + + void geometry(gfx::vertex_array const &Vertices, gfx::index_array const &Indices, glm::dvec3 const &Origin) + { + auto const vertex{[&Vertices, &Origin](std::size_t const Index) { return Origin + glm::dvec3{Vertices[Index].position}; }}; + if (Indices.empty()) + { + for (std::size_t index = 0; index + 2 < Vertices.size(); index += 3) + { + triangle(vertex(index), vertex(index + 1), vertex(index + 2)); + } + return; + } + for (std::size_t index = 0; index + 2 < Indices.size(); index += 3) + { + triangle(vertex(Indices[index]), vertex(Indices[index + 1]), vertex(Indices[index + 2])); + } + } + + void shape(scene::shape_node const &Shape) + { + auto const &data{Shape.data()}; + if ((data.area.radius >= 0.f) && (glm::length(glm::dvec2{data.area.center.x - point.x, data.area.center.z - point.z}) > data.area.radius)) + { + return; + } + if (false == data.vertices.empty()) + { + for (std::size_t index = 0; index + 2 < data.vertices.size(); index += 3) + { + triangle(data.vertices[index].position, data.vertices[index + 1].position, data.vertices[index + 2].position); + } + return; + } + // uploaded, the renderer keeps the geometry relative to the origin of the shape + if ((GfxRenderer == nullptr) || ((data.geometry.bank == 0) && (data.geometry.chunk == 0))) + { + return; + } + geometry(GfxRenderer->Vertices(data.geometry), GfxRenderer->Indices(data.geometry), data.origin); + } +}; + +// the height of the ground of the scenery (its shapes: the terrain) under the point, not higher than Ceiling. false where there's +// none: the ground of terrain models, or none loaded +bool ground_height(glm::dvec3 const &Point, double const Ceiling, double &Height) +{ + if (simulation::Region == nullptr) + { + return false; + } + ground_probe probe; + probe.point = Point; + probe.ceiling = Ceiling; + for (auto *section : simulation::Region->sections(Point, 1.f)) + { + for (auto const &shape : section->m_shapes) + { + probe.shape(shape); + } + for (auto const &cell : section->m_cells) + { + if (glm::length(glm::dvec2{cell.m_area.center.x - Point.x, cell.m_area.center.z - Point.z}) > cell.m_area.radius) + { + continue; + } + for (auto const &shape : cell.m_shapesopaque) + { + probe.shape(shape); + } + } + } + Height = probe.height; + return probe.found; +} + +// a place on a path: the point of its axis and the way along it, level, of length 1 +struct path_place +{ + glm::dvec3 position{0.0}; + glm::dvec3 forward{0.0, 0.0, 1.0}; +}; + +// the place Distance from Track at Offset along it, going the way of Direction (1 towards its end, -1 towards its start), through the +// connected paths. false where the paths end earlier (or reach a crossing of roads) +bool path_ahead(TTrack *Track, double Offset, double Direction, double Distance, path_place &Place) +{ + auto *track{Track}; + auto left{Distance}; + // as far as a train could go; a loop of paths ends it as well + for (int step = 0; (step < 10000) && (track != nullptr); ++step) + { + auto const length{track->Length()}; + auto const room{Direction > 0.0 ? length - Offset : Offset}; + if (left > room) + { + left -= room; + track = track->Connected(Direction > 0.0 ? 1 : -1, Direction); + if ((track == nullptr) || (track->eType == tt_Cross)) + { + return false; + } + Offset = (Direction > 0.0 ? 0.0 : track->Length()); + continue; + } + auto const segment{track->CurrentSegment()}; + if (segment == nullptr) + { + return false; + } + auto const at{std::clamp(Offset + Direction * left, 0.0, length)}; + glm::vec3 rotation; + segment->RaPositionGet(at, Place.position, rotation); + // the way of the path from two points about the place + glm::dvec3 behind; + glm::dvec3 ahead; + segment->RaPositionGet(std::clamp(at - 0.5, 0.0, length), behind, rotation); + segment->RaPositionGet(std::clamp(at + 0.5, 0.0, length), ahead, rotation); + auto way{(ahead - behind) * Direction}; + way.y = 0.0; + if (glm::length(way) < 1e-6) + { + return false; + } + Place.forward = glm::normalize(way); + // on the sleepers, below the heads of the rails + Place.position.y -= track->rail_height(); + return true; + } + return false; +} + +// the yaw of an instance whose model looks along -z, to look along Heading, degrees (-180, 180] +double model_yaw(glm::dvec3 const &Heading) +{ + return std::atan2(-Heading.x, -Heading.z) * 180.0 / std::numbers::pi; +} + +// the turn about the vertical (degrees) for an instance looking along Heading, which plays clip Clip of Animation so that it's at +// frame Frame at the time Time. the whole tenths of a degree come from the heading, the fraction of the tenth tells the shader the +// clip and the moment +float vat_turn(glm::dvec3 const &Heading, vat_clip const &Animation, int const Clip, double const Frame, double const Time) +{ + auto const phase{fraction((Frame - Time * Animation.fps) / Animation.frames)}; + auto const spin{(static_cast(Clip) + 0.25 + 0.5 * phase) / static_cast(Animation.clips)}; + return static_cast((std::floor(model_yaw(Heading) * 10.0) + spin) / 10.0); +} + +// a deer put in the scenery, not a part of it. Moving: the game moves it, so it's drawn on its own +TAnimModel *make_deer(vat_clip const &Animation, glm::dvec3 const &Position, float const Turn, bool const Moving) +{ + auto const range{static_cast(std::ceil(Global.deer_distance + 500.0))}; + auto const definition{std::format("node {} 0 none model 0 0 0 0 {}.t3d none endmodel", range, Animation.model)}; + auto *model{simulation::State.create_model(definition, "none", Position)}; + if (model == nullptr) + { + return 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; + simulation::Region->erase(model); + model->Angles(glm::vec3{0.f, Turn, 0.f}); + if (Moving) + { + // out of the groups of the instances of its model, which keep their places + model->draw_alone(); + } + simulation::Region->insert(model); + ++scene::node_state_version; + return model; +} + +// the deer moved to another place +void move_deer(TAnimModel &Model, glm::dvec3 const &Position) +{ + if (glm::length(Model.location() - Position) < 1e-4) + { + return; + } + simulation::Region->erase(&Model); + Model.location(Position); + simulation::Region->insert(&Model); +} + +void remove_deer(TAnimModel *Model) +{ + if (Model != nullptr) + { + simulation::State.delete_model(Model); + ++scene::node_state_version; + } +} + +// the deer goes on with another animation, at frame Frame of clip Clip at the time Time; where the model can't be made, as it was +void change_deer(TAnimModel *&Model, glm::dvec3 const &Heading, vat_clip const &Animation, int const Clip, double const Frame, double const Time, bool const Moving) +{ + auto *model{make_deer(Animation, Model->location(), vat_turn(Heading, Animation, Clip, Frame, Time), Moving)}; + if (model == nullptr) + { + return; + } + remove_deer(Model); + Model = model; +} + +} // namespace + +simulation::deer_herd simulation::Deer; + +void simulation::deer_herd::update(double const Deltatime) +{ + if (m_region != simulation::Region) + { + // another scenery: the deer went with the one they were put in + m_deer.clear(); + m_travelled = 0.0; + m_region = simulation::Region; + } + auto const *vehicle{simulation::Train != nullptr ? simulation::Train->Dynamic() : nullptr}; + if ((false == Global.deer) || (vehicle == nullptr) || (simulation::Region == nullptr)) + { + for (auto const &animal : m_deer) + { + remove_deer(animal.model); + } + m_deer.clear(); + m_travelled = 0.0; + return; + } + auto const *mover{vehicle->MoverParameters}; + m_travelled += std::abs(mover->V) * Deltatime; + if ((Global.deer_interval > 0.0) && (m_travelled >= Global.deer_interval)) + { + m_travelled = 0.0; + if (Random(0.0, 1.0) < Global.deer_chance) + { + spawn(*vehicle); + } + } + auto const train{vehicle->GetPosition()}; + auto const horn{(mover->WarningSignal & 7) != 0}; + auto const now{Timer::GetTime()}; + std::erase_if(m_deer, [this, &train, horn, now](deer &Animal) { return tend(Animal, train, horn, now); }); +} + +bool simulation::deer_herd::tend(deer &Animal, glm::dvec3 const &Train, bool const Horn, double const Now) const +{ + auto const distance{glm::length(Animal.model->location() - Train)}; + Animal.closest = std::min(Animal.closest, distance); + if (((Animal.closest < Passedclose) && (distance > Animal.closest + Passedmargin)) || (distance > Global.deer_distance + 1500.0)) + { + // the train is past it, or went another way + remove_deer(Animal.model); + return true; + } + switch (Animal.state) + { + case deer_state::grazing: + if ((Horn && (distance < Global.deer_hornrange)) || (distance < Global.deer_fleedistance)) + { + alarm(Animal, Train, Now); + } + break; + case deer_state::alarmed: + if (Now >= Animal.time) + { + change_deer(Animal.model, Animal.heading, Startle, Animal.clip, Startframe, Animal.time, false); + Animal.state = deer_state::startled; + } + break; + case deer_state::startled: + if (Now >= Animal.time + Startletime) + { + Animal.time += Startletime; + change_deer(Animal.model, Animal.heading, Flight, Animal.look, Startframe, Animal.time, true); + flee(Animal); + } + break; + case deer_state::fleeing: + if (run(Animal, Now)) + { + // grazing again, where it stopped, from the moment of the grazing the flight ends in + Animal.frame = Grazeafter * Grazing.fps + Startframe; + Animal.time += Flighttime; + change_deer(Animal.model, Animal.heading, Grazing, 0, Animal.frame, Animal.time, false); + Animal.state = deer_state::resting; + } + break; + default: + break; + } + return false; +} + +void simulation::deer_herd::spawn(TDynamicObject const &Vehicle) +{ + // the end of the train the way it goes, or the cab occupied when it stands + auto const *mover{Vehicle.MoverParameters}; + auto front{mover->V > 0.1}; + if (std::abs(mover->V) <= 0.1) + { + front = (mover->CabOccupied >= 0); + } + auto const &axle{front ? Vehicle.Axle0 : Vehicle.Axle1}; + auto const direction{front ? axle.GetDirection() : -axle.GetDirection()}; + path_place place; + if (false == path_ahead(axle.GetTrack(), axle.GetTranslation(), direction, Global.deer_distance, place)) + { + return; + } + // across the path, to one side or the other, a little askew + auto const side{Random(0.0, 1.0) < 0.5 ? -1.0 : 1.0}; + auto const askew{Random(-25.0, 25.0) * std::numbers::pi / 180.0}; + glm::dvec3 const across{place.forward.z * side, 0.0, -place.forward.x * side}; + deer animal; + animal.heading = glm::normalize(across * std::cos(askew) + place.forward * std::sin(askew)); + animal.start = place.position; + // grazing from a moment of its own + animal.frame = Random(0.0, Grazing.frames); + animal.time = Timer::GetTime(); + animal.model = make_deer(Grazing, place.position, vat_turn(animal.heading, Grazing, 0, animal.frame, animal.time), false); + if (animal.model == nullptr) + { + return; + } + m_deer.emplace_back(animal); +} + +void simulation::deer_herd::alarm(deer &Animal, glm::dvec3 const &Train, double const Now) const +{ + // it starts from the moment of the grazing nearest ahead whose start it has (every Window seconds) + auto const grazed{fraction((Animal.frame + (Now - Animal.time) * Grazing.fps) / Grazing.frames) * Grazing.frames / Grazing.fps}; + auto const length{Grazing.frames / Grazing.fps}; + auto const windows{static_cast(std::lround(length / Window))}; + auto window{static_cast(std::ceil(grazed / Window - 1e-6))}; + auto wait{static_cast(window) * Window - grazed}; + window %= windows; + Animal.clip = window; + Animal.time = Now + std::max(0.0, wait); + // it looks at the train: to its left or to its right (the left of a heading h is (h.z, 0, -h.x)) + auto const towards{Train - Animal.model->location()}; + Animal.look = ((towards.x * Animal.heading.z - towards.z * Animal.heading.x) > 0.0 ? 0 : 1); + Animal.state = deer_state::alarmed; +} + +void simulation::deer_herd::flee(deer &Animal) const +{ + Animal.state = deer_state::fleeing; + Animal.start = Animal.model->location(); + // the ground along the way it'll run, where there's none found the height of the step before + Animal.ground.front() = Animal.start.y; + auto const length{flight_way(Flighttime)}; + for (std::size_t sample = 1; sample < Groundsamples; ++sample) + { + auto const point{Animal.start + Animal.heading * (length * static_cast(sample) / static_cast(Groundsamples - 1))}; + double height{0.0}; + Animal.ground[sample] = (ground_height(point, Animal.start.y + Groundceiling, height) ? height : Animal.ground[sample - 1]); + } +} + +bool simulation::deer_herd::run(deer &Animal, double const Now) const +{ + auto const elapsed{std::clamp(Now - Animal.time, 0.0, Flighttime)}; + auto const way{flight_way(elapsed)}; + auto const step{way / flight_way(Flighttime) * static_cast(Groundsamples - 1)}; + auto const sample{std::min(static_cast(step), Groundsamples - 2)}; + auto position{Animal.start + Animal.heading * way}; + position.y = std::lerp(Animal.ground[sample], Animal.ground[sample + 1], step - static_cast(sample)); + move_deer(*Animal.model, position); + return (Now - Animal.time >= Flighttime); +} diff --git a/world/deer.h b/world/deer.h new file mode 100644 index 000000000..308fc01b1 --- /dev/null +++ b/world/deer.h @@ -0,0 +1,86 @@ +/* +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 + +class TAnimModel; +class TDynamicObject; +namespace scene +{ +class basic_region; +} + +// deer at the tracks (Global.deer, off unless eu07.ini turns it on): every Global.deer_interval metres the train of the player +// covers, a deer is put (with the chance Global.deer_chance) on the path the train goes along, Global.deer_distance metres ahead +// of it, grazing (the model animals/jelen_je, animated by textures). the horn of the train within Global.deer_hornrange of it, or +// the train coming closer than Global.deer_fleedistance, frightens it: it raises its head (animals/jelen_zryw), looks at the train +// and runs away from the tracks in bounds (animals/jelen_skok, moved by the game along with the hooves), and grazes again where it +// stopped. the game picks the clip and the moment of each of the models (shader vat, vat_random -1), so one hands over to the next +// in the very same pose. the deer go once the train is past them. they aren't parts of the scenery (not saved, not edited) +namespace simulation +{ + +class deer_herd +{ + public: + // after the trains moved, Deltatime: the time of the simulation the frame took, seconds + void update(double const Deltatime); + // deer about at the moment + std::size_t count() const + { + return m_deer.size(); + } + + private: + // the ground under the way the deer runs, at equal steps from its start to its end + static constexpr std::size_t Groundsamples{9}; + enum class deer_state + { + grazing, // till it hears the horn or sees the train close + alarmed, // grazing still, till the moment of the grazing its startle starts from (time) + startled, // raises its head (since time) + fleeing, // looks at the train and runs away (since time) + resting // grazes where it stopped, for good + }; + struct deer + { + TAnimModel *model{nullptr}; + glm::dvec3 start{0.0}; // where it stood when it started to run, or stands + glm::dvec3 heading{0.0, 0.0, 1.0}; // where its head points, level, of length 1 + deer_state state{deer_state::grazing}; + double frame{0.0}; // of the grazing at the time time + double time{0.0}; // of the simulation (Timer::GetTime()), seconds; see deer_state + int clip{0}; // of the startle: the moment of the grazing it starts from + int look{0}; // 0 looks at the train to its left, 1 to its right + double closest{1e9}; // the train came this close, metres + std::array ground{}; // heights of the ground along the way it runs + }; + // the train at Train, its horn sounding, at the time Now: the deer takes fright, runs, or goes (true) once the train is past it + bool tend(deer &Animal, glm::dvec3 const &Train, bool const Horn, double const Now) const; + // puts a deer on the path ahead of the train + void spawn(TDynamicObject const &Vehicle); + // the deer took fright: when it starts, where it looks + void alarm(deer &Animal, glm::dvec3 const &Train, double const Now) const; + // the deer starts to run + void flee(deer &Animal) const; + // moves a running deer, true when it stopped + bool run(deer &Animal, double const Now) const; + + std::vector m_deer; + double m_travelled{0.0}; // by the train since the last deer, metres + scene::basic_region const *m_region{nullptr}; // of the scenery the deer were put in +}; + +extern deer_herd Deer; + +} // namespace simulation