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