Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions libs/s25main/ai/aijh/AIConstruction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "buildings/nobMilitary.h"
#include "buildings/nobUsual.h"
#include "helpers/containerUtils.h"
#include "random/Random.h"
#include "nodeObjs/noFlag.h"
#include "nodeObjs/noRoadNode.h"
#include "gameTypes/BuildingQuality.h"
Expand Down Expand Up @@ -465,10 +466,12 @@
const BuildingType biggestBld = GetBiggestAllowedMilBuilding().value();

const Inventory& inventory = aii.GetInventory();
if(((rand() % 3) == 0 || inventory.people[Job::Private] < 15)
uint8_t playerId = aii.GetPlayerId();
if((RANDOM.Rand(RANDOM_CONTEXT2(playerId), 3) == 0 || inventory.people[Job::Private] < 15)
&& (inventory.goods[GoodType::Stones] > 6 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0))
bld = BuildingType::Guardhouse;
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && rand() % 10 != 0 && aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && RANDOM.Rand(RANDOM_CONTEXT2(playerId), 10) != 0
&& aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
{
if(aii.CanBuildBuildingtype(BuildingType::Watchtower))
return BuildingType::Watchtower;
Expand All @@ -478,13 +481,12 @@
{
if(aijh.UpdateUpgradeBuilding() < 0 && bldPlanner.GetNumBuildingSites(biggestBld) < 1
&& (inventory.goods[GoodType::Stones] > 20 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0)
&& rand() % 10 != 0)
&& RANDOM.Rand(RANDOM_CONTEXT2(playerId), 10) != 0)
{
return biggestBld;
}
}

uint8_t playerId = aii.GetPlayerId();
sortedMilitaryBlds military = aii.gwb.LookForMilitaryBuildings(pt, 3);
for(const nobBaseMilitary* milBld : military)
{
Expand All @@ -493,7 +495,7 @@
// Prüfen ob Feind in der Nähe
if(milBld->GetPlayer() != playerId && distance < 35)
{
int randmil = rand();
int randmil = RANDOM.Rand(RANDOM_CONTEXT2(playerId), std::numeric_limits<int>::max());

Check warning on line 498 in libs/s25main/ai/aijh/AIConstruction.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/ai/aijh/AIConstruction.cpp#L498

Added line #L498 was not covered by tests
bool buildCatapult = randmil % 8 == 0 && aii.CanBuildCatapult()
&& bldPlanner.GetNumAdditionalBuildingsWanted(BuildingType::Catapult) > 0;
// another catapult within "min" radius? ->dont build here!
Expand Down
20 changes: 11 additions & 9 deletions libs/s25main/ai/aijh/AIPlayerJH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "notifications/RoadNote.h"
#include "notifications/ShipNote.h"
#include "pathfinding/PathConditionRoad.h"
#include "random/Random.h"
#include "nodeObjs/noAnimal.h"
#include "nodeObjs/noFlag.h"
#include "nodeObjs/noShip.h"
Expand Down Expand Up @@ -344,7 +345,7 @@
DistributeGoodsByBlocking(GoodType::Boards, 30);
DistributeGoodsByBlocking(GoodType::Stones, 50);
// go to the picked random warehouse and try to build around it
int randomStore = rand() % (storehouses.size());
int randomStore = RANDOM.Rand(RANDOM_CONTEXT2(playerId), storehouses.size());
auto it = storehouses.begin();
std::advance(it, randomStore);
const MapPoint whPos = (*it)->GetPos();
Expand All @@ -365,7 +366,7 @@
const std::list<nobMilitary*>& militaryBuildings = aii.GetMilitaryBuildings();
if(militaryBuildings.empty())
return;
int randomMiliBld = rand() % militaryBuildings.size();
int randomMiliBld = RANDOM.Rand(RANDOM_CONTEXT2(playerId), militaryBuildings.size());
auto it2 = militaryBuildings.begin();
std::advance(it2, randomMiliBld);
MapPoint bldPos = (*it2)->GetPos();
Expand Down Expand Up @@ -1209,7 +1210,7 @@
aii.FoundColony(ship);
else
{
const unsigned offset = rand() % helpers::MaxEnumValue_v<ShipDirection>;
const unsigned offset = RANDOM.Rand(RANDOM_CONTEXT2(playerId), helpers::MaxEnumValue_v<ShipDirection>);

Check warning on line 1213 in libs/s25main/ai/aijh/AIPlayerJH.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/ai/aijh/AIPlayerJH.cpp#L1213

Added line #L1213 was not covered by tests
for(auto dir : helpers::EnumRange<ShipDirection>{})
{
dir = ShipDirection((rttr::enum_cast(dir) + offset) % helpers::MaxEnumValue_v<ShipDirection>);
Expand Down Expand Up @@ -1254,9 +1255,7 @@

UpdateNodesAround(pt, 3);

int random = rand();

if(random % 2 == 0)
if(RANDOM.Rand(RANDOM_CONTEXT2(playerId), 2) == 0)

Check warning on line 1258 in libs/s25main/ai/aijh/AIPlayerJH.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/ai/aijh/AIPlayerJH.cpp#L1258

Added line #L1258 was not covered by tests
AddMilitaryBuildJob(pt);
else // if (random % 12 == 0)
AddBuildJob(BuildingType::Woodcutter, pt);
Expand Down Expand Up @@ -1538,7 +1537,7 @@
// We skip the current building with a probability of limit/numMilBlds
// -> For twice the number of blds as the limit we will most likely skip every 2nd building
// This way we check roughly (at most) limit buildings but avoid any preference for one building over an other
if(rand() % numMilBlds > limit)
if(static_cast<unsigned>(RANDOM.Rand(RANDOM_CONTEXT2(playerId), static_cast<int>(numMilBlds))) > limit)
continue;

if(milBld->GetFrontierDistance() == FrontierDistance::Far) // inland building? -> skip it
Expand Down Expand Up @@ -1571,7 +1570,7 @@

// shuffle everything but headquarters and harbors without any troops in them
std::shuffle(potentialTargets.begin() + hq_or_harbor_without_soldiers, potentialTargets.end(),
std::mt19937(std::random_device()()));
std::mt19937(RANDOM.Rand(RANDOM_CONTEXT2(playerId), 2048)));

// check for each potential attacking target the number of available attacking soldiers
for(const nobBaseMilitary* target : potentialTargets)
Expand Down Expand Up @@ -1704,7 +1703,10 @@
unsigned limit = 15;
unsigned skip = 0;
if(searcharoundharborspots.size() > 15)
skip = std::max<int>(rand() % (searcharoundharborspots.size() / 15 + 1) * 15, 1) - 1;
skip =
std::max<int>(
RANDOM.Rand(RANDOM_CONTEXT2(playerId), static_cast<int>(searcharoundharborspots.size() / 15 + 1) * 15), 1)
- 1;

Check warning on line 1709 in libs/s25main/ai/aijh/AIPlayerJH.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/ai/aijh/AIPlayerJH.cpp#L1706-L1709

Added lines #L1706 - L1709 were not covered by tests
for(unsigned i = skip; i < searcharoundharborspots.size() && limit > 0; i++)
{
limit--;
Expand Down
Loading