]> git.localhorst.tv Git - blobs.git/blob - src/creature/Creature.hpp
make texture part of genome
[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 BackSkin(double s) noexcept { skin_back = s; }
107         double BackSkin() const noexcept { return skin_back; }
108
109         void SideSkin(double s) noexcept { skin_side = s; }
110         double SideSkin() const noexcept { return skin_side; }
111
112         void Mass(double m) noexcept { mass = m; }
113         double Mass() const noexcept { return mass; }
114         void Ingest(int res, double amount) noexcept;
115
116         void DoWork(double amount) noexcept;
117
118         void Size(double s) noexcept { size = s; }
119         double Size() const noexcept { return size; }
120
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;
125
126         double EnergyEfficiency() const noexcept;
127         double ExhaustionFactor() const noexcept;
128         double FatigueFactor() const noexcept;
129
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;
144
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;
155
156         void Hurt(double d) noexcept;
157         void Die() 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;
163
164         void AddParent(Creature &);
165         const std::vector<Creature *> &Parents() const noexcept { return parents; }
166
167         Stats &GetStats() noexcept { return stats; }
168         const Stats &GetStats() const noexcept { return stats; }
169
170         Memory &GetMemory() noexcept { return memory; }
171         const Memory &GetMemory() const noexcept { return memory; }
172
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();
176
177         void AddGoal(std::unique_ptr<Goal> &&);
178         const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
179
180         void Tick(double dt);
181
182         Situation &GetSituation() noexcept { return situation; }
183         const Situation &GetSituation() const noexcept { return situation; }
184
185         Steering &GetSteering() noexcept { return steering; }
186         const Steering &GetSteering() const noexcept { return steering; }
187
188         void HeadingTarget(const glm::dvec3 &t) noexcept { heading_target = t; heading_manual = true; }
189
190         math::AABB CollisionBounds() const noexcept;
191         glm::dmat4 CollisionTransform() const noexcept;
192
193         void OnCollide(Creature &other);
194
195         glm::dmat4 LocalTransform() noexcept;
196
197         void BuildVAO();
198         void KillVAO();
199         void Draw(graphics::Viewport &);
200
201 private:
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;
207
208 private:
209         world::Simulation &sim;
210         std::string name;
211
212         Genome genome;
213         Genome::Properties<double> properties;
214         Composition composition;
215
216         glm::dvec3 base_color;
217         glm::dvec4 highlight_color;
218         double skin_back;
219         double skin_side;
220
221         double mass;
222         double size;
223
224         double birth;
225         double death;
226         Callback on_death;
227         bool removable;
228
229         std::vector<Creature *> parents;
230
231         Stats stats;
232         Memory memory;
233
234         std::unique_ptr<Goal> bg_task;
235         std::vector<std::unique_ptr<Goal>> goals;
236
237         Situation situation;
238         Steering steering;
239         glm::dvec3 heading_target;
240         bool heading_manual;
241
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;
248
249         struct Attributes {
250                 glm::vec3 position;
251                 glm::vec3 normal;
252                 glm::vec3 texture;
253         };
254         std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
255
256 };
257
258 /// put creature on planet and configure it to (hopefully) survive
259 void Spawn(Creature &, world::Planet &);
260
261 /// split the creature into two
262 void Split(Creature &);
263
264 }
265 }
266
267 #endif