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 inventory(PLAYER_INVENTORY_SIZE),
37 updateName("<not set>");
44 // Y direction is ignored
45 void Player::accelerate(v3f target_speed, f32 max_increase)
47 if(m_speed.X < target_speed.X - max_increase)
48 m_speed.X += max_increase;
49 else if(m_speed.X > target_speed.X + max_increase)
50 m_speed.X -= max_increase;
51 else if(m_speed.X < target_speed.X)
52 m_speed.X = target_speed.X;
53 else if(m_speed.X > target_speed.X)
54 m_speed.X = target_speed.X;
56 if(m_speed.Z < target_speed.Z - max_increase)
57 m_speed.Z += max_increase;
58 else if(m_speed.Z > target_speed.Z + max_increase)
59 m_speed.Z -= max_increase;
60 else if(m_speed.Z < target_speed.Z)
61 m_speed.Z = target_speed.Z;
62 else if(m_speed.Z > target_speed.Z)
63 m_speed.Z = target_speed.Z;
72 RemotePlayer::RemotePlayer(
73 scene::ISceneNode* parent,
74 IrrlichtDevice *device,
76 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
79 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
81 if(parent != NULL && device != NULL)
83 // ISceneNode stores a member called SceneManager
84 scene::ISceneManager* mgr = SceneManager;
85 video::IVideoDriver* driver = mgr->getVideoDriver();
86 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
88 // Add a text node for showing the name
89 wchar_t wname[1] = {0};
90 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
91 wname, video::SColor(255,255,255,255), this);
92 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
94 // Attach a simple mesh to the player for showing an image
95 scene::SMesh *mesh = new scene::SMesh();
97 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
98 video::SColor c(255,255,255,255);
99 video::S3DVertex vertices[4] =
101 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
102 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
103 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
104 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
106 u16 indices[] = {0,1,2,2,3,0};
107 buf->append(vertices, 4, indices, 6);
109 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
110 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
111 buf->getMaterial().setTexture(0, driver->getTexture("../data/player.png"));
112 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
113 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
114 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
116 mesh->addMeshBuffer(buf);
120 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
121 video::SColor c(255,255,255,255);
122 video::S3DVertex vertices[4] =
124 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
125 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
126 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
127 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
129 u16 indices[] = {0,1,2,2,3,0};
130 buf->append(vertices, 4, indices, 6);
132 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
133 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
134 buf->getMaterial().setTexture(0, driver->getTexture("../data/player_back.png"));
135 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
136 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
138 mesh->addMeshBuffer(buf);
141 m_node = mgr->addMeshSceneNode(mesh, this);
143 m_node->setPosition(v3f(0,0,0));
147 RemotePlayer::~RemotePlayer()
149 if(SceneManager != NULL)
150 ISceneNode::remove();
153 void RemotePlayer::updateName(const char *name)
155 Player::updateName(name);
158 wchar_t wname[PLAYERNAME_SIZE];
159 mbstowcs(wname, m_name, strlen(m_name)+1);
160 m_text->setText(wname);
164 void RemotePlayer::move(f32 dtime, Map &map)
166 m_pos_animation_time_counter += dtime;
167 m_pos_animation_counter += dtime;
168 v3f movevector = m_position - m_oldpos;
170 if(m_pos_animation_time < 0.001)
173 moveratio = m_pos_animation_counter / m_pos_animation_time;
176 m_showpos = m_oldpos + movevector * moveratio;
178 ISceneNode::setPosition(m_showpos);
188 LocalPlayer::LocalPlayer()
192 LocalPlayer::~LocalPlayer()
196 void LocalPlayer::move(f32 dtime, Map &map)
198 v3f position = getPosition();
199 v3f oldpos = position;
200 v3s16 oldpos_i = floatToInt(oldpos);
202 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
203 <<oldpos_i.Z<<")"<<std::endl;*/
205 position += m_speed * dtime;
207 // Skip collision detection if player is non-local
208 if(isLocal() == false)
210 setPosition(position);
218 v3s16 pos_i = floatToInt(position);
221 Check if player is in water
226 v3s16 pp = floatToInt(position + v3f(0,0,0));
227 in_water = content_liquid(map.getNode(pp).d);
231 v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
232 in_water = content_liquid(map.getNode(pp).d);
235 catch(InvalidPositionException &e)
240 // The frame length is limited to the player going 0.1*BS per call
241 f32 d = (float)BS * 0.15;
243 #define PLAYER_RADIUS (BS*0.3)
244 #define PLAYER_HEIGHT (BS*1.7)
246 core::aabbox3d<f32> playerbox(
247 position.X - PLAYER_RADIUS,
249 position.Z - PLAYER_RADIUS,
250 position.X + PLAYER_RADIUS,
251 position.Y + PLAYER_HEIGHT,
252 position.Z + PLAYER_RADIUS
254 core::aabbox3d<f32> playerbox_old(
255 oldpos.X - PLAYER_RADIUS,
257 oldpos.Z - PLAYER_RADIUS,
258 oldpos.X + PLAYER_RADIUS,
259 oldpos.Y + PLAYER_HEIGHT,
260 oldpos.Z + PLAYER_RADIUS
263 //hilightboxes.push_back(playerbox);
265 touching_ground = false;
267 /*std::cout<<"Checking collisions for ("
268 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
270 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
273 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
274 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
275 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
277 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
281 catch(InvalidPositionException &e)
283 // Doing nothing here will block the player from
284 // walking over map borders
287 core::aabbox3d<f32> nodebox = Map::getNodeBox(
290 // See if the player is touching ground
292 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
293 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
294 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
295 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
296 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
298 touching_ground = true;
301 if(playerbox.intersectsWithBox(nodebox))
309 for(u16 i=0; i<3; i++)
311 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
312 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
313 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
314 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
315 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
316 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
318 bool main_edge_collides =
319 ((nodemax > playermin && nodemax <= playermin_old + d
320 && m_speed.dotProduct(dirs[i]) < 0)
322 (nodemin < playermax && nodemin >= playermax_old - d
323 && m_speed.dotProduct(dirs[i]) > 0));
325 bool other_edges_collide = true;
326 for(u16 j=0; j<3; j++)
330 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
331 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
332 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
333 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
334 if(!(nodemax - d > playermin && nodemin + d < playermax))
336 other_edges_collide = false;
341 if(main_edge_collides && other_edges_collide)
343 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
344 position -= position.dotProduct(dirs[i]) * dirs[i];
345 position += oldpos.dotProduct(dirs[i]) * dirs[i];
349 } // if(playerbox.intersectsWithBox(nodebox))
354 setPosition(position);
357 void LocalPlayer::applyControl(float dtime)
360 #define WALK_ACCELERATION (4.0 * BS)
361 #define WALKSPEED_MAX (4.0 * BS)
362 f32 walk_acceleration = WALK_ACCELERATION;
363 f32 walkspeed_max = WALKSPEED_MAX;
365 setPitch(control.pitch);
368 v3f move_direction = v3f(0,0,1);
369 move_direction.rotateXZBy(getYaw());
371 v3f speed = v3f(0,0,0);
374 bool superspeed = false;
375 if(control.superspeed)
377 speed += move_direction;
383 speed += move_direction;
387 speed -= move_direction;
391 speed += move_direction.crossProduct(v3f(0,1,0));
395 speed += move_direction.crossProduct(v3f(0,-1,0));
401 v3f speed = getSpeed();
407 v3f speed = getSpeed();
413 // The speed of the player (Y is ignored)
415 speed = speed.normalize() * walkspeed_max * 5;
417 speed = speed.normalize() * walkspeed_max;
419 f32 inc = walk_acceleration * BS * dtime;
421 // Accelerate to target speed with maximum increment
422 accelerate(speed, inc);