]> git.lizzy.rs Git - minetest.git/blob - src/tool.cpp
Small fix in getHitParams
[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
25 void ToolCapabilities::serialize(std::ostream &os) const
26 {
27         writeU8(os, 0); // version
28         writeF1000(os, full_punch_interval);
29         writeS16(os, max_drop_level);
30         writeU32(os, groupcaps.size());
31         for(std::map<std::string, ToolGroupCap>::const_iterator
32                         i = groupcaps.begin(); i != groupcaps.end(); i++){
33                 const std::string *name = &i->first;
34                 const ToolGroupCap *cap = &i->second;
35                 os<<serializeString(*name);
36                 writeF1000(os, cap->maxwear);
37                 writeF1000(os, cap->maxlevel);
38                 writeU32(os, cap->times.size());
39                 for(std::map<int, float>::const_iterator
40                                 i = cap->times.begin(); i != cap->times.end(); i++){
41                         writeS16(os, i->first);
42                         writeF1000(os, i->second);
43                 }
44         }
45 }
46
47 void ToolCapabilities::deSerialize(std::istream &is)
48 {
49         int version = readU8(is);
50         if(version != 0) throw SerializationError(
51                         "unsupported ToolCapabilities version");
52         full_punch_interval = readF1000(is);
53         max_drop_level = readS16(is);
54         groupcaps.clear();
55         u32 groupcaps_size = readU32(is);
56         for(u32 i=0; i<groupcaps_size; i++){
57                 std::string name = deSerializeString(is);
58                 ToolGroupCap cap;
59                 cap.maxwear = readF1000(is);
60                 cap.maxlevel = readF1000(is);
61                 u32 times_size = readU32(is);
62                 for(u32 i=0; i<times_size; i++){
63                         int level = readS16(is);
64                         float time = readF1000(is);
65                         cap.times[level] = time;
66                 }
67                 groupcaps[name] = cap;
68         }
69 }
70
71 DigParams getDigParams(const ItemGroupList &groups,
72                 const ToolCapabilities *tp, float time_from_last_punch)
73 {
74         //infostream<<"getDigParams"<<std::endl;
75         /* Check group dig_immediate */
76         switch(itemgroup_get(groups, "dig_immediate")){
77         case 1:
78                 //infostream<<"dig_immediate=1"<<std::endl;
79                 return DigParams(true, 0.0, 0);
80         case 2:
81                 //infostream<<"dig_immediate=2"<<std::endl;
82                 return DigParams(true, 1.0, 0);
83         default:
84                 break;
85         }
86         
87         // Values to be returned (with a bit of conversion)
88         bool result_diggable = false;
89         float result_time = 0.0;
90         float result_wear = 0.0;
91
92         int level = itemgroup_get(groups, "level");
93         //infostream<<"level="<<level<<std::endl;
94         for(std::map<std::string, ToolGroupCap>::const_iterator
95                         i = tp->groupcaps.begin(); i != tp->groupcaps.end(); i++){
96                 const std::string &name = i->first;
97                 //infostream<<"group="<<name<<std::endl;
98                 const ToolGroupCap &cap = i->second;
99                 int rating = itemgroup_get(groups, name);
100                 float time = 0;
101                 bool time_exists = cap.getTime(rating, &time);
102                 if(!result_diggable || time < result_time){
103                         if(cap.maxlevel > level && time_exists){
104                                 result_diggable = true;
105                                 result_time = time;
106                                 int leveldiff = cap.maxlevel - level;
107                                 result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
108                         }
109                 }
110         }
111         //infostream<<"result_diggable="<<result_diggable<<std::endl;
112         //infostream<<"result_time="<<result_time<<std::endl;
113         //infostream<<"result_wear="<<result_wear<<std::endl;
114
115         if(time_from_last_punch < tp->full_punch_interval){
116                 float f = time_from_last_punch / tp->full_punch_interval;
117                 //infostream<<"f="<<f<<std::endl;
118                 result_time /= f;
119                 result_wear /= f;
120         }
121
122         u16 wear_i = 65535.*result_wear;
123         return DigParams(result_diggable, result_time, wear_i);
124 }
125
126 DigParams getDigParams(const ItemGroupList &groups,
127                 const ToolCapabilities *tp)
128 {
129         return getDigParams(groups, tp, 1000000);
130 }
131
132 HitParams getHitParams(const ItemGroupList &groups,
133                 const ToolCapabilities *tp, float time_from_last_punch)
134 {
135         DigParams digprop = getDigParams(groups, tp,
136                         time_from_last_punch);
137         
138         if(time_from_last_punch > tp->full_punch_interval)
139                 time_from_last_punch = tp->full_punch_interval;
140         // Damage in hp is equivalent to nodes dug in time_from_last_punch
141         s16 hp = 0;
142         if(digprop.diggable)
143                 hp = time_from_last_punch / digprop.time;
144         // Wear is the same as for digging a single node
145         s16 wear = (float)digprop.wear;
146
147         return HitParams(hp, wear);
148 }
149
150 HitParams getHitParams(const ItemGroupList &groups,
151                 const ToolCapabilities *tp)
152 {
153         return getHitParams(groups, tp, 1000000);
154 }
155