1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00

Do not use floating point equality checks

This commit is contained in:
Ivan Savenko 2024-02-13 23:42:05 +02:00
parent 9ebd194ab1
commit 7359b66f99
10 changed files with 42 additions and 48 deletions

View File

@ -62,16 +62,12 @@ void DamageCache::buildDamageCache(std::shared_ptr<HypotheticBattle> hb, int sid
int64_t DamageCache::getDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)
{
auto damage = damageCache[attacker->unitId()][defender->unitId()] * attacker->getCount();
bool wasComputedBefore = damageCache[attacker->unitId()].count(defender->unitId());
if(damage == 0)
{
if (!wasComputedBefore)
cacheDamage(attacker, defender, hb);
damage = damageCache[attacker->unitId()][defender->unitId()] * attacker->getCount();
}
return static_cast<int64_t>(damage);
return damageCache[attacker->unitId()][defender->unitId()] * attacker->getCount();
}
int64_t DamageCache::getOriginalDamage(const battle::Unit * attacker, const battle::Unit * defender, std::shared_ptr<CBattleInfoCallback> hb)

View File

@ -270,7 +270,7 @@ EvaluationResult BattleExchangeEvaluator::findBestTarget(
{
float score = evaluateExchange(ap, 0, targets, damageCache, hb);
if(score > result.score || (score == result.score && result.wait))
if(score > result.score || (vstd::isAlmostEqual(score, result.score) && result.wait))
{
result.score = score;
result.bestAttack = ap;

View File

@ -632,7 +632,8 @@ void AIGateway::showBlockingDialog(const std::string & text, const std::vector<C
auto topObj = objects.front()->id == hero->id ? objects.back() : objects.front();
auto objType = topObj->ID; // top object should be our hero
auto goalObjectID = nullkiller->getTargetObject();
auto ratio = (float)nullkiller->dangerEvaluator->evaluateDanger(target, hero.get()) / (float)hero->getTotalStrength();
auto danger = nullkiller->dangerEvaluator->evaluateDanger(target, hero.get());
auto ratio = static_cast<float>(danger) / hero->getTotalStrength();
answer = topObj->id == goalObjectID; // no if we do not aim to visit this object
logAi->trace("Query hook: %s(%s) by %s danger ratio %f", target.toString(), topObj->getObjectName(), hero.name, ratio);
@ -648,7 +649,7 @@ void AIGateway::showBlockingDialog(const std::string & text, const std::vector<C
}
else if(objType == Obj::ARTIFACT || objType == Obj::RESOURCE)
{
bool dangerUnknown = ratio == 0;
bool dangerUnknown = danger == 0;
bool dangerTooHigh = ratio > (1 / SAFE_ATTACK_CONSTANT);
answer = !dangerUnknown && !dangerTooHigh;

View File

@ -226,7 +226,7 @@ void DangerHitMapAnalyzer::calculateTileOwners()
}
}
if(ourDistance == enemyDistance)
if(vstd::isAlmostEqual(ourDistance, enemyDistance))
{
hitMap[pos.x][pos.y][pos.z].closestTown = nullptr;
}

View File

@ -685,6 +685,19 @@ namespace vstd
return a + (b - a) * f;
}
template<typename Floating>
bool isAlmostZero(const Floating & value)
{
constexpr Floating epsilon(0.00001);
return std::abs(value) < epsilon;
}
template<typename Floating1, typename Floating2>
bool isAlmostEqual(const Floating1 & left, const Floating2 & right)
{
return isAlmostZero(left - right);
}
///compile-time version of std::abs for ints for int3, in clang++15 std::abs is constexpr
static constexpr int abs(int i) {
if(i < 0) return -i;

View File

@ -141,7 +141,7 @@ void MapViewCache::update(const std::shared_ptr<IMapRendererContext> & context)
void MapViewCache::render(const std::shared_ptr<IMapRendererContext> & context, Canvas & target, bool fullRedraw)
{
bool mapMoved = (cachedPosition != model->getMapViewCenter());
bool lazyUpdate = !mapMoved && !fullRedraw && context->viewTransitionProgress() == 0;
bool lazyUpdate = !mapMoved && !fullRedraw && vstd::isAlmostZero(context->viewTransitionProgress());
Rect dimensions = model->getTilesTotalRect();
@ -184,7 +184,7 @@ void MapViewCache::render(const std::shared_ptr<IMapRendererContext> & context,
}
}
if(context->viewTransitionProgress() != 0)
if(!vstd::isAlmostZero(context->viewTransitionProgress()))
target.drawTransparent(*terrainTransition, Point(0, 0), 1.0 - context->viewTransitionProgress());
cachedPosition = model->getMapViewCenter();

View File

@ -41,10 +41,10 @@ bool ColorFilter::operator != (const ColorFilter & other) const
bool ColorFilter::operator == (const ColorFilter & other) const
{
return
r.r == other.r.r && r.g && other.r.g && r.b == other.r.b && r.a == other.r.a &&
g.r == other.g.r && g.g && other.g.g && g.b == other.g.b && g.a == other.g.a &&
b.r == other.b.r && b.g && other.b.g && b.b == other.b.b && b.a == other.b.a &&
a == other.a;
vstd::isAlmostEqual(r.r, other.r.r) && vstd::isAlmostEqual(r.g, other.r.g) && vstd::isAlmostEqual(r.b, other.r.b) && vstd::isAlmostEqual(r.a, other.r.a) &&
vstd::isAlmostEqual(g.r, other.g.r) && vstd::isAlmostEqual(g.g, other.g.g) && vstd::isAlmostEqual(g.b, other.g.b) && vstd::isAlmostEqual(g.a, other.g.a) &&
vstd::isAlmostEqual(b.r, other.b.r) && vstd::isAlmostEqual(b.g, other.b.g) && vstd::isAlmostEqual(b.b, other.b.b) && vstd::isAlmostEqual(b.a, other.b.a) &&
vstd::isAlmostEqual(a, other.a);
}
ColorFilter ColorFilter::genEmptyShifter( )

View File

@ -21,23 +21,24 @@
void CSlider::mouseDragged(const Point & cursorPosition, const Point & lastUpdateDistance)
{
double v = 0;
double newPosition = 0;
if(getOrientation() == Orientation::HORIZONTAL)
{
v = cursorPosition.x - pos.x - 24;
v *= positions;
v /= (pos.w - 48);
newPosition = cursorPosition.x - pos.x - 24;
newPosition *= positions;
newPosition /= (pos.w - 48);
}
else
{
v = cursorPosition.y - pos.y - 24;
v *= positions;
v /= (pos.h - 48);
newPosition = cursorPosition.y - pos.y - 24;
newPosition *= positions;
newPosition /= (pos.h - 48);
}
v += 0.5;
if(v!=value)
int positionInteger = std::round(newPosition);
if(positionInteger != value)
{
scrollTo(static_cast<int>(v));
scrollTo(static_cast<int>(newPosition));
}
}

View File

@ -844,8 +844,8 @@ void CCreatureHandler::loadUnitAnimInfo(JsonNode & graphics, CLegacyConfigParser
missile["attackClimaxFrame"].Float() = parser.readNumber();
// assume that creature is not a shooter and should not have whole missile field
if (missile["frameAngles"].Vector()[0].Float() == 0 &&
missile["attackClimaxFrame"].Float() == 0)
if (missile["frameAngles"].Vector()[0].Integer() == 0 &&
missile["attackClimaxFrame"].Integer() == 0)
graphics.Struct().erase("missile");
}
@ -987,10 +987,10 @@ void CCreatureHandler::loadStackExperience(CCreature * creature, const JsonNode
int lastVal = 0;
for (const JsonNode &val : values)
{
if (val.Float() != lastVal)
if (val.Integer() != lastVal)
{
JsonNode bonusInput = exp["bonus"];
bonusInput["val"].Float() = static_cast<int>(val.Float()) - lastVal;
bonusInput["val"].Float() = val.Integer() - lastVal;
auto bonus = JsonUtils::parseBonus (bonusInput);
bonus->source = BonusSource::STACK_EXPERIENCE;

View File

@ -113,23 +113,6 @@ public:
return *this;
}
bool operator==(const float3 & i) const { return (x == i.x) && (y == i.y) && (z == i.z); }
bool operator!=(const float3 & i) const { return (x != i.x) || (y != i.y) || (z != i.z); }
bool operator<(const float3 & i) const
{
if (z<i.z)
return true;
if (z>i.z)
return false;
if (y<i.y)
return true;
if (y>i.y)
return false;
return x<i.x;
}
std::string toString() const
{
return "(" + std::to_string(x) +