From 4e6971e59358b40956bb4c25f8f85e4cc018dbec Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 25 Jul 2015 11:56:24 +0200 Subject: [PATCH] Cleanup server addparticle(spawner) by merge two identical functions. --- src/script/lua_api/l_particles.cpp | 58 ++++++------------- src/server.cpp | 90 ++++++++++-------------------- src/server.h | 23 ++------ 3 files changed, 52 insertions(+), 119 deletions(-) diff --git a/src/script/lua_api/l_particles.cpp b/src/script/lua_api/l_particles.cpp index d99d8f6a9..2532b2b08 100644 --- a/src/script/lua_api/l_particles.cpp +++ b/src/script/lua_api/l_particles.cpp @@ -96,14 +96,8 @@ int ModApiParticles::l_add_particle(lua_State *L) texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); } - if (playername == "") { // spawn for all players - getServer(L)->spawnParticleAll(pos, vel, acc, + getServer(L)->spawnParticle(playername, pos, vel, acc, expirationtime, size, collisiondetection, vertical, texture); - } else { - getServer(L)->spawnParticle(playername.c_str(), - pos, vel, acc, expirationtime, - size, collisiondetection, vertical, texture); - } return 1; } @@ -195,30 +189,18 @@ int ModApiParticles::l_add_particlespawner(lua_State *L) texture = getstringfield_default(L, 1, "texture", ""); playername = getstringfield_default(L, 1, "playername", ""); } - if (playername == "") { //spawn for all players - u32 id = getServer(L)->addParticleSpawnerAll( amount, time, - minpos, maxpos, - minvel, maxvel, - minacc, maxacc, - minexptime, maxexptime, - minsize, maxsize, - collisiondetection, - vertical, - texture); - lua_pushnumber(L, id); - } else { - u32 id = getServer(L)->addParticleSpawner(playername.c_str(), - amount, time, - minpos, maxpos, - minvel, maxvel, - minacc, maxacc, - minexptime, maxexptime, - minsize, maxsize, - collisiondetection, - vertical, - texture); - lua_pushnumber(L, id); - } + + u32 id = getServer(L)->addParticleSpawner(amount, time, + minpos, maxpos, + minvel, maxvel, + minacc, maxacc, + minexptime, maxexptime, + minsize, maxsize, + collisiondetection, + vertical, + texture, playername); + lua_pushnumber(L, id); + return 1; } @@ -228,16 +210,12 @@ int ModApiParticles::l_delete_particlespawner(lua_State *L) { // Get parameters u32 id = luaL_checknumber(L, 1); + std::string playername = ""; + if (lua_gettop(L) == 2) { + playername = luaL_checkstring(L, 2); + } - if (lua_gettop(L) == 2) // only delete for one player - { - const char *playername = luaL_checkstring(L, 2); - getServer(L)->deleteParticleSpawner(playername, id); - } - else // delete for all players - { - getServer(L)->deleteParticleSpawnerAll(id); - } + getServer(L)->deleteParticleSpawner(playername, id); return 1; } diff --git a/src/server.cpp b/src/server.cpp index cb7e35ecd..33cba17e5 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3067,34 +3067,36 @@ void Server::notifyPlayers(const std::wstring &msg) SendChatMessage(PEER_ID_INEXISTENT,msg); } -void Server::spawnParticle(const char *playername, v3f pos, +void Server::spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool vertical, const std::string &texture) { - Player *player = m_env->getPlayer(playername); - if(!player) - return; - SendSpawnParticle(player->peer_id, pos, velocity, acceleration, + u16 peer_id = PEER_ID_INEXISTENT; + if (playername != "") { + Player* player = m_env->getPlayer(playername.c_str()); + if (!player) + return; + peer_id = player->peer_id; + } + + SendSpawnParticle(peer_id, pos, velocity, acceleration, expirationtime, size, collisiondetection, vertical, texture); } -void Server::spawnParticleAll(v3f pos, v3f velocity, v3f acceleration, - float expirationtime, float size, - bool collisiondetection, bool vertical, const std::string &texture) -{ - SendSpawnParticle(PEER_ID_INEXISTENT,pos, velocity, acceleration, - expirationtime, size, collisiondetection, vertical, texture); -} - -u32 Server::addParticleSpawner(const char *playername, u16 amount, float spawntime, +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 vertical, const std::string &texture) + bool collisiondetection, bool vertical, const std::string &texture, + const std::string &playername) { - Player *player = m_env->getPlayer(playername); - if(!player) - return -1; + u16 peer_id = PEER_ID_INEXISTENT; + if (playername != "") { + Player* player = m_env->getPlayer(playername.c_str()); + if (!player) + return -1; + peer_id = player->peer_id; + } u32 id = 0; for(;;) // look for unused particlespawner id @@ -3109,7 +3111,7 @@ u32 Server::addParticleSpawner(const char *playername, u16 amount, float spawnti } } - SendAddParticleSpawner(player->peer_id, amount, spawntime, + SendAddParticleSpawner(peer_id, amount, spawntime, minpos, maxpos, minvel, maxvel, minacc, maxacc, minexptime, maxexptime, minsize, maxsize, collisiondetection, vertical, texture, id); @@ -3117,55 +3119,21 @@ u32 Server::addParticleSpawner(const char *playername, u16 amount, float spawnti return id; } -u32 Server::addParticleSpawnerAll(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 vertical, const std::string &texture) +void Server::deleteParticleSpawner(const std::string &playername, u32 id) { - u32 id = 0; - for(;;) // look for unused particlespawner id - { - id++; - if (std::find(m_particlespawner_ids.begin(), - m_particlespawner_ids.end(), id) - == m_particlespawner_ids.end()) - { - m_particlespawner_ids.push_back(id); - break; - } + u16 peer_id = PEER_ID_INEXISTENT; + if (playername != "") { + Player* player = m_env->getPlayer(playername.c_str()); + if (!player) + return; + peer_id = player->peer_id; } - SendAddParticleSpawner(PEER_ID_INEXISTENT, amount, spawntime, - minpos, maxpos, minvel, maxvel, minacc, maxacc, - minexptime, maxexptime, minsize, maxsize, - collisiondetection, vertical, texture, id); - - return id; -} - -void Server::deleteParticleSpawner(const char *playername, u32 id) -{ - Player *player = m_env->getPlayer(playername); - if(!player) - return; - m_particlespawner_ids.erase( std::remove(m_particlespawner_ids.begin(), m_particlespawner_ids.end(), id), m_particlespawner_ids.end()); - SendDeleteParticleSpawner(player->peer_id, id); -} - -void Server::deleteParticleSpawnerAll(u32 id) -{ - m_particlespawner_ids.erase( - std::remove(m_particlespawner_ids.begin(), - m_particlespawner_ids.end(), id), - m_particlespawner_ids.end()); - SendDeleteParticleSpawner(PEER_ID_INEXISTENT, id); + SendDeleteParticleSpawner(peer_id, id); } Inventory* Server::createDetachedInventory(const std::string &name) diff --git a/src/server.h b/src/server.h index 28909611b..d16230967 100644 --- a/src/server.h +++ b/src/server.h @@ -269,34 +269,21 @@ public: void notifyPlayer(const char *name, const std::wstring &msg); void notifyPlayers(const std::wstring &msg); - void spawnParticle(const char *playername, + void spawnParticle(const std::string &playername, v3f pos, v3f velocity, v3f acceleration, float expirationtime, float size, bool collisiondetection, bool vertical, const std::string &texture); - void spawnParticleAll(v3f pos, v3f velocity, v3f acceleration, - float expirationtime, float size, - bool collisiondetection, bool vertical, const std::string &texture); - - u32 addParticleSpawner(const char *playername, - u16 amount, float spawntime, + u32 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 vertical, const std::string &texture); + bool collisiondetection, bool vertical, const std::string &texture, + const std::string &playername); - u32 addParticleSpawnerAll(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 vertical, const std::string &texture); - - void deleteParticleSpawner(const char *playername, u32 id); - void deleteParticleSpawnerAll(u32 id); + void deleteParticleSpawner(const std::string &playername, u32 id); // Creates or resets inventory Inventory* createDetachedInventory(const std::string &name);