]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/tool.cpp
Merge pull request #505 from RealBadAngel/master
[dragonfireclient.git] / src / tool.cpp
index 69141f4e5a4b30be0884d3fc124217e87f2ce901..c1e268ff11c9ff4f512a3a865924de90e92bba74 100644 (file)
@@ -3,28 +3,30 @@ Minetest-c55
 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+GNU Lesser General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
+You should have received a copy of the GNU Lesser General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
 #include "tool.h"
-#include "utility.h"
 #include "itemdef.h" // For itemgroup_get()
 #include "log.h"
+#include "inventory.h"
+#include "util/serialize.h"
+#include "util/numeric.h"
 
 void ToolCapabilities::serialize(std::ostream &os) const
 {
-       writeU8(os, 0); // version
+       writeU8(os, 1); // version
        writeF1000(os, full_punch_interval);
        writeS16(os, max_drop_level);
        writeU32(os, groupcaps.size());
@@ -33,8 +35,8 @@ void ToolCapabilities::serialize(std::ostream &os) const
                const std::string *name = &i->first;
                const ToolGroupCap *cap = &i->second;
                os<<serializeString(*name);
-               writeF1000(os, cap->maxwear);
-               writeF1000(os, cap->maxlevel);
+               writeS16(os, cap->uses);
+               writeS16(os, cap->maxlevel);
                writeU32(os, cap->times.size());
                for(std::map<int, float>::const_iterator
                                i = cap->times.begin(); i != cap->times.end(); i++){
@@ -47,7 +49,7 @@ void ToolCapabilities::serialize(std::ostream &os) const
 void ToolCapabilities::deSerialize(std::istream &is)
 {
        int version = readU8(is);
-       if(version != 0) throw SerializationError(
+       if(version != 1) throw SerializationError(
                        "unsupported ToolCapabilities version");
        full_punch_interval = readF1000(is);
        max_drop_level = readS16(is);
@@ -56,8 +58,8 @@ void ToolCapabilities::deSerialize(std::istream &is)
        for(u32 i=0; i<groupcaps_size; i++){
                std::string name = deSerializeString(is);
                ToolGroupCap cap;
-               cap.maxwear = readF1000(is);
-               cap.maxlevel = readF1000(is);
+               cap.uses = readS16(is);
+               cap.maxlevel = readS16(is);
                u32 times_size = readU32(is);
                for(u32 i=0; i<times_size; i++){
                        int level = readS16(is);
@@ -68,18 +70,18 @@ void ToolCapabilities::deSerialize(std::istream &is)
        }
 }
 
-DigParams getDigParams(const std::map<std::string, int> &groups,
+DigParams getDigParams(const ItemGroupList &groups,
                const ToolCapabilities *tp, float time_from_last_punch)
 {
        //infostream<<"getDigParams"<<std::endl;
        /* Check group dig_immediate */
        switch(itemgroup_get(groups, "dig_immediate")){
-       case 1:
-               //infostream<<"dig_immediate=1"<<std::endl;
-               return DigParams(true, 0.0, 0);
        case 2:
                //infostream<<"dig_immediate=2"<<std::endl;
-               return DigParams(true, 1.0, 0);
+               return DigParams(true, 0.5, 0, "dig_immediate");
+       case 3:
+               //infostream<<"dig_immediate=3"<<std::endl;
+               return DigParams(true, 0.0, 0, "dig_immediate");
        default:
                break;
        }
@@ -88,6 +90,7 @@ DigParams getDigParams(const std::map<std::string, int> &groups,
        bool result_diggable = false;
        float result_time = 0.0;
        float result_wear = 0.0;
+       std::string result_main_group = "";
 
        int level = itemgroup_get(groups, "level");
        //infostream<<"level="<<level<<std::endl;
@@ -100,11 +103,15 @@ DigParams getDigParams(const std::map<std::string, int> &groups,
                float time = 0;
                bool time_exists = cap.getTime(rating, &time);
                if(!result_diggable || time < result_time){
-                       if(cap.maxlevel > level && time_exists){
+                       if(cap.maxlevel >= level && time_exists){
                                result_diggable = true;
-                               result_time = time;
                                int leveldiff = cap.maxlevel - level;
-                               result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
+                               result_time = time / MYMAX(1, leveldiff);
+                               if(cap.uses != 0)
+                                       result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
+                               else
+                                       result_wear = 0;
+                               result_main_group = name;
                        }
                }
        }
@@ -120,21 +127,23 @@ DigParams getDigParams(const std::map<std::string, int> &groups,
        }
 
        u16 wear_i = 65535.*result_wear;
-       return DigParams(result_diggable, result_time, wear_i);
+       return DigParams(result_diggable, result_time, wear_i, result_main_group);
 }
 
-DigParams getDigParams(const std::map<std::string, int> &groups,
+DigParams getDigParams(const ItemGroupList &groups,
                const ToolCapabilities *tp)
 {
        return getDigParams(groups, tp, 1000000);
 }
 
-HitParams getHitParams(const std::map<std::string, int> &groups,
+HitParams getHitParams(const ItemGroupList &groups,
                const ToolCapabilities *tp, float time_from_last_punch)
 {
        DigParams digprop = getDigParams(groups, tp,
                        time_from_last_punch);
        
+       if(time_from_last_punch > tp->full_punch_interval)
+               time_from_last_punch = tp->full_punch_interval;
        // Damage in hp is equivalent to nodes dug in time_from_last_punch
        s16 hp = 0;
        if(digprop.diggable)
@@ -142,12 +151,46 @@ HitParams getHitParams(const std::map<std::string, int> &groups,
        // Wear is the same as for digging a single node
        s16 wear = (float)digprop.wear;
 
-       return HitParams(hp, wear);
+       return HitParams(hp, wear, digprop.main_group);
 }
 
-HitParams getHitParams(const std::map<std::string, int> &groups,
+HitParams getHitParams(const ItemGroupList &groups,
                const ToolCapabilities *tp)
 {
        return getHitParams(groups, tp, 1000000);
 }
 
+PunchDamageResult getPunchDamage(
+               const ItemGroupList &armor_groups,
+               const ToolCapabilities *toolcap,
+               const ItemStack *punchitem,
+               float time_from_last_punch
+){
+       bool do_hit = true;
+       {
+               if(do_hit && punchitem){
+                       if(itemgroup_get(armor_groups, "punch_operable") &&
+                                       (toolcap == NULL || punchitem->name == ""))
+                               do_hit = false;
+               }
+               if(do_hit){
+                       if(itemgroup_get(armor_groups, "immortal"))
+                               do_hit = false;
+               }
+       }
+       
+       PunchDamageResult result;
+       if(do_hit)
+       {
+               HitParams hitparams = getHitParams(armor_groups, toolcap,
+                               time_from_last_punch);
+               result.did_punch = true;
+               result.wear = hitparams.wear;
+               result.damage = hitparams.hp;
+               result.main_group = hitparams.main_group;
+       }
+
+       return result;
+}
+
+