Skip to content

Commit f8eff3c

Browse files
Hunter: Use GetPointsInRadius() instead of square
1 parent f299328 commit f8eff3c

4 files changed

Lines changed: 38 additions & 75 deletions

File tree

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,51 +1965,26 @@ bool AIPlayerJH::HuntablesinRange(const MapPoint pt, unsigned min)
19651965
// check first if no other hunter(or hunter buildingsite) is nearby
19661966
if(aii.isBuildingNearby(BuildingType::Hunter, pt, 14))
19671967
return false;
1968-
unsigned maxrange = 25;
1969-
unsigned short fx, fy, lx, ly;
1970-
const unsigned short SQUARE_SIZE = 19;
1971-
unsigned huntablecount = 0;
1972-
if(pt.x > SQUARE_SIZE)
1973-
fx = pt.x - SQUARE_SIZE;
1974-
else
1975-
fx = 0;
1976-
if(pt.y > SQUARE_SIZE)
1977-
fy = pt.y - SQUARE_SIZE;
1978-
else
1979-
fy = 0;
1980-
if(pt.x + SQUARE_SIZE < gwb.GetWidth())
1981-
lx = pt.x + SQUARE_SIZE;
1982-
else
1983-
lx = gwb.GetWidth() - 1;
1984-
if(pt.y + SQUARE_SIZE < gwb.GetHeight())
1985-
ly = pt.y + SQUARE_SIZE;
1986-
else
1987-
ly = gwb.GetHeight() - 1;
1988-
// Durchgehen und nach Tieren suchen
1989-
for(MapPoint p2(0, fy); p2.y <= ly; ++p2.y)
1990-
{
1991-
for(p2.x = fx; p2.x <= lx; ++p2.x)
1968+
const unsigned maxrange = 25;
1969+
1970+
const auto pointToAnimal = [this](const MapPoint pt, unsigned) -> const noAnimal* {
1971+
for(const noBase& fig : gwb.GetFigures(pt))
19921972
{
1993-
// Search for animals
1994-
for(const noBase& fig : gwb.GetFigures(p2))
1995-
{
1996-
if(fig.GetType() == NodalObjectType::Animal)
1997-
{
1998-
// Ist das Tier überhaupt zum Jagen geeignet?
1999-
if(!static_cast<const noAnimal&>(fig).CanHunted())
2000-
continue;
2001-
// Und komme ich hin?
2002-
if(gwb.FindHumanPath(pt, static_cast<const noAnimal&>(fig).GetPos(), maxrange))
2003-
// Dann nehmen wir es
2004-
{
2005-
if(++huntablecount >= min)
2006-
return true;
2007-
}
2008-
}
2009-
}
1973+
if(fig.GetType() == NodalObjectType::Animal)
1974+
return static_cast<const noAnimal*>(&fig);
20101975
}
2011-
}
2012-
return false;
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);
1986+
1987+
return available_animals.size() >= min;
20131988
}
20141989

20151990
void AIPlayerJH::InitStoreAndMilitarylists()

libs/s25main/figures/nofHunter.cpp

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -131,39 +131,24 @@ void nofHunter::HandleDerivedEvent(unsigned /*id*/)
131131

132132
void nofHunter::TryStartHunting()
133133
{
134-
// Find animals in a square around building (actually should be circle, but animals are moving anyway)
135-
const int SQUARE_SIZE = 19;
136-
137-
// Liste mit den gefundenen Tieren
138-
std::vector<noAnimal*> available_animals;
139-
140-
// Durchgehen und nach Tieren suchen
141-
Position curPos;
142-
for(curPos.y = pos.y - SQUARE_SIZE; curPos.y <= pos.y + SQUARE_SIZE; ++curPos.y)
143-
{
144-
for(curPos.x = pos.x - SQUARE_SIZE; curPos.x <= pos.x + SQUARE_SIZE; ++curPos.x)
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))
145137
{
146-
MapPoint curMapPos = world->MakeMapPoint(curPos);
147-
148-
// nach Tieren suchen
149-
for(auto& figure : world->GetFigures(curMapPos))
150-
{
151-
if(figure.GetType() != NodalObjectType::Animal)
152-
continue;
153-
// Ist das Tier überhaupt zum Jagen geeignet?
154-
auto& animal = static_cast<noAnimal&>(figure);
155-
if(!animal.CanHunted())
156-
continue;
157-
158-
// Und komme ich hin?
159-
if(pos == animal.GetPos() || world->FindHumanPath(pos, animal.GetPos(), MAX_HUNTING_DISTANCE))
160-
{
161-
// Dann nehmen wir es
162-
available_animals.push_back(&animal);
163-
}
164-
}
138+
if(figure.GetType() != NodalObjectType::Animal)
139+
continue;
140+
return static_cast<noAnimal*>(&figure);
165141
}
166-
}
142+
return nullptr;
143+
};
144+
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));
148+
};
149+
150+
const auto available_animals =
151+
world->GetPointsInRadius(pos, ANIMAL_RADIUS, pointToAnimal, canAnimalBeHunted, true);
167152

168153
// Gibt es überhaupt ein Tier, das ich jagen kann?
169154
if(!available_animals.empty())

libs/s25main/figures/nofSkinner.cpp

Lines changed: 1 addition & 1 deletion
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 "gameData/GameConsts.h"
1920
#include "gameData/JobConsts.h"
2021

2122
using namespace leatheraddon;
@@ -187,7 +188,6 @@ void nofSkinner::TryStartSkinning()
187188
HandleStateWaiting1();
188189
else
189190
{
190-
const int ANIMAL_RADIUS = 19;
191191
const auto pointToAnimal = [world = this->world](const MapPoint pt, unsigned) -> noAnimal* {
192192
for(auto& figure : world->GetFigures(pt))
193193
{

libs/s25main/gameData/GameConsts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ constexpr auto gfs_to_duration(const unsigned gfs)
4141
/// Reichweite der Bergarbeiter
4242
constexpr unsigned MINER_RADIUS = 2;
4343

44+
/// Suchradius für Tiere (Jäger, Gerber, KI)
45+
constexpr unsigned ANIMAL_RADIUS = 19;
46+
4447
/// Konstante für die Pfadrichtung bei einer Schiffsverbindung
4548
constexpr unsigned char SHIP_DIR = 100;
4649
constexpr unsigned char INVALID_DIR = 0xFF;

0 commit comments

Comments
 (0)