tool.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Minetest
  3. Copyright (C) 2010-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. #pragma once
  17. #include "irrlichttypes.h"
  18. #include <string>
  19. #include <iostream>
  20. #include "itemgroup.h"
  21. #include <json/json.h>
  22. struct ItemDefinition;
  23. struct ToolGroupCap
  24. {
  25. std::unordered_map<int, float> times;
  26. int maxlevel = 1;
  27. int uses = 20;
  28. ToolGroupCap() = default;
  29. bool getTime(int rating, float *time) const
  30. {
  31. std::unordered_map<int, float>::const_iterator i = times.find(rating);
  32. if (i == times.end()) {
  33. *time = 0;
  34. return false;
  35. }
  36. *time = i->second;
  37. return true;
  38. }
  39. void toJson(Json::Value &object) const;
  40. void fromJson(const Json::Value &json);
  41. };
  42. typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
  43. typedef std::unordered_map<std::string, s16> DamageGroup;
  44. struct ToolCapabilities
  45. {
  46. float full_punch_interval;
  47. int max_drop_level;
  48. ToolGCMap groupcaps;
  49. DamageGroup damageGroups;
  50. int punch_attack_uses;
  51. ToolCapabilities(
  52. float full_punch_interval_ = 1.4f,
  53. int max_drop_level_ = 1,
  54. const ToolGCMap &groupcaps_ = ToolGCMap(),
  55. const DamageGroup &damageGroups_ = DamageGroup(),
  56. int punch_attack_uses_ = 0
  57. ):
  58. full_punch_interval(full_punch_interval_),
  59. max_drop_level(max_drop_level_),
  60. groupcaps(groupcaps_),
  61. damageGroups(damageGroups_),
  62. punch_attack_uses(punch_attack_uses_)
  63. {}
  64. void serialize(std::ostream &os, u16 version) const;
  65. void deSerialize(std::istream &is);
  66. void serializeJson(std::ostream &os) const;
  67. void deserializeJson(std::istream &is);
  68. };
  69. struct DigParams
  70. {
  71. bool diggable;
  72. // Digging time in seconds
  73. float time;
  74. // Caused wear
  75. u16 wear;
  76. std::string main_group;
  77. DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
  78. const std::string &a_main_group = ""):
  79. diggable(a_diggable),
  80. time(a_time),
  81. wear(a_wear),
  82. main_group(a_main_group)
  83. {}
  84. };
  85. DigParams getDigParams(const ItemGroupList &groups,
  86. const ToolCapabilities *tp);
  87. struct HitParams
  88. {
  89. s16 hp;
  90. u16 wear;
  91. HitParams(s16 hp_ = 0, u16 wear_ = 0):
  92. hp(hp_),
  93. wear(wear_)
  94. {}
  95. };
  96. HitParams getHitParams(const ItemGroupList &armor_groups,
  97. const ToolCapabilities *tp, float time_from_last_punch);
  98. HitParams getHitParams(const ItemGroupList &armor_groups,
  99. const ToolCapabilities *tp);
  100. struct PunchDamageResult
  101. {
  102. bool did_punch = false;
  103. int damage = 0;
  104. int wear = 0;
  105. PunchDamageResult() = default;
  106. };
  107. struct ItemStack;
  108. PunchDamageResult getPunchDamage(
  109. const ItemGroupList &armor_groups,
  110. const ToolCapabilities *toolcap,
  111. const ItemStack *punchitem,
  112. float time_from_last_punch
  113. );
  114. f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand);