Line data Source code
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 "jthread/jmutex.h"
27 : #include <list>
28 :
29 : #define PLAYERNAME_SIZE 20
30 :
31 : #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
32 :
33 : struct PlayerControl
34 : {
35 1 : PlayerControl()
36 : {
37 1 : up = false;
38 1 : down = false;
39 1 : left = false;
40 1 : right = false;
41 1 : jump = false;
42 1 : aux1 = false;
43 1 : sneak = false;
44 1 : LMB = false;
45 1 : RMB = false;
46 1 : pitch = 0;
47 1 : yaw = 0;
48 1 : }
49 1166 : PlayerControl(
50 : bool a_up,
51 : bool a_down,
52 : bool a_left,
53 : bool a_right,
54 : bool a_jump,
55 : bool a_aux1,
56 : bool a_sneak,
57 : bool a_LMB,
58 : bool a_RMB,
59 : float a_pitch,
60 : float a_yaw
61 : )
62 : {
63 1166 : up = a_up;
64 1166 : down = a_down;
65 1166 : left = a_left;
66 1166 : right = a_right;
67 1166 : jump = a_jump;
68 1166 : aux1 = a_aux1;
69 1166 : sneak = a_sneak;
70 1166 : LMB = a_LMB;
71 1166 : RMB = a_RMB;
72 1166 : pitch = a_pitch;
73 1166 : yaw = a_yaw;
74 1166 : }
75 : bool up;
76 : bool down;
77 : bool left;
78 : bool right;
79 : bool jump;
80 : bool aux1;
81 : bool sneak;
82 : bool LMB;
83 : bool RMB;
84 : float pitch;
85 : float yaw;
86 : };
87 :
88 : class Map;
89 : class IGameDef;
90 : struct CollisionInfo;
91 : class PlayerSAO;
92 : struct HudElement;
93 : class Environment;
94 :
95 : // IMPORTANT:
96 : // Do *not* perform an assignment or copy operation on a Player or
97 : // RemotePlayer object! This will copy the lock held for HUD synchronization
98 : class Player
99 : {
100 : public:
101 :
102 : Player(IGameDef *gamedef, const char *name);
103 : virtual ~Player() = 0;
104 :
105 0 : virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
106 0 : {}
107 0 : virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
108 : std::vector<CollisionInfo> *collision_info)
109 0 : {}
110 :
111 6608 : v3f getSpeed()
112 : {
113 6608 : return m_speed;
114 : }
115 :
116 3735 : void setSpeed(v3f speed)
117 : {
118 3735 : m_speed = speed;
119 3735 : }
120 :
121 : void accelerateHorizontal(v3f target_speed, f32 max_increase);
122 : void accelerateVertical(v3f target_speed, f32 max_increase);
123 :
124 130561 : v3f getPosition()
125 : {
126 130561 : return m_position;
127 : }
128 :
129 : v3s16 getLightPosition() const;
130 :
131 1430992 : v3f getEyeOffset()
132 : {
133 : // This is at the height of the eyes of the current figure
134 : // return v3f(0, BS*1.5, 0);
135 : // This is more like in minecraft
136 1430992 : if(camera_barely_in_ceiling)
137 0 : return v3f(0,BS*1.5,0);
138 : else
139 1430992 : return v3f(0,BS*1.625,0);
140 : }
141 :
142 1429826 : v3f getEyePosition()
143 : {
144 1429826 : return m_position + getEyeOffset();
145 : }
146 :
147 3750 : virtual void setPosition(const v3f &position)
148 : {
149 3750 : if (position != m_position)
150 1832 : m_dirty = true;
151 3750 : m_position = position;
152 3750 : }
153 :
154 1186 : void setPitch(f32 pitch)
155 : {
156 1186 : if (pitch != m_pitch)
157 417 : m_dirty = true;
158 1186 : m_pitch = pitch;
159 1186 : }
160 :
161 1186 : virtual void setYaw(f32 yaw)
162 : {
163 1186 : if (yaw != m_yaw)
164 478 : m_dirty = true;
165 1186 : m_yaw = yaw;
166 1186 : }
167 :
168 2852 : f32 getPitch()
169 : {
170 2852 : return m_pitch;
171 : }
172 :
173 2857314 : f32 getYaw()
174 : {
175 2857314 : return m_yaw;
176 : }
177 :
178 80 : u16 getBreath()
179 : {
180 80 : return m_breath;
181 : }
182 :
183 2 : virtual void setBreath(u16 breath)
184 : {
185 2 : if (breath != m_breath)
186 0 : m_dirty = true;
187 2 : m_breath = breath;
188 2 : }
189 :
190 0 : f32 getRadPitch()
191 : {
192 0 : return -1.0 * m_pitch * core::DEGTORAD;
193 : }
194 :
195 0 : f32 getRadYaw()
196 : {
197 0 : return (m_yaw + 90.) * core::DEGTORAD;
198 : }
199 :
200 5 : const char * getName() const
201 : {
202 5 : return m_name;
203 : }
204 :
205 0 : core::aabbox3d<f32> getCollisionbox() {
206 0 : return m_collisionbox;
207 : }
208 :
209 2 : u32 getFreeHudID() {
210 2 : size_t size = hud.size();
211 3 : for (size_t i = 0; i != size; i++) {
212 1 : if (!hud[i])
213 0 : return i;
214 : }
215 2 : return size;
216 : }
217 :
218 0 : void setHotbarItemcount(s32 hotbar_itemcount) {
219 0 : hud_hotbar_itemcount = hotbar_itemcount;
220 0 : }
221 0 : s32 getHotbarItemcount() {
222 0 : return hud_hotbar_itemcount;
223 : }
224 0 : void setHotbarImage(std::string name) {
225 0 : hud_hotbar_image = name;
226 0 : }
227 0 : std::string getHotbarImage() {
228 0 : return hud_hotbar_image;
229 : }
230 0 : void setHotbarSelectedImage(std::string name) {
231 0 : hud_hotbar_selected_image = name;
232 0 : }
233 0 : std::string getHotbarSelectedImage() {
234 0 : return hud_hotbar_selected_image;
235 : }
236 :
237 0 : void setSky(const video::SColor &bgcolor, const std::string &type,
238 : const std::vector<std::string> ¶ms) {
239 0 : m_sky_bgcolor = bgcolor;
240 0 : m_sky_type = type;
241 0 : m_sky_params = params;
242 0 : }
243 0 : void getSky(video::SColor *bgcolor, std::string *type,
244 : std::vector<std::string> *params) {
245 0 : *bgcolor = m_sky_bgcolor;
246 0 : *type = m_sky_type;
247 0 : *params = m_sky_params;
248 0 : }
249 0 : void overrideDayNightRatio(bool do_override, float ratio) {
250 0 : m_day_night_ratio_do_override = do_override;
251 0 : m_day_night_ratio = ratio;
252 0 : }
253 0 : void getDayNightRatio(bool *do_override, float *ratio) {
254 0 : *do_override = m_day_night_ratio_do_override;
255 0 : *ratio = m_day_night_ratio;
256 0 : }
257 0 : void setLocalAnimations(v2s32 frames[4], float frame_speed) {
258 0 : for (int i = 0; i < 4; i++)
259 0 : local_animations[i] = frames[i];
260 0 : local_animation_speed = frame_speed;
261 0 : }
262 0 : void getLocalAnimations(v2s32 *frames, float *frame_speed) {
263 0 : for (int i = 0; i < 4; i++)
264 0 : frames[i] = local_animations[i];
265 0 : *frame_speed = local_animation_speed;
266 0 : }
267 :
268 0 : virtual bool isLocal() const
269 0 : { return false; }
270 0 : virtual PlayerSAO *getPlayerSAO()
271 0 : { return NULL; }
272 0 : virtual void setPlayerSAO(PlayerSAO *sao)
273 0 : { FATAL_ERROR("FIXME"); }
274 :
275 : /*
276 : serialize() writes a bunch of text that can contain
277 : any characters except a '\0', and such an ending that
278 : deSerialize stops reading exactly at the right point.
279 : */
280 : void serialize(std::ostream &os);
281 : void deSerialize(std::istream &is, std::string playername);
282 :
283 0 : bool checkModified() const
284 : {
285 0 : return m_dirty || inventory.checkModified();
286 : }
287 :
288 0 : void setModified(const bool x)
289 : {
290 0 : m_dirty = x;
291 0 : if (x == false)
292 0 : inventory.setModified(x);
293 0 : }
294 :
295 : // Use a function, if isDead can be defined by other conditions
296 0 : bool isDead() { return hp == 0; }
297 :
298 : bool touching_ground;
299 : // This oscillates so that the player jumps a bit above the surface
300 : bool in_liquid;
301 : // This is more stable and defines the maximum speed of the player
302 : bool in_liquid_stable;
303 : // Gets the viscosity of water to calculate friction
304 : u8 liquid_viscosity;
305 : bool is_climbing;
306 : bool swimming_vertical;
307 : bool camera_barely_in_ceiling;
308 : v3f eye_offset_first;
309 : v3f eye_offset_third;
310 :
311 : Inventory inventory;
312 :
313 : f32 movement_acceleration_default;
314 : f32 movement_acceleration_air;
315 : f32 movement_acceleration_fast;
316 : f32 movement_speed_walk;
317 : f32 movement_speed_crouch;
318 : f32 movement_speed_fast;
319 : f32 movement_speed_climb;
320 : f32 movement_speed_jump;
321 : f32 movement_liquid_fluidity;
322 : f32 movement_liquid_fluidity_smooth;
323 : f32 movement_liquid_sink;
324 : f32 movement_gravity;
325 :
326 : float physics_override_speed;
327 : float physics_override_jump;
328 : float physics_override_gravity;
329 : bool physics_override_sneak;
330 : bool physics_override_sneak_glitch;
331 :
332 : v2s32 local_animations[4];
333 : float local_animation_speed;
334 :
335 : u16 hp;
336 :
337 : float hurt_tilt_timer;
338 : float hurt_tilt_strength;
339 :
340 : u16 peer_id;
341 :
342 : std::string inventory_formspec;
343 :
344 : PlayerControl control;
345 0 : PlayerControl getPlayerControl()
346 : {
347 0 : return control;
348 : }
349 :
350 : u32 keyPressed;
351 :
352 :
353 : HudElement* getHud(u32 id);
354 : u32 addHud(HudElement* hud);
355 : HudElement* removeHud(u32 id);
356 : void clearHud();
357 3498 : u32 maxHudId() {
358 3498 : return hud.size();
359 : }
360 :
361 : u32 hud_flags;
362 : s32 hud_hotbar_itemcount;
363 : std::string hud_hotbar_image;
364 : std::string hud_hotbar_selected_image;
365 : protected:
366 : IGameDef *m_gamedef;
367 :
368 : char m_name[PLAYERNAME_SIZE];
369 : u16 m_breath;
370 : f32 m_pitch;
371 : f32 m_yaw;
372 : v3f m_speed;
373 : v3f m_position;
374 : core::aabbox3d<f32> m_collisionbox;
375 :
376 : bool m_dirty;
377 :
378 : std::vector<HudElement *> hud;
379 :
380 : std::string m_sky_type;
381 : video::SColor m_sky_bgcolor;
382 : std::vector<std::string> m_sky_params;
383 :
384 : bool m_day_night_ratio_do_override;
385 : float m_day_night_ratio;
386 : private:
387 : // Protect some critical areas
388 : // hud for example can be modified by EmergeThread
389 : // and ServerThread
390 : JMutex m_mutex;
391 : };
392 :
393 :
394 : /*
395 : Player on the server
396 : */
397 : class RemotePlayer : public Player
398 : {
399 : public:
400 0 : RemotePlayer(IGameDef *gamedef, const char *name):
401 : Player(gamedef, name),
402 0 : m_sao(NULL)
403 0 : {}
404 0 : virtual ~RemotePlayer() {}
405 :
406 : void save(std::string savedir);
407 :
408 0 : PlayerSAO *getPlayerSAO()
409 0 : { return m_sao; }
410 0 : void setPlayerSAO(PlayerSAO *sao)
411 0 : { m_sao = sao; }
412 : void setPosition(const v3f &position);
413 :
414 : private:
415 : PlayerSAO *m_sao;
416 : };
417 :
418 : #endif
419 :
|