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