]> git.localhorst.tv Git - blobs.git/blobdiff - src/world/world.cpp
spherical sun
[blobs.git] / src / world / world.cpp
index 4f98d9a63bdef3b241123e8cf2bbdf30b76ce3de..02c142ac5af08f52ecd42114bc8b6019d556bcad 100644 (file)
@@ -1,4 +1,5 @@
 #include "Body.hpp"
+#include "CreatureCreatureCollision.hpp"
 #include "Orbit.hpp"
 #include "Planet.hpp"
 #include "Resource.hpp"
@@ -9,9 +10,11 @@
 #include "TileType.hpp"
 
 #include "../app/Assets.hpp"
+#include "../creature/Composition.hpp"
 #include "../creature/Creature.hpp"
 #include "../graphics/Viewport.hpp"
 #include "../math/const.hpp"
+#include "../math/geometry.hpp"
 #include "../math/OctaveNoise.hpp"
 #include "../math/SimplexNoise.hpp"
 
@@ -23,9 +26,6 @@
 #include <glm/gtx/io.hpp>
 #include <glm/gtx/transform.hpp>
 
-using blobs::G;
-using blobs::PI_2p0;
-
 using std::sin;
 using std::cos;
 using std::pow;
@@ -55,9 +55,6 @@ Body::Body()
 }
 
 Body::~Body() {
-       for (creature::Creature *c : creatures) {
-               delete c;
-       }
 }
 
 void Body::SetSimulation(Simulation &s) noexcept {
@@ -104,7 +101,7 @@ double Body::GravitationalParameter() const noexcept {
 
 double Body::OrbitalPeriod() const noexcept {
        if (parent) {
-               return PI_2p0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
+               return PI * 2.0 * sqrt(pow(orbit.SemiMajorAxis(), 3) / (G * (parent->Mass() + Mass())));
        } else {
                return 0.0;
        }
@@ -114,7 +111,7 @@ double Body::RotationalPeriod() const noexcept {
        if (std::abs(angular) < std::numeric_limits<double>::epsilon()) {
                return std::numeric_limits<double>::infinity();
        } else {
-               return PI_2p0 * Inertia() / angular;
+               return PI * 2.0 * Inertia() / angular;
        }
 }
 
@@ -138,22 +135,38 @@ 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();
-       for (creature::Creature *c : Creatures()) {
+       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 =
-                       orbit.Matrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()))
+                       orbit.Matrix(PI * 2.0 * (GetSimulation().Time() / OrbitalPeriod()))
                        * glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
                inverse_orbital =
                        glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x)
-                       * orbit.InverseMatrix(PI_2p0 * (GetSimulation().Time() / OrbitalPeriod()));
+                       * orbit.InverseMatrix(PI * 2.0 * (GetSimulation().Time() / OrbitalPeriod()));
        } else {
                orbital = glm::eulerAngleXY(axis_tilt.x, axis_tilt.y);
                inverse_orbital = glm::eulerAngleYX(-axis_tilt.y, -axis_tilt.x);
@@ -166,6 +179,35 @@ void Body::Cache() noexcept {
                * glm::eulerAngleY(-rotation);
 }
 
+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 (glm::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() * -glm::dot(c.Normal(), c.AVel()));
+               c.B().GetSituation().Accelerate(c.Normal() * -glm::dot(c.Normal(), c.BVel()));
+               // TODO: notify participants so they can be annoyed
+       }
+}
+
 void Body::AddCreature(creature::Creature *c) {
        creatures.push_back(c);
 }
@@ -178,6 +220,26 @@ void Body::RemoveCreature(creature::Creature *c) {
 }
 
 
+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)
 , ecc(0.0)
@@ -252,7 +314,7 @@ double mean2eccentric(double M, double e) {
        for (int i = 0; i < 100; ++i) {
                double dE = (E - e * sin(E) - M) / (1 - e * cos(E));
                E -= dE;
-               if (abs(dE) < 1.0e-6) break;
+               if (std::abs(dE) < 1.0e-6) break;
        }
        return E;
 }
@@ -290,28 +352,107 @@ Planet::Planet(int sidelength)
 Planet::~Planet() {
 }
 
-const TileType &Planet::TypeAt(int srf, int x, int y) const {
-       return GetSimulation().TileTypes()[TileAt(srf, x, y).type];
+namespace {
+/// map p onto cube, s gives the surface, u and v the position in [-1,1]
+void cubemap(const glm::dvec3 &p, int &s, double &u, double &v) noexcept {
+       const glm::dvec3 p_abs(glm::abs(p));
+       const glm::bvec3 p_pos(glm::greaterThan(p, glm::dvec3(0.0)));
+       double max_axis = 0.0;
+
+       if (p_pos.x && p_abs.x >= p_abs.y && p_abs.x >= p_abs.z) {
+               max_axis = p_abs.x;
+               u = p.y;
+               v = p.z;
+               s = 1;
+       }
+       if (!p_pos.x && p_abs.x >= p_abs.y && p_abs.x >= p_abs.z) {
+               max_axis = p_abs.x;
+               u = -p.y;
+               v = -p.z;
+               s = 4;
+       }
+       if (p_pos.y && p_abs.y >= p_abs.x && p_abs.y >= p_abs.z) {
+               max_axis = p_abs.y;
+               u = p.z;
+               v = p.x;
+               s = 2;
+       }
+       if (!p_pos.y && p_abs.y >= p_abs.x && p_abs.y >= p_abs.z) {
+               max_axis = p_abs.y;
+               u = -p.z;
+               v = -p.x;
+               s = 5;
+       }
+       if (p_pos.z && p_abs.z >= p_abs.x && p_abs.z >= p_abs.y) {
+               max_axis = p_abs.z;
+               u = p.x;
+               v = p.y;
+               s = 0;
+       }
+       if (!p_pos.z && p_abs.z >= p_abs.x && p_abs.z >= p_abs.y) {
+               max_axis = p_abs.z;
+               u = -p.x;
+               v = -p.y;
+               s = 3;
+       }
+       u /= max_axis;
+       v /= max_axis;
+}
+/// get p from cube, s being surface, u and v the position in [-1,1],
+/// gives a vector from the center to the surface
+glm::dvec3 cubeunmap(int s, double u, double v) {
+       switch (s) {
+               default:
+               case 0: return glm::dvec3(u, v, 1.0); // +Z
+               case 1: return glm::dvec3(1.0, u, v); // +X
+               case 2: return glm::dvec3(v, 1.0, u); // +Y
+               case 3: return glm::dvec3(-u, -v, -1.0); // -Z
+               case 4: return glm::dvec3(-1.0, -u, -v); // -X
+               case 5: return glm::dvec3(-v, -1.0, -u); // -Y
+       };
+}
+}
+
+Tile &Planet::TileAt(const glm::dvec3 &p) noexcept {
+       int srf = 0;
+       double u = 0.0;
+       double v = 0.0;
+       cubemap(p, srf, u, v);
+       int x = glm::clamp(int(u * Radius() + Radius()), 0, sidelength - 1);
+       int y = glm::clamp(int(v * Radius() + Radius()), 0, sidelength - 1);
+       return TileAt(srf, x, y);
+}
+
+const Tile &Planet::TileAt(const glm::dvec3 &p) const noexcept {
+       int srf = 0;
+       double u = 0.0;
+       double v = 0.0;
+       cubemap(p, srf, u, v);
+       int x = glm::clamp(int(u * Radius() + Radius()), 0, sidelength - 1);
+       int y = glm::clamp(int(v * Radius() + Radius()), 0, sidelength - 1);
+       return TileAt(srf, x, y);
+}
+
+const TileType &Planet::TileTypeAt(const glm::dvec3 &p) const noexcept {
+       return GetSimulation().TileTypes()[TileAt(p).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]));
+Tile &Planet::TileAt(int surface, int x, int y) noexcept {
+       return tiles[IndexOf(surface, x, y)];
 }
 
-double Planet::SurfaceElevation(int srf, const glm::dvec3 &pos) const noexcept {
-       return srf < 3
-               ? pos[(srf + 2) % 3] - Radius()
-               : -pos[(srf + 2) % 3] - Radius();
+const Tile &Planet::TileAt(int surface, int x, int y) const noexcept {
+       return tiles[IndexOf(surface, x, y)];
+}
+
+const TileType &Planet::TypeAt(int srf, int x, int y) const noexcept {
+       return GetSimulation().TileTypes()[TileAt(srf, x, y).type];
 }
 
 glm::dvec3 Planet::TileCenter(int srf, int x, int y, double e) const noexcept {
-       glm::dvec3 center(0.0f);
-       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;
+       double u = (double(x) - Radius() + 0.5) / Radius();
+       double v = (double(y) - Radius() + 0.5) / Radius();
+       return glm::normalize(cubeunmap(srf, u, v)) * (Radius() + e);
 }
 
 void Planet::BuildVAO(const Set<TileType> &ts) {
@@ -320,8 +461,10 @@ void Planet::BuildVAO(const Set<TileType> &ts) {
        vao->BindAttributes();
        vao->EnableAttribute(0);
        vao->EnableAttribute(1);
+       vao->EnableAttribute(2);
        vao->AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
-       vao->AttributePointer<glm::vec3>(1, false, offsetof(Attributes, tex_coord));
+       vao->AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
+       vao->AttributePointer<glm::vec3>(2, false, offsetof(Attributes, tex_coord));
        vao->ReserveAttributes(TilesTotal() * 4, GL_STATIC_DRAW);
        {
                auto attrib = vao->MapAttributes(GL_WRITE_ONLY);
@@ -333,35 +476,46 @@ void Planet::BuildVAO(const Set<TileType> &ts) {
                for (int index = 0, surface = 0; surface < 6; ++surface) {
                        for (int y = 0; y < sidelength; ++y) {
                                for (int x = 0; x < sidelength; ++x, ++index) {
+                                       glm::vec3 pos[4];
+                                       pos[0][(surface + 0) % 3] = float(x + 0) - offset;
+                                       pos[0][(surface + 1) % 3] = float(y + 0) - offset;
+                                       pos[0][(surface + 2) % 3] = offset;
+                                       pos[1][(surface + 0) % 3] = float(x + 0) - offset;
+                                       pos[1][(surface + 1) % 3] = float(y + 1) - offset;
+                                       pos[1][(surface + 2) % 3] = offset;
+                                       pos[2][(surface + 0) % 3] = float(x + 1) - offset;
+                                       pos[2][(surface + 1) % 3] = float(y + 0) - offset;
+                                       pos[2][(surface + 2) % 3] = offset;
+                                       pos[3][(surface + 0) % 3] = float(x + 1) - offset;
+                                       pos[3][(surface + 1) % 3] = float(y + 1) - offset;
+                                       pos[3][(surface + 2) % 3] = offset;
+
                                        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] = tex_u_begin;
-                                       attrib[4 * index + 0].tex_coord[1] = 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 = glm::normalize(pos[0]) * (surface < 3 ? offset : -offset);
+                                       attrib[4 * index + 0].normal = pos[0];
+                                       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].position = glm::normalize(pos[1]) * (surface < 3 ? offset : -offset);
+                                       attrib[4 * index + 1].normal = pos[1];
+                                       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].position = glm::normalize(pos[2]) * (surface < 3 ? offset : -offset);
+                                       attrib[4 * index + 2].normal = pos[2];
+                                       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].position = glm::normalize(pos[3]) * (surface < 3 ? offset : -offset);
+                                       attrib[4 * index + 3].normal = pos[3];
+                                       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;
                                }
                        }
@@ -404,19 +558,7 @@ void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) {
        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);
-       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);
-       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);
-       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);
-       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);
-       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(TilesTotal() * 6);
 }
 
 
@@ -462,7 +604,7 @@ void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
                                }
                                float elevation = math::OctaveNoise(
                                        elevation_gen,
-                                       to_tile / p.Radius(),
+                                       glm::vec3(to_tile / p.Radius()),
                                        3,   // octaves
                                        0.5, // persistence
                                        5 / p.Radius(), // frequency
@@ -471,7 +613,7 @@ void GenerateEarthlike(const Set<TileType> &tiles, Planet &p) noexcept {
                                );
                                float variation = math::OctaveNoise(
                                        variation_gen,
-                                       to_tile / p.Radius(),
+                                       glm::vec3(to_tile / p.Radius()),
                                        3,   // octaves
                                        0.5, // persistence
                                        16 / p.Radius(), // frequency
@@ -561,5 +703,18 @@ std::vector<TileType::Yield>::const_iterator TileType::FindResource(int r) const
        return yield;
 }
 
+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;
+}
+
 }
 }