Botcraft 26.3
Loading...
Searching...
No Matches
EntityAttribute.cpp
Go to the documentation of this file.
3
4namespace Botcraft
5{
6 EntityAttribute::EntityAttribute(const Type type_, const double base_value_)
7 {
8 type = type_;
9 base_value = base_value_;
11 up_to_date = false;
12 }
13
14 void EntityAttribute::SetBaseValue(const double new_value)
15 {
16 if (new_value == base_value)
17 {
18 return;
19 }
20 base_value = new_value;
21 up_to_date = false;
22 }
23
25 {
26 modifiers.clear();
27 up_to_date = false;
28 }
29
31 {
32 // If no element is removed, value is still up to date
33 if (modifiers.erase(key) > 0)
34 {
35 up_to_date = false;
36 }
37 }
38
39 void EntityAttribute::SetModifier(const ModifierKey& key, const Modifier& modifier)
40 {
41 modifiers[key] = modifier;
42 up_to_date = false;
43 }
44
49
51 {
52 return base_value;
53 }
54
55 const std::map<EntityAttribute::ModifierKey, EntityAttribute::Modifier>& EntityAttribute::GetModifiers() const
56 {
57 return modifiers;
58 }
59
61 {
62 if (!up_to_date)
63 {
65 }
66 return current_value;
67 }
68
69 std::optional<EntityAttribute::Modifier> EntityAttribute::GetModifier(const ModifierKey& key)
70 {
71 auto it = modifiers.find(key);
72 if (it == modifiers.end())
73 {
74 return std::optional<Modifier>();
75 }
76 return it->second;
77 }
78
79#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
80 std::string EntityAttribute::TypeToString(const Type type)
81 {
82 switch (type)
83 {
84#if PROTOCOL_VERSION < 735 /* < 1.16 */
85 case Type::MaxHealth:
86 return "generic.maxHealth";
88 return "generic.followRange";
90 return "generic.knockbackResistance";
92 return "generic.movementSpeed";
94 return "generic.flyingSpeed";
96 return "generic.attackDamage";
97#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
99 return "generic.attackKnockback";
100#endif
102 return "generic.attackSpeed";
103 case Type::Armor:
104 return "generic.armor";
106 return "generic.armorToughness";
107 case Type::Luck:
108 return "generic.luck";
110 return "zombie.spawnReinforcements";
111 case Type::HorseJumpStrength:
112 return "horse.jumpStrength";
113#else
114 case Type::MaxHealth:
115 return "minecraft:generic.max_health";
117 return "minecraft:generic.follow_range";
119 return "minecraft:generic.knockback_resistance";
121 return "minecraft:generic.movement_speed";
123 return "minecraft:generic.flying_speed";
125 return "minecraft:generic.attack_damage";
127 return "minecraft:generic.attack_knockback";
129 return "minecraft:generic.attack_speed";
130 case Type::Armor:
131 return "minecraft:generic.armor";
133 return "minecraft:generic.armor_toughness";
134 case Type::Luck:
135 return "minecraft:generic.luck";
136#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
138 return "minecraft:generic.max_absorption";
139#endif
141 return "minecraft:zombie.spawn_reinforcements";
142 case Type::HorseJumpStrength:
143 return "minecraft:horse.jump_strength";
144#endif
145 default:
146 return "";
147 }
148 }
149
150 EntityAttribute::Type EntityAttribute::StringToType(const std::string& s)
151 {
152
153#if PROTOCOL_VERSION < 735 /* < 1.16 */
154 if (s == "generic.maxHealth")
155 {
157 }
158 if (s == "generic.followRange")
159 {
161 }
162 if (s == "generic.knockbackResistance")
163 {
165 }
166 if (s == "generic.movementSpeed")
167 {
169 }
170 if (s == "generic.flyingSpeed")
171 {
173 }
174 if (s == "generic.attackDamage")
175 {
177 }
178#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
179 if (s == "generic.attackKnockback")
180 {
182 }
183#endif
184 if (s == "generic.attackSpeed")
185 {
187 }
188 if (s == "generic.armor")
189 {
191 }
192 if (s == "generic.armorToughness")
193 {
195 }
196 if (s == "generic.luck")
197 {
199 }
200 if (s == "zombie.spawnReinforcements")
201 {
203 }
204 if (s == "horse.jumpStrength")
205 {
206 return Type::HorseJumpStrength;
207 }
208#else
209 if (s == "minecraft:generic.max_health")
210 {
211 return Type::MaxHealth;
212 }
213 if (s == "minecraft:generic.follow_range")
214 {
215 return Type::FollowRange;
216 }
217 if (s == "minecraft:generic.knockback_resistance")
218 {
220 }
221 if (s == "minecraft:generic.movement_speed")
222 {
223 return Type::MovementSpeed;
224 }
225 if (s == "minecraft:generic.flying_speed")
226 {
227 return Type::FlyingSpeed;
228 }
229 if (s == "minecraft:generic.attack_damage")
230 {
231 return Type::AttackDamage;
232 }
233 if (s == "minecraft:generic.attack_knockback")
234 {
236 }
237 if (s == "minecraft:generic.attack_speed")
238 {
239 return Type::AttackSpeed;
240 }
241 if (s == "minecraft:generic.armor")
242 {
243 return Type::Armor;
244 }
245 if (s == "minecraft:generic.armor_toughness")
246 {
248 }
249 if (s == "minecraft:generic.luck")
250 {
251 return Type::Luck;
252 }
253#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
254 if (s == "minecraft:generic.max_absorption")
255 {
256 return Type::MaxAbsorption;
257 }
258#endif
259 if (s == "minecraft:zombie.spawn_reinforcements")
260 {
262 }
263 if (s == "minecraft:horse.jump_strength")
264 {
265 return Type::HorseJumpStrength;
266 }
267#endif
268 return Type::Unknown;
269 }
270#endif
271
273 {
275 for (const auto& [uuid, m] : modifiers)
276 {
277 if (m.operation != Modifier::Operation::Add)
278 {
279 continue;
280 }
281 current_value += m.amount;
282 }
283
284 double current_mult = 1.0;
285 for (const auto& [uuid, m] : modifiers)
286 {
287 if (m.operation != Modifier::Operation::MultiplyBase)
288 {
289 continue;
290 }
291 current_mult += m.amount;
292 }
293 current_value *= current_mult;
294
295 for (const auto& [uuid, m] : modifiers)
296 {
297 if (m.operation != Modifier::Operation::MultiplyTotal)
298 {
299 continue;
300 }
301 current_value *= (1.0 + m.amount);
302 }
303
304 switch (type)
305 {
306 case Type::MaxHealth:
307 current_value = std::clamp(current_value, 1.0, 1024.0);
308 break;
310 current_value = std::clamp(current_value, 0.0, 2048.0);
311 break;
313#if PROTOCOL_VERSION < 776 /* < 26.2 */
314 current_value = std::clamp(current_value, 0.0, 1.0);
315#else
316 current_value = std::clamp(current_value, -2.0, 1.0);
317#endif
318 break;
320 current_value = std::clamp(current_value, 0.0, 1024.0);
321 break;
323 current_value = std::clamp(current_value, 0.0, 1024.0);
324 break;
326 current_value = std::clamp(current_value, 0.0, 2048.0);
327 break;
328#if PROTOCOL_VERSION > 404 /* > 1.13.2 */
330 current_value = std::clamp(current_value, 0.0, 5.0);
331 break;
332#endif
334 current_value = std::clamp(current_value, 0.0, 1024.0);
335 break;
336 case Type::Armor:
337 current_value = std::clamp(current_value, 0.0, 30.0);
338 break;
340 current_value = std::clamp(current_value, 0.0, 20.0);
341 break;
342 case Type::Luck:
343 current_value = std::clamp(current_value, -1024.0, 1024.0);
344 break;
345#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
347 current_value = std::clamp(current_value, 0.0, 2048.0);
348 break;
349#endif
351 current_value = std::clamp(current_value, 0.0, 1.0);
352 break;
353#if PROTOCOL_VERSION < 766 /* < 1.20.5 */
354 case Type::HorseJumpStrength:
355 current_value = std::clamp(current_value, 0.0, 2.0);
356 break;
357#endif
358#if PROTOCOL_VERSION > 765 /* > 1.20.4 */
360 current_value = std::clamp(current_value, 0.0, 1024.0);
361 break;
363 current_value = std::clamp(current_value, 0.0, 64.0);
364 break;
366 current_value = std::clamp(current_value, 0.0, 64.0);
367 break;
369 current_value = std::clamp(current_value, 0.0, 100.0);
370 break;
371 case Type::Gravity:
372 current_value = std::clamp(current_value, -1.0, 1.0);
373 break;
375 current_value = std::clamp(current_value, 0.0, 32.0);
376 break;
378 current_value = std::clamp(current_value, -1024.0, 1024.0);
379 break;
380 case Type::Scale:
381 current_value = std::clamp(current_value, 0.0625, 16.0);
382 break;
383 case Type::StepHeight:
384 current_value = std::clamp(current_value, 0.0, 10.0);
385 break;
386#endif
387#if PROTOCOL_VERSION > 766 /* > 1.20.6 */
389 current_value = std::clamp(current_value, 0.0, 1024.0);
390 break;
392 current_value = std::clamp(current_value, 0.0, 1.0);
393 break;
395 current_value = std::clamp(current_value, 0.0, 1024.0);
396 break;
398 current_value = std::clamp(current_value, 0.0, 1.0);
399 break;
401 current_value = std::clamp(current_value, 0.0, 1024.0);
402 break;
404 current_value = std::clamp(current_value, 0.0, 1.0);
405 break;
407 current_value = std::clamp(current_value, 0.0, 20.0);
408 break;
410 current_value = std::clamp(current_value, 0.0, 1.0);
411 break;
413 current_value = std::clamp(current_value, 0.0, 1.0);
414 break;
415#endif
416#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
417 case Type::TemptRange:
418 current_value = std::clamp(current_value, 0.0, 2048.0);
419 break;
420#endif
421#if PROTOCOL_VERSION > 770 /* > 1.21.5 */
423 current_value = std::clamp(current_value, 0.0, 32.0);
424 break;
426 current_value = std::clamp(current_value, 0.0, 6.0e7);
427 break;
429 current_value = std::clamp(current_value, 0.0, 6.0e7);
430 break;
431#endif
432#if PROTOCOL_VERSION > 775 /* > 26.1.2 */
434 current_value = std::clamp(current_value, 0.0, 2048.0);
435 break;
437 current_value = std::clamp(current_value, 0.0, 512.0);
438 break;
439 case Type::Bounciness:
440 current_value = std::clamp(current_value, 0.0, 1.0);
441 break;
443 current_value = std::clamp(current_value, 0.0, 2048.0);
444 break;
446 current_value = std::clamp(current_value, 0.0, 512.0);
447 break;
448#endif
449 default:
450 // No min or max value in case of Unknown attribute
451 break;
452 }
453
454 up_to_date = true;
455 }
456
458}
#define DEFINE_ENUM_STRINGIFYER_RANGE(Enum, min_value, max_value)
void RemoveModifier(const ModifierKey &key)
const std::map< ModifierKey, Modifier > & GetModifiers() const
std::optional< Modifier > GetModifier(const ModifierKey &key)
void SetBaseValue(const double new_value)
void SetModifier(const ModifierKey &key, const Modifier &modifier)
std::map< ModifierKey, Modifier > modifiers