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 script_run_callbacks(L, 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 script_run_callbacks(L, 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(player); // player
74 lua_pushstring(L,type.c_str()); // event type
76 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
77 } catch (LuaError &e) {
78 getServer()->setAsyncFatalError(e.what());
82 void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams)
84 SCRIPTAPI_PRECHECKHEADER
86 // Get core.registered_on_mapgen_inits
87 lua_getglobal(L, "core");
88 lua_getfield(L, -1, "registered_on_mapgen_inits");
93 lua_pushstring(L, mgparams->mg_name.c_str());
94 lua_setfield(L, -2, "mgname");
96 lua_pushinteger(L, mgparams->seed);
97 lua_setfield(L, -2, "seed");
99 lua_pushinteger(L, mgparams->water_level);
100 lua_setfield(L, -2, "water_level");
102 std::string flagstr = writeFlagString(mgparams->flags,
103 flagdesc_mapgen, (u32)-1);
104 lua_pushstring(L, flagstr.c_str());
105 lua_setfield(L, -2, "flags");
107 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
110 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
112 SCRIPTAPI_PRECHECKHEADER
113 verbosestream<<"scriptapi_add_environment"<<std::endl;
117 Add ActiveBlockModifiers to environment
120 // Get core.registered_abms
121 lua_getglobal(L, "core");
122 lua_getfield(L, -1, "registered_abms");
123 luaL_checktype(L, -1, LUA_TTABLE);
124 int registered_abms = lua_gettop(L);
126 if(lua_istable(L, registered_abms)){
127 int table = lua_gettop(L);
129 while(lua_next(L, table) != 0){
130 // key at index -2 and value at index -1
131 int id = lua_tonumber(L, -2);
132 int current_abm = lua_gettop(L);
134 std::set<std::string> trigger_contents;
135 lua_getfield(L, current_abm, "nodenames");
136 if(lua_istable(L, -1)){
137 int table = lua_gettop(L);
139 while(lua_next(L, table) != 0){
140 // key at index -2 and value at index -1
141 luaL_checktype(L, -1, LUA_TSTRING);
142 trigger_contents.insert(lua_tostring(L, -1));
143 // removes value, keeps key for next iteration
146 } else if(lua_isstring(L, -1)){
147 trigger_contents.insert(lua_tostring(L, -1));
151 std::set<std::string> required_neighbors;
152 lua_getfield(L, current_abm, "neighbors");
153 if(lua_istable(L, -1)){
154 int table = lua_gettop(L);
156 while(lua_next(L, table) != 0){
157 // key at index -2 and value at index -1
158 luaL_checktype(L, -1, LUA_TSTRING);
159 required_neighbors.insert(lua_tostring(L, -1));
160 // removes value, keeps key for next iteration
163 } else if(lua_isstring(L, -1)){
164 required_neighbors.insert(lua_tostring(L, -1));
168 float trigger_interval = 10.0;
169 getfloatfield(L, current_abm, "interval", trigger_interval);
171 int trigger_chance = 50;
172 getintfield(L, current_abm, "chance", trigger_chance);
174 LuaABM *abm = new LuaABM(L, id, trigger_contents,
175 required_neighbors, trigger_interval, trigger_chance);
177 env->addActiveBlockModifier(abm);
179 // removes value, keeps key for next iteration