mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-13 19:54:17 +02:00
Use binary search for roulette wheel. TODO: implement in other places.
This commit is contained in:
@@ -2217,7 +2217,7 @@ ObjectInfo CRmgTemplateZone::getRandomObject(CMapGenerator* gen, CTreasurePileIn
|
|||||||
{
|
{
|
||||||
//int objectsVisitableFromBottom = 0; //for debug
|
//int objectsVisitableFromBottom = 0; //for debug
|
||||||
|
|
||||||
std::vector<std::pair<ui32, ObjectInfo>> tresholds;
|
std::vector<std::pair<ui32, ObjectInfo>> thresholds;
|
||||||
ui32 total = 0;
|
ui32 total = 0;
|
||||||
|
|
||||||
//calculate actual treasure value range based on remaining value
|
//calculate actual treasure value range based on remaining value
|
||||||
@@ -2320,13 +2320,13 @@ ObjectInfo CRmgTemplateZone::getRandomObject(CMapGenerator* gen, CTreasurePileIn
|
|||||||
|
|
||||||
total += oi.probability;
|
total += oi.probability;
|
||||||
//assert (oi.value > 0);
|
//assert (oi.value > 0);
|
||||||
tresholds.push_back (std::make_pair (total, oi));
|
thresholds.push_back (std::make_pair (total, oi));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//logGlobal->infoStream() << boost::format ("Number of objects visitable from bottom: %d") % objectsVisitableFromBottom;
|
//logGlobal->infoStream() << boost::format ("Number of objects visitable from bottom: %d") % objectsVisitableFromBottom;
|
||||||
|
|
||||||
if (tresholds.empty())
|
if (thresholds.empty())
|
||||||
{
|
{
|
||||||
ObjectInfo oi;
|
ObjectInfo oi;
|
||||||
//Generate pandora Box with gold if the value is extremely high
|
//Generate pandora Box with gold if the value is extremely high
|
||||||
@@ -2359,12 +2359,13 @@ ObjectInfo CRmgTemplateZone::getRandomObject(CMapGenerator* gen, CTreasurePileIn
|
|||||||
{
|
{
|
||||||
int r = gen->rand.nextInt (1, total);
|
int r = gen->rand.nextInt (1, total);
|
||||||
|
|
||||||
for (auto t : tresholds)
|
//binary search = fastest
|
||||||
|
auto it = std::lower_bound(thresholds.begin(), thresholds.end(), r,
|
||||||
|
[](const std::pair<ui32, ObjectInfo> &rhs, const int lhs)->bool
|
||||||
{
|
{
|
||||||
if (r <= t.first)
|
return rhs.first < lhs;
|
||||||
return t.second;
|
});
|
||||||
}
|
return it->second;
|
||||||
assert (0); //we should never be here
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ObjectInfo(); // unreachable
|
return ObjectInfo(); // unreachable
|
||||||
|
Reference in New Issue
Block a user