]> git.lizzy.rs Git - dragonfireclient.git/blob - src/tool.h
Allow overriding tool capabilities through itemstack metadata
[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 ToolGroupCap
29 {
30         std::unordered_map<int, float> times;
31         int maxlevel = 1;
32         int uses = 20;
33
34         ToolGroupCap() = default;
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         void toJson(Json::Value &object) const;
48         void fromJson(const Json::Value &json);
49 };
50
51
52 typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
53 typedef std::unordered_map<std::string, s16> DamageGroup;
54
55 struct ToolCapabilities
56 {
57         float full_punch_interval;
58         int max_drop_level;
59         ToolGCMap groupcaps;
60         DamageGroup damageGroups;
61
62         ToolCapabilities(
63                         float full_punch_interval_=1.4,
64                         int max_drop_level_=1,
65                         const ToolGCMap &groupcaps_ = ToolGCMap(),
66                         const DamageGroup &damageGroups_ = DamageGroup()
67         ):
68                 full_punch_interval(full_punch_interval_),
69                 max_drop_level(max_drop_level_),
70                 groupcaps(groupcaps_),
71                 damageGroups(damageGroups_)
72         {}
73
74         void serialize(std::ostream &os, u16 version) const;
75         void deSerialize(std::istream &is);
76         void serializeJson(std::ostream &os) const;
77         void deserializeJson(std::istream &is);
78 };
79
80 struct DigParams
81 {
82         bool diggable;
83         // Digging time in seconds
84         float time;
85         // Caused wear
86         u16 wear;
87         std::string main_group;
88
89         DigParams(bool a_diggable = false, float a_time = 0.0f, u16 a_wear = 0,
90                         const std::string &a_main_group = ""):
91                 diggable(a_diggable),
92                 time(a_time),
93                 wear(a_wear),
94                 main_group(a_main_group)
95         {}
96 };
97
98 DigParams getDigParams(const ItemGroupList &groups,
99                 const ToolCapabilities *tp, float time_from_last_punch);
100
101 DigParams getDigParams(const ItemGroupList &groups,
102                 const ToolCapabilities *tp);
103
104 struct HitParams
105 {
106         s16 hp;
107         s16 wear;
108
109         HitParams(s16 hp_=0, s16 wear_=0):
110                 hp(hp_),
111                 wear(wear_)
112         {}
113 };
114
115 HitParams getHitParams(const ItemGroupList &armor_groups,
116                 const ToolCapabilities *tp, float time_from_last_punch);
117
118 HitParams getHitParams(const ItemGroupList &armor_groups,
119                 const ToolCapabilities *tp);
120
121 struct PunchDamageResult
122 {
123         bool did_punch = false;
124         int damage = 0;
125         int wear = 0;
126
127         PunchDamageResult() = default;
128 };
129
130 struct ItemStack;
131
132 PunchDamageResult getPunchDamage(
133                 const ItemGroupList &armor_groups,
134                 const ToolCapabilities *toolcap,
135                 const ItemStack *punchitem,
136                 float time_from_last_punch
137 );