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