]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_env.cpp
Move files to subdirectories (#6599)
[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/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 << "scriptapi_add_environment" << 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.push_back(lua_tostring(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.push_back(lua_tostring(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.push_back(lua_tostring(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.push_back(lua_tostring(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                 LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
155                         trigger_interval, trigger_chance, simple_catch_up);
156
157                 env->addActiveBlockModifier(abm);
158
159                 // removes value, keeps key for next iteration
160                 lua_pop(L, 1);
161         }
162         lua_pop(L, 1);
163
164         // Get core.registered_lbms
165         lua_getglobal(L, "core");
166         lua_getfield(L, -1, "registered_lbms");
167         int registered_lbms = lua_gettop(L);
168
169         if (!lua_istable(L, registered_lbms)) {
170                 lua_pop(L, 1);
171                 throw LuaError("core.registered_lbms was not a lua table, as expected.");
172         }
173
174         lua_pushnil(L);
175         while (lua_next(L, registered_lbms)) {
176                 // key at index -2 and value at index -1
177                 int id = lua_tonumber(L, -2);
178                 int current_lbm = lua_gettop(L);
179
180                 std::set<std::string> trigger_contents;
181                 lua_getfield(L, current_lbm, "nodenames");
182                 if (lua_istable(L, -1)) {
183                         int table = lua_gettop(L);
184                         lua_pushnil(L);
185                         while (lua_next(L, table)) {
186                                 // key at index -2 and value at index -1
187                                 luaL_checktype(L, -1, LUA_TSTRING);
188                                 trigger_contents.insert(lua_tostring(L, -1));
189                                 // removes value, keeps key for next iteration
190                                 lua_pop(L, 1);
191                         }
192                 } else if (lua_isstring(L, -1)) {
193                         trigger_contents.insert(lua_tostring(L, -1));
194                 }
195                 lua_pop(L, 1);
196
197                 std::string name;
198                 getstringfield(L, current_lbm, "name", name);
199
200                 bool run_at_every_load = getboolfield_default(L, current_lbm,
201                         "run_at_every_load", false);
202
203                 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
204                         run_at_every_load);
205
206                 env->addLoadingBlockModifierDef(lbm);
207
208                 // removes value, keeps key for next iteration
209                 lua_pop(L, 1);
210         }
211         lua_pop(L, 1);
212 }
213
214 void ScriptApiEnv::on_emerge_area_completion(
215         v3s16 blockpos, int action, ScriptCallbackState *state)
216 {
217         Server *server = getServer();
218
219         // This function should be executed with envlock held.
220         // The caller (LuaEmergeAreaCallback in src/script/lua_api/l_env.cpp)
221         // should have obtained the lock.
222         // Note that the order of these locks is important!  Envlock must *ALWAYS*
223         // be acquired before attempting to acquire scriptlock, or else ServerThread
224         // will try to acquire scriptlock after it already owns envlock, thus
225         // deadlocking EmergeThread and ServerThread
226
227         SCRIPTAPI_PRECHECKHEADER
228
229         int error_handler = PUSH_ERROR_HANDLER(L);
230
231         lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
232         luaL_checktype(L, -1, LUA_TFUNCTION);
233
234         push_v3s16(L, blockpos);
235         lua_pushinteger(L, action);
236         lua_pushinteger(L, state->refcount);
237         lua_rawgeti(L, LUA_REGISTRYINDEX, state->args_ref);
238
239         setOriginDirect(state->origin.c_str());
240
241         try {
242                 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
243         } catch (LuaError &e) {
244                 server->setAsyncFatalError(
245                                 std::string("on_emerge_area_completion: ") + e.what() + "\n"
246                                 + script_get_backtrace(L));
247         }
248
249         lua_pop(L, 1); // Pop error handler
250
251         if (state->refcount == 0) {
252                 luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
253                 luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);
254         }
255 }