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 writeU8(os, 2); // version (protocol >= 18)
31 writeF1000(os, full_punch_interval);
32 writeS16(os, max_drop_level);
33 writeU32(os, groupcaps.size());
34 for (ToolGCMap::const_iterator 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 (UNORDERED_MAP<int, float>::const_iterator
42 j = cap->times.begin(); j != cap->times.end(); ++j) {
43 writeS16(os, j->first);
44 writeF1000(os, j->second);
48 writeU32(os, damageGroups.size());
50 for (DamageGroup::const_iterator i = damageGroups.begin();
51 i != damageGroups.end(); ++i) {
52 os << serializeString(i->first);
53 writeS16(os, i->second);
57 void ToolCapabilities::deSerialize(std::istream &is)
59 int version = readU8(is);
60 if(version != 1 && version != 2) throw SerializationError(
61 "unsupported ToolCapabilities version");
62 full_punch_interval = readF1000(is);
63 max_drop_level = readS16(is);
65 u32 groupcaps_size = readU32(is);
66 for(u32 i=0; i<groupcaps_size; i++){
67 std::string name = deSerializeString(is);
69 cap.uses = readS16(is);
70 cap.maxlevel = readS16(is);
71 u32 times_size = readU32(is);
72 for(u32 i=0; i<times_size; i++){
73 int level = readS16(is);
74 float time = readF1000(is);
75 cap.times[level] = time;
77 groupcaps[name] = cap;
81 u32 damage_groups_size = readU32(is);
82 for(u32 i=0; i<damage_groups_size; i++){
83 std::string name = deSerializeString(is);
84 s16 rating = readS16(is);
85 damageGroups[name] = rating;
90 DigParams getDigParams(const ItemGroupList &groups,
91 const ToolCapabilities *tp, float time_from_last_punch)
93 //infostream<<"getDigParams"<<std::endl;
94 /* Check group dig_immediate */
95 switch(itemgroup_get(groups, "dig_immediate")){
97 //infostream<<"dig_immediate=2"<<std::endl;
98 return DigParams(true, 0.5, 0, "dig_immediate");
100 //infostream<<"dig_immediate=3"<<std::endl;
101 return DigParams(true, 0, 0, "dig_immediate");
106 // Values to be returned (with a bit of conversion)
107 bool result_diggable = false;
108 float result_time = 0.0;
109 float result_wear = 0.0;
110 std::string result_main_group = "";
112 int level = itemgroup_get(groups, "level");
113 //infostream<<"level="<<level<<std::endl;
114 for (ToolGCMap::const_iterator i = tp->groupcaps.begin();
115 i != tp->groupcaps.end(); ++i) {
116 const std::string &name = i->first;
117 //infostream<<"group="<<name<<std::endl;
118 const ToolGroupCap &cap = i->second;
119 int rating = itemgroup_get(groups, name);
121 bool time_exists = cap.getTime(rating, &time);
122 if(!result_diggable || time < result_time){
123 if(cap.maxlevel >= level && time_exists){
124 result_diggable = true;
125 int leveldiff = cap.maxlevel - level;
126 result_time = time / MYMAX(1, leveldiff);
128 result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
131 result_main_group = name;
135 //infostream<<"result_diggable="<<result_diggable<<std::endl;
136 //infostream<<"result_time="<<result_time<<std::endl;
137 //infostream<<"result_wear="<<result_wear<<std::endl;
139 if(time_from_last_punch < tp->full_punch_interval){
140 float f = time_from_last_punch / tp->full_punch_interval;
141 //infostream<<"f="<<f<<std::endl;
146 u16 wear_i = 65535.*result_wear;
147 return DigParams(result_diggable, result_time, wear_i, result_main_group);
150 DigParams getDigParams(const ItemGroupList &groups,
151 const ToolCapabilities *tp)
153 return getDigParams(groups, tp, 1000000);
156 HitParams getHitParams(const ItemGroupList &armor_groups,
157 const ToolCapabilities *tp, float time_from_last_punch)
160 float full_punch_interval = tp->full_punch_interval;
162 for (DamageGroup::const_iterator i = tp->damageGroups.begin();
163 i != tp->damageGroups.end(); ++i) {
164 s16 armor = itemgroup_get(armor_groups, i->first);
165 damage += i->second * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
169 return HitParams(damage, 0);
172 HitParams getHitParams(const ItemGroupList &armor_groups,
173 const ToolCapabilities *tp)
175 return getHitParams(armor_groups, tp, 1000000);
178 PunchDamageResult getPunchDamage(
179 const ItemGroupList &armor_groups,
180 const ToolCapabilities *toolcap,
181 const ItemStack *punchitem,
182 float time_from_last_punch
186 if(do_hit && punchitem){
187 if(itemgroup_get(armor_groups, "punch_operable") &&
188 (toolcap == NULL || punchitem->name == ""))
192 if(itemgroup_get(armor_groups, "immortal"))
197 PunchDamageResult result;
200 HitParams hitparams = getHitParams(armor_groups, toolcap,
201 time_from_last_punch);
202 result.did_punch = true;
203 result.wear = hitparams.wear;
204 result.damage = hitparams.hp;