From 559dd9946988cd35a7c26bcafe7d0f8c42dc547a Mon Sep 17 00:00:00 2001 From: Ekdohibs Date: Mon, 21 Mar 2016 12:58:52 +0100 Subject: Make node timers more efficient --- src/environment.cpp | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/environment.cpp b/src/environment.cpp index 413bc7ff1..eea264699 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1030,17 +1030,17 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime) m_lbm_mgr.applyLBMs(this, block, stamp); // Run node timers - std::map elapsed_timers = + std::vector elapsed_timers = block->m_node_timers.step((float)dtime_s); - if(!elapsed_timers.empty()){ + if (!elapsed_timers.empty()) { MapNode n; - for(std::map::iterator + for (std::vector::iterator i = elapsed_timers.begin(); i != elapsed_timers.end(); ++i){ - n = block->getNodeNoEx(i->first); - v3s16 p = i->first + block->getPosRelative(); - if(m_script->node_on_timer(p,n,i->second.elapsed)) - block->setNodeTimer(i->first,NodeTimer(i->second.timeout,0)); + n = block->getNodeNoEx(i->position); + v3s16 p = i->position + block->getPosRelative(); + if (m_script->node_on_timer(p, n, i->elapsed)) + block->setNodeTimer(NodeTimer(i->timeout, 0, i->position)); } } @@ -1434,17 +1434,19 @@ void ServerEnvironment::step(float dtime) MOD_REASON_BLOCK_EXPIRED); // Run node timers - std::map elapsed_timers = + std::vector elapsed_timers = block->m_node_timers.step((float)dtime); - if(!elapsed_timers.empty()){ + if (!elapsed_timers.empty()) { MapNode n; - for(std::map::iterator + for (std::vector::iterator i = elapsed_timers.begin(); - i != elapsed_timers.end(); ++i){ - n = block->getNodeNoEx(i->first); - p = i->first + block->getPosRelative(); - if(m_script->node_on_timer(p,n,i->second.elapsed)) - block->setNodeTimer(i->first,NodeTimer(i->second.timeout,0)); + i != elapsed_timers.end(); ++i) { + n = block->getNodeNoEx(i->position); + p = i->position + block->getPosRelative(); + if (m_script->node_on_timer(p, n, i->elapsed)) { + block->setNodeTimer(NodeTimer( + i->timeout, 0, i->position)); + } } } } -- cgit v1.2.3 From 613797a3048907275ceebe29582b9fc2761b1f25 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Oct 2016 09:03:55 +0200 Subject: Replace various std::map with UNORDERED_MAP + various cleanups This is part 2 for 5f084cd98d7b3326b51320455364337539710efd Other improvements: * Use the defined ItemGroupList when used * make Client::checkPrivilege const * inline some trivial functions * Add ActiveObjectMap typedef * Add SettingsEntries typedef --- src/client.cpp | 4 +-- src/client.h | 6 ++--- src/clientiface.cpp | 55 ++++++++++++++----------------------- src/clientiface.h | 7 +++-- src/content_cao.cpp | 6 ++--- src/content_cao.h | 2 +- src/emerge.cpp | 6 ++--- src/emerge.h | 2 +- src/environment.cpp | 60 ++++++++++++++--------------------------- src/environment.h | 8 +++--- src/game.cpp | 9 +++---- src/itemdef.cpp | 4 +-- src/itemgroup.h | 6 ++--- src/mapsector.cpp | 28 +++++-------------- src/mapsector.h | 4 +-- src/mg_schematic.cpp | 4 +-- src/script/common/c_content.cpp | 8 +++--- src/script/common/c_content.h | 6 ++--- src/script/common/c_converter.h | 4 +-- src/script/lua_api/l_util.cpp | 4 +-- src/server.cpp | 12 ++++----- src/settings.cpp | 26 +++++++++--------- src/settings.h | 6 +++-- 23 files changed, 110 insertions(+), 167 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/client.cpp b/src/client.cpp index 483b22caa..a599e21dc 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -303,7 +303,7 @@ Client::~Client() delete m_inventory_from_server; // Delete detached inventories - for (std::map::iterator + for (UNORDERED_MAP::iterator i = m_detached_inventories.begin(); i != m_detached_inventories.end(); ++i) { delete i->second; @@ -1437,7 +1437,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::DETACHED: { - if(m_detached_inventories.count(loc.name) == 0) + if (m_detached_inventories.count(loc.name) == 0) return NULL; return m_detached_inventories[loc.name]; } diff --git a/src/client.h b/src/client.h index b479062a0..fb479068f 100644 --- a/src/client.h +++ b/src/client.h @@ -462,7 +462,7 @@ public: u16 getHP(); u16 getBreath(); - bool checkPrivilege(const std::string &priv) + bool checkPrivilege(const std::string &priv) const { return (m_privileges.count(priv) != 0); } bool getChatMessage(std::wstring &message); @@ -670,11 +670,11 @@ private: std::map m_sounds_to_objects; // Privileges - std::set m_privileges; + UNORDERED_SET m_privileges; // Detached inventories // key = name - std::map m_detached_inventories; + UNORDERED_MAP m_detached_inventories; // Storage for mesh data for creating multiple instances of the same mesh StringMap m_mesh_data; diff --git a/src/clientiface.cpp b/src/clientiface.cpp index a3a17d435..e7ad39579 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -605,11 +605,8 @@ ClientInterface::~ClientInterface() { MutexAutoLock clientslock(m_clients_mutex); - for(std::map::iterator - i = m_clients.begin(); - i != m_clients.end(); ++i) - { - + for (UNORDERED_MAP::iterator i = m_clients.begin(); + i != m_clients.end(); ++i) { // Delete client delete i->second; } @@ -621,10 +618,8 @@ std::vector ClientInterface::getClientIDs(ClientState min_state) std::vector reply; MutexAutoLock clientslock(m_clients_mutex); - for(std::map::iterator - i = m_clients.begin(); - i != m_clients.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_clients.begin(); + i != m_clients.end(); ++i) { if (i->second->getState() >= min_state) reply.push_back(i->second->peer_id); } @@ -691,8 +686,7 @@ void ClientInterface::sendToAll(u16 channelnum, NetworkPacket* pkt, bool reliable) { MutexAutoLock clientslock(m_clients_mutex); - for(std::map::iterator - i = m_clients.begin(); + for(UNORDERED_MAP::iterator i = m_clients.begin(); i != m_clients.end(); ++i) { RemoteClient *client = i->second; @@ -705,11 +699,10 @@ void ClientInterface::sendToAll(u16 channelnum, RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min) { MutexAutoLock clientslock(m_clients_mutex); - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return NULL; if (n->second->getState() >= state_min) @@ -720,11 +713,10 @@ RemoteClient* ClientInterface::getClientNoEx(u16 peer_id, ClientState state_min) RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState state_min) { - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return NULL; if (n->second->getState() >= state_min) @@ -736,11 +728,10 @@ RemoteClient* ClientInterface::lockedGetClientNoEx(u16 peer_id, ClientState stat ClientState ClientInterface::getClientState(u16 peer_id) { MutexAutoLock clientslock(m_clients_mutex); - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return CS_Invalid; return n->second->getState(); @@ -749,11 +740,10 @@ ClientState ClientInterface::getClientState(u16 peer_id) void ClientInterface::setPlayerName(u16 peer_id,std::string name) { MutexAutoLock clientslock(m_clients_mutex); - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n != m_clients.end()) + if (n != m_clients.end()) n->second->setName(name); } @@ -762,11 +752,10 @@ void ClientInterface::DeleteClient(u16 peer_id) MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client may not exist; clients are immediately removed if their // access is denied, and this event occurs later then. - if(n == m_clients.end()) + if (n == m_clients.end()) return; /* @@ -797,10 +786,9 @@ void ClientInterface::CreateClient(u16 peer_id) MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // The client shouldn't already exist - if(n != m_clients.end()) return; + if (n != m_clients.end()) return; // Create client RemoteClient *client = new RemoteClient(); @@ -814,8 +802,7 @@ void ClientInterface::event(u16 peer_id, ClientStateEvent event) MutexAutoLock clientlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // No client to deliver event if (n == m_clients.end()) @@ -836,8 +823,7 @@ u16 ClientInterface::getProtocolVersion(u16 peer_id) MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // No client to get version if (n == m_clients.end()) @@ -851,8 +837,7 @@ void ClientInterface::setClientVersion(u16 peer_id, u8 major, u8 minor, u8 patch MutexAutoLock conlock(m_clients_mutex); // Error check - std::map::iterator n; - n = m_clients.find(peer_id); + UNORDERED_MAP::iterator n = m_clients.find(peer_id); // No client to set versions if (n == m_clients.end()) diff --git a/src/clientiface.h b/src/clientiface.h index c09942909..8985ef71f 100644 --- a/src/clientiface.h +++ b/src/clientiface.h @@ -25,10 +25,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serialization.h" // for SER_FMT_VER_INVALID #include "threading/mutex.h" #include "network/networkpacket.h" +#include "util/cpp11_container.h" #include #include -#include #include class MapBlock; @@ -502,8 +502,7 @@ protected: void lock() { m_clients_mutex.lock(); } void unlock() { m_clients_mutex.unlock(); } - std::map& getClientList() - { return m_clients; } + UNORDERED_MAP& getClientList() { return m_clients; } private: /* update internal player list */ @@ -513,7 +512,7 @@ private: con::Connection* m_con; Mutex m_clients_mutex; // Connected clients (behind the con mutex) - std::map m_clients; + UNORDERED_MAP m_clients; std::vector m_clients_names; //for announcing masterserver // Environment diff --git a/src/content_cao.cpp b/src/content_cao.cpp index f414b2b9b..609422f26 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -1505,10 +1505,8 @@ void GenericCAO::updateBonePosition() return; m_animated_meshnode->setJointMode(irr::scene::EJUOR_CONTROL); // To write positions to the mesh on render - for(std::map >::const_iterator ii = m_bone_position.begin(); - ii != m_bone_position.end(); ++ii) - { + for(UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { std::string bone_name = (*ii).first; v3f bone_pos = (*ii).second.X; v3f bone_rot = (*ii).second.Y; diff --git a/src/content_cao.h b/src/content_cao.h index bf99fd3ba..cf14a1e18 100644 --- a/src/content_cao.h +++ b/src/content_cao.h @@ -90,7 +90,7 @@ private: int m_animation_speed; int m_animation_blend; bool m_animation_loop; - std::map > m_bone_position; // stores position and rotation for each bone name + UNORDERED_MAP > m_bone_position; // stores position and rotation for each bone name std::string m_attachment_bone; v3f m_attachment_position; v3f m_attachment_rotation; diff --git a/src/emerge.cpp b/src/emerge.cpp index daf42f5e2..bdb5e0729 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -369,12 +369,10 @@ bool EmergeManager::pushBlockEmergeData( } -bool EmergeManager::popBlockEmergeData( - v3s16 pos, - BlockEmergeData *bedata) +bool EmergeManager::popBlockEmergeData(v3s16 pos, BlockEmergeData *bedata) { std::map::iterator it; - std::map::iterator it2; + UNORDERED_MAP::iterator it2; it = m_blocks_enqueued.find(pos); if (it == m_blocks_enqueued.end()) diff --git a/src/emerge.h b/src/emerge.h index cf0677145..71ad97da3 100644 --- a/src/emerge.h +++ b/src/emerge.h @@ -156,7 +156,7 @@ private: Mutex m_queue_mutex; std::map m_blocks_enqueued; - std::map m_peer_queue_count; + UNORDERED_MAP m_peer_queue_count; u16 m_qlimit_total; u16 m_qlimit_diskonly; diff --git a/src/environment.cpp b/src/environment.cpp index eea264699..34b3c34f4 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1124,14 +1124,12 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n) void ServerEnvironment::getObjectsInsideRadius(std::vector &objects, v3f pos, float radius) { - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (ActiveObjectMap::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; u16 id = i->first; v3f objectpos = obj->getBasePosition(); - if(objectpos.getDistanceFrom(pos) > radius) + if (objectpos.getDistanceFrom(pos) > radius) continue; objects.push_back(id); } @@ -1142,8 +1140,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) infostream << "ServerEnvironment::clearObjects(): " << "Removing all active objects" << std::endl; std::vector objects_to_remove; - for (std::map::iterator - i = m_active_objects.begin(); + for (ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER) @@ -1518,10 +1515,8 @@ void ServerEnvironment::step(float dtime) send_recommended = true; } - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for(ActiveObjectMap::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; // Don't step if is to be removed or stored statically if(obj->m_removed || obj->m_pending_deactivation) @@ -1554,7 +1549,7 @@ void ServerEnvironment::step(float dtime) Manage particle spawner expiration */ if (m_particle_management_interval.step(dtime, 1.0)) { - for (std::map::iterator i = m_particle_spawners.begin(); + for (UNORDERED_MAP::iterator i = m_particle_spawners.begin(); i != m_particle_spawners.end(); ) { //non expiring spawners if (i->second == PARTICLE_SPAWNER_NO_EXPIRY) { @@ -1579,8 +1574,7 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) u32 id = 0; for (;;) { // look for unused particlespawner id id++; - std::map::iterator f; - f = m_particle_spawners.find(id); + UNORDERED_MAP::iterator f = m_particle_spawners.find(id); if (f == m_particle_spawners.end()) { m_particle_spawners[id] = time; break; @@ -1589,31 +1583,21 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) return id; } -void ServerEnvironment::deleteParticleSpawner(u32 id) -{ - m_particle_spawners.erase(id); -} - ServerActiveObject* ServerEnvironment::getActiveObject(u16 id) { - std::map::iterator n; - n = m_active_objects.find(id); - if(n == m_active_objects.end()) - return NULL; - return n->second; + ActiveObjectMap::iterator n = m_active_objects.find(id); + return (n != m_active_objects.end() ? n->second : NULL); } -bool isFreeServerActiveObjectId(u16 id, - std::map &objects) +bool isFreeServerActiveObjectId(u16 id, ActiveObjectMap &objects) { - if(id == 0) + if (id == 0) return false; return objects.find(id) == objects.end(); } -u16 getFreeServerActiveObjectId( - std::map &objects) +u16 getFreeServerActiveObjectId(ActiveObjectMap &objects) { //try to reuse id's as late as possible static u16 last_used_id = 0; @@ -1659,8 +1643,7 @@ void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, - discard objects that are found in current_objects. - add remaining objects to added_objects */ - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; @@ -1756,8 +1739,7 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock( so_it = block->m_static_objects.m_active.begin(); so_it != block->m_static_objects.m_active.end(); ++so_it) { // Get the ServerActiveObject counterpart to this StaticObject - std::map::iterator ao_it; - ao_it = m_active_objects.find(so_it->first); + ActiveObjectMap::iterator ao_it = m_active_objects.find(so_it->first); if (ao_it == m_active_objects.end()) { // If this ever happens, there must be some kind of nasty bug. errorstream << "ServerEnvironment::setStaticForObjectsInBlock(): " @@ -1806,8 +1788,8 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, verbosestream<<"ServerEnvironment::addActiveObjectRaw(): " <<"supplied with id "<getId()<getId(), m_active_objects) == false) - { + + if(!isFreeServerActiveObjectId(object->getId(), m_active_objects)) { errorstream<<"ServerEnvironment::addActiveObjectRaw(): " <<"id is not free ("<getId()<<")"<environmentDeletes()) @@ -1875,8 +1857,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, void ServerEnvironment::removeRemovedObjects() { std::vector objects_to_remove; - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; ServerActiveObject* obj = i->second; @@ -1894,7 +1875,7 @@ void ServerEnvironment::removeRemovedObjects() We will delete objects that are marked as removed or thatare waiting for deletion after deactivation */ - if(obj->m_removed == false && obj->m_pending_deactivation == false) + if (!obj->m_removed && !obj->m_pending_deactivation) continue; /* @@ -2094,8 +2075,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) void ServerEnvironment::deactivateFarObjects(bool force_delete) { std::vector objects_to_remove; - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; assert(obj); diff --git a/src/environment.h b/src/environment.h index c6786faed..1ba7b196f 100644 --- a/src/environment.h +++ b/src/environment.h @@ -300,6 +300,8 @@ enum ClearObjectsMode { This is not thread-safe. Server uses an environment mutex. */ +typedef UNORDERED_MAP ActiveObjectMap; + class ServerEnvironment : public Environment { public: @@ -338,7 +340,7 @@ public: void loadDefaultMeta(); u32 addParticleSpawner(float exptime); - void deleteParticleSpawner(u32 id); + void deleteParticleSpawner(u32 id) { m_particle_spawners.erase(id); } /* External ActiveObject interface @@ -491,7 +493,7 @@ private: // World path const std::string m_path_world; // Active object list - std::map m_active_objects; + ActiveObjectMap m_active_objects; // Outgoing network message buffer for active objects std::queue m_active_object_messages; // Some timers @@ -522,7 +524,7 @@ private: // Particles IntervalLimiter m_particle_management_interval; - std::map m_particle_spawners; + UNORDERED_MAP m_particle_spawners; }; #ifndef SERVER diff --git a/src/game.cpp b/src/game.cpp index 5a3b10879..22d9ffef6 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -605,7 +605,7 @@ public: void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver, gui::IGUIFont *font) const { - std::map m_meta; + UNORDERED_MAP m_meta; for (std::deque::const_iterator k = m_log.begin(); k != m_log.end(); ++k) { @@ -615,8 +615,7 @@ public: i != piece.values.end(); ++i) { const std::string &id = i->first; const float &value = i->second; - std::map::iterator j = - m_meta.find(id); + UNORDERED_MAP::iterator j = m_meta.find(id); if (j == m_meta.end()) { m_meta[id] = Meta(value); @@ -643,7 +642,7 @@ public: sizeof(usable_colors) / sizeof(*usable_colors); u32 next_color_i = 0; - for (std::map::iterator i = m_meta.begin(); + for (UNORDERED_MAP::iterator i = m_meta.begin(); i != m_meta.end(); ++i) { Meta &meta = i->second; video::SColor color(255, 200, 200, 200); @@ -659,7 +658,7 @@ public: s32 textx2 = textx + 200 - 15; s32 meta_i = 0; - for (std::map::const_iterator i = m_meta.begin(); + for (UNORDERED_MAP::const_iterator i = m_meta.begin(); i != m_meta.end(); ++i) { const std::string &id = i->first; const Meta &meta = i->second; diff --git a/src/itemdef.cpp b/src/itemdef.cpp index a6c627a03..1aa6331dc 100644 --- a/src/itemdef.cpp +++ b/src/itemdef.cpp @@ -146,9 +146,9 @@ void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const } os<::const_iterator + for (ItemGroupList::const_iterator i = groups.begin(); i != groups.end(); ++i){ - os<first); + os << serializeString(i->first); writeS16(os, i->second); } os< -#include +#include "util/cpp11_container.h" -typedef std::map ItemGroupList; +typedef UNORDERED_MAP ItemGroupList; static inline int itemgroup_get(const ItemGroupList &groups, const std::string &name) { - std::map::const_iterator i = groups.find(name); + ItemGroupList::const_iterator i = groups.find(name); if(i == groups.end()) return 0; return i->second; diff --git a/src/mapsector.cpp b/src/mapsector.cpp index 1588a5962..410689f5e 100644 --- a/src/mapsector.cpp +++ b/src/mapsector.cpp @@ -42,9 +42,8 @@ void MapSector::deleteBlocks() m_block_cache = NULL; // Delete all - for(std::map::iterator i = m_blocks.begin(); - i != m_blocks.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_blocks.begin(); + i != m_blocks.end(); ++i) { delete i->second; } @@ -56,20 +55,13 @@ MapBlock * MapSector::getBlockBuffered(s16 y) { MapBlock *block; - if(m_block_cache != NULL && y == m_block_cache_y){ + if (m_block_cache != NULL && y == m_block_cache_y) { return m_block_cache; } // If block doesn't exist, return NULL - std::map::iterator n = m_blocks.find(y); - if(n == m_blocks.end()) - { - block = NULL; - } - // If block exists, return it - else{ - block = n->second; - } + UNORDERED_MAP::iterator n = m_blocks.find(y); + block = (n != m_blocks.end() ? n->second : NULL); // Cache the last result m_block_cache_y = y; @@ -135,18 +127,12 @@ void MapSector::deleteBlock(MapBlock *block) void MapSector::getBlocks(MapBlockVect &dest) { - for(std::map::iterator bi = m_blocks.begin(); - bi != m_blocks.end(); ++bi) - { + for (UNORDERED_MAP::iterator bi = m_blocks.begin(); + bi != m_blocks.end(); ++bi) { dest.push_back(bi->second); } } -bool MapSector::empty() -{ - return m_blocks.empty(); -} - /* ServerMapSector */ diff --git a/src/mapsector.h b/src/mapsector.h index 4c1ce86a3..c3bff3575 100644 --- a/src/mapsector.h +++ b/src/mapsector.h @@ -63,7 +63,7 @@ public: void getBlocks(MapBlockVect &dest); - bool empty(); + bool empty() const { return m_blocks.empty(); } // Always false at the moment, because sector contains no metadata. bool differs_from_disk; @@ -71,7 +71,7 @@ public: protected: // The pile of MapBlocks - std::map m_blocks; + UNORDERED_MAP m_blocks; Map *m_parent; // Position on parent (in MapBlock widths) diff --git a/src/mg_schematic.cpp b/src/mg_schematic.cpp index 0b95fa267..e028215dc 100644 --- a/src/mg_schematic.cpp +++ b/src/mg_schematic.cpp @@ -564,14 +564,14 @@ void Schematic::applyProbabilities(v3s16 p0, void generate_nodelist_and_update_ids(MapNode *nodes, size_t nodecount, std::vector *usednodes, INodeDefManager *ndef) { - std::map nodeidmap; + UNORDERED_MAP nodeidmap; content_t numids = 0; for (size_t i = 0; i != nodecount; i++) { content_t id; content_t c = nodes[i].getContent(); - std::map::const_iterator it = nodeidmap.find(c); + UNORDERED_MAP::const_iterator it = nodeidmap.find(c); if (it == nodeidmap.end()) { id = numids; numids++; diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 6fb9080bc..f20a65903 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -1063,8 +1063,7 @@ void push_flags_string(lua_State *L, FlagDesc *flagdesc, u32 flags, u32 flagmask /******************************************************************************/ /******************************************************************************/ -void read_groups(lua_State *L, int index, - std::map &result) +void read_groups(lua_State *L, int index, ItemGroupList &result) { if (!lua_istable(L,index)) return; @@ -1083,11 +1082,10 @@ void read_groups(lua_State *L, int index, } /******************************************************************************/ -void push_groups(lua_State *L, const std::map &groups) +void push_groups(lua_State *L, const ItemGroupList &groups) { lua_newtable(L); - std::map::const_iterator it; - for (it = groups.begin(); it != groups.end(); ++it) { + for (ItemGroupList::const_iterator it = groups.begin(); it != groups.end(); ++it) { lua_pushnumber(L, it->second); lua_setfield(L, -2, it->first.c_str()); } diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index 46416ad8e..2a2228b6d 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -33,11 +33,11 @@ extern "C" { } #include -#include #include #include "irrlichttypes_bloated.h" #include "util/string.h" +#include "itemgroup.h" namespace Json { class Value; } @@ -106,10 +106,10 @@ void pushnode (lua_State *L, const MapNode &n, NodeBox read_nodebox (lua_State *L, int index); void read_groups (lua_State *L, int index, - std::map &result); + ItemGroupList &result); void push_groups (lua_State *L, - const std::map &groups); + const ItemGroupList &groups); //TODO rename to "read_enum_field" int getenumfield (lua_State *L, int table, diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index eefac0ed7..a5fbee765 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define C_CONVERTER_H_ #include -#include +#include "util/cpp11_container.h" #include "irrlichttypes_bloated.h" #include "common/c_types.h" @@ -60,7 +60,7 @@ bool getintfield(lua_State *L, int table, bool getintfield(lua_State *L, int table, const char *fieldname, u32 &result); void read_groups(lua_State *L, int index, - std::map &result); + UNORDERED_MAP &result); bool getboolfield(lua_State *L, int table, const char *fieldname, bool &result); bool getfloatfield(lua_State *L, int table, diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 13c0d702f..fa2d15b03 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -220,7 +220,7 @@ int ModApiUtil::l_write_json(lua_State *L) int ModApiUtil::l_get_dig_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; - std::map groups; + ItemGroupList groups; read_groups(L, 1, groups); ToolCapabilities tp = read_tool_capabilities(L, 2); if(lua_isnoneornil(L, 3)) @@ -235,7 +235,7 @@ int ModApiUtil::l_get_dig_params(lua_State *L) int ModApiUtil::l_get_hit_params(lua_State *L) { NO_MAP_LOCK_REQUIRED; - std::map groups; + UNORDERED_MAP groups; read_groups(L, 1, groups); ToolCapabilities tp = read_tool_capabilities(L, 2); if(lua_isnoneornil(L, 3)) diff --git a/src/server.cpp b/src/server.cpp index c615aee13..5b67b321a 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -669,7 +669,7 @@ void Server::AsyncRunStep(bool initial_step) MutexAutoLock envlock(m_env_mutex); m_clients.lock(); - std::map clients = m_clients.getClientList(); + UNORDERED_MAP clients = m_clients.getClientList(); ScopeProfiler sp(g_profiler, "Server: checking added and deleted objs"); // Radius inside which objects are active @@ -685,8 +685,7 @@ void Server::AsyncRunStep(bool initial_step) if (player_radius == 0 && is_transfer_limited) player_radius = radius; - for (std::map::iterator - i = clients.begin(); + for (UNORDERED_MAP::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = i->second; @@ -696,7 +695,7 @@ void Server::AsyncRunStep(bool initial_step) continue; Player *player = m_env->getPlayer(client->peer_id); - if(player == NULL) { + if (player == NULL) { // This can happen if the client timeouts somehow /*warningstream<peer_id @@ -817,10 +816,9 @@ void Server::AsyncRunStep(bool initial_step) } m_clients.lock(); - std::map clients = m_clients.getClientList(); + UNORDERED_MAP clients = m_clients.getClientList(); // Route data to every client - for (std::map::iterator - i = clients.begin(); + for (UNORDERED_MAP::iterator i = clients.begin(); i != clients.end(); ++i) { RemoteClient *client = i->second; std::string reliable_data; diff --git a/src/settings.cpp b/src/settings.cpp index 91ea9c58b..c4c3c9073 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -196,9 +196,8 @@ void Settings::writeLines(std::ostream &os, u32 tab_depth) const { MutexAutoLock lock(m_mutex); - for (std::map::const_iterator - it = m_settings.begin(); - it != m_settings.end(); ++it) + for (SettingEntries::const_iterator it = m_settings.begin(); + it != m_settings.end(); ++it) printEntry(os, it->first, it->second, tab_depth); } @@ -231,7 +230,7 @@ void Settings::printEntry(std::ostream &os, const std::string &name, bool Settings::updateConfigObject(std::istream &is, std::ostream &os, const std::string &end, u32 tab_depth) { - std::map::const_iterator it; + SettingEntries::const_iterator it; std::set present_entries; std::string line, name, value; bool was_modified = false; @@ -381,7 +380,7 @@ const SettingsEntry &Settings::getEntry(const std::string &name) const { MutexAutoLock lock(m_mutex); - std::map::const_iterator n; + SettingEntries::const_iterator n; if ((n = m_settings.find(name)) == m_settings.end()) { if ((n = m_defaults.find(name)) == m_defaults.end()) throw SettingNotFoundException("Setting [" + name + "] not found."); @@ -572,9 +571,8 @@ bool Settings::exists(const std::string &name) const std::vector Settings::getNames() const { std::vector names; - for (std::map::const_iterator - i = m_settings.begin(); - i != m_settings.end(); ++i) { + for (SettingEntries::const_iterator i = m_settings.begin(); + i != m_settings.end(); ++i) { names.push_back(i->first); } return names; @@ -880,7 +878,7 @@ bool Settings::remove(const std::string &name) { MutexAutoLock lock(m_mutex); - std::map::iterator it = m_settings.find(name); + SettingEntries::iterator it = m_settings.find(name); if (it != m_settings.end()) { delete it->second.group; m_settings.erase(it); @@ -912,7 +910,6 @@ void Settings::updateValue(const Settings &other, const std::string &name) try { std::string val = other.get(name); - m_settings[name] = val; } catch (SettingNotFoundException &e) { } @@ -968,8 +965,9 @@ void Settings::updateNoLock(const Settings &other) void Settings::clearNoLock() { - std::map::const_iterator it; - for (it = m_settings.begin(); it != m_settings.end(); ++it) + + for (SettingEntries::const_iterator it = m_settings.begin(); + it != m_settings.end(); ++it) delete it->second.group; m_settings.clear(); @@ -978,8 +976,8 @@ void Settings::clearNoLock() void Settings::clearDefaultsNoLock() { - std::map::const_iterator it; - for (it = m_defaults.begin(); it != m_defaults.end(); ++it) + for (SettingEntries::const_iterator it = m_defaults.begin(); + it != m_defaults.end(); ++it) delete it->second.group; m_defaults.clear(); } diff --git a/src/settings.h b/src/settings.h index c6c044779..b19733514 100644 --- a/src/settings.h +++ b/src/settings.h @@ -98,6 +98,8 @@ struct SettingsEntry { bool is_group; }; +typedef UNORDERED_MAP SettingEntries; + class Settings { public: Settings() {} @@ -231,8 +233,8 @@ private: void doCallbacks(const std::string &name) const; - std::map m_settings; - std::map m_defaults; + SettingEntries m_settings; + SettingEntries m_defaults; SettingsCallbackMap m_callbacks; -- cgit v1.2.3 From 656faf7373587bc59b47986a28dbd2fce4c45474 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 12:21:41 +0200 Subject: Player/LocalPlayer/RemotePlayer inheritance cleanup (part 2 on X) * Server/Client Environments now have an helper to cast Player object in the right type to use it * Server: use RemotePlayer everywhere and remove previous added casts * Client: use LocalPlayer where needed * Environment: remove unused functions (getPlayers(), getRandomConnectedPlayer(), getNearestConnectedPlayer()) --- src/client.cpp | 2 +- src/environment.cpp | 84 +++++++++---------------------------- src/environment.h | 18 ++++---- src/network/serverpackethandler.cpp | 24 ++++------- src/script/lua_api/l_inventory.cpp | 2 +- src/script/lua_api/l_inventory.h | 9 +--- src/script/lua_api/l_object.cpp | 20 ++++----- src/script/lua_api/l_object.h | 1 - src/script/lua_api/l_server.cpp | 21 +++++----- src/server.cpp | 26 ++++++------ src/server.h | 21 +++++----- src/serverobject.h | 25 ++++++----- 12 files changed, 97 insertions(+), 156 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/client.cpp b/src/client.cpp index 63653998a..392dabde6 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -1418,7 +1418,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::PLAYER: { - Player *player = m_env.getPlayer(loc.name.c_str()); + LocalPlayer *player = m_env.getPlayer(loc.name.c_str()); if(!player) return NULL; return &player->inventory; diff --git a/src/environment.cpp b/src/environment.cpp index 34b3c34f4..d30b70527 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -127,65 +127,6 @@ Player * Environment::getPlayer(const char *name) return NULL; } -Player * Environment::getRandomConnectedPlayer() -{ - std::vector connected_players = getPlayers(true); - u32 chosen_one = myrand() % connected_players.size(); - u32 j = 0; - for(std::vector::iterator - i = connected_players.begin(); - i != connected_players.end(); ++i) { - if(j == chosen_one) { - Player *player = *i; - return player; - } - j++; - } - return NULL; -} - -Player * Environment::getNearestConnectedPlayer(v3f pos) -{ - std::vector connected_players = getPlayers(true); - f32 nearest_d = 0; - Player *nearest_player = NULL; - for(std::vector::iterator - i = connected_players.begin(); - i != connected_players.end(); ++i) { - Player *player = *i; - f32 d = player->getPosition().getDistanceFrom(pos); - if(d < nearest_d || nearest_player == NULL) { - nearest_d = d; - nearest_player = player; - } - } - return nearest_player; -} - -std::vector Environment::getPlayers() -{ - return m_players; -} - -std::vector Environment::getPlayers(bool ignore_disconnected) -{ - std::vector newlist; - for(std::vector::iterator - i = m_players.begin(); - i != m_players.end(); ++i) { - Player *player = *i; - - if(ignore_disconnected) { - // Ignore disconnected players - if(player->peer_id == 0) - continue; - } - - newlist.push_back(player); - } - return newlist; -} - u32 Environment::getDayNightRatio() { MutexAutoLock lock(this->m_time_lock); @@ -199,11 +140,6 @@ void Environment::setTimeOfDaySpeed(float speed) m_time_of_day_speed = speed; } -float Environment::getTimeOfDaySpeed() -{ - return m_time_of_day_speed; -} - void Environment::setDayNightRatioOverride(bool enable, u32 value) { MutexAutoLock lock(this->m_time_lock); @@ -625,6 +561,16 @@ ServerMap & ServerEnvironment::getServerMap() return *m_map; } +RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id) +{ + return dynamic_cast(Environment::getPlayer(peer_id)); +} + +RemotePlayer *ServerEnvironment::getPlayer(const char* name) +{ + return dynamic_cast(Environment::getPlayer(name)); +} + bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p) { float distance = pos1.getDistanceFrom(pos2); @@ -2349,6 +2295,16 @@ ClientMap & ClientEnvironment::getClientMap() return *m_map; } +LocalPlayer *ClientEnvironment::getPlayer(const u16 peer_id) +{ + return dynamic_cast(Environment::getPlayer(peer_id)); +} + +LocalPlayer *ClientEnvironment::getPlayer(const char* name) +{ + return dynamic_cast(Environment::getPlayer(name)); +} + void ClientEnvironment::addPlayer(Player *player) { DSTACK(FUNCTION_NAME); diff --git a/src/environment.h b/src/environment.h index 1ba7b196f..66d9c19c0 100644 --- a/src/environment.h +++ b/src/environment.h @@ -74,12 +74,6 @@ public: virtual void addPlayer(Player *player); void removePlayer(Player *player); - Player * getPlayer(u16 peer_id); - Player * getPlayer(const char *name); - Player * getRandomConnectedPlayer(); - Player * getNearestConnectedPlayer(v3f pos); - std::vector getPlayers(); - std::vector getPlayers(bool ignore_disconnected); u32 getDayNightRatio(); @@ -91,7 +85,6 @@ public: void stepTimeOfDay(float dtime); void setTimeOfDaySpeed(float speed); - float getTimeOfDaySpeed(); void setDayNightRatioOverride(bool enable, u32 value); @@ -101,6 +94,9 @@ public: u32 m_added_objects; protected: + Player * getPlayer(u16 peer_id); + Player * getPlayer(const char *name); + // peer_ids in here should be unique, except that there may be many 0s std::vector m_players; @@ -440,6 +436,8 @@ public: void setStaticForActiveObjectsInBlock(v3s16 blockpos, bool static_exists, v3s16 static_block=v3s16(0,0,0)); + RemotePlayer *getPlayer(const u16 peer_id); + RemotePlayer *getPlayer(const char* name); private: /* @@ -640,8 +638,10 @@ public: { m_player_names.remove(name); } void updateCameraOffset(v3s16 camera_offset) { m_camera_offset = camera_offset; } - v3s16 getCameraOffset() - { return m_camera_offset; } + v3s16 getCameraOffset() const { return m_camera_offset; } + + LocalPlayer *getPlayer(const u16 peer_id); + LocalPlayer *getPlayer(const char* name); private: ClientMap *m_map; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 3fba7f720..6ef768295 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -800,8 +800,7 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) pitch = modulo360f(pitch); yaw = modulo360f(yaw); - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -880,8 +879,7 @@ void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt) void Server::handleCommand_InventoryAction(NetworkPacket* pkt) { - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1081,8 +1079,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt) *pkt >> damage; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1117,8 +1114,7 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) *pkt >> breath; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1231,8 +1227,7 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt) if (pkt->getSize() < 2) return; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1308,8 +1303,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) verbosestream << "TOSERVER_INTERACT: action=" << (int)action << ", item=" << item_i << ", pointed=" << pointed.dump() << std::endl; - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1730,8 +1724,7 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt) fields[fieldname] = pkt->readLongString(); } - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -1782,8 +1775,7 @@ void Server::handleCommand_InventoryFields(NetworkPacket* pkt) fields[fieldname] = pkt->readLongString(); } - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(pkt->getPeerId())); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index de9f9374a..110e68d23 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -420,7 +420,7 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc) luaL_getmetatable(L, className); lua_setmetatable(L, -2); } -void InvRef::createPlayer(lua_State *L, Player *player) +void InvRef::createPlayer(lua_State *L, RemotePlayer *player) { NO_MAP_LOCK_REQUIRED; InventoryLocation loc; diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h index 2d4b29d0c..cc5333965 100644 --- a/src/script/lua_api/l_inventory.h +++ b/src/script/lua_api/l_inventory.h @@ -25,7 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "inventory.h" #include "inventorymanager.h" -class Player; +class RemotePlayer; /* InvRef @@ -112,7 +112,7 @@ public: // Creates an InvRef and leaves it on top of stack // Not callable from Lua; all references are created on the C side. static void create(lua_State *L, const InventoryLocation &loc); - static void createPlayer(lua_State *L, Player *player); + static void createPlayer(lua_State *L, RemotePlayer *player); static void createNodeMeta(lua_State *L, v3s16 p); static void Register(lua_State *L); }; @@ -123,11 +123,6 @@ private: static int l_get_inventory(lua_State *L); - static void inventory_set_list_from_lua(Inventory *inv, const char *name, - lua_State *L, int tableindex, int forcesize); - static void inventory_get_list_to_lua(Inventory *inv, const char *name, - lua_State *L); - public: static void Initialize(lua_State *L, int top); }; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 34e175ad0..b58d8e6cd 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -508,7 +508,7 @@ int ObjectRef::l_set_local_animation(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it @@ -554,7 +554,7 @@ int ObjectRef::l_set_eye_offset(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it @@ -1256,7 +1256,7 @@ int ObjectRef::l_hud_add(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1319,7 +1319,7 @@ int ObjectRef::l_hud_remove(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1339,7 +1339,7 @@ int ObjectRef::l_hud_change(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1552,7 +1552,7 @@ int ObjectRef::l_hud_set_hotbar_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1567,7 +1567,7 @@ int ObjectRef::l_hud_get_hotbar_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1581,7 +1581,7 @@ int ObjectRef::l_hud_set_hotbar_selected_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1596,7 +1596,7 @@ int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1610,7 +1610,7 @@ int ObjectRef::l_set_sky(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index a7ac9dff9..dfc1b49d2 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc., class ServerActiveObject; class LuaEntitySAO; class PlayerSAO; -class Player; class RemotePlayer; /* diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 59d3f5c70..95e5da07f 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -106,7 +106,7 @@ int ModApiServer::l_get_player_ip(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char * name = luaL_checkstring(L, 1); - Player *player = getEnv(L)->getPlayer(name); + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); if(player == NULL) { lua_pushnil(L); // no such player @@ -133,9 +133,8 @@ int ModApiServer::l_get_player_information(lua_State *L) NO_MAP_LOCK_REQUIRED; const char * name = luaL_checkstring(L, 1); - Player *player = getEnv(L)->getPlayer(name); - if(player == NULL) - { + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); + if (player == NULL) { lua_pushnil(L); // no such player return 1; } @@ -278,15 +277,15 @@ int ModApiServer::l_ban_player(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char * name = luaL_checkstring(L, 1); - Player *player = getEnv(L)->getPlayer(name); - if(player == NULL) - { + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); + if (player == NULL) { lua_pushboolean(L, false); // no such player return 1; } try { - Address addr = getServer(L)->getPeerAddress(getEnv(L)->getPlayer(name)->peer_id); + Address addr = getServer(L)->getPeerAddress( + dynamic_cast(getEnv(L))->getPlayer(name)->peer_id); std::string ip_str = addr.serializeString(); getServer(L)->setIpBanned(ip_str, name); } @@ -314,9 +313,9 @@ int ModApiServer::l_kick_player(lua_State *L) { message = "Kicked."; } - Player *player = getEnv(L)->getPlayer(name); - if (player == NULL) - { + + RemotePlayer *player = dynamic_cast(getEnv(L))->getPlayer(name); + if (player == NULL) { lua_pushboolean(L, false); // No such player return 1; } diff --git a/src/server.cpp b/src/server.cpp index 540d23b9d..dee8a3d70 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2638,7 +2638,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) ++i; } - RemotePlayer *player = dynamic_cast(m_env->getPlayer(peer_id)); + RemotePlayer *player = m_env->getPlayer(peer_id); /* Run scripts and remove from environment */ if(player != NULL) { @@ -2850,7 +2850,7 @@ std::string Server::getPlayerName(u16 peer_id) PlayerSAO* Server::getPlayerSAO(u16 peer_id) { - RemotePlayer *player = dynamic_cast(m_env->getPlayer(peer_id)); + RemotePlayer *player = m_env->getPlayer(peer_id); if (player == NULL) return NULL; return player->getPlayerSAO(); @@ -2989,7 +2989,7 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, return true; } -u32 Server::hudAdd(Player *player, HudElement *form) +u32 Server::hudAdd(RemotePlayer *player, HudElement *form) { if (!player) return -1; @@ -3001,7 +3001,7 @@ u32 Server::hudAdd(Player *player, HudElement *form) return id; } -bool Server::hudRemove(Player *player, u32 id) { +bool Server::hudRemove(RemotePlayer *player, u32 id) { if (!player) return false; @@ -3016,7 +3016,7 @@ bool Server::hudRemove(Player *player, u32 id) { return true; } -bool Server::hudChange(Player *player, u32 id, HudElementStat stat, void *data) +bool Server::hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *data) { if (!player) return false; @@ -3058,7 +3058,7 @@ bool Server::hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount) return true; } -void Server::hudSetHotbarImage(Player *player, std::string name) +void Server::hudSetHotbarImage(RemotePlayer *player, std::string name) { if (!player) return; @@ -3067,14 +3067,14 @@ void Server::hudSetHotbarImage(Player *player, std::string name) SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_IMAGE, name); } -std::string Server::hudGetHotbarImage(Player *player) +std::string Server::hudGetHotbarImage(RemotePlayer *player) { if (!player) return ""; return player->getHotbarImage(); } -void Server::hudSetHotbarSelectedImage(Player *player, std::string name) +void Server::hudSetHotbarSelectedImage(RemotePlayer *player, std::string name) { if (!player) return; @@ -3083,7 +3083,7 @@ void Server::hudSetHotbarSelectedImage(Player *player, std::string name) SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name); } -std::string Server::hudGetHotbarSelectedImage(Player *player) +std::string Server::hudGetHotbarSelectedImage(RemotePlayer *player) { if (!player) return ""; @@ -3091,8 +3091,8 @@ std::string Server::hudGetHotbarSelectedImage(Player *player) return player->getHotbarSelectedImage(); } -bool Server::setLocalPlayerAnimations(Player *player, - v2s32 animation_frames[4], f32 frame_speed) +bool Server::setLocalPlayerAnimations(RemotePlayer *player, + v2s32 animation_frames[4], f32 frame_speed) { if (!player) return false; @@ -3102,7 +3102,7 @@ bool Server::setLocalPlayerAnimations(Player *player, return true; } -bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third) +bool Server::setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third) { if (!player) return false; @@ -3113,7 +3113,7 @@ bool Server::setPlayerEyeOffset(Player *player, v3f first, v3f third) return true; } -bool Server::setSky(Player *player, const video::SColor &bgcolor, +bool Server::setSky(RemotePlayer *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms) { if (!player) diff --git a/src/server.h b/src/server.h index 555aab692..8eb1afc9f 100644 --- a/src/server.h +++ b/src/server.h @@ -307,25 +307,26 @@ public: Map & getMap() { return m_env->getMap(); } ServerEnvironment & getEnv() { return *m_env; } - u32 hudAdd(Player *player, HudElement *element); - bool hudRemove(Player *player, u32 id); - bool hudChange(Player *player, u32 id, HudElementStat stat, void *value); + u32 hudAdd(RemotePlayer *player, HudElement *element); + bool hudRemove(RemotePlayer *player, u32 id); + bool hudChange(RemotePlayer *player, u32 id, HudElementStat stat, void *value); bool hudSetFlags(RemotePlayer *player, u32 flags, u32 mask); bool hudSetHotbarItemcount(RemotePlayer *player, s32 hotbar_itemcount); s32 hudGetHotbarItemcount(RemotePlayer *player) const { return player->getHotbarItemcount(); } - void hudSetHotbarImage(Player *player, std::string name); - std::string hudGetHotbarImage(Player *player); - void hudSetHotbarSelectedImage(Player *player, std::string name); - std::string hudGetHotbarSelectedImage(Player *player); + void hudSetHotbarImage(RemotePlayer *player, std::string name); + std::string hudGetHotbarImage(RemotePlayer *player); + void hudSetHotbarSelectedImage(RemotePlayer *player, std::string name); + std::string hudGetHotbarSelectedImage(RemotePlayer *player); inline Address getPeerAddress(u16 peer_id) { return m_con.GetPeerAddress(peer_id); } - bool setLocalPlayerAnimations(Player *player, v2s32 animation_frames[4], f32 frame_speed); - bool setPlayerEyeOffset(Player *player, v3f first, v3f third); + bool setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4], + f32 frame_speed); + bool setPlayerEyeOffset(RemotePlayer *player, v3f first, v3f third); - bool setSky(Player *player, const video::SColor &bgcolor, + bool setSky(RemotePlayer *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); bool overrideDayNightRatio(RemotePlayer *player, bool do_override, float brightness); diff --git a/src/serverobject.h b/src/serverobject.h index 9f8d5403c..cfe2b6bcc 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -44,7 +44,6 @@ Some planning class ServerEnvironment; struct ItemStack; -class Player; struct ToolCapabilities; struct ObjectProperties; @@ -69,23 +68,23 @@ public: // environment virtual bool environmentDeletes() const { return true; } - + // Create a certain type of ServerActiveObject static ServerActiveObject* create(ActiveObjectType type, ServerEnvironment *env, u16 id, v3f pos, const std::string &data); - + /* Some simple getters/setters */ v3f getBasePosition(){ return m_base_position; } void setBasePosition(v3f pos){ m_base_position = pos; } ServerEnvironment* getEnv(){ return m_env; } - + /* Some more dynamic interface */ - + virtual void setPos(v3f pos) { setBasePosition(pos); } // continuous: if true, object does not stop immediately at pos @@ -96,7 +95,7 @@ public: virtual float getMinimumSavedMovement(); virtual std::string getDescription(){return "SAO";} - + /* Step object in time. Messages added to messages are sent to client over network. @@ -108,13 +107,13 @@ public: packet. */ virtual void step(float dtime, bool send_recommended){} - + /* The return value of this is passed to the client-side object when it is created */ virtual std::string getClientInitializationData(u16 protocol_version){return "";} - + /* The return value of this is passed to the server-side object when it is created (converted from static to active - actually @@ -131,7 +130,7 @@ public: */ virtual bool isStaticAllowed() const {return true;} - + // Returns tool wear virtual int punch(v3f dir, const ToolCapabilities *toolcap=NULL, @@ -207,7 +206,7 @@ public: - This can be set to true by anything else too. */ bool m_removed; - + /* This is set to true when an object should be removed from the active object list but couldn't be removed because the id has to be @@ -218,7 +217,7 @@ public: list. */ bool m_pending_deactivation; - + /* Whether the object's static data has been stored to a block */ @@ -228,12 +227,12 @@ public: a copy of the static data resides. */ v3s16 m_static_block; - + /* Queue of messages to be sent to the client */ std::queue m_messages_out; - + protected: // Used for creating objects based on type typedef ServerActiveObject* (*Factory) -- cgit v1.2.3 From fd5a130b86c08f0b3190c3d81affd4869c139fb7 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 16:31:22 +0200 Subject: More code cleanup (UNORDERED + RemotePlayer/LocalPlayer) * ClientEnvironment now uses UNORDERED MAP for active objects * Use RemotePlayer and LocalPlayer everywhere it's possible * Minor code style fixes * Drop Client::getBreath() unused function --- src/client.cpp | 21 +++----- src/client.h | 1 - src/clientiface.cpp | 13 ++--- src/content_cao.cpp | 7 ++- src/environment.cpp | 105 ++++++++++++++++-------------------- src/environment.h | 12 ++--- src/network/clientpackethandler.cpp | 18 +++---- src/network/serverpackethandler.cpp | 6 +-- src/script/lua_api/l_object.cpp | 36 ++++++------- src/server.cpp | 57 ++++++++++---------- 10 files changed, 124 insertions(+), 152 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/client.cpp b/src/client.cpp index 392dabde6..cd010e592 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -383,7 +383,7 @@ void Client::step(float dtime) if(counter <= 0.0) { counter = 2.0; - Player *myplayer = m_env.getLocalPlayer(); + LocalPlayer *myplayer = m_env.getLocalPlayer(); FATAL_ERROR_IF(myplayer == NULL, "Local player not found in environment."); u16 proto_version_min = g_settings->getFlag("send_pre_v25_init") ? @@ -613,7 +613,7 @@ void Client::step(float dtime) { // Do this every seconds after TOCLIENT_INVENTORY // Reset the locally changed inventory to the authoritative inventory - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); player->inventory = *m_inventory_from_server; m_inventory_updated = true; } @@ -1191,7 +1191,7 @@ void Client::sendChatMessage(const std::wstring &message) void Client::sendChangePassword(const std::string &oldpassword, const std::string &newpassword) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); if (player == NULL) return; @@ -1317,7 +1317,7 @@ void Client::sendPlayerPos() void Client::sendPlayerItem(u16 item) { - Player *myplayer = m_env.getLocalPlayer(); + LocalPlayer *myplayer = m_env.getLocalPlayer(); if(myplayer == NULL) return; @@ -1398,7 +1398,7 @@ bool Client::getLocalInventoryUpdated() // Copies the inventory of the local player to parameter void Client::getLocalInventory(Inventory &dst) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); dst = player->inventory; } @@ -1411,7 +1411,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::CURRENT_PLAYER: { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); return &player->inventory; } @@ -1537,18 +1537,11 @@ void Client::setCrack(int level, v3s16 pos) u16 Client::getHP() { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); return player->hp; } -u16 Client::getBreath() -{ - Player *player = m_env.getLocalPlayer(); - assert(player != NULL); - return player->getBreath(); -} - bool Client::getChatMessage(std::wstring &message) { if(m_chat_queue.size() == 0) diff --git a/src/client.h b/src/client.h index a71c1dcbc..72baac2d2 100644 --- a/src/client.h +++ b/src/client.h @@ -460,7 +460,6 @@ public: void setCrack(int level, v3s16 pos); u16 getHP(); - u16 getBreath(); bool checkPrivilege(const std::string &priv) const { return (m_privileges.count(priv) != 0); } diff --git a/src/clientiface.cpp b/src/clientiface.cpp index e7ad39579..e55c07cb6 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -77,9 +77,9 @@ void RemoteClient::GetNextBlocks ( if(m_nothing_to_send_pause_timer >= 0) return; - Player *player = env->getPlayer(peer_id); + RemotePlayer *player = env->getPlayer(peer_id); // This can happen sometimes; clients and players are not in perfect sync. - if(player == NULL) + if (player == NULL) return; // Won't send anything if already sending @@ -645,8 +645,7 @@ void ClientInterface::step(float dtime) void ClientInterface::UpdatePlayerList() { - if (m_env != NULL) - { + if (m_env != NULL) { std::vector clients = getClientIDs(); m_clients_names.clear(); @@ -654,10 +653,8 @@ void ClientInterface::UpdatePlayerList() if(!clients.empty()) infostream<<"Players:"<::iterator - i = clients.begin(); - i != clients.end(); ++i) { - Player *player = m_env->getPlayer(*i); + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { + RemotePlayer *player = m_env->getPlayer(*i); if (player == NULL) continue; diff --git a/src/content_cao.cpp b/src/content_cao.cpp index 207a630d7..a53768149 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -655,12 +655,11 @@ void GenericCAO::initialize(const std::string &data) if(m_is_player) { - Player *player = m_env->getPlayer(m_name.c_str()); - if(player && player->isLocal()) - { + LocalPlayer *player = m_env->getPlayer(m_name.c_str()); + if (player && player->isLocal()) { m_is_local_player = true; m_is_visible = false; - LocalPlayer* localplayer = dynamic_cast(player); + LocalPlayer* localplayer = player; assert( localplayer != NULL ); localplayer->setCAO(this); diff --git a/src/environment.cpp b/src/environment.cpp index d30b70527..bc246f66c 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -105,20 +105,20 @@ void Environment::removePlayer(Player* player) } } -Player * Environment::getPlayer(u16 peer_id) +Player *Environment::getPlayer(u16 peer_id) { - for(std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; - if(player->peer_id == peer_id) + if (player->peer_id == peer_id) return player; } return NULL; } -Player * Environment::getPlayer(const char *name) +Player *Environment::getPlayer(const char *name) { - for(std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; if(strcmp(player->getName(), name) == 0) @@ -602,11 +602,9 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, const std::string &str_reason, bool reconnect) { for (std::vector::iterator it = m_players.begin(); - it != m_players.end(); - ++it) { + it != m_players.end(); ++it) { ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id, - (*it)->protocol_version, (AccessDeniedCode)reason, - str_reason, reconnect); + (*it)->protocol_version, reason, str_reason, reconnect); } } @@ -633,14 +631,14 @@ void ServerEnvironment::savePlayer(RemotePlayer *player) player->save(players_path); } -Player *ServerEnvironment::loadPlayer(const std::string &playername) +RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) { bool newplayer = false; bool found = false; std::string players_path = m_path_world + DIR_DELIM "players" DIR_DELIM; std::string path = players_path + playername; - RemotePlayer *player = static_cast(getPlayer(playername.c_str())); + RemotePlayer *player = getPlayer(playername.c_str()); if (!player) { player = new RemotePlayer(m_gamedef, ""); newplayer = true; @@ -1254,10 +1252,10 @@ void ServerEnvironment::step(float dtime) */ { ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); - for(std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { - Player *player = *i; + for (std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { + RemotePlayer *player = dynamic_cast(*i); + assert(player); // Ignore disconnected players if(player->peer_id == 0) @@ -1277,12 +1275,12 @@ void ServerEnvironment::step(float dtime) Get player block positions */ std::vector players_blockpos; - for(std::vector::iterator - i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { - Player *player = *i; + RemotePlayer *player = dynamic_cast(*i); + assert(player); // Ignore disconnected players - if(player->peer_id == 0) + if (player->peer_id == 0) continue; v3s16 blockpos = getNodeBlockPos( @@ -1381,8 +1379,7 @@ void ServerEnvironment::step(float dtime) block->m_node_timers.step((float)dtime); if (!elapsed_timers.empty()) { MapNode n; - for (std::vector::iterator - i = elapsed_timers.begin(); + for (std::vector::iterator i = elapsed_timers.begin(); i != elapsed_timers.end(); ++i) { n = block->getNodeNoEx(i->position); p = i->position + block->getPosRelative(); @@ -1571,7 +1568,7 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) Finds out what new objects have been added to inside a radius around a position */ -void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, +void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects) @@ -1624,7 +1621,7 @@ void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, Finds out what objects have been removed from inside a radius around a position */ -void ServerEnvironment::getRemovedActiveObjects(Player *player, s16 radius, +void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects) @@ -2269,10 +2266,8 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr, ClientEnvironment::~ClientEnvironment() { // delete active objects - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { delete i->second; } @@ -2312,18 +2307,18 @@ void ClientEnvironment::addPlayer(Player *player) It is a failure if player is local and there already is a local player */ - FATAL_ERROR_IF(player->isLocal() == true && getLocalPlayer() != NULL, - "Player is local but there is already a local player"); + FATAL_ERROR_IF(player->isLocal() && getLocalPlayer() != NULL, + "Player is local but there is already a local player"); Environment::addPlayer(player); } -LocalPlayer * ClientEnvironment::getLocalPlayer() +LocalPlayer *ClientEnvironment::getLocalPlayer() { for(std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; - if(player->isLocal()) + if (player->isLocal()) return (LocalPlayer*)player; } return NULL; @@ -2407,11 +2402,11 @@ void ClientEnvironment::step(float dtime) { // Apply physics - if(free_move == false && is_climbing == false) + if(!free_move && !is_climbing) { // Gravity v3f speed = lplayer->getSpeed(); - if(lplayer->in_liquid == false) + if(!lplayer->in_liquid) speed.Y -= lplayer->movement_gravity * lplayer->physics_override_gravity * dtime_part * 2; // Liquid floating / sinking @@ -2566,14 +2561,15 @@ void ClientEnvironment::step(float dtime) /* Stuff that can be done in an arbitarily large dtime */ - for(std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { - Player *player = *i; + LocalPlayer *player = dynamic_cast(*i); + assert(player); /* Handle non-local players */ - if(player->isLocal() == false) { + if (!player->isLocal()) { // Move player->move(dtime, this, 100*BS); @@ -2604,10 +2600,8 @@ void ClientEnvironment::step(float dtime) g_profiler->avg("CEnv: num of objects", m_active_objects.size()); bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21); - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ClientActiveObject* obj = i->second; // Step object obj->step(dtime, this); @@ -2666,15 +2660,14 @@ GenericCAO* ClientEnvironment::getGenericCAO(u16 id) ClientActiveObject* ClientEnvironment::getActiveObject(u16 id) { - std::map::iterator n; - n = m_active_objects.find(id); - if(n == m_active_objects.end()) + UNORDERED_MAP::iterator n = m_active_objects.find(id); + if (n == m_active_objects.end()) return NULL; return n->second; } -bool isFreeClientActiveObjectId(u16 id, - std::map &objects) +bool isFreeClientActiveObjectId(const u16 id, + UNORDERED_MAP &objects) { if(id == 0) return false; @@ -2682,19 +2675,17 @@ bool isFreeClientActiveObjectId(u16 id, return objects.find(id) == objects.end(); } -u16 getFreeClientActiveObjectId( - std::map &objects) +u16 getFreeClientActiveObjectId(UNORDERED_MAP &objects) { //try to reuse id's as late as possible static u16 last_used_id = 0; u16 startid = last_used_id; - for(;;) - { + for(;;) { last_used_id ++; - if(isFreeClientActiveObjectId(last_used_id, objects)) + if (isFreeClientActiveObjectId(last_used_id, objects)) return last_used_id; - if(last_used_id == startid) + if (last_used_id == startid) return 0; } } @@ -2714,8 +2705,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) } object->setId(new_id); } - if(isFreeClientActiveObjectId(object->getId(), m_active_objects) == false) - { + if(!isFreeClientActiveObjectId(object->getId(), m_active_objects)) { infostream<<"ClientEnvironment::addActiveObject(): " <<"id is not free ("<getId()<<")"< &dest) { - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ClientActiveObject* obj = i->second; f32 d = (obj->getPosition() - origin).getLength(); diff --git a/src/environment.h b/src/environment.h index 66d9c19c0..99066c367 100644 --- a/src/environment.h +++ b/src/environment.h @@ -94,8 +94,8 @@ public: u32 m_added_objects; protected: - Player * getPlayer(u16 peer_id); - Player * getPlayer(const char *name); + Player *getPlayer(u16 peer_id); + Player *getPlayer(const char *name); // peer_ids in here should be unique, except that there may be many 0s std::vector m_players; @@ -324,7 +324,7 @@ public: // Save players void saveLoadedPlayers(); void savePlayer(RemotePlayer *player); - Player *loadPlayer(const std::string &playername); + RemotePlayer *loadPlayer(const std::string &playername); /* Save and load time of day and game timer @@ -368,7 +368,7 @@ public: Find out what new objects have been added to inside a radius around a position */ - void getAddedActiveObjects(Player *player, s16 radius, + void getAddedActiveObjects(RemotePlayer *player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects); @@ -377,7 +377,7 @@ public: Find out what new objects have been removed from inside a radius around a position */ - void getRemovedActiveObjects(Player* player, s16 radius, + void getRemovedActiveObjects(RemotePlayer* player, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects); @@ -649,7 +649,7 @@ private: ITextureSource *m_texturesource; IGameDef *m_gamedef; IrrlichtDevice *m_irr; - std::map m_active_objects; + UNORDERED_MAP m_active_objects; std::vector m_simple_objects; std::queue m_client_event_queue; IntervalLimiter m_active_object_light_update_interval; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 35e350f20..b39356e92 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -110,7 +110,7 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt) playerpos -= v3f(0, BS / 2, 0); // Set player position - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->setPosition(playerpos); @@ -176,7 +176,7 @@ void Client::handleCommand_InitLegacy(NetworkPacket* pkt) // Set player position - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->setPosition(playerpos_f); @@ -333,7 +333,7 @@ void Client::handleCommand_Inventory(NetworkPacket* pkt) std::string datastring(pkt->getString(0), pkt->getSize()); std::istringstream is(datastring, std::ios_base::binary); - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->inventory.deSerialize(is); @@ -486,7 +486,7 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt) void Client::handleCommand_Movement(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g; @@ -511,7 +511,7 @@ void Client::handleCommand_Movement(NetworkPacket* pkt) void Client::handleCommand_HP(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u8 oldhp = player->hp; @@ -532,7 +532,7 @@ void Client::handleCommand_HP(NetworkPacket* pkt) void Client::handleCommand_Breath(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u16 breath; @@ -544,7 +544,7 @@ void Client::handleCommand_Breath(NetworkPacket* pkt) void Client::handleCommand_MovePlayer(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); v3f pos; @@ -840,7 +840,7 @@ void Client::handleCommand_Privileges(NetworkPacket* pkt) void Client::handleCommand_InventoryFormSpec(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); // Store formspec in LocalPlayer @@ -1098,7 +1098,7 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt) *pkt >> flags >> mask; - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 6ef768295..554025747 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -1052,7 +1052,7 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt) message += (wchar_t)tmp_wchar; } - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1179,7 +1179,7 @@ void Server::handleCommand_Password(NetworkPacket* pkt) newpwd += c; } - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() @@ -1255,7 +1255,7 @@ void Server::handleCommand_PlayerItem(NetworkPacket* pkt) void Server::handleCommand_Respawn(NetworkPacket* pkt) { - Player *player = m_env->getPlayer(pkt->getPeerId()); + RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); if (player == NULL) { errorstream << "Server::ProcessData(): Canceling: " "No player for peer_id=" << pkt->getPeerId() diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index b58d8e6cd..74b33da37 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -533,7 +533,7 @@ int ObjectRef::l_get_local_animation(lua_State *L) { NO_MAP_LOCK_REQUIRED ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -584,7 +584,7 @@ int ObjectRef::l_get_eye_offset(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it @@ -762,7 +762,7 @@ int ObjectRef::l_is_player(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); lua_pushboolean(L, (player != NULL)); return 1; } @@ -973,7 +973,7 @@ int ObjectRef::l_is_player_connected(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); lua_pushboolean(L, (player != NULL && player->peer_id != 0)); return 1; } @@ -983,7 +983,7 @@ int ObjectRef::l_get_player_name(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushlstring(L, "", 0); return 1; @@ -998,7 +998,7 @@ int ObjectRef::l_get_player_velocity(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushnil(L); return 1; @@ -1013,7 +1013,7 @@ int ObjectRef::l_get_look_dir(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it float pitch = player->getRadPitchDep(); @@ -1033,7 +1033,7 @@ int ObjectRef::l_get_look_pitch(lua_State *L) "Deprecated call to get_look_pitch, use get_look_vertical instead"); ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadPitchDep()); @@ -1050,7 +1050,7 @@ int ObjectRef::l_get_look_yaw(lua_State *L) "Deprecated call to get_look_yaw, use get_look_horizontal instead"); ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadYawDep()); @@ -1062,7 +1062,7 @@ int ObjectRef::l_get_look_vertical(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadPitch()); @@ -1074,7 +1074,7 @@ int ObjectRef::l_get_look_horizontal(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; // Do it lua_pushnumber(L, player->getRadYaw()); @@ -1179,7 +1179,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; std::string formspec = luaL_checkstring(L, 2); @@ -1194,7 +1194,7 @@ int ObjectRef::l_get_inventory_formspec(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; std::string formspec = player->inventory_formspec; @@ -1207,7 +1207,7 @@ int ObjectRef::l_get_player_control(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushlstring(L, "", 0); return 1; @@ -1241,7 +1241,7 @@ int ObjectRef::l_get_player_control_bits(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) { lua_pushlstring(L, "", 0); return 1; @@ -1416,7 +1416,7 @@ int ObjectRef::l_hud_get(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1493,7 +1493,7 @@ int ObjectRef::l_hud_get_flags(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; @@ -1649,7 +1649,7 @@ int ObjectRef::l_get_sky(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - Player *player = getplayer(ref); + RemotePlayer *player = getplayer(ref); if (player == NULL) return 0; video::SColor bgcolor(255, 255, 255, 255); diff --git a/src/server.cpp b/src/server.cpp index dee8a3d70..67dbe1545 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -692,7 +692,7 @@ void Server::AsyncRunStep(bool initial_step) if (client->getState() < CS_DefinitionsSent) continue; - Player *player = m_env->getPlayer(client->peer_id); + RemotePlayer *player = m_env->getPlayer(client->peer_id); if (player == NULL) { // This can happen if the client timeouts somehow /*warningstream<getPlayer(peer_id); + RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); @@ -1896,7 +1896,7 @@ void Server::SendEyeOffset(u16 peer_id, v3f first, v3f third) } void Server::SendPlayerPrivileges(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); + RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); if(player->peer_id == PEER_ID_INEXISTENT) return; @@ -1917,7 +1917,7 @@ void Server::SendPlayerPrivileges(u16 peer_id) void Server::SendPlayerInventoryFormspec(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); + RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); if(player->peer_id == PEER_ID_INEXISTENT) return; @@ -1962,7 +1962,7 @@ s32 Server::playSound(const SimpleSoundSpec &spec, std::vector dst_clients; if(params.to_player != "") { - Player *player = m_env->getPlayer(params.to_player.c_str()); + RemotePlayer *player = m_env->getPlayer(params.to_player.c_str()); if(!player){ infostream<<"Server::playSound: Player \""< clients = m_clients.getClientIDs(); - for(std::vector::iterator - i = clients.begin(); i != clients.end(); ++i) { - Player *player = m_env->getPlayer(*i); - if(!player) + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { + RemotePlayer *player = m_env->getPlayer(*i); + if (!player) continue; - if(pos_exists) { + if (pos_exists) { if(player->getPosition().getDistanceFrom(pos) > params.max_hear_distance) continue; @@ -2048,7 +2047,7 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id, i != clients.end(); ++i) { if (far_players) { // Get player - if(Player *player = m_env->getPlayer(*i)) { + if (RemotePlayer *player = m_env->getPlayer(*i)) { // If player is far away, only set modified blocks not sent v3f player_pos = player->getPosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { @@ -2076,7 +2075,7 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id, if(far_players) { // Get player - if(Player *player = m_env->getPlayer(*i)) { + if (RemotePlayer *player = m_env->getPlayer(*i)) { // If player is far away, only set modified blocks not sent v3f player_pos = player->getPosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { @@ -2661,8 +2660,8 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { // Get player - Player *player = m_env->getPlayer(*i); - if(!player) + RemotePlayer *player = m_env->getPlayer(*i); + if (!player) continue; // Get name of player @@ -2842,8 +2841,8 @@ RemoteClient* Server::getClientNoEx(u16 peer_id, ClientState state_min) std::string Server::getPlayerName(u16 peer_id) { - Player *player = m_env->getPlayer(peer_id); - if(player == NULL) + RemotePlayer *player = m_env->getPlayer(peer_id); + if (player == NULL) return "[id="+itos(peer_id)+"]"; return player->getName(); } @@ -2870,13 +2869,12 @@ std::wstring Server::getStatusString() bool first = true; os< clients = m_clients.getClientIDs(); - for(std::vector::iterator i = clients.begin(); - i != clients.end(); ++i) { + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { // Get player - Player *player = m_env->getPlayer(*i); + RemotePlayer *player = m_env->getPlayer(*i); // Get name of player std::wstring name = L"unknown"; - if(player != NULL) + if (player != NULL) name = narrow_to_wide(player->getName()); // Add name to information string if(!first) @@ -2912,12 +2910,11 @@ void Server::reportPrivsModified(const std::string &name) std::vector clients = m_clients.getClientIDs(); for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { - Player *player = m_env->getPlayer(*i); + RemotePlayer *player = m_env->getPlayer(*i); reportPrivsModified(player->getName()); } } else { - RemotePlayer *player = - dynamic_cast(m_env->getPlayer(name.c_str())); + RemotePlayer *player = m_env->getPlayer(name.c_str()); if (!player) return; SendPlayerPrivileges(player->peer_id); @@ -2932,8 +2929,8 @@ void Server::reportPrivsModified(const std::string &name) void Server::reportInventoryFormspecModified(const std::string &name) { - Player *player = m_env->getPlayer(name.c_str()); - if(!player) + RemotePlayer *player = m_env->getPlayer(name.c_str()); + if (!player) return; SendPlayerInventoryFormspec(player->peer_id); } @@ -2963,7 +2960,7 @@ void Server::notifyPlayer(const char *name, const std::wstring &msg) m_admin_chat->outgoing_queue.push_back(new ChatEventChat("", msg)); } - Player *player = m_env->getPlayer(name); + RemotePlayer *player = m_env->getPlayer(name); if (!player) { return; } @@ -2981,7 +2978,7 @@ bool Server::showFormspec(const char *playername, const std::string &formspec, if (!m_env) return false; - Player *player = m_env->getPlayer(playername); + RemotePlayer *player = m_env->getPlayer(playername); if (!player) return false; @@ -3152,7 +3149,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - Player* player = m_env->getPlayer(playername.c_str()); + RemotePlayer* player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; @@ -3176,7 +3173,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - Player* player = m_env->getPlayer(playername.c_str()); + RemotePlayer* player = m_env->getPlayer(playername.c_str()); if (!player) return -1; peer_id = player->peer_id; @@ -3199,7 +3196,7 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - Player* player = m_env->getPlayer(playername.c_str()); + RemotePlayer* player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; -- cgit v1.2.3 From 7bbd716426bf989bf071e2322a9b797cc5f78acb Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 17:56:38 +0200 Subject: RemotePlayer/LocalPlayer Player base class proper separation (code cleanup) (patch 3 of X) * remove IGameDef from Player class, only LocalPlayer has it now * move many attributes/functions only used by LocalPlayer from Player to LocalPlayer * move many attributes/functions only used by RemotePlayer from Player to RemotePlayer * make some functions const * hudGetHotbarSelectedImage now returns const ref * RemotePlayer getHotbarSelectedImage now returns const ref * various code style fixes --- src/environment.cpp | 22 ++-- src/environment.h | 2 +- src/localplayer.cpp | 24 +++- src/localplayer.h | 24 +++- src/player.cpp | 176 ++++++++++++-------------- src/player.h | 270 ++++++++++++++++------------------------ src/script/lua_api/l_object.cpp | 2 +- src/server.cpp | 22 +--- src/server.h | 5 +- 9 files changed, 255 insertions(+), 292 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/environment.cpp b/src/environment.cpp index bc246f66c..514aa918a 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -603,8 +603,9 @@ void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, { for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { - ((Server*)m_gamedef)->DenyAccessVerCompliant((*it)->peer_id, - (*it)->protocol_version, reason, str_reason, reconnect); + RemotePlayer *player = dynamic_cast(*it); + ((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id, + player->protocol_version, reason, str_reason, reconnect); } } @@ -618,7 +619,7 @@ void ServerEnvironment::saveLoadedPlayers() ++it) { RemotePlayer *player = static_cast(*it); if (player->checkModified()) { - player->save(players_path); + player->save(players_path, m_gamedef); } } } @@ -628,7 +629,7 @@ void ServerEnvironment::savePlayer(RemotePlayer *player) std::string players_path = m_path_world + DIR_DELIM "players"; fs::CreateDir(players_path); - player->save(players_path); + player->save(players_path, m_gamedef); } RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) @@ -640,7 +641,7 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) RemotePlayer *player = getPlayer(playername.c_str()); if (!player) { - player = new RemotePlayer(m_gamedef, ""); + player = new RemotePlayer("", m_gamedef->idef()); newplayer = true; } @@ -2300,15 +2301,14 @@ LocalPlayer *ClientEnvironment::getPlayer(const char* name) return dynamic_cast(Environment::getPlayer(name)); } -void ClientEnvironment::addPlayer(Player *player) +void ClientEnvironment::addPlayer(LocalPlayer *player) { DSTACK(FUNCTION_NAME); /* - It is a failure if player is local and there already is a local - player + It is a failure if already is a local player */ - FATAL_ERROR_IF(player->isLocal() && getLocalPlayer() != NULL, - "Player is local but there is already a local player"); + FATAL_ERROR_IF(getLocalPlayer() != NULL, + "Player is local but there is already a local player"); Environment::addPlayer(player); } @@ -2563,7 +2563,7 @@ void ClientEnvironment::step(float dtime) */ for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { - LocalPlayer *player = dynamic_cast(*i); + Player *player = *i; assert(player); /* diff --git a/src/environment.h b/src/environment.h index 99066c367..7ed38ad5d 100644 --- a/src/environment.h +++ b/src/environment.h @@ -579,7 +579,7 @@ public: void step(f32 dtime); - virtual void addPlayer(Player *player); + virtual void addPlayer(LocalPlayer *player); LocalPlayer * getLocalPlayer(); /* diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 732ca8acf..bc242a59d 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -26,16 +26,29 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "environment.h" #include "map.h" -#include "util/numeric.h" +#include "client.h" /* LocalPlayer */ -LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name): - Player(gamedef, name), +LocalPlayer::LocalPlayer(Client *gamedef, const char *name): + Player(name, gamedef->idef()), parent(0), + got_teleported(false), isAttached(false), + touching_ground(false), + in_liquid(false), + in_liquid_stable(false), + liquid_viscosity(0), + is_climbing(false), + swimming_vertical(false), + // Movement overrides are multipliers and must be 1 by default + physics_override_speed(1.0f), + physics_override_jump(1.0f), + physics_override_gravity(1.0f), + physics_override_sneak(true), + physics_override_sneak_glitch(true), overridePosition(v3f(0,0,0)), last_position(v3f(0,0,0)), last_speed(v3f(0,0,0)), @@ -47,6 +60,8 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name): hotbar_image(""), hotbar_selected_image(""), light_color(255,255,255,255), + hurt_tilt_timer(0.0f), + hurt_tilt_strength(0.0f), m_sneak_node(32767,32767,32767), m_sneak_node_exists(false), m_need_to_get_new_sneak_node(true), @@ -54,7 +69,8 @@ LocalPlayer::LocalPlayer(IGameDef *gamedef, const char *name): m_old_node_below(32767,32767,32767), m_old_node_below_type("air"), m_can_jump(false), - m_cao(NULL) + m_cao(NULL), + m_gamedef(gamedef) { // Initialize hp to 0, so that no hearts will be shown if server // doesn't support health points diff --git a/src/localplayer.h b/src/localplayer.h index 8897adc5e..182b51d4d 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -21,8 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #define LOCALPLAYER_HEADER #include "player.h" +#include "environment.h" #include +class Client; class Environment; class GenericCAO; class ClientActiveObject; @@ -32,7 +34,7 @@ enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM}; // no local class LocalPlayer : public Player { public: - LocalPlayer(IGameDef *gamedef, const char *name); + LocalPlayer(Client *gamedef, const char *name); virtual ~LocalPlayer(); bool isLocal() const @@ -42,7 +44,23 @@ public: ClientActiveObject *parent; + bool got_teleported; bool isAttached; + bool touching_ground; + // This oscillates so that the player jumps a bit above the surface + bool in_liquid; + // This is more stable and defines the maximum speed of the player + bool in_liquid_stable; + // Gets the viscosity of water to calculate friction + u8 liquid_viscosity; + bool is_climbing; + bool swimming_vertical; + + float physics_override_speed; + float physics_override_jump; + float physics_override_gravity; + bool physics_override_sneak; + bool physics_override_sneak_glitch; v3f overridePosition; @@ -71,6 +89,9 @@ public: video::SColor light_color; + float hurt_tilt_timer; + float hurt_tilt_strength; + GenericCAO* getCAO() const { return m_cao; } @@ -102,6 +123,7 @@ private: bool m_can_jump; GenericCAO* m_cao; + Client *m_gamedef; }; #endif diff --git a/src/player.cpp b/src/player.cpp index fd72d63b6..c0d367134 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -32,31 +32,19 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" // strlcpy -Player::Player(IGameDef *gamedef, const char *name): - got_teleported(false), - touching_ground(false), - in_liquid(false), - in_liquid_stable(false), - liquid_viscosity(0), - is_climbing(false), - swimming_vertical(false), +Player::Player(const char *name, IItemDefManager *idef): camera_barely_in_ceiling(false), - inventory(gamedef->idef()), + inventory(idef), hp(PLAYER_MAX_HP), - hurt_tilt_timer(0), - hurt_tilt_strength(0), - protocol_version(0), peer_id(PEER_ID_INEXISTENT), keyPressed(0), // protected - m_gamedef(gamedef), m_breath(PLAYER_MAX_BREATH), m_pitch(0), m_yaw(0), m_speed(0,0,0), m_position(0,0,0), - m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30), - m_dirty(false) + m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30) { strlcpy(m_name, name, PLAYERNAME_SIZE); @@ -92,13 +80,6 @@ Player::Player(IGameDef *gamedef, const char *name): movement_gravity = 9.81 * BS; local_animation_speed = 0.0; - // Movement overrides are multipliers and must be 1 by default - physics_override_speed = 1; - physics_override_jump = 1; - physics_override_gravity = 1; - physics_override_sneak = true; - physics_override_sneak_glitch = true; - hud_flags = HUD_FLAG_HOTBAR_VISIBLE | HUD_FLAG_HEALTHBAR_VISIBLE | HUD_FLAG_CROSSHAIR_VISIBLE | HUD_FLAG_WIELDITEM_VISIBLE | @@ -117,70 +98,6 @@ v3s16 Player::getLightPosition() const return floatToInt(m_position + v3f(0,BS+BS/2,0), BS); } -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.setS32("hp", hp); - args.setS32("breath", m_breath); - - args.writeLines(os); - - os<<"PlayerArgsEnd\n"; - - inventory.serialize(os); -} - -void Player::deSerialize(std::istream &is, std::string playername) -{ - Settings args; - - if (!args.parseConfigLines(is, "PlayerArgsEnd")) { - throw SerializationError("PlayerArgsEnd of player " + - playername + " not found!"); - } - - m_dirty = true; - //args.getS32("version"); // Version field value not used - std::string name = args.get("name"); - strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); - setPitch(args.getFloat("pitch")); - setYaw(args.getFloat("yaw")); - setPosition(args.getV3F("position")); - try{ - hp = args.getS32("hp"); - }catch(SettingNotFoundException &e) { - hp = PLAYER_MAX_HP; - } - try{ - m_breath = args.getS32("breath"); - }catch(SettingNotFoundException &e) { - m_breath = PLAYER_MAX_BREATH; - } - - inventory.deSerialize(is); - - if(inventory.getList("craftpreview") == NULL) { - // Convert players without craftpreview - inventory.addList("craftpreview", 1); - - bool craftresult_is_preview = true; - if(args.exists("craftresult_is_preview")) - craftresult_is_preview = args.getBool("craftresult_is_preview"); - if(craftresult_is_preview) - { - // Clear craftresult - inventory.getList("craftresult")->changeItem(0, ItemStack()); - } - } -} - u32 Player::addHud(HudElement *toadd) { MutexAutoLock lock(m_mutex); @@ -227,17 +144,24 @@ void Player::clearHud() } } +/* + RemotePlayer +*/ // static config cache for remoteplayer bool RemotePlayer::m_setting_cache_loaded = false; float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; -RemotePlayer::RemotePlayer(IGameDef *gamedef, const char *name): - Player(gamedef, name), +RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): + Player(name, idef), + protocol_version(0), m_sao(NULL), + m_dirty(false), m_last_chat_message_sent(time(NULL)), m_chat_message_allowance(5.0f), - m_message_rate_overhead(0) + m_message_rate_overhead(0), + hud_hotbar_image(""), + hud_hotbar_selected_image("") { if (!RemotePlayer::m_setting_cache_loaded) { RemotePlayer::m_setting_chat_message_limit_per_10sec = @@ -260,7 +184,7 @@ RemotePlayer::RemotePlayer(IGameDef *gamedef, const char *name): movement_gravity = g_settings->getFloat("movement_gravity") * BS; } -void RemotePlayer::save(std::string savedir) +void RemotePlayer::save(std::string savedir, IGameDef *gamedef) { /* * We have to open all possible player files in the players directory @@ -269,7 +193,7 @@ void RemotePlayer::save(std::string savedir) */ // A player to deserialize files into to check their names - RemotePlayer testplayer(m_gamedef, ""); + RemotePlayer testplayer("", gamedef->idef()); savedir += DIR_DELIM; std::string path = savedir + m_name; @@ -309,11 +233,75 @@ void RemotePlayer::save(std::string savedir) return; } -/* - RemotePlayer -*/ +void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) +{ + Settings args; + + if (!args.parseConfigLines(is, "PlayerArgsEnd")) { + throw SerializationError("PlayerArgsEnd of player " + + playername + " not found!"); + } + + m_dirty = true; + //args.getS32("version"); // Version field value not used + std::string name = args.get("name"); + strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); + setPitch(args.getFloat("pitch")); + setYaw(args.getFloat("yaw")); + setPosition(args.getV3F("position")); + try{ + hp = args.getS32("hp"); + }catch(SettingNotFoundException &e) { + hp = PLAYER_MAX_HP; + } + try{ + m_breath = args.getS32("breath"); + }catch(SettingNotFoundException &e) { + m_breath = PLAYER_MAX_BREATH; + } + + inventory.deSerialize(is); + + if(inventory.getList("craftpreview") == NULL) { + // Convert players without craftpreview + inventory.addList("craftpreview", 1); + + bool craftresult_is_preview = true; + if(args.exists("craftresult_is_preview")) + craftresult_is_preview = args.getBool("craftresult_is_preview"); + if(craftresult_is_preview) + { + // Clear craftresult + inventory.getList("craftresult")->changeItem(0, ItemStack()); + } + } +} + +void RemotePlayer::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.setS32("hp", hp); + args.setS32("breath", m_breath); + + args.writeLines(os); + + os<<"PlayerArgsEnd\n"; + + inventory.serialize(os); +} + void RemotePlayer::setPosition(const v3f &position) { + if (position != m_position) + m_dirty = true; + Player::setPosition(position); if(m_sao) m_sao->setBasePosition(position); diff --git a/src/player.h b/src/player.h index fbd88fc71..1980a86a3 100644 --- a/src/player.h +++ b/src/player.h @@ -112,7 +112,7 @@ class Player { public: - Player(IGameDef *gamedef, const char *name); + Player(const char *name, IItemDefManager *idef); virtual ~Player() = 0; virtual void move(f32 dtime, Environment *env, f32 pos_max_d) @@ -151,80 +151,32 @@ public: virtual void setPosition(const v3f &position) { - if (position != m_position) - m_dirty = true; m_position = position; } - void setPitch(f32 pitch) + virtual void setPitch(f32 pitch) { - if (pitch != m_pitch) - m_dirty = true; m_pitch = pitch; } virtual void setYaw(f32 yaw) { - if (yaw != m_yaw) - m_dirty = true; m_yaw = yaw; } - f32 getPitch() - { - return m_pitch; - } - - f32 getYaw() - { - return m_yaw; - } - - u16 getBreath() - { - return m_breath; - } - - virtual void setBreath(u16 breath) - { - if (breath != m_breath) - m_dirty = true; - m_breath = breath; - } - - // Deprecated - f32 getRadPitchDep() - { - return -1.0 * m_pitch * core::DEGTORAD; - } - - // Deprecated - f32 getRadYawDep() - { - return (m_yaw + 90.) * core::DEGTORAD; - } - - f32 getRadPitch() - { - return m_pitch * core::DEGTORAD; - } + f32 getPitch() const { return m_pitch; } + f32 getYaw() const { return m_yaw; } + u16 getBreath() const { return m_breath; } - f32 getRadYaw() - { - return m_yaw * core::DEGTORAD; - } + virtual void setBreath(u16 breath) { m_breath = breath; } - const char *getName() const - { - return m_name; - } + f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } + f32 getRadYaw() const { return m_yaw * core::DEGTORAD; } + const char *getName() const { return m_name; } + aabb3f getCollisionbox() const { return m_collisionbox; } - aabb3f getCollisionbox() + u32 getFreeHudID() { - return m_collisionbox; - } - - u32 getFreeHudID() { size_t size = hud.size(); for (size_t i = 0; i != size; i++) { if (!hud[i]) @@ -233,41 +185,6 @@ public: return size; } - void setHotbarImage(const std::string &name) - { - hud_hotbar_image = name; - } - - std::string getHotbarImage() - { - return hud_hotbar_image; - } - - void setHotbarSelectedImage(const std::string &name) - { - hud_hotbar_selected_image = name; - } - - std::string getHotbarSelectedImage() { - return hud_hotbar_selected_image; - } - - void setSky(const video::SColor &bgcolor, const std::string &type, - const std::vector ¶ms) - { - m_sky_bgcolor = bgcolor; - m_sky_type = type; - m_sky_params = params; - } - - void getSky(video::SColor *bgcolor, std::string *type, - std::vector *params) - { - *bgcolor = m_sky_bgcolor; - *type = m_sky_type; - *params = m_sky_params; - } - void setLocalAnimations(v2s32 frames[4], float frame_speed) { for (int i = 0; i < 4; i++) @@ -282,49 +199,8 @@ public: *frame_speed = local_animation_speed; } - virtual bool isLocal() const - { - return false; - } + virtual bool isLocal() const { return false; } - virtual void setPlayerSAO(PlayerSAO *sao) - { - FATAL_ERROR("FIXME"); - } - - /* - serialize() writes a bunch of text that can contain - any characters except a '\0', and such an ending that - deSerialize stops reading exactly at the right point. - */ - void serialize(std::ostream &os); - void deSerialize(std::istream &is, std::string playername); - - bool checkModified() const - { - return m_dirty || inventory.checkModified(); - } - - void setModified(const bool x) - { - m_dirty = x; - if (!x) - inventory.setModified(x); - } - - // Use a function, if isDead can be defined by other conditions - bool isDead() { return hp == 0; } - - bool got_teleported; - bool touching_ground; - // This oscillates so that the player jumps a bit above the surface - bool in_liquid; - // This is more stable and defines the maximum speed of the player - bool in_liquid_stable; - // Gets the viscosity of water to calculate friction - u8 liquid_viscosity; - bool is_climbing; - bool swimming_vertical; bool camera_barely_in_ceiling; v3f eye_offset_first; v3f eye_offset_third; @@ -344,21 +220,11 @@ public: f32 movement_liquid_sink; f32 movement_gravity; - float physics_override_speed; - float physics_override_jump; - float physics_override_gravity; - bool physics_override_sneak; - bool physics_override_sneak_glitch; - v2s32 local_animations[4]; float local_animation_speed; u16 hp; - float hurt_tilt_timer; - float hurt_tilt_strength; - - u16 protocol_version; u16 peer_id; std::string inventory_formspec; @@ -368,7 +234,6 @@ public: u32 keyPressed; - HudElement* getHud(u32 id); u32 addHud(HudElement* hud); HudElement* removeHud(u32 id); @@ -376,11 +241,7 @@ public: u32 hud_flags; s32 hud_hotbar_itemcount; - std::string hud_hotbar_image; - std::string hud_hotbar_selected_image; protected: - IGameDef *m_gamedef; - char m_name[PLAYERNAME_SIZE]; u16 m_breath; f32 m_pitch; @@ -389,13 +250,7 @@ protected: v3f m_position; aabb3f m_collisionbox; - bool m_dirty; - std::vector hud; - - std::string m_sky_type; - video::SColor m_sky_bgcolor; - std::vector m_sky_params; private: // Protect some critical areas // hud for example can be modified by EmergeThread @@ -414,15 +269,14 @@ enum RemotePlayerChatResult { class RemotePlayer : public Player { public: - RemotePlayer(IGameDef *gamedef, const char *name); + RemotePlayer(const char *name, IItemDefManager *idef); virtual ~RemotePlayer() {} - void save(std::string savedir); + void save(std::string savedir, IGameDef *gamedef); + void deSerialize(std::istream &is, const std::string &playername); - PlayerSAO *getPlayerSAO() - { return m_sao; } - void setPlayerSAO(PlayerSAO *sao) - { m_sao = sao; } + PlayerSAO *getPlayerSAO() { return m_sao; } + void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } void setPosition(const v3f &position); const RemotePlayerChatResult canSendChatMessage(); @@ -446,8 +300,92 @@ public: *ratio = m_day_night_ratio; } + // Use a function, if isDead can be defined by other conditions + bool isDead() const { return hp == 0; } + + void setHotbarImage(const std::string &name) + { + hud_hotbar_image = name; + } + + std::string getHotbarImage() const + { + return hud_hotbar_image; + } + + void setHotbarSelectedImage(const std::string &name) + { + hud_hotbar_selected_image = name; + } + + const std::string &getHotbarSelectedImage() const + { + return hud_hotbar_selected_image; + } + + // Deprecated + f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } + + // Deprecated + f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } + + void setSky(const video::SColor &bgcolor, const std::string &type, + const std::vector ¶ms) + { + m_sky_bgcolor = bgcolor; + m_sky_type = type; + m_sky_params = params; + } + + void getSky(video::SColor *bgcolor, std::string *type, + std::vector *params) + { + *bgcolor = m_sky_bgcolor; + *type = m_sky_type; + *params = m_sky_params; + } + + bool checkModified() const { return m_dirty || inventory.checkModified(); } + + void setModified(const bool x) + { + m_dirty = x; + if (!x) + inventory.setModified(x); + } + + virtual void setBreath(u16 breath) + { + if (breath != m_breath) + m_dirty = true; + Player::setBreath(breath); + } + + virtual void setPitch(f32 pitch) + { + if (pitch != m_pitch) + m_dirty = true; + Player::setPitch(pitch); + } + + virtual void setYaw(f32 yaw) + { + if (yaw != m_yaw) + m_dirty = true; + Player::setYaw(yaw); + } + + u16 protocol_version; private: + /* + serialize() writes a bunch of text that can contain + any characters except a '\0', and such an ending that + deSerialize stops reading exactly at the right point. + */ + void serialize(std::ostream &os); + PlayerSAO *m_sao; + bool m_dirty; static bool m_setting_cache_loaded; static float m_setting_chat_message_limit_per_10sec; @@ -459,6 +397,12 @@ private: bool m_day_night_ratio_do_override; float m_day_night_ratio; + std::string hud_hotbar_image; + std::string hud_hotbar_selected_image; + + std::string m_sky_type; + video::SColor m_sky_bgcolor; + std::vector m_sky_params; }; #endif diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 74b33da37..a1f83919c 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1600,7 +1600,7 @@ int ObjectRef::l_hud_get_hotbar_selected_image(lua_State *L) if (player == NULL) return 0; - std::string name = getServer(L)->hudGetHotbarSelectedImage(player); + const std::string &name = getServer(L)->hudGetHotbarSelectedImage(player); lua_pushlstring(L, name.c_str(), name.size()); return 1; } diff --git a/src/server.cpp b/src/server.cpp index edd97e225..71e71f43e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1108,7 +1108,7 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id) SendPlayerBreath(peer_id); // Show death screen if necessary - if(player->isDead()) + if (player->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); // Note things in chat if not in simple singleplayer mode @@ -3080,14 +3080,6 @@ void Server::hudSetHotbarSelectedImage(RemotePlayer *player, std::string name) SendHUDSetParam(player->peer_id, HUD_PARAM_HOTBAR_SELECTED_IMAGE, name); } -std::string Server::hudGetHotbarSelectedImage(RemotePlayer *player) -{ - if (!player) - return ""; - - return player->getHotbarSelectedImage(); -} - bool Server::setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4], f32 frame_speed) { @@ -3408,11 +3400,10 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version /* Try to get an existing player */ - RemotePlayer *player = static_cast(m_env->getPlayer(name)); + RemotePlayer *player = m_env->getPlayer(name); // If player is already connected, cancel - if(player != NULL && player->peer_id != 0) - { + if (player != NULL && player->peer_id != 0) { infostream<<"emergePlayer(): Player already connected"<getPlayer(peer_id) != NULL) - { + if (m_env->getPlayer(peer_id) != NULL) { infostream<<"emergePlayer(): Player with wrong name but same" " peer_id already exists"<(m_env->loadPlayer(name)); + player = m_env->loadPlayer(name); } // Create player if it doesn't exist if (!player) { newplayer = true; - player = new RemotePlayer(this, name); + player = new RemotePlayer(name, this->idef()); // Set player position infostream<<"Server: Finding spawn place for player \"" <getHotbarSelectedImage(); + } inline Address getPeerAddress(u16 peer_id) { return m_con.GetPeerAddress(peer_id); } -- cgit v1.2.3 From 569b89b36fff058390cb90458da4285552a9c97e Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 19:08:23 +0200 Subject: Move RemotePlayer code to its own cpp/header --- src/CMakeLists.txt | 1 + src/clientiface.cpp | 2 +- src/content_sao.cpp | 6 +- src/content_sao.h | 5 +- src/environment.cpp | 2 +- src/environment.h | 2 +- src/localplayer.h | 1 + src/player.cpp | 206 ----------------------------------- src/player.h | 149 -------------------------- src/remoteplayer.cpp | 230 ++++++++++++++++++++++++++++++++++++++++ src/remoteplayer.h | 175 ++++++++++++++++++++++++++++++ src/script/lua_api/l_object.cpp | 2 +- src/script/lua_api/l_object.h | 2 +- src/server.cpp | 8 +- src/server.h | 2 +- 15 files changed, 423 insertions(+), 370 deletions(-) create mode 100644 src/remoteplayer.cpp create mode 100644 src/remoteplayer.h (limited to 'src/environment.cpp') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 753291cba..8e3ae95ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -445,6 +445,7 @@ set(common_SRCS porting.cpp profiler.cpp quicktune.cpp + remoteplayer.cpp rollback.cpp rollback_interface.cpp serialization.cpp diff --git a/src/clientiface.cpp b/src/clientiface.cpp index fbfc16770..d78cf1c53 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "clientiface.h" #include "util/numeric.h" #include "util/mathconstants.h" -#include "player.h" +#include "remoteplayer.h" #include "settings.h" #include "mapblock.h" #include "network/connection.h" diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 1664f5993..5d3ed38bc 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -26,7 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serialization.h" // For compressZlib #include "tool.h" // For ToolCapabilities #include "gamedef.h" -#include "player.h" +#include "remoteplayer.h" #include "server.h" #include "scripting_game.h" #include "genericobject.h" @@ -391,7 +391,7 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version) (*ii).second.X, (*ii).second.Y)); // m_bone_position.size } os<::const_iterator ii = m_attachment_child_ids.begin(); + for (UNORDERED_SET::const_iterator ii = m_attachment_child_ids.begin(); (ii != m_attachment_child_ids.end()); ++ii) { if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) { os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(), obj->getClientInitializationData(protocol_version))); @@ -880,7 +880,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) m_physics_override_jump, m_physics_override_gravity, m_physics_override_sneak, m_physics_override_sneak_glitch)); // 5 os << serializeLongString(gob_cmd_update_nametag_attributes(m_prop.nametag_color)); // 6 (GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES) : Deprecated, for backwards compatibility only. - for (UNORDERED_SET::const_iterator ii = m_attachment_child_ids.begin(); + for (UNORDERED_SET::const_iterator ii = m_attachment_child_ids.begin(); ii != m_attachment_child_ids.end(); ++ii) { if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) { os << serializeLongString(gob_cmd_update_infant(*ii, obj->getSendType(), obj->getClientInitializationData(protocol_version))); diff --git a/src/content_sao.h b/src/content_sao.h index 341ebb5da..76a3a37da 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -22,7 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serverobject.h" #include "itemgroup.h" -#include "player.h" #include "object_properties.h" /* @@ -157,6 +156,8 @@ public: } }; +class RemotePlayer; + class PlayerSAO : public ServerActiveObject { public: @@ -231,7 +232,7 @@ public: void disconnected(); - RemotePlayer* getPlayer() { return m_player; } + RemotePlayer *getPlayer() { return m_player; } u16 getPeerID() const { return m_peer_id; } // Cheat prevention diff --git a/src/environment.cpp b/src/environment.cpp index 514aa918a..ff43ac516 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -2705,7 +2705,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object) } object->setId(new_id); } - if(!isFreeClientActiveObjectId(object->getId(), m_active_objects)) { + if (!isFreeClientActiveObjectId(object->getId(), m_active_objects)) { infostream<<"ClientEnvironment::addActiveObject(): " <<"id is not free ("<getId()<<")"< ¤t_objects, std::queue &removed_objects); diff --git a/src/localplayer.h b/src/localplayer.h index 182b51d4d..9d43128aa 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -28,6 +28,7 @@ class Client; class Environment; class GenericCAO; class ClientActiveObject; +class IGameDef; enum LocalPlayerAnimations {NO_ANIM, WALK_ANIM, DIG_ANIM, WD_ANIM}; // no local animation, walking, digging, both diff --git a/src/player.cpp b/src/player.cpp index c0d367134..fa82a79f4 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -19,15 +19,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "player.h" -#include #include "threading/mutex_auto_lock.h" #include "util/numeric.h" #include "hud.h" #include "constants.h" #include "gamedef.h" #include "settings.h" -#include "content_sao.h" -#include "filesys.h" #include "log.h" #include "porting.h" // strlcpy @@ -143,206 +140,3 @@ void Player::clearHud() hud.pop_back(); } } - -/* - RemotePlayer -*/ -// static config cache for remoteplayer -bool RemotePlayer::m_setting_cache_loaded = false; -float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; -u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; - -RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): - Player(name, idef), - protocol_version(0), - m_sao(NULL), - m_dirty(false), - m_last_chat_message_sent(time(NULL)), - m_chat_message_allowance(5.0f), - m_message_rate_overhead(0), - hud_hotbar_image(""), - hud_hotbar_selected_image("") -{ - if (!RemotePlayer::m_setting_cache_loaded) { - RemotePlayer::m_setting_chat_message_limit_per_10sec = - g_settings->getFloat("chat_message_limit_per_10sec"); - RemotePlayer::m_setting_chat_message_limit_trigger_kick = - g_settings->getU16("chat_message_limit_trigger_kick"); - RemotePlayer::m_setting_cache_loaded = true; - } - movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS; - movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS; - movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS; - movement_speed_walk = g_settings->getFloat("movement_speed_walk") * BS; - movement_speed_crouch = g_settings->getFloat("movement_speed_crouch") * BS; - movement_speed_fast = g_settings->getFloat("movement_speed_fast") * BS; - movement_speed_climb = g_settings->getFloat("movement_speed_climb") * BS; - movement_speed_jump = g_settings->getFloat("movement_speed_jump") * BS; - movement_liquid_fluidity = g_settings->getFloat("movement_liquid_fluidity") * BS; - movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS; - movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS; - movement_gravity = g_settings->getFloat("movement_gravity") * BS; -} - -void RemotePlayer::save(std::string savedir, IGameDef *gamedef) -{ - /* - * We have to open all possible player files in the players directory - * and check their player names because some file systems are not - * case-sensitive and player names are case-sensitive. - */ - - // A player to deserialize files into to check their names - RemotePlayer testplayer("", gamedef->idef()); - - savedir += DIR_DELIM; - std::string path = savedir + m_name; - for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) { - if (!fs::PathExists(path)) { - // Open file and serialize - std::ostringstream ss(std::ios_base::binary); - serialize(ss); - if (!fs::safeWriteToFile(path, ss.str())) { - infostream << "Failed to write " << path << std::endl; - } - setModified(false); - return; - } - // Open file and deserialize - std::ifstream is(path.c_str(), std::ios_base::binary); - if (!is.good()) { - infostream << "Failed to open " << path << std::endl; - return; - } - testplayer.deSerialize(is, path); - is.close(); - if (strcmp(testplayer.getName(), m_name) == 0) { - // Open file and serialize - std::ostringstream ss(std::ios_base::binary); - serialize(ss); - if (!fs::safeWriteToFile(path, ss.str())) { - infostream << "Failed to write " << path << std::endl; - } - setModified(false); - return; - } - path = savedir + m_name + itos(i); - } - - infostream << "Didn't find free file for player " << m_name << std::endl; - return; -} - -void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) -{ - Settings args; - - if (!args.parseConfigLines(is, "PlayerArgsEnd")) { - throw SerializationError("PlayerArgsEnd of player " + - playername + " not found!"); - } - - m_dirty = true; - //args.getS32("version"); // Version field value not used - std::string name = args.get("name"); - strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); - setPitch(args.getFloat("pitch")); - setYaw(args.getFloat("yaw")); - setPosition(args.getV3F("position")); - try{ - hp = args.getS32("hp"); - }catch(SettingNotFoundException &e) { - hp = PLAYER_MAX_HP; - } - try{ - m_breath = args.getS32("breath"); - }catch(SettingNotFoundException &e) { - m_breath = PLAYER_MAX_BREATH; - } - - inventory.deSerialize(is); - - if(inventory.getList("craftpreview") == NULL) { - // Convert players without craftpreview - inventory.addList("craftpreview", 1); - - bool craftresult_is_preview = true; - if(args.exists("craftresult_is_preview")) - craftresult_is_preview = args.getBool("craftresult_is_preview"); - if(craftresult_is_preview) - { - // Clear craftresult - inventory.getList("craftresult")->changeItem(0, ItemStack()); - } - } -} - -void RemotePlayer::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.setS32("hp", hp); - args.setS32("breath", m_breath); - - args.writeLines(os); - - os<<"PlayerArgsEnd\n"; - - inventory.serialize(os); -} - -void RemotePlayer::setPosition(const v3f &position) -{ - if (position != m_position) - m_dirty = true; - - Player::setPosition(position); - if(m_sao) - m_sao->setBasePosition(position); -} - -const RemotePlayerChatResult RemotePlayer::canSendChatMessage() -{ - // Rate limit messages - u32 now = time(NULL); - float time_passed = now - m_last_chat_message_sent; - m_last_chat_message_sent = now; - - // If this feature is disabled - if (m_setting_chat_message_limit_per_10sec <= 0.0) { - return RPLAYER_CHATRESULT_OK; - } - - m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f); - if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) { - m_chat_message_allowance = m_setting_chat_message_limit_per_10sec; - } - - if (m_chat_message_allowance < 1.0f) { - infostream << "Player " << m_name - << " chat limited due to excessive message amount." << std::endl; - - // Kick player if flooding is too intensive - m_message_rate_overhead++; - if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) { - return RPLAYER_CHATRESULT_KICK; - } - - return RPLAYER_CHATRESULT_FLOODING; - } - - // Reinit message overhead - if (m_message_rate_overhead > 0) { - m_message_rate_overhead = 0; - } - - m_chat_message_allowance -= 1.0f; - return RPLAYER_CHATRESULT_OK; -} - diff --git a/src/player.h b/src/player.h index 1980a86a3..3c945b100 100644 --- a/src/player.h +++ b/src/player.h @@ -99,9 +99,7 @@ struct PlayerControl }; class Map; -class IGameDef; struct CollisionInfo; -class PlayerSAO; struct HudElement; class Environment; @@ -258,152 +256,5 @@ private: Mutex m_mutex; }; -enum RemotePlayerChatResult { - RPLAYER_CHATRESULT_OK, - RPLAYER_CHATRESULT_FLOODING, - RPLAYER_CHATRESULT_KICK, -}; -/* - Player on the server -*/ -class RemotePlayer : public Player -{ -public: - RemotePlayer(const char *name, IItemDefManager *idef); - virtual ~RemotePlayer() {} - - void save(std::string savedir, IGameDef *gamedef); - void deSerialize(std::istream &is, const std::string &playername); - - PlayerSAO *getPlayerSAO() { return m_sao; } - void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } - void setPosition(const v3f &position); - - const RemotePlayerChatResult canSendChatMessage(); - - void setHotbarItemcount(s32 hotbar_itemcount) - { - hud_hotbar_itemcount = hotbar_itemcount; - } - - s32 getHotbarItemcount() const { return hud_hotbar_itemcount; } - - void overrideDayNightRatio(bool do_override, float ratio) - { - m_day_night_ratio_do_override = do_override; - m_day_night_ratio = ratio; - } - - void getDayNightRatio(bool *do_override, float *ratio) - { - *do_override = m_day_night_ratio_do_override; - *ratio = m_day_night_ratio; - } - - // Use a function, if isDead can be defined by other conditions - bool isDead() const { return hp == 0; } - - void setHotbarImage(const std::string &name) - { - hud_hotbar_image = name; - } - - std::string getHotbarImage() const - { - return hud_hotbar_image; - } - - void setHotbarSelectedImage(const std::string &name) - { - hud_hotbar_selected_image = name; - } - - const std::string &getHotbarSelectedImage() const - { - return hud_hotbar_selected_image; - } - - // Deprecated - f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } - - // Deprecated - f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } - - void setSky(const video::SColor &bgcolor, const std::string &type, - const std::vector ¶ms) - { - m_sky_bgcolor = bgcolor; - m_sky_type = type; - m_sky_params = params; - } - - void getSky(video::SColor *bgcolor, std::string *type, - std::vector *params) - { - *bgcolor = m_sky_bgcolor; - *type = m_sky_type; - *params = m_sky_params; - } - - bool checkModified() const { return m_dirty || inventory.checkModified(); } - - void setModified(const bool x) - { - m_dirty = x; - if (!x) - inventory.setModified(x); - } - - virtual void setBreath(u16 breath) - { - if (breath != m_breath) - m_dirty = true; - Player::setBreath(breath); - } - - virtual void setPitch(f32 pitch) - { - if (pitch != m_pitch) - m_dirty = true; - Player::setPitch(pitch); - } - - virtual void setYaw(f32 yaw) - { - if (yaw != m_yaw) - m_dirty = true; - Player::setYaw(yaw); - } - - u16 protocol_version; -private: - /* - serialize() writes a bunch of text that can contain - any characters except a '\0', and such an ending that - deSerialize stops reading exactly at the right point. - */ - void serialize(std::ostream &os); - - PlayerSAO *m_sao; - bool m_dirty; - - static bool m_setting_cache_loaded; - static float m_setting_chat_message_limit_per_10sec; - static u16 m_setting_chat_message_limit_trigger_kick; - - u32 m_last_chat_message_sent; - float m_chat_message_allowance; - u16 m_message_rate_overhead; - - bool m_day_night_ratio_do_override; - float m_day_night_ratio; - std::string hud_hotbar_image; - std::string hud_hotbar_selected_image; - - std::string m_sky_type; - video::SColor m_sky_bgcolor; - std::vector m_sky_params; -}; - #endif diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp new file mode 100644 index 000000000..f64d1d690 --- /dev/null +++ b/src/remoteplayer.cpp @@ -0,0 +1,230 @@ +/* +Minetest +Copyright (C) 2010-2016 celeron55, Perttu Ahola +Copyright (C) 2014-2016 nerzhul, Loic Blot + +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 "remoteplayer.h" +#include "content_sao.h" +#include "filesys.h" +#include "gamedef.h" +#include "porting.h" // strlcpy +#include "settings.h" + + +/* + RemotePlayer +*/ +// static config cache for remoteplayer +bool RemotePlayer::m_setting_cache_loaded = false; +float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; +u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; + +RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): + Player(name, idef), + protocol_version(0), + m_sao(NULL), + m_dirty(false), + m_last_chat_message_sent(time(NULL)), + m_chat_message_allowance(5.0f), + m_message_rate_overhead(0), + hud_hotbar_image(""), + hud_hotbar_selected_image("") +{ + if (!RemotePlayer::m_setting_cache_loaded) { + RemotePlayer::m_setting_chat_message_limit_per_10sec = + g_settings->getFloat("chat_message_limit_per_10sec"); + RemotePlayer::m_setting_chat_message_limit_trigger_kick = + g_settings->getU16("chat_message_limit_trigger_kick"); + RemotePlayer::m_setting_cache_loaded = true; + } + movement_acceleration_default = g_settings->getFloat("movement_acceleration_default") * BS; + movement_acceleration_air = g_settings->getFloat("movement_acceleration_air") * BS; + movement_acceleration_fast = g_settings->getFloat("movement_acceleration_fast") * BS; + movement_speed_walk = g_settings->getFloat("movement_speed_walk") * BS; + movement_speed_crouch = g_settings->getFloat("movement_speed_crouch") * BS; + movement_speed_fast = g_settings->getFloat("movement_speed_fast") * BS; + movement_speed_climb = g_settings->getFloat("movement_speed_climb") * BS; + movement_speed_jump = g_settings->getFloat("movement_speed_jump") * BS; + movement_liquid_fluidity = g_settings->getFloat("movement_liquid_fluidity") * BS; + movement_liquid_fluidity_smooth = g_settings->getFloat("movement_liquid_fluidity_smooth") * BS; + movement_liquid_sink = g_settings->getFloat("movement_liquid_sink") * BS; + movement_gravity = g_settings->getFloat("movement_gravity") * BS; +} + +void RemotePlayer::save(std::string savedir, IGameDef *gamedef) +{ + /* + * We have to open all possible player files in the players directory + * and check their player names because some file systems are not + * case-sensitive and player names are case-sensitive. + */ + + // A player to deserialize files into to check their names + RemotePlayer testplayer("", gamedef->idef()); + + savedir += DIR_DELIM; + std::string path = savedir + m_name; + for (u32 i = 0; i < PLAYER_FILE_ALTERNATE_TRIES; i++) { + if (!fs::PathExists(path)) { + // Open file and serialize + std::ostringstream ss(std::ios_base::binary); + serialize(ss); + if (!fs::safeWriteToFile(path, ss.str())) { + infostream << "Failed to write " << path << std::endl; + } + setModified(false); + return; + } + // Open file and deserialize + std::ifstream is(path.c_str(), std::ios_base::binary); + if (!is.good()) { + infostream << "Failed to open " << path << std::endl; + return; + } + testplayer.deSerialize(is, path); + is.close(); + if (strcmp(testplayer.getName(), m_name) == 0) { + // Open file and serialize + std::ostringstream ss(std::ios_base::binary); + serialize(ss); + if (!fs::safeWriteToFile(path, ss.str())) { + infostream << "Failed to write " << path << std::endl; + } + setModified(false); + return; + } + path = savedir + m_name + itos(i); + } + + infostream << "Didn't find free file for player " << m_name << std::endl; + return; +} + +void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) +{ + Settings args; + + if (!args.parseConfigLines(is, "PlayerArgsEnd")) { + throw SerializationError("PlayerArgsEnd of player " + + playername + " not found!"); + } + + m_dirty = true; + //args.getS32("version"); // Version field value not used + std::string name = args.get("name"); + strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); + setPitch(args.getFloat("pitch")); + setYaw(args.getFloat("yaw")); + setPosition(args.getV3F("position")); + try { + hp = args.getS32("hp"); + } catch(SettingNotFoundException &e) { + hp = PLAYER_MAX_HP; + } + + try { + m_breath = args.getS32("breath"); + } catch(SettingNotFoundException &e) { + m_breath = PLAYER_MAX_BREATH; + } + + inventory.deSerialize(is); + + if(inventory.getList("craftpreview") == NULL) { + // Convert players without craftpreview + inventory.addList("craftpreview", 1); + + bool craftresult_is_preview = true; + if(args.exists("craftresult_is_preview")) + craftresult_is_preview = args.getBool("craftresult_is_preview"); + if(craftresult_is_preview) + { + // Clear craftresult + inventory.getList("craftresult")->changeItem(0, ItemStack()); + } + } +} + +void RemotePlayer::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.setS32("hp", hp); + args.setS32("breath", m_breath); + + args.writeLines(os); + + os<<"PlayerArgsEnd\n"; + + inventory.serialize(os); +} + +void RemotePlayer::setPosition(const v3f &position) +{ + if (position != m_position) + m_dirty = true; + + Player::setPosition(position); + if(m_sao) + m_sao->setBasePosition(position); +} + +const RemotePlayerChatResult RemotePlayer::canSendChatMessage() +{ + // Rate limit messages + u32 now = time(NULL); + float time_passed = now - m_last_chat_message_sent; + m_last_chat_message_sent = now; + + // If this feature is disabled + if (m_setting_chat_message_limit_per_10sec <= 0.0) { + return RPLAYER_CHATRESULT_OK; + } + + m_chat_message_allowance += time_passed * (m_setting_chat_message_limit_per_10sec / 8.0f); + if (m_chat_message_allowance > m_setting_chat_message_limit_per_10sec) { + m_chat_message_allowance = m_setting_chat_message_limit_per_10sec; + } + + if (m_chat_message_allowance < 1.0f) { + infostream << "Player " << m_name + << " chat limited due to excessive message amount." << std::endl; + + // Kick player if flooding is too intensive + m_message_rate_overhead++; + if (m_message_rate_overhead > RemotePlayer::m_setting_chat_message_limit_trigger_kick) { + return RPLAYER_CHATRESULT_KICK; + } + + return RPLAYER_CHATRESULT_FLOODING; + } + + // Reinit message overhead + if (m_message_rate_overhead > 0) { + m_message_rate_overhead = 0; + } + + m_chat_message_allowance -= 1.0f; + return RPLAYER_CHATRESULT_OK; +} diff --git a/src/remoteplayer.h b/src/remoteplayer.h new file mode 100644 index 000000000..f6c70b0e9 --- /dev/null +++ b/src/remoteplayer.h @@ -0,0 +1,175 @@ +/* +Minetest +Copyright (C) 2010-2016 celeron55, Perttu Ahola +Copyright (C) 2014-2016 nerzhul, Loic Blot + +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. +*/ + +#ifndef REMOTEPLAYER_HEADER +#define REMOTEPLAYER_HEADER + +#include "player.h" + +class PlayerSAO; + +enum RemotePlayerChatResult { + RPLAYER_CHATRESULT_OK, + RPLAYER_CHATRESULT_FLOODING, + RPLAYER_CHATRESULT_KICK, +}; +/* + Player on the server +*/ +class RemotePlayer : public Player +{ +public: + RemotePlayer(const char *name, IItemDefManager *idef); + virtual ~RemotePlayer() {} + + void save(std::string savedir, IGameDef *gamedef); + void deSerialize(std::istream &is, const std::string &playername); + + PlayerSAO *getPlayerSAO() { return m_sao; } + void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } + void setPosition(const v3f &position); + + const RemotePlayerChatResult canSendChatMessage(); + + void setHotbarItemcount(s32 hotbar_itemcount) + { + hud_hotbar_itemcount = hotbar_itemcount; + } + + s32 getHotbarItemcount() const { return hud_hotbar_itemcount; } + + void overrideDayNightRatio(bool do_override, float ratio) + { + m_day_night_ratio_do_override = do_override; + m_day_night_ratio = ratio; + } + + void getDayNightRatio(bool *do_override, float *ratio) + { + *do_override = m_day_night_ratio_do_override; + *ratio = m_day_night_ratio; + } + + // Use a function, if isDead can be defined by other conditions + bool isDead() const { return hp == 0; } + + void setHotbarImage(const std::string &name) + { + hud_hotbar_image = name; + } + + std::string getHotbarImage() const + { + return hud_hotbar_image; + } + + void setHotbarSelectedImage(const std::string &name) + { + hud_hotbar_selected_image = name; + } + + const std::string &getHotbarSelectedImage() const + { + return hud_hotbar_selected_image; + } + + // Deprecated + f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } + + // Deprecated + f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } + + void setSky(const video::SColor &bgcolor, const std::string &type, + const std::vector ¶ms) + { + m_sky_bgcolor = bgcolor; + m_sky_type = type; + m_sky_params = params; + } + + void getSky(video::SColor *bgcolor, std::string *type, + std::vector *params) + { + *bgcolor = m_sky_bgcolor; + *type = m_sky_type; + *params = m_sky_params; + } + + bool checkModified() const { return m_dirty || inventory.checkModified(); } + + void setModified(const bool x) + { + m_dirty = x; + if (!x) + inventory.setModified(x); + } + + virtual void setBreath(u16 breath) + { + if (breath != m_breath) + m_dirty = true; + Player::setBreath(breath); + } + + virtual void setPitch(f32 pitch) + { + if (pitch != m_pitch) + m_dirty = true; + Player::setPitch(pitch); + } + + virtual void setYaw(f32 yaw) + { + if (yaw != m_yaw) + m_dirty = true; + Player::setYaw(yaw); + } + + u16 protocol_version; +private: + /* + serialize() writes a bunch of text that can contain + any characters except a '\0', and such an ending that + deSerialize stops reading exactly at the right point. + */ + void serialize(std::ostream &os); + + PlayerSAO *m_sao; + bool m_dirty; + + static bool m_setting_cache_loaded; + static float m_setting_chat_message_limit_per_10sec; + static u16 m_setting_chat_message_limit_trigger_kick; + + u32 m_last_chat_message_sent; + float m_chat_message_allowance; + u16 m_message_rate_overhead; + + bool m_day_night_ratio_do_override; + float m_day_night_ratio; + std::string hud_hotbar_image; + std::string hud_hotbar_selected_image; + + std::string m_sky_type; + video::SColor m_sky_bgcolor; + std::vector m_sky_params; +}; + +#endif diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index a1f83919c..bb352e429 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -107,7 +107,7 @@ PlayerSAO* ObjectRef::getplayersao(ObjectRef *ref) return (PlayerSAO*)obj; } -RemotePlayer* ObjectRef::getplayer(ObjectRef *ref) +RemotePlayer *ObjectRef::getplayer(ObjectRef *ref) { PlayerSAO *playersao = getplayersao(ref); if (playersao == NULL) diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index dfc1b49d2..09f10e417 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -47,7 +47,7 @@ private: static PlayerSAO* getplayersao(ObjectRef *ref); - static RemotePlayer* getplayer(ObjectRef *ref); + static RemotePlayer *getplayer(ObjectRef *ref); // Exported functions diff --git a/src/server.cpp b/src/server.cpp index 71e71f43e..a93c143c7 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2688,7 +2688,7 @@ void Server::DeleteClient(u16 peer_id, ClientDeletionReason reason) SendChatMessage(PEER_ID_INEXISTENT,message); } -void Server::UpdateCrafting(RemotePlayer* player) +void Server::UpdateCrafting(RemotePlayer *player) { DSTACK(FUNCTION_NAME); @@ -3141,7 +3141,7 @@ void Server::spawnParticle(const std::string &playername, v3f pos, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - RemotePlayer* player = m_env->getPlayer(playername.c_str()); + RemotePlayer *player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; @@ -3165,7 +3165,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - RemotePlayer* player = m_env->getPlayer(playername.c_str()); + RemotePlayer *player = m_env->getPlayer(playername.c_str()); if (!player) return -1; peer_id = player->peer_id; @@ -3188,7 +3188,7 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) u16 peer_id = PEER_ID_INEXISTENT; if (playername != "") { - RemotePlayer* player = m_env->getPlayer(playername.c_str()); + RemotePlayer *player = m_env->getPlayer(playername.c_str()); if (!player) return; peer_id = player->peer_id; diff --git a/src/server.h b/src/server.h index fc4758c5f..6ee61a0eb 100644 --- a/src/server.h +++ b/src/server.h @@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "environment.h" #include "chat_interface.h" #include "clientiface.h" -#include "player.h" +#include "remoteplayer.h" #include "network/networkpacket.h" #include #include -- cgit v1.2.3 From 70f104be076321330a0827010704761a040d8ec7 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 8 Oct 2016 23:13:38 +0200 Subject: Environment cleanup * Move client list to ServerEnvironment and use RemotePlayer members instead of Player * ClientEnvironment only use setLocalPlayer to specify the current player * Remove ClientEnvironment dead code on player list (in fact other players are CAO not Player objects) * Drop LocalPlayer::getPlayer(xxx) functions which aren't used. * Improve a little bit performance by using const ref list for ClientEnvironment::getPlayerNames() & Client::getConnectedPlayerNames() * Drop isLocal() function from (Local)Player which is not needed anymore because of previous changes This change permits to cleanup shared client list which is very old code. ClientEnvironment doesn't use player list anymore, it only contains the local player, as addPlayer is only called from Client constructor client side. Clients are only CAO on client side, this cleanup permit to remove confusion about player list. --- src/client.cpp | 14 ++--- src/client.h | 5 +- src/content_cao.cpp | 8 +-- src/environment.cpp | 165 +++++++++++++++++++--------------------------------- src/environment.h | 32 ++++------ src/localplayer.h | 5 -- src/player.h | 16 ----- src/remoteplayer.h | 14 +++++ 8 files changed, 97 insertions(+), 162 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/client.cpp b/src/client.cpp index cd010e592..62bd274aa 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -257,7 +257,7 @@ Client::Client( m_localdb(NULL) { // Add local player - m_env.addPlayer(new LocalPlayer(this, playername)); + m_env.setLocalPlayer(new LocalPlayer(this, playername)); m_mapper = new Mapper(device, this); m_cache_save_interval = g_settings->getU16("server_map_save_interval"); @@ -1418,8 +1418,9 @@ Inventory* Client::getInventory(const InventoryLocation &loc) break; case InventoryLocation::PLAYER: { - LocalPlayer *player = m_env.getPlayer(loc.name.c_str()); - if(!player) + // Check if we are working with local player inventory + LocalPlayer *player = m_env.getLocalPlayer(); + if (!player || strcmp(player->getName(), loc.name.c_str()) != 0) return NULL; return &player->inventory; } @@ -1500,11 +1501,6 @@ ClientActiveObject * Client::getSelectedActiveObject( return NULL; } -std::list Client::getConnectedPlayerNames() -{ - return m_env.getPlayerNames(); -} - float Client::getAnimationTime() { return m_animation_time; @@ -1664,7 +1660,7 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur ClientEvent Client::getClientEvent() { ClientEvent event; - if(m_client_event_queue.size() == 0) { + if (m_client_event_queue.empty()) { event.type = CE_NONE; } else { diff --git a/src/client.h b/src/client.h index 72baac2d2..6d24c0b1d 100644 --- a/src/client.h +++ b/src/client.h @@ -452,7 +452,10 @@ public: core::line3d shootline_on_map ); - std::list getConnectedPlayerNames(); + const std::list &getConnectedPlayerNames() + { + return m_env.getPlayerNames(); + } float getAnimationTime(); diff --git a/src/content_cao.cpp b/src/content_cao.cpp index a53768149..00e17f328 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -653,10 +653,10 @@ void GenericCAO::initialize(const std::string &data) pos_translator.init(m_position); updateNodePos(); - if(m_is_player) - { - LocalPlayer *player = m_env->getPlayer(m_name.c_str()); - if (player && player->isLocal()) { + if (m_is_player) { + // Check if it's the current player + LocalPlayer *player = m_env->getLocalPlayer(); + if (player && strcmp(player->getName(), m_name.c_str()) == 0) { m_is_local_player = true; m_is_visible = false; LocalPlayer* localplayer = player; diff --git a/src/environment.cpp b/src/environment.cpp index ff43ac516..ceaa01d7a 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -69,62 +69,6 @@ Environment::Environment(): Environment::~Environment() { - // Deallocate players - for(std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - delete (*i); - } -} - -void Environment::addPlayer(Player *player) -{ - DSTACK(FUNCTION_NAME); - /* - Check that peer_ids are unique. - Also check that names are unique. - Exception: there can be multiple players with peer_id=0 - */ - // If peer id is non-zero, it has to be unique. - if(player->peer_id != 0) - FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique"); - // Name has to be unique. - FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique"); - // Add. - m_players.push_back(player); -} - -void Environment::removePlayer(Player* player) -{ - for (std::vector::iterator it = m_players.begin(); - it != m_players.end(); ++it) { - if ((*it) == player) { - delete *it; - m_players.erase(it); - return; - } - } -} - -Player *Environment::getPlayer(u16 peer_id) -{ - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - Player *player = *i; - if (player->peer_id == peer_id) - return player; - } - return NULL; -} - -Player *Environment::getPlayer(const char *name) -{ - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - Player *player = *i; - if(strcmp(player->getName(), name) == 0) - return player; - } - return NULL; } u32 Environment::getDayNightRatio() @@ -545,10 +489,16 @@ ServerEnvironment::~ServerEnvironment() m_map->drop(); // Delete ActiveBlockModifiers - for(std::vector::iterator + for (std::vector::iterator i = m_abms.begin(); i != m_abms.end(); ++i){ delete i->abm; } + + // Deallocate players + for (std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { + delete (*i); + } } Map & ServerEnvironment::getMap() @@ -563,12 +513,53 @@ ServerMap & ServerEnvironment::getServerMap() RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id) { - return dynamic_cast(Environment::getPlayer(peer_id)); + for (std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { + RemotePlayer *player = *i; + if (player->peer_id == peer_id) + return player; + } + return NULL; } RemotePlayer *ServerEnvironment::getPlayer(const char* name) { - return dynamic_cast(Environment::getPlayer(name)); + for (std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { + RemotePlayer *player = *i; + if (strcmp(player->getName(), name) == 0) + return player; + } + return NULL; +} + +void ServerEnvironment::addPlayer(RemotePlayer *player) +{ + DSTACK(FUNCTION_NAME); + /* + Check that peer_ids are unique. + Also check that names are unique. + Exception: there can be multiple players with peer_id=0 + */ + // If peer id is non-zero, it has to be unique. + if (player->peer_id != 0) + FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique"); + // Name has to be unique. + FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique"); + // Add. + m_players.push_back(player); +} + +void ServerEnvironment::removePlayer(RemotePlayer *player) +{ + for (std::vector::iterator it = m_players.begin(); + it != m_players.end(); ++it) { + if ((*it) == player) { + delete *it; + m_players.erase(it); + return; + } + } } bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p) @@ -601,7 +592,7 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, const std::string &str_reason, bool reconnect) { - for (std::vector::iterator it = m_players.begin(); + for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { RemotePlayer *player = dynamic_cast(*it); ((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id, @@ -614,7 +605,7 @@ void ServerEnvironment::saveLoadedPlayers() std::string players_path = m_path_world + DIR_DELIM "players"; fs::CreateDir(players_path); - for (std::vector::iterator it = m_players.begin(); + for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { RemotePlayer *player = static_cast(*it); @@ -1253,7 +1244,7 @@ void ServerEnvironment::step(float dtime) */ { ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); - for (std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { RemotePlayer *player = dynamic_cast(*i); assert(player); @@ -1276,7 +1267,7 @@ void ServerEnvironment::step(float dtime) Get player block positions */ std::vector players_blockpos; - for (std::vector::iterator i = m_players.begin(); + for (std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { RemotePlayer *player = dynamic_cast(*i); assert(player); @@ -2255,6 +2246,7 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr, ITextureSource *texturesource, IGameDef *gamedef, IrrlichtDevice *irr): m_map(map), + m_local_player(NULL), m_smgr(smgr), m_texturesource(texturesource), m_gamedef(gamedef), @@ -2291,37 +2283,16 @@ ClientMap & ClientEnvironment::getClientMap() return *m_map; } -LocalPlayer *ClientEnvironment::getPlayer(const u16 peer_id) -{ - return dynamic_cast(Environment::getPlayer(peer_id)); -} - -LocalPlayer *ClientEnvironment::getPlayer(const char* name) -{ - return dynamic_cast(Environment::getPlayer(name)); -} - -void ClientEnvironment::addPlayer(LocalPlayer *player) +void ClientEnvironment::setLocalPlayer(LocalPlayer *player) { DSTACK(FUNCTION_NAME); /* It is a failure if already is a local player */ - FATAL_ERROR_IF(getLocalPlayer() != NULL, - "Player is local but there is already a local player"); - - Environment::addPlayer(player); -} + FATAL_ERROR_IF(m_local_player != NULL, + "Local player already allocated"); -LocalPlayer *ClientEnvironment::getLocalPlayer() -{ - for(std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - Player *player = *i; - if (player->isLocal()) - return (LocalPlayer*)player; - } - return NULL; + m_local_player = player; } void ClientEnvironment::step(float dtime) @@ -2558,24 +2529,6 @@ void ClientEnvironment::step(float dtime) } } - /* - Stuff that can be done in an arbitarily large dtime - */ - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - Player *player = *i; - assert(player); - - /* - Handle non-local players - */ - if (!player->isLocal()) { - // Move - player->move(dtime, this, 100*BS); - - } - } - // Update lighting on local player (used for wield item) u32 day_night_ratio = getDayNightRatio(); { diff --git a/src/environment.h b/src/environment.h index f19708ad7..83ad69562 100644 --- a/src/environment.h +++ b/src/environment.h @@ -72,9 +72,6 @@ public: virtual Map & getMap() = 0; - virtual void addPlayer(Player *player); - void removePlayer(Player *player); - u32 getDayNightRatio(); // 0-23999 @@ -94,12 +91,6 @@ public: u32 m_added_objects; protected: - Player *getPlayer(u16 peer_id); - Player *getPlayer(const char *name); - - // peer_ids in here should be unique, except that there may be many 0s - std::vector m_players; - GenericAtomic m_time_of_day_speed; /* @@ -325,6 +316,8 @@ public: void saveLoadedPlayers(); void savePlayer(RemotePlayer *player); RemotePlayer *loadPlayer(const std::string &playername); + void addPlayer(RemotePlayer *player); + void removePlayer(RemotePlayer *player); /* Save and load time of day and game timer @@ -520,6 +513,9 @@ private: // Can raise to high values like 15s with eg. map generation mods. float m_max_lag_estimate; + // peer_ids in here should be unique, except that there may be many 0s + std::vector m_players; + // Particles IntervalLimiter m_particle_management_interval; UNORDERED_MAP m_particle_spawners; @@ -579,8 +575,8 @@ public: void step(f32 dtime); - virtual void addPlayer(LocalPlayer *player); - LocalPlayer * getLocalPlayer(); + virtual void setLocalPlayer(LocalPlayer *player); + LocalPlayer *getLocalPlayer() { return m_local_player; } /* ClientSimpleObjects @@ -630,21 +626,15 @@ public: u16 attachement_parent_ids[USHRT_MAX + 1]; - std::list getPlayerNames() - { return m_player_names; } - void addPlayerName(std::string name) - { m_player_names.push_back(name); } - void removePlayerName(std::string name) - { m_player_names.remove(name); } + const std::list &getPlayerNames() { return m_player_names; } + void addPlayerName(const std::string &name) { m_player_names.push_back(name); } + void removePlayerName(const std::string &name) { m_player_names.remove(name); } void updateCameraOffset(v3s16 camera_offset) { m_camera_offset = camera_offset; } v3s16 getCameraOffset() const { return m_camera_offset; } - - LocalPlayer *getPlayer(const u16 peer_id); - LocalPlayer *getPlayer(const char* name); - private: ClientMap *m_map; + LocalPlayer *m_local_player; scene::ISceneManager *m_smgr; ITextureSource *m_texturesource; IGameDef *m_gamedef; diff --git a/src/localplayer.h b/src/localplayer.h index 9d43128aa..eb727d7e3 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -38,11 +38,6 @@ public: LocalPlayer(Client *gamedef, const char *name); virtual ~LocalPlayer(); - bool isLocal() const - { - return true; - } - ClientActiveObject *parent; bool got_teleported; diff --git a/src/player.h b/src/player.h index 3c945b100..6ac5dfe65 100644 --- a/src/player.h +++ b/src/player.h @@ -183,22 +183,6 @@ public: return size; } - void setLocalAnimations(v2s32 frames[4], float frame_speed) - { - for (int i = 0; i < 4; i++) - local_animations[i] = frames[i]; - local_animation_speed = frame_speed; - } - - void getLocalAnimations(v2s32 *frames, float *frame_speed) - { - for (int i = 0; i < 4; i++) - frames[i] = local_animations[i]; - *frame_speed = local_animation_speed; - } - - virtual bool isLocal() const { return false; } - bool camera_barely_in_ceiling; v3f eye_offset_first; v3f eye_offset_third; diff --git a/src/remoteplayer.h b/src/remoteplayer.h index f6c70b0e9..1b1a90de3 100644 --- a/src/remoteplayer.h +++ b/src/remoteplayer.h @@ -142,6 +142,20 @@ public: Player::setYaw(yaw); } + void setLocalAnimations(v2s32 frames[4], float frame_speed) + { + for (int i = 0; i < 4; i++) + local_animations[i] = frames[i]; + local_animation_speed = frame_speed; + } + + void getLocalAnimations(v2s32 *frames, float *frame_speed) + { + for (int i = 0; i < 4; i++) + frames[i] = local_animations[i]; + *frame_speed = local_animation_speed; + } + u16 protocol_version; private: /* -- cgit v1.2.3 From c9e7a27eeb628be78a835abadf8afe1177eb90c5 Mon Sep 17 00:00:00 2001 From: raymoo Date: Thu, 4 Aug 2016 13:09:21 -0700 Subject: Attached particle spawners --- doc/lua_api.txt | 2 + src/client.h | 1 + src/content_sao.cpp | 10 ++++- src/environment.cpp | 24 +++++++++++ src/environment.h | 4 +- src/network/clientpackethandler.cpp | 3 ++ src/particles.cpp | 81 ++++++++++++++++++++++++------------- src/particles.h | 3 +- src/script/lua_api/l_particles.cpp | 11 +++++ src/server.cpp | 17 ++++++-- src/server.h | 2 + src/serverobject.cpp | 1 - src/serverobject.h | 10 +++++ 13 files changed, 132 insertions(+), 37 deletions(-) (limited to 'src/environment.cpp') diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 6bdcd4fe4..a1d598e7d 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -4120,6 +4120,8 @@ The Biome API is still in an experimental phase and subject to change. collision_removal = false, -- ^ collision_removal: if true then particle is removed when it collides, -- ^ requires collisiondetection = true to have any effect + attached = ObjectRef, + -- ^ attached: if defined, makes particle positions relative to this object. vertical = false, -- ^ vertical: if true faces player using y axis only texture = "image.png", diff --git a/src/client.h b/src/client.h index 6d24c0b1d..9f5bda059 100644 --- a/src/client.h +++ b/src/client.h @@ -201,6 +201,7 @@ struct ClientEvent f32 maxsize; bool collisiondetection; bool collision_removal; + u16 attached_id; bool vertical; std::string *texture; u32 id; diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 5d3ed38bc..375a43c90 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -156,6 +156,11 @@ LuaEntitySAO::~LuaEntitySAO() if(m_registered){ m_env->getScriptIface()->luaentity_Remove(m_id); } + + for (UNORDERED_SET::iterator it = m_attached_particle_spawners.begin(); + it != m_attached_particle_spawners.end(); ++it) { + m_env->deleteParticleSpawner(*it, false); + } } void LuaEntitySAO::addedToEnvironment(u32 dtime_s) @@ -817,7 +822,6 @@ PlayerSAO::~PlayerSAO() { if(m_inventory != &m_player->inventory) delete m_inventory; - } std::string PlayerSAO::getDescription() @@ -844,6 +848,10 @@ void PlayerSAO::removingFromEnvironment() m_player->peer_id = 0; m_env->savePlayer(m_player); m_env->removePlayer(m_player); + for (UNORDERED_SET::iterator it = m_attached_particle_spawners.begin(); + it != m_attached_particle_spawners.end(); ++it) { + m_env->deleteParticleSpawner(*it, false); + } } } diff --git a/src/environment.cpp b/src/environment.cpp index ceaa01d7a..ceaf40d89 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1518,6 +1518,30 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) return id; } +u32 ServerEnvironment::addParticleSpawner(float exptime, u16 attached_id) +{ + u32 id = addParticleSpawner(exptime); + m_particle_spawner_attachments[id] = attached_id; + if (ServerActiveObject *obj = getActiveObject(attached_id)) { + obj->attachParticleSpawner(id); + } + return id; +} + +void ServerEnvironment::deleteParticleSpawner(u32 id, bool remove_from_object) +{ + m_particle_spawners.erase(id); + UNORDERED_MAP::iterator it = m_particle_spawner_attachments.find(id); + if (it != m_particle_spawner_attachments.end()) { + u16 obj_id = (*it).second; + ServerActiveObject *sao = getActiveObject(obj_id); + if (sao != NULL && remove_from_object) { + sao->detachParticleSpawner(id); + } + m_particle_spawner_attachments.erase(id); + } +} + ServerActiveObject* ServerEnvironment::getActiveObject(u16 id) { ActiveObjectMap::iterator n = m_active_objects.find(id); diff --git a/src/environment.h b/src/environment.h index 83ad69562..3f3c1cf2c 100644 --- a/src/environment.h +++ b/src/environment.h @@ -329,7 +329,8 @@ public: void loadDefaultMeta(); u32 addParticleSpawner(float exptime); - void deleteParticleSpawner(u32 id) { m_particle_spawners.erase(id); } + u32 addParticleSpawner(float exptime, u16 attached_id); + void deleteParticleSpawner(u32 id, bool remove_from_object = true); /* External ActiveObject interface @@ -519,6 +520,7 @@ private: // Particles IntervalLimiter m_particle_management_interval; UNORDERED_MAP m_particle_spawners; + UNORDERED_MAP m_particle_spawner_attachments; }; #ifndef SERVER diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index b39356e92..090741f9f 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -944,9 +944,11 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) bool vertical = false; bool collision_removal = false; + u16 attached_id = 0; try { *pkt >> vertical; *pkt >> collision_removal; + *pkt >> attached_id; } catch (...) {} @@ -966,6 +968,7 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) event.add_particlespawner.maxsize = maxsize; event.add_particlespawner.collisiondetection = collisiondetection; event.add_particlespawner.collision_removal = collision_removal; + event.add_particlespawner.attached_id = attached_id; event.add_particlespawner.vertical = vertical; event.add_particlespawner.texture = new std::string(texture); event.add_particlespawner.id = id; diff --git a/src/particles.cpp b/src/particles.cpp index ccca691d1..2efee6ada 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -213,7 +213,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, u16 amount, float time, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, - bool collisiondetection, bool collision_removal, bool vertical, + bool collisiondetection, bool collision_removal, u16 attached_id, bool vertical, video::ITexture *texture, u32 id, ParticleManager *p_manager) : m_particlemanager(p_manager) { @@ -234,6 +234,7 @@ ParticleSpawner::ParticleSpawner(IGameDef* gamedef, scene::ISceneManager *smgr, m_maxsize = maxsize; m_collisiondetection = collisiondetection; m_collision_removal = collision_removal; + m_attached_id = attached_id; m_vertical = vertical; m_texture = texture; m_time = 0; @@ -251,6 +252,15 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) { m_time += dtime; + bool unloaded = false; + v3f attached_offset = v3f(0,0,0); + if (m_attached_id != 0) { + if (ClientActiveObject *attached = env->getActiveObject(m_attached_id)) + attached_offset = attached->getPosition() / BS; + else + unloaded = true; + } + if (m_spawntime != 0) // Spawner exists for a predefined timespan { for(std::vector::iterator i = m_spawntimes.begin(); @@ -260,33 +270,41 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) { m_amount--; - v3f pos = random_v3f(m_minpos, m_maxpos); - v3f vel = random_v3f(m_minvel, m_maxvel); - v3f acc = random_v3f(m_minacc, m_maxacc); - float exptime = rand()/(float)RAND_MAX - *(m_maxexptime-m_minexptime) - +m_minexptime; - float size = rand()/(float)RAND_MAX - *(m_maxsize-m_minsize) - +m_minsize; - - Particle* toadd = new Particle( - m_gamedef, - m_smgr, - m_player, - env, - pos, - vel, - acc, - exptime, - size, - m_collisiondetection, - m_collision_removal, - m_vertical, - m_texture, - v2f(0.0, 0.0), - v2f(1.0, 1.0)); - m_particlemanager->addParticle(toadd); + // Pretend to, but don't actually spawn a + // particle if it is attached to an unloaded + // object. + if (!unloaded) { + v3f pos = random_v3f(m_minpos, m_maxpos) + + attached_offset; + v3f vel = random_v3f(m_minvel, m_maxvel); + v3f acc = random_v3f(m_minacc, m_maxacc); + // Make relative to offest + pos += attached_offset; + float exptime = rand()/(float)RAND_MAX + *(m_maxexptime-m_minexptime) + +m_minexptime; + float size = rand()/(float)RAND_MAX + *(m_maxsize-m_minsize) + +m_minsize; + + Particle* toadd = new Particle( + m_gamedef, + m_smgr, + m_player, + env, + pos, + vel, + acc, + exptime, + size, + m_collisiondetection, + m_collision_removal, + m_vertical, + m_texture, + v2f(0.0, 0.0), + v2f(1.0, 1.0)); + m_particlemanager->addParticle(toadd); + } i = m_spawntimes.erase(i); } else @@ -297,11 +315,15 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env) } else // Spawner exists for an infinity timespan, spawn on a per-second base { + // Skip this step if attached to an unloaded object + if (unloaded) + return; for (int i = 0; i <= m_amount; i++) { if (rand()/(float)RAND_MAX < dtime) { - v3f pos = random_v3f(m_minpos, m_maxpos); + v3f pos = random_v3f(m_minpos, m_maxpos) + + attached_offset; v3f vel = random_v3f(m_minvel, m_maxvel); v3f acc = random_v3f(m_minacc, m_maxacc); float exptime = rand()/(float)RAND_MAX @@ -453,6 +475,7 @@ void ParticleManager::handleParticleEvent(ClientEvent *event, IGameDef *gamedef, event->add_particlespawner.maxsize, event->add_particlespawner.collisiondetection, event->add_particlespawner.collision_removal, + event->add_particlespawner.attached_id, event->add_particlespawner.vertical, texture, event->add_particlespawner.id, diff --git a/src/particles.h b/src/particles.h index bc3ca53b7..eb8c6665d 100644 --- a/src/particles.h +++ b/src/particles.h @@ -119,6 +119,7 @@ class ParticleSpawner float minsize, float maxsize, bool collisiondetection, bool collision_removal, + u16 attached_id, bool vertical, video::ITexture *texture, u32 id, @@ -154,7 +155,7 @@ class ParticleSpawner bool m_collisiondetection; bool m_collision_removal; bool m_vertical; - + u16 m_attached_id; }; /** diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index 263e35407..667ac7272 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -18,6 +18,7 @@ with this program; if not, write to the Free Software Foundation, Inc., */ #include "lua_api/l_particles.h" +#include "lua_api/l_object.h" #include "lua_api/l_internal.h" #include "common/c_converter.h" #include "server.h" @@ -138,6 +139,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) time= minexptime= maxexptime= minsize= maxsize= 1; bool collisiondetection, vertical, collision_removal; collisiondetection = vertical = collision_removal = false; + ServerActiveObject *attached = NULL; std::string texture = ""; std::string playername = ""; @@ -198,6 +200,14 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) "collisiondetection", collisiondetection); collision_removal = getboolfield_default(L, 1, "collision_removal", collision_removal); + + lua_getfield(L, 1, "attached"); + if (!lua_isnil(L, -1)) { + ObjectRef *ref = ObjectRef::checkobject(L, -1); + lua_pop(L, 1); + attached = ObjectRef::getobject(ref); + } + vertical = getboolfield_default(L, 1, "vertical", vertical); texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); @@ -211,6 +221,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) minsize, maxsize, collisiondetection, collision_removal, + attached, vertical, texture, playername); lua_pushnumber(L, id); diff --git a/src/server.cpp b/src/server.cpp index a93c143c7..e67f37d56 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1678,7 +1678,7 @@ void Server::SendSpawnParticle(u16 peer_id, v3f pos, v3f velocity, v3f accelerat void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, u32 id) + u16 attached_id, bool vertical, const std::string &texture, u32 id) { DSTACK(FUNCTION_NAME); @@ -1692,6 +1692,7 @@ void Server::SendAddParticleSpawner(u16 peer_id, u16 amount, float spawntime, v3 pkt << id << vertical; pkt << collision_removal; + pkt << attached_id; if (peer_id != PEER_ID_INEXISTENT) { Send(&pkt); @@ -3156,7 +3157,7 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc, float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, - bool vertical, const std::string &texture, + ServerActiveObject *attached, bool vertical, const std::string &texture, const std::string &playername) { // m_env will be NULL if the server is initializing @@ -3171,11 +3172,19 @@ u32 Server::addParticleSpawner(u16 amount, float spawntime, peer_id = player->peer_id; } - u32 id = m_env->addParticleSpawner(spawntime); + u16 attached_id = attached ? attached->getId() : 0; + + u32 id; + if (attached_id == 0) + id = m_env->addParticleSpawner(spawntime); + else + id = m_env->addParticleSpawner(spawntime, attached_id); + SendAddParticleSpawner(peer_id, amount, spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, - collisiondetection, collision_removal, vertical, texture, id); + collisiondetection, collision_removal, attached_id, vertical, + texture, id); return id; } diff --git a/src/server.h b/src/server.h index 6ee61a0eb..5fc2a9133 100644 --- a/src/server.h +++ b/src/server.h @@ -258,6 +258,7 @@ public: float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, + ServerActiveObject *attached, bool vertical, const std::string &texture, const std::string &playername); @@ -434,6 +435,7 @@ private: float minexptime, float maxexptime, float minsize, float maxsize, bool collisiondetection, bool collision_removal, + u16 attached_id, bool vertical, const std::string &texture, u32 id); void SendDeleteParticleSpawner(u16 peer_id, u32 id); diff --git a/src/serverobject.cpp b/src/serverobject.cpp index 236d7e8dc..191247829 100644 --- a/src/serverobject.cpp +++ b/src/serverobject.cpp @@ -98,4 +98,3 @@ bool ServerActiveObject::setWieldedItem(const ItemStack &item) } return false; } - diff --git a/src/serverobject.h b/src/serverobject.h index cfe2b6bcc..63650e3be 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -188,6 +188,15 @@ public: { return 0; } virtual ItemStack getWieldedItem() const; virtual bool setWieldedItem(const ItemStack &item); + inline void attachParticleSpawner(u32 id) + { + m_attached_particle_spawners.insert(id); + } + inline void detachParticleSpawner(u32 id) + { + m_attached_particle_spawners.erase(id); + } + /* Number of players which know about this object. Object won't be @@ -242,6 +251,7 @@ protected: ServerEnvironment *m_env; v3f m_base_position; + UNORDERED_SET m_attached_particle_spawners; private: // Used for creating objects based on type -- cgit v1.2.3 From 63a3bb229a9ff288ab01e3e80c8477da5e84ee1e Mon Sep 17 00:00:00 2001 From: paramat Date: Wed, 19 Oct 2016 01:37:12 +0100 Subject: Changes to static object storage limit and error message Move static object storage force-delete message from errorstream to warningstream. Increase 'max objects per block' setting to 64. Add missing spaces in warning code. --- builtin/settingtypes.txt | 2 +- minetest.conf.example | 2 +- src/defaultsettings.cpp | 2 +- src/environment.cpp | 14 +++++++------- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/environment.cpp') diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 9b5f386e0..492d7c79d 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -817,7 +817,7 @@ max_clearobjects_extra_loaded_blocks (Max. clearobjects extra blocks) int 4096 server_unload_unused_data_timeout (Unload unused server data) int 29 # Maximum number of statically stored objects in a block. -max_objects_per_block (Maximum objects per block) int 49 +max_objects_per_block (Maximum objects per block) int 64 # See http://www.sqlite.org/pragma.html#pragma_synchronous sqlite_synchronous (Synchronous SQLite) enum 2 0,1,2 diff --git a/minetest.conf.example b/minetest.conf.example index e24d3a384..b7d705bac 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -1008,7 +1008,7 @@ # Maximum number of statically stored objects in a block. # type: int -# max_objects_per_block = 49 +# max_objects_per_block = 64 # See http://www.sqlite.org/pragma.html#pragma_synchronous # type: enum values: 0, 1, 2 diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 522aa8481..a64840a8f 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -283,7 +283,7 @@ void set_default_settings(Settings *settings) settings->setDefault("time_send_interval", "5"); settings->setDefault("time_speed", "72"); settings->setDefault("server_unload_unused_data_timeout", "29"); - settings->setDefault("max_objects_per_block", "49"); + settings->setDefault("max_objects_per_block", "64"); settings->setDefault("server_map_save_interval", "5.3"); settings->setDefault("chat_message_max_size", "500"); settings->setDefault("chat_message_limit_per_10sec", "8.0"); diff --git a/src/environment.cpp b/src/environment.cpp index ceaf40d89..ecda1b6a4 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -2175,13 +2175,13 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete) if(block) { - if(block->m_static_objects.m_stored.size() >= g_settings->getU16("max_objects_per_block")){ - errorstream<<"ServerEnv: Trying to store id="<getId() - <<" statically but block "<m_static_objects.m_stored.size() - <<" objects." - <<" Forcing delete."<m_static_objects.m_stored.size() >= g_settings->getU16("max_objects_per_block")) { + warningstream << "ServerEnv: Trying to store id = " << obj->getId() + << " statically but block " << PP(blockpos) + << " already contains " + << block->m_static_objects.m_stored.size() + << " objects." + << " Forcing delete." << std::endl; force_delete = true; } else { // If static counterpart already exists in target block, -- cgit v1.2.3 From 9d25242c5c1411d692254cf910345d51c9a24fa3 Mon Sep 17 00:00:00 2001 From: Ner'zhul Date: Sun, 30 Oct 2016 14:53:26 +0100 Subject: PlayerSAO/LocalPlayer refactor: (#4612) * Create UnitSAO, a common part between PlayerSAO & LuaEntitySAO * Move breath to PlayerSAO & LocalPlayer * Migrate m_yaw from (Remote)Player & LuaEntitySAO to UnitSAO * Migrate m_yaw from Player to LocalPlayer for client * Move some functions outside of player class to PlayerSAO/RemotePlayer or LocalPlayer depending on which class needs it * Move pitch to LocalPlayer & PlayerSAO * Move m_position from Player to LocalPlayer * Move camera_barely_in_ceiling to LocalPlayer as it's used only there * use PlayerSAO::m_base_position for Server side positions * remove a unused variable * ServerActiveObject::setPos now uses const ref * use ServerEnv::loadPlayer unconditionnaly as it creates RemotePlayer only if it's not already loaded * Move hp from Player to LocalPlayer * Move m_hp from LuaEntitySAO to UnitSAO * Use m_hp from PlayerSAO/UnitSAO instead of RemotePlayer --- src/clientiface.cpp | 14 ++-- src/collision.cpp | 2 +- src/content_sao.cpp | 155 +++++++++++++++++++----------------- src/content_sao.h | 62 +++++++++++---- src/environment.cpp | 35 ++++---- src/environment.h | 7 +- src/localplayer.cpp | 7 ++ src/localplayer.h | 41 ++++++++++ src/network/clientpackethandler.cpp | 1 - src/network/serverpackethandler.cpp | 55 +++++++------ src/player.cpp | 14 +--- src/player.h | 50 ------------ src/remoteplayer.cpp | 66 +++++++-------- src/remoteplayer.h | 35 +------- src/script/lua_api/l_object.cpp | 32 ++++---- src/server.cpp | 71 +++++++++-------- src/serverobject.h | 2 +- src/unittest/test_player.cpp | 39 +++++---- 18 files changed, 353 insertions(+), 335 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/clientiface.cpp b/src/clientiface.cpp index d78cf1c53..d2e3a6da0 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "environment.h" #include "map.h" #include "emerge.h" -#include "serverobject.h" // TODO this is used for cleanup of only +#include "content_sao.h" // TODO this is used for cleanup of only #include "log.h" #include "util/srp.h" @@ -82,6 +82,10 @@ void RemoteClient::GetNextBlocks ( if (player == NULL) return; + PlayerSAO *sao = player->getPlayerSAO(); + if (sao == NULL) + return; + // Won't send anything if already sending if(m_blocks_sending.size() >= g_settings->getU16 ("max_simultaneous_block_sends_per_client")) @@ -90,7 +94,7 @@ void RemoteClient::GetNextBlocks ( return; } - v3f playerpos = player->getPosition(); + v3f playerpos = sao->getBasePosition(); v3f playerspeed = player->getSpeed(); v3f playerspeeddir(0,0,0); if(playerspeed.getLength() > 1.0*BS) @@ -103,10 +107,10 @@ void RemoteClient::GetNextBlocks ( v3s16 center = getNodeBlockPos(center_nodepos); // Camera position and direction - v3f camera_pos = player->getEyePosition(); + v3f camera_pos = sao->getEyePosition(); v3f camera_dir = v3f(0,0,1); - camera_dir.rotateYZBy(player->getPitch()); - camera_dir.rotateXZBy(player->getYaw()); + camera_dir.rotateYZBy(sao->getPitch()); + camera_dir.rotateXZBy(sao->getYaw()); /*infostream<<"camera_dir=("<(env); - if (s_env != 0) { + if (s_env != NULL) { f32 distance = speed_f->getLength(); std::vector s_objects; s_env->getObjectsInsideRadius(s_objects, *pos_f, distance * 1.5); diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 375a43c90..23a064085 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -118,14 +118,12 @@ LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", ""); LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &name, const std::string &state): - ServerActiveObject(env, pos), + UnitSAO(env, pos), m_init_name(name), m_init_state(state), m_registered(false), - m_hp(-1), m_velocity(0,0,0), m_acceleration(0,0,0), - m_yaw(0), m_properties_sent(true), m_last_sent_yaw(0), m_last_sent_position(0,0,0), @@ -664,16 +662,6 @@ v3f LuaEntitySAO::getAcceleration() return m_acceleration; } -void LuaEntitySAO::setYaw(float yaw) -{ - m_yaw = yaw; -} - -float LuaEntitySAO::getYaw() -{ - return m_yaw; -} - void LuaEntitySAO::setTextureMod(const std::string &mod) { std::string str = gob_cmd_set_texture_mod(mod); @@ -762,10 +750,9 @@ bool LuaEntitySAO::collideWithObjects(){ // No prototype, PlayerSAO does not need to be deserialized -PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, - const std::set &privs, bool is_singleplayer): - ServerActiveObject(env_, v3f(0,0,0)), - m_player(player_), +PlayerSAO::PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer): + UnitSAO(env_, v3f(0,0,0)), + m_player(NULL), m_peer_id(peer_id_), m_inventory(NULL), m_damage(0), @@ -777,7 +764,6 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id m_position_not_sent(false), m_armor_groups_sent(false), m_properties_sent(true), - m_privs(privs), m_is_singleplayer(is_singleplayer), m_animation_speed(0), m_animation_blend(0), @@ -786,6 +772,8 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id m_bone_position_sent(false), m_attachment_parent_id(0), m_attachment_sent(false), + m_breath(PLAYER_MAX_BREATH), + m_pitch(0), // public m_physics_override_speed(1), m_physics_override_jump(1), @@ -794,10 +782,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id m_physics_override_sneak_glitch(true), m_physics_override_sent(false) { - assert(m_player); // pre-condition assert(m_peer_id != 0); // pre-condition - setBasePosition(m_player->getPosition()); - m_inventory = &m_player->inventory; m_armor_groups["fleshy"] = 100; m_prop.hp_max = PLAYER_MAX_HP; @@ -816,6 +801,7 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id // end of default appearance m_prop.is_visible = true; m_prop.makes_footstep_sound = true; + m_hp = PLAYER_MAX_HP; } PlayerSAO::~PlayerSAO() @@ -824,6 +810,14 @@ PlayerSAO::~PlayerSAO() delete m_inventory; } +void PlayerSAO::initialize(RemotePlayer *player, const std::set &privs) +{ + assert(player); + m_player = player; + m_privs = privs; + m_inventory = &m_player->inventory; +} + std::string PlayerSAO::getDescription() { return std::string("player ") + m_player->getName(); @@ -833,10 +827,10 @@ std::string PlayerSAO::getDescription() void PlayerSAO::addedToEnvironment(u32 dtime_s) { ServerActiveObject::addedToEnvironment(dtime_s); - ServerActiveObject::setBasePosition(m_player->getPosition()); + ServerActiveObject::setBasePosition(m_base_position); m_player->setPlayerSAO(this); m_player->peer_id = m_peer_id; - m_last_good_position = m_player->getPosition(); + m_last_good_position = m_base_position; } // Called before removing from environment @@ -844,9 +838,9 @@ void PlayerSAO::removingFromEnvironment() { ServerActiveObject::removingFromEnvironment(); if (m_player->getPlayerSAO() == this) { - m_player->setPlayerSAO(NULL); m_player->peer_id = 0; m_env->savePlayer(m_player); + m_player->setPlayerSAO(NULL); m_env->removePlayer(m_player); for (UNORDERED_SET::iterator it = m_attached_particle_spawners.begin(); it != m_attached_particle_spawners.end(); ++it) { @@ -870,8 +864,8 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) os<getName()); // name writeU8(os, 1); // is_player writeS16(os, getId()); //id - writeV3F1000(os, m_player->getPosition() + v3f(0,BS*1,0)); - writeF1000(os, m_player->getYaw()); + writeV3F1000(os, m_base_position + v3f(0,BS*1,0)); + writeF1000(os, m_yaw); writeS16(os, getHP()); writeU8(os, 6 + m_bone_position.size() + m_attachment_child_ids.size()); // number of messages stuffed in here @@ -900,8 +894,8 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) writeU8(os, 0); // version os<getName()); // name writeU8(os, 1); // is_player - writeV3F1000(os, m_player->getPosition() + v3f(0,BS*1,0)); - writeF1000(os, m_player->getYaw()); + writeV3F1000(os, m_base_position + v3f(0,BS*1,0)); + writeF1000(os, m_yaw); writeS16(os, getHP()); writeU8(os, 2); // number of messages stuffed in here os<setPosition(m_last_good_position); + setBasePosition(m_last_good_position); ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } @@ -969,14 +963,13 @@ void PlayerSAO::step(float dtime, bool send_recommended) // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally // If the object gets detached this comes into effect automatically from the last known origin - if(isAttached()) - { + if (isAttached()) { v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition(); m_last_good_position = pos; - m_player->setPosition(pos); + setBasePosition(pos); } - if(send_recommended == false) + if (!send_recommended) return; // If the object is attached client-side, don't waste bandwidth sending its position to clients @@ -988,12 +981,12 @@ void PlayerSAO::step(float dtime, bool send_recommended) if(isAttached()) // Just in case we ever do send attachment position too pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition(); else - pos = m_player->getPosition() + v3f(0,BS*1,0); + pos = m_base_position + v3f(0,BS*1,0); std::string str = gob_cmd_update_position( pos, v3f(0,0,0), v3f(0,0,0), - m_player->getYaw(), + m_yaw, true, false, update_interval @@ -1003,7 +996,7 @@ void PlayerSAO::step(float dtime, bool send_recommended) m_messages_out.push(aom); } - if(m_armor_groups_sent == false) { + if (!m_armor_groups_sent) { m_armor_groups_sent = true; std::string str = gob_cmd_update_armor_groups( m_armor_groups); @@ -1012,7 +1005,7 @@ void PlayerSAO::step(float dtime, bool send_recommended) m_messages_out.push(aom); } - if(m_physics_override_sent == false){ + if (!m_physics_override_sent) { m_physics_override_sent = true; std::string str = gob_cmd_update_physics_override(m_physics_override_speed, m_physics_override_jump, m_physics_override_gravity, @@ -1022,7 +1015,7 @@ void PlayerSAO::step(float dtime, bool send_recommended) m_messages_out.push(aom); } - if(m_animation_sent == false){ + if (!m_animation_sent) { m_animation_sent = true; std::string str = gob_cmd_update_animation( m_animation_range, m_animation_speed, m_animation_blend, m_animation_loop); @@ -1055,16 +1048,20 @@ void PlayerSAO::step(float dtime, bool send_recommended) void PlayerSAO::setBasePosition(const v3f &position) { + if (m_player && position != m_base_position) + m_player->setDirty(true); + // This needs to be ran for attachments too ServerActiveObject::setBasePosition(position); m_position_not_sent = true; } -void PlayerSAO::setPos(v3f pos) +void PlayerSAO::setPos(const v3f &pos) { if(isAttached()) return; - m_player->setPosition(pos); + + setBasePosition(pos); // Movement caused by this command is always valid m_last_good_position = pos; ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); @@ -1074,22 +1071,34 @@ void PlayerSAO::moveTo(v3f pos, bool continuous) { if(isAttached()) return; - m_player->setPosition(pos); + + setBasePosition(pos); // Movement caused by this command is always valid m_last_good_position = pos; ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } -void PlayerSAO::setYaw(float yaw) +void PlayerSAO::setYaw(const float yaw, bool send_data) { - m_player->setYaw(yaw); - ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); + if (m_player && yaw != m_yaw) + m_player->setDirty(true); + + UnitSAO::setYaw(yaw); + + // Datas should not be sent at player initialization + if (send_data) + ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } -void PlayerSAO::setPitch(float pitch) +void PlayerSAO::setPitch(const float pitch, bool send_data) { - m_player->setPitch(pitch); - ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); + if (m_player && pitch != m_pitch) + m_player->setDirty(true); + + m_pitch = pitch; + + if (send_data) + ((Server*)m_env->getGameDef())->SendMovePlayer(m_peer_id); } int PlayerSAO::punch(v3f dir, @@ -1153,13 +1162,8 @@ int PlayerSAO::punch(v3f dir, return hitparams.wear; } -void PlayerSAO::rightClick(ServerActiveObject *clicker) -{ -} - -s16 PlayerSAO::getHP() const +void PlayerSAO::rightClick(ServerActiveObject *) { - return m_player->hp; } s16 PlayerSAO::readDamage() @@ -1169,12 +1173,16 @@ s16 PlayerSAO::readDamage() return damage; } -void PlayerSAO::setHP(s16 hp) +void PlayerSAO::setHP(s16 hp, bool direct) { - s16 oldhp = m_player->hp; + if (direct) { + m_hp = hp; + return; + } + + s16 oldhp = m_hp; - s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, - hp - oldhp); + s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp); if (hp_change == 0) return; hp = oldhp + hp_change; @@ -1184,11 +1192,11 @@ void PlayerSAO::setHP(s16 hp) else if (hp > PLAYER_MAX_HP) hp = PLAYER_MAX_HP; - if(hp < oldhp && g_settings->getBool("enable_damage") == false) { + if (hp < oldhp && !g_settings->getBool("enable_damage")) { return; } - m_player->hp = hp; + m_hp = hp; if (oldhp > hp) m_damage += (oldhp - hp); @@ -1198,14 +1206,12 @@ void PlayerSAO::setHP(s16 hp) m_properties_sent = false; } -u16 PlayerSAO::getBreath() const +void PlayerSAO::setBreath(const u16 breath) { - return m_player->getBreath(); -} + if (m_player && breath != m_breath) + m_player->setDirty(true); -void PlayerSAO::setBreath(u16 breath) -{ - m_player->setBreath(breath); + m_breath = breath; } void PlayerSAO::setArmorGroups(const ItemGroupList &armor_groups) @@ -1355,7 +1361,7 @@ bool PlayerSAO::checkMovementCheat() { if (isAttached() || m_is_singleplayer || g_settings->getBool("disable_anticheat")) { - m_last_good_position = m_player->getPosition(); + m_last_good_position = m_base_position; return false; } @@ -1382,7 +1388,7 @@ bool PlayerSAO::checkMovementCheat() // Tolerance. The lag pool does this a bit. //player_max_speed *= 2.5; - v3f diff = (m_player->getPosition() - m_last_good_position); + v3f diff = (m_base_position - m_last_good_position); float d_vert = diff.Y; diff.Y = 0; float d_horiz = diff.getLength(); @@ -1392,27 +1398,26 @@ bool PlayerSAO::checkMovementCheat() required_time = d_vert / player_max_speed; // Moving upwards if (m_move_pool.grab(required_time)) { - m_last_good_position = m_player->getPosition(); + m_last_good_position = m_base_position; } else { actionstream << "Player " << m_player->getName() << " moved too fast; resetting position" << std::endl; - m_player->setPosition(m_last_good_position); + setBasePosition(m_last_good_position); cheated = true; } return cheated; } -bool PlayerSAO::getCollisionBox(aabb3f *toset) { - //update collision box - *toset = m_player->getCollisionbox(); - +bool PlayerSAO::getCollisionBox(aabb3f *toset) +{ + *toset = aabb3f(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30); toset->MinEdge += m_base_position; toset->MaxEdge += m_base_position; - return true; } -bool PlayerSAO::collideWithObjects(){ +bool PlayerSAO::collideWithObjects() +{ return true; } diff --git a/src/content_sao.h b/src/content_sao.h index 76a3a37da..4ea6277ff 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -23,12 +23,35 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serverobject.h" #include "itemgroup.h" #include "object_properties.h" +#include "constants.h" + +class UnitSAO: public ServerActiveObject +{ +public: + UnitSAO(ServerEnvironment *env, v3f pos): + ServerActiveObject(env, pos), + m_hp(-1), m_yaw(0) {} + virtual ~UnitSAO() {} + + virtual void setYaw(const float yaw) { m_yaw = yaw; } + float getYaw() const { return m_yaw; }; + f32 getRadYaw() const { return m_yaw * core::DEGTORAD; } + // Deprecated + f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } + + s16 getHP() const { return m_hp; } + // Use a function, if isDead can be defined by other conditions + bool isDead() const { return m_hp == 0; } +protected: + s16 m_hp; + float m_yaw; +}; /* LuaEntitySAO needs some internals exposed. */ -class LuaEntitySAO : public ServerActiveObject +class LuaEntitySAO : public UnitSAO { public: LuaEntitySAO(ServerEnvironment *env, v3f pos, @@ -74,8 +97,7 @@ public: v3f getVelocity(); void setAcceleration(v3f acceleration); v3f getAcceleration(); - void setYaw(float yaw); - float getYaw(); + void setTextureMod(const std::string &mod); void setSprite(v2s16 p, int num_frames, float framelength, bool select_horiz_by_yawpitch); @@ -91,10 +113,9 @@ private: bool m_registered; struct ObjectProperties m_prop; - s16 m_hp; v3f m_velocity; v3f m_acceleration; - float m_yaw; + ItemGroupList m_armor_groups; bool m_properties_sent; @@ -158,11 +179,10 @@ public: class RemotePlayer; -class PlayerSAO : public ServerActiveObject +class PlayerSAO : public UnitSAO { public: - PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, - const std::set &privs, bool is_singleplayer); + PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer); ~PlayerSAO(); ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_PLAYER; } @@ -182,10 +202,14 @@ public: bool isAttached(); void step(float dtime, bool send_recommended); void setBasePosition(const v3f &position); - void setPos(v3f pos); + void setPos(const v3f &pos); void moveTo(v3f pos, bool continuous); - void setYaw(float); - void setPitch(float); + void setYaw(const float yaw, bool send_data = true); + void setPitch(const float pitch, bool send_data = true); + f32 getPitch() const { return m_pitch; } + f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } + // Deprecated + f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } /* Interaction interface @@ -196,11 +220,10 @@ public: ServerActiveObject *puncher, float time_from_last_punch); void rightClick(ServerActiveObject *clicker); - s16 getHP() const; - void setHP(s16 hp); + void setHP(s16 hp, bool direct = false); s16 readDamage(); - u16 getBreath() const; - void setBreath(u16 breath); + u16 getBreath() const { return m_breath; } + void setBreath(const u16 breath); void setArmorGroups(const ItemGroupList &armor_groups); ItemGroupList getArmorGroups(); void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop); @@ -283,6 +306,11 @@ public: bool getCollisionBox(aabb3f *toset); bool collideWithObjects(); + void initialize(RemotePlayer *player, const std::set &privs); + + v3f getEyePosition() const { return m_base_position + getEyeOffset(); } + v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); } + private: std::string getPropertyPacket(); @@ -326,8 +354,8 @@ private: v3f m_attachment_position; v3f m_attachment_rotation; bool m_attachment_sent; - - + u16 m_breath; + f32 m_pitch; public: float m_physics_override_speed; float m_physics_override_jump; diff --git a/src/environment.cpp b/src/environment.cpp index ecda1b6a4..13c64b37c 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -608,9 +608,8 @@ void ServerEnvironment::saveLoadedPlayers() for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { - RemotePlayer *player = static_cast(*it); - if (player->checkModified()) { - player->save(players_path, m_gamedef); + if ((*it)->checkModified()) { + (*it)->save(players_path, m_gamedef); } } } @@ -623,7 +622,7 @@ void ServerEnvironment::savePlayer(RemotePlayer *player) player->save(players_path, m_gamedef); } -RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) +RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername, PlayerSAO *sao) { bool newplayer = false; bool found = false; @@ -641,7 +640,8 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) std::ifstream is(path.c_str(), std::ios_base::binary); if (!is.good()) continue; - player->deSerialize(is, path); + + player->deSerialize(is, path, sao); is.close(); if (player->getName() == playername) { @@ -657,11 +657,13 @@ RemotePlayer *ServerEnvironment::loadPlayer(const std::string &playername) << " not found" << std::endl; if (newplayer) delete player; + return NULL; } - if (newplayer) + if (newplayer) { addPlayer(player); + } player->setModified(false); return player; } @@ -1271,12 +1273,16 @@ void ServerEnvironment::step(float dtime) i != m_players.end(); ++i) { RemotePlayer *player = dynamic_cast(*i); assert(player); + // Ignore disconnected players if (player->peer_id == 0) continue; + PlayerSAO *playersao = player->getPlayerSAO(); + assert(playersao); + v3s16 blockpos = getNodeBlockPos( - floatToInt(player->getPosition(), BS)); + floatToInt(playersao->getBasePosition(), BS)); players_blockpos.push_back(blockpos); } @@ -1584,7 +1590,7 @@ u16 ServerEnvironment::addActiveObject(ServerActiveObject *object) Finds out what new objects have been added to inside a radius around a position */ -void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, +void ServerEnvironment::getAddedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects) @@ -1594,7 +1600,6 @@ void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, if (player_radius_f < 0) player_radius_f = 0; - /* Go through the object list, - discard m_removed objects, @@ -1602,20 +1607,21 @@ void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, - discard objects that are found in current_objects. - add remaining objects to added_objects */ - for(ActiveObjectMap::iterator i = m_active_objects.begin(); + for (ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; // Get object ServerActiveObject *object = i->second; - if(object == NULL) + if (object == NULL) continue; // Discard if removed or deactivating if(object->m_removed || object->m_pending_deactivation) continue; - f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition()); + f32 distance_f = object->getBasePosition(). + getDistanceFrom(playersao->getBasePosition()); if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { // Discard if too far if (distance_f > player_radius_f && player_radius_f != 0) @@ -1637,7 +1643,7 @@ void ServerEnvironment::getAddedActiveObjects(RemotePlayer *player, s16 radius, Finds out what objects have been removed from inside a radius around a position */ -void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius, +void ServerEnvironment::getRemovedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects) @@ -1647,7 +1653,6 @@ void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius if (player_radius_f < 0) player_radius_f = 0; - /* Go through current_objects; object is removed if: - object is not found in m_active_objects (this is actually an @@ -1675,7 +1680,7 @@ void ServerEnvironment::getRemovedActiveObjects(RemotePlayer *player, s16 radius continue; } - f32 distance_f = object->getBasePosition().getDistanceFrom(player->getPosition()); + f32 distance_f = object->getBasePosition().getDistanceFrom(playersao->getBasePosition()); if (object->getType() == ACTIVEOBJECT_TYPE_PLAYER) { if (distance_f <= player_radius_f || player_radius_f == 0) continue; diff --git a/src/environment.h b/src/environment.h index 3f3c1cf2c..4bee40e57 100644 --- a/src/environment.h +++ b/src/environment.h @@ -54,6 +54,7 @@ class ClientMap; class GameScripting; class Player; class RemotePlayer; +class PlayerSAO; class Environment { @@ -315,7 +316,7 @@ public: // Save players void saveLoadedPlayers(); void savePlayer(RemotePlayer *player); - RemotePlayer *loadPlayer(const std::string &playername); + RemotePlayer *loadPlayer(const std::string &playername, PlayerSAO *sao); void addPlayer(RemotePlayer *player); void removePlayer(RemotePlayer *player); @@ -362,7 +363,7 @@ public: Find out what new objects have been added to inside a radius around a position */ - void getAddedActiveObjects(RemotePlayer *player, s16 radius, + void getAddedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &added_objects); @@ -371,7 +372,7 @@ public: Find out what new objects have been removed from inside a radius around a position */ - void getRemovedActiveObjects(RemotePlayer *player, s16 radius, + void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, std::set ¤t_objects, std::queue &removed_objects); diff --git a/src/localplayer.cpp b/src/localplayer.cpp index bc242a59d..71efb2343 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., LocalPlayer::LocalPlayer(Client *gamedef, const char *name): Player(name, gamedef->idef()), parent(0), + hp(PLAYER_MAX_HP), got_teleported(false), isAttached(false), touching_ground(false), @@ -62,6 +63,7 @@ LocalPlayer::LocalPlayer(Client *gamedef, const char *name): light_color(255,255,255,255), hurt_tilt_timer(0.0f), hurt_tilt_strength(0.0f), + m_position(0,0,0), m_sneak_node(32767,32767,32767), m_sneak_node_exists(false), m_need_to_get_new_sneak_node(true), @@ -69,6 +71,11 @@ LocalPlayer::LocalPlayer(Client *gamedef, const char *name): m_old_node_below(32767,32767,32767), m_old_node_below_type("air"), m_can_jump(false), + m_breath(PLAYER_MAX_BREATH), + m_yaw(0), + m_pitch(0), + camera_barely_in_ceiling(false), + m_collisionbox(-BS * 0.30, 0.0, -BS * 0.30, BS * 0.30, BS * 1.75, BS * 0.30), m_cao(NULL), m_gamedef(gamedef) { diff --git a/src/localplayer.h b/src/localplayer.h index eb727d7e3..749f8f8ce 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -40,6 +40,7 @@ public: ClientActiveObject *parent; + u16 hp; bool got_teleported; bool isAttached; bool touching_ground; @@ -99,10 +100,45 @@ public: u32 maxHudId() const { return hud.size(); } + u16 getBreath() const { return m_breath; } + void setBreath(u16 breath) { m_breath = breath; } + + v3s16 getLightPosition() const + { + return floatToInt(m_position + v3f(0,BS+BS/2,0), BS); + } + + void setYaw(f32 yaw) + { + m_yaw = yaw; + } + + f32 getYaw() const { return m_yaw; } + + void setPitch(f32 pitch) + { + m_pitch = pitch; + } + + f32 getPitch() const { return m_pitch; } + + void setPosition(const v3f &position) + { + m_position = position; + } + + v3f getPosition() const { return m_position; } + v3f getEyePosition() const { return m_position + getEyeOffset(); } + v3f getEyeOffset() const + { + float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f; + return v3f(0, BS * eye_height, 0); + } private: void accelerateHorizontal(const v3f &target_speed, const f32 max_increase); void accelerateVertical(const v3f &target_speed, const f32 max_increase); + v3f m_position; // This is used for determining the sneaking range v3s16 m_sneak_node; // Whether the player is allowed to sneak @@ -117,6 +153,11 @@ private: v3s16 m_old_node_below; std::string m_old_node_below_type; bool m_can_jump; + u16 m_breath; + f32 m_yaw; + f32 m_pitch; + bool camera_barely_in_ceiling; + aabb3f m_collisionbox; GenericCAO* m_cao; Client *m_gamedef; diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 090741f9f..411982f69 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -634,7 +634,6 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt) m_media_downloader->addFile(name, sha1_raw); } - std::vector remote_media; try { std::string str; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 554025747..5e70b4c6c 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -809,13 +809,6 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) return; } - // If player is dead we don't care of this packet - if (player->isDead()) { - verbosestream << "TOSERVER_PLAYERPOS: " << player->getName() - << " is dead. Ignoring packet"; - return; - } - PlayerSAO *playersao = player->getPlayerSAO(); if (playersao == NULL) { errorstream << "Server::ProcessData(): Canceling: " @@ -825,10 +818,17 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt) return; } - player->setPosition(position); + // If player is dead we don't care of this packet + if (playersao->isDead()) { + verbosestream << "TOSERVER_PLAYERPOS: " << player->getName() + << " is dead. Ignoring packet"; + return; + } + + playersao->setBasePosition(position); player->setSpeed(speed); - player->setPitch(pitch); - player->setYaw(yaw); + playersao->setPitch(pitch, false); + playersao->setYaw(yaw, false); player->keyPressed = keyPressed; player->control.up = (keyPressed & 1); player->control.down = (keyPressed & 2); @@ -1100,7 +1100,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt) if (g_settings->getBool("enable_damage")) { actionstream << player->getName() << " damaged by " - << (int)damage << " hp at " << PP(player->getPosition() / BS) + << (int)damage << " hp at " << PP(playersao->getBasePosition() / BS) << std::endl; playersao->setHP(playersao->getHP() - damage); @@ -1124,16 +1124,6 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) return; } - /* - * If player is dead, we don't need to update the breath - * He is dead ! - */ - if (player->isDead()) { - verbosestream << "TOSERVER_BREATH: " << player->getName() - << " is dead. Ignoring packet"; - return; - } - PlayerSAO *playersao = player->getPlayerSAO(); if (playersao == NULL) { @@ -1144,6 +1134,16 @@ void Server::handleCommand_Breath(NetworkPacket* pkt) return; } + /* + * If player is dead, we don't need to update the breath + * He is dead ! + */ + if (playersao->isDead()) { + verbosestream << "TOSERVER_BREATH: " << player->getName() + << " is dead. Ignoring packet"; + return; + } + playersao->setBreath(breath); SendPlayerBreath(pkt->getPeerId()); } @@ -1264,13 +1264,16 @@ void Server::handleCommand_Respawn(NetworkPacket* pkt) return; } - if (!player->isDead()) + PlayerSAO *playersao = player->getPlayerSAO(); + assert(playersao); + + if (!playersao->isDead()) return; RespawnPlayer(pkt->getPeerId()); actionstream << player->getName() << " respawns at " - << PP(player->getPosition()/BS) << std::endl; + << PP(playersao->getBasePosition() / BS) << std::endl; // ActiveObject is added to environment in AsyncRunStep after // the previous addition has been successfully removed @@ -1322,9 +1325,9 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) return; } - if (player->isDead()) { + if (playersao->isDead()) { verbosestream << "TOSERVER_INTERACT: " << player->getName() - << " is dead. Ignoring packet"; + << " is dead. Ignoring packet"; return; } @@ -1455,7 +1458,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt) ToolCapabilities toolcap = punchitem.getToolCapabilities(m_itemdef); v3f dir = (pointed_object->getBasePosition() - - (player->getPosition() + player->getEyeOffset()) + (playersao->getBasePosition() + playersao->getEyeOffset()) ).normalize(); float time_from_last_punch = playersao->resetTimeFromLastPunch(); diff --git a/src/player.cpp b/src/player.cpp index fa82a79f4..9c321d571 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -30,18 +30,11 @@ with this program; if not, write to the Free Software Foundation, Inc., Player::Player(const char *name, IItemDefManager *idef): - camera_barely_in_ceiling(false), inventory(idef), - hp(PLAYER_MAX_HP), peer_id(PEER_ID_INEXISTENT), keyPressed(0), // protected - m_breath(PLAYER_MAX_BREATH), - m_pitch(0), - m_yaw(0), - m_speed(0,0,0), - m_position(0,0,0), - m_collisionbox(-BS*0.30,0.0,-BS*0.30,BS*0.30,BS*1.75,BS*0.30) + m_speed(0,0,0) { strlcpy(m_name, name, PLAYERNAME_SIZE); @@ -90,11 +83,6 @@ Player::~Player() clearHud(); } -v3s16 Player::getLightPosition() const -{ - return floatToInt(m_position + v3f(0,BS+BS/2,0), BS); -} - u32 Player::addHud(HudElement *toadd) { MutexAutoLock lock(m_mutex); diff --git a/src/player.h b/src/player.h index 6ac5dfe65..5f9bb7ec9 100644 --- a/src/player.h +++ b/src/player.h @@ -129,49 +129,7 @@ public: m_speed = speed; } - v3f getPosition() - { - return m_position; - } - - v3s16 getLightPosition() const; - - v3f getEyeOffset() - { - float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f; - return v3f(0, BS * eye_height, 0); - } - - v3f getEyePosition() - { - return m_position + getEyeOffset(); - } - - virtual void setPosition(const v3f &position) - { - m_position = position; - } - - virtual void setPitch(f32 pitch) - { - m_pitch = pitch; - } - - virtual void setYaw(f32 yaw) - { - m_yaw = yaw; - } - - f32 getPitch() const { return m_pitch; } - f32 getYaw() const { return m_yaw; } - u16 getBreath() const { return m_breath; } - - virtual void setBreath(u16 breath) { m_breath = breath; } - - f32 getRadPitch() const { return m_pitch * core::DEGTORAD; } - f32 getRadYaw() const { return m_yaw * core::DEGTORAD; } const char *getName() const { return m_name; } - aabb3f getCollisionbox() const { return m_collisionbox; } u32 getFreeHudID() { @@ -183,7 +141,6 @@ public: return size; } - bool camera_barely_in_ceiling; v3f eye_offset_first; v3f eye_offset_third; @@ -205,8 +162,6 @@ public: v2s32 local_animations[4]; float local_animation_speed; - u16 hp; - u16 peer_id; std::string inventory_formspec; @@ -225,12 +180,7 @@ public: s32 hud_hotbar_itemcount; protected: char m_name[PLAYERNAME_SIZE]; - u16 m_breath; - f32 m_pitch; - f32 m_yaw; v3f m_speed; - v3f m_position; - aabb3f m_collisionbox; std::vector hud; private: diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp index f64d1d690..605346928 100644 --- a/src/remoteplayer.cpp +++ b/src/remoteplayer.cpp @@ -96,7 +96,7 @@ void RemotePlayer::save(std::string savedir, IGameDef *gamedef) infostream << "Failed to open " << path << std::endl; return; } - testplayer.deSerialize(is, path); + testplayer.deSerialize(is, path, NULL); is.close(); if (strcmp(testplayer.getName(), m_name) == 0) { // Open file and serialize @@ -115,37 +115,46 @@ void RemotePlayer::save(std::string savedir, IGameDef *gamedef) return; } -void RemotePlayer::deSerialize(std::istream &is, const std::string &playername) +void RemotePlayer::deSerialize(std::istream &is, const std::string &playername, + PlayerSAO *sao) { Settings args; if (!args.parseConfigLines(is, "PlayerArgsEnd")) { - throw SerializationError("PlayerArgsEnd of player " + - playername + " not found!"); + throw SerializationError("PlayerArgsEnd of player " + playername + " not found!"); } m_dirty = true; //args.getS32("version"); // Version field value not used std::string name = args.get("name"); strlcpy(m_name, name.c_str(), PLAYERNAME_SIZE); - setPitch(args.getFloat("pitch")); - setYaw(args.getFloat("yaw")); - setPosition(args.getV3F("position")); - try { - hp = args.getS32("hp"); - } catch(SettingNotFoundException &e) { - hp = PLAYER_MAX_HP; - } - try { - m_breath = args.getS32("breath"); - } catch(SettingNotFoundException &e) { - m_breath = PLAYER_MAX_BREATH; + if (sao) { + try { + sao->setHP(args.getS32("hp"), true); + } catch(SettingNotFoundException &e) { + sao->setHP(PLAYER_MAX_HP, true); + } + + try { + sao->setBasePosition(args.getV3F("position")); + } catch (SettingNotFoundException &e) {} + + try { + sao->setPitch(args.getFloat("pitch"), false); + } catch (SettingNotFoundException &e) {} + try { + sao->setYaw(args.getFloat("yaw"), false); + } catch (SettingNotFoundException &e) {} + + try { + sao->setBreath(args.getS32("breath")); + } catch (SettingNotFoundException &e) {} } inventory.deSerialize(is); - if(inventory.getList("craftpreview") == NULL) { + if (inventory.getList("craftpreview") == NULL) { // Convert players without craftpreview inventory.addList("craftpreview", 1); @@ -167,11 +176,14 @@ void RemotePlayer::serialize(std::ostream &os) 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.setS32("hp", hp); - args.setS32("breath", m_breath); + + if (m_sao) { + args.setS32("hp", m_sao->getHP()); + args.setV3F("position", m_sao->getBasePosition()); + args.setFloat("pitch", m_sao->getPitch()); + args.setFloat("yaw", m_sao->getYaw()); + args.setS32("breath", m_sao->getBreath()); + } args.writeLines(os); @@ -180,16 +192,6 @@ void RemotePlayer::serialize(std::ostream &os) inventory.serialize(os); } -void RemotePlayer::setPosition(const v3f &position) -{ - if (position != m_position) - m_dirty = true; - - Player::setPosition(position); - if(m_sao) - m_sao->setBasePosition(position); -} - const RemotePlayerChatResult RemotePlayer::canSendChatMessage() { // Rate limit messages diff --git a/src/remoteplayer.h b/src/remoteplayer.h index 1b1a90de3..61b5a23de 100644 --- a/src/remoteplayer.h +++ b/src/remoteplayer.h @@ -40,11 +40,10 @@ public: virtual ~RemotePlayer() {} void save(std::string savedir, IGameDef *gamedef); - void deSerialize(std::istream &is, const std::string &playername); + void deSerialize(std::istream &is, const std::string &playername, PlayerSAO *sao); PlayerSAO *getPlayerSAO() { return m_sao; } void setPlayerSAO(PlayerSAO *sao) { m_sao = sao; } - void setPosition(const v3f &position); const RemotePlayerChatResult canSendChatMessage(); @@ -67,9 +66,6 @@ public: *ratio = m_day_night_ratio; } - // Use a function, if isDead can be defined by other conditions - bool isDead() const { return hp == 0; } - void setHotbarImage(const std::string &name) { hud_hotbar_image = name; @@ -90,12 +86,6 @@ public: return hud_hotbar_selected_image; } - // Deprecated - f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; } - - // Deprecated - f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; } - void setSky(const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms) { @@ -121,27 +111,6 @@ public: inventory.setModified(x); } - virtual void setBreath(u16 breath) - { - if (breath != m_breath) - m_dirty = true; - Player::setBreath(breath); - } - - virtual void setPitch(f32 pitch) - { - if (pitch != m_pitch) - m_dirty = true; - Player::setPitch(pitch); - } - - virtual void setYaw(f32 yaw) - { - if (yaw != m_yaw) - m_dirty = true; - Player::setYaw(yaw); - } - void setLocalAnimations(v2s32 frames[4], float frame_speed) { for (int i = 0; i < 4; i++) @@ -156,6 +125,8 @@ public: *frame_speed = local_animation_speed; } + void setDirty(bool dirty) { m_dirty = true; } + u16 protocol_version; private: /* diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 23994181c..cf124f17c 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1013,11 +1013,11 @@ int ObjectRef::l_get_look_dir(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - float pitch = player->getRadPitchDep(); - float yaw = player->getRadYawDep(); + float pitch = co->getRadPitchDep(); + float yaw = co->getRadYawDep(); v3f v(cos(pitch)*cos(yaw), sin(pitch), cos(pitch)*sin(yaw)); push_v3f(L, v); return 1; @@ -1033,10 +1033,10 @@ int ObjectRef::l_get_look_pitch(lua_State *L) "Deprecated call to get_look_pitch, use get_look_vertical instead"); ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadPitchDep()); + lua_pushnumber(L, co->getRadPitchDep()); return 1; } @@ -1050,10 +1050,10 @@ int ObjectRef::l_get_look_yaw(lua_State *L) "Deprecated call to get_look_yaw, use get_look_horizontal instead"); ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadYawDep()); + lua_pushnumber(L, co->getRadYawDep()); return 1; } @@ -1062,10 +1062,10 @@ int ObjectRef::l_get_look_vertical(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadPitch()); + lua_pushnumber(L, co->getRadPitch()); return 1; } @@ -1074,10 +1074,10 @@ int ObjectRef::l_get_look_horizontal(lua_State *L) { NO_MAP_LOCK_REQUIRED; ObjectRef *ref = checkobject(L, 1); - RemotePlayer *player = getplayer(ref); - if (player == NULL) return 0; + PlayerSAO* co = getplayersao(ref); + if (co == NULL) return 0; // Do it - lua_pushnumber(L, player->getRadYaw()); + lua_pushnumber(L, co->getRadYaw()); return 1; } diff --git a/src/server.cpp b/src/server.cpp index e67f37d56..cd526ad77 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -701,11 +701,15 @@ void Server::AsyncRunStep(bool initial_step) continue; } + PlayerSAO *playersao = player->getPlayerSAO(); + if (playersao == NULL) + continue; + std::queue removed_objects; std::queue added_objects; - m_env->getRemovedActiveObjects(player, radius, player_radius, + m_env->getRemovedActiveObjects(playersao, radius, player_radius, client->m_known_objects, removed_objects); - m_env->getAddedActiveObjects(player, radius, player_radius, + m_env->getAddedActiveObjects(playersao, radius, player_radius, client->m_known_objects, added_objects); // Ignore if nothing happened @@ -1108,7 +1112,7 @@ PlayerSAO* Server::StageTwoClientInit(u16 peer_id) SendPlayerBreath(peer_id); // Show death screen if necessary - if (player->isDead()) + if (playersao->isDead()) SendDeathscreen(peer_id, false, v3f(0,0,0)); // Note things in chat if not in simple singleplayer mode @@ -1860,18 +1864,18 @@ void Server::SendMovePlayer(u16 peer_id) DSTACK(FUNCTION_NAME); RemotePlayer *player = m_env->getPlayer(peer_id); assert(player); + PlayerSAO *sao = player->getPlayerSAO(); + assert(sao); NetworkPacket pkt(TOCLIENT_MOVE_PLAYER, sizeof(v3f) + sizeof(f32) * 2, peer_id); - pkt << player->getPosition() << player->getPitch() << player->getYaw(); + pkt << sao->getBasePosition() << sao->getPitch() << sao->getYaw(); { - v3f pos = player->getPosition(); - f32 pitch = player->getPitch(); - f32 yaw = player->getYaw(); + v3f pos = sao->getBasePosition(); verbosestream << "Server: Sending TOCLIENT_MOVE_PLAYER" << " pos=(" << pos.X << "," << pos.Y << "," << pos.Z << ")" - << " pitch=" << pitch - << " yaw=" << yaw + << " pitch=" << sao->getPitch() + << " yaw=" << sao->getYaw() << std::endl; } @@ -1984,8 +1988,12 @@ s32 Server::playSound(const SimpleSoundSpec &spec, if (!player) continue; + PlayerSAO *sao = player->getPlayerSAO(); + if (!sao) + continue; + if (pos_exists) { - if(player->getPosition().getDistanceFrom(pos) > + if(sao->getBasePosition().getDistanceFrom(pos) > params.max_hear_distance) continue; } @@ -2044,14 +2052,17 @@ void Server::sendRemoveNode(v3s16 p, u16 ignore_id, pkt << p; std::vector clients = m_clients.getClientIDs(); - for(std::vector::iterator i = clients.begin(); - i != clients.end(); ++i) { + for (std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { if (far_players) { // Get player if (RemotePlayer *player = m_env->getPlayer(*i)) { + PlayerSAO *sao = player->getPlayerSAO(); + if (!sao) + continue; + // If player is far away, only set modified blocks not sent - v3f player_pos = player->getPosition(); - if(player_pos.getDistanceFrom(p_f) > maxd) { + v3f player_pos = sao->getBasePosition(); + if (player_pos.getDistanceFrom(p_f) > maxd) { far_players->push_back(*i); continue; } @@ -2071,14 +2082,16 @@ void Server::sendAddNode(v3s16 p, MapNode n, u16 ignore_id, v3f p_f = intToFloat(p, BS); std::vector clients = m_clients.getClientIDs(); - for(std::vector::iterator i = clients.begin(); - i != clients.end(); ++i) { - - if(far_players) { + for(std::vector::iterator i = clients.begin(); i != clients.end(); ++i) { + if (far_players) { // Get player if (RemotePlayer *player = m_env->getPlayer(*i)) { + PlayerSAO *sao = player->getPlayerSAO(); + if (!sao) + continue; + // If player is far away, only set modified blocks not sent - v3f player_pos = player->getPosition(); + v3f player_pos = sao->getBasePosition(); if(player_pos.getDistanceFrom(p_f) > maxd) { far_players->push_back(*i); continue; @@ -3426,10 +3439,9 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version return NULL; } - // Load player if it isn't already loaded - if (!player) { - player = m_env->loadPlayer(name); - } + // Create a new player active object + PlayerSAO *playersao = new PlayerSAO(m_env, peer_id, isSingleplayer()); + player = m_env->loadPlayer(name, playersao); // Create player if it doesn't exist if (!player) { @@ -3438,8 +3450,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version // Set player position infostream<<"Server: Finding spawn place for player \"" <setPosition(pos); + playersao->setBasePosition(findSpawnPos()); // Make sure the player is saved player->setModified(true); @@ -3450,18 +3461,14 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id, u16 proto_version // If the player exists, ensure that they respawn inside legal bounds // This fixes an assert crash when the player can't be added // to the environment - if (objectpos_over_limit(player->getPosition())) { + if (objectpos_over_limit(playersao->getBasePosition())) { actionstream << "Respawn position for player \"" << name << "\" outside limits, resetting" << std::endl; - v3f pos = findSpawnPos(); - player->setPosition(pos); + playersao->setBasePosition(findSpawnPos()); } } - // Create a new player active object - PlayerSAO *playersao = new PlayerSAO(m_env, player, peer_id, - getPlayerEffectivePrivs(player->getName()), - isSingleplayer()); + playersao->initialize(player, getPlayerEffectivePrivs(player->getName())); player->protocol_version = proto_version; diff --git a/src/serverobject.h b/src/serverobject.h index 63650e3be..9884eb0a1 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -85,7 +85,7 @@ public: Some more dynamic interface */ - virtual void setPos(v3f pos) + virtual void setPos(const v3f &pos) { setBasePosition(pos); } // continuous: if true, object does not stop immediately at pos virtual void moveTo(v3f pos, bool continuous) diff --git a/src/unittest/test_player.cpp b/src/unittest/test_player.cpp index 5de9eaaf2..fba422475 100644 --- a/src/unittest/test_player.cpp +++ b/src/unittest/test_player.cpp @@ -46,11 +46,14 @@ void TestPlayer::runTests(IGameDef *gamedef) void TestPlayer::testSave(IGameDef *gamedef) { RemotePlayer rplayer("testplayer_save", gamedef->idef()); - rplayer.setBreath(10); - rplayer.hp = 8; - rplayer.setYaw(0.1f); - rplayer.setPitch(0.6f); - rplayer.setPosition(v3f(450.2f, -15.7f, 68.1f)); + PlayerSAO sao(NULL, 1, false); + sao.initialize(&rplayer, std::set()); + rplayer.setPlayerSAO(&sao); + sao.setBreath(10); + sao.setHP(8, true); + sao.setYaw(0.1f, false); + sao.setPitch(0.6f, false); + sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f)); rplayer.save(".", gamedef); UASSERT(fs::PathExists("testplayer_save")); } @@ -58,24 +61,28 @@ void TestPlayer::testSave(IGameDef *gamedef) void TestPlayer::testLoad(IGameDef *gamedef) { RemotePlayer rplayer("testplayer_load", gamedef->idef()); - rplayer.setBreath(10); - rplayer.hp = 8; - rplayer.setYaw(0.1f); - rplayer.setPitch(0.6f); - rplayer.setPosition(v3f(450.2f, -15.7f, 68.1f)); + PlayerSAO sao(NULL, 1, false); + sao.initialize(&rplayer, std::set()); + rplayer.setPlayerSAO(&sao); + sao.setBreath(10); + sao.setHP(8, true); + sao.setYaw(0.1f, false); + sao.setPitch(0.6f, false); + sao.setBasePosition(v3f(450.2f, -15.7f, 68.1f)); rplayer.save(".", gamedef); UASSERT(fs::PathExists("testplayer_load")); RemotePlayer rplayer_load("testplayer_load", gamedef->idef()); + PlayerSAO sao_load(NULL, 2, false); std::ifstream is("testplayer_load", std::ios_base::binary); UASSERT(is.good()); - rplayer_load.deSerialize(is, "testplayer_load"); + rplayer_load.deSerialize(is, "testplayer_load", &sao_load); is.close(); UASSERT(strcmp(rplayer_load.getName(), "testplayer_load") == 0); - UASSERT(rplayer.getBreath() == 10); - UASSERT(rplayer.hp == 8); - UASSERT(rplayer.getYaw() == 0.1f); - UASSERT(rplayer.getPitch() == 0.6f); - UASSERT(rplayer.getPosition() == v3f(450.2f, -15.7f, 68.1f)); + UASSERT(sao_load.getBreath() == 10); + UASSERT(sao_load.getHP() == 8); + UASSERT(sao_load.getYaw() == 0.1f); + UASSERT(sao_load.getPitch() == 0.6f); + UASSERT(sao_load.getBasePosition() == v3f(450.2f, -15.7f, 68.1f)); } -- cgit v1.2.3