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/glm.hpp"
35 using Callback = std::function<void(Creature &)>;
38 // [0,1], zero being good, one bad
40 // static gain per second
42 // adjust value by delta
43 void Add(double delta) noexcept {
44 value = glm::clamp(value + delta, 0.0, 1.0);
46 bool Empty() const noexcept { return value < 0.000001; }
47 bool Good() const noexcept { return value < 0.25; }
48 bool Okay() const noexcept { return value < 0.5; }
49 bool Bad() const noexcept { return !Okay(); }
50 bool Critical() const noexcept { return value > 0.75; }
51 bool Full() const noexcept { return value > 0.999999; }
55 Stat &Damage() noexcept { return stat[0]; }
56 const Stat &Damage() const noexcept { return stat[0]; }
57 Stat &Breath() noexcept { return stat[1]; }
58 const Stat &Breath() const noexcept { return stat[1]; }
59 Stat &Thirst() noexcept { return stat[2]; }
60 const Stat &Thirst() const noexcept { return stat[2]; }
61 Stat &Hunger() noexcept { return stat[3]; }
62 const Stat &Hunger() const noexcept { return stat[3]; }
63 Stat &Exhaustion() noexcept { return stat[4]; }
64 const Stat &Exhaustion() const noexcept { return stat[4]; }
65 Stat &Fatigue() noexcept { return stat[5]; }
66 const Stat &Fatigue() const noexcept { return stat[5]; }
67 Stat &Boredom() noexcept { return stat[6]; }
68 const Stat &Boredom() const noexcept { return stat[6]; }
72 explicit Creature(world::Simulation &);
75 Creature(const Creature &) = delete;
76 Creature &operator =(const Creature &) = delete;
78 Creature(Creature &&) = delete;
79 Creature &operator =(Creature &&) = delete;
82 world::Simulation &GetSimulation() noexcept { return sim; }
83 const world::Simulation &GetSimulation() const noexcept { return sim; }
85 void Name(const std::string &n) noexcept { name = n; }
86 const std::string &Name() const noexcept { return name; }
88 Genome &GetGenome() noexcept { return genome; }
89 const Genome &GetGenome() const noexcept { return genome; }
91 Genome::Properties<double> &GetProperties() noexcept { return properties; }
92 const Genome::Properties<double> &GetProperties() const noexcept { return properties; }
94 void AddMass(int res, double amount);
95 const Composition &GetComposition() const noexcept { return composition; }
97 void BaseColor(const glm::dvec3 &c) noexcept { base_color = c; }
98 const glm::dvec3 &BaseColor() const noexcept { return base_color; }
100 void HighlightColor(const glm::dvec3 &c) noexcept;
101 glm::dvec4 HighlightColor() const noexcept { return highlight_color; }
103 void Mass(double m) noexcept { mass = m; }
104 double Mass() const noexcept { return mass; }
105 void Ingest(int res, double amount) noexcept;
107 void Size(double s) noexcept { size = s; }
108 double Size() const noexcept { return size; }
110 double Born() const noexcept { return birth; }
111 double Age() const noexcept;
112 /// age-depended multiplier, peak being the maximum in lifetime [0,1]
113 double AgeFactor(double peak) const noexcept;
115 double ExhaustionFactor() const noexcept;
116 double FatigueFactor() const noexcept;
118 // stats with effects applied
119 double Strength() const noexcept;
120 double Stamina() const noexcept;
121 double Dexerty() const noexcept;
122 double Intelligence() const noexcept;
123 double Lifetime() const noexcept;
124 double Fertility() const noexcept;
125 double Mutability() const noexcept;
126 double OffspringMass() const noexcept;
128 /// chance of giving birth per tick
129 double OffspringChance() const noexcept;
130 /// chance of random genetic mutation per tick
131 double MutateChance() const noexcept;
133 void Hurt(double d) noexcept;
135 void OnDeath(Callback cb) noexcept { on_death = cb; }
136 void Remove() noexcept { removable = true; }
137 bool Removable() const noexcept { return removable; }
139 Stats &GetStats() noexcept { return stats; }
140 const Stats &GetStats() const noexcept { return stats; }
142 Memory &GetMemory() noexcept { return memory; }
143 const Memory &GetMemory() const noexcept { return memory; }
145 /// constantly active goal. every creature in the simulation is required to have one
146 void SetBackgroundTask(std::unique_ptr<Goal> &&g) { bg_task = std::move(g); }
147 Goal &BackgroundTask() { return *bg_task; }
149 void AddGoal(std::unique_ptr<Goal> &&);
150 const std::vector<std::unique_ptr<Goal>> &Goals() const noexcept { return goals; }
152 void Tick(double dt);
154 Situation &GetSituation() noexcept { return situation; }
155 const Situation &GetSituation() const noexcept { return situation; }
157 Steering &GetSteering() noexcept { return steering; }
158 const Steering &GetSteering() const noexcept { return steering; }
160 glm::dmat4 LocalTransform() noexcept;
163 void Draw(graphics::Viewport &);
166 void TickState(double dt);
167 void TickStats(double dt);
168 void TickBrain(double dt);
169 Situation::Derivative Step(const Situation::Derivative &ds, double dt) const noexcept;
172 world::Simulation ∼
176 Genome::Properties<double> properties;
177 Composition composition;
179 glm::dvec3 base_color;
180 glm::dvec4 highlight_color;
192 std::unique_ptr<Goal> bg_task;
193 std::vector<std::unique_ptr<Goal>> goals;
203 graphics::SimpleVAO<Attributes, unsigned short> vao;
207 /// put creature on planet and configure it to (hopefully) survive
208 void Spawn(Creature &, world::Planet &);
210 /// split the creature into two
211 void Split(Creature &);