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"
31 touching_ground(false),
33 in_water_stable(false),
35 peer_id(PEER_ID_INEXISTENT),
41 updateName("<not set>");
49 void Player::resetInventory()
52 inventory.addList("main", PLAYER_INVENTORY_SIZE);
53 inventory.addList("craft", 9);
54 inventory.addList("craftresult", 1);
57 // Y direction is ignored
58 void Player::accelerate(v3f target_speed, f32 max_increase)
60 v3f d_wanted = target_speed - m_speed;
62 f32 dl_wanted = d_wanted.getLength();
67 v3f d = d_wanted.normalize() * dl;
74 if(m_speed.X < target_speed.X - max_increase)
75 m_speed.X += max_increase;
76 else if(m_speed.X > target_speed.X + max_increase)
77 m_speed.X -= max_increase;
78 else if(m_speed.X < target_speed.X)
79 m_speed.X = target_speed.X;
80 else if(m_speed.X > target_speed.X)
81 m_speed.X = target_speed.X;
83 if(m_speed.Z < target_speed.Z - max_increase)
84 m_speed.Z += max_increase;
85 else if(m_speed.Z > target_speed.Z + max_increase)
86 m_speed.Z -= max_increase;
87 else if(m_speed.Z < target_speed.Z)
88 m_speed.Z = target_speed.Z;
89 else if(m_speed.Z > target_speed.Z)
90 m_speed.Z = target_speed.Z;
94 void Player::serialize(std::ostream &os)
96 // Utilize a Settings object for storing values
98 args.setS32("version", 1);
99 args.set("name", m_name);
100 args.setFloat("pitch", m_pitch);
101 args.setFloat("yaw", m_yaw);
102 args.setV3F("position", m_position);
106 os<<"PlayerArgsEnd\n";
108 inventory.serialize(os);
111 void Player::deSerialize(std::istream &is)
118 throw SerializationError
119 ("Player::deSerialize(): PlayerArgsEnd not found");
121 std::getline(is, line);
122 std::string trimmedline = trim(line);
123 if(trimmedline == "PlayerArgsEnd")
125 args.parseConfigLine(line);
128 //args.getS32("version");
129 std::string name = args.get("name");
130 updateName(name.c_str());
131 m_pitch = args.getFloat("pitch");
132 m_yaw = args.getFloat("yaw");
133 m_position = args.getV3F("position");
135 inventory.deSerialize(is);
144 RemotePlayer::RemotePlayer(
145 scene::ISceneNode* parent,
146 IrrlichtDevice *device,
148 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
151 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
153 if(parent != NULL && device != NULL)
155 // ISceneNode stores a member called SceneManager
156 scene::ISceneManager* mgr = SceneManager;
157 video::IVideoDriver* driver = mgr->getVideoDriver();
158 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
160 // Add a text node for showing the name
161 wchar_t wname[1] = {0};
162 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
163 wname, video::SColor(255,255,255,255), this);
164 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
166 // Attach a simple mesh to the player for showing an image
167 scene::SMesh *mesh = new scene::SMesh();
169 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
170 video::SColor c(255,255,255,255);
171 video::S3DVertex vertices[4] =
173 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
174 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
175 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
176 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
178 u16 indices[] = {0,1,2,2,3,0};
179 buf->append(vertices, 4, indices, 6);
181 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
182 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
183 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player.png").c_str()));
184 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
185 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
186 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
187 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
189 mesh->addMeshBuffer(buf);
193 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
194 video::SColor c(255,255,255,255);
195 video::S3DVertex vertices[4] =
197 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
198 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
199 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
200 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
202 u16 indices[] = {0,1,2,2,3,0};
203 buf->append(vertices, 4, indices, 6);
205 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
206 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
207 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player_back.png").c_str()));
208 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
209 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
210 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
212 mesh->addMeshBuffer(buf);
215 m_node = mgr->addMeshSceneNode(mesh, this);
217 m_node->setPosition(v3f(0,0,0));
221 RemotePlayer::~RemotePlayer()
223 if(SceneManager != NULL)
224 ISceneNode::remove();
227 void RemotePlayer::updateName(const char *name)
229 Player::updateName(name);
232 wchar_t wname[PLAYERNAME_SIZE];
233 mbstowcs(wname, m_name, strlen(m_name)+1);
234 m_text->setText(wname);
238 void RemotePlayer::move(f32 dtime, Map &map)
240 m_pos_animation_time_counter += dtime;
241 m_pos_animation_counter += dtime;
242 v3f movevector = m_position - m_oldpos;
244 if(m_pos_animation_time < 0.001)
247 moveratio = m_pos_animation_counter / m_pos_animation_time;
250 m_showpos = m_oldpos + movevector * moveratio;
252 ISceneNode::setPosition(m_showpos);
262 LocalPlayer::LocalPlayer()
266 LocalPlayer::~LocalPlayer()
270 void LocalPlayer::move(f32 dtime, Map &map)
272 v3f position = getPosition();
273 v3f oldpos = position;
274 v3s16 oldpos_i = floatToInt(oldpos);
276 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
277 <<oldpos_i.Z<<")"<<std::endl;*/
279 position += m_speed * dtime;
281 bool free_move = g_settings.getBool("free_move");
283 // Skip collision detection if player is non-local or
284 // a special movement mode is used
285 if(isLocal() == false || free_move)
287 setPosition(position);
295 v3s16 pos_i = floatToInt(position);
298 Check if player is in water (the oscillating value)
303 v3s16 pp = floatToInt(position + v3f(0,0,0));
304 in_water = content_liquid(map.getNode(pp).d);
308 v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
309 in_water = content_liquid(map.getNode(pp).d);
312 catch(InvalidPositionException &e)
318 Check if player is in water (the stable value)
321 v3s16 pp = floatToInt(position + v3f(0,0,0));
322 in_water_stable = content_liquid(map.getNode(pp).d);
324 catch(InvalidPositionException &e)
326 in_water_stable = false;
329 // The frame length is limited to the player going 0.1*BS per call
330 f32 d = (float)BS * 0.15;
332 #define PLAYER_RADIUS (BS*0.3)
333 #define PLAYER_HEIGHT (BS*1.7)
335 core::aabbox3d<f32> playerbox(
336 position.X - PLAYER_RADIUS,
338 position.Z - PLAYER_RADIUS,
339 position.X + PLAYER_RADIUS,
340 position.Y + PLAYER_HEIGHT,
341 position.Z + PLAYER_RADIUS
343 core::aabbox3d<f32> playerbox_old(
344 oldpos.X - PLAYER_RADIUS,
346 oldpos.Z - PLAYER_RADIUS,
347 oldpos.X + PLAYER_RADIUS,
348 oldpos.Y + PLAYER_HEIGHT,
349 oldpos.Z + PLAYER_RADIUS
352 //hilightboxes.push_back(playerbox);
354 touching_ground = false;
356 /*std::cout<<"Checking collisions for ("
357 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
359 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
362 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
363 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
364 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
366 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
370 catch(InvalidPositionException &e)
372 // Doing nothing here will block the player from
373 // walking over map borders
376 core::aabbox3d<f32> nodebox = Map::getNodeBox(
379 // See if the player is touching ground
381 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
382 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
383 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
384 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
385 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
387 touching_ground = true;
390 if(playerbox.intersectsWithBox(nodebox))
398 for(u16 i=0; i<3; i++)
400 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
401 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
402 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
403 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
404 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
405 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
407 bool main_edge_collides =
408 ((nodemax > playermin && nodemax <= playermin_old + d
409 && m_speed.dotProduct(dirs[i]) < 0)
411 (nodemin < playermax && nodemin >= playermax_old - d
412 && m_speed.dotProduct(dirs[i]) > 0));
414 bool other_edges_collide = true;
415 for(u16 j=0; j<3; j++)
419 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
420 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
421 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
422 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
423 if(!(nodemax - d > playermin && nodemin + d < playermax))
425 other_edges_collide = false;
430 if(main_edge_collides && other_edges_collide)
432 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
433 position -= position.dotProduct(dirs[i]) * dirs[i];
434 position += oldpos.dotProduct(dirs[i]) * dirs[i];
438 } // if(playerbox.intersectsWithBox(nodebox))
443 setPosition(position);
446 void LocalPlayer::applyControl(float dtime)
452 #define WALK_ACCELERATION (4.0 * BS)
453 #define WALKSPEED_MAX (4.0 * BS)
454 f32 walk_acceleration = WALK_ACCELERATION;
455 f32 walkspeed_max = WALKSPEED_MAX;
457 setPitch(control.pitch);
460 v3f move_direction = v3f(0,0,1);
461 move_direction.rotateXZBy(getYaw());
463 v3f speed = v3f(0,0,0);
465 bool free_move = g_settings.getBool("free_move");
466 bool fast_move = g_settings.getBool("fast_move");
467 bool continuous_forward = g_settings.getBool("continuous_forward");
471 v3f speed = getSpeed();
476 // Whether superspeed mode is used or not
477 bool superspeed = false;
479 // If free movement and fast movement, always move fast
480 if(free_move && fast_move)
483 // Auxiliary button 1 (E)
488 // In free movement mode, aux1 descends
489 v3f speed = getSpeed();
493 speed.Y = -walkspeed_max;
498 // If not free movement but fast is allowed, aux1 is
505 if(continuous_forward)
506 speed += move_direction;
510 if(continuous_forward)
513 speed += move_direction;
517 speed -= move_direction;
521 speed += move_direction.crossProduct(v3f(0,1,0));
525 speed += move_direction.crossProduct(v3f(0,-1,0));
531 v3f speed = getSpeed();
535 speed.Y = walkspeed_max;
538 else if(touching_ground)
540 v3f speed = getSpeed();
544 // Use the oscillating value for getting out of water
545 // (so that the player doesn't fly on the surface)
548 v3f speed = getSpeed();
555 // The speed of the player (Y is ignored)
557 speed = speed.normalize() * walkspeed_max * 5;
559 speed = speed.normalize() * walkspeed_max;
561 f32 inc = walk_acceleration * BS * dtime;
563 // Faster acceleration if fast and free movement
564 if(free_move && fast_move)
565 inc = walk_acceleration * BS * dtime * 10;
567 // Accelerate to target speed with maximum increment
568 accelerate(speed, inc);