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 void Player::move(f32 dtime, Map &map)
46 v3f position = getPosition();
47 v3f oldpos = position;
48 v3s16 oldpos_i = floatToInt(oldpos);
50 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
51 <<oldpos_i.Z<<")"<<std::endl;*/
53 position += m_speed * dtime;
55 // Skip collision detection if player is non-local
56 if(isLocal() == false)
58 setPosition(position);
66 v3s16 pos_i = floatToInt(position);
69 Check if player is in water
74 v3s16 pp = floatToInt(position + v3f(0,0,0));
75 in_water = content_liquid(map.getNode(pp).d);
79 v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
80 in_water = content_liquid(map.getNode(pp).d);
83 catch(InvalidPositionException &e)
88 // The frame length is limited to the player going 0.1*BS per call
89 f32 d = (float)BS * 0.15;
91 #define PLAYER_RADIUS (BS*0.3)
92 #define PLAYER_HEIGHT (BS*1.7)
94 core::aabbox3d<f32> playerbox(
95 position.X - PLAYER_RADIUS,
97 position.Z - PLAYER_RADIUS,
98 position.X + PLAYER_RADIUS,
99 position.Y + PLAYER_HEIGHT,
100 position.Z + PLAYER_RADIUS
102 core::aabbox3d<f32> playerbox_old(
103 oldpos.X - PLAYER_RADIUS,
105 oldpos.Z - PLAYER_RADIUS,
106 oldpos.X + PLAYER_RADIUS,
107 oldpos.Y + PLAYER_HEIGHT,
108 oldpos.Z + PLAYER_RADIUS
111 //hilightboxes.push_back(playerbox);
113 touching_ground = false;
115 /*std::cout<<"Checking collisions for ("
116 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
118 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
121 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
122 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
123 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
125 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
129 catch(InvalidPositionException &e)
131 // Doing nothing here will block the player from
132 // walking over map borders
135 core::aabbox3d<f32> nodebox = Map::getNodeBox(
138 // See if the player is touching ground
140 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
141 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
142 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
143 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
144 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
146 touching_ground = true;
149 if(playerbox.intersectsWithBox(nodebox))
157 for(u16 i=0; i<3; i++)
159 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
160 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
161 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
162 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
163 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
164 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
166 bool main_edge_collides =
167 ((nodemax > playermin && nodemax <= playermin_old + d
168 && m_speed.dotProduct(dirs[i]) < 0)
170 (nodemin < playermax && nodemin >= playermax_old - d
171 && m_speed.dotProduct(dirs[i]) > 0));
173 bool other_edges_collide = true;
174 for(u16 j=0; j<3; j++)
178 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
179 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
180 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
181 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
182 if(!(nodemax - d > playermin && nodemin + d < playermax))
184 other_edges_collide = false;
189 if(main_edge_collides && other_edges_collide)
191 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
192 position -= position.dotProduct(dirs[i]) * dirs[i];
193 position += oldpos.dotProduct(dirs[i]) * dirs[i];
197 } // if(playerbox.intersectsWithBox(nodebox))
202 setPosition(position);
205 // Y direction is ignored
206 void Player::accelerate(v3f target_speed, f32 max_increase)
208 if(m_speed.X < target_speed.X - max_increase)
209 m_speed.X += max_increase;
210 else if(m_speed.X > target_speed.X + max_increase)
211 m_speed.X -= max_increase;
212 else if(m_speed.X < target_speed.X)
213 m_speed.X = target_speed.X;
214 else if(m_speed.X > target_speed.X)
215 m_speed.X = target_speed.X;
217 if(m_speed.Z < target_speed.Z - max_increase)
218 m_speed.Z += max_increase;
219 else if(m_speed.Z > target_speed.Z + max_increase)
220 m_speed.Z -= max_increase;
221 else if(m_speed.Z < target_speed.Z)
222 m_speed.Z = target_speed.Z;
223 else if(m_speed.Z > target_speed.Z)
224 m_speed.Z = target_speed.Z;
231 RemotePlayer::RemotePlayer(
232 scene::ISceneNode* parent,
233 IrrlichtDevice *device,
235 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
238 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
240 if(parent != NULL && device != NULL)
242 // ISceneNode stores a member called SceneManager
243 scene::ISceneManager* mgr = SceneManager;
244 video::IVideoDriver* driver = mgr->getVideoDriver();
245 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
247 // Add a text node for showing the name
248 wchar_t wname[1] = {0};
249 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
250 wname, video::SColor(255,255,255,255), this);
251 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
253 // Attach a simple mesh to the player for showing an image
254 scene::SMesh *mesh = new scene::SMesh();
256 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
257 video::SColor c(255,255,255,255);
258 video::S3DVertex vertices[4] =
260 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
261 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
262 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
263 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
265 u16 indices[] = {0,1,2,2,3,0};
266 buf->append(vertices, 4, indices, 6);
268 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
269 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
270 buf->getMaterial().setTexture(0, driver->getTexture("../data/player.png"));
271 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
272 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
273 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
275 mesh->addMeshBuffer(buf);
279 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
280 video::SColor c(255,255,255,255);
281 video::S3DVertex vertices[4] =
283 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
284 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
285 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
286 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
288 u16 indices[] = {0,1,2,2,3,0};
289 buf->append(vertices, 4, indices, 6);
291 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
292 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
293 buf->getMaterial().setTexture(0, driver->getTexture("../data/player_back.png"));
294 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
295 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
297 mesh->addMeshBuffer(buf);
300 scene::IMeshSceneNode *node = mgr->addMeshSceneNode(mesh, this);
302 node->setPosition(v3f(0,0,0));
306 RemotePlayer::~RemotePlayer()
308 if(SceneManager != NULL)
309 ISceneNode::remove();
312 void RemotePlayer::updateName(const char *name)
314 Player::updateName(name);
317 wchar_t wname[PLAYERNAME_SIZE];
318 mbstowcs(wname, m_name, strlen(m_name)+1);
319 m_text->setText(wname);
327 LocalPlayer::LocalPlayer()
331 LocalPlayer::~LocalPlayer()
335 void LocalPlayer::applyControl(float dtime)
338 #define WALK_ACCELERATION (4.0 * BS)
339 #define WALKSPEED_MAX (4.0 * BS)
340 f32 walk_acceleration = WALK_ACCELERATION;
341 f32 walkspeed_max = WALKSPEED_MAX;
343 setPitch(control.pitch);
346 v3f move_direction = v3f(0,0,1);
347 move_direction.rotateXZBy(getYaw());
349 v3f speed = v3f(0,0,0);
352 bool superspeed = false;
353 if(control.superspeed)
355 speed += move_direction;
361 speed += move_direction;
365 speed -= move_direction;
369 speed += move_direction.crossProduct(v3f(0,1,0));
373 speed += move_direction.crossProduct(v3f(0,-1,0));
379 v3f speed = getSpeed();
385 v3f speed = getSpeed();
391 // The speed of the player (Y is ignored)
393 speed = speed.normalize() * walkspeed_max * 5;
395 speed = speed.normalize() * walkspeed_max;
397 f32 inc = walk_acceleration * BS * dtime;
399 // Accelerate to target speed with maximum increment
400 accelerate(speed, inc);