be48cbbda29a71cX-Content-Type-Options: nosniff
Content-Security-Policy: default-src 'none'
Content-Type: text/plain; charset=UTF-8
Content-Length: 8714
Content-Disposition: inline; filename="s_entity.cpp"
Last-Modified: Thu, 03 Oct 2024 07:03:18 GMT
Expires: Sun, 01 Oct 2034 07:03:18 GMT
ETag: "df4b77936836fba837154c63e5a1d472c680a02c"

/*
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.
*/

#include "cpp_api/s_entity.h"
#include "cpp_api/s_internal.h"
#include "log.h"
#include "object_properties.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "server.h"

bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
{
	SCRIPTAPI_PRECHECKHEADER

	verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
			<<name<<"\""<<std::endl;

	// Get core.registered_entities[name]
	lua_getglobal(L, "core");
	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);
	if (lua_type(L, -1) != LUA_TTABLE){
		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
	push_objectRef(L, id);
	luaL_checktype(L, -1, LUA_TUSERDATA);
	if (!luaL_checkudata(L, -1, "ObjectRef"))
		luaL_typerror(L, -1, "ObjectRef");
	lua_setfield(L, -2, "object");

	// core.luaentities[id] = object
	lua_getglobal(L, "core");
	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;
}

void ScriptApiEntity::luaentity_Activate(u16 id,
		const std::string &staticdata, u32 dtime_s)
{
	SCRIPTAPI_PRECHECKHEADER

	verbosestream << "scriptapi_luaentity_activate: id=" << id << std::endl;

	int error_handler = PUSH_ERROR_HANDLER(L);

	// Get core.luaentities[id]
	luaentity_get(L, id);
	int object = lua_gettop(L);

	// Get on_activate function
	lua_getfield(L, -1, "on_activate");
	if (!lua_isnil(L, -1)) {
		luaL_checktype(L, -1, LUA_TFUNCTION);
		lua_pushvalue(L, object); // self
		lua_pushlstring(L, staticdata.c_str(), staticdata.size());
		lua_pushinteger(L, dtime_s);

		setOriginFromTable(object);
		PCALL_RES(lua_pcall(L, 3, 0, error_handler));
	} else {
		lua_pop(L, 1);
	}
	lua_pop(L, 2); // Pop object and error handler
}

void ScriptApiEntity::luaentity_Remove(u16 id)
{
	SCRIPTAPI_PRECHECKHEADER

	verbosestream << "scriptapi_luaentity_rm: id=" << id << std::endl;

	// Get core.luaentities table
	lua_getglobal(L, "core");
	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);

	lua_pop(L, 2); // pop luaentities, core
}

std::string ScriptApiEntity::luaentity_GetStaticdata(u16 id)
{
	SCRIPTAPI_PRECHECKHEADER

	//infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;

	int error_handler = PUSH_ERROR_HANDLER(L);

	// Get core.luaentities[id]
	luaentity_get(L, id);
	int object = lua_gettop(L);

	// Get get_staticdata function
	lua_getfield(L, -1, "get_staticdata");
	if (lua_isnil(L, -1)) {
		lua_pop(L, 2); // Pop entity and  get_staticdata
		return "";
	}
	luaL_checktype(L, -1, LUA_TFUNCTION);
	lua_pushvalue(L, object); // self

	setOriginFromTable(object);
	PCALL_RES(lua_pcall(L, 1, 1, error_handler));

	lua_remove(L, object);
	lua_remove(L, error_handler);

	size_t len = 0;
	const char *s = lua_tolstring(L, -1, &len);
	lua_pop(L, 1); // Pop static data
	return std::string(s, len);
}

void ScriptApiEntity::luaentity_GetProperties(u16 id,
		ObjectProperties *prop)
{
	SCRIPTAPI_PRECHECKHEADER

	//infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;

	// Get core.luaentities[id]
	luaentity_get(L, id);

	// 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);
	getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);

	getfloatfield(L, -1, "weight", prop->weight);

	lua_getfield(L, -1, "collisionbox");
	if (lua_istable(L, -1))
		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
	read_object_properties(L, -1, prop, getServer()->idef());

	// Read initial_properties
	lua_getfield(L, -1, "initial_properties");
	read_object_properties(L, -1, prop, getServer()->idef());
	lua_pop(L, 1);
}

void ScriptApiEntity::luaentity_Step(u16 id, float dtime)
{
	SCRIPTAPI_PRECHECKHEADER

	//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;

	int error_handler = PUSH_ERROR_HANDLER(L);

	// Get core.luaentities[id]
	luaentity_get(L, id);
	int object = lua_gettop(L);
	// State: object is at top of stack
	// Get step function
	lua_getfield(L, -1, "on_step");
	if (lua_isnil(L, -1)) {
		lua_pop(L, 2); // Pop on_step and entity
		return;
	}
	luaL_checktype(L, -1, LUA_TFUNCTION);
	lua_pushvalue(L, object); // self
	lua_pushnumber(L, dtime); // dtime

	setOriginFromTable(object);
	PCALL_RES(lua_pcall(L, 2, 0, error_handler));

	lua_pop(L, 2); // Pop object and error handler
}

// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
//                       tool_capabilities, direction, damage)
bool ScriptApiEntity::luaentity_Punch(u16 id,
		ServerActiveObject *puncher, float time_from_last_punch,
		const ToolCapabilities *toolcap, v3f dir, s16 damage)
{
	SCRIPTAPI_PRECHECKHEADER

	//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;

	int error_handler = PUSH_ERROR_HANDLER(L);

	// Get core.luaentities[id]
	luaentity_get(L,id);
	int object = lua_gettop(L);
	// State: object is at top of stack
	// Get function
	lua_getfield(L, -1, "on_punch");
	if (lua_isnil(L, -1)) {
		lua_pop(L, 2); // Pop on_punch and entity
		return false;
	}
	luaL_checktype(L, -1, LUA_TFUNCTION);
	lua_pushvalue(L, object);  // self
	objectrefGetOrCreate(L, puncher);  // Clicker reference
	lua_pushnumber(L, time_from_last_punch);
	push_tool_capabilities(L, *toolcap);
	push_v3f(L, dir);
	lua_pushnumber(L, damage);

	setOriginFromTable(object);
	PCALL_RES(lua_pcall(L, 6, 1, error_handler));

	bool retval = lua_toboolean(L, -1);
	lua_pop(L, 2); // Pop object and error handler
	return retval;
}

bool ScriptAp

<span class="hl slc"># This assumes that we won&apos;t have dirnames with space, which is</span>
<span class="hl slc"># the case for language codes, which are the only subdirs we expect to</span>
<span class="hl slc"># find in po/ anyway. If you put anything else there, you need to suffer</span>
<span class="hl slc"># the consequences of your actions, so we don&apos;t do sanity checks</span>
langs<span class="hl opt">=</span><span class="hl str">&quot;&quot;</span>

<span class="hl kwa">for</span> lang <span class="hl kwa">in</span> <span class="hl opt">* ;</span> <span class="hl kwa">do</span>
	<span class="hl kwa">if</span> <span class="hl kwb">test</span> <span class="hl opt">!</span> <span class="hl kwb">-d</span> <span class="hl kwd">$lang</span><span class="hl opt">;</span> <span class="hl kwa">then</span>
		<span class="hl kwb">continue</span>
	<span class="hl kwa">fi</span>
	langs<span class="hl opt">=</span><span class="hl str">&quot;</span><span class="hl ipl">$langs</span> <span class="hl str"></span><span class="hl ipl">$lang</span><span class="hl str">&quot;</span>
<span class="hl kwa">done</span>

<span class="hl slc"># go back</span>
<span class="hl kwb">cd</span> ..

<span class="hl slc"># First thing first, update the .pot template. We place it in the po/</span>
<span class="hl slc"># directory at the top level. You a recent enough xgettext that supports</span>
<span class="hl slc"># --package-name</span>
potfile<span class="hl opt">=</span>po<span class="hl opt">/</span>minetest.pot
xgettext <span class="hl kwb">--package-name</span><span class="hl opt">=</span>minetest \
		<span class="hl kwb">-kN_ -kwgettext -kfgettext</span> \
		<span class="hl kwb">-F -n -o</span> <span class="hl kwd">$potfile</span> \
		<span class="hl str">`find src/ -name &apos;*.cpp&apos; -o -name &apos;*.h&apos;`</span> \
		<span class="hl str">`find builtin/ -name &apos;*.lua&apos;`</span>

<span class="hl slc"># Now iterate on all languages and create the po file if missing, or update it</span>
<span class="hl slc"># if it exists already</span>
<span class="hl kwa">for</span> lang <span class="hl kwa">in</span> <span class="hl kwd">$langs</span> <span class="hl opt">;</span> <span class="hl kwa">do</span> <span class="hl slc"># note the missing quotes around $langs</span>
	pofile<span class="hl opt">=</span>po<span class="hl opt">/</span><span class="hl kwd">$lang</span><span class="hl opt">/</span>minetest.po
	<span class="hl kwa">if</span> <span class="hl kwb">test -e</span> <span class="hl kwd">$pofile</span><span class="hl opt">;</span> <span class="hl kwa">then</span>
		<span class="hl kwb">echo</span> <span class="hl str">&quot;[</span><span class="hl ipl">$lang</span><span class="hl str">]: updating strings&quot;</span>
		msgmerge <span class="hl kwb">-F -U</span> <span class="hl kwd">$pofile $potfile</span>
	<span class="hl kwa">else</span>
		<span class="hl slc"># This will ask for the translator identity</span>
		<span class="hl kwb">echo</span> <span class="hl str">&quot;[</span><span class="hl ipl">$lang</span><span class="hl str">]: NEW strings&quot;</span>
		msginit <span class="hl kwb">-l</span> <span class="hl kwd">$lang</span> <span class="hl kwb">-o</span> <span class="hl kwd">$pofile</span> <span class="hl kwb">-i</span> <span class="hl kwd">$potfile</span>
	<span class="hl kwa">fi</span>
<span class="hl kwa">done</span>
</code></pre></td></tr></table>
</div> <!-- class=content -->
<div class='footer'>generated by <a href='https://git.zx2c4.com/cgit/about/'>cgit v1.2.3</a> (<a href='https://git-scm.com/'>git 2.25.1</a>) at 2024-10-03 07:03:18 +0000</div>
</div> <!-- id=cgit -->
</body>
</html>
