]> git.lizzy.rs Git - minetest.git/blob - src/tool.cpp
Fix temporary path crash in TestAuthDatabase (#7753)
[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, 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);
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 < 3)
87                 throw SerializationError("unsupported ToolCapabilities version");
88
89         full_punch_interval = readF1000(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 = readF1000(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 has fixed time and no wear
176         switch (itemgroup_get(groups, "dig_immediate")) {
177         case 2:
178                 return DigParams(true, 0.5, 0, "dig_immediate");
179         case 3:
180                 return DigParams(true, 0, 0, "dig_immediate");
181         default:
182                 break;
183         }
184
185         // Values to be returned (with a bit of conversion)
186         bool result_diggable = false;
187         float result_time = 0.0;
188         float result_wear = 0.0;
189         std::string result_main_group;
190
191         int level = itemgroup_get(groups, "level");
192         for (const auto &groupcap : tp->groupcaps) {
193                 const ToolGroupCap &cap = groupcap.second;
194
195                 int leveldiff = cap.maxlevel - level;
196                 if (leveldiff < 0)
197                         continue;
198
199                 const std::string &groupname = groupcap.first;
200                 float time = 0;
201                 int rating = itemgroup_get(groups, groupname);
202                 bool time_exists = cap.getTime(rating, &time);
203                 if (!time_exists)
204                         continue;
205
206                 if (leveldiff > 1)
207                         time /= leveldiff;
208                 if (!result_diggable || time < result_time) {
209                         result_time = time;
210                         result_diggable = true;
211                         if (cap.uses != 0)
212                                 result_wear = 1.0 / cap.uses / pow(3.0, leveldiff);
213                         else
214                                 result_wear = 0;
215                         result_main_group = groupname;
216                 }
217         }
218
219         u16 wear_i = U16_MAX * result_wear;
220         return DigParams(result_diggable, result_time, wear_i, result_main_group);
221 }
222
223 HitParams getHitParams(const ItemGroupList &armor_groups,
224                 const ToolCapabilities *tp, float time_from_last_punch)
225 {
226         s16 damage = 0;
227         float full_punch_interval = tp->full_punch_interval;
228
229         for (const auto &damageGroup : tp->damageGroups) {
230                 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
231                 damage += damageGroup.second
232                                 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
233                                 * armor / 100.0;
234         }
235
236         return {damage, 0};
237 }
238
239 HitParams getHitParams(const ItemGroupList &armor_groups,
240                 const ToolCapabilities *tp)
241 {
242         return getHitParams(armor_groups, tp, 1000000);
243 }
244
245 PunchDamageResult getPunchDamage(
246                 const ItemGroupList &armor_groups,
247                 const ToolCapabilities *toolcap,
248                 const ItemStack *punchitem,
249                 float time_from_last_punch
250 ){
251         bool do_hit = true;
252         {
253                 if (do_hit && punchitem) {
254                         if (itemgroup_get(armor_groups, "punch_operable") &&
255                                         (toolcap == NULL || punchitem->name.empty()))
256                                 do_hit = false;
257                 }
258
259                 if (do_hit) {
260                         if(itemgroup_get(armor_groups, "immortal"))
261                                 do_hit = false;
262                 }
263         }
264
265         PunchDamageResult result;
266         if(do_hit)
267         {
268                 HitParams hitparams = getHitParams(armor_groups, toolcap,
269                                 time_from_last_punch);
270                 result.did_punch = true;
271                 result.wear = hitparams.wear;
272                 result.damage = hitparams.hp;
273         }
274
275         return result;
276 }
277
278