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