ef2a3bdfb16eeda1ccedc608570c73028ab0d894
[oweals/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 RemotePlayer::RemotePlayer(
232                 scene::ISceneNode* parent,
233                 IrrlichtDevice *device,
234                 s32 id):
235         scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
236         m_text(NULL)
237 {
238         m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
239         
240         if(parent != NULL && device != NULL)
241         {
242                 // ISceneNode stores a member called SceneManager
243                 scene::ISceneManager* mgr = SceneManager;
244                 video::IVideoDriver* driver = mgr->getVideoDriver();
245                 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
246
247                 // Add a text node for showing the name
248                 wchar_t wname[1] = {0};
249                 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
250                                 wname, video::SColor(255,255,255,255), this);
251                 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
252
253                 // Attach a simple mesh to the player for showing an image
254                 scene::SMesh *mesh = new scene::SMesh();
255                 { // Front
256                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
257                 video::SColor c(255,255,255,255);
258                 video::S3DVertex vertices[4] =
259                 {
260                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
261                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
262                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
263                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
264                 };
265                 u16 indices[] = {0,1,2,2,3,0};
266                 buf->append(vertices, 4, indices, 6);
267                 // Set material
268                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
269                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
270                 buf->getMaterial().setTexture(0, driver->getTexture("../data/player.png"));
271                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
272                 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
273                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
274                 // Add to mesh
275                 mesh->addMeshBuffer(buf);
276                 buf->drop();
277                 }
278                 { // Back
279                 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
280                 video::SColor c(255,255,255,255);
281                 video::S3DVertex vertices[4] =
282                 {
283                         video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
284                         video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
285                         video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
286                         video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
287                 };
288                 u16 indices[] = {0,1,2,2,3,0};
289                 buf->append(vertices, 4, indices, 6);
290                 // Set material
291                 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
292                 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
293                 buf->getMaterial().setTexture(0, driver->getTexture("../data/player_back.png"));
294                 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
295                 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
296                 // Add to mesh
297                 mesh->addMeshBuffer(buf);
298                 buf->drop();
299                 }
300                 scene::IMeshSceneNode *node = mgr->addMeshSceneNode(mesh, this);
301                 mesh->drop();
302                 node->setPosition(v3f(0,0,0));
303         }
304 }
305
306 RemotePlayer::~RemotePlayer()
307 {
308         if(SceneManager != NULL)
309                 ISceneNode::remove();
310 }
311
312 void RemotePlayer::updateName(const char *name)
313 {
314         Player::updateName(name);
315         if(m_text != NULL)
316         {
317                 wchar_t wname[PLAYERNAME_SIZE];
318                 mbstowcs(wname, m_name, strlen(m_name)+1);
319                 m_text->setText(wname);
320         }
321 }
322
323 /*
324         LocalPlayer
325 */
326
327 LocalPlayer::LocalPlayer()
328 {
329 }
330
331 LocalPlayer::~LocalPlayer()
332 {
333 }
334
335 void LocalPlayer::applyControl(float dtime)
336 {
337         // Random constants
338 #define WALK_ACCELERATION (4.0 * BS)
339 #define WALKSPEED_MAX (4.0 * BS)
340         f32 walk_acceleration = WALK_ACCELERATION;
341         f32 walkspeed_max = WALKSPEED_MAX;
342         
343         setPitch(control.pitch);
344         setYaw(control.yaw);
345         
346         v3f move_direction = v3f(0,0,1);
347         move_direction.rotateXZBy(getYaw());
348         
349         v3f speed = v3f(0,0,0);
350
351         // Superspeed mode
352         bool superspeed = false;
353         if(control.superspeed)
354         {
355                 speed += move_direction;
356                 superspeed = true;
357         }
358
359         if(control.up)
360         {
361                 speed += move_direction;
362         }
363         if(control.down)
364         {
365                 speed -= move_direction;
366         }
367         if(control.left)
368         {
369                 speed += move_direction.crossProduct(v3f(0,1,0));
370         }
371         if(control.right)
372         {
373                 speed += move_direction.crossProduct(v3f(0,-1,0));
374         }
375         if(control.jump)
376         {
377                 if(touching_ground)
378                 {
379                         v3f speed = getSpeed();
380                         speed.Y = 6.5*BS;
381                         setSpeed(speed);
382                 }
383                 else if(in_water)
384                 {
385                         v3f speed = getSpeed();
386                         speed.Y = 2.0*BS;
387                         setSpeed(speed);
388                 }
389         }
390
391         // The speed of the player (Y is ignored)
392         if(superspeed)
393                 speed = speed.normalize() * walkspeed_max * 5;
394         else
395                 speed = speed.normalize() * walkspeed_max;
396         
397         f32 inc = walk_acceleration * BS * dtime;
398         
399         // Accelerate to target speed with maximum increment
400         accelerate(speed, inc);
401 }
402
403