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