LCOV - code coverage report
Current view: top level - src/script/cpp_api - s_env.cpp (source / functions) Hit Total Coverage
Test: report Lines: 1 76 1.3 %
Date: 2015-07-11 18:23:49 Functions: 2 6 33.3 %

          Line data    Source code
       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           0 : void ScriptApiEnv::environment_OnGenerated(v3s16 minp, v3s16 maxp,
      30             :                 u32 blockseed)
      31             : {
      32           0 :         SCRIPTAPI_PRECHECKHEADER
      33             : 
      34             :         // Get core.registered_on_generateds
      35           0 :         lua_getglobal(L, "core");
      36           0 :         lua_getfield(L, -1, "registered_on_generateds");
      37             :         // Call callbacks
      38           0 :         push_v3s16(L, minp);
      39           0 :         push_v3s16(L, maxp);
      40           0 :         lua_pushnumber(L, blockseed);
      41           0 :         script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_FIRST);
      42           0 : }
      43             : 
      44           0 : void ScriptApiEnv::environment_Step(float dtime)
      45             : {
      46           0 :         SCRIPTAPI_PRECHECKHEADER
      47             :         //infostream<<"scriptapi_environment_step"<<std::endl;
      48             : 
      49             :         // Get core.registered_globalsteps
      50           0 :         lua_getglobal(L, "core");
      51           0 :         lua_getfield(L, -1, "registered_globalsteps");
      52             :         // Call callbacks
      53           0 :         lua_pushnumber(L, dtime);
      54             :         try {
      55           0 :                 script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
      56           0 :         } catch (LuaError &e) {
      57           0 :                 getServer()->setAsyncFatalError(e.what());
      58             :         }
      59           0 : }
      60             : 
      61           0 : void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
      62             : {
      63           0 :         SCRIPTAPI_PRECHECKHEADER
      64             : 
      65           0 :         if (player == NULL)
      66           0 :                 return;
      67             : 
      68             :         // Get minetest.registered_playerevents
      69           0 :         lua_getglobal(L, "minetest");
      70           0 :         lua_getfield(L, -1, "registered_playerevents");
      71             : 
      72             :         // Call callbacks
      73           0 :         objectrefGetOrCreate(L, player);   // player
      74           0 :         lua_pushstring(L,type.c_str()); // event type
      75             :         try {
      76           0 :                 script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
      77           0 :         } catch (LuaError &e) {
      78           0 :                 getServer()->setAsyncFatalError(e.what());
      79             :         }
      80             : }
      81             : 
      82           0 : void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
      83             : {
      84           0 :         SCRIPTAPI_PRECHECKHEADER
      85           0 :         verbosestream<<"scriptapi_add_environment"<<std::endl;
      86           0 :         setEnv(env);
      87             : 
      88             :         /*
      89             :                 Add ActiveBlockModifiers to environment
      90             :         */
      91             : 
      92             :         // Get core.registered_abms
      93           0 :         lua_getglobal(L, "core");
      94           0 :         lua_getfield(L, -1, "registered_abms");
      95           0 :         luaL_checktype(L, -1, LUA_TTABLE);
      96           0 :         int registered_abms = lua_gettop(L);
      97             : 
      98           0 :         if(lua_istable(L, registered_abms)){
      99           0 :                 int table = lua_gettop(L);
     100           0 :                 lua_pushnil(L);
     101           0 :                 while(lua_next(L, table) != 0){
     102             :                         // key at index -2 and value at index -1
     103           0 :                         int id = lua_tonumber(L, -2);
     104           0 :                         int current_abm = lua_gettop(L);
     105             : 
     106           0 :                         std::set<std::string> trigger_contents;
     107           0 :                         lua_getfield(L, current_abm, "nodenames");
     108           0 :                         if(lua_istable(L, -1)){
     109           0 :                                 int table = lua_gettop(L);
     110           0 :                                 lua_pushnil(L);
     111           0 :                                 while(lua_next(L, table) != 0){
     112             :                                         // key at index -2 and value at index -1
     113           0 :                                         luaL_checktype(L, -1, LUA_TSTRING);
     114           0 :                                         trigger_contents.insert(lua_tostring(L, -1));
     115             :                                         // removes value, keeps key for next iteration
     116           0 :                                         lua_pop(L, 1);
     117             :                                 }
     118           0 :                         } else if(lua_isstring(L, -1)){
     119           0 :                                 trigger_contents.insert(lua_tostring(L, -1));
     120             :                         }
     121           0 :                         lua_pop(L, 1);
     122             : 
     123           0 :                         std::set<std::string> required_neighbors;
     124           0 :                         lua_getfield(L, current_abm, "neighbors");
     125           0 :                         if(lua_istable(L, -1)){
     126           0 :                                 int table = lua_gettop(L);
     127           0 :                                 lua_pushnil(L);
     128           0 :                                 while(lua_next(L, table) != 0){
     129             :                                         // key at index -2 and value at index -1
     130           0 :                                         luaL_checktype(L, -1, LUA_TSTRING);
     131           0 :                                         required_neighbors.insert(lua_tostring(L, -1));
     132             :                                         // removes value, keeps key for next iteration
     133           0 :                                         lua_pop(L, 1);
     134             :                                 }
     135           0 :                         } else if(lua_isstring(L, -1)){
     136           0 :                                 required_neighbors.insert(lua_tostring(L, -1));
     137             :                         }
     138           0 :                         lua_pop(L, 1);
     139             : 
     140           0 :                         float trigger_interval = 10.0;
     141           0 :                         getfloatfield(L, current_abm, "interval", trigger_interval);
     142             : 
     143           0 :                         int trigger_chance = 50;
     144           0 :                         getintfield(L, current_abm, "chance", trigger_chance);
     145             : 
     146             :                         LuaABM *abm = new LuaABM(L, id, trigger_contents,
     147           0 :                                         required_neighbors, trigger_interval, trigger_chance);
     148             : 
     149           0 :                         env->addActiveBlockModifier(abm);
     150             : 
     151             :                         // removes value, keeps key for next iteration
     152           0 :                         lua_pop(L, 1);
     153             :                 }
     154             :         }
     155           0 :         lua_pop(L, 1);
     156           3 : }

Generated by: LCOV version 1.11