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, 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)){
98 int table = lua_gettop(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);
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);
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
117 } else if(lua_isstring(L, -1)){
118 trigger_contents.insert(lua_tostring(L, -1));
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);
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
134 } else if(lua_isstring(L, -1)){
135 required_neighbors.insert(lua_tostring(L, -1));
139 float trigger_interval = 10.0;
140 getfloatfield(L, current_abm, "interval", trigger_interval);
142 int trigger_chance = 50;
143 getintfield(L, current_abm, "chance", trigger_chance);
145 bool simple_catch_up = true;
146 getboolfield(L, current_abm, "catch_up", simple_catch_up);
148 LuaABM *abm = new LuaABM(L, id, trigger_contents, required_neighbors,
149 trigger_interval, trigger_chance, simple_catch_up);
151 env->addActiveBlockModifier(abm);
153 // removes value, keeps key for next iteration
158 throw LuaError("core.registered_abms was not a lua table, as expected.");
162 // Get core.registered_lbms
163 lua_getglobal(L, "core");
164 lua_getfield(L, -1, "registered_lbms");
165 int registered_lbms = lua_gettop(L);
167 if (!lua_istable(L, registered_lbms)) {
169 throw LuaError("core.registered_lbms was not a lua table, as expected.");
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);
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);
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
190 } else if (lua_isstring(L, -1)) {
191 trigger_contents.insert(lua_tostring(L, -1));
196 getstringfield(L, current_lbm, "name", name);
198 bool run_at_every_load = getboolfield_default(L, current_lbm,
199 "run_at_every_load", false);
201 LuaLBM *lbm = new LuaLBM(L, id, trigger_contents, name,
204 env->addLoadingBlockModifierDef(lbm);
206 // removes value, keeps key for next iteration
212 void ScriptApiEnv::on_emerge_area_completion(
213 v3s16 blockpos, int action, ScriptCallbackState *state)
215 Server *server = getServer();
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);
223 SCRIPTAPI_PRECHECKHEADER
225 int error_handler = PUSH_ERROR_HANDLER(L);
227 lua_rawgeti(L, LUA_REGISTRYINDEX, state->callback_ref);
228 luaL_checktype(L, -1, LUA_TFUNCTION);
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);
235 setOriginDirect(state->origin.c_str());
238 PCALL_RES(lua_pcall(L, 4, 0, error_handler));
239 } catch (LuaError &e) {
240 server->setAsyncFatalError(e.what());
243 lua_pop(L, 1); // Pop error handler
245 if (state->refcount == 0) {
246 luaL_unref(L, LUA_REGISTRYINDEX, state->callback_ref);
247 luaL_unref(L, LUA_REGISTRYINDEX, state->args_ref);