]> git.localhorst.tv Git - blobs.git/blob - src/creature/creature.cpp
randomize creature properties a bit
[blobs.git] / src / creature / creature.cpp
1 #include "Creature.hpp"
2 #include "Genome.hpp"
3 #include "Situation.hpp"
4 #include "Steering.hpp"
5
6 #include "Goal.hpp"
7 #include "InhaleNeed.hpp"
8 #include "IngestNeed.hpp"
9 #include "Need.hpp"
10 #include "../app/Assets.hpp"
11 #include "../world/Body.hpp"
12 #include "../world/Planet.hpp"
13 #include "../world/Simulation.hpp"
14 #include "../world/TileType.hpp"
15
16 #include <algorithm>
17 #include <glm/gtx/transform.hpp>
18
19 #include <iostream>
20 #include <glm/gtx/io.hpp>
21
22
23 namespace blobs {
24 namespace creature {
25
26 Creature::Creature(world::Simulation &sim)
27 : sim(sim)
28 , name()
29 , genome()
30 , mass(1.0)
31 , size(1.0)
32 , health(1.0)
33 , needs()
34 , goals()
35 , situation()
36 , steering()
37 , vel(0.0)
38 , vao() {
39 }
40
41 Creature::~Creature() {
42 }
43
44 void Creature::Hurt(double dt) noexcept {
45         health = std::max(0.0, health - dt);
46 }
47
48 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
49         std::cout << "new goal: " << g->Describe() << std::endl;
50         g->Enable();
51         goals.emplace_back(std::move(g));
52 }
53
54 namespace {
55
56 bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
57         return b->Urgency() < a->Urgency();
58 }
59
60 }
61
62 void Creature::Tick(double dt) {
63         // TODO: better integration method
64         glm::dvec3 acc(steering.Acceleration(*this));
65         situation.Move(vel * dt);
66         vel += acc * dt;
67
68         for (auto &need : needs) {
69                 need->Tick(dt);
70         }
71         for (auto &goal : goals) {
72                 goal->Tick(dt);
73         }
74         // do background stuff
75         for (auto &need : needs) {
76                 need->ApplyEffect(*this, dt);
77         }
78         if (goals.empty()) {
79                 return;
80         }
81         // if active goal can be interrupted, check priorities
82         if (goals.size() > 1 && goals[0]->Interruptible()) {
83                 Goal *old_top = &*goals[0];
84                 std::sort(goals.begin(), goals.end(), GoalCompare);
85                 Goal *new_top = &*goals[0];
86                 if (new_top != old_top) {
87                         std::cout << "changing goal from " << old_top->Describe()
88                                 << " to " << new_top->Describe() << std::endl;
89                 }
90         }
91         goals[0]->Action();
92         for (auto goal = goals.begin(); goal != goals.end();) {
93                 if ((*goal)->Complete()) {
94                         std::cout << "complete goal: " << (*goal)->Describe() << std::endl;
95                         goals.erase(goal);
96                 } else {
97                         ++goal;
98                 }
99         }
100 }
101
102 glm::dmat4 Creature::LocalTransform() noexcept {
103         // TODO: surface transform
104         const double half_size = size * 0.5;
105         const glm::dvec3 &pos = situation.Position();
106         return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
107                 * glm::scale(glm::dvec3(half_size, half_size, half_size));
108 }
109
110 void Creature::BuildVAO() {
111         vao.Bind();
112         vao.BindAttributes();
113         vao.EnableAttribute(0);
114         vao.EnableAttribute(1);
115         vao.EnableAttribute(2);
116         vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
117         vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
118         vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
119         vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
120         {
121                 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
122                 const float offset = 1.0f;
123                 for (int surface = 0; surface < 6; ++surface) {
124                         const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
125                         const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
126
127                         attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
128                         attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
129                         attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
130                         attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
131                         attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
132                         attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
133                         attrib[4 * surface + 0].texture.x = tex_u_begin;
134                         attrib[4 * surface + 0].texture.y = 1.0f;
135                         attrib[4 * surface + 0].texture.z = surface;
136
137                         attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
138                         attrib[4 * surface + 1].position[(surface + 1) % 3] =  offset;
139                         attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
140                         attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
141                         attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
142                         attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
143                         attrib[4 * surface + 1].texture.x = tex_u_end;
144                         attrib[4 * surface + 1].texture.y = 1.0f;
145                         attrib[4 * surface + 1].texture.z = surface;
146
147                         attrib[4 * surface + 2].position[(surface + 0) % 3] =  offset;
148                         attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
149                         attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
150                         attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
151                         attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
152                         attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
153                         attrib[4 * surface + 2].texture.x = tex_u_begin;
154                         attrib[4 * surface + 2].texture.y = 0.0f;
155                         attrib[4 * surface + 2].texture.z = surface;
156
157                         attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
158                         attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
159                         attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
160                         attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
161                         attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
162                         attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
163                         attrib[4 * surface + 3].texture.x = tex_u_end;
164                         attrib[4 * surface + 3].texture.y = 0.0f;
165                         attrib[4 * surface + 3].texture.z = surface;
166                 }
167         }
168         vao.BindElements();
169         vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
170         {
171                 auto element = vao.MapElements(GL_WRITE_ONLY);
172                 for (int surface = 0; surface < 3; ++surface) {
173                         element[6 * surface + 0] = 4 * surface + 0;
174                         element[6 * surface + 1] = 4 * surface + 2;
175                         element[6 * surface + 2] = 4 * surface + 1;
176                         element[6 * surface + 3] = 4 * surface + 1;
177                         element[6 * surface + 4] = 4 * surface + 2;
178                         element[6 * surface + 5] = 4 * surface + 3;
179                 }
180                 for (int surface = 3; surface < 6; ++surface) {
181                         element[6 * surface + 0] = 4 * surface + 0;
182                         element[6 * surface + 1] = 4 * surface + 1;
183                         element[6 * surface + 2] = 4 * surface + 2;
184                         element[6 * surface + 3] = 4 * surface + 2;
185                         element[6 * surface + 4] = 4 * surface + 1;
186                         element[6 * surface + 5] = 4 * surface + 3;
187                 }
188         }
189         vao.Unbind();
190 }
191
192 void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
193         vao.Bind();
194         vao.DrawTriangles(6 * 6);
195 }
196
197
198 void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
199         p.AddCreature(&c);
200         c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
201
202         // probe surrounding area for common resources
203         int start = p.SideLength() / 2 - 2;
204         int end = start + 5;
205         std::map<int, double> yields;
206         for (int y = start; y < end; ++y) {
207                 for (int x = start; x < end; ++x) {
208                         const world::TileType &t = assets.data.tile_types[p.TileAt(0, x, y).type];
209                         for (auto yield : t.resources) {
210                                 yields[yield.resource] += yield.ubiquity;
211                         }
212                 }
213         }
214         int liquid = -1;
215         int solid = -1;
216         for (auto e : yields) {
217                 if (assets.data.resources[e.first].state == world::Resource::LIQUID) {
218                         if (liquid < 0 || e.second > yields[liquid]) {
219                                 liquid = e.first;
220                         }
221                 } else if (assets.data.resources[e.first].state == world::Resource::SOLID) {
222                         if (solid < 0 || e.second > yields[solid]) {
223                                 solid = e.first;
224                         }
225                 }
226         }
227
228         Genome genome;
229         if (p.HasAtmosphere()) {
230                 genome.composition.push_back({
231                         p.Atmosphere(),    // resource
232                         { 0.01, 0.00001 }, // mass
233                         { 0.5,  0.001 },   // intake
234                         { 0.1,  0.0005 }   // penalty
235                 });
236         }
237         if (liquid > -1) {
238                 genome.composition.push_back({
239                         liquid,          // resource
240                         { 0.6,  0.01 },  // mass
241                         { 0.2,  0.001 }, // intake
242                         { 0.01, 0.002 }  // penalty
243                 });
244         }
245         if (solid > -1) {
246                 genome.composition.push_back({
247                         solid,           // resource
248                         { 0.4,   0.01 },  // mass
249                         { 0.1,   0.001 }, // intake
250                         { 0.001, 0.0001 } // penalty
251                 });
252         }
253
254         genome.Configure(assets, c);
255         c.GetSteering().MaxAcceleration(1.4);
256         c.GetSteering().MaxSpeed(4.4);
257 }
258
259 void Genome::Configure(app::Assets &assets, Creature &c) const {
260         c.GetGenome() = *this;
261         double mass = 0.0;
262         double volume = 0.0;
263         for (const auto &comp : composition) {
264                 double comp_mass = comp.mass.FakeNormal(assets.random.SNorm());
265                 double intake = comp.intake.FakeNormal(assets.random.SNorm());
266                 double penalty = comp.intake.FakeNormal(assets.random.SNorm());
267
268                 mass += comp_mass;
269                 volume += comp_mass / assets.data.resources[comp.resource].density;
270
271                 std::unique_ptr<Need> need;
272                 if (assets.data.resources[comp.resource].state == world::Resource::SOLID) {
273                         need.reset(new IngestNeed(comp.resource, intake, penalty));
274                         need->gain = intake * 0.05;
275                 } else if (assets.data.resources[comp.resource].state == world::Resource::LIQUID) {
276                         need.reset(new IngestNeed(comp.resource, intake, penalty));
277                         need->gain = intake * 0.1;
278                 } else {
279                         need.reset(new InhaleNeed(comp.resource, intake, penalty));
280                         need->gain = intake * 0.5;
281                 }
282                 need->name = assets.data.resources[comp.resource].label;
283                 need->inconvenient = 0.5;
284                 need->critical = 0.95;
285                 c.AddNeed(std::move(need));
286         }
287         c.Mass(mass);
288         c.Size(std::cbrt(volume));
289 }
290
291 Situation::Situation()
292 : planet(nullptr)
293 , position(0.0)
294 , surface(0)
295 , type(LOST) {
296 }
297
298 Situation::~Situation() {
299 }
300
301 bool Situation::OnPlanet() const noexcept {
302         return type == PLANET_SURFACE;
303 }
304
305 bool Situation::OnSurface() const noexcept {
306         return type == PLANET_SURFACE;
307 }
308
309 world::Tile &Situation::GetTile() const noexcept {
310         double side_length = planet->SideLength();
311         double offset = side_length * 0.5;
312         double x = std::max(0.0, std::min(side_length, position.x + offset));
313         double y = std::max(0.0, std::min(side_length, position.y + offset));
314         return planet->TileAt(surface, int(x), int(y));
315 }
316
317 const world::TileType &Situation::GetTileType() const noexcept {
318         return planet->GetSimulation().TileTypes()[GetTile().type];
319 }
320
321 void Situation::Move(const glm::dvec3 &dp) noexcept {
322         position += dp;
323         if (OnSurface()) {
324                 // enforce ground constraint
325                 if (Surface() < 3) {
326                         position[(Surface() + 2) % 3] = std::max(0.0, position[(Surface() + 2) % 3]);
327                 } else {
328                         position[(Surface() + 2) % 3] = std::min(0.0, position[(Surface() + 2) % 3]);
329                 }
330         }
331 }
332
333 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
334         type = PLANET_SURFACE;
335         planet = &p;
336         surface = srf;
337         position = pos;
338 }
339
340
341 Steering::Steering()
342 : seek_target(0.0)
343 , max_accel(1.0)
344 , max_speed(1.0)
345 , halting(false)
346 , seeking(false) {
347 }
348
349 Steering::~Steering() {
350 }
351
352 void Steering::Halt() noexcept {
353         halting = true;
354         seeking = false;
355 }
356
357 void Steering::GoTo(const glm::dvec3 &t) noexcept {
358         seek_target = t;
359         halting = false;
360         seeking = true;
361 }
362
363 glm::dvec3 Steering::Acceleration(Creature &c) const noexcept {
364         glm::dvec3 acc(0.0);
365         if (halting) {
366                 SumForce(acc, c.Velocity() * -max_accel);
367         }
368         if (seeking) {
369                 glm::dvec3 diff = seek_target - c.GetSituation().Position();
370                 if (!allzero(diff)) {
371                         SumForce(acc, ((normalize(diff) * max_speed) - c.Velocity()) * max_accel);
372                 }
373         }
374         return acc;
375 }
376
377 bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
378         if (allzero(in) || anynan(in)) {
379                 return false;
380         }
381         double cur = allzero(out) ? 0.0 : length(out);
382         double rem = max_accel - cur;
383         if (rem < 0.0) {
384                 return true;
385         }
386         double add = length(in);
387         if (add > rem) {
388                 // this method is off if in and out are in different
389                 // directions, but gives okayish results
390                 out += in * (1.0 / add);
391                 return true;
392         } else {
393                 out += in;
394                 return false;
395         }
396 }
397
398 }
399 }