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