Botcraft 1.21.5
Loading...
Searching...
No Matches
Blockstate.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <bitset>
4#include <deque>
5#include <map>
6#include <string>
7#include <vector>
8
10
13
14namespace Botcraft
15{
16#if USE_GUI
17 namespace Renderer
18 {
19 class Atlas;
20 }
21#endif
22
29
30#if PROTOCOL_VERSION < 347 /* < 1.13 */
31 using BlockstateId = std::pair<int, unsigned char>;
32#else
33 using BlockstateId = unsigned int;
34#endif
35
37 {
38#if PROTOCOL_VERSION < 347 /* < 1.13 */
39 int id = -1;
40 unsigned char metadata = 0;
41#else
42 int id = -1;
43#endif
44 /// @brief True if the block is air (air, cave_air, void and structure_void are counted as air)
45 bool air = false;
46 /// @brief True if can't go through it
48 /// @brief True if not a full 1x1x1 block OR at least one face texture has transparency
49 bool transparent = false;
50 /// @brief True for lava
51 bool lava = false;
52 /// @brief True for water
53 bool water = false;
54 /// @brief True for blocks that are always waterlogged (kelp, seagrass...)
56 /// @brief True if can be used as a ladder
57 bool climbable = false;
58 /// @brief True if block can hurt when walking in/on it
59 bool hazardous = false;
60 /// @brief True if this block drops item when broken with no tool
61 bool any_tool_harvest = false;
62 /// @brief True if this block is slime
63 bool slime = false;
64 /// @brief True if this block has the BEDS tag
65 bool bed = false;
66 /// @brief True if this block is soul_sand
67 bool soul_sand = false;
68 /// @brief True if this block is honey
69 bool honey = false;
70 /// @brief True if this block is scaffolding
71 bool scaffolding = false;
72 /// @brief True if this block is cobweb
73 bool cobweb = false;
74 /// @brief True if this block is a bubble column going up
76 /// @brief True if this block is a bubble column going down
78 /// @brief True if this block is sweet_berry_bush
79 bool berry_bush = false;
80 /// @brief True if this block is powder_snow
81 bool powder_snow = false;
82 /// @brief Max horizontal offset value of the colliders (for bamboo and pointed dripstone)
83 float horizontal_offset = 0.0f;
84 /// @brief Digging hardness
85 float hardness = -2.0f;
86 /// @brief Slipperiness coefficient
87 float friction = 0.6f;
88 /// @brief True if the model is a custom one (chests/banners etc...)
89 bool custom = false;
91 std::string name = "";
92 std::string path = "";
93 std::vector<std::string> variables = std::vector<std::string>(0);
94 std::vector<BestTool> best_tools = std::vector<BestTool>(0);
96 };
97
99 {
100 public:
101 /// @brief Create a blockstate reading files from properties path
102 /// @param properties The properties of this blockstate
103 Blockstate(const BlockstateProperties& properties);
104
105 /// @brief Create a blockstate from a given model, ignoring path in properties
106 /// @param properties The properties of this blockstate
107 /// @param model_ The model of this blockstate
108 Blockstate(const BlockstateProperties& properties, const Model& model_);
109
110 BlockstateId GetId() const;
111 const Model& GetModel(const unsigned short index) const;
112 unsigned char GetModelId(const Position& pos) const;
113 size_t GetNumModels() const;
114 const std::string& GetName() const;
115 const std::string& GetVariableValue(const std::string& variable) const;
117 std::set<AABB> GetCollidersAtPos(const Position& pos) const;
118 /// @brief Get the closest point on this blockstate placed at block_pos from a reference pos
119 /// @param block_pos Block position
120 /// @param pos Reference position to find the closest point from
121 /// @return The closest point inside the block AABB shapes
122 Vector3<double> GetClosestPoint(const Position& block_pos, const Vector3<double>& pos) const;
123
124 bool IsAir() const;
125 bool IsSolid() const;
126 bool IsTransparent() const;
127 bool IsFluid() const;
128 bool IsFluidOrWaterlogged() const;
129 bool IsLava() const;
130 bool IsFluidFalling() const;
131 bool IsWater() const;
132 bool IsWaterlogged() const;
133 bool IsWaterOrWaterlogged() const;
134 bool IsClimbable() const;
135 bool IsHazardous() const;
136 bool IsSlime() const;
137 bool IsBed() const;
138 bool IsSoulSand() const;
139 bool IsHoney() const;
140 bool IsScaffolding() const;
141 bool IsCobweb() const;
142 bool IsBubbleColumn() const;
143 bool IsUpBubbleColumn() const;
144 bool IsDownBubbleColumn() const;
145 bool IsBerryBush() const;
146 bool IsPowderSnow() const;
147 float GetHardness() const;
148 float GetFriction() const;
149 TintType GetTintType() const;
150 /// @brief Get fluid height for this block. Does not take into account neighbouring blocks
151 /// @return Height of fluid in this block, between 0 and 1
152 float GetFluidHeight() const;
153
154 /// @brief Compute the amount of time (in s) required to mine this block
155 /// @param tool_type The tool used to mine
156 /// @param tool_material The material the tool is made of
157 /// @param tool_efficiency_additional_speed Additional speed added by current tool level of efficiency enchantment (efficiency˛ + (efficiency > 0))
158 /// @param haste Level of haste applied to the player
159 /// @param fatigue Level of mining fatigue applied to the player
160 /// @param on_ground Boolean indicating whether or not the player is currently on the ground
161 /// @param speed_factor Speed multiplier to apply (e.g. 0.2 if under water with no aqua affinity)
162 /// @return The time required to mine this block in these conditions, or -1 if can't be mined
163 float GetMiningTimeSeconds(const ToolType tool_type, const ToolMaterial tool_material,
164 const float tool_efficiency_additional_speed = 0.0f, const unsigned char haste = 0, const unsigned char fatigue = 0,
165 const bool on_ground = true, const float speed_factor = 1.0f) const;
166
167#if PROTOCOL_VERSION < 347 /* < 1.13 */
168 static unsigned int IdMetadataToId(const int id_, const unsigned char metadata_);
169 static void IdToIdMetadata(const unsigned int input_id, int& output_id, unsigned char& output_metadata);
170#endif
171 static void ClearCache();
172
173#if USE_GUI
174 static void UpdateModelsWithAtlasData(const Renderer::Atlas* atlas);
175#endif
176
177 private:
178 void LoadProperties(const BlockstateProperties& properties);
179 void LoadWeightedModels(const std::deque<std::pair<Model, int>>& models_to_load);
180 bool GetBoolFromCondition(const ProtocolCraft::Json::Value& condition) const;
181 /// @brief Check if a given string condition match this blockstate variables
182 /// @param condition String to check, example: "layers=1"
183 /// @return True if variables values match, false otherwise
184 bool MatchCondition(const std::string& condition) const;
185
186 // std::set does not invalidate pointers when growing
187 static std::set<std::string> unique_strings;
188 static const std::string* GetUniqueStringPtr(const std::string& s);
189 static std::deque<Model> unique_models;
190 static size_t GetUniqueModelIndex(const Model& model);
191 static std::map<std::string, ProtocolCraft::Json::Value> cached_jsons;
192
194 {
195 bool operator()(const std::string* a, const std::string* b) const
196 {
197 return *a < *b;
198 }
199 };
200
201 private:
203
204
205 enum class BlockstateFlags : size_t
206 {
207 Air = 0,
208 Solid,
210 Lava,
211 Water,
217 Climbable,
218 Hazardous,
220 Slime,
221 Bed,
222 SoulSand,
223 Honey,
225 Cobweb,
228 BerryBush,
233 };
234
235 std::bitset<static_cast<size_t>(BlockstateFlags::NUM_FLAGS)> flags;
236 float hardness;
237 float friction;
239 const std::string* m_name;
240
241 std::vector<size_t> models_indices;
242 std::vector<int> models_weights;
244
245 std::vector<BestTool> best_tools;
246
247 std::map<const std::string*, const std::string*, string_ptr_compare> variables; // map is smaller in RAM than unordered_map
248 };
249} // Botcraft
bool IsHazardous() const
bool IsBerryBush() const
Vector3< double > GetClosestPoint(const Position &block_pos, const Vector3< double > &pos) const
Get the closest point on this blockstate placed at block_pos from a reference pos.
static const std::string * GetUniqueStringPtr(const std::string &s)
bool IsWaterOrWaterlogged() const
const std::string & GetName() const
bool IsCobweb() const
BlockstateId GetId() const
bool IsDownBubbleColumn() const
bool IsTransparent() const
bool IsFluidOrWaterlogged() const
bool IsClimbable() const
static std::set< std::string > unique_strings
bool MatchCondition(const std::string &condition) const
Check if a given string condition match this blockstate variables.
std::set< AABB > GetCollidersAtPos(const Position &pos) const
std::vector< size_t > models_indices
unsigned char GetModelId(const Position &pos) const
std::map< const std::string *, const std::string *, string_ptr_compare > variables
void LoadWeightedModels(const std::deque< std::pair< Model, int > > &models_to_load)
float GetHardness() const
bool IsFluidFalling() const
bool IsBubbleColumn() const
TintType GetTintType() const
bool IsPowderSnow() const
Vector3< double > GetHorizontalOffsetAtPos(const Position &pos) const
size_t GetNumModels() const
void LoadProperties(const BlockstateProperties &properties)
static void UpdateModelsWithAtlasData(const Renderer::Atlas *atlas)
std::vector< int > models_weights
std::vector< BestTool > best_tools
bool IsUpBubbleColumn() const
static std::deque< Model > unique_models
bool IsWaterlogged() const
std::bitset< static_cast< size_t >(BlockstateFlags::NUM_FLAGS)> flags
BlockstateId blockstate_id
float GetFriction() const
const std::string * m_name
static void ClearCache()
static size_t GetUniqueModelIndex(const Model &model)
float GetFluidHeight() const
Get fluid height for this block.
const std::string & GetVariableValue(const std::string &variable) const
static std::map< std::string, ProtocolCraft::Json::Value > cached_jsons
bool IsScaffolding() const
bool IsSoulSand() const
const Model & GetModel(const unsigned short index) const
float GetMiningTimeSeconds(const ToolType tool_type, const ToolMaterial tool_material, const float tool_efficiency_additional_speed=0.0f, const unsigned char haste=0, const unsigned char fatigue=0, const bool on_ground=true, const float speed_factor=1.0f) const
Compute the amount of time (in s) required to mine this block.
bool GetBoolFromCondition(const ProtocolCraft::Json::Value &condition) const
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
unsigned int BlockstateId
ToolMaterial min_material
bool climbable
True if can be used as a ladder.
bool powder_snow
True if this block is powder_snow.
ProtocolCraft::Json::Value down_bubble_column
True if this block is a bubble column going down.
bool cobweb
True if this block is cobweb.
bool water
True for water.
std::vector< std::string > variables
bool honey
True if this block is honey.
float friction
Slipperiness coefficient.
std::vector< BestTool > best_tools
bool slime
True if this block is slime.
float horizontal_offset
Max horizontal offset value of the colliders (for bamboo and pointed dripstone)
float hardness
Digging hardness.
bool berry_bush
True if this block is sweet_berry_bush.
ProtocolCraft::Json::Value colliders
bool transparent
True if not a full 1x1x1 block OR at least one face texture has transparency.
ProtocolCraft::Json::Value solid
True if can't go through it.
bool soul_sand
True if this block is soul_sand.
bool custom
True if the model is a custom one (chests/banners etc...)
ProtocolCraft::Json::Value waterlogged
True for blocks that are always waterlogged (kelp, seagrass...)
bool any_tool_harvest
True if this block drops item when broken with no tool.
bool air
True if the block is air (air, cave_air, void and structure_void are counted as air)
bool hazardous
True if block can hurt when walking in/on it.
ProtocolCraft::Json::Value up_bubble_column
True if this block is a bubble column going up.
bool scaffolding
True if this block is scaffolding.
bool bed
True if this block has the BEDS tag.
bool operator()(const std::string *a, const std::string *b) const