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