]> git.localhorst.tv Git - blobs.git/commitdiff
figure out our creature's metabolism
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 17 Nov 2017 21:05:47 +0000 (22:05 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Fri, 17 Nov 2017 21:05:47 +0000 (22:05 +0100)
highly simplified of course

assets
src/app/app.cpp
src/blobs.cpp
src/world/Creature.hpp
src/world/Planet.hpp
src/world/Resource.hpp
src/world/creature.cpp
src/world/world.cpp

diff --git a/assets b/assets
index b63c91ead8b510b614efccc97b4000fc3aca9c87..46d3c2d2fb145405994b69291037a3c9e66ab3d1 160000 (submodule)
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit b63c91ead8b510b614efccc97b4000fc3aca9c87
+Subproject commit 46d3c2d2fb145405994b69291037a3c9e66ab3d1
index ccccd9a6c15c5b2590e8a27b0274c74541c1451e..b3ae4780ec6c5147a315d227fb11aa66e0b89d5c 100644 (file)
@@ -242,6 +242,19 @@ void Assets::ReadResources(io::TokenStreamReader &in) {
                        in.Skip(io::Token::EQUALS);
                        if (name == "label") {
                                in.ReadString(data.resources[id].label);
+                       } else if (name == "state") {
+                               in.ReadIdentifier(name);
+                               if (name == "solid") {
+                                       data.resources[id].state = world::Resource::SOLID;
+                               } else if (name == "liquid") {
+                                       data.resources[id].state = world::Resource::LIQUID;
+                               } else if (name == "gas") {
+                                       data.resources[id].state = world::Resource::GAS;
+                               } else if (name == "plasma") {
+                                       data.resources[id].state = world::Resource::PLASMA;
+                               } else {
+                                       throw std::runtime_error("unknown resource state '" + name + "'");
+                               }
                        } else {
                                throw std::runtime_error("unknown resource property '" + name + "'");
                        }
index 918925664f3d66f553685d831363e1e0110b5de0..e0a4769ba2eadd965fadca11b81d26613fdaee2c 100644 (file)
@@ -56,6 +56,7 @@ int main(int argc, char *argv[]) {
        sim.AddPlanet(moon);
 
        world::GenerateEarthlike(assets.data.tiles, planet);
+       planet.Atmosphere(assets.data.resources["air"].id);
        world::GenerateTest(assets.data.tiles, moon);
        world::GenerateTest(assets.data.tiles, second_planet);
 
@@ -68,9 +69,7 @@ int main(int argc, char *argv[]) {
 
        auto blob = new world::Creature;
        blob->BuildVAO();
-       planet.AddCreature(blob);
-       blob->Surface(0);
-       blob->Position(glm::dvec3(0.0, 0.0, 0.0));
+       Spawn(*blob, planet, assets);
 
        app::MasterState state(assets, sim);
        state.GetCamera()
index 170d52b0e036452e0b78120c3ab7a856a3bab336..83623a2ba04ad19f40147a2cf8f82507e2d2bfd3 100644 (file)
@@ -15,6 +15,7 @@ namespace graphics {
 namespace world {
 
 class Body;
+class Planet;
 
 class Creature {
 
@@ -36,6 +37,18 @@ public:
        void Surface(int s) noexcept { surface = s; }
        void Position(const glm::dvec3 &p) noexcept { position = p; }
 
+       void RequireBreathing(int r) noexcept { breathes = r; }
+       int Breathes() const noexcept { return breathes; }
+       bool MustBreathe() const noexcept { return breathes > -1; }
+
+       void RequireDrinking(int r) noexcept { drinks = r; }
+       int Drinks() const noexcept { return drinks; }
+       bool MustDrink() const noexcept { return drinks > -1; }
+
+       void RequireEating(int r) noexcept { eats = r; }
+       int Eats() const noexcept { return eats; }
+       bool MustEat() const noexcept { return eats > -1; }
+
        glm::dmat4 LocalTransform() noexcept;
 
        void BuildVAO();
@@ -46,6 +59,10 @@ private:
        int surface;
        glm::dvec3 position;
 
+       int breathes;
+       int drinks;
+       int eats;
+
        struct Attributes {
                glm::vec3 position;
                glm::vec3 normal;
@@ -55,6 +72,9 @@ private:
 
 };
 
+/// put creature on planet and configure it to (hopefully) survive
+void Spawn(Creature &, Planet &, app::Assets &);
+
 }
 }
 
index af3b512aea6b058bc97b4dadb7435b0bc65d4347..991985f968173e2eb98f0ddac4b4e3fcdaa9984f 100644 (file)
@@ -64,6 +64,10 @@ public:
 
        glm::dvec3 TileCenter(int surface, int x, int y) const noexcept;
 
+       void Atmosphere(int a) noexcept { atmosphere = a; }
+       int Atmosphere() const noexcept { return atmosphere; }
+       bool HasAtmosphere() const noexcept { return atmosphere >= 0; }
+
        void BuildVAO(const Set<TileType> &);
        void Draw(app::Assets &, graphics::Viewport &) override;
 
@@ -71,6 +75,8 @@ private:
        int sidelength;
        std::vector<Tile> tiles;
 
+       int atmosphere;
+
        struct Attributes {
                glm::vec3 position;
                glm::vec3 tex_coord;
index 5dd383442952b53e7ee9783288840c1ad1d534dd..6250fc67ac4a9424cb305dc7e66b14f12538dfb5 100644 (file)
@@ -14,6 +14,16 @@ struct Resource {
 
        int id;
 
+       enum State {
+               SOLID = 0,
+               LIQUID = 1,
+               GAS = 2,
+               PLASMA = 3,
+       };
+       // the resource's natural state
+       // TODO: something about temperature and pressure and stuff
+       int state;
+
 };
 
 }
index aa85b593427f06ac17c80d169711901c079e5bc8..8018e7c05a4a10e382901e4ce763ebf20492a5ce 100644 (file)
@@ -1,15 +1,26 @@
 #include "Creature.hpp"
 
 #include "Body.hpp"
+#include "Planet.hpp"
+#include "TileType.hpp"
+#include "../app/Assets.hpp"
 
 #include <glm/gtx/transform.hpp>
 
+#include <iostream>
+
 
 namespace blobs {
 namespace world {
 
 Creature::Creature()
-: vao() {
+: body(nullptr)
+, surface(0)
+, position()
+, breathes(-1)
+, drinks(-1)
+, eats(-1)
+, vao() {
 }
 
 Creature::~Creature() {
@@ -110,5 +121,51 @@ void Creature::Draw(app::Assets &assets, graphics::Viewport &viewport) {
        vao.DrawTriangles(6 * 6);
 }
 
+
+void Spawn(Creature &c, Planet &p, app::Assets &assets) {
+       p.AddCreature(&c);
+       c.Surface(0);
+       c.Position(glm::dvec3(0.0, 0.0, 0.0));
+
+       // probe surrounding area for common resources
+       int start = p.SideLength() / 2 - 2;
+       int end = start + 5;
+       std::map<int, double> yields;
+       for (int y = start; y < end; ++y) {
+               for (int x = start; x < end; ++x) {
+                       const TileType &t = assets.data.tiles[p.TileAt(0, x, y).type];
+                       for (auto yield : t.resources) {
+                               yields[yield.resource] += yield.ubiquity;
+                       }
+               }
+       }
+       int liquid = -1;
+       int solid = -1;
+       for (auto e : yields) {
+               if (assets.data.resources[e.first].state == Resource::LIQUID) {
+                       if (liquid < 0 || e.second > yields[liquid]) {
+                               liquid = e.first;
+                       }
+               } else if (assets.data.resources[e.first].state == Resource::SOLID) {
+                       if (solid < 0 || e.second > yields[solid]) {
+                               solid = e.first;
+                       }
+               }
+       }
+
+       if (p.HasAtmosphere()) {
+               std::cout << "require breathing " << assets.data.resources[p.Atmosphere()].label << std::endl;
+               c.RequireBreathing(p.Atmosphere());
+       }
+       if (liquid > -1) {
+               std::cout << "require drinking " << assets.data.resources[liquid].label << std::endl;
+               c.RequireDrinking(liquid);
+       }
+       if (solid > -1) {
+               std::cout << "require eating " << assets.data.resources[solid].label << std::endl;
+               c.RequireEating(solid);
+       }
+}
+
 }
 }
index ed2ade3bb5a6daca721925b3285d759313e3ea72..9f19b68c02cdaa866a5c7bdc687daf945ad97f7b 100644 (file)
@@ -268,6 +268,7 @@ Planet::Planet(int sidelength)
 : Body()
 , sidelength(sidelength)
 , tiles(TilesTotal())
+, atmosphere(-1)
 , vao() {
        Radius(double(sidelength) / 2.0);
 }