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