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 ToolGroupCap::toJson(Json::Value &object) const
30 object["maxlevel"] = maxlevel;
31 object["uses"] = uses;
33 Json::Value times_object;
34 for (auto time : times)
35 times_object[time.first] = time.second;
36 object["times"] = times_object;
39 void ToolGroupCap::fromJson(const Json::Value &json)
41 if (json.isObject()) {
42 if (json["maxlevel"].isInt())
43 maxlevel = json["maxlevel"].asInt();
44 if (json["uses"].isInt())
45 uses = json["uses"].asInt();
46 const Json::Value ×_object = json["times"];
47 if (times_object.isArray()) {
48 Json::ArrayIndex size = times_object.size();
49 for (Json::ArrayIndex i = 0; i < size; ++i)
50 if (times_object[i].isDouble())
51 times[i] = times_object[i].asFloat();
56 void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
58 writeU8(os, 3); // protocol_version >= 36
59 writeF1000(os, full_punch_interval);
60 writeS16(os, max_drop_level);
61 writeU32(os, groupcaps.size());
62 for (const auto &groupcap : groupcaps) {
63 const std::string *name = &groupcap.first;
64 const ToolGroupCap *cap = &groupcap.second;
65 os << serializeString(*name);
66 writeS16(os, cap->uses);
67 writeS16(os, cap->maxlevel);
68 writeU32(os, cap->times.size());
69 for (const auto &time : cap->times) {
70 writeS16(os, time.first);
71 writeF1000(os, time.second);
75 writeU32(os, damageGroups.size());
77 for (const auto &damageGroup : damageGroups) {
78 os << serializeString(damageGroup.first);
79 writeS16(os, damageGroup.second);
83 void ToolCapabilities::deSerialize(std::istream &is)
85 int version = readU8(is);
87 throw SerializationError("unsupported ToolCapabilities version");
89 full_punch_interval = readF1000(is);
90 max_drop_level = readS16(is);
92 u32 groupcaps_size = readU32(is);
93 for (u32 i = 0; i < groupcaps_size; i++) {
94 std::string name = deSerializeString(is);
96 cap.uses = readS16(is);
97 cap.maxlevel = readS16(is);
98 u32 times_size = readU32(is);
99 for(u32 i = 0; i < times_size; i++) {
100 int level = readS16(is);
101 float time = readF1000(is);
102 cap.times[level] = time;
104 groupcaps[name] = cap;
107 u32 damage_groups_size = readU32(is);
108 for (u32 i = 0; i < damage_groups_size; i++) {
109 std::string name = deSerializeString(is);
110 s16 rating = readS16(is);
111 damageGroups[name] = rating;
115 void ToolCapabilities::serializeJson(std::ostream &os) const
118 root["full_punch_interval"] = full_punch_interval;
119 root["max_drop_level"] = max_drop_level;
121 Json::Value groupcaps_object;
122 for (auto groupcap : groupcaps) {
123 groupcap.second.toJson(groupcaps_object[groupcap.first]);
125 root["groupcaps"] = groupcaps_object;
127 Json::Value damage_groups_object;
128 DamageGroup::const_iterator dgiter;
129 for (dgiter = damageGroups.begin(); dgiter != damageGroups.end(); ++dgiter) {
130 damage_groups_object[dgiter->first] = dgiter->second;
132 root["damage_groups"] = damage_groups_object;
137 void ToolCapabilities::deserializeJson(std::istream &is)
141 if (root.isObject()) {
142 if (root["full_punch_interval"].isDouble())
143 full_punch_interval = root["full_punch_interval"].asFloat();
144 if (root["max_drop_level"].isInt())
145 max_drop_level = root["max_drop_level"].asInt();
147 Json::Value &groupcaps_object = root["groupcaps"];
148 if (groupcaps_object.isObject()) {
149 Json::ValueIterator gciter;
150 for (gciter = groupcaps_object.begin();
151 gciter != groupcaps_object.end(); ++gciter) {
152 ToolGroupCap groupcap;
153 groupcap.fromJson(*gciter);
154 groupcaps[gciter.key().asString()] = groupcap;
158 Json::Value &damage_groups_object = root["damage_groups"];
159 if (damage_groups_object.isObject()) {
160 Json::ValueIterator dgiter;
161 for (dgiter = damage_groups_object.begin();
162 dgiter != damage_groups_object.end(); ++dgiter) {
163 Json::Value &value = *dgiter;
165 damageGroups[dgiter.key().asString()] =
172 DigParams getDigParams(const ItemGroupList &groups,
173 const ToolCapabilities *tp)
175 // Group dig_immediate has fixed time and no wear
176 switch (itemgroup_get(groups, "dig_immediate")) {
178 return DigParams(true, 0.5, 0, "dig_immediate");
180 return DigParams(true, 0, 0, "dig_immediate");
185 // Values to be returned (with a bit of conversion)
186 bool result_diggable = false;
187 float result_time = 0.0;
188 float result_wear = 0.0;
189 std::string result_main_group;
191 int level = itemgroup_get(groups, "level");
192 for (const auto &groupcap : tp->groupcaps) {
193 const ToolGroupCap &cap = groupcap.second;
195 int leveldiff = cap.maxlevel - level;
199 const std::string &groupname = groupcap.first;
201 int rating = itemgroup_get(groups, groupname);
202 bool time_exists = cap.getTime(rating, &time);
208 if (!result_diggable || time < result_time) {
210 result_diggable = true;
212 result_wear = 1.0 / cap.uses / pow(3.0, leveldiff);
215 result_main_group = groupname;
219 u16 wear_i = U16_MAX * result_wear;
220 return DigParams(result_diggable, result_time, wear_i, result_main_group);
223 HitParams getHitParams(const ItemGroupList &armor_groups,
224 const ToolCapabilities *tp, float time_from_last_punch)
227 float full_punch_interval = tp->full_punch_interval;
229 for (const auto &damageGroup : tp->damageGroups) {
230 s16 armor = itemgroup_get(armor_groups, damageGroup.first);
231 damage += damageGroup.second
232 * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
239 HitParams getHitParams(const ItemGroupList &armor_groups,
240 const ToolCapabilities *tp)
242 return getHitParams(armor_groups, tp, 1000000);
245 PunchDamageResult getPunchDamage(
246 const ItemGroupList &armor_groups,
247 const ToolCapabilities *toolcap,
248 const ItemStack *punchitem,
249 float time_from_last_punch
253 if (do_hit && punchitem) {
254 if (itemgroup_get(armor_groups, "punch_operable") &&
255 (toolcap == NULL || punchitem->name.empty()))
260 if(itemgroup_get(armor_groups, "immortal"))
265 PunchDamageResult result;
268 HitParams hitparams = getHitParams(armor_groups, toolcap,
269 time_from_last_punch);
270 result.did_punch = true;
271 result.wear = hitparams.wear;
272 result.damage = hitparams.hp;