]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/localplayer.h
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / localplayer.h
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 "lighting.h"
27 #include <list>
28
29 class Client;
30 class Environment;
31 class GenericCAO;
32 class ClientActiveObject;
33 class ClientEnvironment;
34 class IGameDef;
35 struct ContentFeatures;
36 struct collisionMoveResult;
37
38 enum LocalPlayerAnimations
39 {
40         NO_ANIM,
41         WALK_ANIM,
42         DIG_ANIM,
43         WD_ANIM
44 }; // no local animation, walking, digging, both
45
46 class LocalPlayer : public Player
47 {
48 public:
49         LocalPlayer(Client *client, const char *name);
50         virtual ~LocalPlayer() = default;
51
52         // Initialize hp to 0, so that no hearts will be shown if server
53         // doesn't support health points
54         u16 hp = 0;
55         bool touching_ground = false;
56         // This oscillates so that the player jumps a bit above the surface
57         bool in_liquid = false;
58         // This is more stable and defines the maximum speed of the player
59         bool in_liquid_stable = false;
60         // Slows down the player when moving through
61         u8 move_resistance = 0;
62         bool is_climbing = false;
63         bool swimming_vertical = false;
64         bool swimming_pitch = false;
65
66         float physics_override_speed = 1.0f;
67         float physics_override_jump = 1.0f;
68         float physics_override_gravity = 1.0f;
69         bool physics_override_sneak = true;
70         bool physics_override_sneak_glitch = false;
71         // Temporary option for old move code
72         bool physics_override_new_move = true;
73
74         void move(f32 dtime, Environment *env, f32 pos_max_d);
75         void move(f32 dtime, Environment *env, f32 pos_max_d,
76                         std::vector<CollisionInfo> *collision_info);
77         // Temporary option for old move code
78         void old_move(f32 dtime, Environment *env, f32 pos_max_d,
79                         std::vector<CollisionInfo> *collision_info);
80
81         void applyControl(float dtime, Environment *env);
82
83         v3s16 getStandingNodePos();
84         v3s16 getFootstepNodePos();
85
86         // Used to check if anything changed and prevent sending packets if not
87         v3f last_position;
88         v3f last_speed;
89         float last_pitch = 0.0f;
90         float last_yaw = 0.0f;
91         u32 last_keyPressed = 0;
92         u8 last_camera_fov = 0;
93         u8 last_wanted_range = 0;
94
95         float camera_impact = 0.0f;
96
97         bool makes_footstep_sound = true;
98
99         int last_animation = NO_ANIM;
100         float last_animation_speed = 0.0f;
101
102         std::string hotbar_image = "";
103         std::string hotbar_selected_image = "";
104
105         video::SColor light_color = video::SColor(255, 255, 255, 255);
106
107         float hurt_tilt_timer = 0.0f;
108         float hurt_tilt_strength = 0.0f;
109
110         GenericCAO *getCAO() const { return m_cao; }
111
112         ClientActiveObject *getParent() const;
113
114         void setCAO(GenericCAO *toset)
115         {
116                 assert(!m_cao); // Pre-condition
117                 m_cao = toset;
118         }
119
120         u32 maxHudId() const { return hud.size(); }
121
122         u16 getBreath() const { return m_breath; }
123         void setBreath(u16 breath) { m_breath = breath; }
124
125         v3s16 getLightPosition() const;
126
127         void setYaw(f32 yaw) { m_yaw = yaw; }
128         f32 getYaw() const { return m_yaw; }
129
130         void setPitch(f32 pitch) { m_pitch = pitch; }
131         f32 getPitch() const { return m_pitch; }
132
133         inline void setPosition(const v3f &position)
134         {
135                 m_position = position;
136                 if (! m_freecam)
137                         m_legit_position = position;
138                 m_sneak_node_exists = false;
139         }
140
141         v3f getPosition() const { return m_position; }
142
143         v3f getLegitPosition() const { return m_legit_position; }
144
145         v3f getLegitSpeed() const { return m_freecam ? m_legit_speed : m_speed; }
146
147         v3f getSendSpeed();
148
149         inline void setLegitPosition(const v3f &position)
150         {
151                 if (m_freecam)
152                         m_legit_position = position;
153                 else
154                         setPosition(position);
155         }
156
157         inline void freecamEnable()
158         {
159                 m_freecam = true;
160         }
161
162         inline void freecamDisable()
163         {
164                 m_freecam = false;
165                 setPosition(m_legit_position);
166                 setSpeed(m_legit_speed);
167         }
168
169         // Non-transformed eye offset getters
170         // For accurate positions, use the Camera functions
171         v3f getEyePosition() const { return m_position + getEyeOffset(); }
172         v3f getEyeOffset() const;
173         void setEyeHeight(float eye_height) { m_eye_height = eye_height; }
174
175         void setCollisionbox(const aabb3f &box) { m_collisionbox = box; }
176
177         const aabb3f& getCollisionbox() const { return m_collisionbox; }
178
179         float getZoomFOV() const { return m_zoom_fov; }
180         void setZoomFOV(float zoom_fov) { m_zoom_fov = zoom_fov; }
181
182         bool getAutojump() const { return m_autojump; }
183
184         bool isDead() const;
185
186         inline void addVelocity(const v3f &vel)
187         {
188                 added_velocity += vel;
189         }
190
191         inline Lighting& getLighting() { return m_lighting; }
192
193         void tryReattach(int id);
194
195         bool isWaitingForReattach() const;
196
197         bool canWalkOn(const ContentFeatures &f);
198
199 private:
200         void accelerate(const v3f &target_speed, const f32 max_increase_H,
201                 const f32 max_increase_V, const bool use_pitch);
202         bool updateSneakNode(Map *map, const v3f &position, const v3f &sneak_max);
203         float getSlipFactor(Environment *env, const v3f &speedH);
204         void handleAutojump(f32 dtime, Environment *env,
205                 const collisionMoveResult &result,
206                 const v3f &position_before_move, const v3f &speed_before_move,
207                 f32 pos_max_d);
208
209         bool m_freecam = false;
210         v3f m_position;
211         v3f m_legit_position;
212         v3f m_legit_speed;
213         v3s16 m_standing_node;
214
215         v3s16 m_sneak_node = v3s16(32767, 32767, 32767);
216         // Stores the top bounding box of m_sneak_node
217         aabb3f m_sneak_node_bb_top = aabb3f(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
218         // Whether the player is allowed to sneak
219         bool m_sneak_node_exists = false;
220         // Whether a "sneak ladder" structure is detected at the players pos
221         // see detectSneakLadder() in the .cpp for more info (always false if disabled)
222         bool m_sneak_ladder_detected = false;
223
224         // ***** Variables for temporary option of the old move code *****
225         // Stores the max player uplift by m_sneak_node
226         f32 m_sneak_node_bb_ymax = 0.0f;
227         // Whether recalculation of m_sneak_node and its top bbox is needed
228         bool m_need_to_get_new_sneak_node = true;
229         // Node below player, used to determine whether it has been removed,
230         // and its old type
231         v3s16 m_old_node_below = v3s16(32767, 32767, 32767);
232         std::string m_old_node_below_type = "air";
233         // ***** End of variables for temporary option *****
234
235         bool m_can_jump = false;
236         bool m_disable_jump = false;
237         u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
238         f32 m_yaw = 0.0f;
239         f32 m_pitch = 0.0f;
240         bool camera_barely_in_ceiling = false;
241         aabb3f m_collisionbox = aabb3f(-BS * 0.30f, 0.0f, -BS * 0.30f, BS * 0.30f,
242                 BS * 1.75f, BS * 0.30f);
243         float m_eye_height = 1.625f;
244         float m_zoom_fov = 0.0f;
245         bool m_autojump = false;
246         float m_autojump_time = 0.0f;
247
248         v3f added_velocity = v3f(0.0f); // cleared on each move()
249         // TODO: Rename to adhere to convention: added_velocity --> m_added_velocity
250
251         GenericCAO *m_cao = nullptr;
252         Client *m_client;
253         Lighting m_lighting;
254 };