@@ -129,26 +129,31 @@ void nofHunter::HandleDerivedEvent(unsigned /*id*/)
129129 }
130130}
131131
132- void nofHunter::TryStartHunting ()
132+ std::vector<noAnimal*> nofHunter::GetAnimalsInRange (const GameWorldBase& world, const MapPoint pos,
133+ unsigned radius, unsigned maxDistance,
134+ bool (*isValidAnimal)(const noAnimal*))
133135{
134- // Find animals in a circle around building
135- const auto pointToAnimal = [world = this ->world ](const MapPoint pt, unsigned ) -> noAnimal* {
136- for (auto & figure : world->GetFigures (pt))
136+ const auto pointToAnimal = [&world](const MapPoint pt, unsigned ) -> noAnimal* {
137+ for (auto & figure : world.GetFigures (pt))
137138 {
138- if (figure.GetType () != NodalObjectType::Animal)
139- continue ;
140- return static_cast <noAnimal*>(&figure);
139+ if (figure.GetType () == NodalObjectType::Animal)
140+ return static_cast <noAnimal*>(&figure);
141141 }
142142 return nullptr ;
143143 };
144144
145- const auto canAnimalBeHunted = [pos = this -> pos ](const noAnimal* const animal) {
146- return animal && animal-> CanHunted ( )
147- && (pos == animal->GetPos () || world-> FindHumanPath (pos, animal->GetPos (), MAX_HUNTING_DISTANCE ));
145+ const auto canAnimalBeUsed = [pos, maxDistance, &world, isValidAnimal ](const noAnimal* const animal) {
146+ return animal && isValidAnimal (animal )
147+ && (pos == animal->GetPos () || world. FindHumanPath (pos, animal->GetPos (), maxDistance ));
148148 };
149149
150- const auto available_animals =
151- world->GetPointsInRadius (pos, ANIMAL_RADIUS , pointToAnimal, canAnimalBeHunted, true );
150+ return world.GetPointsInRadius (pos, radius, pointToAnimal, canAnimalBeUsed, true );
151+ }
152+
153+ void nofHunter::TryStartHunting ()
154+ {
155+ const auto available_animals = GetAnimalsInRange (*world, pos, ANIMAL_RADIUS , MAX_HUNTING_DISTANCE ,
156+ [](const noAnimal* a) { return a->CanHunted (); });
152157
153158 // Gibt es überhaupt ein Tier, das ich jagen kann?
154159 if (!available_animals.empty ())
0 commit comments