Tune caves
[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, 0); // 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                 writeF1000(os, cap->maxwear);
38                 writeF1000(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 != 0) 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.maxwear = readF1000(is);
61                 cap.maxlevel = readF1000(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                                 result_time = time;
108                                 int leveldiff = cap.maxlevel - level;
109                                 result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
110                                 result_main_group = name;
111                         }
112                 }
113         }
114         //infostream<<"result_diggable="<<result_diggable<<std::endl;
115         //infostream<<"result_time="<<result_time<<std::endl;
116         //infostream<<"result_wear="<<result_wear<<std::endl;
117
118         if(time_from_last_punch < tp->full_punch_interval){
119                 float f = time_from_last_punch / tp->full_punch_interval;
120                 //infostream<<"f="<<f<<std::endl;
121                 result_time /= f;
122                 result_wear /= f;
123         }
124
125         u16 wear_i = 65535.*result_wear;
126         return DigParams(result_diggable, result_time, wear_i, result_main_group);
127 }
128
129 DigParams getDigParams(const ItemGroupList &groups,
130                 const ToolCapabilities *tp)
131 {
132         return getDigParams(groups, tp, 1000000);
133 }
134
135 HitParams getHitParams(const ItemGroupList &groups,
136                 const ToolCapabilities *tp, float time_from_last_punch)
137 {
138         DigParams digprop = getDigParams(groups, tp,
139                         time_from_last_punch);
140         
141         if(time_from_last_punch > tp->full_punch_interval)
142                 time_from_last_punch = tp->full_punch_interval;
143         // Damage in hp is equivalent to nodes dug in time_from_last_punch
144         s16 hp = 0;
145         if(digprop.diggable)
146                 hp = time_from_last_punch / digprop.time;
147         // Wear is the same as for digging a single node
148         s16 wear = (float)digprop.wear;
149
150         return HitParams(hp, wear, digprop.main_group);
151 }
152
153 HitParams getHitParams(const ItemGroupList &groups,
154                 const ToolCapabilities *tp)
155 {
156         return getHitParams(groups, tp, 1000000);
157 }
158
159 PunchDamageResult getPunchDamage(
160                 const ItemGroupList &armor_groups,
161                 const ToolCapabilities *toolcap,
162                 const ItemStack *punchitem,
163                 float time_from_last_punch
164 ){
165         bool do_hit = true;
166         {
167                 if(do_hit && punchitem){
168                         if(itemgroup_get(armor_groups, "punch_operable") &&
169                                         (toolcap == NULL || punchitem->name == ""))
170                                 do_hit = false;
171                 }
172                 if(do_hit){
173                         if(itemgroup_get(armor_groups, "immortal"))
174                                 do_hit = false;
175                 }
176         }
177         
178         PunchDamageResult result;
179         if(do_hit)
180         {
181                 HitParams hitparams = getHitParams(armor_groups, toolcap,
182                                 time_from_last_punch);
183                 result.did_punch = true;
184                 result.wear = hitparams.wear;
185                 result.damage = hitparams.hp;
186                 result.main_group = hitparams.main_group;
187         }
188
189         return result;
190 }
191
192