Skip to content
Merged
42 changes: 0 additions & 42 deletions libs/s25main/GamePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,15 +1332,12 @@ void GamePlayer::RegulateAllTroops()
milBld->RegulateTroops();
}

/// Prüft von allen Militärgebäuden die Fahnen neu
void GamePlayer::RecalcMilitaryFlags()
{
for(nobMilitary* milBld : buildings.GetMilitaryBuildings())
milBld->LookForEnemyBuildings(nullptr);
}

/// Sucht für Soldaten ein neues Militärgebäude, als Argument wird Referenz auf die
/// entsprechende Soldatenanzahl im Lagerhaus verlangt
void GamePlayer::NewSoldiersAvailable(const unsigned& soldier_count)
{
RTTR_Assert(soldier_count > 0);
Expand Down Expand Up @@ -1436,7 +1433,6 @@ void GamePlayer::ChangeMilitarySettings(const MilitarySettings& military_setting
RefreshDefenderList();
}

/// Setzt neue Werkzeugeinstellungen
void GamePlayer::ChangeToolsSettings(const ToolSettings& tools_settings,
const helpers::EnumArray<int8_t, Tool>& orderChanges)
{
Expand All @@ -1459,7 +1455,6 @@ void GamePlayer::ChangeToolsSettings(const ToolSettings& tools_settings,
}
}

/// Setzt neue Verteilungseinstellungen
void GamePlayer::ChangeDistribution(const Distributions& distribution_settings)
{
unsigned idx = 0;
Expand All @@ -1471,7 +1466,6 @@ void GamePlayer::ChangeDistribution(const Distributions& distribution_settings)
RecalcDistribution();
}

/// Setzt neue Baureihenfolge-Einstellungen
void GamePlayer::ChangeBuildOrder(bool useCustomBuildOrder, const BuildOrders& order_data)
{
this->useCustomBuildOrder_ = useCustomBuildOrder;
Expand Down Expand Up @@ -1570,7 +1564,6 @@ void GamePlayer::IncreaseMerchandiseStatistic(GoodType type)
}
}

/// Calculates current statistics
void GamePlayer::CalcStatistics()
{
// Waren aus der Inventur zählen
Expand Down Expand Up @@ -1710,7 +1703,6 @@ void GamePlayer::AcceptPact(const unsigned id, const PactType pt, const unsigned
}
}

/// Bündnis (real, d.h. spielentscheidend) abschließen
void GamePlayer::MakePact(const PactType pt, const unsigned char other_player, const unsigned duration)
{
pacts[other_player][pt].accepted = true;
Expand All @@ -1722,7 +1714,6 @@ void GamePlayer::MakePact(const PactType pt, const unsigned char other_player, c
std::make_unique<PostMsg>(world.GetEvMgr().GetCurrentGF(), pt, world.GetPlayer(other_player), true));
}

/// Zeigt an, ob ein Pakt besteht
PactState GamePlayer::GetPactState(const PactType pt, const unsigned char other_player) const
{
// Prüfen, ob Bündnis in Kraft ist
Expand All @@ -1739,7 +1730,6 @@ PactState GamePlayer::GetPactState(const PactType pt, const unsigned char other_
return PactState::None;
}

/// all allied players get a letter with the location
void GamePlayer::NotifyAlliesOfLocation(const MapPoint pt)
{
for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
Expand All @@ -1751,7 +1741,6 @@ void GamePlayer::NotifyAlliesOfLocation(const MapPoint pt)
}
}

/// Gibt die verbleibende Dauer zurück, die ein Bündnis noch laufen wird (DURATION_INFINITE = für immer)
unsigned GamePlayer::GetRemainingPactTime(const PactType pt, const unsigned char other_player) const
{
if(pacts[other_player][pt].duration)
Expand All @@ -1769,8 +1758,6 @@ unsigned GamePlayer::GetRemainingPactTime(const PactType pt, const unsigned char
return 0;
}

/// Gibt Einverständnis, dass dieser Spieler den Pakt auflösen will
/// Falls dieser Spieler einen Bündnisvorschlag gemacht hat, wird dieser dagegen zurückgenommen
void GamePlayer::CancelPact(const PactType pt, const unsigned char otherPlayerIdx)
{
// Don't try to cancel pact with self
Expand Down Expand Up @@ -1888,11 +1875,9 @@ void GamePlayer::DecreaseInventoryWare(const GoodType ware, const unsigned count
global_inventory.Remove(ConvertShields(ware), count);
}

/// Registriert ein Schiff beim Einwohnermeldeamt
void GamePlayer::RegisterShip(noShip& ship)
{
ships.push_back(&ship);
// Evtl bekommt das Schiffchen gleich was zu tun?
GetJobForShip(ship);
}

Expand All @@ -1909,7 +1894,6 @@ struct ShipForHarbor
}
};

/// Schiff für Hafen bestellen
bool GamePlayer::OrderShip(nobHarborBuilding& hb)
{
std::vector<ShipForHarbor> sfh;
Expand Down Expand Up @@ -1981,18 +1965,15 @@ bool GamePlayer::OrderShip(nobHarborBuilding& hb)
return false;
}

/// Meldet das Schiff wieder ab
void GamePlayer::RemoveShip(noShip* ship)
{
auto it = helpers::find(ships, ship);
RTTR_Assert(it != ships.end());
ships.erase(it);
}

/// Versucht, für ein untätiges Schiff eine Arbeit zu suchen
void GamePlayer::GetJobForShip(noShip& ship)
{
// Evtl. steht irgendwo eine Expedition an und das Schiff kann diese übernehmen
nobHarborBuilding* best = nullptr;
int best_points = 0;
std::vector<Direction> best_route;
Expand Down Expand Up @@ -2040,7 +2021,6 @@ void GamePlayer::GetJobForShip(noShip& ship)

// Einen Hafen gefunden?
if(best)
// Dann bekommt das gleich der Hafen
ship.GoToHarbor(*best, best_route);
}

Expand All @@ -2049,7 +2029,6 @@ unsigned GamePlayer::GetShipID(const noShip& ship) const
return static_cast<unsigned>(helpers::indexOf(ships, &ship));
}

/// Gibt ein Schiff anhand der ID zurück bzw. nullptr, wenn keines mit der ID existiert
noShip* GamePlayer::GetShipByID(const unsigned ship_id) const
{
if(ship_id >= ships.size())
Expand All @@ -2058,19 +2037,6 @@ noShip* GamePlayer::GetShipByID(const unsigned ship_id) const
return ships[ship_id];
}

void GamePlayer::AddHarborsAtSea(std::vector<nobHarborBuilding*>& harborBuildings, const SeaId seaId) const
{
for(nobHarborBuilding* harbor : buildings.GetHarbors())
{
if(helpers::contains(harborBuildings, harbor))
continue;

if(world.IsHarborAtSea(harbor->GetHarborPosID(), seaId))
harborBuildings.push_back(harbor);
}
}

/// Gibt die Anzahl der Schiffe, die einen bestimmten Hafen ansteuern, zurück
unsigned GamePlayer::GetShipsToHarbor(const nobHarborBuilding& hb) const
{
unsigned count = 0;
Expand All @@ -2083,8 +2049,6 @@ unsigned GamePlayer::GetShipsToHarbor(const nobHarborBuilding& hb) const
return count;
}

/// Sucht einen Hafen in der Nähe, wo dieses Schiff seine Waren abladen kann
/// gibt true zurück, falls erfolgreich
bool GamePlayer::FindHarborForUnloading(noShip* ship, const MapPoint start, HarborId* goalHarborId,
std::vector<Direction>* route, nobHarborBuilding* exception)
{
Expand Down Expand Up @@ -2169,7 +2133,6 @@ void GamePlayer::TestForEmergencyProgramm()
}
}

/// Testet die Bündnisse, ob sie nicht schon abgelaufen sind
void GamePlayer::TestPacts()
{
for(unsigned i = 0; i < world.GetNumPlayers(); ++i)
Expand Down Expand Up @@ -2224,9 +2187,6 @@ bool GamePlayer::CanBuildCatapult() const
return bc.buildings[BuildingType::Catapult] + bc.buildingSites[BuildingType::Catapult] < max;
}

/// A ship has discovered new hostile territory --> determines if this is new
/// i.e. there is a sufficient distance to older locations
/// Returns true if yes and false if not
bool GamePlayer::ShipDiscoveredHostileTerritory(const MapPoint location)
{
// Prüfen, ob Abstand zu bisherigen Punkten nicht zu klein
Expand All @@ -2242,7 +2202,6 @@ bool GamePlayer::ShipDiscoveredHostileTerritory(const MapPoint location)
return true;
}

/// For debug only
bool GamePlayer::IsDependentFigure(const noFigure& fig)
{
for(const nobBaseWarehouse* wh : buildings.GetStorehouses())
Expand Down Expand Up @@ -2293,7 +2252,6 @@ struct WarehouseDistanceComparator
}
};

/// Send wares to warehouse wh
void GamePlayer::Trade(nobBaseWarehouse* goalWh, const boost_variant2<GoodType, Job>& what, unsigned count) const
{
if(!world.GetGGS().isEnabled(AddonId::TRADE))
Expand Down
2 changes: 0 additions & 2 deletions libs/s25main/GamePlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ class GamePlayer : public GamePlayerInfo
unsigned GetNumShips() const { return ships.size(); }
/// Gibt liste der Schiffe zurück
const std::vector<noShip*>& GetShips() const { return ships; }
/// Add all unique harbor buildings at the given sea to the given vector
void AddHarborsAtSea(std::vector<nobHarborBuilding*>& harborBuildings, SeaId seaId) const;
/// Gibt die Anzahl der Schiffe, die einen bestimmten Hafen ansteuern, zurück
unsigned GetShipsToHarbor(const nobHarborBuilding& hb) const;
/// Sucht einen Hafen in der Nähe, wo dieses Schiff seine Waren abladen kann
Expand Down
56 changes: 43 additions & 13 deletions libs/s25main/Pathfinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

#include "GamePlayer.h"
#include "helpers/EnumRange.h"
#include "helpers/IdRange.h"
#include "pathfinding/FreePathFinder.h"
#include "pathfinding/FreePathFinderImpl.h"
#include "pathfinding/PathConditionHuman.h"
#include "pathfinding/PathConditionShip.h"
#include "pathfinding/PathConditionTrade.h"
#include "pathfinding/RoadPathFinder.h"
#include "pathfinding/ShipPathData.h"
#include "world/GameWorld.h"
#include "gameTypes/ShipDirection.h"
#include "gameData/GameConsts.h"
Expand Down Expand Up @@ -51,11 +53,10 @@ RoadPathDirection GameWorld::FindPathForWareOnRoads(const noRoadNode& start, con
}

bool GameWorldBase::FindShipPathToHarbor(const MapPoint start, HarborId harborId, SeaId seaId,
std::vector<Direction>* route, unsigned* length)
std::vector<Direction>* route, unsigned* length) const
{
// Find the distance to the furthest harbor from the target harbor and take that as maximum
unsigned maxDistance = 0;
const MapPoint coastalPoint = GetCoastalPoint(harborId, seaId);
RTTR_Assert(coastalPoint.isValid());

// already arrived?
if(start == coastalPoint)
Expand All @@ -66,31 +67,60 @@ bool GameWorldBase::FindShipPathToHarbor(const MapPoint start, HarborId harborId
route->clear();
return true;
}
auto& shipPathData = GetShipPathData();
// If we start from a harbor we can get the route directly w/o the costly pathfinding
for(const auto startHbId : helpers::idRange<HarborId>(GetNumHarborPoints()))
{
if(GetCoastalPoint(startHbId, seaId) == start)
{
auto hbRoute = shipPathData.getHarborConnection(startHbId, harborId, seaId);
if(length)
*length = hbRoute.size();
if(route)
*route = std::move(hbRoute);
}
}

for(const auto dir : helpers::EnumRange<ShipDirection>{})
// Try cache first
bool reversed = false;
if(const auto* cachedPath = shipPathData.findCachedPath(start, coastalPoint, reversed))
{
const std::vector<HarborPos::Neighbor>& neighbors = GetHarborNeighbors(harborId, dir);
for(const HarborPos::Neighbor& neighbor : neighbors)
if(route)
{
if(IsHarborAtSea(neighbor.id, seaId) && neighbor.distance > maxDistance)
maxDistance = neighbor.distance;
*route = *cachedPath;
if(reversed)
{
std::reverse(route->begin(), route->end());
for(auto& d : *route)
d = d + 3u; // reverse direction
}
}
if(length)
*length = cachedPath->size();
return true;
}
// Add a few fields reserve
maxDistance += 6;
return FindShipPath(start, coastalPoint, maxDistance, route, length);

// Not in cache -> compute full path and add
std::vector<Direction> newRoute;
if(!FindShipPath(start, coastalPoint, std::numeric_limits<unsigned>::max(), &newRoute, length))
return false;
shipPathData.addToCache(start, coastalPoint, newRoute);
if(route)
*route = std::move(newRoute);

return true;
}

bool GameWorldBase::FindShipPath(const MapPoint start, const MapPoint dest, unsigned maxDistance,
std::vector<Direction>* route, unsigned* length)
std::vector<Direction>* route, unsigned* length) const
{
return GetFreePathFinder().FindPath(start, dest, true, maxDistance, route, length, nullptr,
PathConditionShip(*this));
}

/// Prüft, ob eine Schiffsroute noch Gültigkeit hat
bool GameWorld::CheckShipRoute(const MapPoint start, const std::vector<Direction>& route, const unsigned pos,
MapPoint* dest)
MapPoint* dest) const
{
return GetFreePathFinder().CheckRoute(start, route, pos, PathConditionShip(*this), dest);
}
Expand Down
3 changes: 2 additions & 1 deletion libs/s25main/SerializedGameData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
/// 12: leatheraddon added, three new building types and three new goods
/// 13: SeaId & HarborId: World::harborData w/o dummy entry at 0
/// 14: Remove "age" field in nobBaseMilitary
static const unsigned currentGameDataVersion = 14;
/// 15: Add sea to HarborPos::Neighbor
static const unsigned currentGameDataVersion = 15;
// clang-format on

std::unique_ptr<GameObject> SerializedGameData::Create_GameObject(const GO_Type got, const unsigned obj_id)
Expand Down
6 changes: 3 additions & 3 deletions libs/s25main/TradePathCache.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2005 - 2021 Settlers Freaks (sf-team at siedler25.org)
// Copyright (C) 2005 - 2026 Settlers Freaks (sf-team at siedler25.org)
//
// SPDX-License-Identifier: GPL-2.0-or-later

Expand All @@ -8,7 +8,7 @@
#include "world/GameWorld.h"
#include "gameData/GameConsts.h"

bool TradePathCache::pathExists(const MapPoint start, const MapPoint goal, const unsigned char player)
bool TradePathCache::pathExists(const MapPoint start, const MapPoint goal, const PlayerIdx player)
{
RTTR_Assert(start != goal);

Expand Down Expand Up @@ -57,7 +57,7 @@ int TradePathCache::findEntry(const MapPoint start, const MapPoint goal, const P
return -1;
}

void TradePathCache::addEntry(TradePath path, const unsigned char player)
void TradePathCache::addEntry(TradePath path, const PlayerIdx player)
{
Entry entry{player, world.GetEvMgr().GetCurrentGF(), std::move(path)};

Expand Down
Loading
Loading