Cpp11 initializers: last src root changeset (#6022)
[oweals/minetest.git] / src / tool.h
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 #ifndef TOOL_HEADER
21 #define TOOL_HEADER
22
23 #include "irrlichttypes.h"
24 #include <string>
25 #include <iostream>
26 #include "itemgroup.h"
27
28 struct ToolGroupCap
29 {
30         std::unordered_map<int, float> times;
31         int maxlevel = 1;
32         int uses = 20;
33
34         ToolGroupCap() {}
35
36         bool getTime(int rating, float *time) const
37         {
38                 std::unordered_map<int, float>::const_iterator i = times.find(rating);
39                 if (i == times.end()) {
40                         *time = 0;
41                         return false;
42                 }
43                 *time = i->second;
44                 return true;
45         }
46 };
47
48
49 typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
50 typedef std::unordered_map<std::string, s16> DamageGroup;
51
52 struct ToolCapabilities
53 {
54         float full_punch_interval;
55         int max_drop_level;
56         ToolGCMap groupcaps;
57         DamageGroup damageGroups;
58
59         ToolCapabilities(
60                         float full_punch_interval_=1.4,
61                         int max_drop_level_=1,
62                         const ToolGCMap &groupcaps_ = ToolGCMap(),
63                         const DamageGroup &damageGroups_ = DamageGroup()
64         ):
65                 full_punch_interval(full_punch_interval_),
66                 max_drop_level(max_drop_level_),
67                 groupcaps(groupcaps_),
68                 damageGroups(damageGroups_)
69         {}
70
71         void serialize(std::ostream &os, u16 version) const;
72         void deSerialize(std::istream &is);
73 };
74
75 struct DigParams
76 {
77         bool diggable;
78         // Digging time in seconds
79         float time;
80         // Caused wear
81         u16 wear;
82         std::string main_group;
83
84         DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
85                         const std::string &a_main_group = ""):
86                 diggable(a_diggable),
87                 time(a_time),
88                 wear(a_wear),
89                 main_group(a_main_group)
90         {}
91 };
92
93 DigParams getDigParams(const ItemGroupList &groups,
94                 const ToolCapabilities *tp, float time_from_last_punch);
95
96 DigParams getDigParams(const ItemGroupList &groups,
97                 const ToolCapabilities *tp);
98
99 struct HitParams
100 {
101         s16 hp;
102         s16 wear;
103
104         HitParams(s16 hp_=0, s16 wear_=0):
105                 hp(hp_),
106                 wear(wear_)
107         {}
108 };
109
110 HitParams getHitParams(const ItemGroupList &armor_groups,
111                 const ToolCapabilities *tp, float time_from_last_punch);
112
113 HitParams getHitParams(const ItemGroupList &armor_groups,
114                 const ToolCapabilities *tp);
115
116 struct PunchDamageResult
117 {
118         bool did_punch = false;
119         int damage = 0;
120         int wear = 0;
121
122         PunchDamageResult() {}
123 };
124
125 struct ItemStack;
126
127 PunchDamageResult getPunchDamage(
128                 const ItemGroupList &armor_groups,
129                 const ToolCapabilities *toolcap,
130                 const ItemStack *punchitem,
131                 float time_from_last_punch
132 );
133
134 #endif
135