]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_env.cpp
Emergeblocks: Fix occasional crash
[minetest.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.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(e.what());
58         }
59 }
60
61 void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
62 {
63         SCRIPTAPI_PRECHECKHEADER
64
65         if (player == NULL)
66                 return;
67
68         // Get minetest.registered_playerevents
69         lua_getglobal(L, "minetest");
70         lua_getfield(L, -1, "registered_playerevents");
71
72         // Call callbacks
73         objectrefGetOrCreate(L, player);   // player
74         lua_pushstring(L,type.c_str()); // event type
75         try {
76                 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
77         } catch (LuaError &e) {
78                 getServer()->setAsyncFatalError(e.what());
79         }
80 }
81
82 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
83 {
84         SCRIPTAPI_PRECHECKHEADER
85         verbosestream << "scriptapi_add_environment" << std::endl;
86         setEnv(env);
87
88         /*
89                 Add {Loading,Active}BlockModifiers to environment
90         */
91
92         // Get core.registered_abms
93         lua_getglobal(L, "core");
94         lua_getfield(L, -1, "registered_abms");
95         int registered_abms = lua_gettop(L);
96
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);
115                                 trigger_contents.insert(lua_tostring(L, -1));
116                                 // removes value, keeps key for next iteration
117                                 lua_pop(L, 1);
118                         }
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);
132                                 required_neighbors.insert(lua_tostring(L, -1));
133                                 // removes value, keeps key for next iteration
134                                 lua_pop(L, 1);
135                         }
136                 } else if (lua_isstring(L, -1)) {
137                         required_neighbors.insert(lua_tostring(L, -1));
138                 }
139                 lua_pop(L, 1);
140
141                 float trigger_interval = 10.0;
142                 getfloatfield(L, current_abm, "interval", trigger_interval);
143
144                 int trigger_chance = 50;
145                 getintfield(L, current_abm, "chance", trigger_chance);
146
147                 bool simple_catch_up = true;
148                 getboolfield(L, current_abm, "catch_up", simple_catch_up);
149
150                 LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
151                         trigger_interval, trigger_chance, simple_catch_up);
152
153                 env->addActiveBlockModifier(abm);
154
155                 // removes value, keeps key for next iteration
156                 lua_pop(L, 1);
157         }
158         lua_pop(L, 1);
159
160         // Get core.registered_lbms
161         lua_getglobal(L, "core");
162         lua_getfield(L, -1, "registered_lbms");
163         int registered_lbms = lua_gettop(L);
164
165         if (!lua_istable(L, registered_lbms)) {
166                 lua_pop(L, 1);
167                 throw LuaError("core.registered_lbms was not a lua table, as expected.");
168         }
169
170         lua_pushnil(L);
171         while (lua_next(L, registered_lbms)) {
172                 // key at index -2 and value at index -1
173                 int id = lua_tonumber(L, -2);
174                 int current_lbm = lua_gettop(L);
175
176                 std::set<std::string> trigger_contents;
177                 lua_getfield(L, current_lbm, "nodenames");
178                 if (lua_istable(L, -1)) {
179                         int table = lua_gettop(L);
180                         lua_pushnil(L);
181                         while (lua_next(L, table)) {
182                                 // key at index -2 and value at index -1
183                                 luaL_checktype(L, -1, LUA_TSTRING);
184                                 trigger_contents.insert(lua_tostring(L, -1));
185                                 // removes value, keeps key for next iteration
186                                 lua_pop(L, 1);
187                         }
188                 } else if (lua_isstring(L, -1)) {
189                         trigger_contents.insert(lua_tostring(L, -1));
190                 }
191                 lua_pop(L, 1);
192
193                 std::string name;
194                 getstringfield(L, current_lbm, "name", name);
195
196                 bool run_at_every_load = getboolfield_default(L, current_lbm,
197                         "run_at_every_load", false);
198
199                 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
200                         run_at_every_load);
201
202                 env->addLoadingBlockModifierDef(lbm);
203
204                 // removes value, keeps key for next iteration
205                 lua_pop(L, 1);
206         }
207         lua_pop(L, 1);
208 }
209
210 void ScriptApiEnv::on_emerge_area_completion(
211         v3s16 blockpos, int action, ScriptCallbackState *state)
212 {
213         Server *server = getServer();
214
215         // This function should be executed with envlock held.
216         // The caller (LuaEmergeAreaCallback in src/script/lua_api/l_env.cpp)
217         // should have obtained the lock.
218         // Note that the order of these locks is important!  Envlock must *ALWAYS*
219         // be acquired before attempting to acquire scriptlock, or else ServerThread
220         // will try to acquire scriptlock after it already owns envlock, thus
221         // deadlocking EmergeThread and ServerThread
222
223         SCRIPTAPI_PRECHECKHEADER
224
225         int error_handler = PUSH_ERROR_HANDLER(L);
226
227         lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
228         luaL_checktype(L, -1, LUA_TFUNCTION);
229
230         push_v3s16(L, blockpos);
231         lua_pushinteger(L, action);
232         lua_pushinteger(L, state->refcount);
233         lua_rawgeti(L, LUA_REGISTRYINDEX, state->args_ref);
234
235         setOriginDirect(state->origin.c_str());
236
237         try {
238                 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
239         } catch (LuaError &e) {
240                 server->setAsyncFatalError(e.what());
241         }
242
243         lua_pop(L, 1); // Pop error handler
244
245         if (state->refcount == 0) {
246                 luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
247                 luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
248         }
249 }