]> git.localhorst.tv Git - blobs.git/blob - src/creature/Creature.hpp
0012d784a5d913132f3c030e73e66d996978673e
[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 "Goal.hpp"
7 #include "Memory.hpp"
8 #include "Situation.hpp"
9 #include "Steering.hpp"
10 #include "../graphics/SimpleVAO.hpp"
11 #include "../math/geometry.hpp"
12 #include "../math/glm.hpp"
13
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 Creature {
34
35 public:
36         using Callback = std::function<void(Creature &)>;
37
38         struct Stat {
39                 // [0,1], zero being good, one bad
40                 double value = 0.0;
41                 // static gain per second
42                 double gain = 0.0;
43                 // adjust value by delta
44                 void Add(double delta) noexcept {
45                         value = glm::clamp(value + delta, 0.0, 1.0);
46                 }
47                 bool Empty() const noexcept { return value < 0.000001; }
48                 bool Good() const noexcept { return value < 0.25; }
49                 bool Okay() const noexcept { return value < 0.5; }
50                 bool Bad() const noexcept { return !Okay(); }
51                 bool Critical() const noexcept { return value > 0.75; }
52                 bool Full() const noexcept { return value > 0.999999; }
53         };
54         struct Stats {
55                 Stat stat[7];
56                 Stat &Damage() noexcept { return stat[0]; }
57                 const Stat &Damage() const noexcept { return stat[0]; }
58                 Stat &Breath() noexcept { return stat[1]; }
59                 const Stat &Breath() const noexcept { return stat[1]; }
60                 Stat &Thirst() noexcept { return stat[2]; }
61                 const Stat &Thirst() const noexcept { return stat[2]; }
62                 Stat &Hunger() noexcept { return stat[3]; }
63                 const Stat &Hunger() const noexcept { return stat[3]; }
64                 Stat &Exhaustion() noexcept { return stat[4]; }
65                 const Stat &Exhaustion() const noexcept { return stat[4]; }
66                 Stat &Fatigue() noexcept { return stat[5]; }
67                 const Stat &Fatigue() const noexcept { return stat[5]; }
68                 Stat &Boredom() noexcept { return stat[6]; }
69                 const Stat &Boredom() const noexcept { return stat[6]; }
70         };
71
72 public:
73         explicit Creature(world::Simulation &);
74         ~Creature();
75
76         Creature(const Creature &) = delete;
77         Creature &operator =(const Creature &) = delete;
78
79         Creature(Creature &&) = delete;
80         Creature &operator =(Creature &&) = delete;
81
82 public:
83         world::Simulation &GetSimulation() noexcept { return sim; }
84         const world::Simulation &GetSimulation() const noexcept { return sim; }
85
86         void Name(const std::string &n) noexcept { name = n; }
87         const std::string &Name() const noexcept { return name; }
88
89         Genome &GetGenome() noexcept { return genome; }
90         const Genome &GetGenome() const noexcept { return genome; }
91
92         Genome::Properties<double> &GetProperties() noexcept { return properties; }
93         const Genome::Properties<double> &GetProperties() const noexcept { return properties; }
94
95         void AddMass(int res, double amount);
96         const Composition &GetComposition() const noexcept { return composition; }
97
98         void BaseColor(const glm::dvec3 &c) noexcept { base_color = c; }
99         const glm::dvec3 &BaseColor() const noexcept { return base_color; }
100
101         void HighlightColor(const glm::dvec3 &c) noexcept;
102         glm::dvec4 HighlightColor() const noexcept { return highlight_color; }
103
104         void Mass(double m) noexcept { mass = m; }
105         double Mass() const noexcept { return mass; }
106         void Ingest(int res, double amount) noexcept;
107
108         void DoWork(double amount) noexcept;
109
110         void Size(double s) noexcept { size = s; }
111         double Size() const noexcept { return size; }
112
113         double Born() const noexcept { return birth; }
114         double Age() const noexcept;
115         /// age-depended multiplier, peak being the maximum in lifetime [0,1]
116         double AgeFactor(double peak) const noexcept;
117
118         double EnergyEfficiency() const noexcept;
119         double ExhaustionFactor() const noexcept;
120         double FatigueFactor() const noexcept;
121
122         // stats with effects applied
123         double Strength() const noexcept;
124         double Stamina() const noexcept;
125         double Dexerty() const noexcept;
126         double Intelligence() const noexcept;
127         double Lifetime() const noexcept;
128         double Fertility() const noexcept;
129         double Mutability() const noexcept;
130         double Adaptability() const noexcept;
131         double OffspringMass() const noexcept;
132
133         double PerceptionRange() const noexcept;
134         double PerceptionOmniRange() const noexcept;
135         double PerceptionField() const noexcept;
136         bool PerceptionTest(const glm::dvec3 &) const noexcept;
137         /// chance of giving birth per tick
138         double OffspringChance() const noexcept;
139         /// chance of arbitrary genetic mutation per tick
140         double MutateChance() const noexcept;
141         /// chance of environmental genetic mutation per tick
142         double AdaptChance() const noexcept;
143
144         void Hurt(double d) noexcept;
145         void Die() noexcept;
146         bool Dead() const noexcept;
147         void WhenDead(Callback cb) noexcept { on_death = cb; }
148         void Remove() noexcept;
149         bool Removable() const noexcept { return removable; }
150         void Removed() noexcept;
151
152         void AddParent(Creature &);
153         const std::vector<Creature *> &Parents() const noexcept { return parents; }
154
155         Stats &GetStats() noexcept { return stats; }
156         const Stats &GetStats() const noexcept { return stats; }
157
158         Memory &GetMemory() noexcept { return memory; }
159         const Memory &GetMemory() const noexcept { return memory; }
160
161         /// constantly active goal. every creature in the simulation is required to have one
162         void SetBackgroundTask(std::unique_ptr<Goal> &&g) { bg_task = std::move(g); }
163         Goal &BackgroundTask() { return *bg_task; }
164
165         void AddGoal(std::unique_ptr<Goal> &&);
166         const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
167
168         void Tick(double dt);
169
170         Situation &GetSituation() noexcept { return situation; }
171         const Situation &GetSituation() const noexcept { return situation; }
172
173         Steering &GetSteering() noexcept { return steering; }
174         const Steering &GetSteering() const noexcept { return steering; }
175
176         math::AABB CollisionBox() const noexcept;
177         glm::dmat4 CollisionTransform() const noexcept;
178
179         glm::dmat4 LocalTransform() noexcept;
180
181         void BuildVAO();
182         void KillVAO();
183         void Draw(graphics::Viewport &);
184
185 private:
186         void TickState(double dt);
187         void TickStats(double dt);
188         void TickBrain(double dt);
189         Situation::Derivative Step(const Situation::Derivative &ds, double dt) const noexcept;
190
191 private:
192         world::Simulation &sim;
193         std::string name;
194
195         Genome genome;
196         Genome::Properties<double> properties;
197         Composition composition;
198
199         glm::dvec3 base_color;
200         glm::dvec4 highlight_color;
201
202         double mass;
203         double size;
204
205         double birth;
206         double death;
207         Callback on_death;
208         bool removable;
209
210         std::vector<Creature *> parents;
211
212         Stats stats;
213         Memory memory;
214
215         std::unique_ptr<Goal> bg_task;
216         std::vector<std::unique_ptr<Goal>> goals;
217
218         Situation situation;
219         Steering steering;
220
221         struct Attributes {
222                 glm::vec3 position;
223                 glm::vec3 normal;
224                 glm::vec3 texture;
225         };
226         std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
227
228 };
229
230 /// put creature on planet and configure it to (hopefully) survive
231 void Spawn(Creature &, world::Planet &);
232
233 /// split the creature into two
234 void Split(Creature &);
235
236 }
237 }
238
239 #endif