/* Minetest-c55 Copyright (C) 2010-2011 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "player.h" #include "map.h" #include "connection.h" #include "constants.h" #include "utility.h" Player::Player(): touching_ground(false), in_water(false), in_water_stable(false), swimming_up(false), inventory_backup(NULL), craftresult_is_preview(true), hp(20), peer_id(PEER_ID_INEXISTENT), m_selected_item(0), m_pitch(0), m_yaw(0), m_speed(0,0,0), m_position(0,0,0) { updateName(""); resetInventory(); } Player::~Player() { delete inventory_backup; } void Player::wieldItem(u16 item) { m_selected_item = item; } void Player::resetInventory() { inventory.clear(); inventory.addList("main", PLAYER_INVENTORY_SIZE); inventory.addList("craft", 9); inventory.addList("craftresult", 1); } // Y direction is ignored void Player::accelerate(v3f target_speed, f32 max_increase) { v3f d_wanted = target_speed - m_speed; d_wanted.Y = 0; f32 dl_wanted = d_wanted.getLength(); f32 dl = dl_wanted; if(dl > max_increase) dl = max_increase; v3f d = d_wanted.normalize() * dl; m_speed.X += d.X; m_speed.Z += d.Z; //m_speed += d; #if 0 // old code if(m_speed.X < target_speed.X - max_increase) m_speed.X += max_increase; else if(m_speed.X > target_speed.X + max_increase) m_speed.X -= max_increase; else if(m_speed.X < target_speed.X) m_speed.X = target_speed.X; else if(m_speed.X > target_speed.X) m_speed.X = target_speed.X; if(m_speed.Z < target_speed.Z - max_increase) m_speed.Z += max_increase; else if(m_speed.Z > target_speed.Z + max_increase) m_speed.Z -= max_increase; else if(m_speed.Z < target_speed.Z) m_speed.Z = target_speed.Z; else if(m_speed.Z > target_speed.Z) m_speed.Z = target_speed.Z; #endif } void Player::serialize(std::ostream &os) { // Utilize a Settings object for storing values Settings args; args.setS32("version", 1); args.set("name", m_name); //args.set("password", m_password); args.setFloat("pitch", m_pitch); args.setFloat("yaw", m_yaw); args.setV3F("position", m_position); args.setBool("craftresult_is_preview", craftresult_is_preview); args.setS32("hp", hp); args.writeLines(os); os<<"PlayerArgsEnd\n"; // If actual inventory is backed up due to creative mode, save it // instead of the dummy creative mode inventory if(inventory_backup) inventory_backup->serialize(os); else inventory.serialize(os); } void Player::deSerialize(std::istream &is) { Settings args; for(;;) { if(is.eof()) throw SerializationError ("Player::deSerialize(): PlayerArgsEnd not found"); std::string line; std::getline(is, line); std::string trimmedline = trim(line); if(trimmedline == "PlayerArgsEnd") break; args.parseConfigLine(line); } //args.getS32("version"); std::string name = args.get("name"); updateName(name.c_str()); /*std::string password = ""; if(args.exists("password")) password = args.get("password"); updatePassword(password.c_str());*/ m_pitch = args.getFloat("pitch"); m_yaw = args.getFloat("yaw"); m_position = args.getV3F("position"); try{ craftresult_is_preview = args.getBool("craftresult_is_preview"); }catch(SettingNotFoundException &e){ craftresult_is_preview = true; } try{ hp = args.getS32("hp"); }catch(SettingNotFoundException &e){ hp = 20; } /*try{ std::string sprivs = args.get("privs"); if(sprivs == "all") { privs = PRIV_ALL; } else { std::istringstream ss(sprivs); ss>>privs; } }catch(SettingNotFoundException &e){ privs = PRIV_DEFAULT; }*/ inventory.deSerialize(is); } /* RemotePlayer */ #ifndef SERVER RemotePlayer::RemotePlayer( scene::ISceneNode* parent, IrrlichtDevice *device, s32 id): scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id), m_text(NULL) { m_box = core::aabbox3d(-BS/2,0,-BS/2,BS/2,BS*2,BS/2); if(parent != NULL && device != NULL) { // ISceneNode stores a member called SceneManager scene::ISceneManager* mgr = SceneManager; video::IVideoDriver* driver = mgr->getVideoDriver(); gui::IGUIEnvironment* gui = device->getGUIEnvironment(); // Add a text node for showing the name wchar_t wname[1] = {0}; m_text = mgr->addTextSceneNode(gui->getBuiltInFont(), wname, video::SColor(255,255,255,255), this); m_text->setPosition(v3f(0, (f32)BS*2.1, 0)); // Attach a simple mesh to the player for showing an image scene::SMesh *mesh = new scene::SMesh(); { // Front scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); video::S3DVertex vertices[4] = { video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1), video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1), video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0), video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0), }; u16 indices[] = {0,1,2,2,3,0}; buf->append(vertices, 4, indices, 6); // Set material buf->getMaterial().setFlag(video::EMF_LIGHTING, false); //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player.png").c_str())); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true); //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; // Add to mesh mesh->addMeshBuffer(buf); buf->drop(); } { // Back scene::IMeshBuffer *buf = new scene::SMeshBuffer(); video::SColor c(255,255,255,255); video::S3DVertex vertices[4] = { video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1), video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1), video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0), video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0), }; u16 indices[] = {0,1,2,2,3,0}; buf->append(vertices, 4, indices, 6); // Set material buf->getMaterial().setFlag(video::EMF_LIGHTING, false); //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false); buf->getMaterial().setTexture(0, driver->getTexture(getTexturePath("player_back.png").c_str())); buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false); buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true); buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF; // Add to mesh mesh->addMeshBuffer(buf); buf->drop(); } m_node = mgr->addMeshSceneNode(mesh, this); mesh->drop(); m_node->setPosition(v3f(0,0,0)); } } RemotePlayer::~RemotePlayer() { if(SceneManager != NULL) ISceneNode::remove(); } void RemotePlayer::updateName(const char *name) { Player::updateName(name); if(m_text != NULL) { wchar_t wname[PLAYERNAME_SIZE]; mbstowcs(wname, m_name, strlen(m_name)+1); m_text->setText(wname); } } void RemotePlayer::move(f32 dtime, Map &map, f32 pos_max_d) { m_pos_animation_time_counter += dtime; m_pos_animation_counter += dtime; v3f movevector = m_position - m_oldpos; f32 moveratio; if(m_pos_animation_time < 0.001) moveratio = 1.0; else moveratio = m_pos_animation_counter / m_pos_animation_time; if(moveratio > 1.5) moveratio = 1.5; m_showpos = m_oldpos + movevector * moveratio; ISceneNode::setPosition(m_showpos); } #endif #ifndef SERVER /* LocalPlayer */ LocalPlayer::LocalPlayer(): m_sneak_node(32767,32767,32767), m_sneak_node_exists(false) { // Initialize hp to 0, so that no hearts will be shown if server // doesn't support health points hp = 0; // No tool wielded initially wield = NULL; } LocalPlayer::~LocalPlayer() { } void LocalPlayer::wieldItem(u16 item) { m_selected_item = item; if(wield) { InventoryItem* i = inventory.getList("main")->getItem(m_selected_item); if(i && strcmp(i->getName(), "ToolItem") == 0) { wield->getMaterial(0).setTexture(0, i->getImageRaw()); wield->setVisible(true); } else wield->setVisible(false); } } void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d, core::list *collision_info) { v3f position = getPosition(); v3f oldpos = position; v3s16 oldpos_i = floatToInt(oldpos, BS); v3f old_speed = m_speed; /*std::cout<<"oldpos_i=("< pos_max_d); float player_radius = BS*0.35; float player_height = BS*1.7; // Maximum distance over border for sneaking f32 sneak_max = BS*0.4; /* If sneaking, player has larger collision radius to keep from falling */ /*if(control.sneak) player_radius = sneak_max + d*1.1;*/ /* If sneaking, keep in range from the last walked node and don't fall off from it */ if(control.sneak && m_sneak_node_exists) { f32 maxd = 0.5*BS + sneak_max; v3f lwn_f = intToFloat(m_sneak_node, BS); position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd); position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd); f32 min_y = lwn_f.Y + 0.5*BS; if(position.Y < min_y) { position.Y = min_y; //v3f old_speed = m_speed; if(m_speed.Y < 0) m_speed/* Minetest Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "test.h" #include <sstream> #include "log.h" #include "serialization.h" #include "nodedef.h" #include "noise.h" class TestFilePath : public TestBase { public: TestFilePath() { TestManager::registerTestModule(this); } const char *getName() { return "TestFilePath"; } void runTests(IGameDef *gamedef); void testIsDirDelimiter(); void testPathStartsWith(); void testRemoveLastPathComponent(); void testRemoveLastPathComponentWithTrailingDelimiter(); void testRemoveRelativePathComponent(); }; static TestFilePath g_test_instance; void TestFilePath::runTests(IGameDef *gamedef) { TEST(testIsDirDelimiter); TEST(testPathStartsWith); TEST(testRemoveLastPathComponent); TEST(testRemoveLastPathComponentWithTrailingDelimiter); TEST(testRemoveRelativePathComponent); } //////////////////////////////////////////////////////////////////////////////// // adjusts a POSIX path to system-specific conventions // -> changes '/' to DIR_DELIM // -> absolute paths start with "C:\\" on windows std::string p(std::string path) { for (size_t i = 0; i < path.size(); ++i) { if (path[i] == '/') { path.replace(i, 1, DIR_DELIM); i += std::string(DIR_DELIM).size() - 1; // generally a no-op } } #ifdef _WIN32 if (path[0] == '\\') path = "C:" + path; #endif return path; } void TestFilePath::testIsDirDelimiter() { UASSERT(fs::IsDirDelimiter('/') == true); UASSERT(fs::IsDirDelimiter('A') == false); UASSERT(fs::IsDirDelimiter(0) == false); #ifdef _WIN32 UASSERT(fs::IsDirDelimiter('\\') == true); #else UASSERT(fs::IsDirDelimiter('\\') == false); #endif } void TestFilePath::testPathStartsWith() { const int numpaths = 12; std::string paths[numpaths] = { "", p("/"), p("/home/user/minetest"), p("/home/user/minetest/bin"), p("/home/user/.minetest"),