3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "itemgroup.h"
23 #include "inventory.h"
24 #include "exceptions.h"
25 #include "util/serialize.h"
26 #include "util/numeric.h"
28 void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
30 writeU8(os, 3); // protocol_version >= 36
31 writeF1000(os, full_punch_interval);
32 writeS16(os, max_drop_level);
33 writeU32(os, groupcaps.size());
34 for (const auto &groupcap : groupcaps) {
35 const std::string *name = &groupcap.first;
36 const ToolGroupCap *cap = &groupcap.second;
37 os << serializeString(*name);
38 writeS16(os, cap->uses);
39 writeS16(os, cap->maxlevel);
40 writeU32(os, cap->times.size());
41 for (const auto &time : cap->times) {
42 writeS16(os, time.first);
43 writeF1000(os, time.second);
47 writeU32(os, damageGroups.size());
49 for (const auto &damageGroup : damageGroups) {
50 os << serializeString(damageGroup.first);
51 writeS16(os, damageGroup.second);
55 void ToolCapabilities::deSerialize(std::istream &is)
57 int version = readU8(is);
59 throw SerializationError("unsupported ToolCapabilities version");
61 full_punch_interval = readF1000(is);
62 max_drop_level = readS16(is);
64 u32 groupcaps_size = readU32(is);
65 for (u32 i = 0; i < groupcaps_size; i++) {
66 std::string name = deSerializeString(is);
68 cap.uses = readS16(is);
69 cap.maxlevel = readS16(is);
70 u32 times_size = readU32(is);
71 for(u32 i = 0; i < times_size; i++) {
72 int level = readS16(is);
73 float time = readF1000(is);
74 cap.times[level] = time;
76 groupcaps[name] = cap;
79 u32 damage_groups_size = readU32(is);
80 for (u32 i = 0; i < damage_groups_size; i++) {
81 std::string name = deSerializeString(is);
82 s16 rating = readS16(is);
83 damageGroups[name] = rating;
87 DigParams getDigParams(const ItemGroupList &groups,
88 const ToolCapabilities *tp, float time_from_last_punch)
90 //infostream<<"getDigParams"<<std::endl;
91 /* Check group dig_immediate */
92 switch(itemgroup_get(groups, "dig_immediate")){
94 //infostream<<"dig_immediate=2"<<std::endl;
95 return DigParams(true, 0.5, 0, "dig_immediate");
97 //infostream<<"dig_immediate=3"<<std::endl;
98 return DigParams(true, 0, 0, "dig_immediate");
103 // Values to be returned (with a bit of conversion)
104 bool result_diggable = false;
105 float result_time = 0.0;
106 float result_wear = 0.0;
107 std::string result_main_group;
109 int level = itemgroup_get(groups, "level");
110 //infostream<<"level="<<level<<std::endl;
111 for (const auto &groupcap : tp->groupcaps) {
112 const std::string &name = groupcap.first;
113 //infostream<<"group="<<name<<std::endl;
114 const ToolGroupCap &cap = groupcap.second;
115 int rating = itemgroup_get(groups, name);
117 bool time_exists = cap.getTime(rating, &time);
118 if(!result_diggable || time < result_time){
119 if(cap.maxlevel >= level && time_exists){
120 result_diggable = true;
121 int leveldiff = cap.maxlevel - level;
122 result_time = time / MYMAX(1, leveldiff);
124 result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
127 result_main_group = name;
131 //infostream<<"result_diggable="<<result_diggable<<std::endl;
132 //infostream<<"result_time="<<result_time<<std::endl;
133 //infostream<<"result_wear="<<result_wear<<std::endl;
135 if(time_from_last_punch < tp->full_punch_interval){
136 float f = time_from_last_punch / tp->full_punch_interval;
137 //infostream<<"f="<<f<<std::endl;
142 u16 wear_i = 65535.*result_wear;
143 return DigParams(result_diggable, result_time, wear_i, result_main_group);
146 DigParams getDigParams(const ItemGroupList &groups,
147 const ToolCapabilities *tp)
149 return getDigParams(groups, tp, 1000000);
152 HitParams getHitParams(const ItemGroupList &armor_groups,
153 const ToolCapabilities *tp, float time_from_last_punch)
156 float full_punch_interval = tp->full_punch_interval;
158 for (const auto &damageGroup : tp->damageGroups) {
159 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
160 damage += damageGroup.second
161 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
168 HitParams getHitParams(const ItemGroupList &armor_groups,
169 const ToolCapabilities *tp)
171 return getHitParams(armor_groups, tp, 1000000);
174 PunchDamageResult getPunchDamage(
175 const ItemGroupList &armor_groups,
176 const ToolCapabilities *toolcap,
177 const ItemStack *punchitem,
178 float time_from_last_punch
182 if (do_hit && punchitem) {
183 if (itemgroup_get(armor_groups, "punch_operable") &&
184 (toolcap == NULL || punchitem->name.empty()))
189 if(itemgroup_get(armor_groups, "immortal"))
194 PunchDamageResult result;
197 HitParams hitparams = getHitParams(armor_groups, toolcap,
198 time_from_last_punch);
199 result.did_punch = true;
200 result.wear = hitparams.wear;
201 result.damage = hitparams.hp;