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