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