1 #ifndef BLOBS_CREATURE_CREATURE_HPP_
2 #define BLOBS_CREATURE_CREATURE_HPP_
4 #include "Composition.hpp"
7 #include "Situation.hpp"
8 #include "Steering.hpp"
9 #include "../graphics/SimpleVAO.hpp"
10 #include "../math/geometry.hpp"
11 #include "../math/glm.hpp"
38 using Callback = std::function<void(Creature &)>;
41 // [0,1], zero being good, one bad
43 // static gain per second
45 // adjust value by delta
46 void Add(double delta) noexcept {
47 value = glm::clamp(value + delta, 0.0, 1.0);
49 bool Empty() const noexcept { return value < 0.000001; }
50 bool Good() const noexcept { return value < 0.25; }
51 bool Okay() const noexcept { return value < 0.5; }
52 bool Bad() const noexcept { return !Okay(); }
53 bool Critical() const noexcept { return value > 0.75; }
54 bool Full() const noexcept { return value > 0.999999; }
58 Stat &Damage() noexcept { return stat[0]; }
59 const Stat &Damage() const noexcept { return stat[0]; }
60 Stat &Breath() noexcept { return stat[1]; }
61 const Stat &Breath() const noexcept { return stat[1]; }
62 Stat &Thirst() noexcept { return stat[2]; }
63 const Stat &Thirst() const noexcept { return stat[2]; }
64 Stat &Hunger() noexcept { return stat[3]; }
65 const Stat &Hunger() const noexcept { return stat[3]; }
66 Stat &Exhaustion() noexcept { return stat[4]; }
67 const Stat &Exhaustion() const noexcept { return stat[4]; }
68 Stat &Fatigue() noexcept { return stat[5]; }
69 const Stat &Fatigue() const noexcept { return stat[5]; }
70 Stat &Boredom() noexcept { return stat[6]; }
71 const Stat &Boredom() const noexcept { return stat[6]; }
75 explicit Creature(world::Simulation &);
78 Creature(const Creature &) = delete;
79 Creature &operator =(const Creature &) = delete;
81 Creature(Creature &&) = delete;
82 Creature &operator =(Creature &&) = delete;
85 world::Simulation &GetSimulation() noexcept { return sim; }
86 const world::Simulation &GetSimulation() const noexcept { return sim; }
88 void Name(const std::string &n) noexcept { name = n; }
89 const std::string &Name() const noexcept { return name; }
91 Genome &GetGenome() noexcept { return genome; }
92 const Genome &GetGenome() const noexcept { return genome; }
94 Genome::Properties<double> &GetProperties() noexcept { return properties; }
95 const Genome::Properties<double> &GetProperties() const noexcept { return properties; }
97 void AddMass(int res, double amount);
98 const Composition &GetComposition() const noexcept { return composition; }
100 void BaseColor(const glm::dvec3 &c) noexcept { base_color = c; }
101 const glm::dvec3 &BaseColor() const noexcept { return base_color; }
103 void HighlightColor(const glm::dvec3 &c) noexcept;
104 glm::dvec4 HighlightColor() const noexcept { return highlight_color; }
106 void Mass(double m) noexcept { mass = m; }
107 double Mass() const noexcept { return mass; }
108 void Ingest(int res, double amount) noexcept;
110 void DoWork(double amount) noexcept;
112 void Size(double s) noexcept { size = s; }
113 double Size() const noexcept { return size; }
115 double Born() const noexcept { return birth; }
116 double Age() const noexcept;
117 /// age-depended multiplier, peak being the maximum in lifetime [0,1]
118 double AgeFactor(double peak) const noexcept;
120 double EnergyEfficiency() const noexcept;
121 double ExhaustionFactor() const noexcept;
122 double FatigueFactor() const noexcept;
124 // stats with effects applied
125 double Strength() const noexcept;
126 double StrengthFactor() const noexcept;
127 double Stamina() const noexcept;
128 double StaminaFactor() const noexcept;
129 double Dexerty() const noexcept;
130 double DexertyFactor() const noexcept;
131 double Intelligence() const noexcept;
132 double IntelligenceFactor() const noexcept;
133 double Lifetime() const noexcept;
134 double Fertility() const noexcept;
135 double Mutability() const noexcept;
136 double Adaptability() const noexcept;
137 double OffspringMass() const noexcept;
139 double PerceptionRange() const noexcept;
140 double PerceptionOmniRange() const noexcept;
141 double PerceptionField() const noexcept;
142 bool PerceptionTest(const glm::dvec3 &) const noexcept;
143 /// chance of giving birth per tick
144 double OffspringChance() const noexcept;
145 /// chance of arbitrary genetic mutation per tick
146 double MutateChance() const noexcept;
147 /// chance of environmental genetic mutation per tick
148 double AdaptChance() const noexcept;
150 void Hurt(double d) noexcept;
152 bool Dead() const noexcept;
153 void WhenDead(Callback cb) noexcept { on_death = cb; }
154 void Remove() noexcept;
155 bool Removable() const noexcept { return removable; }
156 void Removed() noexcept;
158 void AddParent(Creature &);
159 const std::vector<Creature *> &Parents() const noexcept { return parents; }
161 Stats &GetStats() noexcept { return stats; }
162 const Stats &GetStats() const noexcept { return stats; }
164 Memory &GetMemory() noexcept { return memory; }
165 const Memory &GetMemory() const noexcept { return memory; }
167 /// constantly active goal. every creature in the simulation is required to have one
168 void SetBackgroundTask(std::unique_ptr<Goal> &&g);
169 Goal &BackgroundTask();
171 void AddGoal(std::unique_ptr<Goal> &&);
172 const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
174 void Tick(double dt);
176 Situation &GetSituation() noexcept { return situation; }
177 const Situation &GetSituation() const noexcept { return situation; }
179 Steering &GetSteering() noexcept { return steering; }
180 const Steering &GetSteering() const noexcept { return steering; }
182 void HeadingTarget(const glm::dvec3 &t) noexcept { heading_target = t; heading_manual = true; }
184 math::AABB CollisionBounds() const noexcept;
185 glm::dmat4 CollisionTransform() const noexcept;
187 void OnCollide(Creature &other);
189 glm::dmat4 LocalTransform() noexcept;
193 void Draw(graphics::Viewport &);
196 void Cache() noexcept;
197 void TickState(double dt);
198 void TickStats(double dt);
199 void TickBrain(double dt);
200 Situation::Derivative Step(const Situation::Derivative &ds, double dt) const noexcept;
203 world::Simulation ∼
207 Genome::Properties<double> properties;
208 Composition composition;
210 glm::dvec3 base_color;
211 glm::dvec4 highlight_color;
221 std::vector<Creature *> parents;
226 std::unique_ptr<Goal> bg_task;
227 std::vector<std::unique_ptr<Goal>> goals;
231 glm::dvec3 heading_target;
234 // cached because steering makes heavy use of this
235 double perception_range;
236 double perception_range_squared;
237 double perception_omni_range;
238 double perception_omni_range_squared;
239 double perception_field;
246 std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
250 /// put creature on planet and configure it to (hopefully) survive
251 void Spawn(Creature &, world::Planet &);
253 /// split the creature into two
254 void Split(Creature &);