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