1 #ifndef BLOBS_CREATURE_CREATURE_HPP_
2 #define BLOBS_CREATURE_CREATURE_HPP_
4 #include "Composition.hpp"
8 #include "Situation.hpp"
9 #include "Steering.hpp"
10 #include "../graphics/SimpleVAO.hpp"
11 #include "../math/geometry.hpp"
12 #include "../math/glm.hpp"
36 using Callback = std::function<void(Creature &)>;
39 // [0,1], zero being good, one bad
41 // static gain per second
43 // adjust value by delta
44 void Add(double delta) noexcept {
45 value = glm::clamp(value + delta, 0.0, 1.0);
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; }
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]; }
73 explicit Creature(world::Simulation &);
76 Creature(const Creature &) = delete;
77 Creature &operator =(const Creature &) = delete;
79 Creature(Creature &&) = delete;
80 Creature &operator =(Creature &&) = delete;
83 world::Simulation &GetSimulation() noexcept { return sim; }
84 const world::Simulation &GetSimulation() const noexcept { return sim; }
86 void Name(const std::string &n) noexcept { name = n; }
87 const std::string &Name() const noexcept { return name; }
89 Genome &GetGenome() noexcept { return genome; }
90 const Genome &GetGenome() const noexcept { return genome; }
92 Genome::Properties<double> &GetProperties() noexcept { return properties; }
93 const Genome::Properties<double> &GetProperties() const noexcept { return properties; }
95 void AddMass(int res, double amount);
96 const Composition &GetComposition() const noexcept { return composition; }
98 void BaseColor(const glm::dvec3 &c) noexcept { base_color = c; }
99 const glm::dvec3 &BaseColor() const noexcept { return base_color; }
101 void HighlightColor(const glm::dvec3 &c) noexcept;
102 glm::dvec4 HighlightColor() const noexcept { return highlight_color; }
104 void Mass(double m) noexcept { mass = m; }
105 double Mass() const noexcept { return mass; }
106 void Ingest(int res, double amount) noexcept;
108 void DoWork(double amount) noexcept;
110 void Size(double s) noexcept { size = s; }
111 double Size() const noexcept { return size; }
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;
118 double EnergyEfficiency() const noexcept;
119 double ExhaustionFactor() const noexcept;
120 double FatigueFactor() const noexcept;
122 // stats with effects applied
123 double Strength() const noexcept;
124 double StrengthFactor() const noexcept;
125 double Stamina() const noexcept;
126 double StaminaFactor() const noexcept;
127 double Dexerty() const noexcept;
128 double DexertyFactor() const noexcept;
129 double Intelligence() const noexcept;
130 double IntelligenceFactor() const noexcept;
131 double Lifetime() const noexcept;
132 double Fertility() const noexcept;
133 double Mutability() const noexcept;
134 double Adaptability() const noexcept;
135 double OffspringMass() const noexcept;
137 double PerceptionRange() const noexcept;
138 double PerceptionOmniRange() const noexcept;
139 double PerceptionField() const noexcept;
140 bool PerceptionTest(const glm::dvec3 &) const noexcept;
141 /// chance of giving birth per tick
142 double OffspringChance() const noexcept;
143 /// chance of arbitrary genetic mutation per tick
144 double MutateChance() const noexcept;
145 /// chance of environmental genetic mutation per tick
146 double AdaptChance() const noexcept;
148 void Hurt(double d) noexcept;
150 bool Dead() const noexcept;
151 void WhenDead(Callback cb) noexcept { on_death = cb; }
152 void Remove() noexcept;
153 bool Removable() const noexcept { return removable; }
154 void Removed() noexcept;
156 void AddParent(Creature &);
157 const std::vector<Creature *> &Parents() const noexcept { return parents; }
159 Stats &GetStats() noexcept { return stats; }
160 const Stats &GetStats() const noexcept { return stats; }
162 Memory &GetMemory() noexcept { return memory; }
163 const Memory &GetMemory() const noexcept { return memory; }
165 /// constantly active goal. every creature in the simulation is required to have one
166 void SetBackgroundTask(std::unique_ptr<Goal> &&g) { bg_task = std::move(g); }
167 Goal &BackgroundTask() { return *bg_task; }
169 void AddGoal(std::unique_ptr<Goal> &&);
170 const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
172 void Tick(double dt);
174 Situation &GetSituation() noexcept { return situation; }
175 const Situation &GetSituation() const noexcept { return situation; }
177 Steering &GetSteering() noexcept { return steering; }
178 const Steering &GetSteering() const noexcept { return steering; }
180 math::AABB CollisionBounds() const noexcept;
181 glm::dmat4 CollisionTransform() const noexcept;
183 glm::dmat4 LocalTransform() noexcept;
187 void Draw(graphics::Viewport &);
190 void TickState(double dt);
191 void TickStats(double dt);
192 void TickBrain(double dt);
193 Situation::Derivative Step(const Situation::Derivative &ds, double dt) const noexcept;
196 world::Simulation ∼
200 Genome::Properties<double> properties;
201 Composition composition;
203 glm::dvec3 base_color;
204 glm::dvec4 highlight_color;
214 std::vector<Creature *> parents;
219 std::unique_ptr<Goal> bg_task;
220 std::vector<std::unique_ptr<Goal>> goals;
230 std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
234 /// put creature on planet and configure it to (hopefully) survive
235 void Spawn(Creature &, world::Planet &);
237 /// split the creature into two
238 void Split(Creature &);