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