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"
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(e.what());
61 void ScriptApiEnv::player_event(ServerActiveObject *player, const std::string &type)
63 SCRIPTAPI_PRECHECKHEADER
68 // Get minetest.registered_playerevents
69 lua_getglobal(L, "minetest");
70 lua_getfield(L, -1, "registered_playerevents");
73 objectrefGetOrCreate(L, player); // player
74 lua_pushstring(L,type.c_str()); // event type
76 runCallbacks(2, RUN_CALLBACKS_MODE_FIRST);
77 } catch (LuaError &e) {
78 getServer()->setAsyncFatalError(e.what());
82 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
84 SCRIPTAPI_PRECHECKHEADER
85 verbosestream << "scriptapi_add_environment" << std::endl;
89 Add {Loading,Active}BlockModifiers to environment
92 // Get core.registered_abms
93 lua_getglobal(L, "core");
94 lua_getfield(L, -1, "registered_abms");
95 int registered_abms = lua_gettop(L);
97 if (!lua_istable(L, registered_abms)) {
99 throw LuaError("core.registered_abms was not a lua table, as expected.");
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);
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);
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
119 } else if (lua_isstring(L, -1)) {
120 trigger_contents.insert(lua_tostring(L, -1));
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);
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
136 } else if (lua_isstring(L, -1)) {
137 required_neighbors.insert(lua_tostring(L, -1));
141 float trigger_interval = 10.0;
142 getfloatfield(L, current_abm, "interval", trigger_interval);
144 int trigger_chance = 50;
145 getintfield(L, current_abm, "chance", trigger_chance);
147 bool simple_catch_up = true;
148 getboolfield(L, current_abm, "catch_up", simple_catch_up);
150 LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
151 trigger_interval, trigger_chance, simple_catch_up);
153 env->addActiveBlockModifier(abm);
155 // removes value, keeps key for next iteration
160 // Get core.registered_lbms
161 lua_getglobal(L, "core");
162 lua_getfield(L, -1, "registered_lbms");
163 int registered_lbms = lua_gettop(L);
165 if (!lua_istable(L, registered_lbms)) {
167 throw LuaError("core.registered_lbms was not a lua table, as expected.");
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);
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);
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
188 } else if (lua_isstring(L, -1)) {
189 trigger_contents.insert(lua_tostring(L, -1));
194 getstringfield(L, current_lbm, "name", name);
196 bool run_at_every_load = getboolfield_default(L, current_lbm,
197 "run_at_every_load", false);
199 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
202 env->addLoadingBlockModifierDef(lbm);
204 // removes value, keeps key for next iteration
210 void ScriptApiEnv::on_emerge_area_completion(
211 v3s16 blockpos, int action, ScriptCallbackState *state)
213 Server *server = getServer();
215 // Note that the order of these locks is important! Envlock must *ALWAYS*
216 // be acquired before attempting to acquire scriptlock, or else ServerThread
217 // will try to acquire scriptlock after it already owns envlock, thus
218 // deadlocking EmergeThread and ServerThread
219 MutexAutoLock envlock(server->m_env_mutex);
221 SCRIPTAPI_PRECHECKHEADER
223 int error_handler = PUSH_ERROR_HANDLER(L);
225 lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
226 luaL_checktype(L, -1, LUA_TFUNCTION);
228 push_v3s16(L, blockpos);
229 lua_pushinteger(L, action);
230 lua_pushinteger(L, state->refcount);
231 lua_rawgeti(L, LUA_REGISTRYINDEX, state->args_ref);
233 setOriginDirect(state->origin.c_str());
236 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
237 } catch (LuaError &e) {
238 server->setAsyncFatalError(e.what());
241 lua_pop(L, 1); // Pop error handler
243 if (state->refcount == 0) {
244 luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
245 luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);