]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_env.cpp
Revert "Make Lint Happy"
[dragonfireclient.git] / src / script / cpp_api / s_env.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "cpp_api/s_env.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "log.h"
24 #include "environment.h"
25 #include "mapgen/mapgen.h"
26 #include "lua_api/l_env.h"
27 #include "server.h"
28
29 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
30         u32 blockseed)
31 {
32         SCRIPTAPI_PRECHECKHEADER
33
34         // Get core.registered_on_generateds
35         lua_getglobal(L, "core");
36         lua_getfield(L, -1, "registered_on_generateds");
37         // Call callbacks
38         push_v3s16(L, minp);
39         push_v3s16(L, maxp);
40         lua_pushnumber(L, blockseed);
41         runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
42 }
43
44 void ScriptApiEnv::environment_Step(float dtime)
45 {
46         SCRIPTAPI_PRECHECKHEADER
47         //infostream << "scriptapi_environment_step" << std::endl;
48
49         // Get core.registered_globalsteps
50         lua_getglobal(L, "core");
51         lua_getfield(L, -1, "registered_globalsteps");
52         // Call callbacks
53         lua_pushnumber(L, dtime);
54         try {
55                 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
56         } catch (LuaError &e) {
57                 getServer()->setAsyncFatalError(
58                                 std::string("environment_Step: ") + e.what() + "\n"
59                                 + script_get_backtrace(L));
60         }
61 }
62
63 void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
64 {
65         SCRIPTAPI_PRECHECKHEADER
66
67         if (player == NULL)
68                 return;
69
70         // Get minetest.registered_playerevents
71         lua_getglobal(L, "minetest");
72         lua_getfield(L, -1, "registered_playerevents");
73
74         // Call callbacks
75         objectrefGetOrCreate(L, player);   // player
76         lua_pushstring(L,type.c_str()); // event type
77         try {
78                 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
79         } catch (LuaError &e) {
80                 getServer()->setAsyncFatalError(
81                                 std::string("player_event: ") + e.what() + "\n"
82                                 + script_get_backtrace(L) );
83         }
84 }
85
86 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
87 {
88         SCRIPTAPI_PRECHECKHEADER
89         verbosestream << "ScriptApiEnv: Environment initialized" << std::endl;
90         setEnv(env);
91
92         /*
93                 Add {Loading,Active}BlockModifiers to environment
94         */
95
96         // Get core.registered_abms
97         lua_getglobal(L, "core");
98         lua_getfield(L, -1, "registered_abms");
99         int registered_abms = lua_gettop(L);
100
101         if (!lua_istable(L, registered_abms)) {
102                 lua_pop(L, 1);
103                 throw LuaError("core.registered_abms was not a lua table, as expected.");
104         }
105         lua_pushnil(L);
106         while (lua_next(L, registered_abms)) {
107                 // key at index -2 and value at index -1
108                 int id = lua_tonumber(L, -2);
109                 int current_abm = lua_gettop(L);
110
111                 std::vector<std::string> trigger_contents;
112                 lua_getfield(L, current_abm, "nodenames");
113                 if (lua_istable(L, -1)) {
114                         int table = lua_gettop(L);
115                         lua_pushnil(L);
116                         while (lua_next(L, table)) {
117                                 // key at index -2 and value at index -1
118                                 luaL_checktype(L, -1, LUA_TSTRING);
119                                 trigger_contents.emplace_back(readParam<std::string>(L, -1));
120                                 // removes value, keeps key for next iteration
121                                 lua_pop(L, 1);
122                         }
123                 } else if (lua_isstring(L, -1)) {
124                         trigger_contents.emplace_back(readParam<std::string>(L, -1));
125                 }
126                 lua_pop(L, 1);
127
128                 std::vector<std::string> required_neighbors;
129                 lua_getfield(L, current_abm, "neighbors");
130                 if (lua_istable(L, -1)) {
131                         int table = lua_gettop(L);
132                         lua_pushnil(L);
133                         while (lua_next(L, table)) {
134                                 // key at index -2 and value at index -1
135                                 luaL_checktype(L, -1, LUA_TSTRING);
136                                 required_neighbors.emplace_back(readParam<std::string>(L, -1));
137                                 // removes value, keeps key for next iteration
138                                 lua_pop(L, 1);
139                         }
140                 } else if (lua_isstring(L, -1)) {
141                         required_neighbors.emplace_back(readParam<std::string>(L, -1));
142                 }
143                 lua_pop(L, 1);
144
145                 float trigger_interval = 10.0;
146                 getfloatfield(L, current_abm, "interval", trigger_interval);
147
148                 int trigger_chance = 50;
149                 getintfield(L, current_abm, "chance", trigger_chance);
150
151                 bool simple_catch_up = true;
152                 getboolfield(L, current_abm, "catch_up", simple_catch_up);
153
154                 lua_getfield(L, current_abm, "action");
155                 luaL_checktype(L, current_abm + 1, LUA_TFUNCTION);
156                 lua_pop(L, 1);
157
158                 LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
159                         trigger_interval, trigger_chance, simple_catch_up);
160
161                 env->addActiveBlockModifier(abm);
162
163                 // removes value, keeps key for next iteration
164                 lua_pop(L, 1);
165         }
166         lua_pop(L, 1);
167
168         // Get core.registered_lbms
169         lua_getglobal(L, "core");
170         lua_getfield(L, -1, "registered_lbms");
171         int registered_lbms = lua_gettop(L);
172
173         if (!lua_istable(L, registered_lbms)) {
174                 lua_pop(L, 1);
175                 throw LuaError("core.registered_lbms was not a lua table, as expected.");
176         }
177
178         lua_pushnil(L);
179         while (lua_next(L, registered_lbms)) {
180                 // key at index -2 and value at index -1
181                 int id = lua_tonumber(L, -2);
182                 int current_lbm = lua_gettop(L);
183
184                 std::set<std::string> trigger_contents;
185                 lua_getfield(L, current_lbm, "nodenames");
186                 if (lua_istable(L, -1)) {
187                         int table = lua_gettop(L);
188                         lua_pushnil(L);
189                         while (lua_next(L, table)) {
190                                 // key at index -2 and value at index -1
191                                 luaL_checktype(L, -1, LUA_TSTRING);
192                                 trigger_contents.insert(readParam<std::string>(L, -1));
193                                 // removes value, keeps key for next iteration
194                                 lua_pop(L, 1);
195                         }
196                 } else if (lua_isstring(L, -1)) {
197                         trigger_contents.insert(readParam<std::string>(L, -1));
198                 }
199                 lua_pop(L, 1);
200
201                 std::string name;
202                 getstringfield(L, current_lbm, "name", name);
203
204                 bool run_at_every_load = getboolfield_default(L, current_lbm,
205                         "run_at_every_load", false);
206
207                 lua_getfield(L, current_lbm, "action");
208                 luaL_checktype(L, current_lbm + 1, LUA_TFUNCTION);
209                 lua_pop(L, 1);
210
211                 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
212                         run_at_every_load);
213
214                 env->addLoadingBlockModifierDef(lbm);
215
216                 // removes value, keeps key for next iteration
217                 lua_pop(L, 1);
218         }
219         lua_pop(L, 1);
220 }
221
222 void ScriptApiEnv::on_emerge_area_completion(
223         v3s16 blockpos, int action, ScriptCallbackState *state)
224 {
225         Server *server = getServer();
226
227         // This function should be executed with envlock held.
228         // The caller (LuaEmergeAreaCallback in src/script/lua_api/l_env.cpp)
229         // should have obtained the lock.
230         // Note that the order of these locks is important!  Envlock must *ALWAYS*
231         // be acquired before attempting to acquire scriptlock, or else ServerThread
232         // will try to acquire scriptlock after it already owns envlock, thus
233         // deadlocking EmergeThread and ServerThread
234
235         SCRIPTAPI_PRECHECKHEADER
236
237         int error_handler = PUSH_ERROR_HANDLER(L);
238
239         lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
240         luaL_checktype(L, -1, LUA_TFUNCTION);
241
242         push_v3s16(L, blockpos);
243         lua_pushinteger(L, action);
244         lua_pushinteger(L, state->refcount);
245         lua_rawgeti(L, LUA_REGISTRYINDEX, state->args_ref);
246
247         setOriginDirect(state->origin.c_str());
248
249         try {
250                 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
251         } catch (LuaError &e) {
252                 server->setAsyncFatalError(
253                                 std::string("on_emerge_area_completion: ") + e.what() + "\n"
254                                 + script_get_backtrace(L));
255         }
256
257         lua_pop(L, 1); // Pop error handler
258
259         if (state->refcount == 0) {
260                 luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
261                 luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
262         }
263 }