From 2025d49798b93180237b6ce62d3ff5d6ee8ebc6e Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Wed, 22 Nov 2017 23:24:47 +0100 Subject: [PATCH] eat what's here --- assets | 2 +- src/app/Assets.hpp | 2 +- src/app/app.cpp | 14 +++++++------- src/blobs.cpp | 8 ++++---- src/creature/IngestNeed.hpp | 1 + src/creature/Situation.hpp | 5 +++++ src/creature/creature.cpp | 25 +++++++++++++++++++++---- src/creature/need.cpp | 18 ++++++++++++++++-- src/world/Planet.hpp | 2 ++ src/world/Simulation.hpp | 11 ++++++++++- src/world/sim.cpp | 4 +++- src/world/world.cpp | 4 ++++ 12 files changed, 75 insertions(+), 21 deletions(-) diff --git a/assets b/assets index 46d3c2d..ce337a6 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 46d3c2d2fb145405994b69291037a3c9e66ab3d1 +Subproject commit ce337a6b5afd87607a94f006631a390eefceeec1 diff --git a/src/app/Assets.hpp b/src/app/Assets.hpp index ab6a4f3..22b6087 100644 --- a/src/app/Assets.hpp +++ b/src/app/Assets.hpp @@ -31,7 +31,7 @@ struct Assets { struct { world::Set resources; - world::Set tiles; + world::Set tile_types; } data; struct { diff --git a/src/app/app.cpp b/src/app/app.cpp index 0ec22b0..b4ede47 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -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); } diff --git a/src/blobs.cpp b/src/blobs.cpp index 5647502..23ab012 100644 --- a/src/blobs.cpp +++ b/src/blobs.cpp @@ -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; diff --git a/src/creature/IngestNeed.hpp b/src/creature/IngestNeed.hpp index b64ee0c..3ad3bf0 100644 --- a/src/creature/IngestNeed.hpp +++ b/src/creature/IngestNeed.hpp @@ -23,6 +23,7 @@ private: int resource; double speed; double damage; + bool ingesting; }; diff --git a/src/creature/Situation.hpp b/src/creature/Situation.hpp index 4dbb1c6..d4a376a 100644 --- a/src/creature/Situation.hpp +++ b/src/creature/Situation.hpp @@ -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; diff --git a/src/creature/creature.cpp b/src/creature/creature.cpp index 66931e9..de3a4b7 100644 --- a/src/creature/creature.cpp +++ b/src/creature/creature.cpp @@ -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 @@ -149,7 +150,7 @@ void Spawn(Creature &c, world::Planet &p, app::Assets &assets) { std::map 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(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(new IngestNeed(solid, 0.03, 0.001)); + std::unique_ptr 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; diff --git a/src/creature/need.cpp b/src/creature/need.cpp index 88b4419..8249631 100644 --- a/src/creature/need.cpp +++ b/src/creature/need.cpp @@ -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) { diff --git a/src/world/Planet.hpp b/src/world/Planet.hpp index af3b512..8cd9efe 100644 --- a/src/world/Planet.hpp +++ b/src/world/Planet.hpp @@ -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); diff --git a/src/world/Simulation.hpp b/src/world/Simulation.hpp index ea81de5..2f2239e 100644 --- a/src/world/Simulation.hpp +++ b/src/world/Simulation.hpp @@ -1,6 +1,8 @@ #ifndef BLOBS_WORLD_SIMULATION_HPP_ #define BLOBS_WORLD_SIMULATION_HPP_ +#include "Set.hpp" + #include @@ -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 &, const Set &); ~Simulation(); Simulation(const Simulation &) = delete; @@ -33,6 +37,9 @@ public: Body &Root() noexcept { return root; } const Body &Root() const noexcept { return root; } + const Set &Resources() const noexcept { return resources; } + const Set &TileTypes() const noexcept { return tile_types; } + const std::set &Bodies() const noexcept { return bodies; } const std::set &Planets() const noexcept { return planets; } const std::set &Suns() const noexcept { return suns; } @@ -41,6 +48,8 @@ public: private: Body &root; + const Set &resources; + const Set &tile_types; std::set bodies; std::set planets; std::set suns; diff --git a/src/world/sim.cpp b/src/world/sim.cpp index aedc5cf..071ad17 100644 --- a/src/world/sim.cpp +++ b/src/world/sim.cpp @@ -8,8 +8,10 @@ namespace blobs { namespace world { -Simulation::Simulation(Body &r) +Simulation::Simulation(Body &r, const Set &res, const Set &tile) : root(r) +, resources(res) +, tile_types(tile) , bodies() , planets() , suns() diff --git a/src/world/world.cpp b/src/world/world.cpp index 56923cf..449307a 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -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(); -- 2.39.2