]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tool.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[dragonfireclient.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 "itemgroup.h"
22 #include "log.h"
23 #include "inventory.h"
24 #include "exceptions.h"
25 #include "util/serialize.h"
26 #include "util/numeric.h"
27
28 void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
29 {
30         if(protocol_version <= 17)
31                 writeU8(os, 1); // version
32         else
33                 writeU8(os, 2); // version
34         writeF1000(os, full_punch_interval);
35         writeS16(os, max_drop_level);
36         writeU32(os, groupcaps.size());
37         for(std::map<std::string, ToolGroupCap>::const_iterator
38                         i = groupcaps.begin(); i != groupcaps.end(); i++){
39                 const std::string *name = &i->first;
40                 const ToolGroupCap *cap = &i->second;
41                 os<<serializeString(*name);
42                 writeS16(os, cap->uses);
43                 writeS16(os, cap->maxlevel);
44                 writeU32(os, cap->times.size());
45                 for(std::map<int, float>::const_iterator
46                                 i = cap->times.begin(); i != cap->times.end(); i++){
47                         writeS16(os, i->first);
48                         writeF1000(os, i->second);
49                 }
50         }
51         if(protocol_version > 17){
52                 writeU32(os, damageGroups.size());
53                 for(std::map<std::string, s16>::const_iterator
54                                 i = damageGroups.begin(); i != damageGroups.end(); i++){
55                         os<<serializeString(i->first);
56                         writeS16(os, i->second);
57                 }
58         }
59 }
60
61 void ToolCapabilities::deSerialize(std::istream &is)
62 {
63         int version = readU8(is);
64         if(version != 1 && version != 2) throw SerializationError(
65                         "unsupported ToolCapabilities version");
66         full_punch_interval = readF1000(is);
67         max_drop_level = readS16(is);
68         groupcaps.clear();
69         u32 groupcaps_size = readU32(is);
70         for(u32 i=0; i<groupcaps_size; i++){
71                 std::string name = deSerializeString(is);
72                 ToolGroupCap cap;
73                 cap.uses = readS16(is);
74                 cap.maxlevel = readS16(is);
75                 u32 times_size = readU32(is);
76                 for(u32 i=0; i<times_size; i++){
77                         int level = readS16(is);
78                         float time = readF1000(is);
79                         cap.times[level] = time;
80                 }
81                 groupcaps[name] = cap;
82         }
83         if(version == 2)
84         {
85                 u32 damage_groups_size = readU32(is);
86                 for(u32 i=0; i<damage_groups_size; i++){
87                         std::string name = deSerializeString(is);
88                         s16 rating = readS16(is);
89                         damageGroups[name] = rating;
90                 }
91         }
92 }
93
94 DigParams getDigParams(const ItemGroupList &groups,
95                 const ToolCapabilities *tp, float time_from_last_punch)
96 {
97         //infostream<<"getDigParams"<<std::endl;
98         /* Check group dig_immediate */
99         switch(itemgroup_get(groups, "dig_immediate")){
100         case 2:
101                 //infostream<<"dig_immediate=2"<<std::endl;
102                 return DigParams(true, 0.5, 0, "dig_immediate");
103         case 3:
104                 //infostream<<"dig_immediate=3"<<std::endl;
105                 return DigParams(true, 0.0, 0, "dig_immediate");
106         default:
107                 break;
108         }
109         
110         // Values to be returned (with a bit of conversion)
111         bool result_diggable = false;
112         float result_time = 0.0;
113         float result_wear = 0.0;
114         std::string result_main_group = "";
115
116         int level = itemgroup_get(groups, "level");
117         //infostream<<"level="<<level<<std::endl;
118         for(std::map<std::string, ToolGroupCap>::const_iterator
119                         i = tp->groupcaps.begin(); i != tp->groupcaps.end(); i++){
120                 const std::string &name = i->first;
121                 //infostream<<"group="<<name<<std::endl;
122                 const ToolGroupCap &cap = i->second;
123                 int rating = itemgroup_get(groups, name);
124                 float time = 0;
125                 bool time_exists = cap.getTime(rating, &time);
126                 if(!result_diggable || time < result_time){
127                         if(cap.maxlevel >= level && time_exists){
128                                 result_diggable = true;
129                                 int leveldiff = cap.maxlevel - level;
130                                 result_time = time / MYMAX(1, leveldiff);
131                                 if(cap.uses != 0)
132                                         result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
133                                 else
134                                         result_wear = 0;
135                                 result_main_group = name;
136                         }
137                 }
138         }
139         //infostream<<"result_diggable="<<result_diggable<<std::endl;
140         //infostream<<"result_time="<<result_time<<std::endl;
141         //infostream<<"result_wear="<<result_wear<<std::endl;
142
143         if(time_from_last_punch < tp->full_punch_interval){
144                 float f = time_from_last_punch / tp->full_punch_interval;
145                 //infostream<<"f="<<f<<std::endl;
146                 result_time /= f;
147                 result_wear /= f;
148         }
149
150         u16 wear_i = 65535.*result_wear;
151         return DigParams(result_diggable, result_time, wear_i, result_main_group);
152 }
153
154 DigParams getDigParams(const ItemGroupList &groups,
155                 const ToolCapabilities *tp)
156 {
157         return getDigParams(groups, tp, 1000000);
158 }
159
160 HitParams getHitParams(const ItemGroupList &armor_groups,
161                 const ToolCapabilities *tp, float time_from_last_punch)
162 {
163         s16 damage = 0;
164         float full_punch_interval = tp->full_punch_interval;
165
166         for(std::map<std::string, s16>::const_iterator
167                         i = tp->damageGroups.begin(); i != tp->damageGroups.end(); i++){
168                 s16 armor = itemgroup_get(armor_groups, i->first);
169                 damage += i->second * rangelim(time_from_last_punch * full_punch_interval, 0.0, 1.0)
170                                 * armor / 100.0;
171         }
172
173         return HitParams(damage, 0);
174 }
175
176 HitParams getHitParams(const ItemGroupList &armor_groups,
177                 const ToolCapabilities *tp)
178 {
179         return getHitParams(armor_groups, tp, 1000000);
180 }
181
182 PunchDamageResult getPunchDamage(
183                 const ItemGroupList &armor_groups,
184                 const ToolCapabilities *toolcap,
185                 const ItemStack *punchitem,
186                 float time_from_last_punch
187 ){
188         bool do_hit = true;
189         {
190                 if(do_hit && punchitem){
191                         if(itemgroup_get(armor_groups, "punch_operable") &&
192                                         (toolcap == NULL || punchitem->name == ""))
193                                 do_hit = false;
194                 }
195                 if(do_hit){
196                         if(itemgroup_get(armor_groups, "immortal"))
197                                 do_hit = false;
198                 }
199         }
200         
201         PunchDamageResult result;
202         if(do_hit)
203         {
204                 HitParams hitparams = getHitParams(armor_groups, toolcap,
205                                 time_from_last_punch);
206                 result.did_punch = true;
207                 result.wear = hitparams.wear;
208                 result.damage = hitparams.hp;
209         }
210
211         return result;
212 }
213
214