3 Copyright (C) 2011 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 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.
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.
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.
22 #include "itemdef.h" // For itemgroup_get()
24 #include "inventory.h"
26 void ToolCapabilities::serialize(std::ostream &os) const
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);
48 void ToolCapabilities::deSerialize(std::istream &is)
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);
56 u32 groupcaps_size = readU32(is);
57 for(u32 i=0; i<groupcaps_size; i++){
58 std::string name = deSerializeString(is);
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;
68 groupcaps[name] = cap;
72 DigParams getDigParams(const ItemGroupList &groups,
73 const ToolCapabilities *tp, float time_from_last_punch)
75 //infostream<<"getDigParams"<<std::endl;
76 /* Check group dig_immediate */
77 switch(itemgroup_get(groups, "dig_immediate")){
79 //infostream<<"dig_immediate=2"<<std::endl;
80 return DigParams(true, 0.5, 0, "dig_immediate");
82 //infostream<<"dig_immediate=3"<<std::endl;
83 return DigParams(true, 0.0, 0, "dig_immediate");
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 = "";
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);
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;
108 int leveldiff = cap.maxlevel - level;
109 result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
110 result_main_group = name;
114 //infostream<<"result_diggable="<<result_diggable<<std::endl;
115 //infostream<<"result_time="<<result_time<<std::endl;
116 //infostream<<"result_wear="<<result_wear<<std::endl;
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;
125 u16 wear_i = 65535.*result_wear;
126 return DigParams(result_diggable, result_time, wear_i, result_main_group);
129 DigParams getDigParams(const ItemGroupList &groups,
130 const ToolCapabilities *tp)
132 return getDigParams(groups, tp, 1000000);
135 HitParams getHitParams(const ItemGroupList &groups,
136 const ToolCapabilities *tp, float time_from_last_punch)
138 DigParams digprop = getDigParams(groups, tp,
139 time_from_last_punch);
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
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;
150 return HitParams(hp, wear, digprop.main_group);
153 HitParams getHitParams(const ItemGroupList &groups,
154 const ToolCapabilities *tp)
156 return getHitParams(groups, tp, 1000000);
159 PunchDamageResult getPunchDamage(
160 const ItemGroupList &armor_groups,
161 const ToolCapabilities *toolcap,
162 const ItemStack *punchitem,
163 float time_from_last_punch
167 if(do_hit && punchitem){
168 if(itemgroup_get(armor_groups, "punch_operable") &&
169 (toolcap == NULL || punchitem->name == ""))
173 if(itemgroup_get(armor_groups, "immortal"))
178 PunchDamageResult result;
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;