Minetest  5.4.0
localplayer.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include "player.h"
23 #include "environment.h"
24 #include "constants.h"
25 #include "settings.h"
26 #include <list>
27 
28 class Client;
29 class Environment;
30 class GenericCAO;
31 class ClientActiveObject;
32 class ClientEnvironment;
33 class IGameDef;
34 struct collisionMoveResult;
35 
37 {
41  WD_ANIM
42 }; // no local animation, walking, digging, both
43 
44 class LocalPlayer : public Player
45 {
46 public:
47  LocalPlayer(Client *client, const char *name);
48  virtual ~LocalPlayer() = default;
49 
50  // Initialize hp to 0, so that no hearts will be shown if server
51  // doesn't support health points
52  u16 hp = 0;
53  bool touching_ground = false;
54  // This oscillates so that the player jumps a bit above the surface
55  bool in_liquid = false;
56  // This is more stable and defines the maximum speed of the player
57  bool in_liquid_stable = false;
58  // Gets the viscosity of water to calculate friction
60  bool is_climbing = false;
61  bool swimming_vertical = false;
62  bool swimming_pitch = false;
63 
64  float physics_override_speed = 1.0f;
65  float physics_override_jump = 1.0f;
69  // Temporary option for old move code
71 
72  void move(f32 dtime, Environment *env, f32 pos_max_d);
73  void move(f32 dtime, Environment *env, f32 pos_max_d,
74  std::vector<CollisionInfo> *collision_info);
75  // Temporary option for old move code
76  void old_move(f32 dtime, Environment *env, f32 pos_max_d,
77  std::vector<CollisionInfo> *collision_info);
78 
79  void applyControl(float dtime, Environment *env);
80 
83 
84  // Used to check if anything changed and prevent sending packets if not
87  float last_pitch = 0.0f;
88  float last_yaw = 0.0f;
89  unsigned int last_keyPressed = 0;
92 
93  float camera_impact = 0.0f;
94 
95  bool makes_footstep_sound = true;
96 
98  float last_animation_speed = 0.0f;
99 
100  std::string hotbar_image = "";
101  std::string hotbar_selected_image = "";
102 
103  video::SColor light_color = video::SColor(255, 255, 255, 255);
104 
105  float hurt_tilt_timer = 0.0f;
106  float hurt_tilt_strength = 0.0f;
107 
108  GenericCAO *getCAO() const { return m_cao; }
109 
110  ClientActiveObject *getParent() const;
111 
112  void setCAO(GenericCAO *toset)
113  {
114  assert(!m_cao); // Pre-condition
115  m_cao = toset;
116  }
117 
118  u32 maxHudId() const { return hud.size(); }
119 
120  u16 getBreath() const { return m_breath; }
121  void setBreath(u16 breath) { m_breath = breath; }
122 
123  v3s16 getLightPosition() const;
124 
125  void setYaw(f32 yaw) { m_yaw = yaw; }
126  f32 getYaw() const { return m_yaw; }
127 
128  void setPitch(f32 pitch) { m_pitch = pitch; }
129  f32 getPitch() const { return m_pitch; }
130 
131  inline void setPosition(const v3f &position)
132  {
133  m_position = position;
134  m_sneak_node_exists = false;
135  }
136 
137  v3f getPosition() const { return m_position; }
138 
139  // Non-transformed eye offset getters
140  // For accurate positions, use the Camera functions
141  v3f getEyePosition() const { return m_position + getEyeOffset(); }
142  v3f getEyeOffset() const;
143  void setEyeHeight(float eye_height) { m_eye_height = eye_height; }
144 
145  void setCollisionbox(const aabb3f &box) { m_collisionbox = box; }
146 
147  const aabb3f& getCollisionbox() const { return m_collisionbox; }
148 
149  float getZoomFOV() const { return m_zoom_fov; }
150  void setZoomFOV(float zoom_fov) { m_zoom_fov = zoom_fov; }
151 
152  bool getAutojump() const { return m_autojump; }
153 
154  bool isDead() const;
155 
156  inline void addVelocity(const v3f &vel)
157  {
158  added_velocity += vel;
159  }
160 
161 private:
162  void accelerate(const v3f &target_speed, const f32 max_increase_H,
163  const f32 max_increase_V, const bool use_pitch);
164  bool updateSneakNode(Map *map, const v3f &position, const v3f &sneak_max);
165  float getSlipFactor(Environment *env, const v3f &speedH);
166  void handleAutojump(f32 dtime, Environment *env,
167  const collisionMoveResult &result,
168  const v3f &position_before_move, const v3f &speed_before_move,
169  f32 pos_max_d);
170 
173 
174  v3s16 m_sneak_node = v3s16(32767, 32767, 32767);
175  // Stores the top bounding box of m_sneak_node
176  aabb3f m_sneak_node_bb_top = aabb3f(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
177  // Whether the player is allowed to sneak
178  bool m_sneak_node_exists = false;
179  // Whether a "sneak ladder" structure is detected at the players pos
180  // see detectSneakLadder() in the .cpp for more info (always false if disabled)
182 
183  // ***** Variables for temporary option of the old move code *****
184  // Stores the max player uplift by m_sneak_node
186  // Whether recalculation of m_sneak_node and its top bbox is needed
188  // Node below player, used to determine whether it has been removed,
189  // and its old type
190  v3s16 m_old_node_below = v3s16(32767, 32767, 32767);
191  std::string m_old_node_below_type = "air";
192  // ***** End of variables for temporary option *****
193 
194  bool m_can_jump = false;
195  bool m_disable_jump = false;
197  f32 m_yaw = 0.0f;
198  f32 m_pitch = 0.0f;
200  aabb3f m_collisionbox = aabb3f(-BS * 0.30f, 0.0f, -BS * 0.30f, BS * 0.30f,
201  BS * 1.75f, BS * 0.30f);
202  float m_eye_height = 1.625f;
203  float m_zoom_fov = 0.0f;
204  bool m_autojump = false;
205  float m_autojump_time = 0.0f;
206 
207  v3f added_velocity = v3f(0.0f); // cleared on each move()
208  // TODO: Rename to adhere to convention: added_velocity --> m_added_velocity
209 
210  GenericCAO *m_cao = nullptr;
212 };
Definition: clientobject.h:37
Definition: clientenvironment.h:64
Definition: client.h:109
Definition: environment.h:47
Definition: content_cao.h:70
Definition: gamedef.h:49
Definition: localplayer.h:45
int last_animation
Definition: localplayer.h:97
v3s16 getLightPosition() const
Definition: localplayer.cpp:702
video::SColor light_color
Definition: localplayer.h:103
bool m_can_jump
Definition: localplayer.h:194
bool in_liquid
Definition: localplayer.h:55
LocalPlayer(Client *client, const char *name)
Definition: localplayer.cpp:35
aabb3f m_sneak_node_bb_top
Definition: localplayer.h:176
bool camera_barely_in_ceiling
Definition: localplayer.h:199
v3f getEyeOffset() const
Definition: localplayer.cpp:707
bool m_sneak_ladder_detected
Definition: localplayer.h:181
bool getAutojump() const
Definition: localplayer.h:152
f32 m_pitch
Definition: localplayer.h:198
float last_yaw
Definition: localplayer.h:88
u16 m_breath
Definition: localplayer.h:196
void handleAutojump(f32 dtime, Environment *env, const collisionMoveResult &result, const v3f &position_before_move, const v3f &speed_before_move, f32 pos_max_d)
Definition: localplayer.cpp:1098
float physics_override_jump
Definition: localplayer.h:65
v3f added_velocity
Definition: localplayer.h:207
void old_move(f32 dtime, Environment *env, f32 pos_max_d, std::vector< CollisionInfo > *collision_info)
Definition: localplayer.cpp:767
bool touching_ground
Definition: localplayer.h:53
void move(f32 dtime, Environment *env, f32 pos_max_d)
Definition: localplayer.cpp:466
void setCAO(GenericCAO *toset)
Definition: localplayer.h:112
bool m_need_to_get_new_sneak_node
Definition: localplayer.h:187
bool swimming_vertical
Definition: localplayer.h:61
float m_eye_height
Definition: localplayer.h:202
float m_autojump_time
Definition: localplayer.h:205
void setBreath(u16 breath)
Definition: localplayer.h:121
aabb3f m_collisionbox
Definition: localplayer.h:200
void setZoomFOV(float zoom_fov)
Definition: localplayer.h:150
void accelerate(const v3f &target_speed, const f32 max_increase_H, const f32 max_increase_V, const bool use_pitch)
Definition: localplayer.cpp:725
bool in_liquid_stable
Definition: localplayer.h:57
v3f last_position
Definition: localplayer.h:85
v3f m_position
Definition: localplayer.h:171
GenericCAO * getCAO() const
Definition: localplayer.h:108
ClientActiveObject * getParent() const
Definition: localplayer.cpp:713
v3s16 getFootstepNodePos()
Definition: localplayer.cpp:685
float physics_override_speed
Definition: localplayer.h:64
void setYaw(f32 yaw)
Definition: localplayer.h:125
u8 last_camera_fov
Definition: localplayer.h:90
u8 liquid_viscosity
Definition: localplayer.h:59
bool m_autojump
Definition: localplayer.h:204
v3f getEyePosition() const
Definition: localplayer.h:141
bool swimming_pitch
Definition: localplayer.h:62
v3s16 m_sneak_node
Definition: localplayer.h:174
void setCollisionbox(const aabb3f &box)
Definition: localplayer.h:145
bool m_sneak_node_exists
Definition: localplayer.h:178
bool isDead() const
Definition: localplayer.cpp:718
virtual ~LocalPlayer()=default
bool m_disable_jump
Definition: localplayer.h:195
bool physics_override_new_move
Definition: localplayer.h:70
std::string hotbar_selected_image
Definition: localplayer.h:101
void setPosition(const v3f &position)
Definition: localplayer.h:131
float hurt_tilt_strength
Definition: localplayer.h:106
f32 getPitch() const
Definition: localplayer.h:129
unsigned int last_keyPressed
Definition: localplayer.h:89
void setEyeHeight(float eye_height)
Definition: localplayer.h:143
u16 hp
Definition: localplayer.h:52
v3s16 m_standing_node
Definition: localplayer.h:172
float getZoomFOV() const
Definition: localplayer.h:149
bool physics_override_sneak
Definition: localplayer.h:67
v3s16 m_old_node_below
Definition: localplayer.h:190
u16 getBreath() const
Definition: localplayer.h:120
f32 getYaw() const
Definition: localplayer.h:126
float hurt_tilt_timer
Definition: localplayer.h:105
u8 last_wanted_range
Definition: localplayer.h:91
float physics_override_gravity
Definition: localplayer.h:66
bool is_climbing
Definition: localplayer.h:60
const aabb3f & getCollisionbox() const
Definition: localplayer.h:147
void addVelocity(const v3f &vel)
Definition: localplayer.h:156
u32 maxHudId() const
Definition: localplayer.h:118
Client * m_client
Definition: localplayer.h:211
f32 m_sneak_node_bb_ymax
Definition: localplayer.h:185
GenericCAO * m_cao
Definition: localplayer.h:210
void applyControl(float dtime, Environment *env)
Definition: localplayer.cpp:471
float m_zoom_fov
Definition: localplayer.h:203
v3f getPosition() const
Definition: localplayer.h:137
bool updateSneakNode(Map *map, const v3f &position, const v3f &sneak_max)
Definition: localplayer.cpp:58
f32 m_yaw
Definition: localplayer.h:197
float last_pitch
Definition: localplayer.h:87
bool physics_override_sneak_glitch
Definition: localplayer.h:68
bool makes_footstep_sound
Definition: localplayer.h:95
float last_animation_speed
Definition: localplayer.h:98
v3s16 getStandingNodePos()
Definition: localplayer.cpp:677
float camera_impact
Definition: localplayer.h:93
std::string m_old_node_below_type
Definition: localplayer.h:191
v3f last_speed
Definition: localplayer.h:86
std::string hotbar_image
Definition: localplayer.h:100
void setPitch(f32 pitch)
Definition: localplayer.h:128
float getSlipFactor(Environment *env, const v3f &speedH)
Definition: localplayer.cpp:1079
Definition: map.h:123
Definition: player.h:123
std::vector< HudElement * > hud
Definition: player.h:219
#define BS
Definition: constants.h:74
#define PLAYER_MAX_BREATH_DEFAULT
Definition: constants.h:96
core::aabbox3d< f32 > aabb3f
Definition: irr_aabb3d.h:26
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
core::vector3df v3f
Definition: irr_v3d.h:26
LocalPlayerAnimations
Definition: localplayer.h:37
@ WD_ANIM
Definition: localplayer.h:41
@ WALK_ANIM
Definition: localplayer.h:39
@ DIG_ANIM
Definition: localplayer.h:40
@ NO_ANIM
Definition: localplayer.h:38
Definition: activeobjectmgr.cpp:25
Definition: collision.h:58