#include "Assets.hpp"
#include "../graphics/Camera.hpp"
#include "../ui/CreaturePanel.hpp"
+#include "../ui/RecordsPanel.hpp"
+#include "../ui/TimePanel.hpp"
namespace blobs {
ui::CreaturePanel &GetCreaturePanel() noexcept { return cp; }
const ui::CreaturePanel &GetCreaturePanel() const noexcept { return cp; }
+ ui::RecordsPanel &GetRecordsPanel() noexcept { return rp; }
+ const ui::RecordsPanel &GetRecordsPanel() const noexcept { return rp; }
+
+ ui::TimePanel &GetTimePanel() noexcept { return tp; }
+ const ui::TimePanel &GetTimePanel() const noexcept { return tp; }
+
private:
void OnResize(int w, int h) override;
bool cam_dragging;
ui::CreaturePanel cp;
+ ui::RecordsPanel rp;
+ ui::TimePanel tp;
int remain;
int thirds;
, cam_orient(PI * 0.375, PI * 0.25, 0.0)
, cam_dragging(false)
, cp(assets)
+, rp(sim)
+, tp(sim)
, remain(0)
, thirds(0)
, paused(false) {
}
viewport.ClearDepth();
- cp.Draw(assets, viewport);
+ cp.Draw(viewport);
+ rp.Draw(viewport);
+ tp.Draw(viewport);
}
}
// .Orbital(glm::vec3(-500.0f, 500.0f, 500.0f))
//;
state.GetCreaturePanel().Show(*blob);
+ state.GetTimePanel().SetBody(planet);
app::Application app(init.window, init.viewport);
SwitchPanel swp(planet, app, state);
void Add(int res, double amount);
bool Has(int res) const noexcept;
double Get(int res) const noexcept;
+ double TotalMass() const noexcept { return total_mass; }
public:
+ std::vector<Component>::size_type size() const noexcept { return components.size(); }
std::vector<Component>::iterator begin() noexcept { return components.begin(); }
std::vector<Component>::iterator end() noexcept { return components.end(); }
std::vector<Component>::const_iterator begin() const noexcept { return components.begin(); }
private:
std::vector<Component> components;
+ double total_mass;
};
void Hurt(double d) noexcept;
void Die() noexcept;
void OnDeath(Callback cb) noexcept { on_death = cb; }
- void Remove() noexcept { removable = true; }
+ void Remove() noexcept;
bool Removable() const noexcept { return removable; }
+ void Removed() noexcept;
+
+ void AddParent(Creature &);
+ const std::vector<Creature *> &Parents() const noexcept { return parents; }
Stats &GetStats() noexcept { return stats; }
const Stats &GetStats() const noexcept { return stats; }
glm::dmat4 LocalTransform() noexcept;
void BuildVAO();
+ void KillVAO();
void Draw(graphics::Viewport &);
private:
double size;
double birth;
+ double death;
Callback on_death;
bool removable;
+ std::vector<Creature *> parents;
+
Stats stats;
Memory memory;
glm::vec3 normal;
glm::vec3 texture;
};
- graphics::SimpleVAO<Attributes, unsigned short> vao;
+ std::unique_ptr<graphics::SimpleVAO<Attributes, unsigned short>> vao;
};
public:
void Tick(double dt);
+ void Erase();
+
private:
/// track time spent on a tile
void TrackStay(const Location &, double t);
#include "IdleGoal.hpp"
#include "../app/Assets.hpp"
#include "../math/const.hpp"
+#include "../ui/string.hpp"
#include "../world/Body.hpp"
#include "../world/Planet.hpp"
#include "../world/Simulation.hpp"
namespace creature {
Composition::Composition()
-: components() {
+: components()
+, total_mass(0.0) {
}
Composition::~Composition() {
components.emplace_back(res, amount);
}
std::sort(components.begin(), components.end(), CompositionCompare);
+ total_mass += amount;
}
bool Composition::Has(int res) const noexcept {
, mass(1.0)
, size(1.0)
, birth(sim.Time())
+, death(0.0)
, on_death()
, removable(false)
+, parents()
, stats()
, memory(*this)
, bg_task()
, situation()
, steering(*this)
, vao() {
+ sim.SetAlive(this);
// all creatures avoid each other for now
steering.Separate(0.1, 1.5);
}
void Creature::AddMass(int res, double amount) {
composition.Add(res, amount);
- double mass = 0.0;
double nonsolid = 0.0;
double volume = 0.0;
for (const auto &c : composition) {
- mass += c.value;
volume += c.value / sim.Assets().data.resources[c.resource].density;
if (sim.Assets().data.resources[c.resource].state != world::Resource::SOLID) {
nonsolid += c.value;
}
}
- Mass(mass);
+ Mass(composition.TotalMass());
Size(std::cbrt(volume));
- highlight_color.a = nonsolid / mass;
+ highlight_color.a = nonsolid / composition.TotalMass();
}
void Creature::HighlightColor(const glm::dvec3 &c) noexcept {
void Creature::Ingest(int res, double amount) noexcept {
// TODO: check foreign materials
- // 10% stays in body
- AddMass(res, amount * 0.1);
+ if (sim.Resources()[res].state == world::Resource::SOLID) {
+ // 15% of solids stays in body
+ AddMass(res, amount * 0.15);
+ } else {
+ // 10% of fluids stays in body
+ AddMass(res, amount * 0.05);
+ }
}
void Creature::Hurt(double amount) noexcept {
stats.Damage().Add(amount);
if (stats.Damage().Full()) {
- std::cout << "[" << int(sim.Time()) << "s] " << name << " ";
+ std::cout << "[" << ui::TimeString(sim.Time()) << "] " << name << " ";
if (stats.Exhaustion().Full()) {
std::cout << "died of exhaustion";
} else if (stats.Breath().Full()) {
} else {
std::cout << "succumed to wounds";
}
- std::cout << " at an age of ";
- {
- int age = int(Age());
- if (age >= 3600) {
- std::cout << (age / 3600) << "h ";
- age %= 3600;
- }
- if (age >= 60) {
- std::cout << (age / 60) << "m ";
- age %= 60;
- }
- std::cout << age << 's';
- }
- std::cout << " (" << int(Age() / properties.Lifetime() * 100)
- << "% of life expectancy of ";
- {
- int lt = int(properties.Lifetime());
- if (lt >= 3600) {
- std::cout << (lt / 3600) << "h ";
- lt %= 3600;
- }
- if (lt >= 60) {
- std::cout << (lt / 60) << "m ";
- lt %= 60;
- }
- std::cout << lt << 's';
- }
- std::cout << ")" << std::endl;
+ std::cout << " at an age of " << ui::TimeString(Age())
+ << " (" << ui::PercentageString(Age() / properties.Lifetime())
+ << "% of life expectancy of " << ui::TimeString(properties.Lifetime())
+ << ")" << std::endl;
Die();
}
}
void Creature::Die() noexcept {
- goals.clear();
+ sim.SetDead(this);
+ death = sim.Time();
steering.Halt();
if (on_death) {
on_death(*this);
Remove();
}
+void Creature::Remove() noexcept {
+ removable = true;
+}
+
+void Creature::Removed() noexcept {
+ bg_task.reset();
+ goals.clear();
+ memory.Erase();
+ KillVAO();
+}
+
+void Creature::AddParent(Creature &p) {
+ parents.push_back(&p);
+}
+
double Creature::Age() const noexcept {
return sim.Time() - birth;
}
}
void Creature::BuildVAO() {
- vao.Bind();
- 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, normal));
- vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
- vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
+ vao.reset(new graphics::SimpleVAO<Attributes, unsigned short>);
+ vao->Bind();
+ 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, normal));
+ vao->AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
+ vao->ReserveAttributes(6 * 4, GL_STATIC_DRAW);
{
- auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
+ auto attrib = vao->MapAttributes(GL_WRITE_ONLY);
const float offset = 1.0f;
for (int surface = 0; surface < 6; ++surface) {
const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
attrib[4 * surface + 3].texture.z = surface;
}
}
- vao.BindElements();
- vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
+ vao->BindElements();
+ vao->ReserveElements(6 * 6, GL_STATIC_DRAW);
{
- auto element = vao.MapElements(GL_WRITE_ONLY);
+ auto element = vao->MapElements(GL_WRITE_ONLY);
for (int surface = 0; surface < 3; ++surface) {
element[6 * surface + 0] = 4 * surface + 0;
element[6 * surface + 1] = 4 * surface + 2;
element[6 * surface + 5] = 4 * surface + 3;
}
}
- vao.Unbind();
+ vao->Unbind();
+}
+
+void Creature::KillVAO() {
+ vao.reset();
}
void Creature::Draw(graphics::Viewport &viewport) {
- vao.Bind();
- vao.DrawTriangles(6 * 6);
+ if (!vao) return;
+ vao->Bind();
+ vao->DrawTriangles(6 * 6);
}
void Split(Creature &c) {
Creature *a = new Creature(c.GetSimulation());
const Situation &s = c.GetSituation();
+ a->AddParent(c);
a->Name(c.GetSimulation().Assets().name.Sequential());
c.GetGenome().Configure(*a);
for (const auto &cmp : c.GetComposition()) {
s.GetPlanet(), s.Surface(),
s.Position() + glm::dvec3(0.0, 0.55 * a->Size(), 0.0));
a->BuildVAO();
- std::cout << "[" << int(c.GetSimulation().Time()) << "s] "
+ std::cout << "[" << ui::TimeString(c.GetSimulation().Time()) << "] "
<< a->Name() << " was born" << std::endl;
Creature *b = new Creature(c.GetSimulation());
+ b->AddParent(c);
b->Name(c.GetSimulation().Assets().name.Sequential());
c.GetGenome().Configure(*b);
for (const auto &cmp : c.GetComposition()) {
s.GetPlanet(), s.Surface(),
s.Position() - glm::dvec3(0.0, 0.55 * b->Size(), 0.0));
b->BuildVAO();
- std::cout << "[" << int(c.GetSimulation().Time()) << "s] "
+ std::cout << "[" << ui::TimeString(c.GetSimulation().Time()) << "] "
<< b->Name() << " was born" << std::endl;
c.Die();
Memory::~Memory() {
}
+void Memory::Erase() {
+ known_types.clear();
+}
+
void Memory::Tick(double dt) {
Situation &s = c.GetSituation();
if (s.OnTile()) {
void BlobBackgroundTask::Tick(double dt) {
if (breathing) {
// TODO: derive breathing ability
+ int gas = Assets().data.resources["air"].id;
+ // TODO: check if in compatible atmosphere
double amount = GetCreature().GetStats().Breath().gain * -(1.5 + 0.5 * GetCreature().ExhaustionFactor());
GetCreature().GetStats().Breath().Add(amount * dt);
+ // maintain ~2.5% gas composition
+ double gas_amount = GetCreature().GetComposition().Get(gas);
+ if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.025) {
+ double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
+ GetCreature().Ingest(gas, add);
+ }
if (GetCreature().GetStats().Breath().Empty()) {
breathing = false;
}
#include "Panel.hpp"
+#include <vector>
+
namespace blobs {
namespace app {
class CreaturePanel {
public:
- explicit CreaturePanel(const app::Assets &);
+ explicit CreaturePanel(app::Assets &);
~CreaturePanel();
CreaturePanel(const CreaturePanel &) = delete;
bool Shown() const noexcept { return c; }
const creature::Creature &GetCreature() const noexcept { return *c; }
- void Draw(app::Assets &, graphics::Viewport &) noexcept;
-
-private:
- void CreateNeeds();
+ void Draw(graphics::Viewport &) noexcept;
private:
- const app::Assets &assets;
+ app::Assets &assets;
creature::Creature *c;
Label *name;
+ Label *parents;
Label *born;
Label *age;
Label *mass;
- Label *pos;
- Label *vel;
- Label *dir;
- Label *tile;
Label *goal;
+ Panel *composition;
+ std::vector<Label *> components;
Meter *stats[7];
Label *props[8];
Panel panel;
public:
Label *Text(const std::string &);
- Label *Decimal(double n, int prec = 2);
- Label *Length(double m);
- Label *Mass(double kg);
- Label *Percentage(double n);
- Label *Time(double s);
Label *Font(const graphics::Font &);
Label *Foreground(const glm::vec4 &);
Label *Background(const glm::vec4 &);
--- /dev/null
+#ifndef BLOBS_UI_RECORDSPANEL_HPP_
+#define BLOBS_UI_RECORDSPANEL_HPP_
+
+#include "Panel.hpp"
+
+
+namespace blobs {
+namespace graphics {
+ class Viewport;
+}
+namespace world {
+ class Simulation;
+}
+namespace ui {
+
+class Label;
+
+class RecordsPanel {
+
+public:
+ explicit RecordsPanel(world::Simulation &);
+ ~RecordsPanel();
+
+public:
+ void Draw(graphics::Viewport &) noexcept;
+
+private:
+ world::Simulation ∼
+ Label *live;
+ std::vector<Label *> records;
+ std::vector<Label *> holders;
+ Panel panel;
+
+};
+
+}
+}
+
+#endif
--- /dev/null
+#ifndef BLOBS_UI_TIMEPANEL_HPP_
+#define BLOBS_UI_TIMEPANEL_HPP_
+
+#include "Panel.hpp"
+
+
+namespace blobs {
+namespace graphics {
+ class Viewport;
+}
+namespace world {
+ class Body;
+ class Simulation;
+}
+namespace ui {
+
+class Label;
+
+class TimePanel {
+
+public:
+ explicit TimePanel(world::Simulation &);
+ ~TimePanel();
+
+public:
+ void SetBody(world::Body &b) noexcept { body = &b; }
+ void UnsetBody() noexcept { body = nullptr; }
+ void Draw(graphics::Viewport &) noexcept;
+
+private:
+ world::Simulation ∼
+ world::Body *body;
+ Label *time;
+ Label *clock;
+ Panel panel;
+
+};
+
+}
+}
+
+#endif
--- /dev/null
+#ifndef BLOBS_UI_STRING_HPP_
+#define BLOBS_UI_STRING_HPP_
+
+#include "../math/glm.hpp"
+
+#include <string>
+
+namespace blobs {
+namespace ui {
+
+std::string DecimalString(double n, int p);
+std::string LengthString(double m);
+std::string MassString(double kg);
+std::string NumberString(int n);
+std::string PercentageString(double n);
+std::string TimeString(double s);
+std::string VectorString(const glm::dvec3 &, int p);
+std::string VectorString(const glm::ivec2 &);
+
+}
+}
+
+#endif
#include "CreaturePanel.hpp"
+#include "RecordsPanel.hpp"
+#include "string.hpp"
+#include "TimePanel.hpp"
#include "Label.hpp"
#include "Meter.hpp"
#include "../app/Assets.hpp"
#include "../creature/Creature.hpp"
#include "../graphics/Viewport.hpp"
+#include "../world/Body.hpp"
+#include "../world/Simulation.hpp"
#include <iomanip>
#include <iostream>
namespace blobs {
namespace ui {
-CreaturePanel::CreaturePanel(const app::Assets &assets)
+CreaturePanel::CreaturePanel(app::Assets &assets)
: assets(assets)
, c(nullptr)
, name(new Label(assets.fonts.large))
+, parents(new Label(assets.fonts.medium))
, born(new Label(assets.fonts.medium))
, age(new Label(assets.fonts.medium))
, mass(new Label(assets.fonts.medium))
-, pos(new Label(assets.fonts.medium))
-, vel(new Label(assets.fonts.medium))
-, dir(new Label(assets.fonts.medium))
-, tile(new Label(assets.fonts.medium))
, goal(new Label(assets.fonts.medium))
+, composition(new Panel)
, stats{nullptr}
, props{nullptr}
, panel() {
- born->Text("00h 00m 00s");
+ Label *parents_label = new Label(assets.fonts.medium);
+ parents_label->Text("Parents");
Label *born_label = new Label(assets.fonts.medium);
born_label->Text("Born");
- Panel *born_panel = new Panel;
- born_panel
- ->Add(born_label)
- ->Add(born)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- age->Text("00h 00m 00s");
Label *age_label = new Label(assets.fonts.medium);
age_label->Text("Age");
- Panel *age_panel = new Panel;
- age_panel
- ->Add(age_label)
- ->Add(age)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- mass->Text("00.000kg");
Label *mass_label = new Label(assets.fonts.medium);
mass_label->Text("Mass");
- Panel *mass_panel = new Panel;
- mass_panel
- ->Add(mass_label)
- ->Add(mass)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- pos->Text("<00.0, 00.0, 00.0>");
- Label *pos_label = new Label(assets.fonts.medium);
- pos_label->Text("Pos");
- Panel *pos_panel = new Panel;
- pos_panel
- ->Add(pos_label)
- ->Add(pos)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- vel->Text("<00.0, 00.0, 00.0>");
- Label *vel_label = new Label(assets.fonts.medium);
- vel_label->Text("Vel");
- Panel *vel_panel = new Panel;
- vel_panel
- ->Add(vel_label)
- ->Add(vel)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- dir->Text("<0.00, 0.00, 0.00>");
- Label *dir_label = new Label(assets.fonts.medium);
- dir_label->Text("Dir");
- Panel *dir_panel = new Panel;
- dir_panel
- ->Add(dir_label)
- ->Add(dir)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- tile->Text("<00, 00> (mountains)");
- Label *tile_label = new Label(assets.fonts.medium);
- tile_label->Text("Tile");
- Panel *tile_panel = new Panel;
- tile_panel
- ->Add(tile_label)
- ->Add(tile)
- ->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
-
- goal->Text("long goal description");
Label *goal_label = new Label(assets.fonts.medium);
goal_label->Text("Goal");
- Panel *goal_panel = new Panel;
- goal_panel
- ->Add(goal_label)
- ->Add(goal)
+
+ Panel *info_label_panel = new Panel;
+ info_label_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(parents_label)
+ ->Add(born_label)
+ ->Add(age_label)
+ ->Add(mass_label)
+ ->Add(goal_label);
+ Panel *info_value_panel = new Panel;
+ info_value_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(parents)
+ ->Add(born)
+ ->Add(age)
+ ->Add(mass)
+ ->Add(goal);
+ Panel *info_panel = new Panel;
+ info_panel
+ ->Direction(Panel::HORIZONTAL)
->Spacing(10.0f)
- ->Direction(Panel::HORIZONTAL);
+ ->Add(info_label_panel)
+ ->Add(info_value_panel);
Label *stat_label[7];
for (int i = 0; i < 7; ++i) {
panel
.Add(name)
- ->Add(age_panel)
- ->Add(born_panel)
- ->Add(mass_panel)
- ->Add(pos_panel)
- ->Add(vel_panel)
- ->Add(dir_panel)
- ->Add(tile_panel)
- ->Add(goal_panel)
+ ->Add(info_panel)
+ ->Add(composition)
->Add(stat_panel)
->Add(prop_panel)
->Padding(glm::vec2(10.0f))
void CreaturePanel::Show(creature::Creature &cr) {
c = &cr;
name->Text(c->Name());
- born->Time(c->Born());
+ born->Text(TimeString(c->Born()));
+
+ if (c->Parents().empty()) {
+ parents->Text("none");
+ } else {
+ std::string parent_string;
+ bool first = true;
+ for (auto p : c->Parents()) {
+ if (first) {
+ first = false;
+ } else {
+ parent_string += " and ";
+ }
+ parent_string += p->Name();
+ }
+ parents->Text(parent_string);
+ }
}
void CreaturePanel::Hide() noexcept {
c = nullptr;
}
-void CreaturePanel::Draw(app::Assets &assets, graphics::Viewport &viewport) noexcept {
+void CreaturePanel::Draw(graphics::Viewport &viewport) noexcept {
if (!c) return;
- age->Time(c->Age());
- mass->Mass(c->Mass());
- {
- const glm::dvec3 &p = c->GetSituation().Position();
- std::stringstream ss;
- ss << std::fixed << std::setprecision(1)
- << "<" << p.x << ", " << p.y << ", " << p.z << ">";
- pos->Text(ss.str());
- }
- {
- const glm::dvec3 &v = c->GetSituation().Velocity();
- std::stringstream ss;
- ss << std::fixed << std::setprecision(1)
- << "<" << v.x << ", " << v.y << ", " << v.z << ">";
- vel->Text(ss.str());
- }
- {
- const glm::dvec3 &d = c->GetSituation().GetState().dir;
- std::stringstream ss;
- ss << std::fixed << std::setprecision(2)
- << "<" << d.x << ", " << d.y << ", " << d.z << ">";
- dir->Text(ss.str());
- }
- {
- glm::ivec2 t = c->GetSituation().SurfacePosition();
- std::stringstream ss;
- ss << std::fixed << std::setprecision(1)
- << "<" << t.x << ", " << t.y
- << "> (" << c->GetSituation().GetTileType().label << ")";
- tile->Text(ss.str());
- }
+ age->Text(TimeString(c->Age()));
+ mass->Text(MassString(c->Mass()));
if (c->Goals().empty()) {
goal->Text("none");
} else {
goal->Text(c->Goals()[0]->Describe());
}
+ const creature::Composition &comp = c->GetComposition();
+ if (comp.size() < components.size()) {
+ composition->Clear();
+ while (comp.size() < components.size()) {
+ delete components.back();
+ components.pop_back();
+ }
+ for (auto l : components) {
+ composition->Add(l);
+ }
+ } else {
+ while (comp.size() > components.size()) {
+ components.emplace_back(new Label(assets.fonts.medium));
+ composition->Add(components.back());
+ }
+ }
+ {
+ int i = 0;
+ for (auto &cmp : comp) {
+ components[i]->Text(
+ PercentageString(cmp.value / comp.TotalMass())
+ + " " + assets.data.resources[cmp.resource].label);
+ ++i;
+ }
+ }
+
for (int i = 0; i < 7; ++i) {
stats[i]->Value(c->GetStats().stat[i].value);
if (c->GetStats().stat[i].Okay()) {
}
}
- props[0]->Decimal(c->Strength());
- props[1]->Decimal(c->Stamina());
- props[2]->Decimal(c->Dexerty());
- props[3]->Decimal(c->Intelligence());
- props[4]->Time(c->Lifetime());
- props[5]->Percentage(c->Fertility());
- props[6]->Percentage(c->Mutability());
- props[7]->Mass(c->OffspringMass());
+ props[0]->Text(DecimalString(c->Strength(), 2));
+ props[1]->Text(DecimalString(c->Stamina(), 2));
+ props[2]->Text(DecimalString(c->Dexerty(), 2));
+ props[3]->Text(DecimalString(c->Intelligence(), 2));
+ props[4]->Text(TimeString(c->Lifetime()));
+ props[5]->Text(PercentageString(c->Fertility()));
+ props[6]->Text(PercentageString(c->Mutability()));
+ props[7]->Text(MassString(c->OffspringMass()));
const glm::vec2 margin(20.0f);
-
panel.Position(glm::vec2(viewport.Width() - margin.x - panel.Size().x, margin.y));
panel.Draw(assets, viewport);
}
+
+RecordsPanel::RecordsPanel(world::Simulation &sim)
+: sim(sim)
+, live(new Label(sim.Assets().fonts.medium))
+, records()
+, holders()
+, panel() {
+ Label *live_label = new Label(sim.Assets().fonts.medium);
+ live_label->Text("Creatures alive");
+
+ Panel *label_panel = new Panel;
+ label_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(live_label);
+
+ Panel *value_panel = new Panel;
+ value_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(live);
+
+ Label *holder_label = new Label(sim.Assets().fonts.medium);
+ holder_label->Text("Holder");
+ Panel *holder_panel = new Panel;
+ holder_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(holder_label);
+
+ records.reserve(sim.Records().size());
+ for (const auto &r : sim.Records()) {
+ Label *label = new Label(sim.Assets().fonts.medium);
+ label->Text(r.name + " record");
+ label_panel->Add(label);
+ Label *value = new Label(sim.Assets().fonts.medium);
+ value->Text("none");
+ value_panel->Add(value);
+ records.push_back(value);
+ Label *holder = new Label(sim.Assets().fonts.medium);
+ holder->Text("nobody");
+ holder_panel->Add(holder);
+ holders.push_back(holder);
+ }
+
+ panel
+ .Direction(Panel::HORIZONTAL)
+ ->Padding(glm::vec2(10.0f))
+ ->Spacing(10.0f)
+ ->Background(glm::vec4(0.7f, 0.7f, 0.7f, 1.0f))
+ ->Add(label_panel)
+ ->Add(value_panel)
+ ->Add(holder_panel);
+}
+
+RecordsPanel::~RecordsPanel() {
+}
+
+void RecordsPanel::Draw(graphics::Viewport &viewport) noexcept {
+ live->Text(NumberString(sim.LiveCreatures().size()));
+ int i = 0;
+ for (const auto &r : sim.Records()) {
+ if (!r) continue;
+ switch (r.type) {
+ default:
+ case world::Record::VALUE:
+ records[i]->Text(DecimalString(r.value, 2));
+ break;
+ case world::Record::LENGTH:
+ records[i]->Text(LengthString(r.value));
+ break;
+ case world::Record::MASS:
+ records[i]->Text(MassString(r.value));
+ break;
+ case world::Record::PERCENTAGE:
+ records[i]->Text(PercentageString(r.value));
+ break;
+ case world::Record::TIME:
+ records[i]->Text(TimeString(r.value));
+ break;
+ }
+ std::string str(r.holder->Name());
+ bool first = true;
+ for (auto p : r.holder->Parents()) {
+ if (first) {
+ first = false;
+ str += " of ";
+ } else {
+ str += " and ";
+ }
+ str += p->Name();
+ }
+ holders[i]->Text(str);
+ ++i;
+ }
+
+ const glm::vec2 margin(20.0f);
+ panel.Position(glm::vec2(margin.x, margin.y));
+ panel.Draw(sim.Assets(), viewport);
+}
+
+
+TimePanel::TimePanel(world::Simulation &sim)
+: sim(sim)
+, body(nullptr)
+, time(new Label(sim.Assets().fonts.medium))
+, clock(new Label(sim.Assets().fonts.medium))
+, panel() {
+ Label *time_label = new Label(sim.Assets().fonts.medium);
+ time_label->Text("Time");
+ Label *clock_label = new Label(sim.Assets().fonts.medium);
+ clock_label->Text("Clock");
+
+ Panel *label_panel = new Panel;
+ label_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(time_label)
+ ->Add(clock_label);
+
+ Panel *value_panel = new Panel;
+ value_panel
+ ->Direction(Panel::VERTICAL)
+ ->Add(time)
+ ->Add(clock);
+
+ panel
+ .Direction(Panel::HORIZONTAL)
+ ->Padding(glm::vec2(10.0f))
+ ->Spacing(10.0f)
+ ->Background(glm::vec4(0.7f, 0.7f, 0.7f, 1.0f))
+ ->Add(label_panel)
+ ->Add(value_panel);
+}
+
+TimePanel::~TimePanel() {
+}
+
+void TimePanel::Draw(graphics::Viewport &viewport) noexcept {
+ time->Text(TimeString(sim.Time()));
+ if (body) {
+ clock->Text(TimeString(std::fmod(sim.Time(), body->RotationalPeriod())));
+ } else {
+ clock->Text("no reference");
+ }
+
+ const glm::vec2 margin(20.0f);
+ panel.Position(glm::vec2(margin.x, viewport.Height() - margin.y - panel.Size().y));
+ panel.Draw(sim.Assets(), viewport);
+}
+
+
+std::string DecimalString(double n, int p) {
+ std::stringstream s;
+ s << std::fixed << std::setprecision(p) << n;
+ return s.str();
+}
+
+std::string LengthString(double m) {
+ std::stringstream s;
+ s << std::fixed << std::setprecision(3);
+ if (m > 1500.0) {
+ s << (m * 0.001) << "km";
+ } else if (m < 0.1) {
+ s << (m * 1000.0) << "mm";
+ } else {
+ s << m << "m";
+ }
+ return s.str();
+}
+
+std::string MassString(double kg) {
+ std::stringstream s;
+ s << std::fixed << std::setprecision(3);
+ if (kg > 1500.0) {
+ s << (kg * 0.001) << "t";
+ } else if (kg < 1.0) {
+ s << (kg * 1000.0) << "g";
+ } else if (kg < 0.001) {
+ s << (kg * 1.0e6) << "mg";
+ } else {
+ s << kg << "kg";
+ }
+ return s.str();
+}
+
+std::string NumberString(int n) {
+ return std::to_string(n);
+}
+
+std::string PercentageString(double n) {
+ std::stringstream s;
+ s << std::fixed << std::setprecision(1) << (n * 100.0) << '%';
+ return s.str();
+}
+
+std::string TimeString(double s) {
+ int is = int(s);
+ std::stringstream ss;
+ if (is >= 3600) {
+ ss << (is / 3600) << "h ";
+ is %= 3600;
+ }
+ if (is >= 60) {
+ ss << (is / 60) << "m ";
+ is %= 60;
+ }
+ ss << is << 's';
+ return ss.str();
+}
+
+std::string VectorString(const glm::dvec3 &v, int p) {
+ std::stringstream ss;
+ ss << std::fixed << std::setprecision(p)
+ << "<" << v.x << ", " << v.y << ", " << v.z << ">";
+ return ss.str();
+}
+
+std::string VectorString(const glm::ivec2 &v) {
+ std::stringstream ss;
+ ss << "<" << v.x << ", " << v.y << ">";
+ return ss.str();
+}
+
}
}
return this;
}
-Label *Label::Decimal(double n, int prec) {
- std::stringstream s;
- s << std::fixed << std::setprecision(prec) << n;
- return Text(s.str());
-}
-
-Label *Label::Length(double m) {
- std::stringstream s;
- s << std::fixed << std::setprecision(3);
- if (m > 1500.0) {
- s << (m * 0.001) << "km";
- } else if (m < 0.1) {
- s << (m * 1000.0) << "mm";
- } else {
- s << m << "m";
- }
- return Text(s.str());
-}
-
-Label *Label::Mass(double kg) {
- std::stringstream s;
- s << std::fixed << std::setprecision(3);
- if (kg > 1500.0) {
- s << (kg * 0.001) << "t";
- } else if (kg < 0.1) {
- s << (kg * 1000.0) << "g";
- } else if (kg < 0.0001) {
- s << (kg * 1.0e6) << "mg";
- } else {
- s << kg << "kg";
- }
- return Text(s.str());
-}
-
-Label *Label::Percentage(double n) {
- std::stringstream s;
- s << std::fixed << std::setprecision(1) << (n * 100.0) << '%';
- return Text(s.str());
-}
-
-Label *Label::Time(double s) {
- int is = int(s);
- std::stringstream ss;
- if (is >= 3600) {
- ss << (is / 3600) << "h ";
- is %= 3600;
- }
- if (is >= 60) {
- ss << (is / 60) << "m ";
- is %= 60;
- }
- ss << is << 's';
- return Text(ss.str());
-}
-
Label *Label::Font(const graphics::Font &f) {
if (font != &f) {
dirty = true;
--- /dev/null
+#ifndef BLOBS_WORLD_RECORD_HPP_
+#define BLOBS_WORLD_RECORD_HPP_
+
+#include <string>
+
+
+namespace blobs {
+namespace creature {
+ class Creature;
+}
+namespace world {
+
+struct Record {
+
+ std::string name = "";
+ creature::Creature *holder = nullptr;
+ double value = 0.0;
+ double time = 0.0;
+ enum Type {
+ VALUE,
+ LENGTH,
+ MASS,
+ PERCENTAGE,
+ TIME,
+ } type = VALUE;
+
+ operator bool() const noexcept { return holder; }
+
+};
+
+}
+}
+
+#endif
#ifndef BLOBS_WORLD_SIMULATION_HPP_
#define BLOBS_WORLD_SIMULATION_HPP_
+#include "Record.hpp"
#include "Set.hpp"
#include "../app/Assets.hpp"
#include <set>
+#include <vector>
namespace blobs {
+namespace creature {
+ class Creature;
+}
namespace world {
class Body;
public:
void Tick(double dt);
- void AddBody(Body &);
- void AddPlanet(Planet &);
- void AddSun(Sun &);
-
Body &Root() noexcept { return root; }
const Body &Root() const noexcept { return root; }
const Set<Resource> &Resources() const noexcept { return assets.data.resources; }
const Set<TileType> &TileTypes() const noexcept { return assets.data.tile_types; }
+ void AddBody(Body &);
+ void AddPlanet(Planet &);
+ void AddSun(Sun &);
+
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; }
+ void SetAlive(creature::Creature *);
+ std::vector<creature::Creature *> &LiveCreatures() noexcept { return alive; }
+ const std::vector<creature::Creature *> &LiveCreatures() const noexcept { return alive; }
+
+ void SetDead(creature::Creature *);
+ std::vector<creature::Creature *> &DeadCreatures() noexcept { return dead; }
+ const std::vector<creature::Creature *> &DeadCreatures() const noexcept { return dead; }
+
double Time() const noexcept { return time; }
+ const std::vector<Record> &Records() const noexcept { return records; }
+ void CheckRecords(creature::Creature &) noexcept;
+
private:
Body &root;
app::Assets &assets;
+
std::set<Body *> bodies;
std::set<Planet *> planets;
std::set<Sun *> suns;
+
+ std::vector<creature::Creature *> alive;
+ std::vector<creature::Creature *> dead;
+
double time;
+ std::vector<Record> records;
};
#include "Body.hpp"
#include "Planet.hpp"
#include "Sun.hpp"
+#include "../creature/Creature.hpp"
+
+#include <algorithm>
namespace blobs {
, bodies()
, planets()
, suns()
-, time(0.0) {
+, alive()
+, dead()
+, time(0.0)
+, records(7) {
AddBody(r);
+ records[0].name = "Age";
+ records[0].type = Record::TIME;
+ records[1].name = "Mass";
+ records[1].type = Record::MASS;
+ records[2].name = "Size";
+ records[2].type = Record::LENGTH;
+ records[3].name = "Strength";
+ records[4].name = "Stamina";
+ records[5].name = "Dexerty";
+ records[6].name = "Intelligence";
}
Simulation::~Simulation() {
+ for (auto c : alive) {
+ delete c;
+ }
+ for (auto c : dead) {
+ delete c;
+ }
}
+void Simulation::Tick(double dt) {
+ time += dt;
+ for (auto body : bodies) {
+ body->Tick(dt);
+ }
+ for (auto c : alive) {
+ CheckRecords(*c);
+ }
+}
void Simulation::AddBody(Body &b) {
b.SetSimulation(*this);
suns.insert(&s);
}
-void Simulation::Tick(double dt) {
- time += dt;
- for (auto body : bodies) {
- body->Tick(dt);
+void Simulation::SetAlive(creature::Creature *c) {
+ alive.push_back(c);
+}
+
+void Simulation::SetDead(creature::Creature *c) {
+ auto entry = std::find(alive.begin(), alive.end(), c);
+ if (entry != alive.end()) {
+ alive.erase(entry);
+ }
+ dead.push_back(c);
+ CheckRecords(*c);
+}
+
+void Simulation::CheckRecords(creature::Creature &c) noexcept {
+ if (c.Age() > records[0].value) {
+ records[0].value = c.Age();
+ records[0].time = Time();
+ records[0].holder = &c;
+ }
+ if (c.Mass() > records[1].value) {
+ records[1].value = c.Mass();
+ records[1].time = Time();
+ records[1].holder = &c;
+ }
+ if (c.Size() > records[2].value) {
+ records[2].value = c.Size();
+ records[2].time = Time();
+ records[2].holder = &c;
+ }
+ if (c.Strength() > records[3].value) {
+ records[3].value = c.Strength();
+ records[3].time = Time();
+ records[3].holder = &c;
+ }
+ if (c.Stamina() > records[4].value) {
+ records[4].value = c.Stamina();
+ records[4].time = Time();
+ records[4].holder = &c;
+ }
+ if (c.Dexerty() > records[5].value) {
+ records[5].value = c.Dexerty();
+ records[5].time = Time();
+ records[5].holder = &c;
+ }
+ if (c.Intelligence() > records[6].value) {
+ records[6].value = c.Intelligence();
+ records[6].time = Time();
+ records[6].holder = &c;
}
}
}
Body::~Body() {
- for (creature::Creature *c : creatures) {
- delete c;
- }
}
void Body::SetSimulation(Simulation &s) noexcept {
// first remove creatures so they don't collide
for (auto c = Creatures().begin(); c != Creatures().end();) {
if ((*c)->Removable()) {
- delete *c;
+ (*c)->Removed();
c = Creatures().erase(c);
} else {
++c;