]> git.localhorst.tv Git - blobs.git/blob - src/world/world.cpp
figure out our creature's metabolism
[blobs.git] / src / world / world.cpp
1 #include "Body.hpp"
2 #include "Orbit.hpp"
3 #include "Planet.hpp"
4 #include "Resource.hpp"
5 #include "Set.hpp"
6 #include "Simulation.hpp"
7 #include "Sun.hpp"
8 #include "Tile.hpp"
9 #include "TileType.hpp"
10
11 #include "Creature.hpp"
12 #include "../const.hpp"
13 #include "../app/Assets.hpp"
14 #include "../graphics/Viewport.hpp"
15 #include "../rand/OctaveNoise.hpp"
16 #include "../rand/SimplexNoise.hpp"
17
18 #include <algorithm>
19 #include <cmath>
20 #include <iostream>
21 #include <glm/gtc/matrix_transform.hpp>
22 #include <glm/gtx/euler_angles.hpp>
23 #include <glm/gtx/io.hpp>
24 #include <glm/gtx/transform.hpp>
25
26 using blobs::G;
27 using blobs::PI_2p0;
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 }
55
56 Body::~Body() {
57         for (Creature *c : creatures) {
58                 delete c;
59         }
60 }
61
62 void Body::SetSimulation(Simulation &s) noexcept {
63         sim = &s;
64         for (auto child : children) {
65                 child->SetSimulation(s);
66         }
67 }
68
69 void Body::SetParent(Body &p) {
70         if (HasParent()) {
71                 UnsetParent();
72         }
73         parent = &p;
74         parent->AddChild(*this);
75 }
76
77 void Body::UnsetParent() {
78         if (!HasParent()) return;
79         parent->RemoveChild(*this);
80         parent = nullptr;
81 }
82
83 void Body::AddChild(Body &c) {
84         children.push_back(&c);
85         c.SetSimulation(*sim);
86 }
87
88 void Body::RemoveChild(Body &c) {
89         auto entry = std::find(children.begin(), children.end(), &c);
90         if (entry != children.end()) {
91                 children.erase(entry);
92         }
93 }
94
95 double Body::Inertia() const noexcept {
96         // assume solid sphere for now
97         return (2.0/5.0) * Mass() * pow(Radius(), 2);
98 }
99
100 double Body::GravitationalParameter() const noexcept {
101         return G * Mass();
102 }
103
104 double Body::OrbitalPeriod() const noexcept {
105         if (parent) {
106                 return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
107         } else {
108                 return 0.0;
109         }
110 }
111
112 double Body::RotationalPeriod() const noexcept {
113         if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
114                 return std::numeric_limits<double>::infinity();
115         } else {
116                 return PI_2p0 * Inertia() / angular;
117         }
118 }
119
120 glm::dmat4 Body::ToUniverse() const noexcept {
121         glm::dmat4 m(1.0);
122         const Body *b = this;
123         while (b->HasParent()) {
124                 m = b->ToParent() * m;
125                 b = &b->Parent();
126         }
127         return m;
128 }
129
130 glm::dmat4 Body::FromUniverse() const noexcept {
131         glm::dmat4 m(1.0);
132         const Body *b = this;
133         while (b->HasParent()) {
134                 m *= b->FromParent();
135                 b = &b->Parent();
136         }
137         return m;
138 }
139
140 void Body::Cache() noexcept {
141         if (parent) {
142                 orbital =
143                         orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
144                         * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
145                 inverse_orbital =
146                         glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
147                         * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
148         } else {
149                 orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
150                 inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
151         }
152         local =
153                 glm::eulerAngleY(rotation)
154                 * glm::eulerAngleXY(surface_tilt.x, surface_tilt.y);
155         inverse_local =
156                 glm::eulerAngleYX(-surface_tilt.y, -surface_tilt.x)
157                 * glm::eulerAngleY(-rotation);
158 }
159
160 void Body::AddCreature(Creature *c) {
161         c->SetBody(*this);
162         creatures.push_back(c);
163 }
164
165
166 Orbit::Orbit()
167 : sma(1.0)
168 , ecc(0.0)
169 , inc(0.0)
170 , asc(0.0)
171 , arg(0.0)
172 , mna(0.0) {
173 }
174
175 Orbit::~Orbit() {
176 }
177
178 double Orbit::SemiMajorAxis() const noexcept {
179         return sma;
180 }
181
182 Orbit &Orbit::SemiMajorAxis(double s) noexcept {
183         sma = s;
184         return *this;
185 }
186
187 double Orbit::Eccentricity() const noexcept {
188         return ecc;
189 }
190
191 Orbit &Orbit::Eccentricity(double e) noexcept {
192         ecc = e;
193         return *this;
194 }
195
196 double Orbit::Inclination() const noexcept {
197         return inc;
198 }
199
200 Orbit &Orbit::Inclination(double i) noexcept {
201         inc = i;
202         return *this;
203 }
204
205 double Orbit::LongitudeAscending() const noexcept {
206         return asc;
207 }
208
209 Orbit &Orbit::LongitudeAscending(double l) noexcept {
210         asc = l;
211         return *this;
212 }
213
214 double Orbit::ArgumentPeriapsis() const noexcept {
215         return arg;
216 }
217
218 Orbit &Orbit::ArgumentPeriapsis(double a) noexcept {
219         arg = a;
220         return *this;
221 }
222
223 double Orbit::MeanAnomaly() const noexcept {
224         return mna;
225 }
226
227 Orbit &Orbit::MeanAnomaly(double m) noexcept {
228         mna = m;
229         return *this;
230 }
231
232 namespace {
233
234 double mean2eccentric(double M, double e) {
235         double E = M; // eccentric anomaly, solve M = E - e sin E
236         // limit to 100 steps to prevent deadlocks in impossible situations
237         for (int i = 0; i < 100; ++i) {
238                 double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
239                 E -= dE;
240                 if (abs(dE) < 1.0e-6) break;
241         }
242         return E;
243 }
244
245 }
246
247 glm::dmat4 Orbit::Matrix(double t) const noexcept {
248         double M = mna + t;
249         double E = mean2eccentric(M, ecc);
250
251         // coordinates in orbital plane, P=x, Q=-z
252         double P = sma * (cos(E) - ecc);
253         double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
254
255         return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
256 }
257
258 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
259         double M = mna + t;
260         double E = mean2eccentric(M, ecc);
261         double P = sma * (cos(E) - ecc);
262         double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
263         return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
264 }
265
266
267 Planet::Planet(int sidelength)
268 : Body()
269 , sidelength(sidelength)
270 , tiles(TilesTotal())
271 , atmosphere(-1)
272 , vao() {
273         Radius(double(sidelength) / 2.0);
274 }
275
276 Planet::~Planet() {
277 }
278
279 glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
280         glm::dvec3 center(0.0f);
281         center[(surface + 0) % 3] = x + 0.5 - Radius();
282         center[(surface + 1) % 3] = y + 0.5 - Radius();
283         center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
284         return center;
285 }
286
287 void Planet::BuildVAO(const Set<TileType> &ts) {
288         vao.Bind();
289         vao.BindAttributes();
290         vao.EnableAttribute(0);
291         vao.EnableAttribute(1);
292         vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
293         vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
294         vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
295         {
296                 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
297                 float offset = Radius();
298
299                 // srf  0  1  2  3  4  5
300                 //  up +Z +X +Y -Z -X -Y
301
302                 for (int index = 0, surface = 0; surface < 6; ++surface) {
303                         for (int y = 0; y < sidelength; ++y) {
304                                 for (int x = 0; x < sidelength; ++x, ++index) {
305                                         float tex = ts[TileAt(surface, x, y).type].texture;
306                                         const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
307                                         const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
308                                         attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
309                                         attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
310                                         attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
311                                         attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
312                                         attrib[4 * index + 0].tex_coord[1] = 1.0f;
313                                         attrib[4 * index + 0].tex_coord[2] = tex;
314
315                                         attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
316                                         attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
317                                         attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
318                                         attrib[4 * index + 1].tex_coord[0] = tex_u_end;
319                                         attrib[4 * index + 1].tex_coord[1] = 1.0f;
320                                         attrib[4 * index + 1].tex_coord[2] = tex;
321
322                                         attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
323                                         attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
324                                         attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
325                                         attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
326                                         attrib[4 * index + 2].tex_coord[1] = 0.0f;
327                                         attrib[4 * index + 2].tex_coord[2] = tex;
328
329                                         attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
330                                         attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
331                                         attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
332                                         attrib[4 * index + 3].tex_coord[0] = tex_u_end;
333                                         attrib[4 * index + 3].tex_coord[1] = 0.0f;
334                                         attrib[4 * index + 3].tex_coord[2] = tex;
335                                 }
336                         }
337                 }
338         }
339         vao.BindElements();
340         vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
341         {
342                 auto element = vao.MapElements(GL_WRITE_ONLY);
343                 int index = 0;
344                 for (int surface = 0; surface < 3; ++surface) {
345                         for (int y = 0; y < sidelength; ++y) {
346                                 for (int x = 0; x < sidelength; ++x, ++index) {
347                                         element[6 * index + 0] = 4 * index + 0;
348                                         element[6 * index + 1] = 4 * index + 2;
349                                         element[6 * index + 2] = 4 * index + 1;
350                                         element[6 * index + 3] = 4 * index + 1;
351                                         element[6 * index + 4] = 4 * index + 2;
352                                         element[6 * index + 5] = 4 * index + 3;
353                                 }
354                         }
355                 }
356                 for (int surface = 3; surface < 6; ++surface) {
357                         for (int y = 0; y < sidelength; ++y) {
358                                 for (int x = 0; x < sidelength; ++x, ++index) {
359                                         element[6 * index + 0] = 4 * index + 0;
360                                         element[6 * index + 1] = 4 * index + 1;
361                                         element[6 * index + 2] = 4 * index + 2;
362                                         element[6 * index + 3] = 4 * index + 2;
363                                         element[6 * index + 4] = 4 * index + 1;
364                                         element[6 * index + 5] = 4 * index + 3;
365                                 }
366                         }
367                 }
368         }
369         vao.Unbind();
370 }
371
372 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
373         vao.Bind();
374         const glm::mat4 &MV = assets.shaders.planet_surface.MV();
375         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
376         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
377         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
378         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
379         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
380         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
381         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
382         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
383         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
384         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
385         assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
386         vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
387 }
388
389
390 void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
391         rand::SimplexNoise elevation_gen(0);
392         rand::SimplexNoise variation_gen(45623752346);
393
394         const int ice = tiles["ice"].id;
395         const int ocean = tiles["ocean"].id;
396         const int water = tiles["water"].id;
397         const int sand = tiles["sand"].id;
398         const int grass = tiles["grass"].id;
399         const int tundra = tiles["tundra"].id;
400         const int taiga = tiles["taiga"].id;
401         const int desert = tiles["desert"].id;
402         const int mntn = tiles["mountain"].id;
403         const int algae = tiles["algae"].id;
404         const int forest = tiles["forest"].id;
405         const int jungle = tiles["jungle"].id;
406         const int rock = tiles["rock"].id;
407         const int wheat = tiles["wheat"].id;
408
409         constexpr double ocean_thresh = -0.2;
410         constexpr double water_thresh = 0.0;
411         constexpr double beach_thresh = 0.05;
412         constexpr double highland_thresh = 0.4;
413         constexpr double mountain_thresh = 0.5;
414
415         const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
416         const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
417         const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
418         const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
419         const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
420
421         for (int surface = 0; surface <= 5; ++surface) {
422                 for (int y = 0; y < p.SideLength(); ++y) {
423                         for (int x = 0; x < p.SideLength(); ++x) {
424                                 glm::dvec3 to_tile = p.TileCenter(surface, x, y);
425                                 double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
426                                 if (near_axis > cap_thresh) {
427                                         p.TileAt(surface, x, y).type = ice;
428                                         continue;
429                                 }
430                                 float elevation = rand::OctaveNoise(
431                                         elevation_gen,
432                                         to_tile / p.Radius(),
433                                         3,   // octaves
434                                         0.5, // persistence
435                                         5 / p.Radius(), // frequency
436                                         2,   // amplitude
437                                         2    // growth
438                                 );
439                                 float variation = rand::OctaveNoise(
440                                         variation_gen,
441                                         to_tile / p.Radius(),
442                                         3,   // octaves
443                                         0.5, // persistence
444                                         16 / p.Radius(), // frequency
445                                         2,   // amplitude
446                                         2    // growth
447                                 );
448                                 if (elevation < ocean_thresh) {
449                                         p.TileAt(surface, x, y).type = ocean;
450                                 } else if (elevation < water_thresh) {
451                                         if (variation > 0.3) {
452                                                 p.TileAt(surface, x, y).type = algae;
453                                         } else {
454                                                 p.TileAt(surface, x, y).type = water;
455                                         }
456                                 } else if (elevation < beach_thresh) {
457                                         p.TileAt(surface, x, y).type = sand;
458                                 } else if (elevation < highland_thresh) {
459                                         if (near_axis < equ_thresh) {
460                                                 if (variation > 0.6) {
461                                                         p.TileAt(surface, x, y).type = grass;
462                                                 } else if (variation > 0.2) {
463                                                         p.TileAt(surface, x, y).type = sand;
464                                                 } else {
465                                                         p.TileAt(surface, x, y).type = desert;
466                                                 }
467                                         } else if (near_axis < fzone_start) {
468                                                 if (variation > 0.4) {
469                                                         p.TileAt(surface, x, y).type = forest;
470                                                 } else if (variation < -0.5) {
471                                                         p.TileAt(surface, x, y).type = jungle;
472                                                 } else if (variation > -0.02 && variation < 0.02) {
473                                                         p.TileAt(surface, x, y).type = wheat;
474                                                 } else {
475                                                         p.TileAt(surface, x, y).type = grass;
476                                                 }
477                                         } else if (near_axis < fzone_end) {
478                                                 p.TileAt(surface, x, y).type = tundra;
479                                         } else {
480                                                 p.TileAt(surface, x, y).type = taiga;
481                                         }
482                                 } else if (elevation < mountain_thresh) {
483                                         if (variation > 0.3) {
484                                                 p.TileAt(surface, x, y).type = mntn;
485                                         } else {
486                                                 p.TileAt(surface, x, y).type = rock;
487                                         }
488                                 } else {
489                                         p.TileAt(surface, x, y).type = mntn;
490                                 }
491                         }
492                 }
493         }
494         p.BuildVAO(tiles);
495 }
496
497 void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
498         for (int surface = 0; surface <= 5; ++surface) {
499                 for (int y = 0; y < p.SideLength(); ++y) {
500                         for (int x = 0; x < p.SideLength(); ++x) {
501                                 if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
502                                         p.TileAt(surface, x, y).type = surface;
503                                 } else {
504                                         p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
505                                 }
506                         }
507                 }
508         }
509         p.BuildVAO(tiles);
510 }
511
512
513 Sun::Sun()
514 : Body() {
515 }
516
517 Sun::~Sun() {
518 }
519
520 }
521 }