3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
26 #include "connection.h"
27 #include "constants.h"
30 touching_ground(false),
32 peer_id(PEER_ID_INEXISTENT),
36 updateName("<not set>");
37 inventory.addList("main", PLAYER_INVENTORY_SIZE);
38 inventory.addList("craft", 9);
39 inventory.addList("craftresult", 1);
46 // Y direction is ignored
47 void Player::accelerate(v3f target_speed, f32 max_increase)
49 v3f d_wanted = target_speed - m_speed;
51 f32 dl_wanted = d_wanted.getLength();
56 v3f d = d_wanted.normalize() * dl;
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;
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;
89 RemotePlayer::RemotePlayer(
90 scene::ISceneNode* parent,
91 IrrlichtDevice *device,
93 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
96 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
98 if(parent != NULL && device != NULL)
100 // ISceneNode stores a member called SceneManager
101 scene::ISceneManager* mgr = SceneManager;
102 video::IVideoDriver* driver = mgr->getVideoDriver();
103 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
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));
111 // Attach a simple mesh to the player for showing an image
112 scene::SMesh *mesh = new scene::SMesh();
114 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
115 video::SColor c(255,255,255,255);
116 video::S3DVertex vertices[4] =
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),
123 u16 indices[] = {0,1,2,2,3,0};
124 buf->append(vertices, 4, indices, 6);
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;
134 mesh->addMeshBuffer(buf);
138 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
139 video::SColor c(255,255,255,255);
140 video::S3DVertex vertices[4] =
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),
147 u16 indices[] = {0,1,2,2,3,0};
148 buf->append(vertices, 4, indices, 6);
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;
157 mesh->addMeshBuffer(buf);
160 m_node = mgr->addMeshSceneNode(mesh, this);
162 m_node->setPosition(v3f(0,0,0));
166 RemotePlayer::~RemotePlayer()
168 if(SceneManager != NULL)
169 ISceneNode::remove();
172 void RemotePlayer::updateName(const char *name)
174 Player::updateName(name);
177 wchar_t wname[PLAYERNAME_SIZE];
178 mbstowcs(wname, m_name, strlen(m_name)+1);
179 m_text->setText(wname);
183 void RemotePlayer::move(f32 dtime, Map &map)
185 m_pos_animation_time_counter += dtime;
186 m_pos_animation_counter += dtime;
187 v3f movevector = m_position - m_oldpos;
189 if(m_pos_animation_time < 0.001)
192 moveratio = m_pos_animation_counter / m_pos_animation_time;
195 m_showpos = m_oldpos + movevector * moveratio;
197 ISceneNode::setPosition(m_showpos);
207 LocalPlayer::LocalPlayer()
211 LocalPlayer::~LocalPlayer()
215 void LocalPlayer::move(f32 dtime, Map &map)
217 v3f position = getPosition();
218 v3f oldpos = position;
219 v3s16 oldpos_i = floatToInt(oldpos);
221 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
222 <<oldpos_i.Z<<")"<<std::endl;*/
224 position += m_speed * dtime;
226 bool haxmode = g_settings.getBool("haxmode");
228 // Skip collision detection if player is non-local
229 if(isLocal() == false || haxmode)
231 setPosition(position);
239 v3s16 pos_i = floatToInt(position);
242 Check if player is in water
247 v3s16 pp = floatToInt(position + v3f(0,0,0));
248 in_water = content_liquid(map.getNode(pp).d);
252 v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
253 in_water = content_liquid(map.getNode(pp).d);
256 catch(InvalidPositionException &e)
261 // The frame length is limited to the player going 0.1*BS per call
262 f32 d = (float)BS * 0.15;
264 #define PLAYER_RADIUS (BS*0.3)
265 #define PLAYER_HEIGHT (BS*1.7)
267 core::aabbox3d<f32> playerbox(
268 position.X - PLAYER_RADIUS,
270 position.Z - PLAYER_RADIUS,
271 position.X + PLAYER_RADIUS,
272 position.Y + PLAYER_HEIGHT,
273 position.Z + PLAYER_RADIUS
275 core::aabbox3d<f32> playerbox_old(
276 oldpos.X - PLAYER_RADIUS,
278 oldpos.Z - PLAYER_RADIUS,
279 oldpos.X + PLAYER_RADIUS,
280 oldpos.Y + PLAYER_HEIGHT,
281 oldpos.Z + PLAYER_RADIUS
284 //hilightboxes.push_back(playerbox);
286 touching_ground = false;
288 /*std::cout<<"Checking collisions for ("
289 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
291 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
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++){
298 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
302 catch(InvalidPositionException &e)
304 // Doing nothing here will block the player from
305 // walking over map borders
308 core::aabbox3d<f32> nodebox = Map::getNodeBox(
311 // See if the player is touching ground
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
319 touching_ground = true;
322 if(playerbox.intersectsWithBox(nodebox))
330 for(u16 i=0; i<3; i++)
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]);
339 bool main_edge_collides =
340 ((nodemax > playermin && nodemax <= playermin_old + d
341 && m_speed.dotProduct(dirs[i]) < 0)
343 (nodemin < playermax && nodemin >= playermax_old - d
344 && m_speed.dotProduct(dirs[i]) > 0));
346 bool other_edges_collide = true;
347 for(u16 j=0; j<3; j++)
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))
357 other_edges_collide = false;
362 if(main_edge_collides && other_edges_collide)
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];
370 } // if(playerbox.intersectsWithBox(nodebox))
375 setPosition(position);
378 void LocalPlayer::applyControl(float dtime)
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;
386 setPitch(control.pitch);
389 v3f move_direction = v3f(0,0,1);
390 move_direction.rotateXZBy(getYaw());
392 v3f speed = v3f(0,0,0);
394 bool haxmode = g_settings.getBool("haxmode");
398 v3f speed = getSpeed();
404 bool superspeed = false;
405 if(control.superspeed)
409 v3f speed = getSpeed();
415 speed += move_direction;
425 speed += move_direction;
429 speed -= move_direction;
433 speed += move_direction.crossProduct(v3f(0,1,0));
437 speed += move_direction.crossProduct(v3f(0,-1,0));
443 v3f speed = getSpeed();
444 /*speed.Y += 20.*BS * dtime * 2;
450 else if(touching_ground)
452 v3f speed = getSpeed();
458 v3f speed = getSpeed();
464 // The speed of the player (Y is ignored)
466 speed = speed.normalize() * walkspeed_max * 5;
468 speed = speed.normalize() * walkspeed_max;
470 f32 inc = walk_acceleration * BS * dtime;
473 inc = walk_acceleration * BS * dtime * 10;
475 // Accelerate to target speed with maximum increment
476 accelerate(speed, inc);