]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tool.cpp
dig_immediate=3 instead of 1 means 0 seconds
[dragonfireclient.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, 1.0, 0);
81         case 3:
82                 //infostream<<"dig_immediate=3"<<std::endl;
83                 return DigParams(true, 0.0, 0);
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
93         int level = itemgroup_get(groups, "level");
94         //infostream<<"level="<<level<<std::endl;
95         for(std::map<std::string, ToolGroupCap>::const_iterator
96                         i = tp->groupcaps.begin(); i != tp->groupcaps.end(); i++){
97                 const std::string &name = i->first;
98                 //infostream<<"group="<<name<<std::endl;
99                 const ToolGroupCap &cap = i->second;
100                 int rating = itemgroup_get(groups, name);
101                 float time = 0;
102                 bool time_exists = cap.getTime(rating, &time);
103                 if(!result_diggable || time < result_time){
104                         if(cap.maxlevel > level && time_exists){
105                                 result_diggable = true;
106                                 result_time = time;
107                                 int leveldiff = cap.maxlevel - level;
108                                 result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
109                         }
110                 }
111         }
112         //infostream<<"result_diggable="<<result_diggable<<std::endl;
113         //infostream<<"result_time="<<result_time<<std::endl;
114         //infostream<<"result_wear="<<result_wear<<std::endl;
115
116         if(time_from_last_punch < tp->full_punch_interval){
117                 float f = time_from_last_punch / tp->full_punch_interval;
118                 //infostream<<"f="<<f<<std::endl;
119                 result_time /= f;
120                 result_wear /= f;
121         }
122
123         u16 wear_i = 65535.*result_wear;
124         return DigParams(result_diggable, result_time, wear_i);
125 }
126
127 DigParams getDigParams(const ItemGroupList &groups,
128                 const ToolCapabilities *tp)
129 {
130         return getDigParams(groups, tp, 1000000);
131 }
132
133 HitParams getHitParams(const ItemGroupList &groups,
134                 const ToolCapabilities *tp, float time_from_last_punch)
135 {
136         DigParams digprop = getDigParams(groups, tp,
137                         time_from_last_punch);
138         
139         if(time_from_last_punch > tp->full_punch_interval)
140                 time_from_last_punch = tp->full_punch_interval;
141         // Damage in hp is equivalent to nodes dug in time_from_last_punch
142         s16 hp = 0;
143         if(digprop.diggable)
144                 hp = time_from_last_punch / digprop.time;
145         // Wear is the same as for digging a single node
146         s16 wear = (float)digprop.wear;
147
148         return HitParams(hp, wear);
149 }
150
151 HitParams getHitParams(const ItemGroupList &groups,
152                 const ToolCapabilities *tp)
153 {
154         return getHitParams(groups, tp, 1000000);
155 }
156
157 PunchDamageResult getPunchDamage(
158                 const ItemGroupList &armor_groups,
159                 const ToolCapabilities *toolcap,
160                 const ItemStack *punchitem,
161                 float time_from_last_punch
162 ){
163         bool do_hit = true;
164         {
165                 if(do_hit && punchitem){
166                         if(itemgroup_get(armor_groups, "punch_operable") &&
167                                         (toolcap == NULL || punchitem->name == ""))
168                                 do_hit = false;
169                 }
170                 if(do_hit){
171                         if(itemgroup_get(armor_groups, "immortal"))
172                                 do_hit = false;
173                 }
174         }
175         
176         PunchDamageResult result;
177         if(do_hit)
178         {
179                 HitParams hitparams = getHitParams(armor_groups, toolcap,
180                                 time_from_last_punch);
181                 result.did_punch = true;
182                 result.wear = hitparams.wear;
183                 result.damage = hitparams.hp;
184         }
185
186         return result;
187 }
188
189