]> git.localhorst.tv Git - blobs.git/blobdiff - src/world/world.cpp
eat what's here
[blobs.git] / src / world / world.cpp
index c9a7a326f3d94a2f4bc496e4100da541df82e794..449307abeefbeeb88ea148171600aed8b2e1a596 100644 (file)
@@ -1,18 +1,26 @@
 #include "Body.hpp"
 #include "Orbit.hpp"
 #include "Planet.hpp"
+#include "Resource.hpp"
+#include "Set.hpp"
 #include "Simulation.hpp"
 #include "Sun.hpp"
 #include "Tile.hpp"
+#include "TileType.hpp"
 
 #include "../const.hpp"
 #include "../app/Assets.hpp"
+#include "../creature/Creature.hpp"
 #include "../graphics/Viewport.hpp"
+#include "../rand/OctaveNoise.hpp"
+#include "../rand/SimplexNoise.hpp"
 
 #include <algorithm>
 #include <cmath>
+#include <iostream>
 #include <glm/gtc/matrix_transform.hpp>
 #include <glm/gtx/euler_angles.hpp>
+#include <glm/gtx/io.hpp>
 #include <glm/gtx/transform.hpp>
 
 using blobs::G;
@@ -41,10 +49,15 @@ Body::Body()
 , orbital(1.0)
 , inverse_orbital(1.0)
 , local(1.0)
-, inverse_local(1.0) {
+, inverse_local(1.0)
+, creatures()
+, atmosphere(-1) {
 }
 
 Body::~Body() {
+       for (creature::Creature *c : creatures) {
+               delete c;
+       }
 }
 
 void Body::SetSimulation(Simulation &s) noexcept {
@@ -125,6 +138,14 @@ glm::dmat4 Body::FromUniverse() const noexcept {
        return m;
 }
 
+void Body::Tick(double dt) {
+       rotation += dt * AngularMomentum() / Inertia();
+       Cache();
+       for (creature::Creature *c : Creatures()) {
+               c->Tick(dt);
+       }
+}
+
 void Body::Cache() noexcept {
        if (parent) {
                orbital =
@@ -145,6 +166,17 @@ void Body::Cache() noexcept {
                * glm::eulerAngleY(-rotation);
 }
 
+void Body::AddCreature(creature::Creature *c) {
+       creatures.push_back(c);
+}
+
+void Body::RemoveCreature(creature::Creature *c) {
+       auto entry = std::find(creatures.begin(), creatures.end(), c);
+       if (entry != creatures.end()) {
+               creatures.erase(entry);
+       }
+}
+
 
 Orbit::Orbit()
 : sma(1.0)
@@ -235,7 +267,7 @@ glm::dmat4 Orbit::Matrix(double t) const noexcept {
        double P = sma * (cos(E) - ecc);
        double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
 
-       return glm::translate(glm::yawPitchRoll(asc, inc, arg), glm::dvec3(P, 0.0, -Q));
+       return glm::yawPitchRoll(asc, inc, arg) * glm::translate(glm::dvec3(P, 0.0, -Q));
 }
 
 glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
@@ -243,14 +275,14 @@ glm::dmat4 Orbit::InverseMatrix(double t) const noexcept {
        double E = mean2eccentric(M, ecc);
        double P = sma * (cos(E) - ecc);
        double Q = sma * sin(E) * sqrt(1 - (ecc * ecc));
-       return glm::transpose(glm::yawPitchRoll(asc, inc, arg)) * glm::translate(glm::dvec3(-P, 0.0, Q));
+       return glm::translate(glm::dvec3(-P, 0.0, Q)) * glm::transpose(glm::yawPitchRoll(asc, inc, arg));
 }
 
 
 Planet::Planet(int sidelength)
 : Body()
 , sidelength(sidelength)
-, tiles(new Tile[TilesTotal()])
+, tiles(TilesTotal())
 , vao() {
        Radius(double(sidelength) / 2.0);
 }
@@ -258,7 +290,19 @@ Planet::Planet(int sidelength)
 Planet::~Planet() {
 }
 
-void Planet::BuildVAOs() {
+const TileType &Planet::TypeAt(int surface, int x, int y) const {
+       return GetSimulation().TileTypes()[TileAt(surface, x, y).type];
+}
+
+glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
+       glm::dvec3 center(0.0f);
+       center[(surface + 0) % 3] = x + 0.5 - Radius();
+       center[(surface + 1) % 3] = y + 0.5 - Radius();
+       center[(surface + 2) % 3] = surface < 3 ? Radius() : -Radius();
+       return center;
+}
+
+void Planet::BuildVAO(const Set<TileType> &ts) {
        vao.Bind();
        vao.BindAttributes();
        vao.EnableAttribute(0);
@@ -268,7 +312,7 @@ void Planet::BuildVAOs() {
        vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
        {
                auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
-               float offset = sidelength * 0.5f;
+               float offset = Radius();
 
                // srf  0  1  2  3  4  5
                //  up +Z +X +Y -Z -X -Y
@@ -276,33 +320,35 @@ void Planet::BuildVAOs() {
                for (int index = 0, surface = 0; surface < 6; ++surface) {
                        for (int y = 0; y < sidelength; ++y) {
                                for (int x = 0; x < sidelength; ++x, ++index) {
-                                       float tex = TileAt(surface, x, y).type;
+                                       float tex = ts[TileAt(surface, x, y).type].texture;
+                                       const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
+                                       const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
                                        attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset;
                                        attrib[4 * index + 0].position[(surface + 1) % 3] = y + 0 - offset;
                                        attrib[4 * index + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       attrib[4 * index + 0].tex_coord[0] = 0.0f;
-                                       attrib[4 * index + 0].tex_coord[1] = 0.0f;
+                                       attrib[4 * index + 0].tex_coord[0] = tex_u_begin;
+                                       attrib[4 * index + 0].tex_coord[1] = 1.0f;
                                        attrib[4 * index + 0].tex_coord[2] = tex;
 
                                        attrib[4 * index + 1].position[(surface + 0) % 3] = x + 0 - offset;
                                        attrib[4 * index + 1].position[(surface + 1) % 3] = y + 1 - offset;
                                        attrib[4 * index + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       attrib[4 * index + 1].tex_coord[0] = 0.0f;
+                                       attrib[4 * index + 1].tex_coord[0] = tex_u_end;
                                        attrib[4 * index + 1].tex_coord[1] = 1.0f;
                                        attrib[4 * index + 1].tex_coord[2] = tex;
 
                                        attrib[4 * index + 2].position[(surface + 0) % 3] = x + 1 - offset;
                                        attrib[4 * index + 2].position[(surface + 1) % 3] = y + 0 - offset;
                                        attrib[4 * index + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       attrib[4 * index + 2].tex_coord[0] = 1.0f;
+                                       attrib[4 * index + 2].tex_coord[0] = tex_u_begin;
                                        attrib[4 * index + 2].tex_coord[1] = 0.0f;
                                        attrib[4 * index + 2].tex_coord[2] = tex;
 
                                        attrib[4 * index + 3].position[(surface + 0) % 3] = x + 1 - offset;
                                        attrib[4 * index + 3].position[(surface + 1) % 3] = y + 1 - offset;
                                        attrib[4 * index + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
-                                       attrib[4 * index + 3].tex_coord[0] = 1.0f;
-                                       attrib[4 * index + 3].tex_coord[1] = 1.0f;
+                                       attrib[4 * index + 3].tex_coord[0] = tex_u_end;
+                                       attrib[4 * index + 3].tex_coord[1] = 0.0f;
                                        attrib[4 * index + 3].tex_coord[2] = tex;
                                }
                        }
@@ -359,15 +405,126 @@ void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
 }
 
 
-void GenerateTest(Planet &p) {
+void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
+       rand::SimplexNoise elevation_gen(0);
+       rand::SimplexNoise variation_gen(45623752346);
+
+       const int ice = tiles["ice"].id;
+       const int ocean = tiles["ocean"].id;
+       const int water = tiles["water"].id;
+       const int sand = tiles["sand"].id;
+       const int grass = tiles["grass"].id;
+       const int tundra = tiles["tundra"].id;
+       const int taiga = tiles["taiga"].id;
+       const int desert = tiles["desert"].id;
+       const int mntn = tiles["mountain"].id;
+       const int algae = tiles["algae"].id;
+       const int forest = tiles["forest"].id;
+       const int jungle = tiles["jungle"].id;
+       const int rock = tiles["rock"].id;
+       const int wheat = tiles["wheat"].id;
+
+       constexpr double ocean_thresh = -0.2;
+       constexpr double water_thresh = 0.0;
+       constexpr double beach_thresh = 0.05;
+       constexpr double highland_thresh = 0.4;
+       constexpr double mountain_thresh = 0.5;
+
+       const glm::dvec3 axis(glm::dvec4(0.0, 1.0, 0.0, 0.0) * glm::eulerAngleXY(p.SurfaceTilt().x, p.SurfaceTilt().y));
+       const double cap_thresh = std::abs(std::cos(p.AxialTilt().x));
+       const double equ_thresh = std::abs(std::sin(p.AxialTilt().x)) / 2.0;
+       const double fzone_start = equ_thresh - (equ_thresh - cap_thresh) / 3.0;
+       const double fzone_end = cap_thresh + (equ_thresh - cap_thresh) / 3.0;
+
+       for (int surface = 0; surface <= 5; ++surface) {
+               for (int y = 0; y < p.SideLength(); ++y) {
+                       for (int x = 0; x < p.SideLength(); ++x) {
+                               glm::dvec3 to_tile = p.TileCenter(surface, x, y);
+                               double near_axis = std::abs(glm::dot(glm::normalize(to_tile), axis));
+                               if (near_axis > cap_thresh) {
+                                       p.TileAt(surface, x, y).type = ice;
+                                       continue;
+                               }
+                               float elevation = rand::OctaveNoise(
+                                       elevation_gen,
+                                       to_tile / p.Radius(),
+                                       3,   // octaves
+                                       0.5, // persistence
+                                       5 / p.Radius(), // frequency
+                                       2,   // amplitude
+                                       2    // growth
+                               );
+                               float variation = rand::OctaveNoise(
+                                       variation_gen,
+                                       to_tile / p.Radius(),
+                                       3,   // octaves
+                                       0.5, // persistence
+                                       16 / p.Radius(), // frequency
+                                       2,   // amplitude
+                                       2    // growth
+                               );
+                               if (elevation < ocean_thresh) {
+                                       p.TileAt(surface, x, y).type = ocean;
+                               } else if (elevation < water_thresh) {
+                                       if (variation > 0.3) {
+                                               p.TileAt(surface, x, y).type = algae;
+                                       } else {
+                                               p.TileAt(surface, x, y).type = water;
+                                       }
+                               } else if (elevation < beach_thresh) {
+                                       p.TileAt(surface, x, y).type = sand;
+                               } else if (elevation < highland_thresh) {
+                                       if (near_axis < equ_thresh) {
+                                               if (variation > 0.6) {
+                                                       p.TileAt(surface, x, y).type = grass;
+                                               } else if (variation > 0.2) {
+                                                       p.TileAt(surface, x, y).type = sand;
+                                               } else {
+                                                       p.TileAt(surface, x, y).type = desert;
+                                               }
+                                       } else if (near_axis < fzone_start) {
+                                               if (variation > 0.4) {
+                                                       p.TileAt(surface, x, y).type = forest;
+                                               } else if (variation < -0.5) {
+                                                       p.TileAt(surface, x, y).type = jungle;
+                                               } else if (variation > -0.02 && variation < 0.02) {
+                                                       p.TileAt(surface, x, y).type = wheat;
+                                               } else {
+                                                       p.TileAt(surface, x, y).type = grass;
+                                               }
+                                       } else if (near_axis < fzone_end) {
+                                               p.TileAt(surface, x, y).type = tundra;
+                                       } else {
+                                               p.TileAt(surface, x, y).type = taiga;
+                                       }
+                               } else if (elevation < mountain_thresh) {
+                                       if (variation > 0.3) {
+                                               p.TileAt(surface, x, y).type = mntn;
+                                       } else {
+                                               p.TileAt(surface, x, y).type = rock;
+                                       }
+                               } else {
+                                       p.TileAt(surface, x, y).type = mntn;
+                               }
+                       }
+               }
+       }
+       p.BuildVAO(tiles);
+}
+
+void GenerateTest(const Set<TileType> &tiles, Planet &p) noexcept {
        for (int surface = 0; surface <= 5; ++surface) {
                for (int y = 0; y < p.SideLength(); ++y) {
                        for (int x = 0; x < p.SideLength(); ++x) {
-                               p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2);
+                               if (x == p.SideLength() / 2 && y == p.SideLength() / 2) {
+                                       p.TileAt(surface, x, y).type = surface;
+                               } else {
+                                       p.TileAt(surface, x, y).type = (x == p.SideLength()/2) + (y == p.SideLength()/2) + 6;
+                               }
                        }
                }
        }
-       p.BuildVAOs();
+       p.BuildVAO(tiles);
 }