Skip to content

Commit 7e4777e

Browse files
committed
Clean up isolated fish water test
1 parent 9c5a98f commit 7e4777e

5 files changed

Lines changed: 32 additions & 34 deletions

File tree

libs/s25main/figures/nofFisher.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,6 @@
1717
#include "random/Random.h"
1818
#include "world/GameWorld.h"
1919

20-
namespace {
21-
22-
bool IsUsableFishResource(const GameWorld& world, const MapPoint pt)
23-
{
24-
if(!world.GetNode(pt).resources.has(ResourceType::Fish))
25-
return false;
26-
27-
if(!world.IsWaterPoint(pt))
28-
return false;
29-
30-
for(const MapPoint nb : world.GetNeighbours(pt))
31-
{
32-
if(world.IsWaterPoint(nb))
33-
return true;
34-
}
35-
36-
return false;
37-
}
38-
39-
} // namespace
40-
4120
nofFisher::nofFisher(const MapPoint pos, const unsigned char player, nobUsual* workplace)
4221
: nofFarmhand(Job::Fisher, pos, player, workplace), fishing_dir(Direction::West), successful(false)
4322
{}
@@ -117,7 +96,7 @@ void nofFisher::WorkStarted()
11796
for(Direction dir : helpers::enumRange(RANDOM_ENUM(Direction)))
11897
{
11998
const Resource neighbourRes = world->GetNode(world->GetNeighbour(pos, dir)).resources;
120-
if(IsUsableFishResource(*world, world->GetNeighbour(pos, dir)))
99+
if(neighbourRes.has(ResourceType::Fish))
121100
{
122101
fishing_dir = dir;
123102
fishAmount = neighbourRes.getAmount();
@@ -151,7 +130,7 @@ nofFarmhand::PointQuality nofFisher::GetPointQuality(const MapPoint pt, bool /*
151130
// irgendwo drumherum muss es Fisch geben
152131
for(const MapPoint nb : world->GetNeighbours(pt))
153132
{
154-
if(IsUsableFishResource(*world, nb))
133+
if(world->GetNode(nb).resources.has(ResourceType::Fish))
155134
return PointQuality::Class1;
156135
}
157136

libs/s25main/figures/nofFisher.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ class nofFisher : public nofFarmhand
2626
/// Abgeleitete Klasse informieren, wenn fertig ist mit Arbeiten
2727
void WorkFinished() override;
2828

29-
/// Returns the quality of this working point or determines if the worker can work here at all
30-
PointQuality GetPointQuality(MapPoint pt, bool isBeforeWork) const override;
31-
3229
public:
3330
nofFisher(MapPoint pos, unsigned char player, nobUsual* workplace);
3431
nofFisher(SerializedGameData& sgd, unsigned obj_id);
@@ -38,4 +35,7 @@ class nofFisher : public nofFarmhand
3835
void Serialize(SerializedGameData& sgd) const override;
3936

4037
GO_Type GetGOT() const final { return GO_Type::NofFisher; }
38+
39+
/// Returns the quality of this working point or determines if the worker can work here at all
40+
PointQuality GetPointQuality(MapPoint pt, bool isBeforeWork) const override;
4141
};

libs/s25main/world/GameWorld.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,7 @@ void GameWorld::SetupResources()
13311331
}
13321332
ConvertMineResourceTypes(ResourceType::Gold, target);
13331333
PlaceAndFixWater();
1334+
RemoveUnusableFishResources();
13341335
}
13351336

13361337
/**
@@ -1375,6 +1376,21 @@ void GameWorld::PlaceAndFixWater()
13751376
}
13761377
}
13771378

1379+
void GameWorld::RemoveUnusableFishResources()
1380+
{
1381+
RTTR_FOREACH_PT(MapPoint, GetSize())
1382+
{
1383+
if(!GetNode(pt).resources.has(ResourceType::Fish))
1384+
continue;
1385+
1386+
if(!IsWaterPoint(pt)
1387+
|| !helpers::contains_if(GetNeighbours(pt), [this](const MapPoint nb) { return IsWaterPoint(nb); }))
1388+
{
1389+
SetResource(pt, Resource(ResourceType::Nothing, 0));
1390+
}
1391+
}
1392+
}
1393+
13781394
/// Gründet vom Schiff aus eine neue Kolonie
13791395
bool GameWorld::FoundColony(const HarborId harbor, const unsigned char player, const SeaId seaId)
13801396
{

libs/s25main/world/GameWorld.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ class GameWorld : public GameWorldBase
153153

154154
// Fills water depending on terrain and Addon setting
155155
void PlaceAndFixWater();
156+
// Removes fish resources that cannot be reached by fisheries
157+
void RemoveUnusableFishResources();
156158

157159
/// Gründet vom Schiff aus eine neue Kolonie, gibt true zurück bei Erfolg
158160
bool FoundColony(HarborId harbor, unsigned char player, SeaId seaId);

tests/s25Main/integration/testBuilding.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
#include "RttrForeachPt.h"
88
#include "buildings/nobBaseMilitary.h"
99
#include "buildings/nobUsual.h"
10+
#include "desktops/dskGameInterface.h"
1011
#include "factories/BuildingFactory.h"
1112
#include "figures/nofFarmhand.h"
1213
#include "figures/nofFisher.h"
13-
#include "gameTypes/Resource.h"
14-
#include "desktops/dskGameInterface.h"
1514
#include "helpers/containerUtils.h"
1615
#include "uiHelper/uiHelpers.hpp"
1716
#include "worldFixtures/CreateEmptyWorld.h"
@@ -21,6 +20,7 @@
2120
#include "nodeObjs/noEnvObject.h"
2221
#include "nodeObjs/noStaticObject.h"
2322
#include "gameTypes/GameTypesOutput.h"
23+
#include "gameTypes/Resource.h"
2424
#include <boost/test/unit_test.hpp>
2525

2626
// LCOV_EXCL_START
@@ -482,7 +482,6 @@ BOOST_FIXTURE_TEST_CASE(RoadRemovesObjs, EmptyWorldFixture1P)
482482
}
483483
}
484484

485-
486485
BOOST_FIXTURE_TEST_CASE(FisherIgnoresIsolatedFishWater, EmptyWorldFixture1PBiggest)
487486
{
488487
const DescIdx<TerrainDesc> tWater = GetWaterTerrain(world.GetDescription());
@@ -493,7 +492,6 @@ BOOST_FIXTURE_TEST_CASE(FisherIgnoresIsolatedFishWater, EmptyWorldFixture1PBigge
493492
BOOST_TEST_REQUIRE(fishery);
494493

495494
nofFisher fisher(fisheryPos, 0, fishery);
496-
const nofFarmhand& farmhand = fisher;
497495

498496
const MapPoint workPt = world.MakeMapPoint(fisheryPos + Position(4, 0));
499497
const MapPoint fishPt = world.GetNeighbour(workPt, Direction::East);
@@ -514,13 +512,16 @@ BOOST_FIXTURE_TEST_CASE(FisherIgnoresIsolatedFishWater, EmptyWorldFixture1PBigge
514512

515513
makeWaterPoint(fishPt);
516514
world.SetResource(fishPt, Resource(ResourceType::Fish, 4));
515+
world.SetupResources();
517516

518-
BOOST_TEST_REQUIRE(static_cast<unsigned>(farmhand.GetPointQuality(workPt))
519-
== static_cast<unsigned>(nofFarmhand::PointQuality::NotPossible));
517+
BOOST_TEST_REQUIRE(!world.GetNode(fishPt).resources.has(ResourceType::Fish));
518+
BOOST_TEST_REQUIRE((fisher.GetPointQuality(workPt, false) == nofFarmhand::PointQuality::NotPossible));
520519

521520
makeWaterPoint(world.GetNeighbour(fishPt, Direction::East));
521+
world.SetResource(fishPt, Resource(ResourceType::Fish, 4));
522+
world.SetupResources();
522523

523-
BOOST_TEST_REQUIRE(static_cast<unsigned>(farmhand.GetPointQuality(workPt))
524-
== static_cast<unsigned>(nofFarmhand::PointQuality::Class1));
524+
BOOST_TEST_REQUIRE(world.GetNode(fishPt).resources.has(ResourceType::Fish));
525+
BOOST_TEST_REQUIRE((fisher.GetPointQuality(workPt, false) == nofFarmhand::PointQuality::Class1));
525526
}
526527
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)