Skip to content

Commit c2c303a

Browse files
wichernFlamefire
authored andcommitted
Fix review findings
1 parent 6da2a5b commit c2c303a

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

libs/s25main/ai/aijh/AIConstruction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
466466
const BuildingType biggestBld = GetBiggestAllowedMilBuilding().value();
467467

468468
const Inventory& inventory = aii.GetInventory();
469-
if((inventory.people[Job::Private] < 15 || AI::random())
469+
if((inventory.people[Job::Private] < 15 || AI::random(3))
470470
&& (inventory.goods[GoodType::Stones] > 6 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0))
471471
bld = BuildingType::Guardhouse;
472472
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && !AI::random(9) && aijh.ggs.isEnabled(AddonId::SEA_ATTACK))

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 4 additions & 5 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-
auto it = storehouses.begin();
348-
std::advance(it, AI::randomIndex(storehouses));
349-
const MapPoint whPos = (*it)->GetPos();
347+
const auto* storehouse = AI::randomElement(storehouses);
348+
const MapPoint whPos = storehouse->GetPos();
350349
UpdateNodesAround(whPos, 15); // update the area we want to build in first
351350
for(const BuildingType i : bldToTest)
352351
{
@@ -1532,10 +1531,10 @@ void AIPlayerJH::TryToAttack()
15321531
constexpr unsigned limit = 40;
15331532
for(const nobMilitary* milBld : militaryBuildings)
15341533
{
1535-
// We skip the current building with a probability of limit/numMilBlds
1534+
// We handle the current building with a probability of limit/numMilBlds
15361535
// -> For twice the number of blds as the limit we will most likely skip every 2nd building
15371536
// This way we check roughly (at most) limit buildings but avoid any preference for one building over an other
1538-
if(AI::random(numMilBlds - 1u, limit))
1537+
if(!AI::random(numMilBlds, limit))
15391538
continue;
15401539

15411540
if(milBld->GetFrontierDistance() == FrontierDistance::Far) // inland building? -> skip it

libs/s25main/ai/random.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace AI {
1111

1212
std::minstd_rand& getRandomGenerator();
1313

14+
// Return a random value (min and max are included)
1415
template<typename T>
1516
T randomValue(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max())
1617
{
@@ -20,22 +21,28 @@ T randomValue(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits
2021
// Return a random bool:
2122
// random() ... will return true|false with 50% chance each
2223
// random(15) ... will return true in 1/15 of the cases
23-
inline bool random(unsigned total = 1u)
24+
// random(20, 5) ... will return true in 5 out of 20 cases, i.e. a probability of 25%. Sames as random(4, 1)
25+
inline bool random(unsigned total = 2u, unsigned chance = 1u)
2426
{
25-
return helpers::randomValue(getRandomGenerator(), 0u, total) == 0u;
27+
RTTR_Assert(total > 0u);
28+
return (chance >= total) || randomValue(1u, total) <= chance;
2629
}
2730

28-
// random(20, 5) ... will return true in ~3/4 of the cases (random(20) > 5)
29-
inline bool random(unsigned total, unsigned chance)
31+
template<typename ContainerT>
32+
inline unsigned randomIndex(const ContainerT& container)
3033
{
31-
return helpers::randomValue(getRandomGenerator(), 0u, total) > chance;
34+
RTTR_Assert(!container.empty());
35+
return randomValue(0u, static_cast<unsigned>(container.size()) - 1u);
3236
}
3337

3438
template<typename ContainerT>
35-
inline unsigned randomIndex(const ContainerT& container)
39+
inline auto randomElement(const ContainerT& container)
3640
{
3741
RTTR_Assert(!container.empty());
38-
return helpers::randomValue(getRandomGenerator(), 0u, static_cast<unsigned>(container.size()) - 1u);
42+
auto it = container.begin();
43+
if(container.size() > 1u)
44+
std::advance(it, randomIndex(container));
45+
return *it;
3946
}
4047

4148
} // namespace AI

0 commit comments

Comments
 (0)