Add Lua on_mapgen_init callback, and minetest.set_mapgen_params API
[oweals/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 "common/c_converter.h"
22 #include "log.h"
23 #include "environment.h"
24 #include "mapgen.h"
25 #include "lua_api/l_env.h"
26
27 extern "C" {
28 #include "lauxlib.h"
29 }
30
31 void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
32                 u32 blockseed)
33 {
34         SCRIPTAPI_PRECHECKHEADER
35
36         // Get minetest.registered_on_generateds
37         lua_getglobal(L, "minetest");
38         lua_getfield(L, -1, "registered_on_generateds");
39         // Call callbacks
40         push_v3s16(L, minp);
41         push_v3s16(L, maxp);
42         lua_pushnumber(L, blockseed);
43         runCallbacks(3, RUN_CALLBACKS_MODE_FIRST);
44 }
45
46 void ScriptApiEnv::environment_Step(float dtime)
47 {
48         SCRIPTAPI_PRECHECKHEADER
49         //infostream<<"scriptapi_environment_step"<<std::endl;
50
51         // Get minetest.registered_globalsteps
52         lua_getglobal(L, "minetest");
53         lua_getfield(L, -1, "registered_globalsteps");
54         // Call callbacks
55         lua_pushnumber(L, dtime);
56         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
57 }
58
59 void ScriptApiEnv::environment_OnMapgenInit(MapgenParams *mgparams)
60 {
61         SCRIPTAPI_PRECHECKHEADER
62         
63         // Get minetest.registered_on_mapgen_inits
64         lua_getglobal(L, "minetest");
65         lua_getfield(L, -1, "registered_on_mapgen_inits");
66
67         // Call callbacks
68         lua_newtable(L);
69         
70         lua_pushstring(L, mgparams->mg_name.c_str());
71         lua_setfield(L, -2, "mgname");
72         
73         lua_pushinteger(L, mgparams->seed);
74         lua_setfield(L, -2, "seed");
75         
76         lua_pushinteger(L, mgparams->water_level);
77         lua_setfield(L, -2, "water_level");
78         
79         std::string flagstr = writeFlagString(mgparams->flags, flagdesc_mapgen);
80         lua_pushstring(L, flagstr.c_str());
81         lua_setfield(L, -2, "flags");
82         
83         runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
84 }
85
86 void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
87 {
88         SCRIPTAPI_PRECHECKHEADER
89         verbosestream<<"scriptapi_add_environment"<<std::endl;
90         setEnv(env);
91
92         /*
93                 Add ActiveBlockModifiers to environment
94         */
95
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);
101
102         if(lua_istable(L, registered_abms)){
103                 int table = lua_gettop(L);
104                 lua_pushnil(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);
109
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);
114                                 lua_pushnil(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
120                                         lua_pop(L, 1);
121                                 }
122                         } else if(lua_isstring(L, -1)){
123                                 trigger_contents.insert(lua_tostring(L, -1));
124                         }
125                         lua_pop(L, 1);
126
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);
131                                 lua_pushnil(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
137                                         lua_pop(L, 1);
138                                 }
139                         } else if(lua_isstring(L, -1)){
140                                 required_neighbors.insert(lua_tostring(L, -1));
141                         }
142                         lua_pop(L, 1);
143
144                         float trigger_interval = 10.0;
145                         getfloatfield(L, current_abm, "interval", trigger_interval);
146
147                         int trigger_chance = 50;
148                         getintfield(L, current_abm, "chance", trigger_chance);
149
150                         LuaABM *abm = new LuaABM(L, id, trigger_contents,
151                                         required_neighbors, trigger_interval, trigger_chance);
152
153                         env->addActiveBlockModifier(abm);
154
155                         // removes value, keeps key for next iteration
156                         lua_pop(L, 1);
157                 }
158         }
159         lua_pop(L, 1);
160 }