Botcraft 26.2
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 front_vector = Vector3<double>(0.0, 0.0, 1.0);
15 right_vector = Vector3<double>(1.0, 0.0, 0.0);
16
18 dirty_inputs = false;
19
21
22 invulnerable = false;
23 flying = false;
24 may_fly = false;
25 instabuild = false;
26 may_build = true;
28 flying_speed = 0.05f;
29 walking_speed = 0.1f;
30
31 health = 0.0f;
32 food = 20;
33 food_saturation = 5.0f;
34
35 in_water = false;
36 in_lava = false;
37 under_water = false;
38 crouching = false;
40 on_climbable = false;
42 jump_delay = 0;
45 supporting_block_pos = std::optional<Position>();
47
48 previous_sprinting = false;
50 previous_position = Vector3<double>(0.0, 0.0, 0.0);
51 previous_yaw = 0.0f;
52 previous_pitch = 0.0f;
53 previous_on_ground = false;
54 previous_jump = false;
55 previous_sneak = false;
56 previous_forward = 0.0f;
57#if PROTOCOL_VERSION > 767 /* > 1.21.1 */
60#endif
61 }
62
67
68
70 {
71 std::shared_lock<std::shared_mutex> lock(entity_mutex);
72 return front_vector;
73 }
74
76 {
77 std::shared_lock<std::shared_mutex> lock(entity_mutex);
78 return xz_vector;
79 }
80
82 {
83 std::shared_lock<std::shared_mutex> lock(entity_mutex);
84 return right_vector;
85 }
86
87
89 {
90 std::shared_lock<std::shared_mutex> lock(entity_mutex);
91 return game_mode;
92 }
93
95 {
96 std::shared_lock<std::shared_mutex> lock(entity_mutex);
97 return abilities_flags;
98 }
99
101 {
102 std::shared_lock<std::shared_mutex> lock(entity_mutex);
103 return invulnerable;
104 }
105
107 {
108 std::shared_lock<std::shared_mutex> lock(entity_mutex);
109 return flying;
110 }
111
113 {
114 std::shared_lock<std::shared_mutex> lock(entity_mutex);
115 return may_fly;
116 }
117
119 {
120 std::shared_lock<std::shared_mutex> lock(entity_mutex);
121 return instabuild;
122 }
123
125 {
126 std::shared_lock<std::shared_mutex> lock(entity_mutex);
127 return may_build;
128 }
129
131 {
132 std::shared_lock<std::shared_mutex> lock(entity_mutex);
133 return flying_speed;
134 }
135
137 {
138 std::shared_lock<std::shared_mutex> lock(entity_mutex);
139 return walking_speed;
140 }
141
143 {
144 std::shared_lock<std::shared_mutex> lock(entity_mutex);
145 return health;
146 }
147
149 {
150 std::shared_lock<std::shared_mutex> lock(entity_mutex);
151 return food;
152 }
153
155 {
156 std::shared_lock<std::shared_mutex> lock(entity_mutex);
157 return food_saturation;
158 }
159
161 {
162 std::shared_lock<std::shared_mutex> lock(entity_mutex);
163 return dirty_inputs;
164 }
165
167 {
168 std::shared_lock<std::shared_mutex> lock(entity_mutex);
169 return on_climbable;
170 }
171
173 {
174 std::shared_lock<std::shared_mutex> lock(entity_mutex);
175 return in_water;
176 }
177
179 {
180 std::shared_lock<std::shared_mutex> lock(entity_mutex);
181 return in_lava;
182 }
183
185 {
186 std::shared_lock<std::shared_mutex> lock(entity_mutex);
187 return in_lava || in_water;
188 }
189
190
191 void LocalPlayer::SetGameMode(const GameType game_mode_)
192 {
193 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
194 game_mode = game_mode_;
195 }
196
197 void LocalPlayer::SetAbilitiesFlags(const char abilities_flags_)
198 {
199 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
200 SetAbilitiesFlagsImpl(abilities_flags_);
201 }
202
203 void LocalPlayer::SetFlyingSpeed(const float flying_speed_)
204 {
205 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
206 flying_speed = flying_speed_;
207 }
208
209 void LocalPlayer::SetWalkingSpeed(const float walking_speed_)
210 {
211 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
212 walking_speed = walking_speed_;
213 }
214
215 void LocalPlayer::SetHealth(const float health_)
216 {
217 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
218 health = health_;
219 }
220
221 void LocalPlayer::SetFood(const int food_)
222 {
223 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
224 food = food_;
225 }
226
227 void LocalPlayer::SetFoodSaturation(const float food_saturation_)
228 {
229 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
230 food_saturation = food_saturation_;
231 }
232
234 {
235 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
236 dirty_inputs = true;
237 }
238
239
241 {
242 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
243 position = pos;
244 }
245
246 void LocalPlayer::SetX(const double x)
247 {
248 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
249 position.x = x;
250 }
251
252 void LocalPlayer::SetY(const double y)
253 {
254 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
255 position.y = y;
256 }
257
258 void LocalPlayer::SetZ(const double z)
259 {
260 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
261 position.z = z;
262 }
263
264 void LocalPlayer::SetYaw(const float yaw_)
265 {
266 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
267 if (yaw != yaw_)
268 {
269 yaw = yaw_;
271 }
272 }
273
274 void LocalPlayer::SetPitch(const float pitch_)
275 {
276 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
277 if (pitch != pitch_)
278 {
279 pitch = pitch_;
281 }
282 }
283
285 {
286 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
287 inputs.forward_axis = std::clamp(f, -1.0f, 1.0f);
288 dirty_inputs = true;
289 }
290
292 {
293 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
294 inputs.forward_axis = std::clamp(inputs.forward_axis + f, -1.0f, 1.0f);
295 dirty_inputs = true;
296 }
297
298 void LocalPlayer::SetInputsLeft(const float f)
299 {
300 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
301 inputs.left_axis = std::clamp(f, -1.0f, 1.0f);
302 dirty_inputs = true;
303 }
304
305 void LocalPlayer::AddInputsLeft(const float f)
306 {
307 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
308 inputs.left_axis = std::clamp(inputs.left_axis + f, -1.0f, 1.0f);
309 dirty_inputs = true;
310 }
311
312 void LocalPlayer::SetInputsJump(const bool b)
313 {
314 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
315 inputs.jump = b;
316 dirty_inputs = true;
317 }
318
320 {
321 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
322 inputs.sneak = b;
323 dirty_inputs = true;
324 }
325
327 {
328 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
329 inputs.sprint = b;
330 dirty_inputs = true;
331 }
332
334 {
335 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
336 inputs = inputs_;
337 dirty_inputs = true;
338 }
339
341 {
342 return true;
343 }
344
346 {
347 return false;
348 }
349
350 void LocalPlayer::LookAt(const Vector3<double>& pos, const bool set_pitch)
351 {
352 std::scoped_lock<std::shared_mutex> lock(entity_mutex);
353 Vector3<double> direction = (pos - position);
354 // We want to set the orientation from the eyes, not the feet
355 direction.y -= GetEyeHeightImpl();
356 direction.Normalize();
357 const float new_pitch = static_cast<float>(-std::asin(direction.y) * 57.29577951308232 /* 180/PI */);
358 float new_yaw = static_cast<float>(-std::atan2(direction.x, direction.z) * 57.29577951308232 /* 180/PI */);
359 if (new_yaw < 0)
360 {
361 new_yaw += 360.0f;
362 }
363 if (new_yaw != yaw ||
364 (set_pitch && new_pitch != pitch))
365 {
366 if (set_pitch)
367 {
368 pitch = new_pitch;
369 }
370 yaw = new_yaw;
372 }
373 }
374
376 {
377 const float rad_yaw = yaw * 0.017453292f /* PI/180 */;
378 const float rad_pitch = pitch * 0.017453292f /* PI/180 */;
379 const float cos_pitch = CosLUT(rad_pitch);
380 front_vector = Vector3<double>(-SinLUT(rad_yaw) * cos_pitch, -SinLUT(rad_pitch), CosLUT(rad_yaw) * cos_pitch);
381
382 // Vector is already normalized as front_vector² = (cos²(rad_yaw) + sin²(rad_yaw)) * cos²(rad_pitch) + sin²(rad_pitch) = 1
383 // front_vector.Normalize();
384
386 xz_vector.y = 0.0;
387 // xzVector.Norm() = sqrt((cos²(rad_yaw) + sin²(rad_yaw)) * cos²(rad_pitch)) = sqrt(cos²(rad_pitch)) = cos(rad_pitch)
388 // xzVector.Normalize();
389 xz_vector = xz_vector / cos_pitch;
390
391 //Right = crossproduct(front, (0, 1.0, 0)).Normalize()
392 right_vector = Vector3<double>(-front_vector.z / cos_pitch, 0.0, front_vector.x / cos_pitch);
393 }
394
396 {
397 inputs.forward_axis = 0.0f;
398 inputs.left_axis = 0.0f;
399 inputs.jump = false;
400 inputs.sneak = false;
401 inputs.sprint = false;
402 dirty_inputs = false;
403 }
404
405 void LocalPlayer::SetAbilitiesFlagsImpl(const char abilities_flags_)
406 {
407 abilities_flags = abilities_flags_;
409 flying = abilities_flags & 0x02;
410 may_fly = abilities_flags & 0x04;
412 may_build = abilities_flags & 0x10;
413 }
414
416 {
418 (invulnerable << 0) |
419 (flying << 1) |
420 (may_fly << 2) |
421 (instabuild << 3) |
422 (may_build << 4);
423 }
424
425#if PROTOCOL_VERSION < 405 /* < 1.14 */
426 double LocalPlayer::GetEyeHeightImpl() const
427 {
428 return crouching ? 1.5575 : 1.62;
429 }
430
431 double LocalPlayer::GetHeightImpl() const
432 {
433 return crouching ? 1.65 : 1.8;
434 }
435#endif
436
437} //Botcraft
float SinLUT(const double d)
float CosLUT(const double d)
virtual double GetHeightImpl() const
Definition Entity.cpp:2233
std::shared_mutex entity_mutex
Definition Entity.hpp:270
Vector3< double > position
Definition Entity.hpp:274
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