Tool.cpp/.h, lua_api/l_util.cpp: Tidy up code and remove dead code
authorHybrid Dog <ovvv@web.de>
Mon, 10 Apr 2017 18:03:53 +0000 (20:03 +0200)
committerparamat <mat.gregory@virginmedia.com>
Wed, 3 Jan 2018 04:01:15 +0000 (04:01 +0000)
src/script/lua_api/l_util.cpp
src/tool.cpp
src/tool.h

index 839c4be1189e061e33bba615c521fe6b373db588..501261dd7cca894079a7148c0fea22ab5a4f4bca 100644 (file)
@@ -158,18 +158,14 @@ int ModApiUtil::l_write_json(lua_State *L)
        return 1;
 }
 
-// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
+// get_dig_params(groups, tool_capabilities)
 int ModApiUtil::l_get_dig_params(lua_State *L)
 {
        NO_MAP_LOCK_REQUIRED;
        ItemGroupList groups;
        read_groups(L, 1, groups);
        ToolCapabilities tp = read_tool_capabilities(L, 2);
-       if(lua_isnoneornil(L, 3))
-               push_dig_params(L, getDigParams(groups, &tp));
-       else
-               push_dig_params(L, getDigParams(groups, &tp,
-                                       luaL_checknumber(L, 3)));
+       push_dig_params(L, getDigParams(groups, &tp));
        return 1;
 }
 
index 7128f191575eff130b060c5e8dc538ef660cb29f..568f0af542df70b6fb957fee581d7142ef9b3448 100644 (file)
@@ -170,16 +170,13 @@ void ToolCapabilities::deserializeJson(std::istream &is)
 }
 
 DigParams getDigParams(const ItemGroupList &groups,
-               const ToolCapabilities *tp, float time_from_last_punch)
+               const ToolCapabilities *tp)
 {
-       //infostream<<"getDigParams"<<std::endl;
-       /* Check group dig_immediate */
-       switch(itemgroup_get(groups, "dig_immediate")){
+       // Group dig_immediate has fixed time and no wear
+       switch (itemgroup_get(groups, "dig_immediate")) {
        case 2:
-               //infostream<<"dig_immediate=2"<<std::endl;
                return DigParams(true, 0.5, 0, "dig_immediate");
        case 3:
-               //infostream<<"dig_immediate=3"<<std::endl;
                return DigParams(true, 0, 0, "dig_immediate");
        default:
                break;
@@ -192,49 +189,37 @@ DigParams getDigParams(const ItemGroupList &groups,
        std::string result_main_group;
 
        int level = itemgroup_get(groups, "level");
-       //infostream<<"level="<<level<<std::endl;
        for (const auto &groupcap : tp->groupcaps) {
-               const std::string &name = groupcap.first;
-               //infostream<<"group="<<name<<std::endl;
                const ToolGroupCap &cap = groupcap.second;
-               int rating = itemgroup_get(groups, name);
+
+               int leveldiff = cap.maxlevel - level;
+               if (leveldiff < 0)
+                       continue;
+
+               const std::string &groupname = groupcap.first;
                float time = 0;
+               int rating = itemgroup_get(groups, groupname);
                bool time_exists = cap.getTime(rating, &time);
-               int leveldiff = cap.maxlevel - level;
-               time /= MYMAX(1, leveldiff);
-               if(!result_diggable || time < result_time){
-                       if(cap.maxlevel >= level && time_exists){
-                               result_diggable = true;
-                               result_time = time;
-                               if(cap.uses != 0)
-                                       result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
-                               else
-                                       result_wear = 0;
-                               result_main_group = name;
-                       }
+               if (!time_exists)
+                       continue;
+
+               if (leveldiff > 1)
+                       time /= leveldiff;
+               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;
+                       result_main_group = groupname;
                }
        }
-       //infostream<<"result_diggable="<<result_diggable<<std::endl;
-       //infostream<<"result_time="<<result_time<<std::endl;
-       //infostream<<"result_wear="<<result_wear<<std::endl;
-
-       if(time_from_last_punch < tp->full_punch_interval){
-               float f = time_from_last_punch / tp->full_punch_interval;
-               //infostream<<"f="<<f<<std::endl;
-               result_time /= f;
-               result_wear /= f;
-       }
 
-       u16 wear_i = 65535.*result_wear;
+       u16 wear_i = U16_MAX * result_wear;
        return DigParams(result_diggable, result_time, wear_i, result_main_group);
 }
 
-DigParams getDigParams(const ItemGroupList &groups,
-               const ToolCapabilities *tp)
-{
-       return getDigParams(groups, tp, 1000000);
-}
-
 HitParams getHitParams(const ItemGroupList &armor_groups,
                const ToolCapabilities *tp, float time_from_last_punch)
 {
index f6b196a49daba6c7559f0525d81a4c194a2f08cf..00fae48815c865723d9dc33e07a786274f8d95a1 100644 (file)
@@ -95,9 +95,6 @@ struct DigParams
        {}
 };
 
-DigParams getDigParams(const ItemGroupList &groups,
-               const ToolCapabilities *tp, float time_from_last_punch);
-
 DigParams getDigParams(const ItemGroupList &groups,
                const ToolCapabilities *tp);