Skip to content

Commit

Permalink
Cleanup server addparticle(spawner) by merge two identical functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nerzhul committed Jul 25, 2015
1 parent a8c5841 commit 4e6971e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 118 deletions.
58 changes: 18 additions & 40 deletions src/script/lua_api/l_particles.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
88 changes: 28 additions & 60 deletions src/server.cpp
Expand Up @@ -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
{
Expand All @@ -3138,34 +3111,29 @@ 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);

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)
Expand Down
23 changes: 5 additions & 18 deletions src/server.h
Expand Up @@ -269,34 +269,21 @@ class Server : public con::PeerHandler, public MapEventReceiver,

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);
Expand Down

0 comments on commit 4e6971e

Please sign in to comment.