Skip to content

Commit 6da2a5b

Browse files
wichernFlamefire
authored andcommitted
Apply review suggestions
1 parent 33948c4 commit 6da2a5b

4 files changed

Lines changed: 34 additions & 17 deletions

File tree

libs/s25main/ai/aijh/AIConstruction.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,10 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
466466
const BuildingType biggestBld = GetBiggestAllowedMilBuilding().value();
467467

468468
const Inventory& inventory = aii.GetInventory();
469-
uint8_t playerId = aii.GetPlayerId();
470-
if((AI::randomValue<int>(0, 2) == 0 || inventory.people[Job::Private] < 15)
469+
if((inventory.people[Job::Private] < 15 || AI::random())
471470
&& (inventory.goods[GoodType::Stones] > 6 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0))
472471
bld = BuildingType::Guardhouse;
473-
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && AI::randomValue<int>(0, 9) != 0
474-
&& aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
472+
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && !AI::random(9) && aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
475473
{
476474
if(aii.CanBuildBuildingtype(BuildingType::Watchtower))
477475
return BuildingType::Watchtower;
@@ -481,12 +479,13 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
481479
{
482480
if(aijh.UpdateUpgradeBuilding() < 0 && bldPlanner.GetNumBuildingSites(biggestBld) < 1
483481
&& (inventory.goods[GoodType::Stones] > 20 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0)
484-
&& AI::randomValue<int>(0, 9) != 0)
482+
&& !AI::random(9u))
485483
{
486484
return biggestBld;
487485
}
488486
}
489487

488+
uint8_t playerId = aii.GetPlayerId();
490489
sortedMilitaryBlds military = aii.gwb.LookForMilitaryBuildings(pt, 3);
491490
for(const nobBaseMilitary* milBld : military)
492491
{
@@ -495,7 +494,7 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
495494
// Prüfen ob Feind in der Nähe
496495
if(milBld->GetPlayer() != playerId && distance < 35)
497496
{
498-
int randmil = AI::randomValue<int>(0, std::numeric_limits<int>::max());
497+
const auto randmil = AI::randomValue(0, std::numeric_limits<int>::max());
499498
bool buildCatapult = randmil % 8 == 0 && aii.CanBuildCatapult()
500499
&& bldPlanner.GetNumAdditionalBuildingsWanted(BuildingType::Catapult) > 0;
501500
// another catapult within "min" radius? ->dont build here!

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,8 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
344344
DistributeGoodsByBlocking(GoodType::Boards, 30);
345345
DistributeGoodsByBlocking(GoodType::Stones, 50);
346346
// go to the picked random warehouse and try to build around it
347-
int randomStore = AI::randomValue<int>(0, storehouses.size() - 1);
348347
auto it = storehouses.begin();
349-
std::advance(it, randomStore);
348+
std::advance(it, AI::randomIndex(storehouses));
350349
const MapPoint whPos = (*it)->GetPos();
351350
UpdateNodesAround(whPos, 15); // update the area we want to build in first
352351
for(const BuildingType i : bldToTest)
@@ -365,7 +364,7 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
365364
const std::list<nobMilitary*>& militaryBuildings = aii.GetMilitaryBuildings();
366365
if(militaryBuildings.empty())
367366
return;
368-
int randomMiliBld = AI::randomValue<int>(0, militaryBuildings.size() - 1);
367+
const int randomMiliBld = static_cast<int>(AI::randomIndex(militaryBuildings));
369368
auto it2 = militaryBuildings.begin();
370369
std::advance(it2, randomMiliBld);
371370
MapPoint bldPos = (*it2)->GetPos();
@@ -1209,7 +1208,7 @@ void AIPlayerJH::HandleExpedition(const noShip* ship)
12091208
aii.FoundColony(ship);
12101209
else
12111210
{
1212-
const unsigned offset = AI::randomValue<unsigned>(0, helpers::MaxEnumValue_v<ShipDirection> - 1);
1211+
const unsigned offset = AI::randomValue(0u, helpers::MaxEnumValue_v<ShipDirection> - 1u);
12131212
for(auto dir : helpers::EnumRange<ShipDirection>{})
12141213
{
12151214
dir = ShipDirection((rttr::enum_cast(dir) + offset) % helpers::MaxEnumValue_v<ShipDirection>);
@@ -1254,7 +1253,7 @@ void AIPlayerJH::HandleTreeChopped(const MapPoint pt)
12541253

12551254
UpdateNodesAround(pt, 3);
12561255

1257-
if(AI::randomValue<int>(0, 1) == 0)
1256+
if(AI::random())
12581257
AddMilitaryBuildJob(pt);
12591258
else // if (random % 12 == 0)
12601259
AddBuildJob(BuildingType::Woodcutter, pt);
@@ -1536,7 +1535,7 @@ void AIPlayerJH::TryToAttack()
15361535
// We skip the current building with a probability of limit/numMilBlds
15371536
// -> For twice the number of blds as the limit we will most likely skip every 2nd building
15381537
// This way we check roughly (at most) limit buildings but avoid any preference for one building over an other
1539-
if(AI::randomValue<unsigned>(0, numMilBlds - 1) > limit)
1538+
if(AI::random(numMilBlds - 1u, limit))
15401539
continue;
15411540

15421541
if(milBld->GetFrontierDistance() == FrontierDistance::Far) // inland building? -> skip it
@@ -1569,7 +1568,7 @@ void AIPlayerJH::TryToAttack()
15691568

15701569
// shuffle everything but headquarters and harbors without any troops in them
15711570
std::shuffle(potentialTargets.begin() + hq_or_harbor_without_soldiers, potentialTargets.end(),
1572-
std::mt19937(AI::randomValue<unsigned>(0, 2047)));
1571+
AI::getRandomGenerator());
15731572

15741573
// check for each potential attacking target the number of available attacking soldiers
15751574
for(const nobBaseMilitary* target : potentialTargets)
@@ -1702,8 +1701,7 @@ void AIPlayerJH::TrySeaAttack()
17021701
unsigned limit = 15;
17031702
unsigned skip = 0;
17041703
if(searcharoundharborspots.size() > 15)
1705-
skip =
1706-
std::max<int>(AI::randomValue<int>(0, static_cast<int>(searcharoundharborspots.size() / 15) * 15), 1) - 1;
1704+
skip = AI::randomValue(0u, static_cast<unsigned>(searcharoundharborspots.size() / 15u)) * 15u;
17071705
for(unsigned i = skip; i < searcharoundharborspots.size() && limit > 0; i++)
17081706
{
17091707
limit--;

libs/s25main/ai/random.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ namespace AI {
88

99
std::minstd_rand& getRandomGenerator()
1010
{
11-
std::random_device rnd_dev;
12-
static std::minstd_rand rng(rnd_dev());
11+
static std::minstd_rand rng(std::random_device{}());
1312
return rng;
1413
}
1514

libs/s25main/ai/random.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,25 @@ T randomValue(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits
1717
return helpers::randomValue(getRandomGenerator(), min, max);
1818
}
1919

20+
// Return a random bool:
21+
// random() ... will return true|false with 50% chance each
22+
// random(15) ... will return true in 1/15 of the cases
23+
inline bool random(unsigned total = 1u)
24+
{
25+
return helpers::randomValue(getRandomGenerator(), 0u, total) == 0u;
26+
}
27+
28+
// random(20, 5) ... will return true in ~3/4 of the cases (random(20) > 5)
29+
inline bool random(unsigned total, unsigned chance)
30+
{
31+
return helpers::randomValue(getRandomGenerator(), 0u, total) > chance;
32+
}
33+
34+
template<typename ContainerT>
35+
inline unsigned randomIndex(const ContainerT& container)
36+
{
37+
RTTR_Assert(!container.empty());
38+
return helpers::randomValue(getRandomGenerator(), 0u, static_cast<unsigned>(container.size()) - 1u);
39+
}
40+
2041
} // namespace AI

0 commit comments

Comments
 (0)