2013-02-23 19:06:57 +01:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "cpp_api/s_entity.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "cpp_api/s_internal.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "object_properties.h"
|
|
|
|
#include "common/c_converter.h"
|
|
|
|
#include "common/c_content.h"
|
2017-03-10 18:25:58 +01:00
|
|
|
#include "server.h"
|
2013-02-23 19:06:57 +01:00
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
2013-02-23 19:06:57 +01:00
|
|
|
|
|
|
|
verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
|
|
|
|
<<name<<"\""<<std::endl;
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.registered_entities[name]
|
|
|
|
lua_getglobal(L, "core");
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_getfield(L, -1, "registered_entities");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
lua_pushstring(L, name);
|
|
|
|
lua_gettable(L, -2);
|
|
|
|
// Should be a table, which we will use as a prototype
|
|
|
|
//luaL_checktype(L, -1, LUA_TTABLE);
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_type(L, -1) != LUA_TTABLE){
|
2013-02-23 19:06:57 +01:00
|
|
|
errorstream<<"LuaEntity name \""<<name<<"\" not defined"<<std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int prototype_table = lua_gettop(L);
|
|
|
|
//dump2(L, "prototype_table");
|
|
|
|
|
|
|
|
// Create entity object
|
|
|
|
lua_newtable(L);
|
|
|
|
int object = lua_gettop(L);
|
|
|
|
|
|
|
|
// Set object metatable
|
|
|
|
lua_pushvalue(L, prototype_table);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
// Add object reference
|
|
|
|
// This should be userdata with metatable ObjectRef
|
2014-10-02 21:58:13 +02:00
|
|
|
objectrefGet(L, id);
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_checktype(L, -1, LUA_TUSERDATA);
|
2014-04-15 19:30:46 +02:00
|
|
|
if (!luaL_checkudata(L, -1, "ObjectRef"))
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_typerror(L, -1, "ObjectRef");
|
|
|
|
lua_setfield(L, -2, "object");
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// core.luaentities[id] = object
|
|
|
|
lua_getglobal(L, "core");
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_getfield(L, -1, "luaentities");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
lua_pushnumber(L, id); // Push id
|
|
|
|
lua_pushvalue(L, object); // Copy object to top of stack
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiEntity::luaentity_Activate(u16 id,
|
2013-02-23 19:06:57 +01:00
|
|
|
const std::string &staticdata, u32 dtime_s)
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2014-04-15 19:30:46 +02:00
|
|
|
verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;
|
2013-02-23 19:06:57 +01:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities[id]
|
2013-11-05 18:06:15 +01:00
|
|
|
luaentity_get(L, id);
|
2013-02-23 19:06:57 +01:00
|
|
|
int object = lua_gettop(L);
|
|
|
|
|
|
|
|
// Get on_activate function
|
|
|
|
lua_getfield(L, -1, "on_activate");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (!lua_isnil(L, -1)) {
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
|
|
|
lua_pushvalue(L, object); // self
|
|
|
|
lua_pushlstring(L, staticdata.c_str(), staticdata.size());
|
|
|
|
lua_pushinteger(L, dtime_s);
|
2015-08-12 04:27:54 +02:00
|
|
|
|
|
|
|
setOriginFromTable(object);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 3, 0, error_handler));
|
2013-11-05 18:06:15 +01:00
|
|
|
} else {
|
|
|
|
lua_pop(L, 1);
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_pop(L, 2); // Pop object and error handler
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiEntity::luaentity_Remove(u16 id)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2014-04-15 19:30:46 +02:00
|
|
|
verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;
|
2013-02-23 19:06:57 +01:00
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities table
|
|
|
|
lua_getglobal(L, "core");
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_getfield(L, -1, "luaentities");
|
|
|
|
luaL_checktype(L, -1, LUA_TTABLE);
|
|
|
|
int objectstable = lua_gettop(L);
|
|
|
|
|
|
|
|
// Set luaentities[id] = nil
|
|
|
|
lua_pushnumber(L, id); // Push id
|
|
|
|
lua_pushnil(L);
|
|
|
|
lua_settable(L, objectstable);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
lua_pop(L, 2); // pop luaentities, core
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
//infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities[id]
|
2013-11-05 18:06:15 +01:00
|
|
|
luaentity_get(L, id);
|
2013-02-23 19:06:57 +01:00
|
|
|
int object = lua_gettop(L);
|
|
|
|
|
|
|
|
// Get get_staticdata function
|
|
|
|
lua_getfield(L, -1, "get_staticdata");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
lua_pop(L, 2); // Pop entity and get_staticdata
|
2013-02-23 19:06:57 +01:00
|
|
|
return "";
|
2014-04-15 19:30:46 +02:00
|
|
|
}
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
|
|
|
lua_pushvalue(L, object); // self
|
2015-08-12 04:27:54 +02:00
|
|
|
|
|
|
|
setOriginFromTable(object);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 1, 1, error_handler));
|
2015-08-12 04:27:54 +02:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_remove(L, object);
|
|
|
|
lua_remove(L, error_handler);
|
2013-02-23 19:06:57 +01:00
|
|
|
|
2013-11-05 18:06:15 +01:00
|
|
|
size_t len = 0;
|
2013-02-23 19:06:57 +01:00
|
|
|
const char *s = lua_tolstring(L, -1, &len);
|
2014-04-15 19:30:46 +02:00
|
|
|
lua_pop(L, 1); // Pop static data
|
2013-02-23 19:06:57 +01:00
|
|
|
return std::string(s, len);
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiEntity::luaentity_GetProperties(u16 id,
|
2013-02-23 19:06:57 +01:00
|
|
|
ObjectProperties *prop)
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
//infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities[id]
|
2014-04-15 19:30:46 +02:00
|
|
|
luaentity_get(L, id);
|
2013-02-23 19:06:57 +01:00
|
|
|
|
|
|
|
// Set default values that differ from ObjectProperties defaults
|
|
|
|
prop->hp_max = 10;
|
|
|
|
|
|
|
|
/* Read stuff */
|
|
|
|
|
|
|
|
prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
|
|
|
|
|
|
|
|
getboolfield(L, -1, "physical", prop->physical);
|
2013-06-14 14:04:46 +02:00
|
|
|
getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);
|
2013-02-23 19:06:57 +01:00
|
|
|
|
|
|
|
getfloatfield(L, -1, "weight", prop->weight);
|
|
|
|
|
|
|
|
lua_getfield(L, -1, "collisionbox");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_istable(L, -1))
|
2013-02-23 19:06:57 +01:00
|
|
|
prop->collisionbox = read_aabb3f(L, -1, 1.0);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
|
|
|
|
getstringfield(L, -1, "visual", prop->visual);
|
|
|
|
|
|
|
|
getstringfield(L, -1, "mesh", prop->mesh);
|
|
|
|
|
|
|
|
// Deprecated: read object properties directly
|
2017-03-10 18:25:58 +01:00
|
|
|
read_object_properties(L, -1, prop, getServer()->idef());
|
2013-02-23 19:06:57 +01:00
|
|
|
|
|
|
|
// Read initial_properties
|
|
|
|
lua_getfield(L, -1, "initial_properties");
|
2017-03-10 18:25:58 +01:00
|
|
|
read_object_properties(L, -1, prop, getServer()->idef());
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities[id]
|
2013-11-05 18:06:15 +01:00
|
|
|
luaentity_get(L, id);
|
2013-02-23 19:06:57 +01:00
|
|
|
int object = lua_gettop(L);
|
|
|
|
// State: object is at top of stack
|
|
|
|
// Get step function
|
|
|
|
lua_getfield(L, -1, "on_step");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
lua_pop(L, 2); // Pop on_step and entity
|
2013-02-23 19:06:57 +01:00
|
|
|
return;
|
2014-04-15 19:30:46 +02:00
|
|
|
}
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
|
|
|
lua_pushvalue(L, object); // self
|
|
|
|
lua_pushnumber(L, dtime); // dtime
|
2015-08-12 04:27:54 +02:00
|
|
|
|
|
|
|
setOriginFromTable(object);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 2, 0, error_handler));
|
2015-08-12 04:27:54 +02:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_pop(L, 2); // Pop object and error handler
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
|
2017-01-21 15:58:07 +01:00
|
|
|
// tool_capabilities, direction, damage)
|
|
|
|
bool ScriptApiEntity::luaentity_Punch(u16 id,
|
2013-02-23 19:06:57 +01:00
|
|
|
ServerActiveObject *puncher, float time_from_last_punch,
|
2017-01-21 15:58:07 +01:00
|
|
|
const ToolCapabilities *toolcap, v3f dir, s16 damage)
|
2013-02-23 19:06:57 +01:00
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities[id]
|
2013-05-25 00:51:02 +02:00
|
|
|
luaentity_get(L,id);
|
2013-02-23 19:06:57 +01:00
|
|
|
int object = lua_gettop(L);
|
|
|
|
// State: object is at top of stack
|
|
|
|
// Get function
|
|
|
|
lua_getfield(L, -1, "on_punch");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_isnil(L, -1)) {
|
2017-01-21 15:58:07 +01:00
|
|
|
lua_pop(L, 2); // Pop on_punch and entity
|
|
|
|
return false;
|
2014-04-15 19:30:46 +02:00
|
|
|
}
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
2014-10-02 21:58:13 +02:00
|
|
|
lua_pushvalue(L, object); // self
|
|
|
|
objectrefGetOrCreate(L, puncher); // Clicker reference
|
2013-02-23 19:06:57 +01:00
|
|
|
lua_pushnumber(L, time_from_last_punch);
|
|
|
|
push_tool_capabilities(L, *toolcap);
|
|
|
|
push_v3f(L, dir);
|
2017-01-21 15:58:07 +01:00
|
|
|
lua_pushnumber(L, damage);
|
2015-08-12 04:27:54 +02:00
|
|
|
|
|
|
|
setOriginFromTable(object);
|
2017-01-31 05:37:04 +01:00
|
|
|
PCALL_RES(lua_pcall(L, 6, 1, error_handler));
|
2015-08-12 04:27:54 +02:00
|
|
|
|
2017-01-21 15:58:07 +01:00
|
|
|
bool retval = lua_toboolean(L, -1);
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_pop(L, 2); // Pop object and error handler
|
2017-01-21 15:58:07 +01:00
|
|
|
return retval;
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calls entity:on_rightclick(ObjectRef clicker)
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiEntity::luaentity_Rightclick(u16 id,
|
2013-02-23 19:06:57 +01:00
|
|
|
ServerActiveObject *clicker)
|
|
|
|
{
|
2013-05-25 00:51:02 +02:00
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
2013-02-23 19:06:57 +01:00
|
|
|
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
|
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
int error_handler = PUSH_ERROR_HANDLER(L);
|
|
|
|
|
2014-04-28 03:02:48 +02:00
|
|
|
// Get core.luaentities[id]
|
2014-04-15 19:30:46 +02:00
|
|
|
luaentity_get(L, id);
|
2013-02-23 19:06:57 +01:00
|
|
|
int object = lua_gettop(L);
|
|
|
|
// State: object is at top of stack
|
|
|
|
// Get function
|
|
|
|
lua_getfield(L, -1, "on_rightclick");
|
2014-04-15 19:30:46 +02:00
|
|
|
if (lua_isnil(L, -1)) {
|
|
|
|
lua_pop(L, 2); // Pop on_rightclick and entity
|
2013-02-23 19:06:57 +01:00
|
|
|
return;
|
2014-04-15 19:30:46 +02:00
|
|
|
}
|
2013-02-23 19:06:57 +01:00
|
|
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
|
|
|
lua_pushvalue(L, object); // self
|
2014-10-02 21:58:13 +02:00
|
|
|
objectrefGetOrCreate(L, clicker); // Clicker reference
|
2015-08-12 04:27:54 +02:00
|
|
|
|
|
|
|
setOriginFromTable(object);
|
2015-08-25 07:44:53 +02:00
|
|
|
PCALL_RES(lua_pcall(L, 2, 0, error_handler));
|
2015-08-12 04:27:54 +02:00
|
|
|
|
2015-08-25 07:44:53 +02:00
|
|
|
lua_pop(L, 2); // Pop object and error handler
|
2013-02-23 19:06:57 +01:00
|
|
|
}
|
2013-05-25 00:51:02 +02:00
|
|
|
|