tool.cpp 6.1 KB

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