]> git.lizzy.rs Git - minetest.git/blob - src/script/cpp_api/s_env.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[minetest.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 "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "log.h"
24 #include "environment.h"
25 #include "mapgen.h"
26 #include "lua_api/l_env.h"
27
28 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
29                 u32 blockseed)
30 {
31         SCRIPTAPI_PRECHECKHEADER
32
33         // Get minetest.registered_on_generateds
34         lua_getglobal(L, "minetest");
35         lua_getfield(L, -1, "registered_on_generateds");
36         // Call callbacks
37         push_v3s16(L, minp);
38         push_v3s16(L, maxp);
39         lua_pushnumber(L, blockseed);
40         script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_FIRST);
41 }
42
43 void ScriptApiEnv::environment_Step(float dtime)
44 {
45         SCRIPTAPI_PRECHECKHEADER
46         //infostream<<"scriptapi_environment_step"<<std::endl;
47
48         // Get minetest.registered_globalsteps
49         lua_getglobal(L, "minetest");
50         lua_getfield(L, -1, "registered_globalsteps");
51         // Call callbacks
52         lua_pushnumber(L, dtime);
53         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
54 }
55
56 void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams)
57 {
58         SCRIPTAPI_PRECHECKHEADER
59         
60         // Get minetest.registered_on_mapgen_inits
61         lua_getglobal(L, "minetest");
62         lua_getfield(L, -1, "registered_on_mapgen_inits");
63
64         // Call callbacks
65         lua_newtable(L);
66         
67         lua_pushstring(L, mgparams->mg_name.c_str());
68         lua_setfield(L, -2, "mgname");
69         
70         lua_pushinteger(L, mgparams->seed);
71         lua_setfield(L, -2, "seed");
72         
73         lua_pushinteger(L, mgparams->water_level);
74         lua_setfield(L, -2, "water_level");
75         
76         std::string flagstr = writeFlagString(mgparams->flags, flagdesc_mapgen);
77         lua_pushstring(L, flagstr.c_str());
78         lua_setfield(L, -2, "flags");
79         
80         script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
81 }
82
83 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
84 {
85         SCRIPTAPI_PRECHECKHEADER
86         verbosestream<<"scriptapi_add_environment"<<std::endl;
87         setEnv(env);
88
89         /*
90                 Add ActiveBlockModifiers to environment
91         */
92
93         // Get minetest.registered_abms
94         lua_getglobal(L, "minetest");
95         lua_getfield(L, -1, "registered_abms");
96         luaL_checktype(L, -1, LUA_TTABLE);
97         int registered_abms = lua_gettop(L);
98
99         if(lua_istable(L, registered_abms)){
100                 int table = lua_gettop(L);
101                 lua_pushnil(L);
102                 while(lua_next(L, table) != 0){
103                         // key at index -2 and value at index -1
104                         int id = lua_tonumber(L, -2);
105                         int current_abm = lua_gettop(L);
106
107                         std::set<std::string> trigger_contents;
108                         lua_getfield(L, current_abm, "nodenames");
109                         if(lua_istable(L, -1)){
110                                 int table = lua_gettop(L);
111                                 lua_pushnil(L);
112                                 while(lua_next(L, table) != 0){
113                                         // key at index -2 and value at index -1
114                                         luaL_checktype(L, -1, LUA_TSTRING);
115                                         trigger_contents.insert(lua_tostring(L, -1));
116                                         // removes value, keeps key for next iteration
117                                         lua_pop(L, 1);
118                                 }
119                         } else if(lua_isstring(L, -1)){
120                                 trigger_contents.insert(lua_tostring(L, -1));
121                         }
122                         lua_pop(L, 1);
123
124                         std::set<std::string> required_neighbors;
125                         lua_getfield(L, current_abm, "neighbors");
126                         if(lua_istable(L, -1)){
127                                 int table = lua_gettop(L);
128                                 lua_pushnil(L);
129                                 while(lua_next(L, table) != 0){
130                                         // key at index -2 and value at index -1
131                                         luaL_checktype(L, -1, LUA_TSTRING);
132                                         required_neighbors.insert(lua_tostring(L, -1));
133                                         // removes value, keeps key for next iteration
134                                         lua_pop(L, 1);
135                                 }
136                         } else if(lua_isstring(L, -1)){
137                                 required_neighbors.insert(lua_tostring(L, -1));
138                         }
139                         lua_pop(L, 1);
140
141                         float trigger_interval = 10.0;
142                         getfloatfield(L, current_abm, "interval", trigger_interval);
143
144                         int trigger_chance = 50;
145                         getintfield(L, current_abm, "chance", trigger_chance);
146
147                         LuaABM *abm = new LuaABM(L, id, trigger_contents,
148                                         required_neighbors, trigger_interval, trigger_chance);
149
150                         env->addActiveBlockModifier(abm);
151
152                         // removes value, keeps key for next iteration
153                         lua_pop(L, 1);
154                 }
155         }
156         lua_pop(L, 1);
157 }