Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
tool.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include "itemgroup.h"
9#include "json-forwards.h"
10#include "common/c_types.h"
11#include <SColor.h>
12
13#include <string>
14#include <iostream>
15#include <map>
16#include <unordered_map>
17#include <optional>
18
19struct ItemDefinition;
20class IItemDefManager;
21
23{
24 std::unordered_map<int, float> times;
25 int maxlevel = 1;
26 int uses = 20;
27
28 ToolGroupCap() = default;
29
30 std::optional<float> getTime(int rating) const {
31 auto i = times.find(rating);
32 if (i == times.end())
33 return std::nullopt;
34 return i->second;
35 }
36
37 void toJson(Json::Value &object) const;
38 void fromJson(const Json::Value &json);
39};
40
41
42typedef std::unordered_map<std::string, struct ToolGroupCap> ToolGCMap;
43typedef std::unordered_map<std::string, s16> DamageGroup;
44
46{
52
54 float full_punch_interval_ = 1.4f,
55 int max_drop_level_ = 1,
56 const ToolGCMap &groupcaps_ = ToolGCMap(),
57 const DamageGroup &damageGroups_ = DamageGroup(),
58 int punch_attack_uses_ = 0
59 ):
60 full_punch_interval(full_punch_interval_),
61 max_drop_level(max_drop_level_),
62 groupcaps(groupcaps_),
63 damageGroups(damageGroups_),
64 punch_attack_uses(punch_attack_uses_)
65 {}
66
67 void serialize(std::ostream &os, u16 version) const;
68 void deSerialize(std::istream &is);
69 void serializeJson(std::ostream &os) const;
70 void deserializeJson(std::istream &is);
71
72private:
73 void deserializeJsonGroupcaps(Json::Value &json);
74 void deserializeJsonDamageGroups(Json::Value &json);
75};
76
78{
79 std::map<f32, video::SColor> colorStops;
80 enum BlendMode : u8 {
83 BlendMode_END // Dummy for validity check
84 };
85 constexpr const static EnumString es_BlendMode[3] = {
88 {0, nullptr}
89 };
91
92 WearBarParams(const std::map<f32, video::SColor> &colorStops, BlendMode blend):
95 {}
96
97 WearBarParams(const video::SColor color):
99 {};
100
101 void serialize(std::ostream &os) const;
102 static WearBarParams deserialize(std::istream &is);
103 void serializeJson(std::ostream &os) const;
104 static std::optional<WearBarParams> deserializeJson(std::istream &is);
105 video::SColor getWearBarColor(f32 durabilityPercent);
106};
107
109{
111 // Digging time in seconds
112 float time;
113 // Caused wear
114 u32 wear; // u32 because wear could be 65536 (single-use tool)
115 std::string main_group;
116
117 DigParams(bool a_diggable = false, float a_time = 0.0f, u32 a_wear = 0,
118 const std::string &a_main_group = ""):
119 diggable(a_diggable),
120 time(a_time),
121 wear(a_wear),
122 main_group(a_main_group)
123 {}
124};
125
127 const ToolCapabilities *tp,
128 const u16 initial_wear = 0);
129
131{
132 s32 hp;
133 // Caused wear
134 u32 wear; // u32 because wear could be 65536 (single-use weapon)
135
136 HitParams(s32 hp_ = 0, u32 wear_ = 0):
137 hp(hp_),
138 wear(wear_)
139 {}
140};
141
142HitParams getHitParams(const ItemGroupList &armor_groups,
143 const ToolCapabilities *tp, float time_from_last_punch,
144 u16 initial_wear = 0);
145
146HitParams getHitParams(const ItemGroupList &armor_groups,
147 const ToolCapabilities *tp);
148
150{
151 bool did_punch = false;
152 int damage = 0;
153 int wear = 0;
154
155 PunchDamageResult() = default;
156};
157
158struct ItemStack;
159
161 const ItemGroupList &armor_groups,
162 const ToolCapabilities *toolcap,
163 const ItemStack *punchitem,
164 float time_from_last_punch,
165 u16 initial_wear = 0
166);
167
168u32 calculateResultWear(const u32 uses, const u16 initial_wear);
169f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item,
170 const IItemDefManager *itemdef_manager);
Definition itemdef.h:129
std::unordered_map< std::string, int > ItemGroupList
Definition itemgroup.h:10
Definition tool.h:109
bool diggable
Definition tool.h:110
std::string main_group
Definition tool.h:115
float time
Definition tool.h:112
DigParams(bool a_diggable=false, float a_time=0.0f, u32 a_wear=0, const std::string &a_main_group="")
Definition tool.h:117
u32 wear
Definition tool.h:114
Definition c_types.h:16
Definition tool.h:131
u32 wear
Definition tool.h:134
HitParams(s32 hp_=0, u32 wear_=0)
Definition tool.h:136
s32 hp
Definition tool.h:132
Definition itemdef.h:66
Definition inventory.h:19
Definition tool.h:150
bool did_punch
Definition tool.h:151
int wear
Definition tool.h:153
PunchDamageResult()=default
int damage
Definition tool.h:152
Definition tool.h:46
ToolCapabilities(float full_punch_interval_=1.4f, int max_drop_level_=1, const ToolGCMap &groupcaps_=ToolGCMap(), const DamageGroup &damageGroups_=DamageGroup(), int punch_attack_uses_=0)
Definition tool.h:53
float full_punch_interval
Definition tool.h:47
void deserializeJsonGroupcaps(Json::Value &json)
Definition tool.cpp:172
ToolGCMap groupcaps
Definition tool.h:49
void serialize(std::ostream &os, u16 version) const
Definition tool.cpp:54
void deserializeJsonDamageGroups(Json::Value &json)
Definition tool.cpp:184
void serializeJson(std::ostream &os) const
Definition tool.cpp:125
void deSerialize(std::istream &is)
Definition tool.cpp:89
int max_drop_level
Definition tool.h:48
void deserializeJson(std::istream &is)
Definition tool.cpp:151
DamageGroup damageGroups
Definition tool.h:50
int punch_attack_uses
Definition tool.h:51
Definition tool.h:23
int uses
Definition tool.h:26
void toJson(Json::Value &object) const
Definition tool.cpp:19
std::optional< float > getTime(int rating) const
Definition tool.h:30
std::unordered_map< int, float > times
Definition tool.h:24
int maxlevel
Definition tool.h:25
void fromJson(const Json::Value &json)
Definition tool.cpp:30
ToolGroupCap()=default
Definition tool.h:78
video::SColor getWearBarColor(f32 durabilityPercent)
Definition tool.cpp:275
void serialize(std::ostream &os) const
Definition tool.cpp:196
void serializeJson(std::ostream &os) const
Definition tool.cpp:230
constexpr static const EnumString es_BlendMode[3]
Definition tool.h:85
std::map< f32, video::SColor > colorStops
Definition tool.h:79
WearBarParams(const video::SColor color)
Definition tool.h:97
static std::optional< WearBarParams > deserializeJson(std::istream &is)
Definition tool.cpp:243
WearBarParams(const std::map< f32, video::SColor > &colorStops, BlendMode blend)
Definition tool.h:92
BlendMode
Definition tool.h:80
@ BLEND_MODE_CONSTANT
Definition tool.h:81
@ BlendMode_END
Definition tool.h:83
@ BLEND_MODE_LINEAR
Definition tool.h:82
static WearBarParams deserialize(std::istream &is)
Definition tool.cpp:207
BlendMode blend
Definition tool.h:90
u32 calculateResultWear(const u32 uses, const u16 initial_wear)
Definition tool.cpp:317
HitParams getHitParams(const ItemGroupList &armor_groups, const ToolCapabilities *tp, float time_from_last_punch, u16 initial_wear=0)
Definition tool.cpp:435
std::unordered_map< std::string, s16 > DamageGroup
Definition tool.h:43
DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp, const u16 initial_wear=0)
Definition tool.cpp:379
f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item, const IItemDefManager *itemdef_manager)
Definition tool.cpp:501
PunchDamageResult getPunchDamage(const ItemGroupList &armor_groups, const ToolCapabilities *toolcap, const ItemStack *punchitem, float time_from_last_punch, u16 initial_wear=0)
Definition tool.cpp:466
std::unordered_map< std::string, struct ToolGroupCap > ToolGCMap
Definition tool.h:42