Botcraft 26.3
Loading...
Searching...
No Matches
LocalPlayer.cpp
Go to the documentation of this file.
3
4#include <limits>
5#include <mutex>
6
7namespace Botcraft
8{
10 {
11 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
12
13 position = Vector3<double>(0.0, std::numeric_limits<double>::quiet_NaN(), 0.0);
14
16 dirty_inputs = false;
17
19
20 invulnerable = false;
21 flying = false;
22 may_fly = false;
23 instabuild = false;
24 may_build = true;
26 flying_speed = 0.05f;
27 walking_speed = 0.1f;
28
29 health = 0.0f;
30 food = 20;
31 food_saturation = 5.0f;
32
33 in_water = false;
34 in_lava = false;
35 under_water = false;
36 crouching = false;
38 on_climbable = false;
40 jump_delay = 0;
43 supporting_block_pos = std::optional<Position>();
45 pending_teleport_id = std::nullopt;
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 GetFrontVectorImpl();
72 }
73
75 {
76 std::shared_lock<std::shared_mutex> lock(entity_mutex);
77 return GetXZVectorImpl();
78 }
79
81 {
82 std::shared_lock<std::shared_mutex> lock(entity_mutex);
83 return GetRightVectorImpl();
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_;
269 }
270 }
271
272 void LocalPlayer::SetPitch(const float pitch_)
273 {
274 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
275 if (pitch != pitch_)
276 {
277 pitch = pitch_;
278 }
279 }
280
282 {
283 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
284 inputs.forward_axis = std::clamp(f, -1.0f, 1.0f);
285 dirty_inputs = true;
286 }
287
289 {
290 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
291 inputs.forward_axis = std::clamp(inputs.forward_axis + f, -1.0f, 1.0f);
292 dirty_inputs = true;
293 }
294
295 void LocalPlayer::SetInputsLeft(const float f)
296 {
297 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
298 inputs.left_axis = std::clamp(f, -1.0f, 1.0f);
299 dirty_inputs = true;
300 }
301
302 void LocalPlayer::AddInputsLeft(const float f)
303 {
304 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
305 inputs.left_axis = std::clamp(inputs.left_axis + f, -1.0f, 1.0f);
306 dirty_inputs = true;
307 }
308
309 void LocalPlayer::SetInputsJump(const bool b)
310 {
311 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
312 inputs.jump = b;
313 dirty_inputs = true;
314 }
315
317 {
318 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
319 inputs.sneak = b;
320 dirty_inputs = true;
321 }
322
324 {
325 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
326 inputs.sprint = b;
327 dirty_inputs = true;
328 }
329
331 {
332 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
333 inputs = inputs_;
334 dirty_inputs = true;
335 }
336
338 {
339 return true;
340 }
341
343 {
344 return false;
345 }
346
347 void LocalPlayer::LookAt(const Vector3<double>& pos, const bool set_pitch)
348 {
349 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
350 Vector3<double> direction = (pos - position);
351 // We want to set the orientation from the eyes, not the feet
352 direction.y -= GetEyeHeightImpl();
353 direction.Normalize();
354 const float new_pitch = static_cast<float>(-std::asin(direction.y) * 57.29577951308232 /* 180/PI */);
355 float new_yaw = static_cast<float>(-std::atan2(direction.x, direction.z) * 57.29577951308232 /* 180/PI */);
356 if (new_yaw < 0)
357 {
358 new_yaw += 360.0f;
359 }
360 if (new_yaw != yaw ||
361 (set_pitch && new_pitch != pitch))
362 {
363 if (set_pitch)
364 {
365 pitch = new_pitch;
366 }
367 yaw = new_yaw;
368 }
369 }
370
372 {
373 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
374 const float rad_pitch = pitch * 0.017453292f /* PI/180 */;
375 const float cos_pitch = CosLUT(rad_pitch);
376 return Vector3<double>(-SinLUT(rad_yaw) * cos_pitch, -SinLUT(rad_pitch), CosLUT(rad_yaw) * cos_pitch);
377 }
378
380 {
381 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
382 const float rad_pitch = pitch * 0.017453292f /* PI/180 */;
383 const float cos_pitch = CosLUT(rad_pitch);
384 return -SinLUT(rad_yaw) * cos_pitch;
385 }
386
388 {
389 const float rad_pitch = pitch * 0.017453292f /* PI/180 */;
390 return -SinLUT(rad_pitch);
391 }
392
394 {
395 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
396 const float rad_pitch = pitch * 0.017453292f /* PI/180 */;
397 const float cos_pitch = CosLUT(rad_pitch);
398 return CosLUT(rad_yaw) * cos_pitch;
399 }
400
402 {
403 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
404 return Vector3<double>(-SinLUT(rad_yaw), 0.0, CosLUT(rad_yaw));
405 }
406
408 {
409 // Right = crossproduct(front, (0, 1.0, 0)).Normalize()
410 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
411 return Vector3<double>(-CosLUT(rad_yaw), 0.0, -SinLUT(rad_yaw));
412 }
413
415 {
416 inputs.forward_axis = 0.0f;
417 inputs.left_axis = 0.0f;
418 inputs.jump = false;
419 inputs.sneak = false;
420 inputs.sprint = false;
421 dirty_inputs = false;
422 }
423
424 void LocalPlayer::SetAbilitiesFlagsImpl(const char abilities_flags_)
425 {
426 abilities_flags = abilities_flags_;
428 flying = abilities_flags & 0x02;
429 may_fly = abilities_flags & 0x04;
431 may_build = abilities_flags & 0x10;
432 }
433
435 {
437 (invulnerable << 0) |
438 (flying << 1) |
439 (may_fly << 2) |
440 (instabuild << 3) |
441 (may_build << 4);
442 }
443
444#if PROTOCOL_VERSION < 405 /* < 1.14 */
445 double LocalPlayer::GetEyeHeightImpl() const
446 {
447 return crouching ? 1.5575 : 1.62;
448 }
449
450 double LocalPlayer::GetHeightImpl() const
451 {
452 return crouching ? 1.65 : 1.8;
453 }
454#endif
455
456} //Botcraft
float SinLUT(const double d)
float CosLUT(const double d)
virtual double GetHeightImpl() const
Definition Entity.cpp:2248
std::shared_mutex entity_mutex
Definition Entity.hpp:320
Vector3< double > position
Definition Entity.hpp:324
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_)
double GetFrontVectorXImpl() const
Vector3< double > GetXZVectorImpl() const
virtual bool IsLocalPlayer() const override
Vector3< double > GetFrontVectorImpl() const
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_)
double GetFrontVectorYImpl() const
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.
bool GetDirtyInputs() const
Vector3< double > GetXZVector() const
GameType GetGameMode() const
void SetInputs(const PlayerInputs &inputs_)
void AddInputsLeft(const float f)
Vector3< double > GetRightVectorImpl() const
std::optional< int > pending_teleport_id
double GetFrontVectorZImpl() const
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
bool may_fly
If this player can fly in creative/spectator.
void SetGameMode(const GameType game_mode_)
bool instabuild
Insta break blocks in creative.
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