]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tool.h
Unify wield item handling (#8677)
[dragonfireclient.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 #include <json/json.h>
27
28 struct ItemDefinition;
29
30 struct ToolGroupCap
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
57 struct ToolCapabilities
58 {
59         float full_punch_interval;
60         int max_drop_level;
61         ToolGCMap groupcaps;
62         DamageGroup damageGroups;
63
64         ToolCapabilities(
65                         float full_punch_interval_=1.4,
66                         int max_drop_level_=1,
67                         const ToolGCMap &groupcaps_ = ToolGCMap(),
68                         const DamageGroup &damageGroups_ = DamageGroup()
69         ):
70                 full_punch_interval(full_punch_interval_),
71                 max_drop_level(max_drop_level_),
72                 groupcaps(groupcaps_),
73                 damageGroups(damageGroups_)
74         {}
75
76         void serialize(std::ostream &os, u16 version) const;
77         void deSerialize(std::istream &is);
78         void serializeJson(std::ostream &os) const;
79         void deserializeJson(std::istream &is);
80 };
81
82 struct DigParams
83 {
84         bool diggable;
85         // Digging time in seconds
86         float time;
87         // Caused wear
88         u16 wear;
89         std::string main_group;
90
91         DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
92                         const std::string &a_main_group = ""):
93                 diggable(a_diggable),
94                 time(a_time),
95                 wear(a_wear),
96                 main_group(a_main_group)
97         {}
98 };
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 = false;
123         int damage = 0;
124         int wear = 0;
125
126         PunchDamageResult() = default;
127 };
128
129 struct ItemStack;
130
131 PunchDamageResult getPunchDamage(
132                 const ItemGroupList &armor_groups,
133                 const ToolCapabilities *toolcap,
134                 const ItemStack *punchitem,
135                 float time_from_last_punch
136 );
137
138 f32 getToolRange(const ItemDefinition &def_selected, const ItemDefinition &def_hand);