Do not add group values of zero to group lists. (#8751)
[oweals/minetest.git] / src / tool.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "tool.h"
21 #include "itemgroup.h"
22 #include "log.h"
23 #include "inventory.h"
24 #include "exceptions.h"
25 #include "util/serialize.h"
26 #include "util/numeric.h"
27
28 void ToolGroupCap::toJson(Json::Value &object) const
29 {
30         object["maxlevel"] = maxlevel;
31         object["uses"] = uses;
32
33         Json::Value times_object;
34         for (auto time : times)
35                 times_object[time.first] = time.second;
36         object["times"] = times_object;
37 }
38
39 void ToolGroupCap::fromJson(const Json::Value &json)
40 {
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 &times_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();
52                 }
53         }
54 }
55
56 void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
57 {
58         writeU8(os, 4); // protocol_version >= 37
59         writeF32(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                         writeF32(os, time.second);
72                 }
73         }
74
75         writeU32(os, damageGroups.size());
76
77         for (const auto &damageGroup : damageGroups) {
78                 os << serializeString(damageGroup.first);
79                 writeS16(os, damageGroup.second);
80         }
81 }
82
83 void ToolCapabilities::deSerialize(std::istream &is)
84 {
85         int version = readU8(is);
86         if (version < 4)
87                 throw SerializationError("unsupported ToolCapabilities version");
88
89         full_punch_interval = readF32(is);
90         max_drop_level = readS16(is);
91         groupcaps.clear();
92         u32 groupcaps_size = readU32(is);
93         for (u32 i = 0; i < groupcaps_size; i++) {
94                 std::string name = deSerializeString(is);
95                 ToolGroupCap cap;
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 = readF32(is);
102                         cap.times[level] = time;
103                 }
104                 groupcaps[name] = cap;
105         }
106
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;
112         }
113 }
114
115 void ToolCapabilities::serializeJson(std::ostream &os) const
116 {
117         Json::Value root;
118         root["full_punch_interval"] = full_punch_interval;
119         root["max_drop_level"] = max_drop_level;
120
121         Json::Value groupcaps_object;
122         for (auto groupcap : groupcaps) {
123                 groupcap.second.toJson(groupcaps_object[groupcap.first]);
124         }
125         root["groupcaps"] = groupcaps_object;
126
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;
131         }
132         root["damage_groups"] = damage_groups_object;
133
134         os << root;
135 }
136
137 void ToolCapabilities::deserializeJson(std::istream &is)
138 {
139         Json::Value root;
140         is >> root;
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();
146
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;
155                         }
156                 }
157
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;
164                                 if (value.isInt())
165                                         damageGroups[dgiter.key().asString()] =
166                                                 value.asInt();
167                         }
168                 }
169         }
170 }
171
172 DigParams getDigParams(const ItemGroupList &groups,
173                 const ToolCapabilities *tp)
174 {
175         // Group dig_immediate defaults to fixed time and no wear
176         if (tp->groupcaps.find("dig_immediate") == tp->groupcaps.cend()) {
177                 switch (itemgroup_get(groups, "dig_immediate")) {
178                 case 2:
179                         return DigParams(true, 0.5, 0, "dig_immediate");
180                 case 3:
181                         return DigParams(true, 0, 0, "dig_immediate");
182                 default:
183                         break;
184                 }
185         }
186
187         // Values to be returned (with a bit of conversion)
188         bool result_diggable = false;
189         float result_time = 0.0;
190         float result_wear = 0.0;
191         std::string result_main_group;
192
193         int level = itemgroup_get(groups, "level");
194         for (const auto &groupcap : tp->groupcaps) {
195                 const ToolGroupCap &cap = groupcap.second;
196
197                 int leveldiff = cap.maxlevel - level;
198                 if (leveldiff < 0)
199                         continue;
200
201                 const std::string &groupname = groupcap.first;
202                 float time = 0;
203                 int rating = itemgroup_get(groups, groupname);
204                 bool time_exists = cap.getTime(rating, &time);
205                 if (!time_exists)
206                         continue;
207
208                 if (leveldiff > 1)
209                         time /= leveldiff;
210                 if (!result_diggable || time < result_time) {
211                         result_time = time;
212                         result_diggable = true;
213                         if (cap.uses != 0)
214                                 result_wear = 1.0 / cap.uses / pow(3.0, leveldiff);
215                         else
216                                 result_wear = 0;
217                         result_main_group = groupname;
218                 }
219         }
220
221         u16 wear_i = U16_MAX * result_wear;
222         return DigParams(result_diggable, result_time, wear_i, result_main_group);
223 }
224
225 HitParams getHitParams(const ItemGroupList &armor_groups,
226                 const ToolCapabilities *tp, float time_from_last_punch)
227 {
228         s16 damage = 0;
229         float full_punch_interval = tp->full_punch_interval;
230
231         for (const auto &damageGroup : tp->damageGroups) {
232                 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
233                 damage += damageGroup.second
234                                 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
235                                 * armor / 100.0;
236         }
237
238         return {damage, 0};
239 }
240
241 HitParams getHitParams(const ItemGroupList &armor_groups,
242                 const ToolCapabilities *tp)
243 {
244         return getHitParams(armor_groups, tp, 1000000);
245 }
246
247 PunchDamageResult getPunchDamage(
248                 const ItemGroupList &armor_groups,
249                 const ToolCapabilities *toolcap,
250                 const ItemStack *punchitem,
251                 float time_from_last_punch
252 ){
253         bool do_hit = true;
254         {
255                 if (do_hit && punchitem) {
256                         if (itemgroup_get(armor_groups, "punch_operable") &&
257                                         (toolcap == NULL || punchitem->name.empty()))
258                                 do_hit = false;
259                 }
260
261                 if (do_hit) {
262                         if(itemgroup_get(armor_groups, "immortal"))
263                                 do_hit = false;
264                 }
265         }
266
267         PunchDamageResult result;
268         if(do_hit)
269         {
270                 HitParams hitparams = getHitParams(armor_groups, toolcap,
271                                 time_from_last_punch);
272                 result.did_punch = true;
273                 result.wear = hitparams.wear;
274                 result.damage = hitparams.hp;
275         }
276
277         return result;
278 }
279
280