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