]> git.lizzy.rs Git - minetest.git/blobdiff - src/tool.cpp
Merge remote branch 'origin/master'
[minetest.git] / src / tool.cpp
index f66fb4e51b51536f19dd9222c3bc0f10fded01e7..04f19749c9332c4c059b49d28b31a3817c70444a 100644 (file)
@@ -1,31 +1,32 @@
 /*
-Minetest-c55
-Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+Copyright (C) 2013 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());
@@ -34,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++){
@@ -48,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);
@@ -57,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);
@@ -77,10 +78,10 @@ DigParams getDigParams(const ItemGroupList &groups,
        switch(itemgroup_get(groups, "dig_immediate")){
        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);
+               return DigParams(true, 0.0, 0, "dig_immediate");
        default:
                break;
        }
@@ -89,6 +90,7 @@ DigParams getDigParams(const ItemGroupList &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;
@@ -101,11 +103,15 @@ DigParams getDigParams(const ItemGroupList &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;
                        }
                }
        }
@@ -121,7 +127,7 @@ DigParams getDigParams(const ItemGroupList &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 ItemGroupList &groups,
@@ -145,7 +151,7 @@ HitParams getHitParams(const ItemGroupList &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 ItemGroupList &groups,
@@ -181,6 +187,7 @@ PunchDamageResult getPunchDamage(
                result.did_punch = true;
                result.wear = hitparams.wear;
                result.damage = hitparams.hp;
+               result.main_group = hitparams.main_group;
        }
 
        return result;