]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.h
Simple fix for camera blinking black when jumping into ceiling with current smaller...
[dragonfireclient.git] / src / player.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
24 #include "inventory.h"
25
26 #define PLAYERNAME_SIZE 20
27
28 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
29
30
31 class Map;
32 class IGameDef;
33 struct CollisionInfo;
34 class PlayerSAO;
35
36 class Player
37 {
38 public:
39
40         Player(IGameDef *gamedef);
41         virtual ~Player() = 0;
42
43         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
44         {}
45
46         v3f getSpeed()
47         {
48                 return m_speed;
49         }
50
51         void setSpeed(v3f speed)
52         {
53                 m_speed = speed;
54         }
55         
56         // Y direction is ignored
57         void accelerate(v3f target_speed, f32 max_increase);
58
59         v3f getPosition()
60         {
61                 return m_position;
62         }
63
64         v3s16 getLightPosition() const;
65
66         v3f getEyeOffset()
67         {
68                 // This is at the height of the eyes of the current figure
69                 // return v3f(0, BS*1.5, 0);
70                 // This is more like in minecraft
71                 if(camera_barely_in_ceiling)
72                         return v3f(0,BS*1.5,0);
73                 else
74                         return v3f(0,BS*1.625,0);
75         }
76
77         v3f getEyePosition()
78         {
79                 return m_position + getEyeOffset();
80         }
81
82         virtual void setPosition(const v3f &position)
83         {
84                 m_position = position;
85         }
86
87         void setPitch(f32 pitch)
88         {
89                 m_pitch = pitch;
90         }
91
92         virtual void setYaw(f32 yaw)
93         {
94                 m_yaw = yaw;
95         }
96
97         f32 getPitch()
98         {
99                 return m_pitch;
100         }
101
102         f32 getYaw()
103         {
104                 return m_yaw;
105         }
106
107         f32 getRadPitch()
108         {
109                 return -1.0 * m_pitch * core::DEGTORAD;
110         }
111
112         f32 getRadYaw()
113         {
114                 return (m_yaw + 90.) * core::DEGTORAD;
115         }
116
117         void updateName(const char *name)
118         {
119                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
120         }
121
122         const char * getName() const
123         {
124                 return m_name;
125         }
126
127         virtual bool isLocal() const
128         { return false; }
129         virtual PlayerSAO *getPlayerSAO()
130         { return NULL; }
131         virtual void setPlayerSAO(PlayerSAO *sao)
132         { assert(0); }
133
134         /*
135                 serialize() writes a bunch of text that can contain
136                 any characters except a '\0', and such an ending that
137                 deSerialize stops reading exactly at the right point.
138         */
139         void serialize(std::ostream &os);
140         void deSerialize(std::istream &is);
141
142         bool touching_ground;
143         // This oscillates so that the player jumps a bit above the surface
144         bool in_water;
145         // This is more stable and defines the maximum speed of the player
146         bool in_water_stable;
147         bool is_climbing;
148         bool swimming_up;
149         bool camera_barely_in_ceiling;
150         
151         u8 light;
152
153         // In creative mode, this is the invisible backup inventory
154         Inventory inventory;
155
156         u16 hp;
157
158         u16 peer_id;
159
160 protected:
161         IGameDef *m_gamedef;
162
163         char m_name[PLAYERNAME_SIZE];
164         f32 m_pitch;
165         f32 m_yaw;
166         v3f m_speed;
167         v3f m_position;
168 };
169
170 #ifndef SERVER
171 struct PlayerControl
172 {
173         PlayerControl()
174         {
175                 up = false;
176                 down = false;
177                 left = false;
178                 right = false;
179                 jump = false;
180                 aux1 = false;
181                 sneak = false;
182                 pitch = 0;
183                 yaw = 0;
184         }
185         PlayerControl(
186                 bool a_up,
187                 bool a_down,
188                 bool a_left,
189                 bool a_right,
190                 bool a_jump,
191                 bool a_aux1,
192                 bool a_sneak,
193                 float a_pitch,
194                 float a_yaw
195         )
196         {
197                 up = a_up;
198                 down = a_down;
199                 left = a_left;
200                 right = a_right;
201                 jump = a_jump;
202                 aux1 = a_aux1;
203                 sneak = a_sneak;
204                 pitch = a_pitch;
205                 yaw = a_yaw;
206         }
207         bool up;
208         bool down;
209         bool left;
210         bool right;
211         bool jump;
212         bool aux1;
213         bool sneak;
214         float pitch;
215         float yaw;
216 };
217
218 class LocalPlayer : public Player
219 {
220 public:
221         LocalPlayer(IGameDef *gamedef);
222         virtual ~LocalPlayer();
223
224         bool isLocal() const
225         {
226                 return true;
227         }
228         
229         void move(f32 dtime, Map &map, f32 pos_max_d,
230                         core::list<CollisionInfo> *collision_info);
231         void move(f32 dtime, Map &map, f32 pos_max_d);
232
233         void applyControl(float dtime);
234
235         v3s16 getStandingNodePos();
236         
237         PlayerControl control;
238
239 private:
240         // This is used for determining the sneaking range
241         v3s16 m_sneak_node;
242         // Whether the player is allowed to sneak
243         bool m_sneak_node_exists;
244 };
245 #endif // !SERVER
246
247 /*
248         Player on the server
249 */
250 class RemotePlayer : public Player
251 {
252 public:
253         RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
254         virtual ~RemotePlayer() {}
255
256         PlayerSAO *getPlayerSAO()
257         { return m_sao; }
258         void setPlayerSAO(PlayerSAO *sao)
259         { m_sao = sao; }
260         void setPosition(const v3f &position);
261
262 private:
263         PlayerSAO *m_sao;
264 };
265
266 #endif
267