tool.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "itemgroup.h"
  19. #include "json-forwards.h"
  20. #include "common/c_types.h"
  21. #include <SColor.h>
  22. #include <string>
  23. #include <iostream>
  24. #include <map>
  25. #include <unordered_map>
  26. #include <optional>
  27. struct ItemDefinition;
  28. class IItemDefManager;
  29. struct ToolGroupCap
  30. {
  31. std::unordered_map<int, float> times;
  32. int maxlevel = 1;
  33. int uses = 20;
  34. ToolGroupCap() = default;
  35. std::optional<float> getTime(int rating) const {
  36. auto i = times.find(rating);
  37. if (i == times.end())
  38. return std::nullopt;
  39. return i->second;
  40. }
  41. void toJson(Json::Value &object) const;
  42. void fromJson(const Json::Value &json);
  43. };
  44. typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
  45. typedef std::unordered_map<std::string, s16> DamageGroup;
  46. struct ToolCapabilities
  47. {
  48. float full_punch_interval;
  49. int max_drop_level;
  50. ToolGCMap groupcaps;
  51. DamageGroup damageGroups;
  52. int punch_attack_uses;
  53. ToolCapabilities(
  54. float full_punch_interval_ = 1.4f,
  55. int max_drop_level_ = 1,
  56. const ToolGCMap &groupcaps_ = ToolGCMap(),
  57. const DamageGroup &damageGroups_ = DamageGroup(),
  58. int punch_attack_uses_ = 0
  59. ):
  60. full_punch_interval(full_punch_interval_),
  61. max_drop_level(max_drop_level_),
  62. groupcaps(groupcaps_),
  63. damageGroups(damageGroups_),
  64. punch_attack_uses(punch_attack_uses_)
  65. {}
  66. void serialize(std::ostream &os, u16 version) const;
  67. void deSerialize(std::istream &is);
  68. void serializeJson(std::ostream &os) const;
  69. void deserializeJson(std::istream &is);
  70. };
  71. struct WearBarParams
  72. {
  73. std::map<f32, video::SColor> colorStops;
  74. enum BlendMode : u8 {
  75. BLEND_MODE_CONSTANT,
  76. BLEND_MODE_LINEAR,
  77. BlendMode_END // Dummy for validity check
  78. };
  79. constexpr const static EnumString es_BlendMode[3] = {
  80. {WearBarParams::BLEND_MODE_CONSTANT, "constant"},
  81. {WearBarParams::BLEND_MODE_LINEAR, "linear"},
  82. {0, nullptr}
  83. };
  84. BlendMode blend;
  85. WearBarParams(const std::map<f32, video::SColor> &colorStops, BlendMode blend):
  86. colorStops(colorStops),
  87. blend(blend)
  88. {}
  89. WearBarParams(const video::SColor color):
  90. WearBarParams({{0.0f, color}}, WearBarParams::BLEND_MODE_CONSTANT)
  91. {};
  92. void serialize(std::ostream &os) const;
  93. static WearBarParams deserialize(std::istream &is);
  94. void serializeJson(std::ostream &os) const;
  95. static std::optional<WearBarParams> deserializeJson(std::istream &is);
  96. video::SColor getWearBarColor(f32 durabilityPercent);
  97. };
  98. struct DigParams
  99. {
  100. bool diggable;
  101. // Digging time in seconds
  102. float time;
  103. // Caused wear
  104. u32 wear; // u32 because wear could be 65536 (single-use tool)
  105. std::string main_group;
  106. DigParams(bool a_diggable = false, float a_time = 0.0f, u32 a_wear = 0,
  107. const std::string &a_main_group = ""):
  108. diggable(a_diggable),
  109. time(a_time),
  110. wear(a_wear),
  111. main_group(a_main_group)
  112. {}
  113. };
  114. DigParams getDigParams(const ItemGroupList &groups,
  115. const ToolCapabilities *tp,
  116. const u16 initial_wear = 0);
  117. struct HitParams
  118. {
  119. s32 hp;
  120. // Caused wear
  121. u32 wear; // u32 because wear could be 65536 (single-use weapon)
  122. HitParams(s32 hp_ = 0, u32 wear_ = 0):
  123. hp(hp_),
  124. wear(wear_)
  125. {}
  126. };
  127. HitParams getHitParams(const ItemGroupList &armor_groups,
  128. const ToolCapabilities *tp, float time_from_last_punch,
  129. u16 initial_wear = 0);
  130. HitParams getHitParams(const ItemGroupList &armor_groups,
  131. const ToolCapabilities *tp);
  132. struct PunchDamageResult
  133. {
  134. bool did_punch = false;
  135. int damage = 0;
  136. int wear = 0;
  137. PunchDamageResult() = default;
  138. };
  139. struct ItemStack;
  140. PunchDamageResult getPunchDamage(
  141. const ItemGroupList &armor_groups,
  142. const ToolCapabilities *toolcap,
  143. const ItemStack *punchitem,
  144. float time_from_last_punch,
  145. u16 initial_wear = 0
  146. );
  147. u32 calculateResultWear(const u32 uses, const u16 initial_wear);
  148. f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item,
  149. const IItemDefManager *itemdef_manager);