]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_env.cpp
Add minetest.register_lbm() to run code on block load only
[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, 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                 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)){
118                                 trigger_contents.insert(lua_tostring(L, -1));
119                         }
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)){
135                                 required_neighbors.insert(lua_tostring(L, -1));
136                         }
137                         lua_pop(L, 1);
138
139                         float trigger_interval = 10.0;
140                         getfloatfield(L, current_abm, "interval", trigger_interval);
141
142                         int trigger_chance = 50;
143                         getintfield(L, current_abm, "chance", trigger_chance);
144
145                         bool simple_catch_up = true;
146                         getboolfield(L, current_abm, "catch_up", simple_catch_up);
147
148                         LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
149                                 trigger_interval, trigger_chance, simple_catch_up);
150
151                         env->addActiveBlockModifier(abm);
152
153                         // removes value, keeps key for next iteration
154                         lua_pop(L, 1);
155                 }
156         } else {
157                 lua_pop(L, 1);
158                 throw LuaError("core.registered_abms was not a lua table, as expected.");
159         }
160         lua_pop(L, 1);
161
162         // Get core.registered_lbms
163         lua_getglobal(L, "core");
164         lua_getfield(L, -1, "registered_lbms");
165         int registered_lbms = lua_gettop(L);
166
167         if (!lua_istable(L, registered_lbms)) {
168                 lua_pop(L, 1);
169                 throw LuaError("core.registered_lbms was not a lua table, as expected.");
170         }
171
172         lua_pushnil(L);
173         while (lua_next(L, registered_lbms)) {
174                 // key at index -2 and value at index -1
175                 int id = lua_tonumber(L, -2);
176                 int current_lbm = lua_gettop(L);
177
178                 std::set<std::string> trigger_contents;
179                 lua_getfield(L, current_lbm, "nodenames");
180                 if (lua_istable(L, -1)) {
181                         int table = lua_gettop(L);
182                         lua_pushnil(L);
183                         while (lua_next(L, table)) {
184                                 // key at index -2 and value at index -1
185                                 luaL_checktype(L, -1, LUA_TSTRING);
186                                 trigger_contents.insert(lua_tostring(L, -1));
187                                 // removes value, keeps key for next iteration
188                                 lua_pop(L, 1);
189                         }
190                 } else if (lua_isstring(L, -1)) {
191                         trigger_contents.insert(lua_tostring(L, -1));
192                 }
193                 lua_pop(L, 1);
194
195                 std::string name;
196                 getstringfield(L, current_lbm, "name", name);
197
198                 bool run_at_every_load = getboolfield_default(L, current_lbm,
199                         "run_at_every_load", false);
200
201                 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
202                         run_at_every_load);
203
204                 env->addLoadingBlockModifierDef(lbm);
205
206                 // removes value, keeps key for next iteration
207                 lua_pop(L, 1);
208         }
209         lua_pop(L, 1);
210 }
211
212 void ScriptApiEnv::on_emerge_area_completion(
213         v3s16 blockpos, int action, ScriptCallbackState *state)
214 {
215         Server *server = getServer();
216
217         // Note that the order of these locks is important!  Envlock must *ALWAYS*
218         // be acquired before attempting to acquire scriptlock, or else ServerThread
219         // will try to acquire scriptlock after it already owns envlock, thus
220         // deadlocking EmergeThread and ServerThread
221         MutexAutoLock envlock(server->m_env_mutex);
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 }