Skip to content

Commit

Permalink
Add edge point to another player if possible
Browse files Browse the repository at this point in the history
Fixes #81
  • Loading branch information
Flamefire committed Nov 28, 2017
1 parent 6d5e2f2 commit c415ad2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/test/testTerritoryRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ BOOST_FIXTURE_TEST_CASE(CreateTerritoryRegion, WorldFixtureEmpty2P)
RTTR_FOREACH_PT(MapPoint, world.GetSize())
{
uint8_t owner = region.GetOwner(Position(pt));
// if(!owner)
// RTTR_REQUIRE_EQUAL_MSG(world.GetNode(pt).owner, 0u, " on " << pt << " iteration " << i);
// else
// RTTR_REQUIRE_NE_MSG(world.GetNode(pt).owner, 0u, " on " << pt << " iteration " << i);
if(!owner)
RTTR_REQUIRE_EQUAL_MSG(world.GetNode(pt).owner, 0u, " on " << pt << " iteration " << i);
else
RTTR_REQUIRE_NE_MSG(world.GetNode(pt).owner, 0u, " on " << pt << " iteration " << i);
}
BOOST_FOREACH(const MapPoint pt, milBldPos)
{
Expand Down
38 changes: 34 additions & 4 deletions src/world/GameWorldGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ void GameWorldGame::RecalcTerritory(const noBaseBuilding& building, TerritoryCha
if(reason == TerritoryChangeReason::Build && sizeChanges[i] < 0)
{
GetPostMgr().SendMsg(
i, new PostMsgWithBuilding(GetEvMgr().GetCurrentGF(), _("Lost land by this building"), PostCategory::Military, building));
i, new PostMsgWithBuilding(GetEvMgr().GetCurrentGF(), _("Lost land by this building"), PostCategory::Military, building));
GetNotifications().publish(BuildingNote(BuildingNote::LostLand, i, building.GetPos(), building.GetBuildingType()));
}
}
Expand Down Expand Up @@ -664,9 +664,39 @@ TerritoryRegion GameWorldGame::CreateTerritoryRegion(const noBaseBuilding& build
}
}

// Wenn kein Land angrenzt, dann nicht nehmen
if(!isPlayerTerritoryNear)
region.SetOwner(pt, 0);
// All good?
if(isPlayerTerritoryNear)
continue;
// No neighbouring player territory found. Look for another
uint8_t newOwner = 0;
for(unsigned d = 0; d < Direction::COUNT; ++d)
{
Position neighbour = ::GetNeighbour(pt + region.startPt, Direction::fromInt(d));
uint8_t nbOwner = region.SafeGetOwner(neighbour - startPt);
// No or same player?
if(!nbOwner || nbOwner == owner)
continue;
bool isPlayerTerritory = true;
// Don't check this point as it would always fail
unsigned exceptDir = (Direction::fromInt(d) + 3u).toUInt();
for(unsigned d2 = 0; d2 < Direction::COUNT; ++d2)
{
if(d2 == exceptDir)
continue;
if(region.SafeGetOwner(::GetNeighbour(neighbour, Direction::fromInt(d2)) - startPt) != nbOwner)
{
isPlayerTerritory = false;
break;
}
}
// First one found gets it
if(isPlayerTerritory)
{
newOwner = nbOwner;
break;
}
}
region.SetOwner(pt, newOwner);
}

return region;
Expand Down
2 changes: 1 addition & 1 deletion src/world/TerritoryRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const TerritoryRegion::TRNode* TerritoryRegion::TryGetNode(Position realPt) cons
return &GetNode(realPt);
}

bool TerritoryRegion::AdjustCoords(Position &pt) const
bool TerritoryRegion::AdjustCoords(Position& pt) const
{
// The region might wrap around world boundaries. So we have to adjust the point so it will still be inside this region even if it is on
// "the other side" of the world wrap Note: Only 1 time wrapping around is allowed which is ensured by the assertion, that this size is
Expand Down
2 changes: 1 addition & 1 deletion src/world/TerritoryRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TerritoryRegion
TRNode* TryGetNode(const MapPoint& pt);
TRNode* TryGetNode(Position pt);
const TRNode* TryGetNode(Position pt) const;
bool AdjustCoords(Position &pt) const;
bool AdjustCoords(Position& pt) const;

const GameWorldBase& world;
std::vector<TRNode> nodes;
Expand Down

0 comments on commit c415ad2

Please sign in to comment.