Move RemotePlayer code to its own cpp/header
[oweals/minetest.git] / src / player.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 #ifndef PLAYER_HEADER
21 #define PLAYER_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include "inventory.h"
25 #include "constants.h" // BS
26 #include "threading/mutex.h"
27 #include <list>
28
29 #define PLAYERNAME_SIZE 20
30
31 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
32 #define PLAYERNAME_ALLOWED_CHARS_USER_EXPL "'a' to 'z', 'A' to 'Z', '0' to '9', '-', '_'"
33
34 struct PlayerControl
35 {
36         PlayerControl()
37         {
38                 up = false;
39                 down = false;
40                 left = false;
41                 right = false;
42                 jump = false;
43                 aux1 = false;
44                 sneak = false;
45                 LMB = false;
46                 RMB = false;
47                 pitch = 0;
48                 yaw = 0;
49                 sidew_move_joystick_axis = .0f;
50                 forw_move_joystick_axis = .0f;
51         }
52
53         PlayerControl(
54                 bool a_up,
55                 bool a_down,
56                 bool a_left,
57                 bool a_right,
58                 bool a_jump,
59                 bool a_aux1,
60                 bool a_sneak,
61                 bool a_zoom,
62                 bool a_LMB,
63                 bool a_RMB,
64                 float a_pitch,
65                 float a_yaw,
66                 float a_sidew_move_joystick_axis,
67                 float a_forw_move_joystick_axis
68         )
69         {
70                 up = a_up;
71                 down = a_down;
72                 left = a_left;
73                 right = a_right;
74                 jump = a_jump;
75                 aux1 = a_aux1;
76                 sneak = a_sneak;
77                 zoom = a_zoom;
78                 LMB = a_LMB;
79                 RMB = a_RMB;
80                 pitch = a_pitch;
81                 yaw = a_yaw;
82                 sidew_move_joystick_axis = a_sidew_move_joystick_axis;
83                 forw_move_joystick_axis = a_forw_move_joystick_axis;
84         }
85         bool up;
86         bool down;
87         bool left;
88         bool right;
89         bool jump;
90         bool aux1;
91         bool sneak;
92         bool zoom;
93         bool LMB;
94         bool RMB;
95         float pitch;
96         float yaw;
97         float sidew_move_joystick_axis;
98         float forw_move_joystick_axis;
99 };
100
101 class Map;
102 struct CollisionInfo;
103 struct HudElement;
104 class Environment;
105
106 // IMPORTANT:
107 // Do *not* perform an assignment or copy operation on a Player or
108 // RemotePlayer object!  This will copy the lock held for HUD synchronization
109 class Player
110 {
111 public:
112
113         Player(const char *name, IItemDefManager *idef);
114         virtual ~Player() = 0;
115
116         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
117         {}
118         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
119                         std::vector<CollisionInfo> *collision_info)
120         {}
121
122         v3f getSpeed()
123         {
124                 return m_speed;
125         }
126
127         void setSpeed(v3f speed)
128         {
129                 m_speed = speed;
130         }
131
132         v3f getPosition()
133         {
134                 return m_position;
135         }
136
137         v3s16 getLightPosition() const;
138
139         v3f getEyeOffset()
140         {
141                 float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f;
142                 return v3f(0, BS * eye_height, 0);
143         }
144
145         v3f getEyePosition()
146         {
147                 return m_position + getEyeOffset();
148         }
149
150         virtual void setPosition(const v3f &position)
151         {
152                 m_position = position;
153         }
154
155         virtual void setPitch(f32 pitch)
156         {
157                 m_pitch = pitch;
158         }
159
160         virtual void setYaw(f32 yaw)
161         {
162                 m_yaw = yaw;
163         }
164
165         f32 getPitch() const { return m_pitch; }
166         f32 getYaw() const { return m_yaw; }
167         u16 getBreath() const { return m_breath; }
168
169         virtual void setBreath(u16 breath) { m_breath = breath; }
170
171         f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
172         f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
173         const char *getName() const { return m_name; }
174         aabb3f getCollisionbox() const { return m_collisionbox; }
175
176         u32 getFreeHudID()
177         {
178                 size_t size = hud.size();
179                 for (size_t i = 0; i != size; i++) {
180                         if (!hud[i])
181                                 return i;
182                 }
183                 return size;
184         }
185
186         void setLocalAnimations(v2s32 frames[4], float frame_speed)
187         {
188                 for (int i = 0; i < 4; i++)
189                         local_animations[i] = frames[i];
190                 local_animation_speed = frame_speed;
191         }
192
193         void getLocalAnimations(v2s32 *frames, float *frame_speed)
194         {
195                 for (int i = 0; i < 4; i++)
196                         frames[i] = local_animations[i];
197                 *frame_speed = local_animation_speed;
198         }
199
200         virtual bool isLocal() const { return false; }
201
202         bool camera_barely_in_ceiling;
203         v3f eye_offset_first;
204         v3f eye_offset_third;
205
206         Inventory inventory;
207
208         f32 movement_acceleration_default;
209         f32 movement_acceleration_air;
210         f32 movement_acceleration_fast;
211         f32 movement_speed_walk;
212         f32 movement_speed_crouch;
213         f32 movement_speed_fast;
214         f32 movement_speed_climb;
215         f32 movement_speed_jump;
216         f32 movement_liquid_fluidity;
217         f32 movement_liquid_fluidity_smooth;
218         f32 movement_liquid_sink;
219         f32 movement_gravity;
220
221         v2s32 local_animations[4];
222         float local_animation_speed;
223
224         u16 hp;
225
226         u16 peer_id;
227
228         std::string inventory_formspec;
229
230         PlayerControl control;
231         const PlayerControl& getPlayerControl() { return control; }
232
233         u32 keyPressed;
234
235         HudElement* getHud(u32 id);
236         u32         addHud(HudElement* hud);
237         HudElement* removeHud(u32 id);
238         void        clearHud();
239
240         u32 hud_flags;
241         s32 hud_hotbar_itemcount;
242 protected:
243         char m_name[PLAYERNAME_SIZE];
244         u16 m_breath;
245         f32 m_pitch;
246         f32 m_yaw;
247         v3f m_speed;
248         v3f m_position;
249         aabb3f m_collisionbox;
250
251         std::vector<HudElement *> hud;
252 private:
253         // Protect some critical areas
254         // hud for example can be modified by EmergeThread
255         // and ServerThread
256         Mutex m_mutex;
257 };
258
259 #endif
260