Skip to content

Commit 088e669

Browse files
committed
Refactor farm field placement checks
1 parent e11591d commit 088e669

5 files changed

Lines changed: 57 additions & 65 deletions

File tree

libs/s25main/figures/nofFarmer.cpp

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ unsigned short nofFarmer::GetCarryID() const
5757
return 71;
5858
}
5959

60+
nofFarmhand::PointQuality nofFarmer::GetNewFieldPointQuality(const GameWorld& world, const MapPoint pt)
61+
{
62+
// Nicht auf Straßen bauen!
63+
for(const auto dir : helpers::EnumRange<Direction>{})
64+
{
65+
if(world.GetPointRoad(pt, dir) != PointRoad::None)
66+
return PointQuality::NotPossible;
67+
}
68+
69+
// Terrain untersuchen
70+
if(!world.IsOfTerrain(pt, [](const auto& desc) { return desc.IsVital(); }))
71+
return PointQuality::NotPossible;
72+
73+
// Ist Platz frei?
74+
NodalObjectType noType = world.GetNO(pt)->GetType();
75+
if(noType != NodalObjectType::Environment && noType != NodalObjectType::Nothing)
76+
return PointQuality::NotPossible;
77+
78+
for(const MapPoint nb : world.GetNeighbours(pt))
79+
{
80+
// Nicht direkt neben andere Getreidefelder und Gebäude setzen!
81+
noType = world.GetNO(nb)->GetType();
82+
if(noType == NodalObjectType::Grainfield || noType == NodalObjectType::Grapefield
83+
|| noType == NodalObjectType::Building || noType == NodalObjectType::Buildingsite)
84+
return PointQuality::NotPossible;
85+
}
86+
87+
return PointQuality::Class2;
88+
}
89+
6090
/// Abgeleitete Klasse informieren, wenn sie anfängt zu arbeiten (Vorbereitungen)
6191
void nofFarmer::WorkStarted()
6292
{
@@ -123,34 +153,7 @@ nofFarmhand::PointQuality nofFarmer::GetPointQuality(const MapPoint pt, bool /*
123153
}
124154
// oder einen freien Platz, wo wir ein neues sähen können
125155
else
126-
{
127-
// Nicht auf Straßen bauen!
128-
for(const auto dir : helpers::EnumRange<Direction>{})
129-
{
130-
if(world->GetPointRoad(pt, dir) != PointRoad::None)
131-
return PointQuality::NotPossible;
132-
}
133-
134-
// Terrain untersuchen
135-
if(!world->IsOfTerrain(pt, [](const auto& desc) { return desc.IsVital(); }))
136-
return PointQuality::NotPossible;
137-
138-
// Ist Platz frei?
139-
NodalObjectType noType = world->GetNO(pt)->GetType();
140-
if(noType != NodalObjectType::Environment && noType != NodalObjectType::Nothing)
141-
return PointQuality::NotPossible;
142-
143-
for(const MapPoint nb : world->GetNeighbours(pt))
144-
{
145-
// Nicht direkt neben andere Getreidefelder und Gebäude setzen!
146-
noType = world->GetNO(nb)->GetType();
147-
if(noType == NodalObjectType::Grainfield || noType == NodalObjectType::Grapefield
148-
|| noType == NodalObjectType::Building || noType == NodalObjectType::Buildingsite)
149-
return PointQuality::NotPossible;
150-
}
151-
152-
return PointQuality::Class2;
153-
}
156+
return GetNewFieldPointQuality(*world, pt);
154157
}
155158

156159
void nofFarmer::WorkAborted()

libs/s25main/figures/nofFarmer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma once
66

77
#include "nofFarmhand.h"
8+
class GameWorld;
89
class SerializedGameData;
910
class nobUsual;
1011

@@ -34,6 +35,8 @@ class nofFarmer : public nofFarmhand
3435
nofFarmer(MapPoint pos, unsigned char player, nobUsual* workplace);
3536
nofFarmer(SerializedGameData& sgd, unsigned obj_id);
3637

38+
static nofFarmhand::PointQuality GetNewFieldPointQuality(const GameWorld& world, MapPoint pt);
39+
3740
void Serialize(SerializedGameData& sgd) const override;
3841

3942
GO_Type GetGOT() const final { return GO_Type::NofFarmer; }

libs/s25main/figures/nofFarmhand.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ nofFarmhand::nofFarmhand(SerializedGameData& sgd, const unsigned obj_id)
2727
: nofBuildingWorker(sgd, obj_id), dest(sgd.PopMapPoint())
2828
{}
2929

30+
unsigned nofFarmhand::GetWorkRadius(const Job job)
31+
{
32+
switch(job)
33+
{
34+
case Job::Carpenter: return 0;
35+
case Job::Hunter:
36+
case Job::Farmer:
37+
case Job::Winegrower: return 2;
38+
case Job::CharBurner: return 3;
39+
case Job::Woodcutter:
40+
case Job::Forester: return 6;
41+
case Job::Fisher: return 7;
42+
case Job::Stonemason: return 8;
43+
default: throw std::logic_error("Invalid job");
44+
}
45+
}
46+
3047
void nofFarmhand::WalkedDerived()
3148
{
3249
switch(state)
@@ -59,21 +76,7 @@ void nofFarmhand::HandleDerivedEvent(const unsigned /*id*/)
5976
{
6077
// Start working after the initial wait period
6178
// Work radius
62-
const unsigned max_radius = [](Job job) {
63-
switch(job)
64-
{
65-
case Job::Carpenter: return 0;
66-
case Job::Hunter:
67-
case Job::Farmer:
68-
case Job::Winegrower: return 2;
69-
case Job::CharBurner: return 3;
70-
case Job::Woodcutter:
71-
case Job::Forester: return 6;
72-
case Job::Fisher: return 7;
73-
case Job::Stonemason: return 8;
74-
default: throw std::logic_error("Invalid job");
75-
}
76-
}(job_);
79+
const unsigned max_radius = GetWorkRadius(job_);
7780
// Number of additional radii in which points should be found
7881
// I.e. 0 => Don't search for points further away than ones already found
7982
const unsigned additionalRadiiToFind = [](Job job) {

libs/s25main/figures/nofFarmhand.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class nofFarmhand : public nofBuildingWorker
5454
nofFarmhand(Job job, MapPoint pos, unsigned char player, nobUsual* workplace);
5555
nofFarmhand(SerializedGameData& sgd, unsigned obj_id);
5656

57+
static unsigned GetWorkRadius(Job job);
58+
5759
void Serialize(SerializedGameData& sgd) const override;
5860

5961
void HandleDerivedEvent(unsigned id) override;

libs/s25main/figures/nofForester.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "addons/const_addons.h"
1313
#include "buildings/noBaseBuilding.h"
1414
#include "network/GameClient.h"
15+
#include "nofFarmer.h"
1516
#include "ogl/glArchivItem_Bitmap_Player.h"
1617
#include "random/Random.h"
1718
#include "world/GameWorld.h"
@@ -21,31 +22,11 @@
2122
namespace {
2223
bool IsPotentialNewFieldForOwnFarm(GameWorld& world, const MapPoint pt, const unsigned char player)
2324
{
24-
constexpr unsigned FARM_FIELD_RADIUS = 2;
25-
26-
for(const auto dir : helpers::EnumRange<Direction>{})
27-
{
28-
if(world.GetPointRoad(pt, dir) != PointRoad::None)
29-
return false;
30-
}
31-
32-
if(!world.IsOfTerrain(pt, [](const auto& desc) { return desc.IsVital(); }))
25+
if(nofFarmer::GetNewFieldPointQuality(world, pt) == nofFarmhand::PointQuality::NotPossible)
3326
return false;
3427

35-
const NodalObjectType noType = world.GetNO(pt)->GetType();
36-
if(noType != NodalObjectType::Environment && noType != NodalObjectType::Nothing)
37-
return false;
38-
39-
for(const MapPoint nb : world.GetNeighbours(pt))
40-
{
41-
const NodalObjectType nbType = world.GetNO(nb)->GetType();
42-
if(nbType == NodalObjectType::Grainfield || nbType == NodalObjectType::Grapefield
43-
|| nbType == NodalObjectType::Building || nbType == NodalObjectType::Buildingsite)
44-
return false;
45-
}
46-
4728
return world.CheckPointsInRadius(
48-
pt, FARM_FIELD_RADIUS,
29+
pt, nofFarmhand::GetWorkRadius(Job::Farmer),
4930
[&world, player](const MapPoint farmPt, unsigned) {
5031
if(world.GetNO(farmPt)->GetType() != NodalObjectType::Building)
5132
return false;

0 commit comments

Comments
 (0)