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 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;
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;
74 RemotePlayer::RemotePlayer(
75 scene::ISceneNode* parent,
76 IrrlichtDevice *device,
78 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
81 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
83 if(parent != NULL && device != NULL)
85 // ISceneNode stores a member called SceneManager
86 scene::ISceneManager* mgr = SceneManager;
87 video::IVideoDriver* driver = mgr->getVideoDriver();
88 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
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));
96 // Attach a simple mesh to the player for showing an image
97 scene::SMesh *mesh = new scene::SMesh();
99 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
100 video::SColor c(255,255,255,255);
101 video::S3DVertex vertices[4] =
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),
108 u16 indices[] = {0,1,2,2,3,0};
109 buf->append(vertices, 4, indices, 6);
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;
119 mesh->addMeshBuffer(buf);
123 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
124 video::SColor c(255,255,255,255);
125 video::S3DVertex vertices[4] =
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),
132 u16 indices[] = {0,1,2,2,3,0};
133 buf->append(vertices, 4, indices, 6);
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;
142 mesh->addMeshBuffer(buf);
145 m_node = mgr->addMeshSceneNode(mesh, this);
147 m_node->setPosition(v3f(0,0,0));
151 RemotePlayer::~RemotePlayer()
153 if(SceneManager != NULL)
154 ISceneNode::remove();
157 void RemotePlayer::updateName(const char *name)
159 Player::updateName(name);
162 wchar_t wname[PLAYERNAME_SIZE];
163 mbstowcs(wname, m_name, strlen(m_name)+1);
164 m_text->setText(wname);
168 void RemotePlayer::move(f32 dtime, Map &map)
170 m_pos_animation_time_counter += dtime;
171 m_pos_animation_counter += dtime;
172 v3f movevector = m_position - m_oldpos;
174 if(m_pos_animation_time < 0.001)
177 moveratio = m_pos_animation_counter / m_pos_animation_time;
180 m_showpos = m_oldpos + movevector * moveratio;
182 ISceneNode::setPosition(m_showpos);
192 LocalPlayer::LocalPlayer()
196 LocalPlayer::~LocalPlayer()
200 void LocalPlayer::move(f32 dtime, Map &map)
202 v3f position = getPosition();
203 v3f oldpos = position;
204 v3s16 oldpos_i = floatToInt(oldpos);
206 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
207 <<oldpos_i.Z<<")"<<std::endl;*/
209 position += m_speed * dtime;
211 // Skip collision detection if player is non-local
212 if(isLocal() == false)
214 setPosition(position);
222 v3s16 pos_i = floatToInt(position);
225 Check if player is in water
230 v3s16 pp = floatToInt(position + v3f(0,0,0));
231 in_water = content_liquid(map.getNode(pp).d);
235 v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
236 in_water = content_liquid(map.getNode(pp).d);
239 catch(InvalidPositionException &e)
244 // The frame length is limited to the player going 0.1*BS per call
245 f32 d = (float)BS * 0.15;
247 #define PLAYER_RADIUS (BS*0.3)
248 #define PLAYER_HEIGHT (BS*1.7)
250 core::aabbox3d<f32> playerbox(
251 position.X - PLAYER_RADIUS,
253 position.Z - PLAYER_RADIUS,
254 position.X + PLAYER_RADIUS,
255 position.Y + PLAYER_HEIGHT,
256 position.Z + PLAYER_RADIUS
258 core::aabbox3d<f32> playerbox_old(
259 oldpos.X - PLAYER_RADIUS,
261 oldpos.Z - PLAYER_RADIUS,
262 oldpos.X + PLAYER_RADIUS,
263 oldpos.Y + PLAYER_HEIGHT,
264 oldpos.Z + PLAYER_RADIUS
267 //hilightboxes.push_back(playerbox);
269 touching_ground = false;
271 /*std::cout<<"Checking collisions for ("
272 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
274 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
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++){
281 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
285 catch(InvalidPositionException &e)
287 // Doing nothing here will block the player from
288 // walking over map borders
290 // Go over borders in debug mode
295 core::aabbox3d<f32> nodebox = Map::getNodeBox(
298 // See if the player is touching ground
300 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
301 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
302 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
303 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
304 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
306 touching_ground = true;
309 if(playerbox.intersectsWithBox(nodebox))
317 for(u16 i=0; i<3; i++)
319 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
320 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
321 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
322 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
323 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
324 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
326 bool main_edge_collides =
327 ((nodemax > playermin && nodemax <= playermin_old + d
328 && m_speed.dotProduct(dirs[i]) < 0)
330 (nodemin < playermax && nodemin >= playermax_old - d
331 && m_speed.dotProduct(dirs[i]) > 0));
333 bool other_edges_collide = true;
334 for(u16 j=0; j<3; j++)
338 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
339 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
340 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
341 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
342 if(!(nodemax - d > playermin && nodemin + d < playermax))
344 other_edges_collide = false;
349 if(main_edge_collides && other_edges_collide)
351 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
352 position -= position.dotProduct(dirs[i]) * dirs[i];
353 position += oldpos.dotProduct(dirs[i]) * dirs[i];
357 } // if(playerbox.intersectsWithBox(nodebox))
362 setPosition(position);
365 void LocalPlayer::applyControl(float dtime)
368 #define WALK_ACCELERATION (4.0 * BS)
369 #define WALKSPEED_MAX (4.0 * BS)
370 f32 walk_acceleration = WALK_ACCELERATION;
371 f32 walkspeed_max = WALKSPEED_MAX;
373 setPitch(control.pitch);
376 v3f move_direction = v3f(0,0,1);
377 move_direction.rotateXZBy(getYaw());
379 v3f speed = v3f(0,0,0);
383 v3f speed = getSpeed();
389 bool superspeed = false;
390 if(control.superspeed)
394 v3f speed = getSpeed();
400 speed += move_direction;
410 speed += move_direction;
414 speed -= move_direction;
418 speed += move_direction.crossProduct(v3f(0,1,0));
422 speed += move_direction.crossProduct(v3f(0,-1,0));
428 v3f speed = getSpeed();
429 /*speed.Y += 20.*BS * dtime * 2;
435 else if(touching_ground)
437 v3f speed = getSpeed();
443 v3f speed = getSpeed();
449 // The speed of the player (Y is ignored)
451 speed = speed.normalize() * walkspeed_max * 5;
453 speed = speed.normalize() * walkspeed_max;
455 f32 inc = walk_acceleration * BS * dtime;
458 inc = walk_acceleration * BS * dtime * 10;
460 // Accelerate to target speed with maximum increment
461 accelerate(speed, inc);