// stats with effects applied
double Strength() const noexcept;
+ double StrengthFactor() const noexcept;
double Stamina() const noexcept;
+ double StaminaFactor() const noexcept;
double Dexerty() const noexcept;
+ double DexertyFactor() const noexcept;
double Intelligence() const noexcept;
+ double IntelligenceFactor() const noexcept;
double Lifetime() const noexcept;
double Fertility() const noexcept;
double Mutability() const noexcept;
private:
void LocateResource();
void SearchVicinity();
+ void Remember();
+ void RandomWalk();
bool NearTarget() const noexcept;
private:
~Memory();
public:
- void Tick(double dt);
-
+ /// remove all memories
void Erase();
+ /// try to remember where stuff was
+ /// when true, pos contains an approximation of the
+ /// location of the best fitting resource
+ bool RememberLocation(const Composition &, glm::dvec3 &pos) const noexcept;
+
+ void Tick(double dt);
+
private:
/// track time spent on a tile
void TrackStay(const Location &, double t);
return properties.Strength() * ExhaustionFactor() * AgeFactor(0.25);
}
+double Creature::StrengthFactor() const noexcept {
+ return Strength() / (Strength() + 1.0);
+}
+
double Creature::Stamina() const noexcept {
return properties.Stamina() * ExhaustionFactor() * AgeFactor(0.25);
}
+double Creature::StaminaFactor() const noexcept {
+ return Stamina() / (Stamina() + 1.0);
+}
+
double Creature::Dexerty() const noexcept {
return properties.Dexerty() * ExhaustionFactor() * AgeFactor(0.25);
}
+double Creature::DexertyFactor() const noexcept {
+ return Dexerty() / (Dexerty() + 1.0);
+}
+
double Creature::Intelligence() const noexcept {
return properties.Intelligence() * FatigueFactor() * AgeFactor(0.25);
}
+double Creature::IntelligenceFactor() const noexcept {
+ return Intelligence() / (Intelligence() + 1.0);
+}
+
double Creature::Lifetime() const noexcept {
return properties.Lifetime();
}
}
double Creature::PerceptionRange() const noexcept {
- return 3.0 * (Dexerty() / (Dexerty() + 1)) + Size();
+ return 3.0 * DexertyFactor() + Size();
}
double Creature::PerceptionOmniRange() const noexcept {
- return 0.5 * (Dexerty() / (Dexerty() + 1)) + Size();
+ return 0.5 * DexertyFactor() + Size();
}
double Creature::PerceptionField() const noexcept {
// this is the cosine of half the angle, so 1.0 is none, -1.0 is perfect
- return 0.8 - (Dexerty() / (Dexerty() + 1));
+ return 0.8 - DexertyFactor();
}
bool Creature::PerceptionTest(const glm::dvec3 &p) const noexcept {
known_types.clear();
}
+bool Memory::RememberLocation(const Composition &accept, glm::dvec3 &pos) const noexcept {
+ double best_rating = -1.0;
+ for (const auto &k : known_types) {
+ const world::TileType &t = c.GetSimulation().TileTypes()[k.first];
+ auto entry = t.FindBestResource(accept);
+ if (entry != t.resources.end()) {
+ double rating = entry->ubiquity / std::max(0.125, 0.25 * glm::length2(c.GetSituation().Position() - k.second.first_loc.position));
+ if (rating > best_rating) {
+ best_rating = rating;
+ pos = k.second.first_loc.position;
+ }
+ rating = entry->ubiquity / std::max(0.125, 0.25 * glm::length2(c.GetSituation().Position() - k.second.last_loc.position));
+ if (rating > best_rating) {
+ best_rating = rating;
+ pos = k.second.last_loc.position;
+ }
+ }
+ }
+ if (best_rating > 0.0) {
+ glm::dvec3 error(
+ c.GetSimulation().Assets().random.SNorm(),
+ c.GetSimulation().Assets().random.SNorm(),
+ c.GetSimulation().Assets().random.SNorm());
+ pos += error * (2.0 * (1.0 - c.IntelligenceFactor()));
+ pos = glm::normalize(pos) * c.GetSituation().GetPlanet().Radius();
+ return true;
+ } else {
+ return false;
+ }
+}
+
void Memory::Tick(double dt) {
Situation &s = c.GetSituation();
if (s.OnSurface()) {
TrackStay({ &s.GetPlanet(), s.Position() }, dt);
}
+ // TODO: forget
}
void Memory::TrackStay(const Location &l, double t) {
} else {
// go find somewhere else
SearchVicinity();
+ if (!found) {
+ Remember();
+ if (!found) {
+ RandomWalk();
+ }
+ }
}
} else {
// well, what now?
// TODO: subtract minimum yield
rating[y + search_radius][x + search_radius] = yield->ubiquity * accept.Get(yield->resource);
// penalize distance
- double dist = std::max(0.125, 0.25 * glm::length(tpos - pos));
+ double dist = std::max(0.125, 0.25 * glm::length2(tpos - pos));
rating[y + search_radius][x + search_radius] /= dist;
}
}
searching = false;
target_pos = glm::normalize(pos + (double(best_pos.x) * step_x) + (double(best_pos.y) * step_y)) * planet.Radius();
GetSteering().GoTo(target_pos);
- } else if (!searching) {
- found = false;
- searching = true;
- target_pos = GetSituation().Position();
- target_pos += Random().SNorm() * step_x;
- target_pos += Random().SNorm() * step_y;
- // bias towards current heading
- target_pos += GetSituation().Heading() * 1.5;
- target_pos = glm::normalize(target_pos) * planet.Radius();
+ }
+}
+
+void LocateResourceGoal::Remember() {
+ glm::dvec3 pos(0.0);
+ if (GetCreature().GetMemory().RememberLocation(accept, pos)) {
+ found = true;
+ searching = false;
+ target_pos = pos;
GetSteering().GoTo(target_pos);
}
}
+void LocateResourceGoal::RandomWalk() {
+ if (searching) {
+ return;
+ }
+
+ const world::Planet &planet = GetSituation().GetPlanet();
+ const glm::dvec3 &pos = GetSituation().Position();
+ const glm::dvec3 normal(planet.NormalAt(pos));
+ const glm::dvec3 step_x(glm::normalize(glm::cross(normal, glm::dvec3(normal.z, normal.x, normal.y))));
+ const glm::dvec3 step_y(glm::normalize(glm::cross(step_x, normal)));
+
+ found = false;
+ searching = true;
+ target_pos = GetSituation().Position();
+ target_pos += Random().SNorm() * step_x;
+ target_pos += Random().SNorm() * step_y;
+ // bias towards current heading
+ target_pos += GetSituation().Heading() * 1.5;
+ target_pos = glm::normalize(target_pos) * planet.Radius();
+ GetSteering().GoTo(target_pos);
+}
+
bool LocateResourceGoal::NearTarget() const noexcept {
const Situation &s = GetSituation();
return s.OnSurface() && glm::length2(s.Position() - target_pos) < 0.5;