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 "itemgroup.h"
23 #include "inventory.h"
24 #include "exceptions.h"
25 #include "util/serialize.h"
26 #include "util/numeric.h"
28 void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
30 if(protocol_version <= 17)
31 writeU8(os, 1); // version
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);
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);
61 void ToolCapabilities::deSerialize(std::istream &is)
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);
69 u32 groupcaps_size = readU32(is);
70 for(u32 i=0; i<groupcaps_size; i++){
71 std::string name = deSerializeString(is);
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;
81 groupcaps[name] = cap;
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;
94 DigParams getDigParams(const ItemGroupList &groups,
95 const ToolCapabilities *tp, float time_from_last_punch)
97 //infostream<<"getDigParams"<<std::endl;
98 /* Check group dig_immediate */
99 switch(itemgroup_get(groups, "dig_immediate")){
101 //infostream<<"dig_immediate=2"<<std::endl;
102 return DigParams(true, 0.5, 0, "dig_immediate");
104 //infostream<<"dig_immediate=3"<<std::endl;
105 return DigParams(true, 0.0, 0, "dig_immediate");
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 = "";
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);
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);
132 result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
135 result_main_group = name;
139 //infostream<<"result_diggable="<<result_diggable<<std::endl;
140 //infostream<<"result_time="<<result_time<<std::endl;
141 //infostream<<"result_wear="<<result_wear<<std::endl;
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;
150 u16 wear_i = 65535.*result_wear;
151 return DigParams(result_diggable, result_time, wear_i, result_main_group);
154 DigParams getDigParams(const ItemGroupList &groups,
155 const ToolCapabilities *tp)
157 return getDigParams(groups, tp, 1000000);
160 HitParams getHitParams(const ItemGroupList &armor_groups,
161 const ToolCapabilities *tp, float time_from_last_punch)
164 float full_punch_interval = tp->full_punch_interval;
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)
173 return HitParams(damage, 0);
176 HitParams getHitParams(const ItemGroupList &armor_groups,
177 const ToolCapabilities *tp)
179 return getHitParams(armor_groups, tp, 1000000);
182 PunchDamageResult getPunchDamage(
183 const ItemGroupList &armor_groups,
184 const ToolCapabilities *toolcap,
185 const ItemStack *punchitem,
186 float time_from_last_punch
190 if(do_hit && punchitem){
191 if(itemgroup_get(armor_groups, "punch_operable") &&
192 (toolcap == NULL || punchitem->name == ""))
196 if(itemgroup_get(armor_groups, "immortal"))
201 PunchDamageResult result;
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;