LCOV - code coverage report
Current view: top level - src/script/cpp_api - s_entity.cpp (source / functions) Hit Total Coverage
Test: report Lines: 1 139 0.7 %
Date: 2015-07-11 18:23:49 Functions: 2 10 20.0 %

          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_entity.h"
      21             : #include "cpp_api/s_internal.h"
      22             : #include "log.h"
      23             : #include "object_properties.h"
      24             : #include "common/c_converter.h"
      25             : #include "common/c_content.h"
      26             : 
      27           0 : bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
      28             : {
      29           0 :         SCRIPTAPI_PRECHECKHEADER
      30             : 
      31           0 :         verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
      32           0 :                         <<name<<"\""<<std::endl;
      33             : 
      34             :         // Get core.registered_entities[name]
      35           0 :         lua_getglobal(L, "core");
      36           0 :         lua_getfield(L, -1, "registered_entities");
      37           0 :         luaL_checktype(L, -1, LUA_TTABLE);
      38           0 :         lua_pushstring(L, name);
      39           0 :         lua_gettable(L, -2);
      40             :         // Should be a table, which we will use as a prototype
      41             :         //luaL_checktype(L, -1, LUA_TTABLE);
      42           0 :         if (lua_type(L, -1) != LUA_TTABLE){
      43           0 :                 errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
      44           0 :                 return false;
      45             :         }
      46           0 :         int prototype_table = lua_gettop(L);
      47             :         //dump2(L, "prototype_table");
      48             : 
      49             :         // Create entity object
      50           0 :         lua_newtable(L);
      51           0 :         int object = lua_gettop(L);
      52             : 
      53             :         // Set object metatable
      54           0 :         lua_pushvalue(L, prototype_table);
      55           0 :         lua_setmetatable(L, -2);
      56             : 
      57             :         // Add object reference
      58             :         // This should be userdata with metatable ObjectRef
      59           0 :         objectrefGet(L, id);
      60           0 :         luaL_checktype(L, -1, LUA_TUSERDATA);
      61           0 :         if (!luaL_checkudata(L, -1, "ObjectRef"))
      62           0 :                 luaL_typerror(L, -1, "ObjectRef");
      63           0 :         lua_setfield(L, -2, "object");
      64             : 
      65             :         // core.luaentities[id] = object
      66           0 :         lua_getglobal(L, "core");
      67           0 :         lua_getfield(L, -1, "luaentities");
      68           0 :         luaL_checktype(L, -1, LUA_TTABLE);
      69           0 :         lua_pushnumber(L, id); // Push id
      70           0 :         lua_pushvalue(L, object); // Copy object to top of stack
      71           0 :         lua_settable(L, -3);
      72             : 
      73           0 :         return true;
      74             : }
      75             : 
      76           0 : void ScriptApiEntity::luaentity_Activate(u16 id,
      77             :                 const std::string &staticdata, u32 dtime_s)
      78             : {
      79           0 :         SCRIPTAPI_PRECHECKHEADER
      80             : 
      81           0 :         verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
      82             : 
      83             :         // Get core.luaentities[id]
      84           0 :         luaentity_get(L, id);
      85           0 :         int object = lua_gettop(L);
      86             : 
      87             :         // Get on_activate function
      88           0 :         lua_getfield(L, -1, "on_activate");
      89           0 :         if (!lua_isnil(L, -1)) {
      90           0 :                 luaL_checktype(L, -1, LUA_TFUNCTION);
      91           0 :                 lua_pushvalue(L, object); // self
      92           0 :                 lua_pushlstring(L, staticdata.c_str(), staticdata.size());
      93           0 :                 lua_pushinteger(L, dtime_s);
      94             :                 // Call with 3 arguments, 0 results
      95           0 :                 if (lua_pcall(L, 3, 0, m_errorhandler))
      96           0 :                         scriptError();
      97             :         } else {
      98           0 :                 lua_pop(L, 1);
      99             :         }
     100           0 :         lua_pop(L, 1); // Pop object
     101           0 : }
     102             : 
     103           0 : void ScriptApiEntity::luaentity_Remove(u16 id)
     104             : {
     105           0 :         SCRIPTAPI_PRECHECKHEADER
     106             : 
     107           0 :         verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
     108             : 
     109             :         // Get core.luaentities table
     110           0 :         lua_getglobal(L, "core");
     111           0 :         lua_getfield(L, -1, "luaentities");
     112           0 :         luaL_checktype(L, -1, LUA_TTABLE);
     113           0 :         int objectstable = lua_gettop(L);
     114             : 
     115             :         // Set luaentities[id] = nil
     116           0 :         lua_pushnumber(L, id); // Push id
     117           0 :         lua_pushnil(L);
     118           0 :         lua_settable(L, objectstable);
     119             : 
     120           0 :         lua_pop(L, 2); // pop luaentities, core
     121           0 : }
     122             : 
     123           0 : std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
     124             : {
     125           0 :         SCRIPTAPI_PRECHECKHEADER
     126             : 
     127             :         //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
     128             : 
     129             :         // Get core.luaentities[id]
     130           0 :         luaentity_get(L, id);
     131           0 :         int object = lua_gettop(L);
     132             : 
     133             :         // Get get_staticdata function
     134           0 :         lua_getfield(L, -1, "get_staticdata");
     135           0 :         if (lua_isnil(L, -1)) {
     136           0 :                 lua_pop(L, 2); // Pop entity and  get_staticdata
     137           0 :                 return "";
     138             :         }
     139             : 
     140           0 :         luaL_checktype(L, -1, LUA_TFUNCTION);
     141           0 :         lua_pushvalue(L, object); // self
     142             :         // Call with 1 arguments, 1 results
     143           0 :         if (lua_pcall(L, 1, 1, m_errorhandler))
     144           0 :                 scriptError();
     145           0 :         lua_remove(L, object); // Remove object
     146             : 
     147           0 :         size_t len = 0;
     148           0 :         const char *s = lua_tolstring(L, -1, &len);
     149           0 :         lua_pop(L, 1); // Pop static data
     150           0 :         return std::string(s, len);
     151             : }
     152             : 
     153           0 : void ScriptApiEntity::luaentity_GetProperties(u16 id,
     154             :                 ObjectProperties *prop)
     155             : {
     156           0 :         SCRIPTAPI_PRECHECKHEADER
     157             : 
     158             :         //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
     159             : 
     160             :         // Get core.luaentities[id]
     161           0 :         luaentity_get(L, id);
     162             : 
     163             :         // Set default values that differ from ObjectProperties defaults
     164           0 :         prop->hp_max = 10;
     165             : 
     166             :         /* Read stuff */
     167             : 
     168           0 :         prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
     169             : 
     170           0 :         getboolfield(L, -1, "physical", prop->physical);
     171           0 :         getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
     172             : 
     173           0 :         getfloatfield(L, -1, "weight", prop->weight);
     174             : 
     175           0 :         lua_getfield(L, -1, "collisionbox");
     176           0 :         if (lua_istable(L, -1))
     177           0 :                 prop->collisionbox = read_aabb3f(L, -1, 1.0);
     178           0 :         lua_pop(L, 1);
     179             : 
     180           0 :         getstringfield(L, -1, "visual", prop->visual);
     181             : 
     182           0 :         getstringfield(L, -1, "mesh", prop->mesh);
     183             : 
     184             :         // Deprecated: read object properties directly
     185           0 :         read_object_properties(L, -1, prop);
     186             : 
     187             :         // Read initial_properties
     188           0 :         lua_getfield(L, -1, "initial_properties");
     189           0 :         read_object_properties(L, -1, prop);
     190           0 :         lua_pop(L, 1);
     191           0 : }
     192             : 
     193           0 : void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
     194             : {
     195           0 :         SCRIPTAPI_PRECHECKHEADER
     196             : 
     197             :         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
     198             : 
     199             :         // Get core.luaentities[id]
     200           0 :         luaentity_get(L, id);
     201           0 :         int object = lua_gettop(L);
     202             :         // State: object is at top of stack
     203             :         // Get step function
     204           0 :         lua_getfield(L, -1, "on_step");
     205           0 :         if (lua_isnil(L, -1)) {
     206           0 :                 lua_pop(L, 2); // Pop on_step and entity
     207           0 :                 return;
     208             :         }
     209           0 :         luaL_checktype(L, -1, LUA_TFUNCTION);
     210           0 :         lua_pushvalue(L, object); // self
     211           0 :         lua_pushnumber(L, dtime); // dtime
     212             :         // Call with 2 arguments, 0 results
     213           0 :         if (lua_pcall(L, 2, 0, m_errorhandler))
     214           0 :                 scriptError();
     215           0 :         lua_pop(L, 1); // Pop object
     216             : }
     217             : 
     218             : // Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
     219             : //                       tool_capabilities, direction)
     220           0 : void ScriptApiEntity::luaentity_Punch(u16 id,
     221             :                 ServerActiveObject *puncher, float time_from_last_punch,
     222             :                 const ToolCapabilities *toolcap, v3f dir)
     223             : {
     224           0 :         SCRIPTAPI_PRECHECKHEADER
     225             : 
     226             :         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
     227             : 
     228             :         // Get core.luaentities[id]
     229           0 :         luaentity_get(L,id);
     230           0 :         int object = lua_gettop(L);
     231             :         // State: object is at top of stack
     232             :         // Get function
     233           0 :         lua_getfield(L, -1, "on_punch");
     234           0 :         if (lua_isnil(L, -1)) {
     235           0 :                 lua_pop(L, 2); // Pop on_punch and entitu
     236           0 :                 return;
     237             :         }
     238           0 :         luaL_checktype(L, -1, LUA_TFUNCTION);
     239           0 :         lua_pushvalue(L, object);  // self
     240           0 :         objectrefGetOrCreate(L, puncher);  // Clicker reference
     241           0 :         lua_pushnumber(L, time_from_last_punch);
     242           0 :         push_tool_capabilities(L, *toolcap);
     243           0 :         push_v3f(L, dir);
     244             :         // Call with 5 arguments, 0 results
     245           0 :         if (lua_pcall(L, 5, 0, m_errorhandler))
     246           0 :                 scriptError();
     247           0 :         lua_pop(L, 1); // Pop object
     248             : }
     249             : 
     250             : // Calls entity:on_rightclick(ObjectRef clicker)
     251           0 : void ScriptApiEntity::luaentity_Rightclick(u16 id,
     252             :                 ServerActiveObject *clicker)
     253             : {
     254           0 :         SCRIPTAPI_PRECHECKHEADER
     255             : 
     256             :         //infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
     257             : 
     258             :         // Get core.luaentities[id]
     259           0 :         luaentity_get(L, id);
     260           0 :         int object = lua_gettop(L);
     261             :         // State: object is at top of stack
     262             :         // Get function
     263           0 :         lua_getfield(L, -1, "on_rightclick");
     264           0 :         if (lua_isnil(L, -1)) {
     265           0 :                 lua_pop(L, 2); // Pop on_rightclick and entity
     266           0 :                 return;
     267             :         }
     268           0 :         luaL_checktype(L, -1, LUA_TFUNCTION);
     269           0 :         lua_pushvalue(L, object); // self
     270           0 :         objectrefGetOrCreate(L, clicker); // Clicker reference
     271             :         // Call with 2 arguments, 0 results
     272           0 :         if (lua_pcall(L, 2, 0, m_errorhandler))
     273           0 :                 scriptError();
     274           0 :         lua_pop(L, 1); // Pop object
     275           3 : }
     276             : 

Generated by: LCOV version 1.11