3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "itemdef.h" // For itemgroup_get()
23 #include "inventory.h"
24 #include "util/serialize.h"
25 #include "util/numeric.h"
27 void ToolCapabilities::serialize(std::ostream &os) const
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);
49 void ToolCapabilities::deSerialize(std::istream &is)
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);
57 u32 groupcaps_size = readU32(is);
58 for(u32 i=0; i<groupcaps_size; i++){
59 std::string name = deSerializeString(is);
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;
69 groupcaps[name] = cap;
73 DigParams getDigParams(const ItemGroupList &groups,
74 const ToolCapabilities *tp, float time_from_last_punch)
76 //infostream<<"getDigParams"<<std::endl;
77 /* Check group dig_immediate */
78 switch(itemgroup_get(groups, "dig_immediate")){
80 //infostream<<"dig_immediate=2"<<std::endl;
81 return DigParams(true, 0.5, 0, "dig_immediate");
83 //infostream<<"dig_immediate=3"<<std::endl;
84 return DigParams(true, 0.0, 0, "dig_immediate");
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 = "";
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);
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);
111 result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
114 result_main_group = name;
118 //infostream<<"result_diggable="<<result_diggable<<std::endl;
119 //infostream<<"result_time="<<result_time<<std::endl;
120 //infostream<<"result_wear="<<result_wear<<std::endl;
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;
129 u16 wear_i = 65535.*result_wear;
130 return DigParams(result_diggable, result_time, wear_i, result_main_group);
133 DigParams getDigParams(const ItemGroupList &groups,
134 const ToolCapabilities *tp)
136 return getDigParams(groups, tp, 1000000);
139 HitParams getHitParams(const ItemGroupList &groups,
140 const ToolCapabilities *tp, float time_from_last_punch)
142 DigParams digprop = getDigParams(groups, tp,
143 time_from_last_punch);
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
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;
154 return HitParams(hp, wear, digprop.main_group);
157 HitParams getHitParams(const ItemGroupList &groups,
158 const ToolCapabilities *tp)
160 return getHitParams(groups, tp, 1000000);
163 PunchDamageResult getPunchDamage(
164 const ItemGroupList &armor_groups,
165 const ToolCapabilities *toolcap,
166 const ItemStack *punchitem,
167 float time_from_last_punch
171 if(do_hit && punchitem){
172 if(itemgroup_get(armor_groups, "punch_operable") &&
173 (toolcap == NULL || punchitem->name == ""))
177 if(itemgroup_get(armor_groups, "immortal"))
182 PunchDamageResult result;
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;