Răsfoiți Sursa

BETA: Moving `HtmlUtil` from '''com.l2jserver.util''' to '''com.l2jserver.gameserver.util'''
* Also adding auto scalable Gauge using getWeightGauge's levels between 1 and 5.
* Also added few new util methods:
* map - Re-Maps a value from one range to another.
* constrain - Constrains a number to be within a range.
* Thanks to: Arduino Project
* Reviewed by: Zoey76, Nos

Rumen Nikiforov 11 ani în urmă
părinte
comite
ad50d9577d

+ 8 - 8
L2J_Server_BETA/java/com/l2jserver/gameserver/model/stats/Formulas.java

@@ -1370,7 +1370,7 @@ public final class Formulas
 		}
 		
 		final double rate = baseMod * elementMod * traitMod * mAtkMod * buffDebuffMod;
-		final double finalRate = traitMod > 0 ? Math.min(Math.max(rate, skill.getMinChance()), skill.getMaxChance()) : 0;
+		final double finalRate = traitMod > 0 ? Util.constrain(rate, skill.getMinChance(), skill.getMaxChance()) : 0;
 		
 		if (attacker.isDebug())
 		{
@@ -1439,7 +1439,7 @@ public final class Formulas
 		rate *= elementMod;
 		
 		// Add Matk/Mdef Bonus (TODO: Pending)
-		double finalRate = Math.min(Math.max(rate, skill.getMinChance()), skill.getMaxChance());
+		final double finalRate = Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
 		if (attacker.isDebug())
 		{
 			final StatsSet set = new StatsSet();
@@ -1502,7 +1502,7 @@ public final class Formulas
 		// Add Matk/Mdef Bonus (TODO: Pending)
 		
 		// Check the Rate Limits.
-		double finalRate = Math.min(Math.max(rate, skill.getMinChance()), skill.getMaxChance());
+		final double finalRate = Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
 		
 		if (attacker.getOwner().isDebug())
 		{
@@ -1862,7 +1862,7 @@ public final class Formulas
 		
 		double attribute_mod_diff = attack_attribute_mod - defence_attribute_mod;
 		
-		attribute_mod_diff = Math.min(Math.max(attribute_mod_diff, min), max);
+		attribute_mod_diff = Util.constrain(attribute_mod_diff, min, max);
 		
 		double result = (attribute_mod_diff / 100.0) + 1;
 		
@@ -2049,7 +2049,7 @@ public final class Formulas
 	{
 		// Lvl Bonus Modifier.
 		rate *= info.getSkill().getMagicLevel() > 0 ? 1 + ((cancelMagicLvl - info.getSkill().getMagicLevel()) / 100.) : 1;
-		return Rnd.get(100) < Math.min(Math.max(rate, skill.getMinChance()), skill.getMaxChance());
+		return Rnd.get(100) < Util.constrain(rate, skill.getMinChance(), skill.getMaxChance());
 	}
 	
 	/**
@@ -2085,7 +2085,7 @@ public final class Formulas
 			double resMod = calcGeneralTraitBonus(caster, target, skill.getTraitType(), false);
 			double lvlBonusMod = calcLvlBonusMod(caster, target, skill);
 			double elementMod = calcAttributeBonus(caster, target, skill);
-			time = (int) Math.ceil(Math.min(Math.max((time * resMod * lvlBonusMod * elementMod) / statMod, time * 0.5), time));
+			time = (int) Math.ceil(Util.constrain(((time * resMod * lvlBonusMod * elementMod) / statMod), (time * 0.5), time));
 		}
 		return time;
 	}
@@ -2192,7 +2192,7 @@ public final class Formulas
 		}
 		
 		final double result = (attacker.getStat().getAttackTrait(traitType) - target.getStat().getDefenceTrait(traitType)) + 1.0;
-		return Math.min(Math.max(result, 0.05), 2.0);
+		return Util.constrain(result, 0.05, 2.0);
 	}
 	
 	public static double calcWeaponTraitBonus(L2Character attacker, L2Character target)
@@ -2223,6 +2223,6 @@ public final class Formulas
 			}
 		}
 		
-		return Math.min(Math.max(weaponTraitBonus * weaknessBonus, 0.05), 2.0);
+		return Util.constrain((weaponTraitBonus * weaknessBonus), 0.05, 2.0);
 	}
 }

+ 17 - 4
L2J_Server_BETA/java/com/l2jserver/util/HtmlUtil.java → L2J_Server_BETA/java/com/l2jserver/gameserver/util/HtmlUtil.java

@@ -16,7 +16,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-package com.l2jserver.util;
+package com.l2jserver.gameserver.util;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -121,18 +121,31 @@ public class HtmlUtil
 		return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_Food_Bg_Center", "L2UI_CT1.Gauges.Gauge_DF_Large_Food_Center", 17, -13);
 	}
 	
+	/**
+	 * Gets the HTML representation of Weight gauge automatically changing level depending on current/max.
+	 * @param width the width
+	 * @param current the current value
+	 * @param max the max value
+	 * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
+	 * @return the HTML
+	 */
+	public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage)
+	{
+		return getWeightGauge(width, current, max, displayAsPercentage, Util.map(current, 0, max, 1, 5));
+	}
+	
 	/**
 	 * Gets the HTML representation of Weight gauge.
 	 * @param width the width
 	 * @param current the current value
 	 * @param max the max value
 	 * @param displayAsPercentage if {@code true} the text in middle will be displayed as percent else it will be displayed as "current / max"
-	 * @param type a number from 1 to 5 for the 5 different colors of weight gauge
+	 * @param level a number from 1 to 5 for the 5 different colors of weight gauge
 	 * @return the HTML
 	 */
-	public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage, long type)
+	public static String getWeightGauge(int width, long current, long max, boolean displayAsPercentage, long level)
 	{
-		return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_bg_Center" + type, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_Center" + type, 17, -13);
+		return getGauge(width, current, max, displayAsPercentage, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_bg_Center" + level, "L2UI_CT1.Gauges.Gauge_DF_Large_Weight_Center" + level, 17, -13);
 	}
 	
 	/**

+ 78 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/util/Util.java

@@ -887,4 +887,82 @@ public final class Util
 		}
 		return index;
 	}
+	
+	/**
+	 * Re-Maps a value from one range to another.
+	 * @param input
+	 * @param inputMin
+	 * @param inputMax
+	 * @param outputMin
+	 * @param outputMax
+	 * @return The mapped value
+	 */
+	public static int map(int input, int inputMin, int inputMax, int outputMin, int outputMax)
+	{
+		return (((input - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin;
+	}
+	
+	/**
+	 * Re-Maps a value from one range to another.
+	 * @param input
+	 * @param inputMin
+	 * @param inputMax
+	 * @param outputMin
+	 * @param outputMax
+	 * @return The mapped value
+	 */
+	public static long map(long input, long inputMin, long inputMax, long outputMin, long outputMax)
+	{
+		return (((input - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin;
+	}
+	
+	/**
+	 * Re-Maps a value from one range to another.
+	 * @param input
+	 * @param inputMin
+	 * @param inputMax
+	 * @param outputMin
+	 * @param outputMax
+	 * @return The mapped value
+	 */
+	public static double map(double input, double inputMin, double inputMax, double outputMin, double outputMax)
+	{
+		return (((input - inputMin) * (outputMax - outputMin)) / (inputMax - inputMin)) + outputMin;
+	}
+	
+	/**
+	 * Constrains a number to be within a range.
+	 * @param input the number to constrain, all data types
+	 * @param min the lower end of the range, all data types
+	 * @param max the upper end of the range, all data types
+	 * @return input: if input is between min and max, min: if input is less than min, max: if input is greater than max
+	 */
+	public static long constrain(int input, int min, int max)
+	{
+		return (input < min) ? min : (input > max) ? max : input;
+	}
+	
+	/**
+	 * Constrains a number to be within a range.
+	 * @param input the number to constrain, all data types
+	 * @param min the lower end of the range, all data types
+	 * @param max the upper end of the range, all data types
+	 * @return input: if input is between min and max, min: if input is less than min, max: if input is greater than max
+	 */
+	public static long constrain(long input, long min, long max)
+	{
+		return (input < min) ? min : (input > max) ? max : input;
+	}
+	
+	/**
+	 * Constrains a number to be within a range.
+	 * @param input the number to constrain, all data types
+	 * @param min the lower end of the range, all data types
+	 * @param max the upper end of the range, all data types
+	 * @return input: if input is between min and max, min: if input is less than min, max: if input is greater than max
+	 */
+	public static double constrain(double input, double min, double max)
+	{
+		return (input < min) ? min : (input > max) ? max : input;
+	}
 }