]> git.lizzy.rs Git - minetest.git/blob - src/tool.cpp
Fix item and wield meshes (#6596)
[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, float time_from_last_punch)
174 {
175         //infostream<<"getDigParams"<<std::endl;
176         /* Check group dig_immediate */
177         switch(itemgroup_get(groups, "dig_immediate")){
178         case 2:
179                 //infostream<<"dig_immediate=2"<<std::endl;
180                 return DigParams(true, 0.5, 0, "dig_immediate");
181         case 3:
182                 //infostream<<"dig_immediate=3"<<std::endl;
183                 return DigParams(true, 0, 0, "dig_immediate");
184         default:
185                 break;
186         }
187
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;
193
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);
201                 float time = 0;
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);
208                                 if(cap.uses != 0)
209                                         result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
210                                 else
211                                         result_wear = 0;
212                                 result_main_group = name;
213                         }
214                 }
215         }
216         //infostream<<"result_diggable="<<result_diggable<<std::endl;
217         //infostream<<"result_time="<<result_time<<std::endl;
218         //infostream<<"result_wear="<<result_wear<<std::endl;
219
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;
223                 result_time /= f;
224                 result_wear /= f;
225         }
226
227         u16 wear_i = 65535.*result_wear;
228         return DigParams(result_diggable, result_time, wear_i, result_main_group);
229 }
230
231 DigParams getDigParams(const ItemGroupList &groups,
232                 const ToolCapabilities *tp)
233 {
234         return getDigParams(groups, tp, 1000000);
235 }
236
237 HitParams getHitParams(const ItemGroupList &armor_groups,
238                 const ToolCapabilities *tp, float time_from_last_punch)
239 {
240         s16 damage = 0;
241         float full_punch_interval = tp->full_punch_interval;
242
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)
247                                 * armor / 100.0;
248         }
249
250         return {damage, 0};
251 }
252
253 HitParams getHitParams(const ItemGroupList &armor_groups,
254                 const ToolCapabilities *tp)
255 {
256         return getHitParams(armor_groups, tp, 1000000);
257 }
258
259 PunchDamageResult getPunchDamage(
260                 const ItemGroupList &armor_groups,
261                 const ToolCapabilities *toolcap,
262                 const ItemStack *punchitem,
263                 float time_from_last_punch
264 ){
265         bool do_hit = true;
266         {
267                 if (do_hit && punchitem) {
268                         if (itemgroup_get(armor_groups, "punch_operable") &&
269                                         (toolcap == NULL || punchitem->name.empty()))
270                                 do_hit = false;
271                 }
272
273                 if (do_hit) {
274                         if(itemgroup_get(armor_groups, "immortal"))
275                                 do_hit = false;
276                 }
277         }
278
279         PunchDamageResult result;
280         if(do_hit)
281         {
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;
287         }
288
289         return result;
290 }
291
292