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