#include "Creature.hpp"
#include "Situation.hpp"
+#include "Steering.hpp"
+#include "Goal.hpp"
#include "InhaleNeed.hpp"
#include "IngestNeed.hpp"
#include "Need.hpp"
#include "../world/Simulation.hpp"
#include "../world/TileType.hpp"
+#include <algorithm>
#include <glm/gtx/transform.hpp>
#include <iostream>
: name()
, health(1.0)
, needs()
+, goals()
, situation()
+, steering()
+, vel(0.0)
, vao() {
}
health = std::max(0.0, health - dt);
}
+void Creature::AddGoal(std::unique_ptr<Goal> &&g) {
+ g->Enable(*this);
+ goals.emplace_back(std::move(g));
+}
+
+namespace {
+
+bool GoalCompare(const std::unique_ptr<Goal> &a, const std::unique_ptr<Goal> &b) {
+ return b->Urgency() < a->Urgency();
+}
+
+}
+
void Creature::Tick(double dt) {
- // update needs
+ // TODO: better integration method
+ glm::dvec3 acc(steering.Acceleration(*this));
+ situation.Move(vel * dt);
+ vel += acc * dt;
+
for (auto &need : needs) {
need->Tick(dt);
}
+ for (auto &goal : goals) {
+ goal->Tick(dt);
+ }
// do background stuff
for (auto &need : needs) {
need->ApplyEffect(*this, dt);
}
+ if (goals.empty()) {
+ return;
+ }
+ // if active goal can be interrupted, check priorities
+ if (goals.size() > 1 && goals[0]->Interruptible()) {
+ std::sort(goals.begin(), goals.end(), GoalCompare);
+ }
+ goals[0]->Action(*this);
+ for (auto goal = goals.begin(); goal != goals.end();) {
+ if ((*goal)->Complete()) {
+ goals.erase(goal);
+ } else {
+ ++goal;
+ }
+ }
}
glm::dmat4 Creature::LocalTransform() noexcept {
return planet->GetSimulation().TileTypes()[GetTile().type];
}
+void Situation::Move(const glm::dvec3 &dp) noexcept {
+ position += dp;
+ if (OnSurface()) {
+ // enforce ground constraint
+ position.z = std::max(0.0, position.z);
+ }
+}
+
void Situation::SetPlanetSurface(world::Planet &p, int srf, const glm::dvec3 &pos) noexcept {
type = PLANET_SURFACE;
planet = &p;
position = pos;
}
+
+Steering::Steering()
+: seek_target(0.0)
+, max_accel(1.0)
+, max_speed(1.0)
+, halting(false)
+, seeking(false) {
+}
+
+Steering::~Steering() {
+}
+
+void Steering::Halt() noexcept {
+ halting = true;
+ seeking = false;
+}
+
+void Steering::GoTo(const glm::dvec3 &t) noexcept {
+ seek_target = t;
+ halting = false;
+ seeking = true;
+}
+
+glm::dvec3 Steering::Acceleration(Creature &c) const noexcept {
+ glm::dvec3 acc(0.0);
+ if (halting) {
+ SumForce(acc, c.Velocity() * -max_accel);
+ }
+ if (seeking) {
+ glm::dvec3 diff = seek_target - c.GetSituation().Position();
+ if (!allzero(diff)) {
+ SumForce(acc, ((normalize(diff) * max_speed) - c.Velocity()) * max_accel);
+ }
+ }
+ return acc;
+}
+
+bool Steering::SumForce(glm::dvec3 &out, const glm::dvec3 &in) const noexcept {
+ if (allzero(in) || anynan(in)) {
+ return false;
+ }
+ double cur = allzero(out) ? 0.0 : length(out);
+ double rem = max_accel - cur;
+ if (rem < 0.0) {
+ return true;
+ }
+ double add = length(in);
+ if (add > rem) {
+ // this method is off if in and out are in different
+ // directions, but gives okayish results
+ out += in * (1.0 / add);
+ return true;
+ } else {
+ out += in;
+ return false;
+ }
+}
+
}
}