]> git.lizzy.rs Git - minetest.git/blob - src/tool.cpp
Bump minimal protocol version to 36 (#6319)
[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 ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
29 {
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);
44                 }
45         }
46
47         writeU32(os, damageGroups.size());
48
49         for (const auto &damageGroup : damageGroups) {
50                 os << serializeString(damageGroup.first);
51                 writeS16(os, damageGroup.second);
52         }
53 }
54
55 void ToolCapabilities::deSerialize(std::istream &is)
56 {
57         int version = readU8(is);
58         if (version < 3)
59                 throw SerializationError("unsupported ToolCapabilities version");
60
61         full_punch_interval = readF1000(is);
62         max_drop_level = readS16(is);
63         groupcaps.clear();
64         u32 groupcaps_size = readU32(is);
65         for (u32 i = 0; i < groupcaps_size; i++) {
66                 std::string name = deSerializeString(is);
67                 ToolGroupCap cap;
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;
75                 }
76                 groupcaps[name] = cap;
77         }
78
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;
84         }
85 }
86
87 DigParams getDigParams(const ItemGroupList &groups,
88                 const ToolCapabilities *tp, float time_from_last_punch)
89 {
90         //infostream<<"getDigParams"<<std::endl;
91         /* Check group dig_immediate */
92         switch(itemgroup_get(groups, "dig_immediate")){
93         case 2:
94                 //infostream<<"dig_immediate=2"<<std::endl;
95                 return DigParams(true, 0.5, 0, "dig_immediate");
96         case 3:
97                 //infostream<<"dig_immediate=3"<<std::endl;
98                 return DigParams(true, 0, 0, "dig_immediate");
99         default:
100                 break;
101         }
102
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;
108
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);
116                 float time = 0;
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);
123                                 if(cap.uses != 0)
124                                         result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
125                                 else
126                                         result_wear = 0;
127                                 result_main_group = name;
128                         }
129                 }
130         }
131         //infostream<<"result_diggable="<<result_diggable<<std::endl;
132         //infostream<<"result_time="<<result_time<<std::endl;
133         //infostream<<"result_wear="<<result_wear<<std::endl;
134
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;
138                 result_time /= f;
139                 result_wear /= f;
140         }
141
142         u16 wear_i = 65535.*result_wear;
143         return DigParams(result_diggable, result_time, wear_i, result_main_group);
144 }
145
146 DigParams getDigParams(const ItemGroupList &groups,
147                 const ToolCapabilities *tp)
148 {
149         return getDigParams(groups, tp, 1000000);
150 }
151
152 HitParams getHitParams(const ItemGroupList &armor_groups,
153                 const ToolCapabilities *tp, float time_from_last_punch)
154 {
155         s16 damage = 0;
156         float full_punch_interval = tp->full_punch_interval;
157
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)
162                                 * armor / 100.0;
163         }
164
165         return {damage, 0};
166 }
167
168 HitParams getHitParams(const ItemGroupList &armor_groups,
169                 const ToolCapabilities *tp)
170 {
171         return getHitParams(armor_groups, tp, 1000000);
172 }
173
174 PunchDamageResult getPunchDamage(
175                 const ItemGroupList &armor_groups,
176                 const ToolCapabilities *toolcap,
177                 const ItemStack *punchitem,
178                 float time_from_last_punch
179 ){
180         bool do_hit = true;
181         {
182                 if (do_hit && punchitem) {
183                         if (itemgroup_get(armor_groups, "punch_operable") &&
184                                         (toolcap == NULL || punchitem->name.empty()))
185                                 do_hit = false;
186                 }
187
188                 if (do_hit) {
189                         if(itemgroup_get(armor_groups, "immortal"))
190                                 do_hit = false;
191                 }
192         }
193
194         PunchDamageResult result;
195         if(do_hit)
196         {
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;
202         }
203
204         return result;
205 }
206
207