Skip to content

Commit e47f390

Browse files
TeTpaAkaest31
authored andcommittedJul 18, 2015
Refactor particle code to remove the while loops
Replaces while loops with proper getfield calls
1 parent 1076bbd commit e47f390

File tree

1 file changed

+78
-87
lines changed

1 file changed

+78
-87
lines changed
 

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

+78-87
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int ModApiParticles::l_add_particle(lua_State *L)
4343
collisiondetection = vertical = false;
4444

4545
std::string texture = "";
46-
const char *playername = "";
46+
std::string playername = "";
4747

4848
if (lua_gettop(L) > 1) // deprecated
4949
{
@@ -60,49 +60,47 @@ int ModApiParticles::l_add_particle(lua_State *L)
6060
}
6161
else if (lua_istable(L, 1))
6262
{
63-
int table = lua_gettop(L);
64-
lua_pushnil(L);
65-
while (lua_next(L, table) != 0)
66-
{
67-
const char *key = lua_tostring(L, -2);
68-
if (strcmp(key, "pos") == 0) {
69-
pos = check_v3f(L, -1);
70-
} else if (strcmp(key,"vel") == 0) {
71-
vel = check_v3f(L, -1);
72-
log_deprecated(L, "The use of vel is deprecated. "
73-
"Use velocity instead");
74-
} else if (strcmp(key,"velocity") == 0) {
75-
vel = check_v3f(L, -1);
76-
} else if (strcmp(key,"acc") == 0) {
77-
acc = check_v3f(L, -1);
78-
log_deprecated(L, "The use of acc is deprecated. "
79-
"Use acceleration instead");
80-
} else if (strcmp(key,"acceleration") == 0) {
81-
acc = check_v3f(L, -1);
82-
} else if (strcmp(key,"expirationtime") == 0) {
83-
expirationtime = luaL_checknumber(L, -1);
84-
} else if (strcmp(key,"size") == 0) {
85-
size = luaL_checknumber(L, -1);
86-
} else if (strcmp(key,"collisiondetection") == 0) {
87-
collisiondetection = lua_toboolean(L, -1);
88-
} else if (strcmp(key,"vertical") == 0) {
89-
vertical = lua_toboolean(L, -1);
90-
} else if (strcmp(key,"texture") == 0) {
91-
texture = luaL_checkstring(L, -1);
92-
} else if (strcmp(key,"playername") == 0) {
93-
playername = luaL_checkstring(L, -1);
94-
}
95-
lua_pop(L, 1);
63+
lua_getfield(L, 1, "pos");
64+
pos = lua_istable(L, -1) ? check_v3f(L, -1) : v3f();
65+
lua_pop(L, 1);
66+
67+
lua_getfield(L, 1, "vel");
68+
if (lua_istable(L, -1)) {
69+
vel = check_v3f(L, -1);
70+
log_deprecated(L, "The use of vel is deprecated. "
71+
"Use velocity instead");
9672
}
73+
lua_pop(L, 1);
74+
75+
lua_getfield(L, 1, "velocity");
76+
vel = lua_istable(L, -1) ? check_v3f(L, -1) : vel;
77+
lua_pop(L, 1);
78+
79+
lua_getfield(L, 1, "acc");
80+
if (lua_istable(L, -1)) {
81+
acc = check_v3f(L, -1);
82+
log_deprecated(L, "The use of acc is deprecated. "
83+
"Use acceleration instead");
84+
}
85+
lua_pop(L, 1);
86+
87+
lua_getfield(L, 1, "acceleration");
88+
acc = lua_istable(L, -1) ? check_v3f(L, -1) : acc;
89+
lua_pop(L, 1);
90+
91+
expirationtime = getfloatfield_default(L, 1, "expirationtime", 1);
92+
size = getfloatfield_default(L, 1, "size", 1);
93+
collisiondetection = getboolfield_default(L, 1,
94+
"collisiondetection", collisiondetection);
95+
vertical = getboolfield_default(L, 1, "vertical", vertical);
96+
texture = getstringfield_default(L, 1, "texture", "");
97+
playername = getstringfield_default(L, 1, "playername", "");
9798
}
98-
if (strcmp(playername, "") == 0) // spawn for all players
99-
{
99+
if (playername == "") { // spawn for all players
100100
getServer(L)->spawnParticleAll(pos, vel, acc,
101101
expirationtime, size, collisiondetection, vertical, texture);
102-
}
103-
else
104-
{
105-
getServer(L)->spawnParticle(playername,
102+
} else {
103+
getServer(L)->spawnParticle(playername.c_str(),
106104
pos, vel, acc, expirationtime,
107105
size, collisiondetection, vertical, texture);
108106
}
@@ -136,7 +134,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
136134
bool collisiondetection, vertical;
137135
collisiondetection= vertical= false;
138136
std::string texture = "";
139-
const char *playername = "";
137+
std::string playername = "";
140138

141139
if (lua_gettop(L) > 1) //deprecated
142140
{
@@ -160,49 +158,44 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
160158
}
161159
else if (lua_istable(L, 1))
162160
{
163-
int table = lua_gettop(L);
164-
lua_pushnil(L);
165-
while (lua_next(L, table) != 0)
166-
{
167-
const char *key = lua_tostring(L, -2);
168-
if(strcmp(key,"amount")==0){
169-
amount=luaL_checknumber(L, -1);
170-
}else if(strcmp(key,"time")==0){
171-
time=luaL_checknumber(L, -1);
172-
}else if(strcmp(key,"minpos")==0){
173-
minpos=check_v3f(L, -1);
174-
}else if(strcmp(key,"maxpos")==0){
175-
maxpos=check_v3f(L, -1);
176-
}else if(strcmp(key,"minvel")==0){
177-
minvel=check_v3f(L, -1);
178-
}else if(strcmp(key,"maxvel")==0){
179-
maxvel=check_v3f(L, -1);
180-
}else if(strcmp(key,"minacc")==0){
181-
minacc=check_v3f(L, -1);
182-
}else if(strcmp(key,"maxacc")==0){
183-
maxacc=check_v3f(L, -1);
184-
}else if(strcmp(key,"minexptime")==0){
185-
minexptime=luaL_checknumber(L, -1);
186-
}else if(strcmp(key,"maxexptime")==0){
187-
maxexptime=luaL_checknumber(L, -1);
188-
}else if(strcmp(key,"minsize")==0){
189-
minsize=luaL_checknumber(L, -1);
190-
}else if(strcmp(key,"maxsize")==0){
191-
maxsize=luaL_checknumber(L, -1);
192-
}else if(strcmp(key,"collisiondetection")==0){
193-
collisiondetection=lua_toboolean(L, -1);
194-
}else if(strcmp(key,"vertical")==0){
195-
vertical=lua_toboolean(L, -1);
196-
}else if(strcmp(key,"texture")==0){
197-
texture=luaL_checkstring(L, -1);
198-
}else if(strcmp(key,"playername")==0){
199-
playername=luaL_checkstring(L, -1);
200-
}
201-
lua_pop(L, 1);
202-
}
161+
amount = getintfield_default(L, 1, "amount", amount);
162+
time = getfloatfield_default(L, 1, "time", time);
163+
164+
lua_getfield(L, 1, "minpos");
165+
minpos = lua_istable(L, -1) ? check_v3f(L, -1) : minpos;
166+
lua_pop(L, 1);
167+
168+
lua_getfield(L, 1, "maxpos");
169+
maxpos = lua_istable(L, -1) ? check_v3f(L, -1) : maxpos;
170+
lua_pop(L, 1);
171+
172+
lua_getfield(L, 1, "minvel");
173+
minvel = lua_istable(L, -1) ? check_v3f(L, -1) : minvel;
174+
lua_pop(L, 1);
175+
176+
lua_getfield(L, 1, "maxvel");
177+
maxvel = lua_istable(L, -1) ? check_v3f(L, -1) : maxvel;
178+
lua_pop(L, 1);
179+
180+
lua_getfield(L, 1, "minacc");
181+
minacc = lua_istable(L, -1) ? check_v3f(L, -1) : minacc;
182+
lua_pop(L, 1);
183+
184+
lua_getfield(L, 1, "maxacc");
185+
maxacc = lua_istable(L, -1) ? check_v3f(L, -1) : maxacc;
186+
lua_pop(L, 1);
187+
188+
minexptime = getfloatfield_default(L, 1, "minexptime", minexptime);
189+
maxexptime = getfloatfield_default(L, 1, "maxexptime", maxexptime);
190+
minsize = getfloatfield_default(L, 1, "minsize", minsize);
191+
maxsize = getfloatfield_default(L, 1, "maxsize", maxsize);
192+
collisiondetection = getboolfield_default(L, 1,
193+
"collisiondetection", collisiondetection);
194+
vertical = getboolfield_default(L, 1, "vertical", vertical);
195+
texture = getstringfield_default(L, 1, "texture", "");
196+
playername = getstringfield_default(L, 1, "playername", "");
203197
}
204-
if (strcmp(playername, "")==0) //spawn for all players
205-
{
198+
if (playername == "") { //spawn for all players
206199
u32 id = getServer(L)->addParticleSpawnerAll( amount, time,
207200
minpos, maxpos,
208201
minvel, maxvel,
@@ -213,10 +206,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
213206
vertical,
214207
texture);
215208
lua_pushnumber(L, id);
216-
}
217-
else
218-
{
219-
u32 id = getServer(L)->addParticleSpawner(playername,
209+
} else {
210+
u32 id = getServer(L)->addParticleSpawner(playername.c_str(),
220211
amount, time,
221212
minpos, maxpos,
222213
minvel, maxvel,

0 commit comments

Comments
 (0)
Please sign in to comment.