Botcraft 1.21.4
Loading...
Searching...
No Matches
LocalPlayer.cpp
Go to the documentation of this file.
2
3#include <limits>
4#include <mutex>
5
6namespace Botcraft
7{
9 {
10 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
11
12 position = Vector3<double>(0.0, std::numeric_limits<double>::quiet_NaN(), 0.0);
13 front_vector = Vector3<double>(0.0, 0.0, 1.0);
14 right_vector = Vector3<double>(1.0, 0.0, 0.0);
15
17 dirty_inputs = false;
18
20
21 invulnerable = false;
22 flying = false;
23 may_fly = false;
24 instabuild = false;
25 may_build = true;
27 flying_speed = 0.05f;
28 walking_speed = 0.1f;
29
30 health = 0.0f;
31 food = 20;
32 food_saturation = 5.0f;
33
34 in_water = false;
35 in_lava = false;
36 under_water = false;
37 crouching = false;
39 on_climbable = false;
41 jump_delay = 0;
44 supporting_block_pos = std::optional<Position>();
46
47 previous_sprinting = false;
49 previous_position = Vector3<double>(0.0, 0.0, 0.0);
50 previous_yaw = 0.0f;
51 previous_pitch = 0.0f;
52 previous_on_ground = false;
53 previous_jump = false;
54 previous_sneak = false;
55 previous_forward = 0.0f;
56#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
59#endif
60 }
61
66
67
69 {
70 std::shared_lock<std::shared_mutex> lock(entity_mutex);
71 return front_vector;
72 }
73
75 {
76 std::shared_lock<std::shared_mutex> lock(entity_mutex);
77 return xz_vector;
78 }
79
81 {
82 std::shared_lock<std::shared_mutex> lock(entity_mutex);
83 return right_vector;
84 }
85
86
88 {
89 std::shared_lock<std::shared_mutex> lock(entity_mutex);
90 return game_mode;
91 }
92
94 {
95 std::shared_lock<std::shared_mutex> lock(entity_mutex);
96 return abilities_flags;
97 }
98
100 {
101 std::shared_lock<std::shared_mutex> lock(entity_mutex);
102 return invulnerable;
103 }
104
106 {
107 std::shared_lock<std::shared_mutex> lock(entity_mutex);
108 return flying;
109 }
110
112 {
113 std::shared_lock<std::shared_mutex> lock(entity_mutex);
114 return may_fly;
115 }
116
118 {
119 std::shared_lock<std::shared_mutex> lock(entity_mutex);
120 return instabuild;
121 }
122
124 {
125 std::shared_lock<std::shared_mutex> lock(entity_mutex);
126 return may_build;
127 }
128
130 {
131 std::shared_lock<std::shared_mutex> lock(entity_mutex);
132 return flying_speed;
133 }
134
136 {
137 std::shared_lock<std::shared_mutex> lock(entity_mutex);
138 return walking_speed;
139 }
140
142 {
143 std::shared_lock<std::shared_mutex> lock(entity_mutex);
144 return health;
145 }
146
148 {
149 std::shared_lock<std::shared_mutex> lock(entity_mutex);
150 return food;
151 }
152
154 {
155 std::shared_lock<std::shared_mutex> lock(entity_mutex);
156 return food_saturation;
157 }
158
160 {
161 std::shared_lock<std::shared_mutex> lock(entity_mutex);
162 return dirty_inputs;
163 }
164
166 {
167 std::shared_lock<std::shared_mutex> lock(entity_mutex);
168 return on_climbable;
169 }
170
172 {
173 std::shared_lock<std::shared_mutex> lock(entity_mutex);
174 return in_water;
175 }
176
178 {
179 std::shared_lock<std::shared_mutex> lock(entity_mutex);
180 return in_lava;
181 }
182
184 {
185 std::shared_lock<std::shared_mutex> lock(entity_mutex);
186 return in_lava || in_water;
187 }
188
189
190 void LocalPlayer::SetGameMode(const GameType game_mode_)
191 {
192 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
193 game_mode = game_mode_;
194 }
195
196 void LocalPlayer::SetAbilitiesFlags(const char abilities_flags_)
197 {
198 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
199 SetAbilitiesFlagsImpl(abilities_flags_);
200 }
201
202 void LocalPlayer::SetFlyingSpeed(const float flying_speed_)
203 {
204 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
205 flying_speed = flying_speed_;
206 }
207
208 void LocalPlayer::SetWalkingSpeed(const float walking_speed_)
209 {
210 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
211 walking_speed = walking_speed_;
212 }
213
214 void LocalPlayer::SetHealth(const float health_)
215 {
216 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
217 health = health_;
218 }
219
220 void LocalPlayer::SetFood(const int food_)
221 {
222 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
223 food = food_;
224 }
225
226 void LocalPlayer::SetFoodSaturation(const float food_saturation_)
227 {
228 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
229 food_saturation = food_saturation_;
230 }
231
233 {
234 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
235 dirty_inputs = true;
236 }
237
238
240 {
241 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
242 position = pos;
243 }
244
245 void LocalPlayer::SetX(const double x)
246 {
247 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
248 position.x = x;
249 }
250
251 void LocalPlayer::SetY(const double y)
252 {
253 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
254 position.y = y;
255 }
256
257 void LocalPlayer::SetZ(const double z)
258 {
259 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
260 position.z = z;
261 }
262
263 void LocalPlayer::SetYaw(const float yaw_)
264 {
265 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
266 if (yaw != yaw_)
267 {
268 yaw = yaw_;
270 }
271 }
272
273 void LocalPlayer::SetPitch(const float pitch_)
274 {
275 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
276 if (pitch != pitch_)
277 {
278 pitch = pitch_;
280 }
281 }
282
284 {
285 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
286 inputs.forward_axis = std::clamp(f, -1.0f, 1.0f);
287 dirty_inputs = true;
288 }
289
291 {
292 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
293 inputs.forward_axis = std::clamp(inputs.forward_axis + f, -1.0f, 1.0f);
294 dirty_inputs = true;
295 }
296
297 void LocalPlayer::SetInputsLeft(const float f)
298 {
299 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
300 inputs.left_axis = std::clamp(f, -1.0f, 1.0f);
301 dirty_inputs = true;
302 }
303
304 void LocalPlayer::AddInputsLeft(const float f)
305 {
306 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
307 inputs.left_axis = std::clamp(inputs.left_axis + f, -1.0f, 1.0f);
308 dirty_inputs = true;
309 }
310
311 void LocalPlayer::SetInputsJump(const bool b)
312 {
313 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
314 inputs.jump = b;
315 dirty_inputs = true;
316 }
317
319 {
320 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
321 inputs.sneak = b;
322 dirty_inputs = true;
323 }
324
326 {
327 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
328 inputs.sprint = b;
329 dirty_inputs = true;
330 }
331
333 {
334 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
335 inputs = inputs_;
336 dirty_inputs = true;
337 }
338
340 {
341 return true;
342 }
343
345 {
346 return false;
347 }
348
349 void LocalPlayer::LookAt(const Vector3<double>& pos, const bool set_pitch)
350 {
351 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
352 Vector3<double> direction = (pos - position);
353 // We want to set the orientation from the eyes, not the feet
354 direction.y -= GetEyeHeightImpl();
355 direction.Normalize();
356 const float new_pitch = static_cast<float>(-std::asin(direction.y) * 57.29577951308232 /* 180/PI */);
357 float new_yaw = static_cast<float>(-std::atan2(direction.x, direction.z) * 57.29577951308232 /* 180/PI */);
358 if (new_yaw < 0)
359 {
360 new_yaw += 360.0f;
361 }
362 if (new_yaw != yaw ||
363 (set_pitch && new_pitch != pitch))
364 {
365 if (set_pitch)
366 {
367 pitch = new_pitch;
368 }
369 yaw = new_yaw;
371 }
372 }
373
375 {
376 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
377 const float rad_pitch = pitch * 0.017453292f /* PI/180 */;
378 const float cos_pitch = std::cos(rad_pitch);
379 front_vector = Vector3<double>(-std::sin(rad_yaw) * cos_pitch, -std::sin(rad_pitch), std::cos(rad_yaw) * cos_pitch);
380
381 // Vector is already normalized as front_vector² = (cos²(rad_yaw) + sin²(rad_yaw)) * cos²(rad_pitch) + sin²(rad_pitch) = 1
382 // front_vector.Normalize();
383
385 xz_vector.y = 0.0;
386 // xzVector.Norm() = sqrt((cos²(rad_yaw) + sin²(rad_yaw)) * cos²(rad_pitch)) = sqrt(cos²(rad_pitch)) = cos(rad_pitch)
387 // xzVector.Normalize();
388 xz_vector = xz_vector / cos_pitch;
389
390 //Right = crossproduct(front, (0, 1.0, 0)).Normalize()
391 right_vector = Vector3<double>(-front_vector.z / cos_pitch, 0.0, front_vector.x / cos_pitch);
392 }
393
395 {
396 inputs.forward_axis = 0.0f;
397 inputs.left_axis = 0.0f;
398 inputs.jump = false;
399 inputs.sneak = false;
400 inputs.sprint = false;
401 dirty_inputs = false;
402 }
403
404 void LocalPlayer::SetAbilitiesFlagsImpl(const char abilities_flags_)
405 {
406 abilities_flags = abilities_flags_;
408 flying = abilities_flags & 0x02;
409 may_fly = abilities_flags & 0x04;
411 may_build = abilities_flags & 0x10;
412 }
413
415 {
417 (invulnerable << 0) |
418 (flying << 1) |
419 (may_fly << 2) |
420 (instabuild << 3) |
421 (may_build << 4);
422 }
423
424#if PROTOCOL_VERSION < 405 /* < 1.14 */
425 double LocalPlayer::GetEyeHeightImpl() const
426 {
427 return crouching ? 1.5575 : 1.62;
428 }
429
430 double LocalPlayer::GetHeightImpl() const
431 {
432 return crouching ? 1.65 : 1.8;
433 }
434#endif
435
436} //Botcraft
virtual double GetHeightImpl() const
Definition Entity.cpp:2004
std::shared_mutex entity_mutex
Definition Entity.hpp:253
Vector3< double > position
Definition Entity.hpp:257
void SetFoodSaturation(const float food_saturation_)
void SetAbilitiesFlags(const char abilities_flags_)
void SetInputsLeft(const float f)
float GetFoodSaturation() const
void SetFood(const int food_)
virtual bool IsLocalPlayer() const override
virtual void SetPitch(const float pitch_) override
Set Pitch angle (look up is -90°, look down is 90°)
virtual void SetPosition(const Vector3< double > &pos) override
void SetInputsForward(const float f)
virtual void SetZ(const double z) override
float GetFlyingSpeed() const
void SetInputsSprint(const bool b)
Vector3< double > GetRightVector() const
PlayerInputs last_sent_inputs
char GetAbilitiesFlags() const
void SetFlyingSpeed(const float flying_speed_)
Vector3< double > stuck_speed_multiplier
virtual void SetY(const double y) override
void AddInputsForward(const float f)
void SetAbilitiesFlagsImpl(const char abilities_flags_)
bool GetInvulnerable() const
void SetWalkingSpeed(const float walking_speed_)
bool flying
Flying in creative/spectator.
Vector3< double > right_vector
bool GetDirtyInputs() const
Vector3< double > GetXZVector() const
GameType GetGameMode() const
void SetInputs(const PlayerInputs &inputs_)
void AddInputsLeft(const float f)
std::optional< Position > supporting_block_pos
virtual void SetX(const double x) override
void SetHealth(const float health_)
virtual void SetYaw(const float yaw_) override
Set Yaw angle (horizontal plane orientation)
virtual bool IsRemotePlayer() const override
void SetInputsJump(const bool b)
void LookAt(const Vector3< double > &pos, const bool set_pitch=false)
float GetWalkingSpeed() const
Vector3< double > GetFrontVector() const
Vector3< double > xz_vector
bool may_fly
If this player can fly in creative/spectator.
void SetGameMode(const GameType game_mode_)
bool instabuild
Insta break blocks in creative.
Vector3< double > front_vector
bool GetInstabuild() const
If true, the player can break any block in no time (creative mode)
Vector3< double > previous_position
void SetInputsSneak(const bool b)
float GetHealth() const
virtual double GetEyeHeightImpl() const