]> git.localhorst.tv Git - blobs.git/blob - src/creature/Creature.hpp
better heading implementation
[blobs.git] / src / creature / Creature.hpp
1 #ifndef BLOBS_CREATURE_CREATURE_HPP_
2 #define BLOBS_CREATURE_CREATURE_HPP_
3
4 #include "Composition.hpp"
5 #include "Genome.hpp"
6 #include "Memory.hpp"
7 #include "Situation.hpp"
8 #include "Steering.hpp"
9 #include "../graphics/SimpleVAO.hpp"
10 #include "../math/geometry.hpp"
11 #include "../math/glm.hpp"
12
13 #include <functional>
14 #include <memory>
15 #include <string>
16 #include <vector>
17
18
19 namespace blobs {
20 namespace app {
21         struct Assets;
22 }
23 namespace graphics {
24         class Viewport;
25 }
26 namespace world {
27         class Body;
28         class Planet;
29         class Simulation;
30 }
31 namespace creature {
32
33 class Goal;
34
35 class Creature {
36
37 public:
38         using Callback = std::function<void(Creature &)>;
39
40         struct Stat {
41                 // [0,1], zero being good, one bad
42                 double value = 0.0;
43                 // static gain per second
44                 double gain = 0.0;
45                 // adjust value by delta
46                 void Add(double delta) noexcept {
47                         value = glm::clamp(value + delta, 0.0, 1.0);
48                 }
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; }
55         };
56         struct Stats {
57                 Stat stat[7];
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]; }
72         };
73
74 public:
75         explicit Creature(world::Simulation &);
76         ~Creature();
77
78         Creature(const Creature &) = delete;
79         Creature &operator =(const Creature &) = delete;
80
81         Creature(Creature &&) = delete;
82         Creature &operator =(Creature &&) = delete;
83
84 public:
85         world::Simulation &GetSimulation() noexcept { return sim; }
86         const world::Simulation &GetSimulation() const noexcept { return sim; }
87
88         void Name(const std::string &n) noexcept { name = n; }
89         const std::string &Name() const noexcept { return name; }
90
91         Genome &GetGenome() noexcept { return genome; }
92         const Genome &GetGenome() const noexcept { return genome; }
93
94         Genome::Properties<double> &GetProperties() noexcept { return properties; }
95         const Genome::Properties<double> &GetProperties() const noexcept { return properties; }
96
97         void AddMass(int res, double amount);
98         const Composition &GetComposition() const noexcept { return composition; }
99
100         void BaseColor(const glm::dvec3 &c) noexcept { base_color = c; }
101         const glm::dvec3 &BaseColor() const noexcept { return base_color; }
102
103         void HighlightColor(const glm::dvec3 &c) noexcept;
104         glm::dvec4 HighlightColor() const noexcept { return highlight_color; }
105
106         void Mass(double m) noexcept { mass = m; }
107         double Mass() const noexcept { return mass; }
108         void Ingest(int res, double amount) noexcept;
109
110         void DoWork(double amount) noexcept;
111
112         void Size(double s) noexcept { size = s; }
113         double Size() const noexcept { return size; }
114
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;
119
120         double EnergyEfficiency() const noexcept;
121         double ExhaustionFactor() const noexcept;
122         double FatigueFactor() const noexcept;
123
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;
138
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;
149
150         void Hurt(double d) noexcept;
151         void Die() 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;
157
158         void AddParent(Creature &);
159         const std::vector<Creature *> &Parents() const noexcept { return parents; }
160
161         Stats &GetStats() noexcept { return stats; }
162         const Stats &GetStats() const noexcept { return stats; }
163
164         Memory &GetMemory() noexcept { return memory; }
165         const Memory &GetMemory() const noexcept { return memory; }
166
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();
170
171         void AddGoal(std::unique_ptr<Goal> &&);
172         const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
173
174         void Tick(double dt);
175
176         Situation &GetSituation() noexcept { return situation; }
177         const Situation &GetSituation() const noexcept { return situation; }
178
179         Steering &GetSteering() noexcept { return steering; }
180         const Steering &GetSteering() const noexcept { return steering; }
181
182         void HeadingTarget(const glm::dvec3 &t) noexcept { heading_target = t; heading_manual = true; }
183
184         math::AABB CollisionBounds() const noexcept;
185         glm::dmat4 CollisionTransform() const noexcept;
186
187         void OnCollide(Creature &other);
188
189         glm::dmat4 LocalTransform() noexcept;
190
191         void BuildVAO();
192         void KillVAO();
193         void Draw(graphics::Viewport &);
194
195 private:
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;
201
202 private:
203         world::Simulation &sim;
204         std::string name;
205
206         Genome genome;
207         Genome::Properties<double> properties;
208         Composition composition;
209
210         glm::dvec3 base_color;
211         glm::dvec4 highlight_color;
212
213         double mass;
214         double size;
215
216         double birth;
217         double death;
218         Callback on_death;
219         bool removable;
220
221         std::vector<Creature *> parents;
222
223         Stats stats;
224         Memory memory;
225
226         std::unique_ptr<Goal> bg_task;
227         std::vector<std::unique_ptr<Goal>> goals;
228
229         Situation situation;
230         Steering steering;
231         glm::dvec3 heading_target;
232         bool heading_manual;
233
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;
240
241         struct Attributes {
242                 glm::vec3 position;
243                 glm::vec3 normal;
244                 glm::vec3 texture;
245         };
246         std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
247
248 };
249
250 /// put creature on planet and configure it to (hopefully) survive
251 void Spawn(Creature &, world::Planet &);
252
253 /// split the creature into two
254 void Split(Creature &);
255
256 }
257 }
258
259 #endif