]> git.lizzy.rs Git - minetest.git/blob - src/tool.cpp
Modernize source code: last part (#6285)
[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, 2); // version (protocol >= 18)
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 != 1 && version != 2) throw SerializationError(
59                         "unsupported ToolCapabilities version");
60         full_punch_interval = readF1000(is);
61         max_drop_level = readS16(is);
62         groupcaps.clear();
63         u32 groupcaps_size = readU32(is);
64         for(u32 i=0; i<groupcaps_size; i++){
65                 std::string name = deSerializeString(is);
66                 ToolGroupCap cap;
67                 cap.uses = readS16(is);
68                 cap.maxlevel = readS16(is);
69                 u32 times_size = readU32(is);
70                 for(u32 i=0; i<times_size; i++){
71                         int level = readS16(is);
72                         float time = readF1000(is);
73                         cap.times[level] = time;
74                 }
75                 groupcaps[name] = cap;
76         }
77         if(version == 2)
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
88 DigParams getDigParams(const ItemGroupList &groups,
89                 const ToolCapabilities *tp, float time_from_last_punch)
90 {
91         //infostream<<"getDigParams"<<std::endl;
92         /* Check group dig_immediate */
93         switch(itemgroup_get(groups, "dig_immediate")){
94         case 2:
95                 //infostream<<"dig_immediate=2"<<std::endl;
96                 return DigParams(true, 0.5, 0, "dig_immediate");
97         case 3:
98                 //infostream<<"dig_immediate=3"<<std::endl;
99                 return DigParams(true, 0, 0, "dig_immediate");
100         default:
101                 break;
102         }
103
104         // Values to be returned (with a bit of conversion)
105         bool result_diggable = false;
106         float result_time = 0.0;
107         float result_wear = 0.0;
108         std::string result_main_group;
109
110         int level = itemgroup_get(groups, "level");
111         //infostream<<"level="<<level<<std::endl;
112         for (const auto &groupcap : tp->groupcaps) {
113                 const std::string &name = groupcap.first;
114                 //infostream<<"group="<<name<<std::endl;
115                 const ToolGroupCap &cap = groupcap.second;
116                 int rating = itemgroup_get(groups, name);
117                 float time = 0;
118                 bool time_exists = cap.getTime(rating, &time);
119                 if(!result_diggable || time < result_time){
120                         if(cap.maxlevel >= level && time_exists){
121                                 result_diggable = true;
122                                 int leveldiff = cap.maxlevel - level;
123                                 result_time = time / MYMAX(1, leveldiff);
124                                 if(cap.uses != 0)
125                                         result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
126                                 else
127                                         result_wear = 0;
128                                 result_main_group = name;
129                         }
130                 }
131         }
132         //infostream<<"result_diggable="<<result_diggable<<std::endl;
133         //infostream<<"result_time="<<result_time<<std::endl;
134         //infostream<<"result_wear="<<result_wear<<std::endl;
135
136         if(time_from_last_punch < tp->full_punch_interval){
137                 float f = time_from_last_punch / tp->full_punch_interval;
138                 //infostream<<"f="<<f<<std::endl;
139                 result_time /= f;
140                 result_wear /= f;
141         }
142
143         u16 wear_i = 65535.*result_wear;
144         return DigParams(result_diggable, result_time, wear_i, result_main_group);
145 }
146
147 DigParams getDigParams(const ItemGroupList &groups,
148                 const ToolCapabilities *tp)
149 {
150         return getDigParams(groups, tp, 1000000);
151 }
152
153 HitParams getHitParams(const ItemGroupList &armor_groups,
154                 const ToolCapabilities *tp, float time_from_last_punch)
155 {
156         s16 damage = 0;
157         float full_punch_interval = tp->full_punch_interval;
158
159         for (const auto &damageGroup : tp->damageGroups) {
160                 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
161                 damage += damageGroup.second
162                                 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
163                                 * armor / 100.0;
164         }
165
166         return {damage, 0};
167 }
168
169 HitParams getHitParams(const ItemGroupList &armor_groups,
170                 const ToolCapabilities *tp)
171 {
172         return getHitParams(armor_groups, tp, 1000000);
173 }
174
175 PunchDamageResult getPunchDamage(
176                 const ItemGroupList &armor_groups,
177                 const ToolCapabilities *toolcap,
178                 const ItemStack *punchitem,
179                 float time_from_last_punch
180 ){
181         bool do_hit = true;
182         {
183                 if (do_hit && punchitem) {
184                         if (itemgroup_get(armor_groups, "punch_operable") &&
185                                         (toolcap == NULL || punchitem->name.empty()))
186                                 do_hit = false;
187                 }
188
189                 if (do_hit) {
190                         if(itemgroup_get(armor_groups, "immortal"))
191                                 do_hit = false;
192                 }
193         }
194
195         PunchDamageResult result;
196         if(do_hit)
197         {
198                 HitParams hitparams = getHitParams(armor_groups, toolcap,
199                                 time_from_last_punch);
200                 result.did_punch = true;
201                 result.wear = hitparams.wear;
202                 result.damage = hitparams.hp;
203         }
204
205         return result;
206 }
207
208