]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.cpp
3c06283a03a4afaab0e37d19d4f83ce9b06675cb
[dragonfireclient.git] / src / player.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 /*
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
22 */
23
24 #include "player.h"
25 #include "map.h"
26 #include "connection.h"
27 #include "constants.h"
28
29 Player::Player():
30         touching_ground(false),
31         in_water(false),
32         peer_id(PEER_ID_INEXISTENT),
33         m_speed(0,0,0),
34         m_position(0,0,0)
35 {
36         updateName("<not set>");
37         inventory.addList("main", PLAYER_INVENTORY_SIZE);
38         inventory.addList("craft", 9);
39         inventory.addList("craftresult", 1);
40 }
41
42 Player::~Player()
43 {
44 }
45
46 // Y direction is ignored
47 void Player::accelerate(v3f target_speed, f32 max_increase)
48 {
49         if(m_speed.X < target_speed.X - max_increase)
50                 m_speed.X += max_increase;
51         else if(m_speed.X > target_speed.X + max_increase)
52                 m_speed.X -= max_increase;
53         else if(m_speed.X < target_speed.X)
54                 m_speed.X = target_speed.X;
55         else if(m_speed.X > target_speed.X)
56                 m_speed.X = target_speed.X;
57
58         if(m_speed.Z < target_speed.Z - max_increase)
59                 m_speed.Z += max_increase;
60         else if(m_speed.Z > target_speed.Z + max_increase)
61                 m_speed.Z -= max_increase;
62         else if(m_speed.Z < target_speed.Z)
63                 m_speed.Z = target_speed.Z;
64         else if(m_speed.Z > target_speed.Z)
65                 m_speed.Z = target_speed.Z;
66 }
67
68 /*
69         RemotePlayer
70 */
71
72 #ifndef SERVER
73
74 RemotePlayer::RemotePlayer(
75                 scene::ISceneNode* parent,
76                 IrrlichtDevice *device,
77                 s32 id):
78         scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
79         m_text(NULL)
80 {
81         m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
82
83         if(parent != NULL && device != NULL)
84         {
85                 // ISceneNode stores a member called SceneManager
86                 scene::ISceneManager* mgr = SceneManager;
87                 video::IVideoDriver* driver = mgr->getVideoDriver();
88                 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
89
90                 // Add a text node for showing the name
91                 wchar_t wname[1] = {0};
92                 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
93                                 wname, video::SColor(255,255,255,255), this);
94                 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
95
96                 // Attach a simple mesh to the player for showing an image
97                 scene::SMesh *mesh = new scene::SMesh();
98                 { // Front
99                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
100                 video::SColor c(255,255,255,255);
101                 video::S3DVertex vertices[4] =
102                 {
103                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
104                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
105                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
106                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
107                 };
108                 u16 indices[] = {0,1,2,2,3,0};
109                 buf->append(vertices, 4, indices, 6);
110                 // Set material
111                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
112                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
113                 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player.png").c_str()));
114                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
115                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
116                 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
117                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
118                 // Add to mesh
119                 mesh->addMeshBuffer(buf);
120                 buf->drop();
121                 }
122                 { // Back
123                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
124                 video::SColor c(255,255,255,255);
125                 video::S3DVertex vertices[4] =
126                 {
127                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
128                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
129                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
130                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
131                 };
132                 u16 indices[] = {0,1,2,2,3,0};
133                 buf->append(vertices, 4, indices, 6);
134                 // Set material
135                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
136                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
137                 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player_back.png").c_str()));
138                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
139                 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
140                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
141                 // Add to mesh
142                 mesh->addMeshBuffer(buf);
143                 buf->drop();
144                 }
145                 m_node = mgr->addMeshSceneNode(mesh, this);
146                 mesh->drop();
147                 m_node->setPosition(v3f(0,0,0));
148         }
149 }
150
151 RemotePlayer::~RemotePlayer()
152 {
153         if(SceneManager != NULL)
154                 ISceneNode::remove();
155 }
156
157 void RemotePlayer::updateName(const char *name)
158 {
159         Player::updateName(name);
160         if(m_text != NULL)
161         {
162                 wchar_t wname[PLAYERNAME_SIZE];
163                 mbstowcs(wname, m_name, strlen(m_name)+1);
164                 m_text->setText(wname);
165         }
166 }
167
168 void RemotePlayer::move(f32 dtime, Map &map)
169 {
170         m_pos_animation_time_counter += dtime;
171         m_pos_animation_counter += dtime;
172         v3f movevector = m_position - m_oldpos;
173         f32 moveratio;
174         if(m_pos_animation_time < 0.001)
175                 moveratio = 1.0;
176         else
177                 moveratio = m_pos_animation_counter / m_pos_animation_time;
178         if(moveratio > 1.5)
179                 moveratio = 1.5;
180         m_showpos = m_oldpos + movevector * moveratio;
181         
182         ISceneNode::setPosition(m_showpos);
183 }
184
185 #endif
186
187 #ifndef SERVER
188 /*
189         LocalPlayer
190 */
191
192 LocalPlayer::LocalPlayer()
193 {
194 }
195
196 LocalPlayer::~LocalPlayer()
197 {
198 }
199
200 void LocalPlayer::move(f32 dtime, Map &map)
201 {
202         v3f position = getPosition();
203         v3f oldpos = position;
204         v3s16 oldpos_i = floatToInt(oldpos);
205
206         /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
207                         <<oldpos_i.Z<<")"<<std::endl;*/
208
209         position += m_speed * dtime;
210
211         // Skip collision detection if player is non-local
212         if(isLocal() == false)
213         {
214                 setPosition(position);
215                 return;
216         }
217
218         /*
219                 Collision detection
220         */
221
222         v3s16 pos_i = floatToInt(position);
223         
224         /*
225                 Check if player is in water
226         */
227         try{
228                 if(in_water)
229                 {
230                         v3s16 pp = floatToInt(position + v3f(0,0,0));
231                         in_water = content_liquid(map.getNode(pp).d);
232                 }
233                 else
234                 {
235                         v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
236                         in_water = content_liquid(map.getNode(pp).d);
237                 }
238         }
239         catch(InvalidPositionException &e)
240         {
241                 in_water = false;
242         }
243
244         // The frame length is limited to the player going 0.1*BS per call
245         f32 d = (float)BS * 0.15;
246
247 #define PLAYER_RADIUS (BS*0.3)
248 #define PLAYER_HEIGHT (BS*1.7)
249
250         core::aabbox3d<f32> playerbox(
251                 position.X - PLAYER_RADIUS,
252                 position.Y - 0.0,
253                 position.Z - PLAYER_RADIUS,
254                 position.X + PLAYER_RADIUS,
255                 position.Y + PLAYER_HEIGHT,
256                 position.Z + PLAYER_RADIUS
257         );
258         core::aabbox3d<f32> playerbox_old(
259                 oldpos.X - PLAYER_RADIUS,
260                 oldpos.Y - 0.0,
261                 oldpos.Z - PLAYER_RADIUS,
262                 oldpos.X + PLAYER_RADIUS,
263                 oldpos.Y + PLAYER_HEIGHT,
264                 oldpos.Z + PLAYER_RADIUS
265         );
266
267         //hilightboxes.push_back(playerbox);
268
269         touching_ground = false;
270         
271         /*std::cout<<"Checking collisions for ("
272                         <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
273                         <<") -> ("
274                         <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
275                         <<"):"<<std::endl;*/
276
277         for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
278                 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
279                         for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
280                                 try{
281                                         if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
282                                                 continue;
283                                         }
284                                 }
285                                 catch(InvalidPositionException &e)
286                                 {
287                                         // Doing nothing here will block the player from
288                                         // walking over map borders
289
290                                         // Go over borders in debug mode
291                                         if(HAXMODE)
292                                                 continue;
293                                 }
294
295                                 core::aabbox3d<f32> nodebox = Map::getNodeBox(
296                                                 v3s16(x,y,z));
297                                 
298                                 // See if the player is touching ground
299                                 if(
300                                                 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
301                                                 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
302                                                 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
303                                                 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
304                                                 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
305                                 ){
306                                         touching_ground = true;
307                                 }
308                                 
309                                 if(playerbox.intersectsWithBox(nodebox))
310                                 {
311                                 
312         v3f dirs[3] = {
313                 v3f(0,0,1), // back
314                 v3f(0,1,0), // top
315                 v3f(1,0,0), // right
316         };
317         for(u16 i=0; i<3; i++)
318         {
319                 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
320                 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
321                 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
322                 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
323                 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
324                 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
325
326                 bool main_edge_collides = 
327                         ((nodemax > playermin && nodemax <= playermin_old + d
328                                 && m_speed.dotProduct(dirs[i]) < 0)
329                         ||
330                         (nodemin < playermax && nodemin >= playermax_old - d
331                                 && m_speed.dotProduct(dirs[i]) > 0));
332
333                 bool other_edges_collide = true;
334                 for(u16 j=0; j<3; j++)
335                 {
336                         if(j == i)
337                                 continue;
338                         f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
339                         f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
340                         f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
341                         f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
342                         if(!(nodemax - d > playermin && nodemin + d < playermax))
343                         {
344                                 other_edges_collide = false;
345                                 break;
346                         }
347                 }
348                 
349                 if(main_edge_collides && other_edges_collide)
350                 {
351                         m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
352                         position -= position.dotProduct(dirs[i]) * dirs[i];
353                         position += oldpos.dotProduct(dirs[i]) * dirs[i];
354                 }
355         
356         }
357                                 } // if(playerbox.intersectsWithBox(nodebox))
358                         } // for x
359                 } // for z
360         } // for y
361
362         setPosition(position);
363 }
364
365 void LocalPlayer::applyControl(float dtime)
366 {
367         // Random constants
368 #define WALK_ACCELERATION (4.0 * BS)
369 #define WALKSPEED_MAX (4.0 * BS)
370         f32 walk_acceleration = WALK_ACCELERATION;
371         f32 walkspeed_max = WALKSPEED_MAX;
372         
373         setPitch(control.pitch);
374         setYaw(control.yaw);
375         
376         v3f move_direction = v3f(0,0,1);
377         move_direction.rotateXZBy(getYaw());
378         
379         v3f speed = v3f(0,0,0);
380
381         if(HAXMODE)
382         {
383                 v3f speed = getSpeed();
384                 speed.Y = 0;
385                 setSpeed(speed);
386         }
387
388         // Superspeed mode
389         bool superspeed = false;
390         if(control.superspeed)
391         {
392                 if(HAXMODE)
393                 {
394                         v3f speed = getSpeed();
395                         speed.Y = -20*BS;
396                         setSpeed(speed);
397                 }
398                 else
399                 {
400                         speed += move_direction;
401                         superspeed = true;
402                 }
403         }
404
405         if(HAXMODE)
406                 superspeed = true;
407
408         if(control.up)
409         {
410                 speed += move_direction;
411         }
412         if(control.down)
413         {
414                 speed -= move_direction;
415         }
416         if(control.left)
417         {
418                 speed += move_direction.crossProduct(v3f(0,1,0));
419         }
420         if(control.right)
421         {
422                 speed += move_direction.crossProduct(v3f(0,-1,0));
423         }
424         if(control.jump)
425         {
426                 if(HAXMODE)
427                 {
428                         v3f speed = getSpeed();
429                         /*speed.Y += 20.*BS * dtime * 2;
430                         if(speed.Y < 0)
431                                 speed.Y = 0;*/
432                         speed.Y = 20*BS;
433                         setSpeed(speed);
434                 }
435                 else if(touching_ground)
436                 {
437                         v3f speed = getSpeed();
438                         speed.Y = 6.5*BS;
439                         setSpeed(speed);
440                 }
441                 else if(in_water)
442                 {
443                         v3f speed = getSpeed();
444                         speed.Y = 2.0*BS;
445                         setSpeed(speed);
446                 }
447         }
448
449         // The speed of the player (Y is ignored)
450         if(superspeed)
451                 speed = speed.normalize() * walkspeed_max * 5;
452         else
453                 speed = speed.normalize() * walkspeed_max;
454         
455         f32 inc = walk_acceleration * BS * dtime;
456
457         if(HAXMODE)
458                 inc = walk_acceleration * BS * dtime * 10;
459
460         // Accelerate to target speed with maximum increment
461         accelerate(speed, inc);
462 }
463 #endif
464