]> git.localhorst.tv Git - blobs.git/blobdiff - src/world/world.cpp
track a few things
[blobs.git] / src / world / world.cpp
index a3494b3024f700ca7d78f24e0e5426ed3a7ac515..b80be094129011d029098b23b5986753e61d5f2d 100644 (file)
@@ -1,18 +1,22 @@
 #include "Body.hpp"
+#include "CreatureCreatureCollision.hpp"
 #include "Orbit.hpp"
 #include "Planet.hpp"
+#include "Resource.hpp"
+#include "Set.hpp"
 #include "Simulation.hpp"
 #include "Sun.hpp"
 #include "Tile.hpp"
-#include "TileSet.hpp"
 #include "TileType.hpp"
 
-#include "Creature.hpp"
-#include "../const.hpp"
 #include "../app/Assets.hpp"
+#include "../creature/Composition.hpp"
+#include "../creature/Creature.hpp"
 #include "../graphics/Viewport.hpp"
-#include "../rand/OctaveNoise.hpp"
-#include "../rand/SimplexNoise.hpp"
+#include "../math/const.hpp"
+#include "../math/geometry.hpp"
+#include "../math/OctaveNoise.hpp"
+#include "../math/SimplexNoise.hpp"
 
 #include <algorithm>
 #include <cmath>
@@ -49,13 +53,11 @@ Body::Body()
 , inverse_orbital(1.0)
 , local(1.0)
 , inverse_local(1.0)
-, creatures() {
+, creatures()
+, atmosphere(-1) {
 }
 
 Body::~Body() {
-       for (Creature *c : creatures) {
-               delete c;
-       }
 }
 
 void Body::SetSimulation(Simulation &s) noexcept {
@@ -136,6 +138,30 @@ glm::dmat4 Body::FromUniverse() const noexcept {
        return m;
 }
 
+namespace {
+std::vector<creature::Creature *> ccache;
+std::vector<CreatureCreatureCollision> collisions;
+}
+
+void Body::Tick(double dt) {
+       rotation += dt * AngularMomentum() / Inertia();
+       Cache();
+       ccache = Creatures();
+       for (creature::Creature *c : ccache) {
+               c->Tick(dt);
+       }
+       // first remove creatures so they don't collide
+       for (auto c = Creatures().begin(); c != Creatures().end();) {
+               if ((*c)->Removable()) {
+                       (*c)->Removed();
+                       c = Creatures().erase(c);
+               } else {
+                       ++c;
+               }
+       }
+       CheckCollision();
+}
+
 void Body::Cache() noexcept {
        if (parent) {
                orbital =
@@ -156,11 +182,66 @@ void Body::Cache() noexcept {
                * glm::eulerAngleY(-rotation);
 }
 
-void Body::AddCreature(Creature *c) {
-       c->SetBody(*this);
+void Body::CheckCollision() noexcept {
+       if (Creatures().size() < 2) return;
+       collisions.clear();
+       auto end = Creatures().end();
+       for (auto i = Creatures().begin(); i != end; ++i) {
+               math::AABB i_box((*i)->CollisionBox());
+               glm::dmat4 i_mat((*i)->CollisionTransform());
+               for (auto j = (i + 1); j != end; ++j) {
+                       glm::dvec3 diff((*i)->GetSituation().Position() - (*j)->GetSituation().Position());
+                       double max_dist = ((*i)->Size() + (*j)->Size()) * 1.74;
+                       if (length2(diff) > max_dist * max_dist) continue;
+                       math::AABB j_box((*j)->CollisionBox());
+                       glm::dmat4 j_mat((*j)->CollisionTransform());
+                       glm::dvec3 normal;
+                       double depth;
+                       if (Intersect(i_box, i_mat, j_box, j_mat, normal, depth)) {
+                               collisions.push_back({ **i, **j, normal, depth });
+                       }
+               }
+       }
+       for (auto &c : collisions) {
+               c.A().GetSituation().Move(c.Normal() * (c.Depth() * -0.5));
+               c.B().GetSituation().Move(c.Normal() * (c.Depth() * 0.5));
+               c.A().GetSituation().Accelerate(c.Normal() * -dot(c.Normal(), c.AVel()));
+               c.B().GetSituation().Accelerate(c.Normal() * -dot(c.Normal(), c.BVel()));
+               // TODO: notify participants so they can be annoyed
+       }
+}
+
+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);
+       }
+}
+
+
+CreatureCreatureCollision::~CreatureCreatureCollision() {
+}
+
+const glm::dvec3 &CreatureCreatureCollision::APos() const noexcept {
+       return a->GetSituation().Position();
+}
+
+const glm::dvec3 &CreatureCreatureCollision::AVel() const noexcept {
+       return a->GetSituation().Velocity();
+}
+
+const glm::dvec3 &CreatureCreatureCollision::BPos() const noexcept {
+       return b->GetSituation().Position();
+}
+
+const glm::dvec3 &CreatureCreatureCollision::BVel() const noexcept {
+       return b->GetSituation().Velocity();
+}
+
 
 Orbit::Orbit()
 : sma(1.0)
@@ -274,24 +355,41 @@ Planet::Planet(int sidelength)
 Planet::~Planet() {
 }
 
-glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept {
+const TileType &Planet::TypeAt(int srf, int x, int y) const {
+       return GetSimulation().TileTypes()[TileAt(srf, x, y).type];
+}
+
+glm::ivec2 Planet::SurfacePosition(int srf, const glm::dvec3 &pos) const noexcept {
+       return glm::ivec2(
+               PositionToTile(pos[(srf + 0) % 3]),
+               PositionToTile(pos[(srf + 1) % 3]));
+}
+
+double Planet::SurfaceElevation(int srf, const glm::dvec3 &pos) const noexcept {
+       return srf < 3
+               ? pos[(srf + 2) % 3] - Radius()
+               : -pos[(srf + 2) % 3] - Radius();
+}
+
+glm::dvec3 Planet::TileCenter(int srf, int x, int y, double e) 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();
+       center[(srf + 0) % 3] = x + 0.5 - Radius();
+       center[(srf + 1) % 3] = y + 0.5 - Radius();
+       center[(srf + 2) % 3] = srf < 3 ? (Radius() + e) : -(Radius() + e);
        return center;
 }
 
-void Planet::BuildVAO(const TileSet &ts) {
-       vao.Bind();
-       vao.BindAttributes();
-       vao.EnableAttribute(0);
-       vao.EnableAttribute(1);
-       vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
-       vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
-       vao.ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
+void Planet::BuildVAO(const Set<TileType> &ts) {
+       vao.reset(new graphics::SimpleVAO<Attributes, unsigned int>);
+       vao->Bind();
+       vao->BindAttributes();
+       vao->EnableAttribute(0);
+       vao->EnableAttribute(1);
+       vao->AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
+       vao->AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
+       vao->ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
        {
-               auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
+               auto attrib = vao->MapAttributes(GL_WRITE_ONLY);
                float offset = Radius();
 
                // srf  0  1  2  3  4  5
@@ -301,43 +399,43 @@ void Planet::BuildVAO(const TileSet &ts) {
                        for (int y = 0; y < sidelength; ++y) {
                                for (int x = 0; x < sidelength; ++x, ++index) {
                                        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;
+                                       const float tex_v_begin = surface < 3 ? 1.0f : 0.0f;
+                                       const float tex_v_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] = tex_u_begin;
-                                       attrib[4 * index + 0].tex_coord[1] = 1.0f;
+                                       attrib[4 * index + 0].tex_coord[0] = 0.0f;
+                                       attrib[4 * index + 0].tex_coord[1] = tex_v_begin;
                                        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] = tex_u_end;
-                                       attrib[4 * index + 1].tex_coord[1] = 1.0f;
+                                       attrib[4 * index + 1].tex_coord[0] = 0.0f;
+                                       attrib[4 * index + 1].tex_coord[1] = tex_v_end;
                                        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] = tex_u_begin;
-                                       attrib[4 * index + 2].tex_coord[1] = 0.0f;
+                                       attrib[4 * index + 2].tex_coord[0] = 1.0f;
+                                       attrib[4 * index + 2].tex_coord[1] = tex_v_begin;
                                        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] = tex_u_end;
-                                       attrib[4 * index + 3].tex_coord[1] = 0.0f;
+                                       attrib[4 * index + 3].tex_coord[0] = 1.0f;
+                                       attrib[4 * index + 3].tex_coord[1] = tex_v_end;
                                        attrib[4 * index + 3].tex_coord[2] = tex;
                                }
                        }
                }
        }
-       vao.BindElements();
-       vao.ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
+       vao->BindElements();
+       vao->ReserveElements(TilesTotal() * 6, GL_STATIC_DRAW);
        {
-               auto element = vao.MapElements(GL_WRITE_ONLY);
+               auto element = vao->MapElements(GL_WRITE_ONLY);
                int index = 0;
                for (int surface = 0; surface < 3; ++surface) {
                        for (int y = 0; y < sidelength; ++y) {
@@ -364,29 +462,32 @@ void Planet::BuildVAO(const TileSet &ts) {
                        }
                }
        }
-       vao.Unbind();
+       vao->Unbind();
 }
 
 void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
-       vao.Bind();
+       if (!vao) return;
+
+       vao->Bind();
        const glm::mat4 &MV = assets.shaders.planet_surface.MV();
        assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, 1.0f, 0.0f)));
-       vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
+       vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 0);
        assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(1.0f, 0.0f, 0.0f, 0.0f)));
-       vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
+       vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 1);
        assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 1.0f, 0.0f, 0.0f)));
-       vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
+       vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 2);
        assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f)));
-       vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
+       vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 3);
        assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(-1.0f, 0.0f, 0.0f, 0.0f)));
-       vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
+       vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 4);
        assets.shaders.planet_surface.SetNormal(glm::vec3(MV * glm::vec4(0.0f, -1.0f, 0.0f, 0.0f)));
-       vao.DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
+       vao->DrawTriangles(TilesPerSurface() * 6, TilesPerSurface() * 6 * 5);
 }
 
 
-void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept {
-       rand::SimplexNoise elevation_gen(0);
+void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
+       math::SimplexNoise elevation_gen(0);
+       math::SimplexNoise variation_gen(45623752346);
 
        const int ice = tiles["ice"].id;
        const int ocean = tiles["ocean"].id;
@@ -394,17 +495,24 @@ void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept {
        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.1;
+       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 = 2.0 * (1.0 - cap_thresh);
+       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;
 
@@ -417,31 +525,63 @@ void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept {
                                        p.TileAt(surface, x, y).type = ice;
                                        continue;
                                }
-                               float elevation = rand::OctaveNoise(
+                               float elevation = math::OctaveNoise(
                                        elevation_gen,
                                        to_tile / p.Radius(),
                                        3,   // octaves
                                        0.5, // persistence
-                                       2 / p.Radius(), // frequency
+                                       5 / p.Radius(), // frequency
+                                       2,   // amplitude
+                                       2    // growth
+                               );
+                               float variation = math::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) {
-                                       p.TileAt(surface, x, y).type = water;
+                                       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 < mountain_thresh) {
-                                       // TODO: perturb climate rings a little
+                               } else if (elevation < highland_thresh) {
                                        if (near_axis < equ_thresh) {
-                                               p.TileAt(surface, x, y).type = desert;
+                                               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) {
-                                               p.TileAt(surface, x, y).type = grass;
+                                               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 = grass;
+                                               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;
@@ -452,7 +592,7 @@ void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept {
        p.BuildVAO(tiles);
 }
 
-void GenerateTest(const TileSet &tiles, Planet &p) noexcept {
+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) {
@@ -475,40 +615,28 @@ Sun::Sun()
 Sun::~Sun() {
 }
 
-TileSet::TileSet()
-: types()
-, names() {
-}
-
-TileSet::~TileSet() {
-}
-
-int TileSet::Add(const TileType &t) {
-       int id = types.size();
-       if (!names.emplace(t.name, id).second) {
-               throw std::runtime_error("duplicate tile type name " + t.name);
-       }
-       types.emplace_back(t);
-       types.back().id = id;
-       return id;
-}
 
-TileType &TileSet::operator [](const std::string &name) {
-       auto entry = names.find(name);
-       if (entry != names.end()) {
-               return types[entry->second];
-       } else {
-               throw std::runtime_error("unknown tile type " + name);
+std::vector<TileType::Yield>::const_iterator TileType::FindResource(int r) const {
+       auto yield = resources.cbegin();
+       for (; yield != resources.cend(); ++yield) {
+               if (yield->resource == r) {
+                       break;
+               }
        }
+       return yield;
 }
 
-const TileType &TileSet::operator [](const std::string &name) const {
-       auto entry = names.find(name);
-       if (entry != names.end()) {
-               return types[entry->second];
-       } else {
-               throw std::runtime_error("unknown tile type " + name);
+std::vector<TileType::Yield>::const_iterator TileType::FindBestResource(const creature::Composition &comp) const {
+       auto best = resources.cend();
+       double best_value = 0.0;
+       for (auto yield = resources.cbegin(); yield != resources.cend(); ++yield) {
+               double value = comp.Get(yield->resource);
+               if (value > best_value) {
+                       best = yield;
+                       best_value = value;
+               }
        }
+       return best;
 }
 
 }