Skip to content

Commit 4e6971e

Browse files
committedJul 25, 2015
Cleanup server addparticle(spawner) by merge two identical functions.
1 parent a8c5841 commit 4e6971e

File tree

3 files changed

+51
-118
lines changed

3 files changed

+51
-118
lines changed
 

Diff for: ‎src/script/lua_api/l_particles.cpp

+18-40
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,8 @@ int ModApiParticles::l_add_particle(lua_State *L)
9696
texture = getstringfield_default(L, 1, "texture", "");
9797
playername = getstringfield_default(L, 1, "playername", "");
9898
}
99-
if (playername == "") { // spawn for all players
100-
getServer(L)->spawnParticleAll(pos, vel, acc,
99+
getServer(L)->spawnParticle(playername, pos, vel, acc,
101100
expirationtime, size, collisiondetection, vertical, texture);
102-
} else {
103-
getServer(L)->spawnParticle(playername.c_str(),
104-
pos, vel, acc, expirationtime,
105-
size, collisiondetection, vertical, texture);
106-
}
107101
return 1;
108102
}
109103

@@ -195,30 +189,18 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
195189
texture = getstringfield_default(L, 1, "texture", "");
196190
playername = getstringfield_default(L, 1, "playername", "");
197191
}
198-
if (playername == "") { //spawn for all players
199-
u32 id = getServer(L)->addParticleSpawnerAll( amount, time,
200-
minpos, maxpos,
201-
minvel, maxvel,
202-
minacc, maxacc,
203-
minexptime, maxexptime,
204-
minsize, maxsize,
205-
collisiondetection,
206-
vertical,
207-
texture);
208-
lua_pushnumber(L, id);
209-
} else {
210-
u32 id = getServer(L)->addParticleSpawner(playername.c_str(),
211-
amount, time,
212-
minpos, maxpos,
213-
minvel, maxvel,
214-
minacc, maxacc,
215-
minexptime, maxexptime,
216-
minsize, maxsize,
217-
collisiondetection,
218-
vertical,
219-
texture);
220-
lua_pushnumber(L, id);
221-
}
192+
193+
u32 id = getServer(L)->addParticleSpawner(amount, time,
194+
minpos, maxpos,
195+
minvel, maxvel,
196+
minacc, maxacc,
197+
minexptime, maxexptime,
198+
minsize, maxsize,
199+
collisiondetection,
200+
vertical,
201+
texture, playername);
202+
lua_pushnumber(L, id);
203+
222204
return 1;
223205
}
224206

@@ -228,16 +210,12 @@ int ModApiParticles::l_delete_particlespawner(lua_State *L)
228210
{
229211
// Get parameters
230212
u32 id = luaL_checknumber(L, 1);
231-
232-
if (lua_gettop(L) == 2) // only delete for one player
233-
{
234-
const char *playername = luaL_checkstring(L, 2);
235-
getServer(L)->deleteParticleSpawner(playername, id);
236-
}
237-
else // delete for all players
238-
{
239-
getServer(L)->deleteParticleSpawnerAll(id);
213+
std::string playername = "";
214+
if (lua_gettop(L) == 2) {
215+
playername = luaL_checkstring(L, 2);
240216
}
217+
218+
getServer(L)->deleteParticleSpawner(playername, id);
241219
return 1;
242220
}
243221

Diff for: ‎src/server.cpp

+28-60
Original file line numberDiff line numberDiff line change
@@ -3067,64 +3067,37 @@ void Server::notifyPlayers(const std::wstring &msg)
30673067
SendChatMessage(PEER_ID_INEXISTENT,msg);
30683068
}
30693069

3070-
void Server::spawnParticle(const char *playername, v3f pos,
3070+
void Server::spawnParticle(const std::string &playername, v3f pos,
30713071
v3f velocity, v3f acceleration,
30723072
float expirationtime, float size, bool
30733073
collisiondetection, bool vertical, const std::string &texture)
30743074
{
3075-
Player *player = m_env->getPlayer(playername);
3076-
if(!player)
3077-
return;
3078-
SendSpawnParticle(player->peer_id, pos, velocity, acceleration,
3079-
expirationtime, size, collisiondetection, vertical, texture);
3080-
}
3075+
u16 peer_id = PEER_ID_INEXISTENT;
3076+
if (playername != "") {
3077+
Player* player = m_env->getPlayer(playername.c_str());
3078+
if (!player)
3079+
return;
3080+
peer_id = player->peer_id;
3081+
}
30813082

3082-
void Server::spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
3083-
float expirationtime, float size,
3084-
bool collisiondetection, bool vertical, const std::string &texture)
3085-
{
3086-
SendSpawnParticle(PEER_ID_INEXISTENT,pos, velocity, acceleration,
3083+
SendSpawnParticle(peer_id, pos, velocity, acceleration,
30873084
expirationtime, size, collisiondetection, vertical, texture);
30883085
}
30893086

3090-
u32 Server::addParticleSpawner(const char *playername, u16 amount, float spawntime,
3087+
u32 Server::addParticleSpawner(u16 amount, float spawntime,
30913088
v3f minpos, v3f maxpos, v3f minvel, v3f maxvel, v3f minacc, v3f maxacc,
30923089
float minexptime, float maxexptime, float minsize, float maxsize,
3093-
bool collisiondetection, bool vertical, const std::string &texture)
3090+
bool collisiondetection, bool vertical, const std::string &texture,
3091+
const std::string &playername)
30943092
{
3095-
Player *player = m_env->getPlayer(playername);
3096-
if(!player)
3097-
return -1;
3098-
3099-
u32 id = 0;
3100-
for(;;) // look for unused particlespawner id
3101-
{
3102-
id++;
3103-
if (std::find(m_particlespawner_ids.begin(),
3104-
m_particlespawner_ids.end(), id)
3105-
== m_particlespawner_ids.end())
3106-
{
3107-
m_particlespawner_ids.push_back(id);
3108-
break;
3109-
}
3093+
u16 peer_id = PEER_ID_INEXISTENT;
3094+
if (playername != "") {
3095+
Player* player = m_env->getPlayer(playername.c_str());
3096+
if (!player)
3097+
return -1;
3098+
peer_id = player->peer_id;
31103099
}
31113100

3112-
SendAddParticleSpawner(player->peer_id, amount, spawntime,
3113-
minpos, maxpos, minvel, maxvel, minacc, maxacc,
3114-
minexptime, maxexptime, minsize, maxsize,
3115-
collisiondetection, vertical, texture, id);
3116-
3117-
return id;
3118-
}
3119-
3120-
u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
3121-
v3f minpos, v3f maxpos,
3122-
v3f minvel, v3f maxvel,
3123-
v3f minacc, v3f maxacc,
3124-
float minexptime, float maxexptime,
3125-
float minsize, float maxsize,
3126-
bool collisiondetection, bool vertical, const std::string &texture)
3127-
{
31283101
u32 id = 0;
31293102
for(;;) // look for unused particlespawner id
31303103
{
@@ -3138,34 +3111,29 @@ u32 Server::addParticleSpawnerAll(u16 amount, float spawntime,
31383111
}
31393112
}
31403113

3141-
SendAddParticleSpawner(PEER_ID_INEXISTENT, amount, spawntime,
3114+
SendAddParticleSpawner(peer_id, amount, spawntime,
31423115
minpos, maxpos, minvel, maxvel, minacc, maxacc,
31433116
minexptime, maxexptime, minsize, maxsize,
31443117
collisiondetection, vertical, texture, id);
31453118

31463119
return id;
31473120
}
31483121

3149-
void Server::deleteParticleSpawner(const char *playername, u32 id)
3122+
void Server::deleteParticleSpawner(const std::string &playername, u32 id)
31503123
{
3151-
Player *player = m_env->getPlayer(playername);
3152-
if(!player)
3153-
return;
3154-
3155-
m_particlespawner_ids.erase(
3156-
std::remove(m_particlespawner_ids.begin(),
3157-
m_particlespawner_ids.end(), id),
3158-
m_particlespawner_ids.end());
3159-
SendDeleteParticleSpawner(player->peer_id, id);
3160-
}
3124+
u16 peer_id = PEER_ID_INEXISTENT;
3125+
if (playername != "") {
3126+
Player* player = m_env->getPlayer(playername.c_str());
3127+
if (!player)
3128+
return;
3129+
peer_id = player->peer_id;
3130+
}
31613131

3162-
void Server::deleteParticleSpawnerAll(u32 id)
3163-
{
31643132
m_particlespawner_ids.erase(
31653133
std::remove(m_particlespawner_ids.begin(),
31663134
m_particlespawner_ids.end(), id),
31673135
m_particlespawner_ids.end());
3168-
SendDeleteParticleSpawner(PEER_ID_INEXISTENT, id);
3136+
SendDeleteParticleSpawner(peer_id, id);
31693137
}
31703138

31713139
Inventory* Server::createDetachedInventory(const std::string &name)

Diff for: ‎src/server.h

+5-18
Original file line numberDiff line numberDiff line change
@@ -269,34 +269,21 @@ class Server : public con::PeerHandler, public MapEventReceiver,
269269

270270
void notifyPlayer(const char *name, const std::wstring &msg);
271271
void notifyPlayers(const std::wstring &msg);
272-
void spawnParticle(const char *playername,
272+
void spawnParticle(const std::string &playername,
273273
v3f pos, v3f velocity, v3f acceleration,
274274
float expirationtime, float size,
275275
bool collisiondetection, bool vertical, const std::string &texture);
276276

277-
void spawnParticleAll(v3f pos, v3f velocity, v3f acceleration,
278-
float expirationtime, float size,
279-
bool collisiondetection, bool vertical, const std::string &texture);
280-
281-
u32 addParticleSpawner(const char *playername,
282-
u16 amount, float spawntime,
277+
u32 addParticleSpawner(u16 amount, float spawntime,
283278
v3f minpos, v3f maxpos,
284279
v3f minvel, v3f maxvel,
285280
v3f minacc, v3f maxacc,
286281
float minexptime, float maxexptime,
287282
float minsize, float maxsize,
288-
bool collisiondetection, bool vertical, const std::string &texture);
289-
290-
u32 addParticleSpawnerAll(u16 amount, float spawntime,
291-
v3f minpos, v3f maxpos,
292-
v3f minvel, v3f maxvel,
293-
v3f minacc, v3f maxacc,
294-
float minexptime, float maxexptime,
295-
float minsize, float maxsize,
296-
bool collisiondetection, bool vertical, const std::string &texture);
283+
bool collisiondetection, bool vertical, const std::string &texture,
284+
const std::string &playername);
297285

298-
void deleteParticleSpawner(const char *playername, u32 id);
299-
void deleteParticleSpawnerAll(u32 id);
286+
void deleteParticleSpawner(const std::string &playername, u32 id);
300287

301288
// Creates or resets inventory
302289
Inventory* createDetachedInventory(const std::string &name);

0 commit comments

Comments
 (0)
Please sign in to comment.