From b795a1df619349d45c3b0ca73e61e68ff483221c Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Fri, 17 Nov 2017 22:05:47 +0100 Subject: [PATCH] figure out our creature's metabolism highly simplified of course --- assets | 2 +- src/app/app.cpp | 13 ++++++++++ src/blobs.cpp | 5 ++-- src/world/Creature.hpp | 20 ++++++++++++++ src/world/Planet.hpp | 6 +++++ src/world/Resource.hpp | 10 +++++++ src/world/creature.cpp | 59 +++++++++++++++++++++++++++++++++++++++++- src/world/world.cpp | 1 + 8 files changed, 111 insertions(+), 5 deletions(-) diff --git a/assets b/assets index b63c91e..46d3c2d 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit b63c91ead8b510b614efccc97b4000fc3aca9c87 +Subproject commit 46d3c2d2fb145405994b69291037a3c9e66ab3d1 diff --git a/src/app/app.cpp b/src/app/app.cpp index ccccd9a..b3ae478 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -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 + "'"); } diff --git a/src/blobs.cpp b/src/blobs.cpp index 9189256..e0a4769 100644 --- a/src/blobs.cpp +++ b/src/blobs.cpp @@ -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() diff --git a/src/world/Creature.hpp b/src/world/Creature.hpp index 170d52b..83623a2 100644 --- a/src/world/Creature.hpp +++ b/src/world/Creature.hpp @@ -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 &); + } } diff --git a/src/world/Planet.hpp b/src/world/Planet.hpp index af3b512..991985f 100644 --- a/src/world/Planet.hpp +++ b/src/world/Planet.hpp @@ -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 &); void Draw(app::Assets &, graphics::Viewport &) override; @@ -71,6 +75,8 @@ private: int sidelength; std::vector tiles; + int atmosphere; + struct Attributes { glm::vec3 position; glm::vec3 tex_coord; diff --git a/src/world/Resource.hpp b/src/world/Resource.hpp index 5dd3834..6250fc6 100644 --- a/src/world/Resource.hpp +++ b/src/world/Resource.hpp @@ -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; + }; } diff --git a/src/world/creature.cpp b/src/world/creature.cpp index aa85b59..8018e7c 100644 --- a/src/world/creature.cpp +++ b/src/world/creature.cpp @@ -1,15 +1,26 @@ #include "Creature.hpp" #include "Body.hpp" +#include "Planet.hpp" +#include "TileType.hpp" +#include "../app/Assets.hpp" #include +#include + 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 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); + } +} + } } diff --git a/src/world/world.cpp b/src/world/world.cpp index ed2ade3..9f19b68 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -268,6 +268,7 @@ Planet::Planet(int sidelength) : Body() , sidelength(sidelength) , tiles(TilesTotal()) +, atmosphere(-1) , vao() { Radius(double(sidelength) / 2.0); } -- 2.39.2