double byf69::average_bearing(ObjList nearby) { double sum = 0; for (ObjList::iterator i = nearby.begin(); i != nearby.end(); i++) { sum += i->bearing; } return sum / nearby.size(); }
ObjInfo byf69::find_closest(ObjList nearby) { ObjInfo closest{}; double dist = INFINITY; for (ObjList::iterator i = nearby.begin(); i != nearby.end(); i++) { if (i->distance < dist) { closest = *i; dist = i->distance; } } return closest; }
ObjInfo byf69::find_best_food(ObjList prey) { ObjInfo best_food{}; /* The best eat is the best value energy_gain * eat_success_chance */ double best_energy_gain = 0; for (ObjList::iterator i = prey.begin(); i != prey.end(); i++) { double effective_health = this->health() - ((movement_cost(this->m_chase_speed, i->distance / this->m_chase_speed)) / start_energy); double success_chance = eat_success_chance(effective_health * start_energy, i->health * start_energy); double energy_gain = (i->health * start_energy) * eat_efficiency; double effective_energy_gain = energy_gain * success_chance; if (effective_energy_gain > best_energy_gain) { best_food = *i; } } return best_food; }
void Craig::hunt(void) { const String fav_food = "Algae"; hunt_event = nullptr; if (health() == 0.0) { return; } // we died ObjList prey = perceive(20.0); double best_d = HUGE; for (ObjList::iterator i = prey.begin(); i != prey.end(); ++i) { if ((*i).species == fav_food) { if (best_d > (*i).distance) { set_course((*i).bearing); best_d = (*i).distance; } } } SmartPointer<Craig> self = SmartPointer<Craig>(this); hunt_event = new Event(10.0, [self](void) { self->hunt(); }); if (health() >= 4.0) spawn(); }