]> git.localhorst.tv Git - blobs.git/commitdiff
eat what's here
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 22 Nov 2017 22:24:47 +0000 (23:24 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Wed, 22 Nov 2017 22:24:47 +0000 (23:24 +0100)
12 files changed:
assets
src/app/Assets.hpp
src/app/app.cpp
src/blobs.cpp
src/creature/IngestNeed.hpp
src/creature/Situation.hpp
src/creature/creature.cpp
src/creature/need.cpp
src/world/Planet.hpp
src/world/Simulation.hpp
src/world/sim.cpp
src/world/world.cpp

diff --git a/assets b/assets
index 46d3c2d2fb145405994b69291037a3c9e66ab3d1..ce337a6b5afd87607a94f006631a390eefceeec1 160000 (submodule)
--- a/assets
+++ b/assets
@@ -1 +1 @@
-Subproject commit 46d3c2d2fb145405994b69291037a3c9e66ab3d1
+Subproject commit ce337a6b5afd87607a94f006631a390eefceeec1
index ab6a4f3300e44b13d0d9be9e32579d426b8def3a..22b60878da42ed6663d5735fa338b13e8f71d061 100644 (file)
@@ -31,7 +31,7 @@ struct Assets {
 
        struct {
                world::Set<world::Resource> resources;
-               world::Set<world::TileType> tiles;
+               world::Set<world::TileType> tile_types;
        } data;
 
        struct {
index 0ec22b02c16607d189cda86107140d14ccde643f..b4ede47f0db1d6bfc96a692da7018ccd45869502 100644 (file)
@@ -188,7 +188,7 @@ Assets::Assets()
        }
 
        {
-               std::ifstream tile_file(data_path + "tiles");
+               std::ifstream tile_file(data_path + "tile_types");
                io::TokenStreamReader tile_reader(tile_file);
                ReadTileTypes(tile_reader);
        }
@@ -279,12 +279,12 @@ void Assets::ReadTileTypes(io::TokenStreamReader &in) {
                in.Skip(io::Token::EQUALS);
 
                int id = 0;
-               if (data.tiles.Has(name)) {
-                       id = data.tiles[name].id;
+               if (data.tile_types.Has(name)) {
+                       id = data.tile_types[name].id;
                } else {
                        world::TileType type;
                        type.name = name;
-                       id = data.tiles.Add(type);
+                       id = data.tile_types.Add(type);
                }
 
                in.Skip(io::Token::ANGLE_BRACKET_OPEN);
@@ -292,9 +292,9 @@ void Assets::ReadTileTypes(io::TokenStreamReader &in) {
                        in.ReadIdentifier(name);
                        in.Skip(io::Token::EQUALS);
                        if (name == "label") {
-                               in.ReadString(data.tiles[id].label);
+                               in.ReadString(data.tile_types[id].label);
                        } else if (name == "texture") {
-                               data.tiles[id].texture = in.GetInt();
+                               data.tile_types[id].texture = in.GetInt();
                        } else if (name == "yield") {
                                in.Skip(io::Token::BRACKET_OPEN);
                                while (in.Peek().type != io::Token::BRACKET_CLOSE) {
@@ -314,7 +314,7 @@ void Assets::ReadTileTypes(io::TokenStreamReader &in) {
                                                in.Skip(io::Token::SEMICOLON);
                                        }
                                        in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
-                                       data.tiles[id].resources.push_back(yield);
+                                       data.tile_types[id].resources.push_back(yield);
                                        if (in.Peek().type == io::Token::COMMA) {
                                                in.Skip(io::Token::COMMA);
                                        }
index 56475029097c8f061ca5ec971d994aac68674200..23ab012fed3a0d31aaca785e327074da2dde6e41 100644 (file)
@@ -49,16 +49,16 @@ int main(int argc, char *argv[]) {
        second_planet.AxialTilt(glm::dvec2(PI * 0.95, 0.0));
        second_planet.AngularMomentum(1.0e8);
 
-       world::Simulation sim(sun);
+       world::Simulation sim(sun, assets.data.resources, assets.data.tile_types);
        sim.AddSun(sun);
        sim.AddPlanet(planet);
        sim.AddPlanet(second_planet);
        sim.AddPlanet(moon);
 
-       world::GenerateEarthlike(assets.data.tiles, planet);
+       world::GenerateEarthlike(assets.data.tile_types, planet);
        planet.Atmosphere(assets.data.resources["air"].id);
-       world::GenerateTest(assets.data.tiles, moon);
-       world::GenerateTest(assets.data.tiles, second_planet);
+       world::GenerateTest(assets.data.tile_types, moon);
+       world::GenerateTest(assets.data.tile_types, second_planet);
 
        std::cout << "length of year: " << planet.OrbitalPeriod() << "s" << std::endl;
        std::cout << "length of moon cycle: " << moon.OrbitalPeriod() << "s" << std::endl;
index b64ee0cfae4c6cfdec8f930a8d346c5abf098eb4..3ad3bf071d792eab1ad297ca55c5afbd6248b12e 100644 (file)
@@ -23,6 +23,7 @@ private:
        int resource;
        double speed;
        double damage;
+       bool ingesting;
 
 };
 
index 4dbb1c694d96b8ea5d534b353ec9c6e5f9a8e2a2..d4a376a8d6f25cfdd853cf46ab729553ec353f0a 100644 (file)
@@ -7,6 +7,8 @@
 namespace blobs {
 namespace world {
        class Planet;
+       class Tile;
+       class TileType;
 }
 namespace creature {
 
@@ -19,8 +21,11 @@ public:
 public:
        bool OnPlanet() const noexcept;
        world::Planet &GetPlanet() const noexcept { return *planet; }
+       bool OnSurface() const noexcept;
        int Surface() const noexcept { return surface; }
        const glm::dvec3 &Position() const noexcept { return position; }
+       world::Tile &GetTile() const noexcept;
+       const world::TileType &GetTileType() const noexcept;
 
        void SetPlanetSurface(world::Planet &, int srf, const glm::dvec3 &pos) noexcept;
 
index 66931e921f58d06e5c227d1b3035f697203886d4..de3a4b7dc6d1b19aea9ccb61e345df6950308fef 100644 (file)
@@ -7,6 +7,7 @@
 #include "../app/Assets.hpp"
 #include "../world/Body.hpp"
 #include "../world/Planet.hpp"
+#include "../world/Simulation.hpp"
 #include "../world/TileType.hpp"
 
 #include <glm/gtx/transform.hpp>
@@ -149,7 +150,7 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
        std::map<int, double> yields;
        for (int y = start; y < end; ++y) {
                for (int x = start; x < end; ++x) {
-                       const world::TileType &t = assets.data.tiles[p.TileAt(0, x, y).type];
+                       const world::TileType &t = assets.data.tile_types[p.TileAt(0, x, y).type];
                        for (auto yield : t.resources) {
                                yields[yield.resource] += yield.ubiquity;
                        }
@@ -182,16 +183,16 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) {
                std::cout << "require drinking " << assets.data.resources[liquid].label << std::endl;
                std::unique_ptr<Need> need(new IngestNeed(liquid, 0.2, 0.01));
                need->name = assets.data.resources[liquid].label;
-               need->gain = 0.0001;
+               need->gain = 0.01;
                need->inconvenient = 0.6;
                need->critical = 0.95;
                c.AddNeed(std::move(need));
        }
        if (solid > -1) {
                std::cout << "require eating " << assets.data.resources[solid].label << std::endl;
-               std::unique_ptr<Need> need(new IngestNeed(solid, 0.03, 0.001));
+               std::unique_ptr<Need> need(new IngestNeed(solid, 0.1, 0.001));
                need->name = assets.data.resources[solid].label;
-               need->gain = 0.00001;
+               need->gain = 0.007;
                need->inconvenient = 0.6;
                need->critical = 0.95;
                c.AddNeed(std::move(need));
@@ -212,6 +213,22 @@ bool Situation::OnPlanet() const noexcept {
        return type == PLANET_SURFACE;
 }
 
+bool Situation::OnSurface() const noexcept {
+       return type == PLANET_SURFACE;
+}
+
+world::Tile &Situation::GetTile() const noexcept {
+       double side_length = planet->SideLength();
+       double offset = side_length * 0.5;
+       double x = std::max(0.0, std::min(side_length, position.x + offset));
+       double y = std::max(0.0, std::min(side_length, position.y + offset));
+       return planet->TileAt(surface, int(x), int(y));
+}
+
+const world::TileType &Situation::GetTileType() const noexcept {
+       return planet->GetSimulation().TileTypes()[GetTile().type];
+}
+
 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
        type = PLANET_SURFACE;
        planet = &p;
index 88b44197c8de80af4f6a0afcb297915c75053a33..8249631038a639925697d9fb4a535d5135a9e1c0 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "Creature.hpp"
 #include "../world/Planet.hpp"
+#include "../world/TileType.hpp"
 
 
 namespace blobs {
@@ -28,15 +29,28 @@ void Need::Decrease(double delta) noexcept {
 IngestNeed::IngestNeed(int resource, double speed, double damage)
 : resource(resource)
 , speed(speed)
-, damage(damage) {
+, damage(damage)
+, ingesting(false) {
 }
 
 IngestNeed::~IngestNeed() {
 }
 
 void IngestNeed::ApplyEffect(Creature &c, double dt) {
+       if (!IsSatisfied()) {
+               ingesting = true;
+       }
        if (!IsSatisfied()) {
                // TODO: find resource and start ingest task
+               if (c.GetSituation().OnSurface()) {
+                       const world::TileType &t = c.GetSituation().GetTileType();
+                       for (auto &yield : t.resources) {
+                               if (yield.resource == resource) {
+                                       Decrease(std::min(yield.ubiquity, speed) * dt);
+                                       break;
+                               }
+                       }
+               }
        }
        if (IsCritical()) {
                c.Hurt(damage * dt);
@@ -55,7 +69,7 @@ InhaleNeed::~InhaleNeed() {
 }
 
 void InhaleNeed::ApplyEffect(Creature &c, double dt) {
-       if (!IsSatisfied() && !inhaling) {
+       if (!IsSatisfied()) {
                inhaling = true;
        }
        if (inhaling) {
index af3b512aea6b058bc97b4dadb7435b0bc65d4347..8cd9efef5233b81cedb6a751622ca827af31f4b8 100644 (file)
@@ -42,6 +42,8 @@ public:
                return tiles[IndexOf(surface, x, y)];
        }
 
+       const TileType &TypeAt(int surface, int x, int y) const;
+
        /// Convert coordinates into a tile index.
        int IndexOf(int surface, int x, int y) const {
                assert(0 <= surface && surface <= 5);
index ea81de5ba226fcc812491aa18bbc44e8c704e361..2f2239efe1d9c77b03b1bd10e7151a7c0c659a0c 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef BLOBS_WORLD_SIMULATION_HPP_
 #define BLOBS_WORLD_SIMULATION_HPP_
 
+#include "Set.hpp"
+
 #include <set>
 
 
@@ -9,12 +11,14 @@ namespace world {
 
 class Body;
 class Planet;
+class Resource;
 class Sun;
+class TileType;
 
 class Simulation {
 
 public:
-       explicit Simulation(Body &root);
+       explicit Simulation(Body &root, const Set<Resource> &, const Set<TileType> &);
        ~Simulation();
 
        Simulation(const Simulation &) = delete;
@@ -33,6 +37,9 @@ public:
        Body &Root() noexcept { return root; }
        const Body &Root() const noexcept { return root; }
 
+       const Set<Resource> &Resources() const noexcept { return resources; }
+       const Set<TileType> &TileTypes() const noexcept { return tile_types; }
+
        const std::set<Body *> &Bodies() const noexcept { return bodies; }
        const std::set<Planet *> &Planets() const noexcept { return planets; }
        const std::set<Sun *> &Suns() const noexcept { return suns; }
@@ -41,6 +48,8 @@ public:
 
 private:
        Body &root;
+       const Set<Resource> &resources;
+       const Set<TileType> &tile_types;
        std::set<Body *> bodies;
        std::set<Planet *> planets;
        std::set<Sun *> suns;
index aedc5cf01017df98b217b87dcc4991b9ddf8848e..071ad17cb0ed5786a998334c6e0fc9cb03a3ac1f 100644 (file)
@@ -8,8 +8,10 @@
 namespace blobs {
 namespace world {
 
-Simulation::Simulation(Body &r)
+Simulation::Simulation(Body &r, const Set<Resource> &res, const Set<TileType> &tile)
 : root(r)
+, resources(res)
+, tile_types(tile)
 , bodies()
 , planets()
 , suns()
index 56923cf6500fb6995bfa7a9a3edac59c57a8c59a..449307abeefbeeb88ea148171600aed8b2e1a596 100644 (file)
@@ -290,6 +290,10 @@ Planet::Planet(int sidelength)
 Planet::~Planet() {
 }
 
+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();