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