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 BackSkin(double s) noexcept { skin_back = s; }
107 double BackSkin() const noexcept { return skin_back; }
109 void SideSkin(double s) noexcept { skin_side = s; }
110 double SideSkin() const noexcept { return skin_side; }
112 void Mass(double m) noexcept { mass = m; }
113 double Mass() const noexcept { return mass; }
114 void Ingest(int res, double amount) noexcept;
116 void DoWork(double amount) noexcept;
118 void Size(double s) noexcept { size = s; }
119 double Size() const noexcept { return size; }
121 double Born() const noexcept { return birth; }
122 double Age() const noexcept;
123 /// age-depended multiplier, peak being the maximum in lifetime [0,1]
124 double AgeFactor(double peak) const noexcept;
126 double EnergyEfficiency() const noexcept;
127 double ExhaustionFactor() const noexcept;
128 double FatigueFactor() const noexcept;
130 // stats with effects applied
131 double Strength() const noexcept;
132 double StrengthFactor() const noexcept;
133 double Stamina() const noexcept;
134 double StaminaFactor() const noexcept;
135 double Dexerty() const noexcept;
136 double DexertyFactor() const noexcept;
137 double Intelligence() const noexcept;
138 double IntelligenceFactor() const noexcept;
139 double Lifetime() const noexcept;
140 double Fertility() const noexcept;
141 double Mutability() const noexcept;
142 double Adaptability() const noexcept;
143 double OffspringMass() const noexcept;
145 double PerceptionRange() const noexcept;
146 double PerceptionOmniRange() const noexcept;
147 double PerceptionField() const noexcept;
148 bool PerceptionTest(const glm::dvec3 &) const noexcept;
149 /// chance of giving birth per tick
150 double OffspringChance() const noexcept;
151 /// chance of arbitrary genetic mutation per tick
152 double MutateChance() const noexcept;
153 /// chance of environmental genetic mutation per tick
154 double AdaptChance() const noexcept;
156 void Hurt(double d) noexcept;
158 bool Dead() const noexcept;
159 void WhenDead(Callback cb) noexcept { on_death = cb; }
160 void Remove() noexcept;
161 bool Removable() const noexcept { return removable; }
162 void Removed() noexcept;
164 void AddParent(Creature &);
165 const std::vector<Creature *> &Parents() const noexcept { return parents; }
167 Stats &GetStats() noexcept { return stats; }
168 const Stats &GetStats() const noexcept { return stats; }
170 Memory &GetMemory() noexcept { return memory; }
171 const Memory &GetMemory() const noexcept { return memory; }
173 /// constantly active goal. every creature in the simulation is required to have one
174 void SetBackgroundTask(std::unique_ptr<Goal> &&g);
175 Goal &BackgroundTask();
177 void AddGoal(std::unique_ptr<Goal> &&);
178 const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
180 void Tick(double dt);
182 Situation &GetSituation() noexcept { return situation; }
183 const Situation &GetSituation() const noexcept { return situation; }
185 Steering &GetSteering() noexcept { return steering; }
186 const Steering &GetSteering() const noexcept { return steering; }
188 void HeadingTarget(const glm::dvec3 &t) noexcept { heading_target = t; heading_manual = true; }
190 math::AABB CollisionBounds() const noexcept;
191 glm::dmat4 CollisionTransform() const noexcept;
193 void OnCollide(Creature &other);
195 glm::dmat4 LocalTransform() noexcept;
199 void Draw(graphics::Viewport &);
202 void Cache() noexcept;
203 void TickState(double dt);
204 void TickStats(double dt);
205 void TickBrain(double dt);
206 Situation::Derivative Step(const Situation::Derivative &ds, double dt) const noexcept;
209 world::Simulation ∼
213 Genome::Properties<double> properties;
214 Composition composition;
216 glm::dvec3 base_color;
217 glm::dvec4 highlight_color;
229 std::vector<Creature *> parents;
234 std::unique_ptr<Goal> bg_task;
235 std::vector<std::unique_ptr<Goal>> goals;
239 glm::dvec3 heading_target;
242 // cached because steering makes heavy use of this
243 double perception_range;
244 double perception_range_squared;
245 double perception_omni_range;
246 double perception_omni_range_squared;
247 double perception_field;
254 std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
258 /// put creature on planet and configure it to (hopefully) survive
259 void Spawn(Creature &, world::Planet &);
261 /// split the creature into two
262 void Split(Creature &);