From 8fb6bb5fa576c7c7bb124ff2da5cba971d60aaba Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sun, 30 Aug 2020 15:51:44 -0700 Subject: [PATCH] Added support for assigning float to stat Created Stat#encodeFloat(float,int) to encode floats into fixed point int Added maxhp stat to monster attributes Changed debugging to output float stat --- core/src/com/riiablo/engine/server/Actioneer.java | 2 +- .../com/riiablo/engine/server/ServerEntityFactory.java | 6 +++++- core/src/com/riiablo/item/Stat.java | 8 ++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/core/src/com/riiablo/engine/server/Actioneer.java b/core/src/com/riiablo/engine/server/Actioneer.java index 148e39c8..9c70217c 100644 --- a/core/src/com/riiablo/engine/server/Actioneer.java +++ b/core/src/com/riiablo/engine/server/Actioneer.java @@ -123,7 +123,7 @@ public class Actioneer extends PassiveSystem { log.debug("{} attack {}", entityId, targetId); Attributes attrs = mAttributesWrapper.get(targetId).attrs; - log.debug("{} {}", targetId, attrs.get(Stat.hitpoints)); + log.debug("{} {}", targetId, attrs.get(Stat.hitpoints).toFloat()); break; case 27: // teleport mPosition.get(entityId).position.set(targetVec); diff --git a/core/src/com/riiablo/engine/server/ServerEntityFactory.java b/core/src/com/riiablo/engine/server/ServerEntityFactory.java index e4b83071..866bc1c1 100644 --- a/core/src/com/riiablo/engine/server/ServerEntityFactory.java +++ b/core/src/com/riiablo/engine/server/ServerEntityFactory.java @@ -157,7 +157,11 @@ public class ServerEntityFactory extends EntityFactory { Attributes attrs = new Attributes(); PropertyList base = attrs.base(); base.clear(); - base.put(Stat.hitpoints, MathUtils.random(monstats.minHP[0], monstats.maxHP[0])); + final float hitpoints = MathUtils.random(monstats.minHP[0], monstats.maxHP[0]); + // FIXME: this can be more elegant -- add support for putting float or auto converting? + final int encodedHitpoints = Stat.encodeFloat(hitpoints, 8); // see above note, this is crude + base.put(Stat.hitpoints, encodedHitpoints); + base.put(Stat.maxhp, encodedHitpoints); attrs.reset(); // propagate base changes mAttributesWrapper.create(id).attrs = attrs; diff --git a/core/src/com/riiablo/item/Stat.java b/core/src/com/riiablo/item/Stat.java index a2cc8c7b..d0ef7704 100644 --- a/core/src/com/riiablo/item/Stat.java +++ b/core/src/com/riiablo/item/Stat.java @@ -820,6 +820,14 @@ public class Stat implements Comparable { return ((val >>> shift) + ((val & mask) / (float) pow)); } + public void setFloat(float value) { + this.val = encodeFloat(value, entry.ValShift); + } + + public static int encodeFloat(float value, int precision) { + return (int) (value * (1 << precision)); + } + public long toLong() { return UnsignedInts.toLong(val); }