2
0

tool.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "itemgroup.h"
  18. #include "log.h"
  19. #include "inventory.h"
  20. #include "exceptions.h"
  21. #include "util/serialize.h"
  22. #include "util/numeric.h"
  23. void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
  24. {
  25. writeU8(os, 3); // protocol_version >= 36
  26. writeF1000(os, full_punch_interval);
  27. writeS16(os, max_drop_level);
  28. writeU32(os, groupcaps.size());
  29. for (const auto &groupcap : groupcaps) {
  30. const std::string *name = &groupcap.first;
  31. const ToolGroupCap *cap = &groupcap.second;
  32. os << serializeString(*name);
  33. writeS16(os, cap->uses);
  34. writeS16(os, cap->maxlevel);
  35. writeU32(os, cap->times.size());
  36. for (const auto &time : cap->times) {
  37. writeS16(os, time.first);
  38. writeF1000(os, time.second);
  39. }
  40. }
  41. writeU32(os, damageGroups.size());
  42. for (const auto &damageGroup : damageGroups) {
  43. os << serializeString(damageGroup.first);
  44. writeS16(os, damageGroup.second);
  45. }
  46. }
  47. void ToolCapabilities::deSerialize(std::istream &is)
  48. {
  49. int version = readU8(is);
  50. if (version < 3)
  51. throw SerializationError("unsupported ToolCapabilities version");
  52. full_punch_interval = readF1000(is);
  53. max_drop_level = readS16(is);
  54. groupcaps.clear();
  55. u32 groupcaps_size = readU32(is);
  56. for (u32 i = 0; i < groupcaps_size; i++) {
  57. std::string name = deSerializeString(is);
  58. ToolGroupCap cap;
  59. cap.uses = readS16(is);
  60. cap.maxlevel = readS16(is);
  61. u32 times_size = readU32(is);
  62. for(u32 i = 0; i < times_size; i++) {
  63. int level = readS16(is);
  64. float time = readF1000(is);
  65. cap.times[level] = time;
  66. }
  67. groupcaps[name] = cap;
  68. }
  69. u32 damage_groups_size = readU32(is);
  70. for (u32 i = 0; i < damage_groups_size; i++) {
  71. std::string name = deSerializeString(is);
  72. s16 rating = readS16(is);
  73. damageGroups[name] = rating;
  74. }
  75. }
  76. DigParams getDigParams(const ItemGroupList &groups,
  77. const ToolCapabilities *tp, float time_from_last_punch)
  78. {
  79. //infostream<<"getDigParams"<<std::endl;
  80. /* Check group dig_immediate */
  81. switch(itemgroup_get(groups, "dig_immediate")){
  82. case 2:
  83. //infostream<<"dig_immediate=2"<<std::endl;
  84. return DigParams(true, 0.5, 0, "dig_immediate");
  85. case 3:
  86. //infostream<<"dig_immediate=3"<<std::endl;
  87. return DigParams(true, 0, 0, "dig_immediate");
  88. default:
  89. break;
  90. }
  91. // Values to be returned (with a bit of conversion)
  92. bool result_diggable = false;
  93. float result_time = 0.0;
  94. float result_wear = 0.0;
  95. std::string result_main_group;
  96. int level = itemgroup_get(groups, "level");
  97. //infostream<<"level="<<level<<std::endl;
  98. for (const auto &groupcap : tp->groupcaps) {
  99. const std::string &name = groupcap.first;
  100. //infostream<<"group="<<name<<std::endl;
  101. const ToolGroupCap &cap = groupcap.second;
  102. int rating = itemgroup_get(groups, name);
  103. float time = 0;
  104. bool time_exists = cap.getTime(rating, &time);
  105. if(!result_diggable || time < result_time){
  106. if(cap.maxlevel >= level && time_exists){
  107. result_diggable = true;
  108. int leveldiff = cap.maxlevel - level;
  109. result_time = time / MYMAX(1, leveldiff);
  110. if(cap.uses != 0)
  111. result_wear = 1.0 / cap.uses / pow(3.0, (double)leveldiff);
  112. else
  113. result_wear = 0;
  114. result_main_group = name;
  115. }
  116. }
  117. }
  118. //infostream<<"result_diggable="<<result_diggable<<std::endl;
  119. //infostream<<"result_time="<<result_time<<std::endl;
  120. //infostream<<"result_wear="<<result_wear<<std::endl;
  121. if(time_from_last_punch < tp->full_punch_interval){
  122. float f = time_from_last_punch / tp->full_punch_interval;
  123. //infostream<<"f="<<f<<std::endl;
  124. result_time /= f;
  125. result_wear /= f;
  126. }
  127. u16 wear_i = 65535.*result_wear;
  128. return DigParams(result_diggable, result_time, wear_i, result_main_group);
  129. }
  130. DigParams getDigParams(const ItemGroupList &groups,
  131. const ToolCapabilities *tp)
  132. {
  133. return getDigParams(groups, tp, 1000000);
  134. }
  135. HitParams getHitParams(const ItemGroupList &armor_groups,
  136. const ToolCapabilities *tp, float time_from_last_punch)
  137. {
  138. s16 damage = 0;
  139. float full_punch_interval = tp->full_punch_interval;
  140. for (const auto &damageGroup : tp->damageGroups) {
  141. s16 armor = itemgroup_get(armor_groups, damageGroup.first);
  142. damage += damageGroup.second
  143. * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
  144. * armor / 100.0;
  145. }
  146. return {damage, 0};
  147. }
  148. HitParams getHitParams(const ItemGroupList &armor_groups,
  149. const ToolCapabilities *tp)
  150. {
  151. return getHitParams(armor_groups, tp, 1000000);
  152. }
  153. PunchDamageResult getPunchDamage(
  154. const ItemGroupList &armor_groups,
  155. const ToolCapabilities *toolcap,
  156. const ItemStack *punchitem,
  157. float time_from_last_punch
  158. ){
  159. bool do_hit = true;
  160. {
  161. if (do_hit && punchitem) {
  162. if (itemgroup_get(armor_groups, "punch_operable") &&
  163. (toolcap == NULL || punchitem->name.empty()))
  164. do_hit = false;
  165. }
  166. if (do_hit) {
  167. if(itemgroup_get(armor_groups, "immortal"))
  168. do_hit = false;
  169. }
  170. }
  171. PunchDamageResult result;
  172. if(do_hit)
  173. {
  174. HitParams hitparams = getHitParams(armor_groups, toolcap,
  175. time_from_last_punch);
  176. result.did_punch = true;
  177. result.wear = hitparams.wear;
  178. result.damage = hitparams.hp;
  179. }
  180. return result;
  181. }