]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/cpp_api/s_env.cpp
Merge remote-tracking branch 'origin/master'
[dragonfireclient.git] / src / script / cpp_api / s_env.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "cpp_api/s_env.h"
21 #include "common/c_converter.h"
22 #include "log.h"
23 #include "environment.h"
24 #include "lua_api/l_env.h"
25
26 extern "C" {
27 #include "lauxlib.h"
28 }
29
30 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
31                 u32 blockseed)
32 {
33         SCRIPTAPI_PRECHECKHEADER
34
35         // Get minetest.registered_on_generateds
36         lua_getglobal(L, "minetest");
37         lua_getfield(L, -1, "registered_on_generateds");
38         // Call callbacks
39         push_v3s16(L, minp);
40         push_v3s16(L, maxp);
41         lua_pushnumber(L, blockseed);
42         runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
43 }
44
45 void ScriptApiEnv::environment_Step(float dtime)
46 {
47         SCRIPTAPI_PRECHECKHEADER
48         //infostream<<"scriptapi_environment_step"<<std::endl;
49
50         // Get minetest.registered_globalsteps
51         lua_getglobal(L, "minetest");
52         lua_getfield(L, -1, "registered_globalsteps");
53         // Call callbacks
54         lua_pushnumber(L, dtime);
55         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
56 }
57
58 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
59 {
60         SCRIPTAPI_PRECHECKHEADER
61         verbosestream<<"scriptapi_add_environment"<<std::endl;
62         setEnv(env);
63
64         /*
65                 Add ActiveBlockModifiers to environment
66         */
67
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);
73
74         if(lua_istable(L, registered_abms)){
75                 int table = lua_gettop(L);
76                 lua_pushnil(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);
81
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);
86                                 lua_pushnil(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
92                                         lua_pop(L, 1);
93                                 }
94                         } else if(lua_isstring(L, -1)){
95                                 trigger_contents.insert(lua_tostring(L, -1));
96                         }
97                         lua_pop(L, 1);
98
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);
103                                 lua_pushnil(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
109                                         lua_pop(L, 1);
110                                 }
111                         } else if(lua_isstring(L, -1)){
112                                 required_neighbors.insert(lua_tostring(L, -1));
113                         }
114                         lua_pop(L, 1);
115
116                         float trigger_interval = 10.0;
117                         getfloatfield(L, current_abm, "interval", trigger_interval);
118
119                         int trigger_chance = 50;
120                         getintfield(L, current_abm, "chance", trigger_chance);
121
122                         LuaABM *abm = new LuaABM(L, id, trigger_contents,
123                                         required_neighbors, trigger_interval, trigger_chance);
124
125                         env->addActiveBlockModifier(abm);
126
127                         // removes value, keeps key for next iteration
128                         lua_pop(L, 1);
129                 }
130         }
131         lua_pop(L, 1);
132 }