2 #include "IdleGoal.hpp"
3 #include "LocateResourceGoal.hpp"
5 #include "Creature.hpp"
6 #include "../app/Assets.hpp"
7 #include "../world/Planet.hpp"
8 #include "../world/Resource.hpp"
9 #include "../world/Simulation.hpp"
10 #include "../world/TileType.hpp"
13 #include <glm/gtx/io.hpp>
19 Goal::Goal(Creature &c)
27 Goal::~Goal() noexcept {
30 Situation &Goal::GetSituation() noexcept {
31 return c.GetSituation();
34 const Situation &Goal::GetSituation() const noexcept {
35 return c.GetSituation();
38 Steering &Goal::GetSteering() noexcept {
39 return c.GetSteering();
42 const Steering &Goal::GetSteering() const noexcept {
43 return c.GetSteering();
46 app::Assets &Goal::Assets() noexcept {
47 return c.GetSimulation().Assets();
50 const app::Assets &Goal::Assets() const noexcept {
51 return c.GetSimulation().Assets();
54 void Goal::SetComplete() noexcept {
63 void Goal::OnComplete(std::function<void(Goal &)> cb) noexcept {
71 IdleGoal::IdleGoal(Creature &c)
77 IdleGoal::~IdleGoal() {
80 std::string IdleGoal::Describe() const {
84 void IdleGoal::Enable() {
87 void IdleGoal::Tick(double dt) {
90 void IdleGoal::Action() {
91 double fert = GetCreature().Fertility();
92 double rand = Assets().random.UNorm();
94 std::cout << "[" << int(GetCreature().GetSimulation().Time())
95 << "s] splitting " << GetCreature().Name()
96 << " because " << fert << " > " << rand << std::endl;
102 LocateResourceGoal::LocateResourceGoal(Creature &c, int res)
112 LocateResourceGoal::~LocateResourceGoal() noexcept {
115 std::string LocateResourceGoal::Describe() const {
116 return "locate " + GetCreature().GetSimulation().Resources()[res].name;
119 void LocateResourceGoal::Enable() {
123 void LocateResourceGoal::Tick(double dt) {
126 void LocateResourceGoal::Action() {
131 double dist = glm::length2(GetSituation().Position() - target_pos);
133 GetSteering().Halt();
136 GetSteering().GoTo(target_pos);
139 } else if (OnTargetTile()) {
140 GetSteering().Halt();
141 if (!GetSituation().Moving()) {
145 GetSteering().GoTo(target_pos);
149 void LocateResourceGoal::LocateResource() {
150 if (GetSituation().OnSurface()) {
151 const world::TileType &t = GetSituation().GetTileType();
152 auto yield = t.FindResource(res);
153 if (yield != t.resources.cend()) {
155 GetSteering().Halt();
158 target_pos = GetSituation().Position();
159 target_srf = GetSituation().Surface();
160 target_tile = GetSituation().GetPlanet().SurfacePosition(target_srf, target_pos);
162 // go find somewhere else
170 void LocateResourceGoal::SearchVicinity() {
171 const world::Planet &planet = GetSituation().GetPlanet();
172 int srf = GetSituation().Surface();
173 const glm::dvec3 &pos = GetSituation().Position();
175 glm::ivec2 loc = planet.SurfacePosition(srf, pos);
176 glm::ivec2 seek_radius(2);
177 glm::ivec2 begin(glm::max(glm::ivec2(0), loc - seek_radius));
178 glm::ivec2 end(glm::min(glm::ivec2(planet.SideLength() - 1), loc + seek_radius));
180 const world::TileType::Yield *best = nullptr;
182 double best_distance;
184 for (int y = begin.y; y <= end.y; ++y) {
185 for (int x = begin.x; x <= end.x; ++x) {
186 const world::TileType &type = planet.TypeAt(srf, x, y);
187 auto yield = type.FindResource(res);
188 if (yield != type.resources.cend()) {
189 double dist = glm::length2(planet.TileCenter(srf, x, y) - pos);
192 best_pos = glm::ivec2(x, y);
193 best_distance = dist;
194 } else if (yield->ubiquity - (dist * 0.125) > best->ubiquity - (best_distance * 0.125)) {
196 best_pos = glm::ivec2(x, y);
197 best_distance = dist;
205 target_pos = planet.TileCenter(srf, best_pos.x, best_pos.y);
207 target_tile = best_pos;
208 GetSteering().GoTo(target_pos);
209 } else if (!searching) {
212 target_pos = GetSituation().Position();
213 target_pos[(srf + 0) % 3] += Assets().random.SNorm();
214 target_pos[(srf + 1) % 3] += Assets().random.SNorm();
215 GetSteering().GoTo(target_pos);
219 bool LocateResourceGoal::OnTargetTile() const noexcept {
220 const Situation &s = GetSituation();
222 && s.Surface() == target_srf
224 && s.SurfacePosition() == target_tile;