]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/tool.cpp
Don't include client/game.h on server build
[dragonfireclient.git] / src / tool.cpp
index 6c4f63ee6055e854f97af92658dea6eea18fe224..075c6b3c5ff13f8bb5e5ada473d85d7c8e80f7ca 100644 (file)
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "inventory.h"
 #include "exceptions.h"
+#include "convert_json.h"
 #include "util/serialize.h"
 #include "util/numeric.h"
 
@@ -66,7 +67,7 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
        for (const auto &groupcap : groupcaps) {
                const std::string *name = &groupcap.first;
                const ToolGroupCap *cap = &groupcap.second;
-               os << serializeString(*name);
+               os << serializeString16(*name);
                writeS16(os, cap->uses);
                writeS16(os, cap->maxlevel);
                writeU32(os, cap->times.size());
@@ -79,7 +80,7 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
        writeU32(os, damageGroups.size());
 
        for (const auto &damageGroup : damageGroups) {
-               os << serializeString(damageGroup.first);
+               os << serializeString16(damageGroup.first);
                writeS16(os, damageGroup.second);
        }
 
@@ -98,12 +99,12 @@ void ToolCapabilities::deSerialize(std::istream &is)
        groupcaps.clear();
        u32 groupcaps_size = readU32(is);
        for (u32 i = 0; i < groupcaps_size; i++) {
-               std::string name = deSerializeString(is);
+               std::string name = deSerializeString16(is);
                ToolGroupCap cap;
                cap.uses = readS16(is);
                cap.maxlevel = readS16(is);
                u32 times_size = readU32(is);
-               for (u32 i = 0; i < times_size; i++) {
+               for(u32 i = 0; i < times_size; i++) {
                        int level = readS16(is);
                        float time = readF32(is);
                        cap.times[level] = time;
@@ -113,7 +114,7 @@ void ToolCapabilities::deSerialize(std::istream &is)
 
        u32 damage_groups_size = readU32(is);
        for (u32 i = 0; i < damage_groups_size; i++) {
-               std::string name = deSerializeString(is);
+               std::string name = deSerializeString16(is);
                s16 rating = readS16(is);
                damageGroups[name] = rating;
        }
@@ -142,7 +143,7 @@ void ToolCapabilities::serializeJson(std::ostream &os) const
        }
        root["damage_groups"] = damage_groups_object;
 
-       os << root;
+       fastWriteJson(root, os);
 }
 
 void ToolCapabilities::deserializeJson(std::istream &is)
@@ -176,14 +177,80 @@ void ToolCapabilities::deserializeJson(std::istream &is)
                                Json::Value &value = *dgiter;
                                if (value.isInt())
                                        damageGroups[dgiter.key().asString()] =
-                                                       value.asInt();
+                                               value.asInt();
                        }
                }
        }
 }
 
-DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp)
+static u32 calculateResultWear(const u32 uses, const u16 initial_wear)
 {
+       if (uses == 0) {
+               // Trivial case: Infinite uses
+               return 0;
+       }
+       /* Finite uses. This is not trivial,
+       as the maximum wear is not neatly evenly divisible by
+       most possible uses numbers. For example, for 128
+       uses, the calculation of wear is trivial, as
+       65536 / 128 uses = 512 wear,
+       so the tool will get 512 wear 128 times in its lifetime.
+       But for a number like 130, this does not work:
+       65536 / 130 uses = 504.123... wear.
+       Since wear must be an integer, we will get
+       504*130 = 65520, which would lead to the wrong number
+       of uses.
+
+       Instead, we partition the "wear range" into blocks:
+       A block represents a single use and can be
+       of two possible sizes: normal and oversized.
+       A normal block is equal to floor(65536 / uses).
+       An oversized block is a normal block plus 1.
+       Then we determine how many oversized and normal
+       blocks we need and finally, whether we add
+       the normal wear or the oversized wear.
+
+       Example for 130 uses:
+       * Normal wear = 504
+       * Number of normal blocks = 114
+       * Oversized wear = 505
+       * Number of oversized blocks = 16
+
+       If we add everything together, we get:
+         114*504 + 16*505 = 65536
+       */
+       u32 result_wear;
+       u32 wear_normal = ((U16_MAX+1) / uses);
+       // Will be non-zero if its not evenly divisible
+       u16 blocks_oversize = (U16_MAX+1) % uses;
+       // Whether to add one extra wear point in case
+       // of oversized wear.
+       u16 wear_extra = 0;
+       if (blocks_oversize > 0) {
+               u16 blocks_normal = uses - blocks_oversize;
+               /* When the wear has reached this value, we
+                  know that wear_normal has been applied
+                  for blocks_normal times, therefore,
+                  only oversized blocks remain.
+                  This also implies the raw tool wear number
+                  increases a bit faster after this point,
+                  but this should be barely noticable by the
+                  player.
+               */
+               u16 wear_extra_at = blocks_normal * wear_normal;
+               if (initial_wear >= wear_extra_at) {
+                       wear_extra = 1;
+               }
+       }
+       result_wear = wear_normal + wear_extra;
+       return result_wear;
+}
+
+DigParams getDigParams(const ItemGroupList &groups,
+               const ToolCapabilities *tp,
+               const u16 initial_wear)
+{
+
        // Group dig_immediate defaults to fixed time and no wear
        if (tp->groupcaps.find("dig_immediate") == tp->groupcaps.cend()) {
                switch (itemgroup_get(groups, "dig_immediate")) {
@@ -199,7 +266,7 @@ DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp)
        // Values to be returned (with a bit of conversion)
        bool result_diggable = false;
        float result_time = 0.0;
-       float result_wear = 0.0;
+       u32 result_wear = 0;
        std::string result_main_group;
 
        int level = itemgroup_get(groups, "level");
@@ -222,47 +289,57 @@ DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp)
                if (!result_diggable || time < result_time) {
                        result_time = time;
                        result_diggable = true;
-                       if (cap.uses != 0)
-                               result_wear = 1.0 / cap.uses / pow(3.0, leveldiff);
-                       else
-                               result_wear = 0;
+                       // The actual number of uses increases
+                       // exponentially with leveldiff.
+                       // If the levels are equal, real_uses equals cap.uses.
+                       u32 real_uses = cap.uses * pow(3.0, leveldiff);
+                       real_uses = MYMIN(real_uses, U16_MAX);
+                       result_wear = calculateResultWear(real_uses, initial_wear);
                        result_main_group = groupname;
                }
        }
 
-       u16 wear_i = U16_MAX * result_wear;
-       return DigParams(result_diggable, result_time, wear_i, result_main_group);
+       return DigParams(result_diggable, result_time, result_wear, result_main_group);
 }
 
-HitParams getHitParams(const ItemGroupList &armor_groups, const ToolCapabilities *tp,
-               float time_from_last_punch)
+HitParams getHitParams(const ItemGroupList &armor_groups,
+               const ToolCapabilities *tp, float time_from_last_punch,
+               u16 initial_wear)
 {
-       s16 damage = 0;
+       s32 damage = 0;
        float result_wear = 0.0f;
-       float punch_interval_multiplier = rangelim(
-                       time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
+       float punch_interval_multiplier =
+                       rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
 
        for (const auto &damageGroup : tp->damageGroups) {
                s16 armor = itemgroup_get(armor_groups, damageGroup.first);
                damage += damageGroup.second * punch_interval_multiplier * armor / 100.0;
        }
 
-       if (tp->punch_attack_uses > 0)
-               result_wear = 1.0f / tp->punch_attack_uses * punch_interval_multiplier;
+       if (tp->punch_attack_uses > 0) {
+               result_wear = calculateResultWear(tp->punch_attack_uses, initial_wear);
+               result_wear *= punch_interval_multiplier;
+       }
+       // Keep damage in sane bounds for simplicity
+       damage = rangelim(damage, -U16_MAX, U16_MAX);
 
-       u16 wear_i = U16_MAX * result_wear;
+       u32 wear_i = (u32) result_wear;
        return {damage, wear_i};
 }
 
-HitParams getHitParams(const ItemGroupList &armor_groups, const ToolCapabilities *tp)
+HitParams getHitParams(const ItemGroupList &armor_groups,
+               const ToolCapabilities *tp)
 {
        return getHitParams(armor_groups, tp, 1000000);
 }
 
-PunchDamageResult getPunchDamage(const ItemGroupList &armor_groups,
-               const ToolCapabilities *toolcap, const ItemStack *punchitem,
-               float time_from_last_punch)
-{
+PunchDamageResult getPunchDamage(
+               const ItemGroupList &armor_groups,
+               const ToolCapabilities *toolcap,
+               const ItemStack *punchitem,
+               float time_from_last_punch,
+               u16 initial_wear
+){
        bool do_hit = true;
        {
                if (do_hit && punchitem) {
@@ -272,15 +349,17 @@ PunchDamageResult getPunchDamage(const ItemGroupList &armor_groups,
                }
 
                if (do_hit) {
-                       if (itemgroup_get(armor_groups, "immortal"))
+                       if(itemgroup_get(armor_groups, "immortal"))
                                do_hit = false;
                }
        }
 
        PunchDamageResult result;
-       if (do_hit) {
-               HitParams hitparams =
-                               getHitParams(armor_groups, toolcap, time_from_last_punch);
+       if(do_hit)
+       {
+               HitParams hitparams = getHitParams(armor_groups, toolcap,
+                               time_from_last_punch,
+                               punchitem->wear);
                result.did_punch = true;
                result.wear = hitparams.wear;
                result.damage = hitparams.hp;
@@ -301,3 +380,4 @@ f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_h
 
        return max_d;
 }
+