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"
24 #include "lua_api/l_env.h"
30 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
33 SCRIPTAPI_PRECHECKHEADER
35 // Get minetest.registered_on_generateds
36 lua_getglobal(L, "minetest");
37 lua_getfield(L, -1, "registered_on_generateds");
41 lua_pushnumber(L, blockseed);
42 runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
45 void ScriptApiEnv::environment_Step(float dtime)
47 SCRIPTAPI_PRECHECKHEADER
48 //infostream<<"scriptapi_environment_step"<<std::endl;
50 // Get minetest.registered_globalsteps
51 lua_getglobal(L, "minetest");
52 lua_getfield(L, -1, "registered_globalsteps");
54 lua_pushnumber(L, dtime);
55 runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
58 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
60 SCRIPTAPI_PRECHECKHEADER
61 verbosestream<<"scriptapi_add_environment"<<std::endl;
65 Add ActiveBlockModifiers to environment
68 // Get minetest.registered_abms
69 lua_getglobal(L, "minetest");
70 lua_getfield(L, -1, "registered_abms");
71 luaL_checktype(L, -1, LUA_TTABLE);
72 int registered_abms = lua_gettop(L);
74 if(lua_istable(L, registered_abms)){
75 int table = lua_gettop(L);
77 while(lua_next(L, table) != 0){
78 // key at index -2 and value at index -1
79 int id = lua_tonumber(L, -2);
80 int current_abm = lua_gettop(L);
82 std::set<std::string> trigger_contents;
83 lua_getfield(L, current_abm, "nodenames");
84 if(lua_istable(L, -1)){
85 int table = lua_gettop(L);
87 while(lua_next(L, table) != 0){
88 // key at index -2 and value at index -1
89 luaL_checktype(L, -1, LUA_TSTRING);
90 trigger_contents.insert(lua_tostring(L, -1));
91 // removes value, keeps key for next iteration
94 } else if(lua_isstring(L, -1)){
95 trigger_contents.insert(lua_tostring(L, -1));
99 std::set<std::string> required_neighbors;
100 lua_getfield(L, current_abm, "neighbors");
101 if(lua_istable(L, -1)){
102 int table = lua_gettop(L);
104 while(lua_next(L, table) != 0){
105 // key at index -2 and value at index -1
106 luaL_checktype(L, -1, LUA_TSTRING);
107 required_neighbors.insert(lua_tostring(L, -1));
108 // removes value, keeps key for next iteration
111 } else if(lua_isstring(L, -1)){
112 required_neighbors.insert(lua_tostring(L, -1));
116 float trigger_interval = 10.0;
117 getfloatfield(L, current_abm, "interval", trigger_interval);
119 int trigger_chance = 50;
120 getintfield(L, current_abm, "chance", trigger_chance);
122 LuaABM *abm = new LuaABM(L, id, trigger_contents,
123 required_neighbors, trigger_interval, trigger_chance);
125 env->addActiveBlockModifier(abm);
127 // removes value, keeps key for next iteration