Implement adding velocity to player from Lua
[oweals/minetest.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 <list>
27
28 class Client;
29 class Environment;
30 class GenericCAO;
31 class ClientActiveObject;
32 class ClientEnvironment;
33 class IGameDef;
34 struct collisionMoveResult;
35
36 enum LocalPlayerAnimations
37 {
38         NO_ANIM,
39         WALK_ANIM,
40         DIG_ANIM,
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         ClientActiveObject *parent = nullptr;
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 isAttached = false;
56         bool touching_ground = false;
57         // This oscillates so that the player jumps a bit above the surface
58         bool in_liquid = false;
59         // This is more stable and defines the maximum speed of the player
60         bool in_liquid_stable = false;
61         // Gets the viscosity of water to calculate friction
62         u8 liquid_viscosity = 0;
63         bool is_climbing = false;
64         bool swimming_vertical = false;
65         bool swimming_pitch = false;
66
67         float physics_override_speed = 1.0f;
68         float physics_override_jump = 1.0f;
69         float physics_override_gravity = 1.0f;
70         bool physics_override_sneak = true;
71         bool physics_override_sneak_glitch = false;
72         // Temporary option for old move code
73         bool physics_override_new_move = true;
74
75         v3f overridePosition;
76
77         void move(f32 dtime, Environment *env, f32 pos_max_d);
78         void move(f32 dtime, Environment *env, f32 pos_max_d,
79                         std::vector<CollisionInfo> *collision_info);
80         // Temporary option for old move code
81         void old_move(f32 dtime, Environment *env, f32 pos_max_d,
82                         std::vector<CollisionInfo> *collision_info);
83
84         void applyControl(float dtime, Environment *env);
85
86         v3s16 getStandingNodePos();
87         v3s16 getFootstepNodePos();
88
89         // Used to check if anything changed and prevent sending packets if not
90         v3f last_position;
91         v3f last_speed;
92         float last_pitch = 0.0f;
93         float last_yaw = 0.0f;
94         unsigned int last_keyPressed = 0;
95         u8 last_camera_fov = 0;
96         u8 last_wanted_range = 0;
97
98         float camera_impact = 0.0f;
99
100         bool makes_footstep_sound = true;
101
102         int last_animation = NO_ANIM;
103         float last_animation_speed;
104
105         std::string hotbar_image = "";
106         std::string hotbar_selected_image = "";
107
108         video::SColor light_color = video::SColor(255, 255, 255, 255);
109
110         float hurt_tilt_timer = 0.0f;
111         float hurt_tilt_strength = 0.0f;
112
113         GenericCAO *getCAO() const { return m_cao; }
114
115         void setCAO(GenericCAO *toset)
116         {
117                 assert(!m_cao); // Pre-condition
118                 m_cao = toset;
119         }
120
121         u32 maxHudId() const { return hud.size(); }
122
123         u16 getBreath() const { return m_breath; }
124         void setBreath(u16 breath) { m_breath = breath; }
125
126         v3s16 getLightPosition() const;
127
128         void setYaw(f32 yaw) { m_yaw = yaw; }
129         f32 getYaw() const { return m_yaw; }
130
131         void setPitch(f32 pitch) { m_pitch = pitch; }
132         f32 getPitch() const { return m_pitch; }
133
134         inline void setPosition(const v3f &position)
135         {
136                 m_position = position;
137                 m_sneak_node_exists = false;
138         }
139
140         v3f getPosition() const { return m_position; }
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         float getZoomFOV() const { return m_zoom_fov; }
148         void setZoomFOV(float zoom_fov) { m_zoom_fov = zoom_fov; }
149
150         bool getAutojump() const { return m_autojump; }
151
152         inline void addVelocity(const v3f &vel)
153         {
154                 added_velocity += vel;
155         }
156
157 private:
158         void accelerate(const v3f &target_speed, const f32 max_increase_H,
159                         const f32 max_increase_V, const bool use_pitch);
160         bool updateSneakNode(Map *map, const v3f &position, const v3f &sneak_max);
161         float getSlipFactor(Environment *env, const v3f &speedH);
162         void handleAutojump(f32 dtime, Environment *env,
163                         const collisionMoveResult &result,
164                         const v3f &position_before_move, const v3f &speed_before_move,
165                         f32 pos_max_d);
166
167         v3f m_position;
168         v3s16 m_standing_node;
169
170         v3s16 m_sneak_node = v3s16(32767, 32767, 32767);
171         // Stores the top bounding box of m_sneak_node
172         aabb3f m_sneak_node_bb_top = aabb3f(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
173         // Whether the player is allowed to sneak
174         bool m_sneak_node_exists = false;
175         // Whether a "sneak ladder" structure is detected at the players pos
176         // see detectSneakLadder() in the .cpp for more info (always false if disabled)
177         bool m_sneak_ladder_detected = false;
178
179         // ***** Variables for temporary option of the old move code *****
180         // Stores the max player uplift by m_sneak_node
181         f32 m_sneak_node_bb_ymax = 0.0f;
182         // Whether recalculation of m_sneak_node and its top bbox is needed
183         bool m_need_to_get_new_sneak_node = true;
184         // Node below player, used to determine whether it has been removed,
185         // and its old type
186         v3s16 m_old_node_below = v3s16(32767, 32767, 32767);
187         std::string m_old_node_below_type = "air";
188         // ***** End of variables for temporary option *****
189
190         bool m_can_jump = false;
191         bool m_disable_jump = false;
192         u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
193         f32 m_yaw = 0.0f;
194         f32 m_pitch = 0.0f;
195         bool camera_barely_in_ceiling = false;
196         aabb3f m_collisionbox = aabb3f(-BS * 0.30f, 0.0f, -BS * 0.30f, BS * 0.30f,
197                         BS * 1.75f, BS * 0.30f);
198         float m_eye_height = 1.625f;
199         float m_zoom_fov = 0.0f;
200         bool m_autojump = false;
201         float m_autojump_time = 0.0f;
202         v3f added_velocity = v3f(0.0f, 0.0f, 0.0f); // cleared on each move()
203
204         GenericCAO *m_cao = nullptr;
205         Client *m_client;
206 };