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