#include "content_nodemeta.h"
#include "utility.h"
#include "tool.h"
+#include "daynightratio.h"
static void stackDump(lua_State *L, std::ostream &o)
{
}
}
+/*
+ SimpleSoundSpec
+*/
+
+static void read_soundspec(lua_State *L, int index, SimpleSoundSpec &spec)
+{
+ if(index < 0)
+ index = lua_gettop(L) + 1 + index;
+ if(lua_isnil(L, index)){
+ } else if(lua_istable(L, index)){
+ getstringfield(L, index, "name", spec.name);
+ getfloatfield(L, index, "gain", spec.gain);
+ } else if(lua_isstring(L, index)){
+ spec.name = lua_tostring(L, index);
+ }
+}
+
/*
ItemDefinition
*/
getboolfield(L, index, "legacy_facedir_simple", f.legacy_facedir_simple);
// Set to true if wall_mounted used to be set to true
getboolfield(L, index, "legacy_wallmounted", f.legacy_wallmounted);
+
+ // Sound table
+ lua_getfield(L, index, "sounds");
+ if(lua_istable(L, -1)){
+ lua_getfield(L, -1, "footstep");
+ read_soundspec(L, -1, f.sound_footstep);
+ lua_pop(L, 1);
+ lua_getfield(L, -1, "dig");
+ read_soundspec(L, -1, f.sound_dig);
+ lua_pop(L, 1);
+ lua_getfield(L, -1, "dug");
+ read_soundspec(L, -1, f.sound_dug);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
return f;
}
static const char className[];
static const luaL_reg methods[];
-
+public:
static ObjectRef *checkobject(lua_State *L, int narg)
{
luaL_checktype(L, narg, LUA_TUSERDATA);
ServerActiveObject *co = ref->m_object;
return co;
}
-
+private:
static LuaEntitySAO* getluaobject(ObjectRef *ref)
{
ServerActiveObject *obj = getobject(ref);
ObjectRef *ref = checkobject(L, 1);
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
- infostream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
+ verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
co->m_removed = true;
return 0;
}
return 0;
}
- // punch(self, puncher); puncher = an another ObjectRef
+ // punch(self, puncher, tool_capabilities, direction, time_from_last_punch)
static int l_punch(lua_State *L)
{
ObjectRef *ref = checkobject(L, 1);
- ObjectRef *ref2 = checkobject(L, 2);
+ ObjectRef *puncher_ref = checkobject(L, 2);
ServerActiveObject *co = getobject(ref);
- ServerActiveObject *co2 = getobject(ref2);
+ ServerActiveObject *puncher = getobject(puncher_ref);
if(co == NULL) return 0;
- if(co2 == NULL) return 0;
+ if(puncher == NULL) return 0;
+ ToolCapabilities toolcap = read_tool_capabilities(L, 3);
+ v3f dir = read_v3f(L, 4);
+ float time_from_last_punch = 1000000;
+ if(lua_isnumber(L, 5))
+ time_from_last_punch = lua_tonumber(L, 5);
// Do it
- co->punch(co2);
+ puncher->punch(dir, &toolcap, puncher, time_from_last_punch);
return 0;
}
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
int hp = lua_tonumber(L, 2);
- infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
- <<" hp="<<hp<<std::endl;
+ /*infostream<<"ObjectRef::l_set_hp(): id="<<co->getId()
+ <<" hp="<<hp<<std::endl;*/
// Do it
co->setHP(hp);
// Return
ServerActiveObject *co = getobject(ref);
if(co == NULL) return 0;
int hp = co->getHP();
- infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
- <<" hp="<<hp<<std::endl;
+ /*infostream<<"ObjectRef::l_get_hp(): id="<<co->getId()
+ <<" hp="<<hp<<std::endl;*/
// Return
lua_pushnumber(L, hp);
return 1;
return 0;
}
+ // set_armor_groups(self, groups)
+ static int l_set_armor_groups(lua_State *L)
+ {
+ ObjectRef *ref = checkobject(L, 1);
+ LuaEntitySAO *co = getluaobject(ref);
+ if(co == NULL) return 0;
+ // Do it
+ ItemGroupList groups;
+ read_groups(L, 2, groups);
+ co->setArmorGroups(groups);
+ return 0;
+ }
+
// DEPRECATED
// get_entity_name(self)
static int l_get_entity_name(lua_State *L)
method(ObjectRef, getyaw),
method(ObjectRef, settexturemod),
method(ObjectRef, setsprite),
+ method(ObjectRef, set_armor_groups),
method(ObjectRef, get_entity_name),
method(ObjectRef, get_luaentity),
// Player-only
if(item.empty() || !item.isKnown(get_server(L)->idef()))
return 0;
// Do it
- ServerActiveObject *obj = new ItemSAO(env, pos, item.getItemString());
+ ServerActiveObject *obj = createItemSAO(env, pos, item.getItemString());
int objectid = env->addActiveObject(obj);
// If failed to add, return nothing (reads as nil)
if(objectid == 0)
// pos = {x=num, y=num, z=num}
static int l_add_rat(lua_State *L)
{
- infostream<<"EnvRef::l_add_rat()"<<std::endl;
- EnvRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
- // pos
- v3f pos = checkFloatPos(L, 2);
- // Do it
- ServerActiveObject *obj = new RatSAO(env, pos);
- env->addActiveObject(obj);
+ infostream<<"EnvRef::l_add_rat(): C++ mobs have been removed."
+ <<" Doing nothing."<<std::endl;
return 0;
}
// pos = {x=num, y=num, z=num}
static int l_add_firefly(lua_State *L)
{
- infostream<<"EnvRef::l_add_firefly()"<<std::endl;
- EnvRef *o = checkobject(L, 1);
- ServerEnvironment *env = o->m_env;
- if(env == NULL) return 0;
- // pos
- v3f pos = checkFloatPos(L, 2);
- // Do it
- ServerActiveObject *obj = new FireflySAO(env, pos);
- env->addActiveObject(obj);
+ infostream<<"EnvRef::l_add_firefly(): C++ mobs have been removed."
+ <<" Doing nothing."<<std::endl;
return 0;
}
EnvRef(ServerEnvironment *env):
m_env(env)
{
- infostream<<"EnvRef created"<<std::endl;
+ //infostream<<"EnvRef created"<<std::endl;
}
~EnvRef()
{
- infostream<<"EnvRef destructing"<<std::endl;
+ //infostream<<"EnvRef destructing"<<std::endl;
}
// Creates an EnvRef and leaves it on top of stack
{0,0}
};
-/*
- Global functions
-*/
-
class LuaABM : public ActiveBlockModifier
{
private:
}
};
+/*
+ ServerSoundParams
+*/
+
+static void read_server_sound_params(lua_State *L, int index,
+ ServerSoundParams ¶ms)
+{
+ if(index < 0)
+ index = lua_gettop(L) + 1 + index;
+ // Clear
+ params = ServerSoundParams();
+ if(lua_istable(L, index)){
+ getfloatfield(L, index, "gain", params.gain);
+ getstringfield(L, index, "to_player", params.to_player);
+ lua_getfield(L, index, "pos");
+ if(!lua_isnil(L, -1)){
+ v3f p = read_v3f(L, -1)*BS;
+ params.pos = p;
+ params.type = ServerSoundParams::SSP_POSITIONAL;
+ }
+ lua_pop(L, 1);
+ lua_getfield(L, index, "object");
+ if(!lua_isnil(L, -1)){
+ ObjectRef *ref = ObjectRef::checkobject(L, -1);
+ ServerActiveObject *sao = ObjectRef::getobject(ref);
+ if(sao){
+ params.object = sao->getId();
+ params.type = ServerSoundParams::SSP_OBJECT;
+ }
+ }
+ lua_pop(L, 1);
+ params.max_hear_distance = BS*getfloatfield_default(L, index,
+ "max_hear_distance", params.max_hear_distance/BS);
+ getboolfield(L, index, "loop", params.loop);
+ }
+}
+
+/*
+ Global functions
+*/
+
// debug(text)
// Writes a line to dstream
static int l_debug(lua_State *L)
lua_getfield(L, table, "name");
if(lua_isstring(L, -1)){
std::string name = lua_tostring(L, -1);
- infostream<<"register_item_raw: "<<name<<std::endl;
+ verbosestream<<"register_item_raw: "<<name<<std::endl;
} else {
throw LuaError(L, "register_item_raw: name is not defined or not a string");
}
// Do it
lua_newtable(L);
int table = lua_gettop(L);
- u64 privs_i = server->getPlayerAuthPrivs(name);
- // Special case for the "name" setting (local player / server owner)
- if(name == g_settings->get("name"))
- privs_i = PRIV_ALL;
+ u64 privs_i = server->getPlayerEffectivePrivs(name);
std::set<std::string> privs_s = privsToSet(privs_i);
for(std::set<std::string>::const_iterator
i = privs_s.begin(); i != privs_s.end(); i++){
return 1;
}
+// sound_play(spec, parameters)
+static int l_sound_play(lua_State *L)
+{
+ SimpleSoundSpec spec;
+ read_soundspec(L, 1, spec);
+ ServerSoundParams params;
+ read_server_sound_params(L, 2, params);
+ s32 handle = get_server(L)->playSound(spec, params);
+ lua_pushinteger(L, handle);
+ return 1;
+}
+
+// sound_stop(handle)
+static int l_sound_stop(lua_State *L)
+{
+ int handle = luaL_checkinteger(L, 1);
+ get_server(L)->stopSound(handle);
+ return 0;
+}
+
static const struct luaL_Reg minetest_f [] = {
{"debug", l_debug},
{"log", l_log},
{"get_current_modname", l_get_current_modname},
{"get_modpath", l_get_modpath},
{"get_worldpath", l_get_worldpath},
+ {"sound_play", l_sound_play},
+ {"sound_stop", l_sound_stop},
{NULL, NULL}
};
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_export"<<std::endl;
+ verbosestream<<"scriptapi_export()"<<std::endl;
StackUnroller stack_unroller(L);
// Store server as light userdata in registry
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_add_environment"<<std::endl;
+ verbosestream<<"scriptapi_add_environment"<<std::endl;
StackUnroller stack_unroller(L);
// Create EnvRef on stack
luaentity
*/
-bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name,
- const std::string &staticdata)
+bool scriptapi_luaentity_add(lua_State *L, u16 id, const char *name)
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
+ verbosestream<<"scriptapi_luaentity_add: id="<<id<<" name=\""
<<name<<"\""<<std::endl;
StackUnroller stack_unroller(L);
lua_pushvalue(L, object); // Copy object to top of stack
lua_settable(L, -3);
+ return true;
+}
+
+void scriptapi_luaentity_activate(lua_State *L, u16 id,
+ const std::string &staticdata)
+{
+ realitycheck(L);
+ assert(lua_checkstack(L, 20));
+ verbosestream<<"scriptapi_luaentity_activate: id="<<id<<std::endl;
+ StackUnroller stack_unroller(L);
+
+ // Get minetest.luaentities[id]
+ luaentity_get(L, id);
+ int object = lua_gettop(L);
+
// Get on_activate function
lua_pushvalue(L, object);
lua_getfield(L, -1, "on_activate");
lua_pushlstring(L, staticdata.c_str(), staticdata.size());
// Call with 2 arguments, 0 results
if(lua_pcall(L, 2, 0, 0))
- script_error(L, "error running function %s:on_activate: %s\n",
- name, lua_tostring(L, -1));
+ script_error(L, "error running function on_activate: %s\n",
+ lua_tostring(L, -1));
}
-
- return true;
}
void scriptapi_luaentity_rm(lua_State *L, u16 id)
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
+ verbosestream<<"scriptapi_luaentity_rm: id="<<id<<std::endl;
// Get minetest.luaentities table
lua_getglobal(L, "minetest");
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
+ //infostream<<"scriptapi_luaentity_get_staticdata: id="<<id<<std::endl;
StackUnroller stack_unroller(L);
// Get minetest.luaentities[id]
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
+ //infostream<<"scriptapi_luaentity_get_properties: id="<<id<<std::endl;
StackUnroller stack_unroller(L);
// Get minetest.luaentities[id]
/* Read stuff */
+ prop->hp_max = getintfield_default(L, -1, "hp_max", 10);
+
getboolfield(L, -1, "physical", prop->physical);
getfloatfield(L, -1, "weight", prop->weight);
script_error(L, "error running function 'on_step': %s\n", lua_tostring(L, -1));
}
-// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch)
+// Calls entity:on_punch(ObjectRef puncher, time_from_last_punch,
+// tool_capabilities, direction)
void scriptapi_luaentity_punch(lua_State *L, u16 id,
- ServerActiveObject *puncher, float time_from_last_punch)
+ ServerActiveObject *puncher, float time_from_last_punch,
+ const ToolCapabilities *toolcap, v3f dir)
{
realitycheck(L);
assert(lua_checkstack(L, 20));
lua_pushvalue(L, object); // self
objectref_get_or_create(L, puncher); // Clicker reference
lua_pushnumber(L, time_from_last_punch);
- // Call with 2 arguments, 0 results
- if(lua_pcall(L, 3, 0, 0))
+ push_tool_capabilities(L, *toolcap);
+ push_v3f(L, dir);
+ // Call with 5 arguments, 0 results
+ if(lua_pcall(L, 5, 0, 0))
script_error(L, "error running function 'on_punch': %s\n", lua_tostring(L, -1));
}