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