1 #include "Creature.hpp"
4 #include "NameGenerator.hpp"
5 #include "Situation.hpp"
6 #include "Steering.hpp"
9 #include "IdleGoal.hpp"
10 #include "InhaleNeed.hpp"
11 #include "IngestNeed.hpp"
13 #include "../app/Assets.hpp"
14 #include "../world/Body.hpp"
15 #include "../world/Planet.hpp"
16 #include "../world/Simulation.hpp"
17 #include "../world/TileType.hpp"
21 #include <glm/gtx/transform.hpp>
24 #include <glm/gtx/io.hpp>
30 Creature::Creature(world::Simulation &sim)
51 Creature::~Creature() {
54 void Creature::Grow(double amount) noexcept {
55 Mass(std::min(properties.props[cur_prop].mass, mass + amount));
58 void Creature::Hurt(double dt) noexcept {
59 health = std::max(0.0, health - dt);
61 std::cout << "[" << int(sim.Time()) << "s] "
62 << name << " died" << std::endl;
67 void Creature::Die() noexcept {
77 double Creature::Size() const noexcept {
81 double Creature::Age() const noexcept {
82 return sim.Time() - birth;
85 double Creature::Fertility() const noexcept {
86 // TODO: lerp based on age?
87 return properties.props[cur_prop].fertility / 3600.0;
90 void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
91 std::cout << "[" << int(sim.Time()) << "s] " << name << " new goal: " << g->Describe() << std::endl;
93 goals.emplace_back(std::move(g));
98 bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
99 return b->Urgency() < a->Urgency();
104 void Creature::Tick(double dt) {
105 if (cur_prop < 5 && Age() > properties.props[cur_prop + 1].age) {
108 std::cout << "[" << int(sim.Time()) << "s] "
109 << name << " died of old age" << std::endl;
115 Situation::State state(situation.GetState());
116 Situation::Derivative a(Step(Situation::Derivative(), 0.0));
117 Situation::Derivative b(Step(a, dt * 0.5));
118 Situation::Derivative c(Step(b, dt * 0.5));
119 Situation::Derivative d(Step(c, dt));
120 Situation::Derivative f(
121 (1.0 / 6.0) * (a.vel + 2.0 * (b.vel + c.vel) + d.vel),
122 (1.0 / 6.0) * (a.acc + 2.0 * (b.acc + c.acc) + d.acc)
124 state.pos += f.vel * dt;
125 state.vel += f.acc * dt;
126 situation.SetState(state);
130 for (auto &need : needs) {
133 for (auto &goal : goals) {
136 // do background stuff
137 for (auto &need : needs) {
138 need->ApplyEffect(*this, dt);
143 // if active goal can be interrupted, check priorities
144 if (goals.size() > 1 && goals[0]->Interruptible()) {
145 Goal *old_top = &*goals[0];
146 std::sort(goals.begin(), goals.end(), GoalCompare);
147 Goal *new_top = &*goals[0];
148 if (new_top != old_top) {
149 std::cout << "[" << int(sim.Time()) << "s] " << name
150 << " changing goal from " << old_top->Describe()
151 << " to " << new_top->Describe() << std::endl;
155 for (auto goal = goals.begin(); goal != goals.end();) {
156 if ((*goal)->Complete()) {
157 std::cout << "[" << int(sim.Time()) << "s] " << name
158 << " complete goal: " << (*goal)->Describe() << std::endl;
166 Situation::Derivative Creature::Step(const Situation::Derivative &ds, double dt) const noexcept {
167 Situation::State s = situation.GetState();
168 s.pos += ds.vel * dt;
169 s.vel += ds.acc * dt;
170 return { s.vel, steering.Acceleration(s) };
173 glm::dmat4 Creature::LocalTransform() noexcept {
174 // TODO: surface transform
175 const double half_size = size * 0.5;
176 const glm::dvec3 &pos = situation.Position();
177 return glm::translate(glm::dvec3(pos.x, pos.y, pos.z + half_size))
178 * glm::scale(glm::dvec3(half_size, half_size, half_size));
181 void Creature::BuildVAO() {
183 vao.BindAttributes();
184 vao.EnableAttribute(0);
185 vao.EnableAttribute(1);
186 vao.EnableAttribute(2);
187 vao.AttributePointer<glm::vec3>(0, false, offsetof(Attributes, position));
188 vao.AttributePointer<glm::vec3>(1, false, offsetof(Attributes, normal));
189 vao.AttributePointer<glm::vec3>(2, false, offsetof(Attributes, texture));
190 vao.ReserveAttributes(6 * 4, GL_STATIC_DRAW);
192 auto attrib = vao.MapAttributes(GL_WRITE_ONLY);
193 const float offset = 1.0f;
194 for (int surface = 0; surface < 6; ++surface) {
195 const float tex_u_begin = surface < 3 ? 1.0f : 0.0f;
196 const float tex_u_end = surface < 3 ? 0.0f : 1.0f;
198 attrib[4 * surface + 0].position[(surface + 0) % 3] = -offset;
199 attrib[4 * surface + 0].position[(surface + 1) % 3] = -offset;
200 attrib[4 * surface + 0].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
201 attrib[4 * surface + 0].normal[(surface + 0) % 3] = 0.0f;
202 attrib[4 * surface + 0].normal[(surface + 1) % 3] = 0.0f;
203 attrib[4 * surface + 0].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
204 attrib[4 * surface + 0].texture.x = tex_u_begin;
205 attrib[4 * surface + 0].texture.y = 1.0f;
206 attrib[4 * surface + 0].texture.z = surface;
208 attrib[4 * surface + 1].position[(surface + 0) % 3] = -offset;
209 attrib[4 * surface + 1].position[(surface + 1) % 3] = offset;
210 attrib[4 * surface + 1].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
211 attrib[4 * surface + 1].normal[(surface + 0) % 3] = 0.0f;
212 attrib[4 * surface + 1].normal[(surface + 1) % 3] = 0.0f;
213 attrib[4 * surface + 1].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
214 attrib[4 * surface + 1].texture.x = tex_u_end;
215 attrib[4 * surface + 1].texture.y = 1.0f;
216 attrib[4 * surface + 1].texture.z = surface;
218 attrib[4 * surface + 2].position[(surface + 0) % 3] = offset;
219 attrib[4 * surface + 2].position[(surface + 1) % 3] = -offset;
220 attrib[4 * surface + 2].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
221 attrib[4 * surface + 2].normal[(surface + 0) % 3] = 0.0f;
222 attrib[4 * surface + 2].normal[(surface + 1) % 3] = 0.0f;
223 attrib[4 * surface + 2].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
224 attrib[4 * surface + 2].texture.x = tex_u_begin;
225 attrib[4 * surface + 2].texture.y = 0.0f;
226 attrib[4 * surface + 2].texture.z = surface;
228 attrib[4 * surface + 3].position[(surface + 0) % 3] = offset;
229 attrib[4 * surface + 3].position[(surface + 1) % 3] = offset;
230 attrib[4 * surface + 3].position[(surface + 2) % 3] = surface < 3 ? offset : -offset;
231 attrib[4 * surface + 3].normal[(surface + 0) % 3] = 0.0f;
232 attrib[4 * surface + 3].normal[(surface + 1) % 3] = 0.0f;
233 attrib[4 * surface + 3].normal[(surface + 2) % 3] = surface < 3 ? 1.0f : -1.0f;
234 attrib[4 * surface + 3].texture.x = tex_u_end;
235 attrib[4 * surface + 3].texture.y = 0.0f;
236 attrib[4 * surface + 3].texture.z = surface;
240 vao.ReserveElements(6 * 6, GL_STATIC_DRAW);
242 auto element = vao.MapElements(GL_WRITE_ONLY);
243 for (int surface = 0; surface < 3; ++surface) {
244 element[6 * surface + 0] = 4 * surface + 0;
245 element[6 * surface + 1] = 4 * surface + 2;
246 element[6 * surface + 2] = 4 * surface + 1;
247 element[6 * surface + 3] = 4 * surface + 1;
248 element[6 * surface + 4] = 4 * surface + 2;
249 element[6 * surface + 5] = 4 * surface + 3;
251 for (int surface = 3; surface < 6; ++surface) {
252 element[6 * surface + 0] = 4 * surface + 0;
253 element[6 * surface + 1] = 4 * surface + 1;
254 element[6 * surface + 2] = 4 * surface + 2;
255 element[6 * surface + 3] = 4 * surface + 2;
256 element[6 * surface + 4] = 4 * surface + 1;
257 element[6 * surface + 5] = 4 * surface + 3;
263 void Creature::Draw(graphics::Viewport &viewport) {
265 vao.DrawTriangles(6 * 6);
269 void Spawn(Creature &c, world::Planet &p) {
271 c.GetSituation().SetPlanetSurface(p, 0, p.TileCenter(0, p.SideLength() / 2, p.SideLength() / 2));
273 // probe surrounding area for common resources
274 int start = p.SideLength() / 2 - 2;
276 std::map<int, double> yields;
277 for (int y = start; y < end; ++y) {
278 for (int x = start; x < end; ++x) {
279 const world::TileType &t = p.TypeAt(0, x, y);
280 for (auto yield : t.resources) {
281 yields[yield.resource] += yield.ubiquity;
287 for (auto e : yields) {
288 if (c.GetSimulation().Resources()[e.first].state == world::Resource::LIQUID) {
289 if (liquid < 0 || e.second > yields[liquid]) {
292 } else if (c.GetSimulation().Resources()[e.first].state == world::Resource::SOLID) {
293 if (solid < 0 || e.second > yields[solid]) {
301 genome.properties.Birth().age = { 0.0, 0.0 };
302 genome.properties.Birth().mass = { 0.5, 0.05 };
303 genome.properties.Birth().fertility = { 0.0, 0.0 };
305 genome.properties.Child().age = { 30.0, 1.0 };
306 genome.properties.Child().mass = { 0.7, 0.05 };
307 genome.properties.Child().fertility = { 0.0, 0.0 };
309 genome.properties.Youth().age = { 60.0, 5.0 };
310 genome.properties.Youth().mass = { 0.9, 0.1 };
311 genome.properties.Youth().fertility = { 0.5, 0.03 };
313 genome.properties.Adult().age = { 120.0, 10.0 };
314 genome.properties.Adult().mass = { 1.2, 0.1 };
315 genome.properties.Adult().fertility = { 0.4, 0.01 };
317 genome.properties.Elder().age = { 360.0, 30.0 };
318 genome.properties.Elder().mass = { 1.0, 0.05 };
319 genome.properties.Elder().fertility = { 0.1, 0.01 };
321 genome.properties.Death().age = { 480.0, 60.0 };
322 genome.properties.Death().mass = { 0.9, 0.05 };
323 genome.properties.Death().fertility = { 0.0, 0.0 };
325 if (p.HasAtmosphere()) {
326 genome.composition.push_back({
327 p.Atmosphere(), // resource
328 { 0.01, 0.00001 }, // mass
329 { 0.5, 0.001 }, // intake
330 { 0.1, 0.0005 }, // penalty
331 { 0.0, 0.0 }, // growth
335 genome.composition.push_back({
337 { 0.6, 0.01 }, // mass
338 { 0.2, 0.001 }, // intake
339 { 0.01, 0.002 }, // penalty
340 { 0.1, 0.0 }, // growth
344 genome.composition.push_back({
346 { 0.4, 0.01 }, // mass
347 //{ 0.1, 0.001 }, // intake
348 { 0.4, 0.001 }, // intake
349 { 0.001, 0.0001 }, // penalty
350 { 10.0, 0.002 }, // growth
357 void Genome::Configure(Creature &c) const {
358 c.GetGenome() = *this;
360 math::GaloisLFSR &random = c.GetSimulation().Assets().random;
362 c.GetProperties() = Instantiate(properties, random);
366 for (const auto &comp : composition) {
367 double comp_mass = comp.mass.FakeNormal(random.SNorm());
368 double intake = comp.intake.FakeNormal(random.SNorm());
369 double penalty = comp.penalty.FakeNormal(random.SNorm());
372 volume += comp_mass / c.GetSimulation().Resources()[comp.resource].density;
374 std::unique_ptr<Need> need;
375 if (c.GetSimulation().Resources()[comp.resource].state == world::Resource::SOLID) {
376 need.reset(new IngestNeed(comp.resource, intake, penalty));
377 need->gain = intake * 0.05;
378 } else if (c.GetSimulation().Resources()[comp.resource].state == world::Resource::LIQUID) {
379 need.reset(new IngestNeed(comp.resource, intake, penalty));
380 need->gain = intake * 0.1;
382 need.reset(new InhaleNeed(comp.resource, intake, penalty));
383 need->gain = intake * 0.5;
385 need->name = c.GetSimulation().Resources()[comp.resource].label;
386 need->growth = comp.growth.FakeNormal(random.SNorm());
387 need->inconvenient = 0.5;
388 need->critical = 0.95;
389 c.AddNeed(std::move(need));
392 c.Mass(c.GetProperties().props[0].mass);
393 c.Density(mass / volume);
394 c.GetSteering().MaxAcceleration(1.4);
395 c.GetSteering().MaxSpeed(4.4);
396 c.AddGoal(std::unique_ptr<Goal>(new IdleGoal(c)));
400 void Split(Creature &c) {
401 Creature *a = new Creature(c.GetSimulation());
402 const Situation &s = c.GetSituation();
403 // TODO: generate names
404 a->Name(c.GetSimulation().Assets().name.Sequential());
406 c.GetGenome().Configure(*a);
407 s.GetPlanet().AddCreature(a);
408 // TODO: duplicate situation somehow
409 a->GetSituation().SetPlanetSurface(
410 s.GetPlanet(), s.Surface(),
411 s.Position() + glm::dvec3(0.0, a->Size() * 0.51, 0.0));
414 Creature *b = new Creature(c.GetSimulation());
415 b->Name(c.GetSimulation().Assets().name.Sequential());
416 c.GetGenome().Configure(*b);
417 s.GetPlanet().AddCreature(b);
418 b->GetSituation().SetPlanetSurface(
419 s.GetPlanet(), s.Surface(),
420 s.Position() + glm::dvec3(0.0, b->Size() * -0.51, 0.0));
427 Memory::Memory(Creature &c)
434 void Memory::Tick(double dt) {
435 Situation &s = c.GetSituation();
437 TrackStay({ &s.GetPlanet(), s.Surface(), s.SurfacePosition() }, dt);
441 void Memory::TrackStay(const Location &l, double t) {
442 const world::TileType &type = l.planet->TypeAt(l.surface, l.coords.x, l.coords.y);
443 auto entry = known_types.find(type.id);
444 if (entry != known_types.end()) {
445 entry->second.last_been = c.GetSimulation().Time();
446 entry->second.last_loc = l;
447 entry->second.time_spent += t;
449 known_types.emplace(type.id, Stay{
450 c.GetSimulation().Time(),
452 c.GetSimulation().Time(),
460 NameGenerator::NameGenerator()
464 NameGenerator::~NameGenerator() {
467 std::string NameGenerator::Sequential() {
468 std::stringstream ss;
469 ss << "Blob " << ++counter;
474 Situation::Situation()
476 , state(glm::dvec3(0.0), glm::dvec3(0.0))
481 Situation::~Situation() {
484 bool Situation::OnPlanet() const noexcept {
485 return type == PLANET_SURFACE;
488 bool Situation::OnSurface() const noexcept {
489 return type == PLANET_SURFACE;
492 bool Situation::OnTile() const noexcept {
493 glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
494 return type == PLANET_SURFACE
495 && t.x >= 0 && t.x < planet->SideLength()
496 && t.y >= 0 && t.y < planet->SideLength();
499 glm::ivec2 Situation::SurfacePosition() const noexcept {
500 return planet->SurfacePosition(surface, state.pos);
503 world::Tile &Situation::GetTile() const noexcept {
504 glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
505 return planet->TileAt(surface, t.x, t.y);
508 const world::TileType &Situation::GetTileType() const noexcept {
509 glm::ivec2 t(planet->SurfacePosition(surface, state.pos));
510 return planet->TypeAt(surface, t.x, t.y);
513 void Situation::Move(const glm::dvec3 &dp) noexcept {
516 // enforce ground constraint
518 state.pos[(Surface() + 2) % 3] = std::max(0.0, state.pos[(Surface() + 2) % 3]);
520 state.pos[(Surface() + 2) % 3] = std::min(0.0, state.pos[(Surface() + 2) % 3]);
525 void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
526 type = PLANET_SURFACE;
541 Steering::~Steering() {
544 void Steering::Halt() noexcept {
549 void Steering::GoTo(const glm::dvec3 &t) noexcept {
555 glm::dvec3 Steering::Acceleration(const Situation::State &s) const noexcept {
558 SumForce(acc, s.vel * -max_accel);
561 glm::dvec3 diff = seek_target - s.pos;
562 if (!allzero(diff)) {
563 SumForce(acc, ((normalize(diff) * max_speed) - s.vel) * max_accel);
569 bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
570 if (allzero(in) || anynan(in)) {
573 double cur = allzero(out) ? 0.0 : length(out);
574 double rem = max_accel - cur;
578 double add = length(in);
580 // this method is off if in and out are in different
581 // directions, but gives okayish results
582 out += in * (1.0 / add);