X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Ftool.cpp;h=568f0af542df70b6fb957fee581d7142ef9b3448;hb=392e80e3f13c977b5499eb424fa7d65085fb08f5;hp=dcd4fbdf8b9567dafa2cdf6b6e34c1e613801a5e;hpb=77120a021df955947841f9646cb61d67a272618b;p=dragonfireclient.git diff --git a/src/tool.cpp b/src/tool.cpp index dcd4fbdf8..568f0af54 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -1,159 +1,245 @@ /* -Minetest-c55 -Copyright (C) 2011 celeron55, Perttu Ahola +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola 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 "itemgroup.h" #include "log.h" #include "inventory.h" +#include "exceptions.h" +#include "util/serialize.h" +#include "util/numeric.h" -void ToolCapabilities::serialize(std::ostream &os) const +void ToolGroupCap::toJson(Json::Value &object) const { - writeU8(os, 0); // version + object["maxlevel"] = maxlevel; + object["uses"] = uses; + + Json::Value times_object; + for (auto time : times) + times_object[time.first] = time.second; + object["times"] = times_object; +} + +void ToolGroupCap::fromJson(const Json::Value &json) +{ + if (json.isObject()) { + if (json["maxlevel"].isInt()) + maxlevel = json["maxlevel"].asInt(); + if (json["uses"].isInt()) + uses = json["uses"].asInt(); + const Json::Value ×_object = json["times"]; + if (times_object.isArray()) { + Json::ArrayIndex size = times_object.size(); + for (Json::ArrayIndex i = 0; i < size; ++i) + if (times_object[i].isDouble()) + times[i] = times_object[i].asFloat(); + } + } +} + +void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const +{ + writeU8(os, 3); // protocol_version >= 36 writeF1000(os, full_punch_interval); writeS16(os, max_drop_level); writeU32(os, groupcaps.size()); - for(std::map::const_iterator - i = groupcaps.begin(); i != groupcaps.end(); i++){ - const std::string *name = &i->first; - const ToolGroupCap *cap = &i->second; - os<maxwear); - writeF1000(os, cap->maxlevel); + for (const auto &groupcap : groupcaps) { + const std::string *name = &groupcap.first; + const ToolGroupCap *cap = &groupcap.second; + os << serializeString(*name); + writeS16(os, cap->uses); + writeS16(os, cap->maxlevel); writeU32(os, cap->times.size()); - for(std::map::const_iterator - i = cap->times.begin(); i != cap->times.end(); i++){ - writeS16(os, i->first); - writeF1000(os, i->second); + for (const auto &time : cap->times) { + writeS16(os, time.first); + writeF1000(os, time.second); } } + + writeU32(os, damageGroups.size()); + + for (const auto &damageGroup : damageGroups) { + os << serializeString(damageGroup.first); + writeS16(os, damageGroup.second); + } } void ToolCapabilities::deSerialize(std::istream &is) { int version = readU8(is); - if(version != 0) throw SerializationError( - "unsupported ToolCapabilities version"); + if (version < 3) + throw SerializationError("unsupported ToolCapabilities version"); + full_punch_interval = readF1000(is); max_drop_level = readS16(is); groupcaps.clear(); u32 groupcaps_size = readU32(is); - for(u32 i=0; ifirst] = dgiter->second; + } + root["damage_groups"] = damage_groups_object; + + os << root; +} + +void ToolCapabilities::deserializeJson(std::istream &is) +{ + Json::Value root; + is >> root; + if (root.isObject()) { + if (root["full_punch_interval"].isDouble()) + full_punch_interval = root["full_punch_interval"].asFloat(); + if (root["max_drop_level"].isInt()) + max_drop_level = root["max_drop_level"].asInt(); + + Json::Value &groupcaps_object = root["groupcaps"]; + if (groupcaps_object.isObject()) { + Json::ValueIterator gciter; + for (gciter = groupcaps_object.begin(); + gciter != groupcaps_object.end(); ++gciter) { + ToolGroupCap groupcap; + groupcap.fromJson(*gciter); + groupcaps[gciter.key().asString()] = groupcap; + } + } + + Json::Value &damage_groups_object = root["damage_groups"]; + if (damage_groups_object.isObject()) { + Json::ValueIterator dgiter; + for (dgiter = damage_groups_object.begin(); + dgiter != damage_groups_object.end(); ++dgiter) { + Json::Value &value = *dgiter; + if (value.isInt()) + damageGroups[dgiter.key().asString()] = + value.asInt(); + } + } + } } DigParams getDigParams(const ItemGroupList &groups, - const ToolCapabilities *tp, float time_from_last_punch) + const ToolCapabilities *tp) { - //infostream<<"getDigParams"<name == "")) + if (do_hit && punchitem) { + if (itemgroup_get(armor_groups, "punch_operable") && + (toolcap == NULL || punchitem->name.empty())) do_hit = false; } - if(do_hit){ + + if (do_hit) { if(itemgroup_get(armor_groups, "immortal")) do_hit = false; } } - + PunchDamageResult result; if(do_hit) { @@ -183,7 +270,6 @@ PunchDamageResult getPunchDamage( result.did_punch = true; result.wear = hitparams.wear; result.damage = hitparams.hp; - result.main_group = hitparams.main_group; } return result;