18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 10:46:45 +01:00
Files
maszyna/world/deer.h
T
lcddisplay fb6fcc8029 Deer only where there's flat ground to bound away over
A deer is put beside the track only where the way it runs away is all over the
terrain of the scenery: ground found under each of the 9 samples of its way,
none of them water (a material named water/woda/wody), no farther than 1.2 m
below or 0.8 m above the sleepers, rising or falling at most 0.45 m between
samples and 1.2 m all along. So no deer on an embankment, in a cutting or by a
river. The side it runs to is picked at random first, then the other one; with
neither flat, the next deer is looked for 200 m on instead of a whole interval.
The ground is sampled when the deer is put there, so the run uses the same
heights. Translucent shapes count as ground too, for water drawn translucent.

Terrain made of models (e3d tiles) gives no ground, so there are no deer there.
No asset changes needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GXgG7F4L42c2F9sDSAJbT2
2026-11-24 12:10:48 +01:00

87 lines
3.5 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/.
*/
#pragma once
#include <array>
#include <vector>
#include <glm/glm.hpp>
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<double, Groundsamples> 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, where there's flat ground beside it; false where there's none
bool 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<deer> 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