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 (const auto &groupcap : groupcaps) {
35 const std::string *name = &groupcap.first;
36 const ToolGroupCap *cap = &groupcap.second;
37 os << serializeString(*name);
38 writeS16(os, cap->uses);
39 writeS16(os, cap->maxlevel);
40 writeU32(os, cap->times.size());
41 for (const auto &time : cap->times) {
42 writeS16(os, time.first);
43 writeF1000(os, time.second);
47 writeU32(os, damageGroups.size());
49 for (const auto &damageGroup : damageGroups) {
50 os << serializeString(damageGroup.first);
51 writeS16(os, damageGroup.second);
55 void ToolCapabilities::deSerialize(std::istream &is)
57 int version = readU8(is);
58 if(version != 1 && version != 2) throw SerializationError(
59 "unsupported ToolCapabilities version");
60 full_punch_interval = readF1000(is);
61 max_drop_level = readS16(is);
63 u32 groupcaps_size = readU32(is);
64 for(u32 i=0; i<groupcaps_size; i++){
65 std::string name = deSerializeString(is);
67 cap.uses = readS16(is);
68 cap.maxlevel = readS16(is);
69 u32 times_size = readU32(is);
70 for(u32 i=0; i<times_size; i++){
71 int level = readS16(is);
72 float time = readF1000(is);
73 cap.times[level] = time;
75 groupcaps[name] = cap;
79 u32 damage_groups_size = readU32(is);
80 for(u32 i=0; i<damage_groups_size; i++){
81 std::string name = deSerializeString(is);
82 s16 rating = readS16(is);
83 damageGroups[name] = rating;
88 DigParams getDigParams(const ItemGroupList &groups,
89 const ToolCapabilities *tp, float time_from_last_punch)
91 //infostream<<"getDigParams"<<std::endl;
92 /* Check group dig_immediate */
93 switch(itemgroup_get(groups, "dig_immediate")){
95 //infostream<<"dig_immediate=2"<<std::endl;
96 return DigParams(true, 0.5, 0, "dig_immediate");
98 //infostream<<"dig_immediate=3"<<std::endl;
99 return DigParams(true, 0, 0, "dig_immediate");
104 // Values to be returned (with a bit of conversion)
105 bool result_diggable = false;
106 float result_time = 0.0;
107 float result_wear = 0.0;
108 std::string result_main_group;
110 int level = itemgroup_get(groups, "level");
111 //infostream<<"level="<<level<<std::endl;
112 for (const auto &groupcap : tp->groupcaps) {
113 const std::string &name = groupcap.first;
114 //infostream<<"group="<<name<<std::endl;
115 const ToolGroupCap &cap = groupcap.second;
116 int rating = itemgroup_get(groups, name);
118 bool time_exists = cap.getTime(rating, &time);
119 if(!result_diggable || time < result_time){
120 if(cap.maxlevel >= level && time_exists){
121 result_diggable = true;
122 int leveldiff = cap.maxlevel - level;
123 result_time = time / MYMAX(1, leveldiff);
125 result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
128 result_main_group = name;
132 //infostream<<"result_diggable="<<result_diggable<<std::endl;
133 //infostream<<"result_time="<<result_time<<std::endl;
134 //infostream<<"result_wear="<<result_wear<<std::endl;
136 if(time_from_last_punch < tp->full_punch_interval){
137 float f = time_from_last_punch / tp->full_punch_interval;
138 //infostream<<"f="<<f<<std::endl;
143 u16 wear_i = 65535.*result_wear;
144 return DigParams(result_diggable, result_time, wear_i, result_main_group);
147 DigParams getDigParams(const ItemGroupList &groups,
148 const ToolCapabilities *tp)
150 return getDigParams(groups, tp, 1000000);
153 HitParams getHitParams(const ItemGroupList &armor_groups,
154 const ToolCapabilities *tp, float time_from_last_punch)
157 float full_punch_interval = tp->full_punch_interval;
159 for (const auto &damageGroup : tp->damageGroups) {
160 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
161 damage += damageGroup.second
162 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
169 HitParams getHitParams(const ItemGroupList &armor_groups,
170 const ToolCapabilities *tp)
172 return getHitParams(armor_groups, tp, 1000000);
175 PunchDamageResult getPunchDamage(
176 const ItemGroupList &armor_groups,
177 const ToolCapabilities *toolcap,
178 const ItemStack *punchitem,
179 float time_from_last_punch
183 if (do_hit && punchitem) {
184 if (itemgroup_get(armor_groups, "punch_operable") &&
185 (toolcap == NULL || punchitem->name.empty()))
190 if(itemgroup_get(armor_groups, "immortal"))
195 PunchDamageResult result;
198 HitParams hitparams = getHitParams(armor_groups, toolcap,
199 time_from_last_punch);
200 result.did_punch = true;
201 result.wear = hitparams.wear;
202 result.damage = hitparams.hp;