3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #include "cpp_api/s_env.h"
21 #include "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
24 #include "environment.h"
25 #include "mapgen/mapgen.h"
26 #include "lua_api/l_env.h"
29 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
32 SCRIPTAPI_PRECHECKHEADER
34 // Get core.registered_on_generateds
35 lua_getglobal(L, "core");
36 lua_getfield(L, -1, "registered_on_generateds");
40 lua_pushnumber(L, blockseed);
41 runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
44 void ScriptApiEnv::environment_Step(float dtime)
46 SCRIPTAPI_PRECHECKHEADER
47 //infostream << "scriptapi_environment_step" << std::endl;
49 // Get core.registered_globalsteps
50 lua_getglobal(L, "core");
51 lua_getfield(L, -1, "registered_globalsteps");
53 lua_pushnumber(L, dtime);
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));
63 void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
65 SCRIPTAPI_PRECHECKHEADER
70 // Get minetest.registered_playerevents
71 lua_getglobal(L, "minetest");
72 lua_getfield(L, -1, "registered_playerevents");
75 objectrefGetOrCreate(L, player); // player
76 lua_pushstring(L,type.c_str()); // event type
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) );
86 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
88 SCRIPTAPI_PRECHECKHEADER
89 verbosestream << "scriptapi_add_environment" << std::endl;
93 Add {Loading,Active}BlockModifiers to environment
96 // Get core.registered_abms
97 lua_getglobal(L, "core");
98 lua_getfield(L, -1, "registered_abms");
99 int registered_abms = lua_gettop(L);
101 if (!lua_istable(L, registered_abms)) {
103 throw LuaError("core.registered_abms was not a lua table, as expected.");
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);
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);
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
123 } else if (lua_isstring(L, -1)) {
124 trigger_contents.push_back(lua_tostring(L, -1));
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);
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
140 } else if (lua_isstring(L, -1)) {
141 required_neighbors.push_back(lua_tostring(L, -1));
145 float trigger_interval = 10.0;
146 getfloatfield(L, current_abm, "interval", trigger_interval);
148 int trigger_chance = 50;
149 getintfield(L, current_abm, "chance", trigger_chance);
151 bool simple_catch_up = true;
152 getboolfield(L, current_abm, "catch_up", simple_catch_up);
154 LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
155 trigger_interval, trigger_chance, simple_catch_up);
157 env->addActiveBlockModifier(abm);
159 // removes value, keeps key for next iteration
164 // Get core.registered_lbms
165 lua_getglobal(L, "core");
166 lua_getfield(L, -1, "registered_lbms");
167 int registered_lbms = lua_gettop(L);
169 if (!lua_istable(L, registered_lbms)) {
171 throw LuaError("core.registered_lbms was not a lua table, as expected.");
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);
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);
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
192 } else if (lua_isstring(L, -1)) {
193 trigger_contents.insert(lua_tostring(L, -1));
198 getstringfield(L, current_lbm, "name", name);
200 bool run_at_every_load = getboolfield_default(L, current_lbm,
201 "run_at_every_load", false);
203 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
206 env->addLoadingBlockModifierDef(lbm);
208 // removes value, keeps key for next iteration
214 void ScriptApiEnv::on_emerge_area_completion(
215 v3s16 blockpos, int action, ScriptCallbackState *state)
217 Server *server = getServer();
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
227 SCRIPTAPI_PRECHECKHEADER
229 int error_handler = PUSH_ERROR_HANDLER(L);
231 lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
232 luaL_checktype(L, -1, LUA_TFUNCTION);
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);
239 setOriginDirect(state->origin.c_str());
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));
249 lua_pop(L, 1); // Pop error handler
251 if (state->refcount == 0) {
252 luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
253 luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);