tool.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "tool.h"
  17. #include "itemdef.h"
  18. #include "itemgroup.h"
  19. #include "log.h"
  20. #include "inventory.h"
  21. #include "exceptions.h"
  22. #include "convert_json.h"
  23. #include "util/serialize.h"
  24. #include "util/numeric.h"
  25. void ToolGroupCap::toJson(Json::Value &object) const
  26. {
  27. object["maxlevel"] = maxlevel;
  28. object["uses"] = uses;
  29. Json::Value times_object;
  30. for (auto time : times)
  31. times_object[time.first] = time.second;
  32. object["times"] = times_object;
  33. }
  34. void ToolGroupCap::fromJson(const Json::Value &json)
  35. {
  36. if (json.isObject()) {
  37. if (json["maxlevel"].isInt())
  38. maxlevel = json["maxlevel"].asInt();
  39. if (json["uses"].isInt())
  40. uses = json["uses"].asInt();
  41. const Json::Value &times_object = json["times"];
  42. if (times_object.isArray()) {
  43. Json::ArrayIndex size = times_object.size();
  44. for (Json::ArrayIndex i = 0; i < size; ++i)
  45. if (times_object[i].isDouble())
  46. times[i] = times_object[i].asFloat();
  47. }
  48. }
  49. }
  50. void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
  51. {
  52. if (protocol_version >= 38)
  53. writeU8(os, 5);
  54. else
  55. writeU8(os, 4); // proto == 37
  56. writeF32(os, full_punch_interval);
  57. writeS16(os, max_drop_level);
  58. writeU32(os, groupcaps.size());
  59. for (const auto &groupcap : groupcaps) {
  60. const std::string *name = &groupcap.first;
  61. const ToolGroupCap *cap = &groupcap.second;
  62. os << serializeString16(*name);
  63. writeS16(os, cap->uses);
  64. writeS16(os, cap->maxlevel);
  65. writeU32(os, cap->times.size());
  66. for (const auto &time : cap->times) {
  67. writeS16(os, time.first);
  68. writeF32(os, time.second);
  69. }
  70. }
  71. writeU32(os, damageGroups.size());
  72. for (const auto &damageGroup : damageGroups) {
  73. os << serializeString16(damageGroup.first);
  74. writeS16(os, damageGroup.second);
  75. }
  76. if (protocol_version >= 38)
  77. writeU16(os, rangelim(punch_attack_uses, 0, U16_MAX));
  78. }
  79. void ToolCapabilities::deSerialize(std::istream &is)
  80. {
  81. int version = readU8(is);
  82. if (version < 4)
  83. throw SerializationError("unsupported ToolCapabilities version");
  84. full_punch_interval = readF32(is);
  85. max_drop_level = readS16(is);
  86. groupcaps.clear();
  87. u32 groupcaps_size = readU32(is);
  88. for (u32 i = 0; i < groupcaps_size; i++) {
  89. std::string name = deSerializeString16(is);
  90. ToolGroupCap cap;
  91. cap.uses = readS16(is);
  92. cap.maxlevel = readS16(is);
  93. u32 times_size = readU32(is);
  94. for(u32 i = 0; i < times_size; i++) {
  95. int level = readS16(is);
  96. float time = readF32(is);
  97. cap.times[level] = time;
  98. }
  99. groupcaps[name] = cap;
  100. }
  101. u32 damage_groups_size = readU32(is);
  102. for (u32 i = 0; i < damage_groups_size; i++) {
  103. std::string name = deSerializeString16(is);
  104. s16 rating = readS16(is);
  105. damageGroups[name] = rating;
  106. }
  107. if (version >= 5)
  108. punch_attack_uses = readU16(is);
  109. }
  110. void ToolCapabilities::serializeJson(std::ostream &os) const
  111. {
  112. Json::Value root;
  113. root["full_punch_interval"] = full_punch_interval;
  114. root["max_drop_level"] = max_drop_level;
  115. root["punch_attack_uses"] = punch_attack_uses;
  116. Json::Value groupcaps_object;
  117. for (const auto &groupcap : groupcaps) {
  118. groupcap.second.toJson(groupcaps_object[groupcap.first]);
  119. }
  120. root["groupcaps"] = groupcaps_object;
  121. Json::Value damage_groups_object;
  122. DamageGroup::const_iterator dgiter;
  123. for (dgiter = damageGroups.begin(); dgiter != damageGroups.end(); ++dgiter) {
  124. damage_groups_object[dgiter->first] = dgiter->second;
  125. }
  126. root["damage_groups"] = damage_groups_object;
  127. fastWriteJson(root, os);
  128. }
  129. void ToolCapabilities::deserializeJson(std::istream &is)
  130. {
  131. Json::Value root;
  132. is >> root;
  133. if (root.isObject()) {
  134. if (root["full_punch_interval"].isDouble())
  135. full_punch_interval = root["full_punch_interval"].asFloat();
  136. if (root["max_drop_level"].isInt())
  137. max_drop_level = root["max_drop_level"].asInt();
  138. if (root["punch_attack_uses"].isInt())
  139. punch_attack_uses = root["punch_attack_uses"].asInt();
  140. Json::Value &groupcaps_object = root["groupcaps"];
  141. if (groupcaps_object.isObject()) {
  142. Json::ValueIterator gciter;
  143. for (gciter = groupcaps_object.begin();
  144. gciter != groupcaps_object.end(); ++gciter) {
  145. ToolGroupCap groupcap;
  146. groupcap.fromJson(*gciter);
  147. groupcaps[gciter.key().asString()] = groupcap;
  148. }
  149. }
  150. Json::Value &damage_groups_object = root["damage_groups"];
  151. if (damage_groups_object.isObject()) {
  152. Json::ValueIterator dgiter;
  153. for (dgiter = damage_groups_object.begin();
  154. dgiter != damage_groups_object.end(); ++dgiter) {
  155. Json::Value &value = *dgiter;
  156. if (value.isInt())
  157. damageGroups[dgiter.key().asString()] =
  158. value.asInt();
  159. }
  160. }
  161. }
  162. }
  163. u32 calculateResultWear(const u32 uses, const u16 initial_wear)
  164. {
  165. if (uses == 0) {
  166. // Trivial case: Infinite uses
  167. return 0;
  168. }
  169. /* Finite uses. This is not trivial,
  170. as the maximum wear is not neatly evenly divisible by
  171. most possible uses numbers. For example, for 128
  172. uses, the calculation of wear is trivial, as
  173. 65536 / 128 uses = 512 wear,
  174. so the tool will get 512 wear 128 times in its lifetime.
  175. But for a number like 130, this does not work:
  176. 65536 / 130 uses = 504.123... wear.
  177. Since wear must be an integer, we will get
  178. 504*130 = 65520, which would lead to the wrong number
  179. of uses.
  180. Instead, we partition the "wear range" into blocks:
  181. A block represents a single use and can be
  182. of two possible sizes: normal and oversized.
  183. A normal block is equal to floor(65536 / uses).
  184. An oversized block is a normal block plus 1.
  185. Then we determine how many oversized and normal
  186. blocks we need and finally, whether we add
  187. the normal wear or the oversized wear.
  188. Example for 130 uses:
  189. * Normal wear = 504
  190. * Number of normal blocks = 114
  191. * Oversized wear = 505
  192. * Number of oversized blocks = 16
  193. If we add everything together, we get:
  194. 114*504 + 16*505 = 65536
  195. */
  196. u32 result_wear;
  197. u32 wear_normal = ((U16_MAX+1) / uses);
  198. // Will be non-zero if its not evenly divisible
  199. u16 blocks_oversize = (U16_MAX+1) % uses;
  200. // Whether to add one extra wear point in case
  201. // of oversized wear.
  202. u16 wear_extra = 0;
  203. if (blocks_oversize > 0) {
  204. u16 blocks_normal = uses - blocks_oversize;
  205. /* When the wear has reached this value, we
  206. know that wear_normal has been applied
  207. for blocks_normal times, therefore,
  208. only oversized blocks remain.
  209. This also implies the raw tool wear number
  210. increases a bit faster after this point,
  211. but this should be barely noticable by the
  212. player.
  213. */
  214. u16 wear_extra_at = blocks_normal * wear_normal;
  215. if (initial_wear >= wear_extra_at) {
  216. wear_extra = 1;
  217. }
  218. }
  219. result_wear = wear_normal + wear_extra;
  220. return result_wear;
  221. }
  222. DigParams getDigParams(const ItemGroupList &groups,
  223. const ToolCapabilities *tp,
  224. const u16 initial_wear)
  225. {
  226. // Group dig_immediate defaults to fixed time and no wear
  227. if (tp->groupcaps.find("dig_immediate") == tp->groupcaps.cend()) {
  228. switch (itemgroup_get(groups, "dig_immediate")) {
  229. case 2:
  230. return DigParams(true, 0.5, 0, "dig_immediate");
  231. case 3:
  232. return DigParams(true, 0, 0, "dig_immediate");
  233. default:
  234. break;
  235. }
  236. }
  237. // Values to be returned (with a bit of conversion)
  238. bool result_diggable = false;
  239. float result_time = 0.0;
  240. u32 result_wear = 0;
  241. std::string result_main_group;
  242. int level = itemgroup_get(groups, "level");
  243. for (const auto &groupcap : tp->groupcaps) {
  244. const ToolGroupCap &cap = groupcap.second;
  245. int leveldiff = cap.maxlevel - level;
  246. if (leveldiff < 0)
  247. continue;
  248. const std::string &groupname = groupcap.first;
  249. float time = 0;
  250. int rating = itemgroup_get(groups, groupname);
  251. bool time_exists = cap.getTime(rating, &time);
  252. if (!time_exists)
  253. continue;
  254. if (leveldiff > 1)
  255. time /= leveldiff;
  256. if (!result_diggable || time < result_time) {
  257. result_time = time;
  258. result_diggable = true;
  259. // The actual number of uses increases
  260. // exponentially with leveldiff.
  261. // If the levels are equal, real_uses equals cap.uses.
  262. u32 real_uses = cap.uses * pow(3.0, leveldiff);
  263. real_uses = MYMIN(real_uses, U16_MAX);
  264. result_wear = calculateResultWear(real_uses, initial_wear);
  265. result_main_group = groupname;
  266. }
  267. }
  268. return DigParams(result_diggable, result_time, result_wear, result_main_group);
  269. }
  270. HitParams getHitParams(const ItemGroupList &armor_groups,
  271. const ToolCapabilities *tp, float time_from_last_punch,
  272. u16 initial_wear)
  273. {
  274. s32 damage = 0;
  275. float result_wear = 0.0f;
  276. float punch_interval_multiplier =
  277. rangelim(time_from_last_punch / tp->full_punch_interval, 0.0f, 1.0f);
  278. for (const auto &damageGroup : tp->damageGroups) {
  279. s16 armor = itemgroup_get(armor_groups, damageGroup.first);
  280. damage += damageGroup.second * punch_interval_multiplier * armor / 100.0;
  281. }
  282. if (tp->punch_attack_uses > 0) {
  283. result_wear = calculateResultWear(tp->punch_attack_uses, initial_wear);
  284. result_wear *= punch_interval_multiplier;
  285. }
  286. // Keep damage in sane bounds for simplicity
  287. damage = rangelim(damage, -U16_MAX, U16_MAX);
  288. u32 wear_i = (u32) result_wear;
  289. return {damage, wear_i};
  290. }
  291. HitParams getHitParams(const ItemGroupList &armor_groups,
  292. const ToolCapabilities *tp)
  293. {
  294. return getHitParams(armor_groups, tp, 1000000);
  295. }
  296. PunchDamageResult getPunchDamage(
  297. const ItemGroupList &armor_groups,
  298. const ToolCapabilities *toolcap,
  299. const ItemStack *punchitem,
  300. float time_from_last_punch,
  301. u16 initial_wear
  302. ){
  303. bool do_hit = true;
  304. {
  305. if (do_hit && punchitem) {
  306. if (itemgroup_get(armor_groups, "punch_operable") &&
  307. (toolcap == NULL || punchitem->name.empty()))
  308. do_hit = false;
  309. }
  310. if (do_hit) {
  311. if(itemgroup_get(armor_groups, "immortal"))
  312. do_hit = false;
  313. }
  314. }
  315. PunchDamageResult result;
  316. if(do_hit)
  317. {
  318. HitParams hitparams = getHitParams(armor_groups, toolcap,
  319. time_from_last_punch,
  320. punchitem->wear);
  321. result.did_punch = true;
  322. result.wear = hitparams.wear;
  323. result.damage = hitparams.hp;
  324. }
  325. return result;
  326. }
  327. f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand)
  328. {
  329. float max_d = def_selected.range;
  330. float max_d_hand = def_hand.range;
  331. if (max_d < 0 && max_d_hand >= 0)
  332. max_d = max_d_hand;
  333. else if (max_d < 0)
  334. max_d = 4.0f;
  335. return max_d;
  336. }