]> git.localhorst.tv Git - blobs.git/blob - src/world/world.cpp
primitive collision response
[blobs.git] / src / world / world.cpp
1 #include "Body.hpp"
2 #include "CreatureCreatureCollision.hpp"
3 #include "Orbit.hpp"
4 #include "Planet.hpp"
5 #include "Resource.hpp"
6 #include "Set.hpp"
7 #include "Simulation.hpp"
8 #include "Sun.hpp"
9 #include "Tile.hpp"
10 #include "TileType.hpp"
11
12 #include "../app/Assets.hpp"
13 #include "../creature/Composition.hpp"
14 #include "../creature/Creature.hpp"
15 #include "../graphics/Viewport.hpp"
16 #include "../math/const.hpp"
17 #include "../math/geometry.hpp"
18 #include "../math/OctaveNoise.hpp"
19 #include "../math/SimplexNoise.hpp"
20
21 #include <algorithm>
22 #include <cmath>
23 #include <iostream>
24 #include <glm/gtc/matrix_transform.hpp>
25 #include <glm/gtx/euler_angles.hpp>
26 #include <glm/gtx/io.hpp>
27 #include <glm/gtx/transform.hpp>
28
29 using blobs::G;
30 using blobs::PI_2p0;
31
32 using std::sin;
33 using std::cos;
34 using std::pow;
35 using std::sqrt;
36
37
38 namespace blobs {
39 namespace world {
40
41 Body::Body()
42 : sim(nullptr)
43 , parent(nullptr)
44 , children()
45 , mass(1.0)
46 , radius(1.0)
47 , orbit()
48 , surface_tilt(0.0, 0.0)
49 , axis_tilt(0.0, 0.0)
50 , rotation(0.0)
51 , angular(0.0)
52 , orbital(1.0)
53 , inverse_orbital(1.0)
54 , local(1.0)
55 , inverse_local(1.0)
56 , creatures()
57 , atmosphere(-1) {
58 }
59
60 Body::~Body() {
61         for (creature::Creature *c : creatures) {
62                 delete c;
63         }
64 }
65
66 void Body::SetSimulation(Simulation &s) noexcept {
67         sim = &s;
68         for (auto child : children) {
69                 child->SetSimulation(s);
70         }
71 }
72
73 void Body::SetParent(Body &p) {
74         if (HasParent()) {
75                 UnsetParent();
76         }
77         parent = &p;
78         parent->AddChild(*this);
79 }
80
81 void Body::UnsetParent() {
82         if (!HasParent()) return;
83         parent->RemoveChild(*this);
84         parent = nullptr;
85 }
86
87 void Body::AddChild(Body &c) {
88         children.push_back(&c);
89         c.SetSimulation(*sim);
90 }
91
92 void Body::RemoveChild(Body &c) {
93         auto entry = std::find(children.begin(), children.end(), &c);
94         if (entry != children.end()) {
95                 children.erase(entry);
96         }
97 }
98
99 double Body::Inertia() const noexcept {
100         // assume solid sphere for now
101         return (2.0/5.0) * Mass() * pow(Radius(), 2);
102 }
103
104 double Body::GravitationalParameter() const noexcept {
105         return G * Mass();
106 }
107
108 double Body::OrbitalPeriod() const noexcept {
109         if (parent) {
110                 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
111         } else {
112                 return 0.0;
113         }
114 }
115
116 double Body::RotationalPeriod() const noexcept {
117         if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
118                 return std::numeric_limits<double>::infinity();
119         } else {
120                 return PI_2p0 * Inertia() / angular;
121         }
122 }
123
124 glm::dmat4 Body::ToUniverse() const noexcept {
125         glm::dmat4 m(1.0);
126         const Body *b = this;
127         while (b->HasParent()) {
128                 m = b->ToParent() * m;
129                 b = &b->Parent();
130         }
131         return m;
132 }
133
134 glm::dmat4 Body::FromUniverse() const noexcept {
135         glm::dmat4 m(1.0);
136         const Body *b = this;
137         while (b->HasParent()) {
138                 m *= b->FromParent();
139                 b = &b->Parent();
140         }
141         return m;
142 }
143
144 namespace {
145 std::vector<creature::Creature *> ccache;
146 std::vector<CreatureCreatureCollision> collisions;
147 }
148
149 void Body::Tick(double dt) {
150         rotation += dt * AngularMomentum() / Inertia();
151         Cache();
152         ccache = Creatures();
153         for (creature::Creature *c : ccache) {
154                 c->Tick(dt);
155         }
156         // first remove creatures so they don't collide
157         for (auto c = Creatures().begin(); c != Creatures().end();) {
158                 if ((*c)->Removable()) {
159                         delete *c;
160                         c = Creatures().erase(c);
161                 } else {
162                         ++c;
163                 }
164         }
165         CheckCollision();
166 }
167
168 void Body::Cache() noexcept {
169         if (parent) {
170                 orbital =
171                         orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
172                         * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
173                 inverse_orbital =
174                         glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
175                         * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
176         } else {
177                 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
178                 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
179         }
180         local =
181                 glm::eulerAngleY(rotation)
182                 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
183         inverse_local =
184                 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
185                 * glm::eulerAngleY(-rotation);
186 }
187
188 void Body::CheckCollision() noexcept {
189         if (Creatures().size() < 2) return;
190         collisions.clear();
191         auto end = Creatures().end();
192         for (auto i = Creatures().begin(); i != end; ++i) {
193                 math::AABB i_box((*i)->CollisionBox());
194                 glm::dmat4 i_mat((*i)->CollisionTransform());
195                 for (auto j = (i + 1); j != end; ++j) {
196                         glm::dvec3 diff((*i)->GetSituation().Position() - (*j)->GetSituation().Position());
197                         double max_dist = ((*i)->Size() + (*j)->Size()) * 1.74;
198                         if (length2(diff) > max_dist * max_dist) continue;
199                         math::AABB j_box((*j)->CollisionBox());
200                         glm::dmat4 j_mat((*j)->CollisionTransform());
201                         glm::dvec3 normal;
202                         double depth;
203                         if (Intersect(i_box, i_mat, j_box, j_mat, normal, depth)) {
204                                 collisions.push_back({ **i, **j, normal, depth });
205                         }
206                 }
207         }
208         for (auto &c : collisions) {
209                 c.A().GetSituation().Move(c.Normal() * (c.Depth() * -0.5));
210                 c.B().GetSituation().Move(c.Normal() * (c.Depth() * 0.5));
211                 // TODO: adjust velocities as well
212                 // TODO: notify participants so they can be annoyed
213         }
214 }
215
216 void Body::AddCreature(creature::Creature *c) {
217         creatures.push_back(c);
218 }
219
220 void Body::RemoveCreature(creature::Creature *c) {
221         auto entry = std::find(creatures.begin(), creatures.end(), c);
222         if (entry != creatures.end()) {
223                 creatures.erase(entry);
224         }
225 }
226
227
228 CreatureCreatureCollision::~CreatureCreatureCollision() {
229 }
230
231 const glm::dvec3 &CreatureCreatureCollision::APos() const noexcept {
232         return a->GetSituation().Position();
233 }
234
235 const glm::dvec3 &CreatureCreatureCollision::BPos() const noexcept {
236         return b->GetSituation().Position();
237 }
238
239
240 Orbit::Orbit()
241 : sma(1.0)
242 , ecc(0.0)
243 , inc(0.0)
244 , asc(0.0)
245 , arg(0.0)
246 , mna(0.0) {
247 }
248
249 Orbit::~Orbit() {
250 }
251
252 double Orbit::SemiMajorAxis() const noexcept {
253         return sma;
254 }
255
256 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
257         sma = s;
258         return *this;
259 }
260
261 double Orbit::Eccentricity() const noexcept {
262         return ecc;
263 }
264
265 Orbit &Orbit::Eccentricity(double e) noexcept {
266         ecc = e;
267         return *this;
268 }
269
270 double Orbit::Inclination() const noexcept {
271         return inc;
272 }
273
274 Orbit &Orbit::Inclination(double i) noexcept {
275         inc = i;
276         return *this;
277 }
278
279 double Orbit::LongitudeAscending() const noexcept {
280         return asc;
281 }
282
283 Orbit &Orbit::LongitudeAscending(double l) noexcept {
284         asc = l;
285         return *this;
286 }
287
288 double Orbit::ArgumentPeriapsis() const noexcept {
289         return arg;
290 }
291
292 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
293         arg = a;
294         return *this;
295 }
296
297 double Orbit::MeanAnomaly() const noexcept {
298         return mna;
299 }
300
301 Orbit &Orbit::MeanAnomaly(double m) noexcept {
302         mna = m;
303         return *this;
304 }
305
306 namespace {
307
308 double mean2eccentric(double M, double e) {
309         double E = M; // eccentric anomaly, solve M = E - e sin E
310         // limit to 100 steps to prevent deadlocks in impossible situations
311         for (int i = 0; i < 100; ++i) {
312                 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
313                 E -= dE;
314                 if (abs(dE) < 1.0e-6) break;
315         }
316         return E;
317 }
318
319 }
320
321 glm::dmat4 Orbit::Matrix(double t) const noexcept {
322         double M = mna + t;
323         double E = mean2eccentric(M, ecc);
324
325         // coordinates in orbital plane, P=x, Q=-z
326         double P = sma * (cos(E) - ecc);
327         double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
328
329         return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
330 }
331
332 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
333         double M = mna + t;
334         double E = mean2eccentric(M, ecc);
335         double P = sma * (cos(E) - ecc);
336         double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
337         return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
338 }
339
340
341 Planet::Planet(int sidelength)
342 : Body()
343 , sidelength(sidelength)
344 , tiles(TilesTotal())
345 , vao() {
346         Radius(double(sidelength) / 2.0);
347 }
348
349 Planet::~Planet() {
350 }
351
352 const TileType &Planet::TypeAt(int srf, int x, int y) const {
353         return GetSimulation().TileTypes()[TileAt(srf, x, y).type];
354 }
355
356 glm::ivec2 Planet::SurfacePosition(int srf, const glm::dvec3 &pos) const noexcept {
357         return glm::ivec2(
358                 PositionToTile(pos[(srf + 0) % 3]),
359                 PositionToTile(pos[(srf + 1) % 3]));
360 }
361
362 double Planet::SurfaceElevation(int srf, const glm::dvec3 &pos) const noexcept {
363         return srf < 3
364                 ? pos[(srf + 2) % 3] - Radius()
365                 : -pos[(srf + 2) % 3] - Radius();
366 }
367
368 glm::dvec3 Planet::TileCenter(int srf, int x, int y, double e) const noexcept {
369         glm::dvec3 center(0.0f);
370         center[(srf + 0) % 3] = x + 0.5 - Radius();
371         center[(srf + 1) % 3] = y + 0.5 - Radius();
372         center[(srf + 2) % 3] = srf < 3 ? (Radius() + e) : -(Radius() + e);
373         return center;
374 }
375
376 void Planet::BuildVAO(const Set<TileType> &ts) {
377         vao.reset(new graphics::SimpleVAO<Attributes, unsigned int>);
378         vao->Bind();
379         vao->BindAttributes();
380         vao->EnableAttribute(0);
381         vao->EnableAttribute(1);
382         vao->AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
383         vao->AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
384         vao->ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
385         {
386                 auto attrib = vao->MapAttributes(GL_WRITE_ONLY);
387                 float offset = Radius();
388
389                 // srf  0  1  2  3  4  5
390                 //  up +Z +X +Y -Z -X -Y
391
392                 for (int index = 0, surface = 0; surface < 6; ++surface) {
393                         for (int y = 0; y < sidelength; ++y) {
394                                 for (int x = 0; x < sidelength; ++x, ++index) {
395                                         float tex = ts[TileAt(surface, x, y).type].texture;
396                                         const float tex_v_begin = surface < 3 ? 1.0f : 0.0f;
397                                         const float tex_v_end = surface < 3 ? 0.0f : 1.0f;
398                                         attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
399                                         attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
400                                         attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
401                                         attrib[4 * index + 0].tex_coord[0] = 0.0f;
402                                         attrib[4 * index + 0].tex_coord[1] = tex_v_begin;
403                                         attrib[4 * index + 0].tex_coord[2] = tex;
404
405                                         attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
406                                         attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
407                                         attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
408                                         attrib[4 * index + 1].tex_coord[0] = 0.0f;
409                                         attrib[4 * index + 1].tex_coord[1] = tex_v_end;
410                                         attrib[4 * index + 1].tex_coord[2] = tex;
411
412                                         attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
413                                         attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
414                                         attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
415                                         attrib[4 * index + 2].tex_coord[0] = 1.0f;
416                                         attrib[4 * index + 2].tex_coord[1] = tex_v_begin;
417                                         attrib[4 * index + 2].tex_coord[2] = tex;
418
419                                         attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
420                                         attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
421                                         attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
422                                         attrib[4 * index + 3].tex_coord[0] = 1.0f;
423                                         attrib[4 * index + 3].tex_coord[1] = tex_v_end;
424                                         attrib[4 * index + 3].tex_coord[2] = tex;
425                                 }
426                         }
427                 }
428         }
429         vao->BindElements();
430         vao->ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
431         {
432                 auto element = vao->MapElements(GL_WRITE_ONLY);
433                 int index = 0;
434                 for (int surface = 0; surface < 3; ++surface) {
435                         for (int y = 0; y < sidelength; ++y) {
436                                 for (int x = 0; x < sidelength; ++x, ++index) {
437                                         element[6 * index + 0] = 4 * index + 0;
438                                         element[6 * index + 1] = 4 * index + 2;
439                                         element[6 * index + 2] = 4 * index + 1;
440                                         element[6 * index + 3] = 4 * index + 1;
441                                         element[6 * index + 4] = 4 * index + 2;
442                                         element[6 * index + 5] = 4 * index + 3;
443                                 }
444                         }
445                 }
446                 for (int surface = 3; surface < 6; ++surface) {
447                         for (int y = 0; y < sidelength; ++y) {
448                                 for (int x = 0; x < sidelength; ++x, ++index) {
449                                         element[6 * index + 0] = 4 * index + 0;
450                                         element[6 * index + 1] = 4 * index + 1;
451                                         element[6 * index + 2] = 4 * index + 2;
452                                         element[6 * index + 3] = 4 * index + 2;
453                                         element[6 * index + 4] = 4 * index + 1;
454                                         element[6 * index + 5] = 4 * index + 3;
455                                 }
456                         }
457                 }
458         }
459         vao->Unbind();
460 }
461
462 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
463         if (!vao) return;
464
465         vao->Bind();
466         const glm::mat4 &MV = assets.shaders.planet_surface.MV();
467         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
468         vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
469         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
470         vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
471         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
472         vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
473         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
474         vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
475         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
476         vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
477         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
478         vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
479 }
480
481
482 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
483         math::SimplexNoise elevation_gen(0);
484         math::SimplexNoise variation_gen(45623752346);
485
486         const int ice = tiles["ice"].id;
487         const int ocean = tiles["ocean"].id;
488         const int water = tiles["water"].id;
489         const int sand = tiles["sand"].id;
490         const int grass = tiles["grass"].id;
491         const int tundra = tiles["tundra"].id;
492         const int taiga = tiles["taiga"].id;
493         const int desert = tiles["desert"].id;
494         const int mntn = tiles["mountain"].id;
495         const int algae = tiles["algae"].id;
496         const int forest = tiles["forest"].id;
497         const int jungle = tiles["jungle"].id;
498         const int rock = tiles["rock"].id;
499         const int wheat = tiles["wheat"].id;
500
501         constexpr double ocean_thresh = -0.2;
502         constexpr double water_thresh = 0.0;
503         constexpr double beach_thresh = 0.05;
504         constexpr double highland_thresh = 0.4;
505         constexpr double mountain_thresh = 0.5;
506
507         const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
508         const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
509         const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
510         const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
511         const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
512
513         for (int surface = 0; surface <= 5; ++surface) {
514                 for (int y = 0; y < p.SideLength(); ++y) {
515                         for (int x = 0; x < p.SideLength(); ++x) {
516                                 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
517                                 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
518                                 if (near_axis > cap_thresh) {
519                                         p.TileAt(surface, x, y).type = ice;
520                                         continue;
521                                 }
522                                 float elevation = math::OctaveNoise(
523                                         elevation_gen,
524                                         to_tile / p.Radius(),
525                                         3,   // octaves
526                                         0.5, // persistence
527                                         5 / p.Radius(), // frequency
528                                         2,   // amplitude
529                                         2    // growth
530                                 );
531                                 float variation = math::OctaveNoise(
532                                         variation_gen,
533                                         to_tile / p.Radius(),
534                                         3,   // octaves
535                                         0.5, // persistence
536                                         16 / p.Radius(), // frequency
537                                         2,   // amplitude
538                                         2    // growth
539                                 );
540                                 if (elevation < ocean_thresh) {
541                                         p.TileAt(surface, x, y).type = ocean;
542                                 } else if (elevation < water_thresh) {
543                                         if (variation > 0.3) {
544                                                 p.TileAt(surface, x, y).type = algae;
545                                         } else {
546                                                 p.TileAt(surface, x, y).type = water;
547                                         }
548                                 } else if (elevation < beach_thresh) {
549                                         p.TileAt(surface, x, y).type = sand;
550                                 } else if (elevation < highland_thresh) {
551                                         if (near_axis < equ_thresh) {
552                                                 if (variation > 0.6) {
553                                                         p.TileAt(surface, x, y).type = grass;
554                                                 } else if (variation > 0.2) {
555                                                         p.TileAt(surface, x, y).type = sand;
556                                                 } else {
557                                                         p.TileAt(surface, x, y).type = desert;
558                                                 }
559                                         } else if (near_axis < fzone_start) {
560                                                 if (variation > 0.4) {
561                                                         p.TileAt(surface, x, y).type = forest;
562                                                 } else if (variation < -0.5) {
563                                                         p.TileAt(surface, x, y).type = jungle;
564                                                 } else if (variation > -0.02 && variation < 0.02) {
565                                                         p.TileAt(surface, x, y).type = wheat;
566                                                 } else {
567                                                         p.TileAt(surface, x, y).type = grass;
568                                                 }
569                                         } else if (near_axis < fzone_end) {
570                                                 p.TileAt(surface, x, y).type = tundra;
571                                         } else {
572                                                 p.TileAt(surface, x, y).type = taiga;
573                                         }
574                                 } else if (elevation < mountain_thresh) {
575                                         if (variation > 0.3) {
576                                                 p.TileAt(surface, x, y).type = mntn;
577                                         } else {
578                                                 p.TileAt(surface, x, y).type = rock;
579                                         }
580                                 } else {
581                                         p.TileAt(surface, x, y).type = mntn;
582                                 }
583                         }
584                 }
585         }
586         p.BuildVAO(tiles);
587 }
588
589 void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
590         for (int surface = 0; surface <= 5; ++surface) {
591                 for (int y = 0; y < p.SideLength(); ++y) {
592                         for (int x = 0; x < p.SideLength(); ++x) {
593                                 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
594                                         p.TileAt(surface, x, y).type = surface;
595                                 } else {
596                                         p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
597                                 }
598                         }
599                 }
600         }
601         p.BuildVAO(tiles);
602 }
603
604
605 Sun::Sun()
606 : Body() {
607 }
608
609 Sun::~Sun() {
610 }
611
612
613 std::vector<TileType::Yield>::const_iterator TileType::FindResource(int r) const {
614         auto yield = resources.cbegin();
615         for (; yield != resources.cend(); ++yield) {
616                 if (yield->resource == r) {
617                         break;
618                 }
619         }
620         return yield;
621 }
622
623 std::vector<TileType::Yield>::const_iterator TileType::FindBestResource(const creature::Composition &comp) const {
624         auto best = resources.cend();
625         double best_value = 0.0;
626         for (auto yield = resources.cbegin(); yield != resources.cend(); ++yield) {
627                 double value = comp.Get(yield->resource);
628                 if (value > best_value) {
629                         best = yield;
630                         best_value = value;
631                 }
632         }
633         return best;
634 }
635
636 }
637 }