Browse Source

Fix potential edge cases in TIMES_STACK_SIZE updater

Ivan Savenko 5 months ago
parent
commit
2d24c28996

+ 1 - 1
config/schemas/bonusInstance.json

@@ -43,7 +43,7 @@
 			"anyOf" : [
 				{
 					"type" : "string",
-					"enum" : [ "TIMES_HERO_LEVEL", "TIMES_STACK_LEVEL", "DIVIDE_STACK_LEVEL", "BONUS_OWNER_UPDATER", "TIMES_HERO_LEVEL_DIVIDE_STACK_LEVEL" ]
+					"enum" : [ "TIMES_HERO_LEVEL", "TIMES_STACK_LEVEL", "DIVIDE_STACK_LEVEL", "BONUS_OWNER_UPDATER", "TIMES_STACK_SIZE", "TIMES_HERO_LEVEL_DIVIDE_STACK_LEVEL" ]
 				},
 				{
 					"description" : "GROWS_WITH_LEVEL updater",

+ 12 - 2
docs/modders/Bonus/Bonus_Updaters.md

@@ -84,15 +84,25 @@ Usage:
 
 Effect: Updates val to `val = clamp(val * floor(stackSize / stepSize), minimum, maximum)`, where stackSize is total number of creatures in current unit stack
 
-Parameters `minimum` and `maximum` are optional and can be dropped if not needed
+Example of short form with default parameters:
 
-Example:
+```json
+"updater" : "TIMES_STACK_SIZE"
+```
+
+Example of long form with custom parameters:
 
 ```json
 "updater" : {
     "type" : "TIMES_STACK_SIZE",
+    
+    // Optional, by default - unlimited
     "minimum" : 0,
+    
+    // Optional, by default - unlimited
     "maximum" : 100,
+    
+    // Optional, by default - 1
     "stepSize" : 2
 }
 ```

+ 0 - 1
lib/bonuses/Updaters.cpp

@@ -119,7 +119,6 @@ std::shared_ptr<Bonus> TimesStackSizeUpdater::apply(const std::shared_ptr<Bonus>
 {
 	auto newBonus = std::make_shared<Bonus>(*b);
 	newBonus->val *= std::clamp(count / stepSize, minimum, maximum);
-	newBonus->updater = nullptr; // prevent double-apply
 	return newBonus;
 }
 

+ 3 - 3
lib/bonuses/Updaters.h

@@ -89,9 +89,9 @@ class DLL_LINKAGE TimesStackSizeUpdater : public IUpdater
 {
 	std::shared_ptr<Bonus> apply(const std::shared_ptr<Bonus> & b, int count) const;
 
-	int minimum;
-	int maximum;
-	int stepSize;
+	int minimum = std::numeric_limits<int>::min();
+	int maximum = std::numeric_limits<int>::max();
+	int stepSize = 1;
 public:
 	TimesStackSizeUpdater() = default;
 	TimesStackSizeUpdater(int minimum, int maximum, int stepSize)

+ 1 - 0
lib/json/JsonBonus.cpp

@@ -386,6 +386,7 @@ static TUpdaterPtr parseUpdater(const JsonNode & updaterJson)
 			{"TIMES_HERO_LEVEL_DIVIDE_STACK_LEVEL", std::make_shared<TimesHeroLevelDivideStackLevelUpdater>()},
 			{"DIVIDE_STACK_LEVEL", std::make_shared<DivideStackLevelUpdater>()},
 			{"TIMES_STACK_LEVEL", std::make_shared<TimesStackLevelUpdater>()},
+			{"TIMES_STACK_SIZE", std::make_shared<TimesStackSizeUpdater>()},
 			{"BONUS_OWNER_UPDATER", std::make_shared<OwnerUpdater>()}
 	};