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(); }
void amw3647::hunt(void) { const String fav_food = "Algae"; hunt_event = Nil<Event>(); if (health() == 0) { return; } update_pos(); double region = sqrt(10 / density); bound(region, grid_max * 0.01, grid_max * 0.1); ObjList prey = perceive(region); density = (prey.size() + 0.01) / (region * region); double best_d = HUGE; double best_me = HUGE; double best_me_bearing = 0; double old_speed = get_speed(); for (ObjInfo i : prey) { if (i.species == species_name()) { if (best_me > i.distance) { best_me = i.distance; best_me_bearing = i.bearing; } } else if (i.species == fav_food) { if (best_d > i.distance) { set_speed(old_speed * 1.01); set_course(i.bearing); best_d = i.distance; course_changed = 0; } } else if (victory(i)) { if (best_d * 0.1 > i.distance) { switch (encounter_strategy) { case FASTER_GUY_WINS: set_speed(i.their_speed + M_E); break; case SLOWER_GUY_WINS: set_speed(i.their_speed - M_E); break; default: set_speed((i.their_speed + 0.01) * 1.01); break; } set_course(i.bearing); best_d = i.distance; course_changed = 0; } } } if (best_d == HUGE) { if(course_changed == 0) { course_changed = 1; if (best_me == HUGE) { set_course(get_course() + drand48() * 0.25 * M_PI); set_speed((1 - drand48() * 0.05) * old_speed); } else { set_course(best_me_bearing); } } } if (p.distance(Point(0, 0)) > (grid_max / 3)) { if(course_changed == 0) { course_changed = 1; set_course(p.bearing(Point(0, 0))); set_speed(old_speed * 0.90); } } SmartPointer<amw3647> self{this}; hunt_event = new Event(10.0, [self] (void) { self->hunt();}); if (health() >= 3.0) spawn(); }