Luanti 5.11.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 <vector>
16#include <map>
17#include <optional>
18
19struct ItemDefinition;
20class IItemDefManager;
21
22/*
23 * NOTE: these structs intentionally use vector<pair<>> or map<> over unordered_map<>
24 * to avoid blowing up the structure sizes. Also because the linear "dumb" approach
25 * works better if you have just a handful of items.
26 */
27
29{
30 std::vector<std::pair<int, float>> times;
31 int maxlevel = 1;
32 int uses = 20;
33
34 ToolGroupCap() = default;
35
36 std::optional<float> getTime(int rating) const {
37 for (auto &it : times) {
38 if (it.first == rating)
39 return it.second;
40 }
41 return std::nullopt;
42 }
43
44 void toJson(Json::Value &object) const;
45 void fromJson(const Json::Value &json);
46};
47
48
49typedef std::map<std::string, ToolGroupCap> ToolGCMap;
50typedef std::map<std::string, s16> DamageGroup;
51
53{
59
61 float full_punch_interval_ = 1.4f,
62 int max_drop_level_ = 1,
63 const ToolGCMap &groupcaps_ = ToolGCMap(),
64 const DamageGroup &damageGroups_ = DamageGroup(),
65 int punch_attack_uses_ = 0
66 ):
67 full_punch_interval(full_punch_interval_),
68 max_drop_level(max_drop_level_),
69 punch_attack_uses(punch_attack_uses_),
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
79private:
80 void deserializeJsonGroupcaps(Json::Value &json);
81 void deserializeJsonDamageGroups(Json::Value &json);
82};
83
85{
86 enum BlendMode : u8 {
89 BlendMode_END // Dummy for validity check
90 };
91 constexpr const static EnumString es_BlendMode[3] = {
92 {BLEND_MODE_CONSTANT, "constant"},
93 {BLEND_MODE_LINEAR, "linear"},
94 {0, nullptr}
95 };
96
97 std::map<f32, video::SColor> colorStops;
99
100 WearBarParams(const std::map<f32, video::SColor> &colorStops, BlendMode blend):
102 blend(blend)
103 {}
104
105 WearBarParams(const video::SColor color):
107 {};
108
109 void serialize(std::ostream &os) const;
110 static WearBarParams deserialize(std::istream &is);
111 void serializeJson(std::ostream &os) const;
112 static std::optional<WearBarParams> deserializeJson(std::istream &is);
113
114 video::SColor getWearBarColor(f32 durabilityPercent);
115};
116
118{
120 // Digging time in seconds
121 float time;
122 // Caused wear
123 u32 wear; // u32 because wear could be 65536 (single-use tool)
124 std::string main_group;
125
126 DigParams(bool a_diggable = false, float a_time = 0.0f, u32 a_wear = 0,
127 const std::string &a_main_group = ""):
128 diggable(a_diggable),
129 time(a_time),
130 wear(a_wear),
131 main_group(a_main_group)
132 {}
133};
134
136 const ToolCapabilities *tp,
137 const u16 initial_wear = 0);
138
140{
141 s32 hp;
142 // Caused wear
143 u32 wear; // u32 because wear could be 65536 (single-use weapon)
144
145 HitParams(s32 hp_ = 0, u32 wear_ = 0):
146 hp(hp_),
147 wear(wear_)
148 {}
149};
150
151HitParams getHitParams(const ItemGroupList &armor_groups,
152 const ToolCapabilities *tp, float time_from_last_punch,
153 u16 initial_wear = 0);
154
155HitParams getHitParams(const ItemGroupList &armor_groups,
156 const ToolCapabilities *tp);
157
159{
160 bool did_punch = false;
161 int damage = 0;
162 int wear = 0;
163
164 PunchDamageResult() = default;
165};
166
167struct ItemStack;
168
170 const ItemGroupList &armor_groups,
171 const ToolCapabilities *toolcap,
172 const ItemStack *punchitem,
173 float time_from_last_punch,
174 u16 initial_wear = 0
175);
176
177u32 calculateResultWear(const u32 uses, const u16 initial_wear);
178f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item,
179 const IItemDefManager *itemdef_manager);
Definition itemdef.h:129
std::unordered_map< std::string, int > ItemGroupList
Definition itemgroup.h:10
Definition tool.h:118
bool diggable
Definition tool.h:119
std::string main_group
Definition tool.h:124
float time
Definition tool.h:121
DigParams(bool a_diggable=false, float a_time=0.0f, u32 a_wear=0, const std::string &a_main_group="")
Definition tool.h:126
u32 wear
Definition tool.h:123
Definition c_types.h:16
Definition tool.h:140
u32 wear
Definition tool.h:143
HitParams(s32 hp_=0, u32 wear_=0)
Definition tool.h:145
s32 hp
Definition tool.h:141
Definition itemdef.h:66
Definition inventory.h:19
Definition tool.h:159
bool did_punch
Definition tool.h:160
int wear
Definition tool.h:162
PunchDamageResult()=default
int damage
Definition tool.h:161
Definition tool.h:53
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:60
float full_punch_interval
Definition tool.h:54
void deserializeJsonGroupcaps(Json::Value &json)
Definition tool.cpp:172
ToolGCMap groupcaps
Definition tool.h:57
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:55
void deserializeJson(std::istream &is)
Definition tool.cpp:151
DamageGroup damageGroups
Definition tool.h:58
int punch_attack_uses
Definition tool.h:56
Definition tool.h:29
int uses
Definition tool.h:32
std::vector< std::pair< int, float > > times
Definition tool.h:30
void toJson(Json::Value &object) const
Definition tool.cpp:19
std::optional< float > getTime(int rating) const
Definition tool.h:36
int maxlevel
Definition tool.h:31
void fromJson(const Json::Value &json)
Definition tool.cpp:30
ToolGroupCap()=default
Definition tool.h:85
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:91
std::map< f32, video::SColor > colorStops
Definition tool.h:97
WearBarParams(const video::SColor color)
Definition tool.h:105
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:100
BlendMode
Definition tool.h:86
@ BLEND_MODE_CONSTANT
Definition tool.h:87
@ BlendMode_END
Definition tool.h:89
@ BLEND_MODE_LINEAR
Definition tool.h:88
static WearBarParams deserialize(std::istream &is)
Definition tool.cpp:207
BlendMode blend
Definition tool.h:98
u32 calculateResultWear(const u32 uses, const u16 initial_wear)
Definition tool.cpp:308
HitParams getHitParams(const ItemGroupList &armor_groups, const ToolCapabilities *tp, float time_from_last_punch, u16 initial_wear=0)
Definition tool.cpp:426
DigParams getDigParams(const ItemGroupList &groups, const ToolCapabilities *tp, const u16 initial_wear=0)
Definition tool.cpp:370
std::map< std::string, s16 > DamageGroup
Definition tool.h:50
f32 getToolRange(const ItemStack &wielded_item, const ItemStack &hand_item, const IItemDefManager *itemdef_manager)
Definition tool.cpp:492
std::map< std::string, ToolGroupCap > ToolGCMap
Definition tool.h:49
PunchDamageResult getPunchDamage(const ItemGroupList &armor_groups, const ToolCapabilities *toolcap, const ItemStack *punchitem, float time_from_last_punch, u16 initial_wear=0)
Definition tool.cpp:457