+AttackGoal::AttackGoal(Creature &self, Creature &target)
+: Goal(self)
+, target(target)
+, damage_target(0.25)
+, damage_dealt(0.0)
+, cooldown(0.0) {
+}
+
+AttackGoal::~AttackGoal() {
+}
+
+std::string AttackGoal::Describe() const {
+ return "attack " + target.Name();
+}
+
+void AttackGoal::Tick(double dt) {
+ cooldown -= dt;
+}
+
+void AttackGoal::Action() {
+ if (target.Dead() || !GetCreature().PerceptionTest(target.GetSituation().Position())) {
+ SetComplete();
+ return;
+ }
+ const glm::dvec3 diff(GetSituation().Position() - target.GetSituation().Position());
+ const double hit_range = GetCreature().Size() * 0.5 * GetCreature().DexertyFactor();
+ const double hit_dist = hit_range + (0.5 * GetCreature().Size()) + 0.5 * (target.Size());
+ if (GetStats().Damage().Critical()) {
+ // flee
+ GetSteering().Pass(diff * 5.0);
+ GetSteering().DontSeparate();
+ GetSteering().Haste(1.0);
+ } else if (glm::length2(diff) > hit_dist * hit_dist) {
+ // full throttle chase
+ GetSteering().Pass(target.GetSituation().Position());
+ GetSteering().DontSeparate();
+ GetSteering().Haste(1.0);
+ } else {
+ // attack
+ GetSteering().Halt();
+ GetSteering().DontSeparate();
+ GetSteering().Haste(1.0);
+ if (cooldown <= 0.0) {
+ constexpr double impulse = 0.05;
+ const double force = GetCreature().Strength();
+ const double damage =
+ force * impulse
+ * (GetCreature().GetComposition().TotalDensity() / target.GetComposition().TotalDensity())
+ * (GetCreature().Mass() / target.Mass())
+ / target.Mass();
+ GetCreature().DoWork(force * impulse * glm::length(diff));
+ target.Hurt(damage);
+ target.GetSituation().Accelerate(glm::normalize(diff) * force * -impulse);
+ damage_dealt += damage;
+ if (damage_dealt >= damage_target || target.Dead()) {
+ SetComplete();
+ if (target.Dead()) {
+ GetCreature().GetSimulation().Log() << GetCreature().Name()
+ << " killed " << target.Name() << std::endl;
+ }
+ }
+ cooldown = 1.0 + (4.0 * (1.0 - GetCreature().DexertyFactor()));
+ }
+ }
+}
+
+void AttackGoal::OnBackground() {
+ // abort if something more important comes up
+ SetComplete();
+}
+
+
+BlobBackgroundTask::BlobBackgroundTask(Creature &c)
+: Goal(c)
+, breathing(false)
+, drink_subtask(nullptr)
+, eat_subtask(nullptr) {
+}
+
+BlobBackgroundTask::~BlobBackgroundTask() {
+}
+
+std::string BlobBackgroundTask::Describe() const {
+ return "being a blob";
+}
+
+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 = GetStats().Breath().gain * -(1.0 + GetCreature().ExhaustionFactor());
+ GetStats().Breath().Add(amount * dt);
+ // maintain ~1% gas composition
+ double gas_amount = GetCreature().GetComposition().Get(gas);
+ if (gas_amount < GetCreature().GetComposition().TotalMass() * 0.01) {
+ double add = std::min(GetCreature().GetComposition().TotalMass() * 0.025 - gas_amount, -amount * dt);
+ GetCreature().Ingest(gas, add);
+ }
+ if (GetStats().Breath().Empty()) {
+ breathing = false;
+ }
+ }
+}
+
+void BlobBackgroundTask::Action() {
+ CheckStats();
+ CheckSplit();
+ CheckMutate();
+}
+
+void BlobBackgroundTask::CheckStats() {
+ Creature::Stats &stats = GetStats();
+
+ if (!breathing && stats.Breath().Bad()) {
+ breathing = true;
+ }
+
+ if (!drink_subtask && stats.Thirst().Bad()) {
+ drink_subtask = new IngestGoal(GetCreature(), stats.Thirst());
+ for (const auto &cmp : GetCreature().GetComposition()) {
+ if (Assets().data.resources[cmp.resource].state == world::Resource::LIQUID) {
+ double value = cmp.value / GetCreature().GetComposition().TotalMass();
+ drink_subtask->Accept(cmp.resource, value);
+ for (const auto &compat : Assets().data.resources[cmp.resource].compatibility) {
+ if (Assets().data.resources[compat.first].state == world::Resource::LIQUID) {
+ drink_subtask->Accept(compat.first, value * compat.second);
+ }
+ }
+ }
+ }
+ drink_subtask->WhenComplete([&](Goal &) { drink_subtask = nullptr; });
+ GetCreature().AddGoal(std::unique_ptr<Goal>(drink_subtask));
+ }
+
+ if (!eat_subtask && stats.Hunger().Bad()) {
+ eat_subtask = new IngestGoal(GetCreature(), stats.Hunger());
+ for (const auto &cmp : GetCreature().GetComposition()) {
+ if (Assets().data.resources[cmp.resource].state == world::Resource::SOLID) {
+ double value = cmp.value / GetCreature().GetComposition().TotalMass();
+ eat_subtask->Accept(cmp.resource, value);
+ for (const auto &compat : Assets().data.resources[cmp.resource].compatibility) {
+ if (Assets().data.resources[compat.first].state == world::Resource::SOLID) {
+ eat_subtask->Accept(compat.first, value * compat.second);
+ }
+ }
+ }
+ }
+ eat_subtask->WhenComplete([&](Goal &) { eat_subtask = nullptr; });
+ GetCreature().AddGoal(std::unique_ptr<Goal>(eat_subtask));
+ }
+}
+
+void BlobBackgroundTask::CheckSplit() {
+ if (GetCreature().Mass() > GetCreature().OffspringMass() * 2.0
+ && GetCreature().OffspringChance() > Random().UNorm()) {
+ GetCreature().GetSimulation().Log() << GetCreature().Name() << " split" << std::endl;
+ Split(GetCreature());
+ return;
+ }
+}
+
+void BlobBackgroundTask::CheckMutate() {
+ // check for random property mutation
+ if (GetCreature().MutateChance() > Random().UNorm()) {
+ double amount = 1.0 + (Random().SNorm() * 0.05);
+ math::Distribution &d = GetCreature().GetGenome().properties.props[Random().UInt(9)];
+ if (Random().UNorm() < 0.5) {
+ d.Mean(d.Mean() * amount);
+ } else {
+ d.StandardDeviation(d.StandardDeviation() * amount);
+ }
+ }
+}
+
+