Skip to content

Commit 7f20e8d

Browse files
committed
Use s25 random class in AIJH
1 parent 828eaad commit 7f20e8d

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

libs/s25main/ai/aijh/AIConstruction.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "buildings/nobMilitary.h"
1717
#include "buildings/nobUsual.h"
1818
#include "helpers/containerUtils.h"
19+
#include "random/Random.h"
1920
#include "nodeObjs/noFlag.h"
2021
#include "nodeObjs/noRoadNode.h"
2122
#include "gameTypes/BuildingQuality.h"
@@ -465,10 +466,12 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
465466
const BuildingType biggestBld = GetBiggestAllowedMilBuilding().value();
466467

467468
const Inventory& inventory = aii.GetInventory();
468-
if(((rand() % 3) == 0 || inventory.people[Job::Private] < 15)
469+
uint8_t playerId = aii.GetPlayerId();
470+
if((RANDOM.Rand(RANDOM_CONTEXT2(playerId), 3) == 0 || inventory.people[Job::Private] < 15)
469471
&& (inventory.goods[GoodType::Stones] > 6 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0))
470472
bld = BuildingType::Guardhouse;
471-
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && rand() % 10 != 0 && aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
473+
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && RANDOM.Rand(RANDOM_CONTEXT2(playerId), 10) != 0
474+
&& aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
472475
{
473476
if(aii.CanBuildBuildingtype(BuildingType::Watchtower))
474477
return BuildingType::Watchtower;
@@ -478,13 +481,12 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
478481
{
479482
if(aijh.UpdateUpgradeBuilding() < 0 && bldPlanner.GetNumBuildingSites(biggestBld) < 1
480483
&& (inventory.goods[GoodType::Stones] > 20 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0)
481-
&& rand() % 10 != 0)
484+
&& RANDOM.Rand(RANDOM_CONTEXT2(playerId), 10) != 0)
482485
{
483486
return biggestBld;
484487
}
485488
}
486489

487-
uint8_t playerId = aii.GetPlayerId();
488490
sortedMilitaryBlds military = aii.gwb.LookForMilitaryBuildings(pt, 3);
489491
for(const nobBaseMilitary* milBld : military)
490492
{
@@ -493,7 +495,7 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
493495
// Prüfen ob Feind in der Nähe
494496
if(milBld->GetPlayer() != playerId && distance < 35)
495497
{
496-
int randmil = rand();
498+
int randmil = RANDOM.Rand(RANDOM_CONTEXT2(playerId), std::numeric_limits<int>::max());
497499
bool buildCatapult = randmil % 8 == 0 && aii.CanBuildCatapult()
498500
&& bldPlanner.GetNumAdditionalBuildingsWanted(BuildingType::Catapult) > 0;
499501
// another catapult within "min" radius? ->dont build here!

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "notifications/RoadNote.h"
2727
#include "notifications/ShipNote.h"
2828
#include "pathfinding/PathConditionRoad.h"
29+
#include "random/Random.h"
2930
#include "nodeObjs/noAnimal.h"
3031
#include "nodeObjs/noFlag.h"
3132
#include "nodeObjs/noShip.h"
@@ -344,7 +345,7 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
344345
DistributeGoodsByBlocking(GoodType::Boards, 30);
345346
DistributeGoodsByBlocking(GoodType::Stones, 50);
346347
// go to the picked random warehouse and try to build around it
347-
int randomStore = rand() % (storehouses.size());
348+
int randomStore = RANDOM.Rand(RANDOM_CONTEXT2(playerId), storehouses.size());
348349
auto it = storehouses.begin();
349350
std::advance(it, randomStore);
350351
const MapPoint whPos = (*it)->GetPos();
@@ -365,7 +366,7 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
365366
const std::list<nobMilitary*>& militaryBuildings = aii.GetMilitaryBuildings();
366367
if(militaryBuildings.empty())
367368
return;
368-
int randomMiliBld = rand() % militaryBuildings.size();
369+
int randomMiliBld = RANDOM.Rand(RANDOM_CONTEXT2(playerId), militaryBuildings.size());
369370
auto it2 = militaryBuildings.begin();
370371
std::advance(it2, randomMiliBld);
371372
MapPoint bldPos = (*it2)->GetPos();
@@ -1209,7 +1210,7 @@ void AIPlayerJH::HandleExpedition(const noShip* ship)
12091210
aii.FoundColony(ship);
12101211
else
12111212
{
1212-
const unsigned offset = rand() % helpers::MaxEnumValue_v<ShipDirection>;
1213+
const unsigned offset = RANDOM.Rand(RANDOM_CONTEXT2(playerId), helpers::MaxEnumValue_v<ShipDirection>);
12131214
for(auto dir : helpers::EnumRange<ShipDirection>{})
12141215
{
12151216
dir = ShipDirection((rttr::enum_cast(dir) + offset) % helpers::MaxEnumValue_v<ShipDirection>);
@@ -1254,9 +1255,7 @@ void AIPlayerJH::HandleTreeChopped(const MapPoint pt)
12541255

12551256
UpdateNodesAround(pt, 3);
12561257

1257-
int random = rand();
1258-
1259-
if(random % 2 == 0)
1258+
if(RANDOM.Rand(RANDOM_CONTEXT2(playerId), 2) == 0)
12601259
AddMilitaryBuildJob(pt);
12611260
else // if (random % 12 == 0)
12621261
AddBuildJob(BuildingType::Woodcutter, pt);
@@ -1538,7 +1537,7 @@ void AIPlayerJH::TryToAttack()
15381537
// We skip the current building with a probability of limit/numMilBlds
15391538
// -> For twice the number of blds as the limit we will most likely skip every 2nd building
15401539
// This way we check roughly (at most) limit buildings but avoid any preference for one building over an other
1541-
if(rand() % numMilBlds > limit)
1540+
if(static_cast<unsigned>(RANDOM.Rand(RANDOM_CONTEXT2(playerId), static_cast<int>(numMilBlds))) > limit)
15421541
continue;
15431542

15441543
if(milBld->GetFrontierDistance() == FrontierDistance::Far) // inland building? -> skip it
@@ -1571,7 +1570,7 @@ void AIPlayerJH::TryToAttack()
15711570

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

15761575
// check for each potential attacking target the number of available attacking soldiers
15771576
for(const nobBaseMilitary* target : potentialTargets)
@@ -1704,7 +1703,10 @@ void AIPlayerJH::TrySeaAttack()
17041703
unsigned limit = 15;
17051704
unsigned skip = 0;
17061705
if(searcharoundharborspots.size() > 15)
1707-
skip = std::max<int>(rand() % (searcharoundharborspots.size() / 15 + 1) * 15, 1) - 1;
1706+
skip =
1707+
std::max<int>(
1708+
RANDOM.Rand(RANDOM_CONTEXT2(playerId), static_cast<int>(searcharoundharborspots.size() / 15 + 1) * 15), 1)
1709+
- 1;
17081710
for(unsigned i = skip; i < searcharoundharborspots.size() && limit > 0; i++)
17091711
{
17101712
limit--;

0 commit comments

Comments
 (0)