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