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 ToolGroupCap::toJson(Json::Value &object) const
30 object["maxlevel"] = maxlevel;
31 object["uses"] = uses;
33 Json::Value times_object;
34 for (auto time : times)
35 times_object[time.first] = time.second;
36 object["times"] = times_object;
39 void ToolGroupCap::fromJson(const Json::Value &json)
41 if (json.isObject()) {
42 if (json["maxlevel"].isInt())
43 maxlevel = json["maxlevel"].asInt();
44 if (json["uses"].isInt())
45 uses = json["uses"].asInt();
46 const Json::Value ×_object = json["times"];
47 if (times_object.isArray()) {
48 Json::ArrayIndex size = times_object.size();
49 for (Json::ArrayIndex i = 0; i < size; ++i)
50 if (times_object[i].isDouble())
51 times[i] = times_object[i].asFloat();
56 void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
58 writeU8(os, 3); // protocol_version >= 36
59 writeF1000(os, full_punch_interval);
60 writeS16(os, max_drop_level);
61 writeU32(os, groupcaps.size());
62 for (const auto &groupcap : groupcaps) {
63 const std::string *name = &groupcap.first;
64 const ToolGroupCap *cap = &groupcap.second;
65 os << serializeString(*name);
66 writeS16(os, cap->uses);
67 writeS16(os, cap->maxlevel);
68 writeU32(os, cap->times.size());
69 for (const auto &time : cap->times) {
70 writeS16(os, time.first);
71 writeF1000(os, time.second);
75 writeU32(os, damageGroups.size());
77 for (const auto &damageGroup : damageGroups) {
78 os << serializeString(damageGroup.first);
79 writeS16(os, damageGroup.second);
83 void ToolCapabilities::deSerialize(std::istream &is)
85 int version = readU8(is);
87 throw SerializationError("unsupported ToolCapabilities version");
89 full_punch_interval = readF1000(is);
90 max_drop_level = readS16(is);
92 u32 groupcaps_size = readU32(is);
93 for (u32 i = 0; i < groupcaps_size; i++) {
94 std::string name = deSerializeString(is);
96 cap.uses = readS16(is);
97 cap.maxlevel = readS16(is);
98 u32 times_size = readU32(is);
99 for(u32 i = 0; i < times_size; i++) {
100 int level = readS16(is);
101 float time = readF1000(is);
102 cap.times[level] = time;
104 groupcaps[name] = cap;
107 u32 damage_groups_size = readU32(is);
108 for (u32 i = 0; i < damage_groups_size; i++) {
109 std::string name = deSerializeString(is);
110 s16 rating = readS16(is);
111 damageGroups[name] = rating;
115 void ToolCapabilities::serializeJson(std::ostream &os) const
118 root["full_punch_interval"] = full_punch_interval;
119 root["max_drop_level"] = max_drop_level;
121 Json::Value groupcaps_object;
122 for (auto groupcap : groupcaps) {
123 groupcap.second.toJson(groupcaps_object[groupcap.first]);
125 root["groupcaps"] = groupcaps_object;
127 Json::Value damage_groups_object;
128 DamageGroup::const_iterator dgiter;
129 for (dgiter = damageGroups.begin(); dgiter != damageGroups.end(); ++dgiter) {
130 damage_groups_object[dgiter->first] = dgiter->second;
132 root["damage_groups"] = damage_groups_object;
137 void ToolCapabilities::deserializeJson(std::istream &is)
141 if (root.isObject()) {
142 if (root["full_punch_interval"].isDouble())
143 full_punch_interval = root["full_punch_interval"].asFloat();
144 if (root["max_drop_level"].isInt())
145 max_drop_level = root["max_drop_level"].asInt();
147 Json::Value &groupcaps_object = root["groupcaps"];
148 if (groupcaps_object.isObject()) {
149 Json::ValueIterator gciter;
150 for (gciter = groupcaps_object.begin();
151 gciter != groupcaps_object.end(); ++gciter) {
152 ToolGroupCap groupcap;
153 groupcap.fromJson(*gciter);
154 groupcaps[gciter.key().asString()] = groupcap;
158 Json::Value &damage_groups_object = root["damage_groups"];
159 if (damage_groups_object.isObject()) {
160 Json::ValueIterator dgiter;
161 for (dgiter = damage_groups_object.begin();
162 dgiter != damage_groups_object.end(); ++dgiter) {
163 Json::Value &value = *dgiter;
165 damageGroups[dgiter.key().asString()] =
172 DigParams getDigParams(const ItemGroupList &groups,
173 const ToolCapabilities *tp, float time_from_last_punch)
175 //infostream<<"getDigParams"<<std::endl;
176 /* Check group dig_immediate */
177 switch(itemgroup_get(groups, "dig_immediate")){
179 //infostream<<"dig_immediate=2"<<std::endl;
180 return DigParams(true, 0.5, 0, "dig_immediate");
182 //infostream<<"dig_immediate=3"<<std::endl;
183 return DigParams(true, 0, 0, "dig_immediate");
188 // Values to be returned (with a bit of conversion)
189 bool result_diggable = false;
190 float result_time = 0.0;
191 float result_wear = 0.0;
192 std::string result_main_group;
194 int level = itemgroup_get(groups, "level");
195 //infostream<<"level="<<level<<std::endl;
196 for (const auto &groupcap : tp->groupcaps) {
197 const std::string &name = groupcap.first;
198 //infostream<<"group="<<name<<std::endl;
199 const ToolGroupCap &cap = groupcap.second;
200 int rating = itemgroup_get(groups, name);
202 bool time_exists = cap.getTime(rating, &time);
203 if(!result_diggable || time < result_time){
204 if(cap.maxlevel >= level && time_exists){
205 result_diggable = true;
206 int leveldiff = cap.maxlevel - level;
207 result_time = time / MYMAX(1, leveldiff);
209 result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
212 result_main_group = name;
216 //infostream<<"result_diggable="<<result_diggable<<std::endl;
217 //infostream<<"result_time="<<result_time<<std::endl;
218 //infostream<<"result_wear="<<result_wear<<std::endl;
220 if(time_from_last_punch < tp->full_punch_interval){
221 float f = time_from_last_punch / tp->full_punch_interval;
222 //infostream<<"f="<<f<<std::endl;
227 u16 wear_i = 65535.*result_wear;
228 return DigParams(result_diggable, result_time, wear_i, result_main_group);
231 DigParams getDigParams(const ItemGroupList &groups,
232 const ToolCapabilities *tp)
234 return getDigParams(groups, tp, 1000000);
237 HitParams getHitParams(const ItemGroupList &armor_groups,
238 const ToolCapabilities *tp, float time_from_last_punch)
241 float full_punch_interval = tp->full_punch_interval;
243 for (const auto &damageGroup : tp->damageGroups) {
244 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
245 damage += damageGroup.second
246 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
253 HitParams getHitParams(const ItemGroupList &armor_groups,
254 const ToolCapabilities *tp)
256 return getHitParams(armor_groups, tp, 1000000);
259 PunchDamageResult getPunchDamage(
260 const ItemGroupList &armor_groups,
261 const ToolCapabilities *toolcap,
262 const ItemStack *punchitem,
263 float time_from_last_punch
267 if (do_hit && punchitem) {
268 if (itemgroup_get(armor_groups, "punch_operable") &&
269 (toolcap == NULL || punchitem->name.empty()))
274 if(itemgroup_get(armor_groups, "immortal"))
279 PunchDamageResult result;
282 HitParams hitparams = getHitParams(armor_groups, toolcap,
283 time_from_last_punch);
284 result.did_punch = true;
285 result.wear = hitparams.wear;
286 result.damage = hitparams.hp;