Skip to content

Commit 725cb4e

Browse files
committedMar 7, 2016
s_env.{cpp, h} cleanups
* Replace string by-val passing with const reference * Fix code style * Remove redundant `int table` definition and indentation level
1 parent d494733 commit 725cb4e

File tree

2 files changed

+56
-57
lines changed

2 files changed

+56
-57
lines changed
 

Diff for: ‎src/script/cpp_api/s_env.cpp

+53-55
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2727
#include "server.h"
2828

2929
void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
30-
u32 blockseed)
30+
u32 blockseed)
3131
{
3232
SCRIPTAPI_PRECHECKHEADER
3333

@@ -44,7 +44,7 @@ void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
4444
void ScriptApiEnv::environment_Step(float dtime)
4545
{
4646
SCRIPTAPI_PRECHECKHEADER
47-
//infostream<<"scriptapi_environment_step"<<std::endl;
47+
//infostream << "scriptapi_environment_step" << std::endl;
4848

4949
// Get core.registered_globalsteps
5050
lua_getglobal(L, "core");
@@ -58,7 +58,7 @@ void ScriptApiEnv::environment_Step(float dtime)
5858
}
5959
}
6060

61-
void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
61+
void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
6262
{
6363
SCRIPTAPI_PRECHECKHEADER
6464

@@ -82,7 +82,7 @@ void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
8282
void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
8383
{
8484
SCRIPTAPI_PRECHECKHEADER
85-
verbosestream<<"scriptapi_add_environment"<<std::endl;
85+
verbosestream << "scriptapi_add_environment" << std::endl;
8686
setEnv(env);
8787

8888
/*
@@ -94,68 +94,66 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
9494
lua_getfield(L, -1, "registered_abms");
9595
int registered_abms = lua_gettop(L);
9696

97-
if(lua_istable(L, registered_abms)){
98-
int table = lua_gettop(L);
99-
lua_pushnil(L);
100-
while(lua_next(L, table) != 0){
101-
// key at index -2 and value at index -1
102-
int id = lua_tonumber(L, -2);
103-
int current_abm = lua_gettop(L);
104-
105-
std::set<std::string> trigger_contents;
106-
lua_getfield(L, current_abm, "nodenames");
107-
if(lua_istable(L, -1)){
108-
int table = lua_gettop(L);
109-
lua_pushnil(L);
110-
while(lua_next(L, table) != 0){
111-
// key at index -2 and value at index -1
112-
luaL_checktype(L, -1, LUA_TSTRING);
113-
trigger_contents.insert(lua_tostring(L, -1));
114-
// removes value, keeps key for next iteration
115-
lua_pop(L, 1);
116-
}
117-
} else if(lua_isstring(L, -1)){
97+
if (!lua_istable(L, registered_abms)) {
98+
lua_pop(L, 1);
99+
throw LuaError("core.registered_abms was not a lua table, as expected.");
100+
}
101+
lua_pushnil(L);
102+
while (lua_next(L, registered_abms)) {
103+
// key at index -2 and value at index -1
104+
int id = lua_tonumber(L, -2);
105+
int current_abm = lua_gettop(L);
106+
107+
std::set<std::string> trigger_contents;
108+
lua_getfield(L, current_abm, "nodenames");
109+
if (lua_istable(L, -1)) {
110+
int table = lua_gettop(L);
111+
lua_pushnil(L);
112+
while (lua_next(L, table)) {
113+
// key at index -2 and value at index -1
114+
luaL_checktype(L, -1, LUA_TSTRING);
118115
trigger_contents.insert(lua_tostring(L, -1));
116+
// removes value, keeps key for next iteration
117+
lua_pop(L, 1);
119118
}
120-
lua_pop(L, 1);
121-
122-
std::set<std::string> required_neighbors;
123-
lua_getfield(L, current_abm, "neighbors");
124-
if(lua_istable(L, -1)){
125-
int table = lua_gettop(L);
126-
lua_pushnil(L);
127-
while(lua_next(L, table) != 0){
128-
// key at index -2 and value at index -1
129-
luaL_checktype(L, -1, LUA_TSTRING);
130-
required_neighbors.insert(lua_tostring(L, -1));
131-
// removes value, keeps key for next iteration
132-
lua_pop(L, 1);
133-
}
134-
} else if(lua_isstring(L, -1)){
119+
} else if (lua_isstring(L, -1)) {
120+
trigger_contents.insert(lua_tostring(L, -1));
121+
}
122+
lua_pop(L, 1);
123+
124+
std::set<std::string> required_neighbors;
125+
lua_getfield(L, current_abm, "neighbors");
126+
if (lua_istable(L, -1)) {
127+
int table = lua_gettop(L);
128+
lua_pushnil(L);
129+
while (lua_next(L, table)) {
130+
// key at index -2 and value at index -1
131+
luaL_checktype(L, -1, LUA_TSTRING);
135132
required_neighbors.insert(lua_tostring(L, -1));
133+
// removes value, keeps key for next iteration
134+
lua_pop(L, 1);
136135
}
137-
lua_pop(L, 1);
136+
} else if (lua_isstring(L, -1)) {
137+
required_neighbors.insert(lua_tostring(L, -1));
138+
}
139+
lua_pop(L, 1);
138140

139-
float trigger_interval = 10.0;
140-
getfloatfield(L, current_abm, "interval", trigger_interval);
141+
float trigger_interval = 10.0;
142+
getfloatfield(L, current_abm, "interval", trigger_interval);
141143

142-
int trigger_chance = 50;
143-
getintfield(L, current_abm, "chance", trigger_chance);
144+
int trigger_chance = 50;
145+
getintfield(L, current_abm, "chance", trigger_chance);
144146

145-
bool simple_catch_up = true;
146-
getboolfield(L, current_abm, "catch_up", simple_catch_up);
147+
bool simple_catch_up = true;
148+
getboolfield(L, current_abm, "catch_up", simple_catch_up);
147149

148-
LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
149-
trigger_interval, trigger_chance, simple_catch_up);
150+
LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
151+
trigger_interval, trigger_chance, simple_catch_up);
150152

151-
env->addActiveBlockModifier(abm);
153+
env->addActiveBlockModifier(abm);
152154

153-
// removes value, keeps key for next iteration
154-
lua_pop(L, 1);
155-
}
156-
} else {
155+
// removes value, keeps key for next iteration
157156
lua_pop(L, 1);
158-
throw LuaError("core.registered_abms was not a lua table, as expected.");
159157
}
160158
lua_pop(L, 1);
161159

Diff for: ‎src/script/cpp_api/s_env.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2626
class ServerEnvironment;
2727
struct ScriptCallbackState;
2828

29-
class ScriptApiEnv : virtual public ScriptApiBase {
29+
class ScriptApiEnv : virtual public ScriptApiBase
30+
{
3031
public:
3132
// Called on environment step
3233
void environment_Step(float dtime);
@@ -35,7 +36,7 @@ class ScriptApiEnv : virtual public ScriptApiBase {
3536
void environment_OnGenerated(v3s16 minp, v3s16 maxp, u32 blockseed);
3637

3738
// Called on player event
38-
void player_event(ServerActiveObject *player, std::string type);
39+
void player_event(ServerActiveObject *player, const std::string &type);
3940

4041
// Called after emerge of a block queued from core.emerge_area()
4142
void on_emerge_area_completion(v3s16 blockpos, int action,

0 commit comments

Comments
 (0)
Please sign in to comment.