]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/tool.cpp
Add an option to disable object <-> object collision for Lua entities
[dragonfireclient.git] / src / tool.cpp
index d6f994307aeb00495e5b13555821e79a547d0f42..4d809e2c44ffc75e3e1e374a439004b19a2d8462 100644 (file)
@@ -1,31 +1,35 @@
 /*
-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
+void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
 {
-       writeU8(os, 0); // version
+       if(protocol_version <= 17)
+               writeU8(os, 1); // version
+       else
+               writeU8(os, 2); // version
        writeF1000(os, full_punch_interval);
        writeS16(os, max_drop_level);
        writeU32(os, groupcaps.size());
@@ -34,8 +38,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++){
@@ -43,12 +47,20 @@ void ToolCapabilities::serialize(std::ostream &os) const
                        writeF1000(os, i->second);
                }
        }
+       if(protocol_version > 17){
+               writeU32(os, damageGroups.size());
+               for(std::map<std::string, s16>::const_iterator
+                               i = damageGroups.begin(); i != damageGroups.end(); i++){
+                       os<<serializeString(i->first);
+                       writeS16(os, i->second);
+               }
+       }
 }
 
 void ToolCapabilities::deSerialize(std::istream &is)
 {
        int version = readU8(is);
-       if(version != 0) throw SerializationError(
+       if(version != 1 && version != 2) throw SerializationError(
                        "unsupported ToolCapabilities version");
        full_punch_interval = readF1000(is);
        max_drop_level = readS16(is);
@@ -57,8 +69,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);
@@ -67,6 +79,15 @@ void ToolCapabilities::deSerialize(std::istream &is)
                }
                groupcaps[name] = cap;
        }
+       if(version == 2)
+       {
+               u32 damage_groups_size = readU32(is);
+               for(u32 i=0; i<damage_groups_size; i++){
+                       std::string name = deSerializeString(is);
+                       s16 rating = readS16(is);
+                       damageGroups[name] = rating;
+               }
+       }
 }
 
 DigParams getDigParams(const ItemGroupList &groups,
@@ -75,12 +96,12 @@ DigParams getDigParams(const ItemGroupList &groups,
        //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;
        }
@@ -89,6 +110,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 +123,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 +147,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,
@@ -130,28 +156,26 @@ DigParams getDigParams(const ItemGroupList &groups,
        return getDigParams(groups, tp, 1000000);
 }
 
-HitParams getHitParams(const ItemGroupList &groups,
+HitParams getHitParams(const ItemGroupList &armor_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)
-               hp = time_from_last_punch / digprop.time;
-       // Wear is the same as for digging a single node
-       s16 wear = (float)digprop.wear;
-
-       return HitParams(hp, wear);
+       s16 damage = 0;
+       float full_punch_interval = tp->full_punch_interval;
+
+       for(std::map<std::string, s16>::const_iterator
+                       i = tp->damageGroups.begin(); i != tp->damageGroups.end(); i++){
+               s16 armor = itemgroup_get(armor_groups, i->first);
+               damage += i->second * rangelim(time_from_last_punch * full_punch_interval, 0.0, 1.0)
+                               * armor / 100.0;
+       }
+
+       return HitParams(damage, 0);
 }
 
-HitParams getHitParams(const ItemGroupList &groups,
+HitParams getHitParams(const ItemGroupList &armor_groups,
                const ToolCapabilities *tp)
 {
-       return getHitParams(groups, tp, 1000000);
+       return getHitParams(armor_groups, tp, 1000000);
 }
 
 PunchDamageResult getPunchDamage(