summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-02-05 02:05:34 +0100
committerAki <please@ignore.pl>2023-02-05 02:05:34 +0100
commita0699b83b0abc7148fd9a180178f82faa6e36788 (patch)
treee38edd407cd8a7a6c1190e397c176e58e1e4d0a8
parent7c8aac19374013e7366b5afbf5d85fe151de3669 (diff)
downloadkurator-a0699b83b0abc7148fd9a180178f82faa6e36788.zip
kurator-a0699b83b0abc7148fd9a180178f82faa6e36788.tar.gz
kurator-a0699b83b0abc7148fd9a180178f82faa6e36788.tar.bz2
Fixed boundary damage dealing and resulting side effect in totals
-rw-r--r--sim/src/HitPoints.cpp11
-rw-r--r--sim/tests/HitPoints.cpp6
2 files changed, 10 insertions, 7 deletions
diff --git a/sim/src/HitPoints.cpp b/sim/src/HitPoints.cpp
index f850451..f23cafd 100644
--- a/sim/src/HitPoints.cpp
+++ b/sim/src/HitPoints.cpp
@@ -1,5 +1,7 @@
#include <kurator/sim/HitPoints.h>
+#include <algorithm>
+
#include <kurator/universe/ShipType.h>
@@ -23,11 +25,12 @@ HitPoints::Layer::consume(double& damage)
if (damage <= 0.0 || points <= 0.0)
return 0.0;
const double modifier = 1.0 - resists;
- const double actual_damage = damage * modifier;
- const double left_to_deal = (actual_damage - points) / modifier;
- points -= actual_damage;
+ const double potential_damage = damage * modifier;
+ const double taken_damage = std::min(potential_damage, points);
+ const double left_to_deal = (potential_damage - taken_damage) / modifier;
+ points -= taken_damage;
damage = left_to_deal;
- return actual_damage;
+ return taken_damage;
}
diff --git a/sim/tests/HitPoints.cpp b/sim/tests/HitPoints.cpp
index 8b8ef84..5c90b4f 100644
--- a/sim/tests/HitPoints.cpp
+++ b/sim/tests/HitPoints.cpp
@@ -30,9 +30,9 @@ TEST(HitPoints, Deal)
EXPECT_DOUBLE_EQ(300.0, points.total());
EXPECT_DOUBLE_EQ(50.0, points.deal(100.0));
EXPECT_DOUBLE_EQ(250.0, points.total());
- EXPECT_DOUBLE_EQ(50.0, points.deal(100.0));
- EXPECT_DOUBLE_EQ(200.0, points.total());
- EXPECT_DOUBLE_EQ(100.0, points.deal(125.0));
+ EXPECT_DOUBLE_EQ(50.0 + 80.0, points.deal(200.0));
+ EXPECT_DOUBLE_EQ(120.0, points.total());
+ EXPECT_DOUBLE_EQ(20.0, points.deal(25.0));
EXPECT_DOUBLE_EQ(100.0, points.total());
EXPECT_DOUBLE_EQ(100.0, points.deal(200.0));
EXPECT_DOUBLE_EQ(0.0, points.total());