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