Cleanup server addparticle(spawner) by merge two identical functions. 2956/head
authorLoic Blot <loic.blot@unix-experience.fr>
Sat, 25 Jul 2015 09:56:24 +0000 (11:56 +0200)
committerLoic Blot <loic.blot@unix-experience.fr>
Sat, 25 Jul 2015 10:24:28 +0000 (12:24 +0200)
src/script/lua_api/l_particles.cpp
src/server.cpp
src/server.h

index d99d8f6a90b43b695d7b01c3d3fd364cb26c64a0..2532b2b08e8efe156e66cbba5ce35dedd29e0a02 100644 (file)
@@ -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);
-
-       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);
+       std::string playername = "";
+       if (lua_gettop(L) == 2) {
+               playername = luaL_checkstring(L, 2);
        }
+
+       getServer(L)->deleteParticleSpawner(playername, id);
        return 1;
 }
 
index cb7e35ecd5e84b4a3d6fd6baa52a56e5cda6860a..33cba17e51bdb0832b044d2ff29ad27dcba6d1ec 100644 (file)
@@ -3067,64 +3067,37 @@ 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,
-                       expirationtime, size, collisiondetection, vertical, texture);
-}
+       u16 peer_id = PEER_ID_INEXISTENT;
+       if (playername != "") {
+               Player* player = m_env->getPlayer(playername.c_str());
+               if (!player)
+                       return;
+               peer_id = player->peer_id;
+       }
 
-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,
+       SendSpawnParticle(peer_id, 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;
-
-       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 -1;
+               peer_id = player->peer_id;
        }
 
-       SendAddParticleSpawner(player->peer_id, amount, spawntime,
-               minpos, maxpos, minvel, maxvel, minacc, maxacc,
-               minexptime, maxexptime, minsize, maxsize,
-               collisiondetection, vertical, texture, id);
-
-       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)
-{
        u32 id = 0;
        for(;;) // look for unused particlespawner id
        {
@@ -3138,7 +3111,7 @@ u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
                }
        }
 
-       SendAddParticleSpawner(PEER_ID_INEXISTENT, amount, spawntime,
+       SendAddParticleSpawner(peer_id, amount, spawntime,
                minpos, maxpos, minvel, maxvel, minacc, maxacc,
                minexptime, maxexptime, minsize, maxsize,
                collisiondetection, vertical, texture, id);
@@ -3146,26 +3119,21 @@ u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
        return id;
 }
 
-void Server::deleteParticleSpawner(const char *playername, u32 id)
+void Server::deleteParticleSpawner(const std::string &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);
-}
+       u16 peer_id = PEER_ID_INEXISTENT;
+       if (playername != "") {
+               Player* player = m_env->getPlayer(playername.c_str());
+               if (!player)
+                       return;
+               peer_id = player->peer_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)
index 28909611b9c83f9ef22726569425a836bc497c73..d16230967eb3eb25ee3cb1cf122e98c08f50dbc9 100644 (file)
@@ -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);
-
-       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);
+               bool collisiondetection, bool vertical, const std::string &texture,
+               const std::string &playername);
 
-       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);