Skip to content

Commit 186f3a6

Browse files
DRY ANIMAL_RADIUS code
1 parent f8eff3c commit 186f3a6

4 files changed

Lines changed: 30 additions & 45 deletions

File tree

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "nodeObjs/noFlag.h"
3333
#include "nodeObjs/noShip.h"
3434
#include "nodeObjs/noTree.h"
35+
#include "figures/nofHunter.h"
3536
#include "gameData/BuildingConsts.h"
3637
#include "gameData/BuildingProperties.h"
3738
#include "gameData/GameConsts.h"
@@ -1965,24 +1966,9 @@ bool AIPlayerJH::HuntablesinRange(const MapPoint pt, unsigned min)
19651966
// check first if no other hunter(or hunter buildingsite) is nearby
19661967
if(aii.isBuildingNearby(BuildingType::Hunter, pt, 14))
19671968
return false;
1968-
const unsigned maxrange = 25;
19691969

1970-
const auto pointToAnimal = [this](const MapPoint pt, unsigned) -> const noAnimal* {
1971-
for(const noBase& fig : gwb.GetFigures(pt))
1972-
{
1973-
if(fig.GetType() == NodalObjectType::Animal)
1974-
return static_cast<const noAnimal*>(&fig);
1975-
}
1976-
return nullptr;
1977-
};
1978-
1979-
const auto canAnimalBeHunted = [this, pt, maxrange](const noAnimal* const animal) {
1980-
return animal && animal->CanHunted()
1981-
&& gwb.FindHumanPath(pt, animal->GetPos(), maxrange);
1982-
};
1983-
1984-
const auto available_animals =
1985-
gwb.GetPointsInRadius(pt, ANIMAL_RADIUS, pointToAnimal, canAnimalBeHunted, true);
1970+
const auto available_animals = nofHunter::GetAnimalsInRange(gwb, pt, ANIMAL_RADIUS, 25,
1971+
[](const noAnimal* a) { return a->CanHunted(); });
19861972

19871973
return available_animals.size() >= min;
19881974
}

libs/s25main/figures/nofHunter.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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())

libs/s25main/figures/nofHunter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include "nofBuildingWorker.h"
88
#include "gameTypes/Direction.h"
9+
#include <vector>
910

11+
class GameWorldBase;
1012
class noAnimal;
1113
class SerializedGameData;
1214
class nobUsual;
@@ -64,6 +66,11 @@ class nofHunter : public nofBuildingWorker
6466

6567
void TryStartHunting();
6668

69+
/// Find animals in a radius around a position that satisfy a predicate and are reachable
70+
static std::vector<noAnimal*> GetAnimalsInRange(const GameWorldBase& world, const MapPoint pos,
71+
unsigned radius, unsigned maxDistance,
72+
bool (*isValidAnimal)(const noAnimal*));
73+
6774
/// das Tier ist nicht mehr verfügbar (von selbst gestorben o.Ä.)
6875
void AnimalLost();
6976
/// wird aufgerufen, wenn die Arbeit abgebrochen wird (von nofBuildingWorker aufgerufen)

libs/s25main/figures/nofSkinner.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "random/Random.h"
1717
#include "world/GameWorld.h"
1818
#include "nodeObjs/noAnimal.h"
19+
#include "figures/nofHunter.h"
1920
#include "gameData/GameConsts.h"
2021
#include "gameData/JobConsts.h"
2122

@@ -188,23 +189,9 @@ void nofSkinner::TryStartSkinning()
188189
HandleStateWaiting1();
189190
else
190191
{
191-
const auto pointToAnimal = [world = this->world](const MapPoint pt, unsigned) -> noAnimal* {
192-
for(auto& figure : world->GetFigures(pt))
193-
{
194-
if(figure.GetType() != NodalObjectType::Animal)
195-
continue;
196-
return checkedCast<noAnimal*>(&figure);
197-
}
198-
return nullptr;
199-
};
200-
201-
const auto canAnimalBeSkinned = [pos = this->pos](const noAnimal* const animal) {
202-
return animal && animal->CanBeSkinned()
203-
&& (pos == animal->GetPos() || world->FindHumanPath(pos, animal->GetPos(), MAX_SKINNING_DISTANCE));
204-
};
205-
206192
const auto available_animals =
207-
world->GetPointsInRadius(pos, ANIMAL_RADIUS, pointToAnimal, canAnimalBeSkinned, true);
193+
nofHunter::GetAnimalsInRange(*world, pos, ANIMAL_RADIUS, MAX_SKINNING_DISTANCE,
194+
[](const noAnimal* a) { return a->CanBeSkinned(); });
208195

209196
if(!available_animals.empty())
210197
{

0 commit comments

Comments
 (0)