X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fcreature%2Fgoal.cpp;h=41f91f37e0ec63fa0b6f9b0cb432a3de48346cad;hb=cead4f0686af352cdbac1f2c2df9b6a21ad9faec;hp=55fa6c9ea7cc20b53565f96cffc9428b3748bd4a;hpb=c49dd02dfabb123e0c6c4b49f761ce6578dfc464;p=blobs.git diff --git a/src/creature/goal.cpp b/src/creature/goal.cpp index 55fa6c9..41f91f3 100644 --- a/src/creature/goal.cpp +++ b/src/creature/goal.cpp @@ -1,3 +1,4 @@ +#include "AttackGoal.hpp" #include "BlobBackgroundTask.hpp" #include "Goal.hpp" #include "IdleGoal.hpp" @@ -22,6 +23,78 @@ namespace blobs { namespace creature { +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(GetCreature().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 (GetCreature().GetStats().Damage().Critical()) { + // flee + GetCreature().GetSteering().Pass(diff * 5.0); + GetCreature().GetSteering().DontSeparate(); + GetCreature().GetSteering().Haste(1.0); + } else if (glm::length2(diff) > hit_dist * hit_dist) { + // full throttle chase + GetCreature().GetSteering().Pass(target.GetSituation().Position()); + GetCreature().GetSteering().DontSeparate(); + GetCreature().GetSteering().Haste(1.0); + } else { + // attack + GetCreature().GetSteering().Halt(); + GetCreature().GetSteering().DontSeparate(); + GetCreature().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) @@ -260,7 +333,7 @@ std::string summarize(const Composition &comp, const app::Assets &assets) { IngestGoal::IngestGoal(Creature &c, Creature::Stat &stat) : Goal(c) , stat(stat) -, accept() +, accept(Assets().data.resources) , locate_subtask(nullptr) , ingesting(false) , resource(-1) @@ -298,7 +371,7 @@ void IngestGoal::Tick(double dt) { } if (ingesting) { if (OnSuitableTile() && !GetSituation().Moving()) { - GetCreature().Ingest(resource, yield * GetCreature().GetComposition().Compatibility(Assets().data.resources, resource) * dt); + GetCreature().Ingest(resource, yield * GetCreature().GetComposition().Compatibility(resource) * dt); stat.Add(-1.0 * yield * dt); if (stat.Empty()) { SetComplete(); @@ -340,7 +413,7 @@ void IngestGoal::Action() { } bool IngestGoal::OnSuitableTile() { - if (!GetSituation().OnSurface()) { + if (!GetSituation().OnGround()) { return false; } const world::TileType &t = GetSituation().GetTileType(); @@ -358,7 +431,7 @@ bool IngestGoal::OnSuitableTile() { LocateResourceGoal::LocateResourceGoal(Creature &c) : Goal(c) -, accept() +, accept(Assets().data.resources) , found(false) , target_pos(0.0) , searching(false)