Refactor particle code to remove the while loops

Replaces while loops with proper getfield calls
stable-0.4
TeTpaAka 2015-07-18 11:52:39 +02:00 committed by est31
parent 1076bbd03e
commit e47f390e0d
1 changed files with 78 additions and 87 deletions

View File

@ -43,7 +43,7 @@ int ModApiParticles::l_add_particle(lua_State *L)
collisiondetection = vertical = false; collisiondetection = vertical = false;
std::string texture = ""; std::string texture = "";
const char *playername = ""; std::string playername = "";
if (lua_gettop(L) > 1) // deprecated if (lua_gettop(L) > 1) // deprecated
{ {
@ -60,49 +60,47 @@ int ModApiParticles::l_add_particle(lua_State *L)
} }
else if (lua_istable(L, 1)) else if (lua_istable(L, 1))
{ {
int table = lua_gettop(L); lua_getfield(L, 1, "pos");
lua_pushnil(L); pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
while (lua_next(L, table) != 0) lua_pop(L, 1);
{
const char *key = lua_tostring(L, -2); lua_getfield(L, 1, "vel");
if (strcmp(key, "pos") == 0) { if (lua_istable(L, -1)) {
pos = check_v3f(L, -1); vel = check_v3f(L, -1);
} else if (strcmp(key,"vel") == 0) { log_deprecated(L, "The use of vel is deprecated. "
vel = check_v3f(L, -1); "Use velocity instead");
log_deprecated(L, "The use of vel is deprecated. "
"Use velocity instead");
} else if (strcmp(key,"velocity") == 0) {
vel = check_v3f(L, -1);
} else if (strcmp(key,"acc") == 0) {
acc = check_v3f(L, -1);
log_deprecated(L, "The use of acc is deprecated. "
"Use acceleration instead");
} else if (strcmp(key,"acceleration") == 0) {
acc = check_v3f(L, -1);
} else if (strcmp(key,"expirationtime") == 0) {
expirationtime = luaL_checknumber(L, -1);
} else if (strcmp(key,"size") == 0) {
size = luaL_checknumber(L, -1);
} else if (strcmp(key,"collisiondetection") == 0) {
collisiondetection = lua_toboolean(L, -1);
} else if (strcmp(key,"vertical") == 0) {
vertical = lua_toboolean(L, -1);
} else if (strcmp(key,"texture") == 0) {
texture = luaL_checkstring(L, -1);
} else if (strcmp(key,"playername") == 0) {
playername = luaL_checkstring(L, -1);
}
lua_pop(L, 1);
} }
lua_pop(L, 1);
lua_getfield(L, 1, "velocity");
vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
lua_pop(L, 1);
lua_getfield(L, 1, "acc");
if (lua_istable(L, -1)) {
acc = check_v3f(L, -1);
log_deprecated(L, "The use of acc is deprecated. "
"Use acceleration instead");
}
lua_pop(L, 1);
lua_getfield(L, 1, "acceleration");
acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
lua_pop(L, 1);
expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
size = getfloatfield_default(L, 1, "size", 1);
collisiondetection = getboolfield_default(L, 1,
"collisiondetection", collisiondetection);
vertical = getboolfield_default(L, 1, "vertical", vertical);
texture = getstringfield_default(L, 1, "texture", "");
playername = getstringfield_default(L, 1, "playername", "");
} }
if (strcmp(playername, "") == 0) // spawn for all players if (playername == "") { // spawn for all players
{
getServer(L)->spawnParticleAll(pos, vel, acc, getServer(L)->spawnParticleAll(pos, vel, acc,
expirationtime, size, collisiondetection, vertical, texture); expirationtime, size, collisiondetection, vertical, texture);
} } else {
else getServer(L)->spawnParticle(playername.c_str(),
{
getServer(L)->spawnParticle(playername,
pos, vel, acc, expirationtime, pos, vel, acc, expirationtime,
size, collisiondetection, vertical, texture); size, collisiondetection, vertical, texture);
} }
@ -136,7 +134,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
bool collisiondetection, vertical; bool collisiondetection, vertical;
collisiondetection= vertical= false; collisiondetection= vertical= false;
std::string texture = ""; std::string texture = "";
const char *playername = ""; std::string playername = "";
if (lua_gettop(L) > 1) //deprecated if (lua_gettop(L) > 1) //deprecated
{ {
@ -160,49 +158,44 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
} }
else if (lua_istable(L, 1)) else if (lua_istable(L, 1))
{ {
int table = lua_gettop(L); amount = getintfield_default(L, 1, "amount", amount);
lua_pushnil(L); time = getfloatfield_default(L, 1, "time", time);
while (lua_next(L, table) != 0)
{ lua_getfield(L, 1, "minpos");
const char *key = lua_tostring(L, -2); minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
if(strcmp(key,"amount")==0){ lua_pop(L, 1);
amount=luaL_checknumber(L, -1);
}else if(strcmp(key,"time")==0){ lua_getfield(L, 1, "maxpos");
time=luaL_checknumber(L, -1); maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
}else if(strcmp(key,"minpos")==0){ lua_pop(L, 1);
minpos=check_v3f(L, -1);
}else if(strcmp(key,"maxpos")==0){ lua_getfield(L, 1, "minvel");
maxpos=check_v3f(L, -1); minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
}else if(strcmp(key,"minvel")==0){ lua_pop(L, 1);
minvel=check_v3f(L, -1);
}else if(strcmp(key,"maxvel")==0){ lua_getfield(L, 1, "maxvel");
maxvel=check_v3f(L, -1); maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
}else if(strcmp(key,"minacc")==0){ lua_pop(L, 1);
minacc=check_v3f(L, -1);
}else if(strcmp(key,"maxacc")==0){ lua_getfield(L, 1, "minacc");
maxacc=check_v3f(L, -1); minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
}else if(strcmp(key,"minexptime")==0){ lua_pop(L, 1);
minexptime=luaL_checknumber(L, -1);
}else if(strcmp(key,"maxexptime")==0){ lua_getfield(L, 1, "maxacc");
maxexptime=luaL_checknumber(L, -1); maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
}else if(strcmp(key,"minsize")==0){ lua_pop(L, 1);
minsize=luaL_checknumber(L, -1);
}else if(strcmp(key,"maxsize")==0){ minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
maxsize=luaL_checknumber(L, -1); maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
}else if(strcmp(key,"collisiondetection")==0){ minsize = getfloatfield_default(L, 1, "minsize", minsize);
collisiondetection=lua_toboolean(L, -1); maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
}else if(strcmp(key,"vertical")==0){ collisiondetection = getboolfield_default(L, 1,
vertical=lua_toboolean(L, -1); "collisiondetection", collisiondetection);
}else if(strcmp(key,"texture")==0){ vertical = getboolfield_default(L, 1, "vertical", vertical);
texture=luaL_checkstring(L, -1); texture = getstringfield_default(L, 1, "texture", "");
}else if(strcmp(key,"playername")==0){ playername = getstringfield_default(L, 1, "playername", "");
playername=luaL_checkstring(L, -1);
}
lua_pop(L, 1);
}
} }
if (strcmp(playername, "")==0) //spawn for all players if (playername == "") { //spawn for all players
{
u32 id = getServer(L)->addParticleSpawnerAll( amount, time, u32 id = getServer(L)->addParticleSpawnerAll( amount, time,
minpos, maxpos, minpos, maxpos,
minvel, maxvel, minvel, maxvel,
@ -213,10 +206,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
vertical, vertical,
texture); texture);
lua_pushnumber(L, id); lua_pushnumber(L, id);
} } else {
else u32 id = getServer(L)->addParticleSpawner(playername.c_str(),
{
u32 id = getServer(L)->addParticleSpawner(playername,
amount, time, amount, time,
minpos, maxpos, minpos, maxpos,
minvel, maxvel, minvel, maxvel,