Skip to content

Commit 33948c4

Browse files
wichernFlamefire
authored andcommitted
Fix off by one error
1 parent 147d082 commit 33948c4

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

libs/s25main/ai/aijh/AIConstruction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
467467

468468
const Inventory& inventory = aii.GetInventory();
469469
uint8_t playerId = aii.GetPlayerId();
470-
if((AI::randomValue<int>(0, 3) == 0 || inventory.people[Job::Private] < 15)
470+
if((AI::randomValue<int>(0, 2) == 0 || inventory.people[Job::Private] < 15)
471471
&& (inventory.goods[GoodType::Stones] > 6 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0))
472472
bld = BuildingType::Guardhouse;
473-
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && AI::randomValue<int>(0, 10) != 0
473+
if(aijh.getAIInterface().isHarborPosClose(pt, 19) && AI::randomValue<int>(0, 9) != 0
474474
&& aijh.ggs.isEnabled(AddonId::SEA_ATTACK))
475475
{
476476
if(aii.CanBuildBuildingtype(BuildingType::Watchtower))
@@ -481,7 +481,7 @@ helpers::OptionalEnum<BuildingType> AIConstruction::ChooseMilitaryBuilding(const
481481
{
482482
if(aijh.UpdateUpgradeBuilding() < 0 && bldPlanner.GetNumBuildingSites(biggestBld) < 1
483483
&& (inventory.goods[GoodType::Stones] > 20 || bldPlanner.GetNumBuildings(BuildingType::Quarry) > 0)
484-
&& AI::randomValue<int>(0, 10) != 0)
484+
&& AI::randomValue<int>(0, 9) != 0)
485485
{
486486
return biggestBld;
487487
}

libs/s25main/ai/aijh/AIPlayerJH.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ 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());
347+
int randomStore = AI::randomValue<int>(0, storehouses.size() - 1);
348348
auto it = storehouses.begin();
349349
std::advance(it, randomStore);
350350
const MapPoint whPos = (*it)->GetPos();
@@ -365,7 +365,7 @@ void AIPlayerJH::PlanNewBuildings(const unsigned gf)
365365
const std::list<nobMilitary*>& militaryBuildings = aii.GetMilitaryBuildings();
366366
if(militaryBuildings.empty())
367367
return;
368-
int randomMiliBld = AI::randomValue<int>(0, militaryBuildings.size());
368+
int randomMiliBld = AI::randomValue<int>(0, militaryBuildings.size() - 1);
369369
auto it2 = militaryBuildings.begin();
370370
std::advance(it2, randomMiliBld);
371371
MapPoint bldPos = (*it2)->GetPos();
@@ -1209,7 +1209,7 @@ void AIPlayerJH::HandleExpedition(const noShip* ship)
12091209
aii.FoundColony(ship);
12101210
else
12111211
{
1212-
const unsigned offset = AI::randomValue<unsigned>(0, helpers::MaxEnumValue_v<ShipDirection>);
1212+
const unsigned offset = AI::randomValue<unsigned>(0, helpers::MaxEnumValue_v<ShipDirection> - 1);
12131213
for(auto dir : helpers::EnumRange<ShipDirection>{})
12141214
{
12151215
dir = ShipDirection((rttr::enum_cast(dir) + offset) % helpers::MaxEnumValue_v<ShipDirection>);
@@ -1254,7 +1254,7 @@ void AIPlayerJH::HandleTreeChopped(const MapPoint pt)
12541254

12551255
UpdateNodesAround(pt, 3);
12561256

1257-
if(AI::randomValue<int>(0, 2) == 0)
1257+
if(AI::randomValue<int>(0, 1) == 0)
12581258
AddMilitaryBuildJob(pt);
12591259
else // if (random % 12 == 0)
12601260
AddBuildJob(BuildingType::Woodcutter, pt);
@@ -1536,7 +1536,7 @@ void AIPlayerJH::TryToAttack()
15361536
// We skip the current building with a probability of limit/numMilBlds
15371537
// -> For twice the number of blds as the limit we will most likely skip every 2nd building
15381538
// 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) > limit)
1539+
if(AI::randomValue<unsigned>(0, numMilBlds - 1) > limit)
15401540
continue;
15411541

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

15701570
// shuffle everything but headquarters and harbors without any troops in them
15711571
std::shuffle(potentialTargets.begin() + hq_or_harbor_without_soldiers, potentialTargets.end(),
1572-
std::mt19937(AI::randomValue<unsigned>(0, 2048)));
1572+
std::mt19937(AI::randomValue<unsigned>(0, 2047)));
15731573

15741574
// check for each potential attacking target the number of available attacking soldiers
15751575
for(const nobBaseMilitary* target : potentialTargets)
@@ -1703,7 +1703,7 @@ void AIPlayerJH::TrySeaAttack()
17031703
unsigned skip = 0;
17041704
if(searcharoundharborspots.size() > 15)
17051705
skip =
1706-
std::max<int>(AI::randomValue<int>(0, static_cast<int>(searcharoundharborspots.size() / 15 + 1) * 15), 1) - 1;
1706+
std::max<int>(AI::randomValue<int>(0, static_cast<int>(searcharoundharborspots.size() / 15) * 15), 1) - 1;
17071707
for(unsigned i = skip; i < searcharoundharborspots.size() && limit > 0; i++)
17081708
{
17091709
limit--;

0 commit comments

Comments
 (0)