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 "common/c_converter.h"
23 #include "environment.h"
25 #include "lua_api/l_env.h"
31 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
34 SCRIPTAPI_PRECHECKHEADER
36 // Get minetest.registered_on_generateds
37 lua_getglobal(L, "minetest");
38 lua_getfield(L, -1, "registered_on_generateds");
42 lua_pushnumber(L, blockseed);
43 runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
46 void ScriptApiEnv::environment_Step(float dtime)
48 SCRIPTAPI_PRECHECKHEADER
49 //infostream<<"scriptapi_environment_step"<<std::endl;
51 // Get minetest.registered_globalsteps
52 lua_getglobal(L, "minetest");
53 lua_getfield(L, -1, "registered_globalsteps");
55 lua_pushnumber(L, dtime);
56 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
59 void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams)
61 SCRIPTAPI_PRECHECKHEADER
63 // Get minetest.registered_on_mapgen_inits
64 lua_getglobal(L, "minetest");
65 lua_getfield(L, -1, "registered_on_mapgen_inits");
70 lua_pushstring(L, mgparams->mg_name.c_str());
71 lua_setfield(L, -2, "mgname");
73 lua_pushinteger(L, mgparams->seed);
74 lua_setfield(L, -2, "seed");
76 lua_pushinteger(L, mgparams->water_level);
77 lua_setfield(L, -2, "water_level");
79 std::string flagstr = writeFlagString(mgparams->flags, flagdesc_mapgen);
80 lua_pushstring(L, flagstr.c_str());
81 lua_setfield(L, -2, "flags");
83 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
86 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
88 SCRIPTAPI_PRECHECKHEADER
89 verbosestream<<"scriptapi_add_environment"<<std::endl;
93 Add ActiveBlockModifiers to environment
96 // Get minetest.registered_abms
97 lua_getglobal(L, "minetest");
98 lua_getfield(L, -1, "registered_abms");
99 luaL_checktype(L, -1, LUA_TTABLE);
100 int registered_abms = lua_gettop(L);
102 if(lua_istable(L, registered_abms)){
103 int table = lua_gettop(L);
105 while(lua_next(L, table) != 0){
106 // key at index -2 and value at index -1
107 int id = lua_tonumber(L, -2);
108 int current_abm = lua_gettop(L);
110 std::set<std::string> trigger_contents;
111 lua_getfield(L, current_abm, "nodenames");
112 if(lua_istable(L, -1)){
113 int table = lua_gettop(L);
115 while(lua_next(L, table) != 0){
116 // key at index -2 and value at index -1
117 luaL_checktype(L, -1, LUA_TSTRING);
118 trigger_contents.insert(lua_tostring(L, -1));
119 // removes value, keeps key for next iteration
122 } else if(lua_isstring(L, -1)){
123 trigger_contents.insert(lua_tostring(L, -1));
127 std::set<std::string> required_neighbors;
128 lua_getfield(L, current_abm, "neighbors");
129 if(lua_istable(L, -1)){
130 int table = lua_gettop(L);
132 while(lua_next(L, table) != 0){
133 // key at index -2 and value at index -1
134 luaL_checktype(L, -1, LUA_TSTRING);
135 required_neighbors.insert(lua_tostring(L, -1));
136 // removes value, keeps key for next iteration
139 } else if(lua_isstring(L, -1)){
140 required_neighbors.insert(lua_tostring(L, -1));
144 float trigger_interval = 10.0;
145 getfloatfield(L, current_abm, "interval", trigger_interval);
147 int trigger_chance = 50;
148 getintfield(L, current_abm, "chance", trigger_chance);
150 LuaABM *abm = new LuaABM(L, id, trigger_contents,
151 required_neighbors, trigger_interval, trigger_chance);
153 env->addActiveBlockModifier(abm);
155 // removes value, keeps key for next iteration