Minetest  5.4.0
tool.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include "irrlichttypes.h"
23 #include <string>
24 #include <iostream>
25 #include "itemgroup.h"
26 #include <json/json.h>
27 
28 struct ItemDefinition;
29 
31 {
32  std::unordered_map<int, float> times;
33  int maxlevel = 1;
34  int uses = 20;
35 
36  ToolGroupCap() = default;
37 
38  bool getTime(int rating, float *time) const
39  {
40  std::unordered_map<int, float>::const_iterator i = times.find(rating);
41  if (i == times.end()) {
42  *time = 0;
43  return false;
44  }
45  *time = i->second;
46  return true;
47  }
48 
49  void toJson(Json::Value &object) const;
50  void fromJson(const Json::Value &json);
51 };
52 
53 
54 typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
55 typedef std::unordered_map<std::string, s16> DamageGroup;
56 
58 {
64 
66  float full_punch_interval_ = 1.4f,
67  int max_drop_level_ = 1,
68  const ToolGCMap &groupcaps_ = ToolGCMap(),
69  const DamageGroup &damageGroups_ = DamageGroup(),
70  int punch_attack_uses_ = 0
71  ):
72  full_punch_interval(full_punch_interval_),
73  max_drop_level(max_drop_level_),
74  groupcaps(groupcaps_),
75  damageGroups(damageGroups_),
76  punch_attack_uses(punch_attack_uses_)
77  {}
78 
79  void serialize(std::ostream &os, u16 version) const;
80  void deSerialize(std::istream &is);
81  void serializeJson(std::ostream &os) const;
82  void deserializeJson(std::istream &is);
83 };
84 
85 struct DigParams
86 {
87  bool diggable;
88  // Digging time in seconds
89  float time;
90  // Caused wear
91  u16 wear;
92  std::string main_group;
93 
94  DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
95  const std::string &a_main_group = ""):
96  diggable(a_diggable),
97  time(a_time),
98  wear(a_wear),
99  main_group(a_main_group)
100  {}
101 };
102 
103 DigParams getDigParams(const ItemGroupList &groups,
104  const ToolCapabilities *tp);
105 
106 struct HitParams
107 {
108  s16 hp;
109  u16 wear;
110 
111  HitParams(s16 hp_ = 0, u16 wear_ = 0):
112  hp(hp_),
113  wear(wear_)
114  {}
115 };
116 
117 HitParams getHitParams(const ItemGroupList &armor_groups,
118  const ToolCapabilities *tp, float time_from_last_punch);
119 
120 HitParams getHitParams(const ItemGroupList &armor_groups,
121  const ToolCapabilities *tp);
122 
124 {
125  bool did_punch = false;
126  int damage = 0;
127  int wear = 0;
128 
129  PunchDamageResult() = default;
130 };
131 
132 struct ItemStack;
133 
135  const ItemGroupList &armor_groups,
136  const ToolCapabilities *toolcap,
137  const ItemStack *punchitem,
138  float time_from_last_punch
139 );
140 
141 f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand);
std::unordered_map< std::string, int > ItemGroupList
Definition: itemgroup.h:25
Definition: tool.h:86
u16 wear
Definition: tool.h:91
bool diggable
Definition: tool.h:87
DigParams(bool a_diggable=false, float a_time=0.0f, u16 a_wear=0, const std::string &a_main_group="")
Definition: tool.h:94
std::string main_group
Definition: tool.h:92
float time
Definition: tool.h:89
Definition: tool.h:107
u16 wear
Definition: tool.h:109
HitParams(s16 hp_=0, u16 wear_=0)
Definition: tool.h:111
s16 hp
Definition: tool.h:108
Definition: itemdef.h:52
Definition: inventory.h:34
Definition: tool.h:124
bool did_punch
Definition: tool.h:125
int wear
Definition: tool.h:127
PunchDamageResult()=default
int damage
Definition: tool.h:126
Definition: tool.h:58
ToolCapabilities(float full_punch_interval_=1.4f, int max_drop_level_=1, const ToolGCMap &groupcaps_=ToolGCMap(), const DamageGroup &damageGroups_=DamageGroup(), int punch_attack_uses_=0)
Definition: tool.h:65
float full_punch_interval
Definition: tool.h:59
ToolGCMap groupcaps
Definition: tool.h:61
void serialize(std::ostream &os, u16 version) const
Definition: tool.cpp:57
void serializeJson(std::ostream &os) const
Definition: tool.cpp:125
void deSerialize(std::istream &is)
Definition: tool.cpp:90
int max_drop_level
Definition: tool.h:60
void deserializeJson(std::istream &is)
Definition: tool.cpp:148
DamageGroup damageGroups
Definition: tool.h:62
int punch_attack_uses
Definition: tool.h:63
Definition: tool.h:31
int uses
Definition: tool.h:34
void toJson(Json::Value &object) const
Definition: tool.cpp:29
std::unordered_map< int, float > times
Definition: tool.h:32
int maxlevel
Definition: tool.h:33
void fromJson(const Json::Value &json)
Definition: tool.cpp:40
bool getTime(int rating, float *time) const
Definition: tool.h:38
ToolGroupCap()=default
PunchDamageResult getPunchDamage(const ItemGroupList &armor_groups, const ToolCapabilities *toolcap, const ItemStack *punchitem, float time_from_last_punch)
Definition: tool.cpp:264
std::unordered_map< std::string, s16 > DamageGroup
Definition: tool.h:55
DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp)
Definition: tool.cpp:185
f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand)
Definition: tool.cpp:297
HitParams getHitParams(const ItemGroupList &armor_groups, const ToolCapabilities *tp, float time_from_last_punch)
Definition: tool.cpp:238
std::unordered_map< std::string, struct ToolGroupCap > ToolGCMap
Definition: tool.h:54