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