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=1"<<std::endl;
80 return DigParams(true, 0.0, 0);
82 //infostream<<"dig_immediate=2"<<std::endl;
83 return DigParams(true, 1.0, 0);
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;
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);
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;
107 int leveldiff = cap.maxlevel - level;
108 result_wear = cap.maxwear / pow(4.0, (double)leveldiff);
112 //infostream<<"result_diggable="<<result_diggable<<std::endl;
113 //infostream<<"result_time="<<result_time<<std::endl;
114 //infostream<<"result_wear="<<result_wear<<std::endl;
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;
123 u16 wear_i = 65535.*result_wear;
124 return DigParams(result_diggable, result_time, wear_i);
127 DigParams getDigParams(const ItemGroupList &groups,
128 const ToolCapabilities *tp)
130 return getDigParams(groups, tp, 1000000);
133 HitParams getHitParams(const ItemGroupList &groups,
134 const ToolCapabilities *tp, float time_from_last_punch)
136 DigParams digprop = getDigParams(groups, tp,
137 time_from_last_punch);
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
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;
148 return HitParams(hp, wear);
151 HitParams getHitParams(const ItemGroupList &groups,
152 const ToolCapabilities *tp)
154 return getHitParams(groups, tp, 1000000);
157 PunchDamageResult getPunchDamage(
158 const ItemGroupList &armor_groups,
159 const ToolCapabilities *toolcap,
160 const ItemStack *punchitem,
161 float time_from_last_punch
165 if(do_hit && punchitem){
166 if(itemgroup_get(armor_groups, "punch_operable") &&
167 (toolcap == NULL || punchitem->name == ""))
171 if(itemgroup_get(armor_groups, "immortal"))
176 PunchDamageResult result;
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;