random scripting work-in-progress

This commit is contained in:
Perttu Ahola 2011-11-11 20:50:09 +02:00
parent f8430723e0
commit a6a1e6ed1a
7 changed files with 55 additions and 24 deletions

@ -131,9 +131,10 @@ end
print("omg lol")
print("minetest dump: "..dump(minetest))
minetest.register_object("a", "dummy string");
minetest.register_entity("a", "dummy string");
local TNT = minetest.new_entity {
--local TNT = minetest.new_entity {
local TNT = {
-- Maybe handle gravity and collision this way? dunno
physical = true,
weight = 5,
@ -176,9 +177,9 @@ end
print("TNT dump: "..dump(TNT))
print("Registering TNT");
minetest.register_object("TNT", TNT)
minetest.register_entity("TNT", TNT)
--print("minetest.registered_objects: "..dump(minetest.registered_objects))
print("minetest.registered_objects:")
serialize(minetest.registered_objects)
--print("minetest.registered_entities: "..dump(minetest.registered_entities))
print("minetest.registered_entities:")
serialize(minetest.registered_entities)

@ -75,6 +75,8 @@ std::string item_craft_get_image_name(const std::string &subname)
return "apple.png^[forcesingle";
else if(subname == "apple_iron")
return "apple_iron.png";
else if(subname == "testobject1") // test object
return "unknown_block.png^[forcesingle";
else
return "cloud.png"; // just something
}
@ -92,13 +94,18 @@ ServerActiveObject* item_craft_create_object(const std::string &subname,
ServerActiveObject *obj = new FireflySAO(env, pos);
return obj;
}
else if(subname == "testobject1")
{
ServerActiveObject *obj = new LuaEntitySAO(env, pos, "TNT", "");
return obj;
}
return NULL;
}
s16 item_craft_get_drop_count(const std::string &subname)
{
if(subname == "rat" || subname == "firefly")
if(subname == "rat" || subname == "firefly" || subname == "testobject1")
return 1;
return -1;

@ -1576,9 +1576,22 @@ std::string LuaEntitySAO::getStaticData()
// name
os<<serializeString(m_init_name);
// state
if(m_registered){
lua_State *L = m_env->getLua();
scriptapi_luaentity_deregister(L, m_id);
std::string state = scriptapi_luaentity_get_state(L, m_id);
os<<serializeString(state);
os<<serializeLongString(state);
} else {
os<<serializeLongString(m_init_state);
}
return os.str();
}
InventoryItem* LuaEntitySAO::createPickedUpItem()
{
std::istringstream is("CraftItem testobject1 1", std::ios_base::binary);
InventoryItem *item = InventoryItem::deSerialize(is);
return item;
}

@ -211,6 +211,7 @@ public:
void step(float dtime, bool send_recommended);
std::string getClientInitializationData();
std::string getStaticData();
InventoryItem* createPickedUpItem();
private:
std::string m_init_name;
std::string m_init_state;

@ -108,7 +108,8 @@ bool script_load(lua_State *L, const char *path)
infostream<<"Loading and running script from "<<path<<std::endl;
int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, 0);
if(ret){
errorstream<<"Failed to load and run script from "<<path<<": "<<lua_tostring(L, -1)<<std::endl;
errorstream<<"Failed to load and run script from "<<path<<":"<<std::endl;
errorstream<<"[LUA] "<<lua_tostring(L, -1)<<std::endl;
lua_pop(L, 1); // Pop error message from stack
return false;
}

@ -77,24 +77,25 @@ static void realitycheck(lua_State *L)
}
// Register new object prototype (must be based on entity)
static int l_register_object(lua_State *L)
static int l_register_entity(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
luaL_checkany(L, 2);
infostream<<"register_object: "<<name<<std::endl;
infostream<<"register_entity: "<<name<<std::endl;
// Get the minetest table
lua_getglobal(L, "minetest");
// Get field "registered_objects"
lua_getfield(L, -1, "registered_objects");
// Get field "registered_entities"
lua_getfield(L, -1, "registered_entities");
luaL_checktype(L, -1, LUA_TTABLE);
int objectstable = lua_gettop(L);
// Object is in param 2
lua_pushvalue(L, 2); // Copy object to top of stack
lua_setfield(L, objectstable, name); // registered_objects[name] = object
lua_setfield(L, objectstable, name); // registered_entities[name] = object
return 0; /* number of results */
}
#if 0
static int l_new_entity(lua_State *L)
{
/* o = o or {}
@ -111,10 +112,11 @@ static int l_new_entity(lua_State *L)
// return table
return 1;
}
#endif
static const struct luaL_Reg minetest_f [] = {
{"register_object", l_register_object},
{"new_entity", l_new_entity},
{"register_entity", l_register_entity},
//{"new_entity", l_new_entity},
{NULL, NULL}
};
@ -251,9 +253,9 @@ void scriptapi_export(lua_State *L, Server *server)
// Get the main minetest table
lua_getglobal(L, "minetest");
// Add registered_objects table in minetest
// Add registered_entities table in minetest
lua_newtable(L);
lua_setfield(L, -2, "registered_objects");
lua_setfield(L, -2, "registered_entities");
// Add object_refs table in minetest
lua_newtable(L);
@ -328,7 +330,7 @@ void scriptapi_luaentity_deregister(lua_State *L, u16 id)
lua_pop(L, 1); // pop object*/
// Set luaentities[id] = nil
lua_pushnumber(L, cobj->getId()); // Push id
lua_pushnumber(L, id); // Push id
lua_pushnil(L);
lua_settable(L, objectstable);
@ -340,7 +342,7 @@ void scriptapi_luaentity_step(lua_State *L, u16 id,
{
realitycheck(L);
assert(lua_checkstack(L, 20));
infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
// Get minetest.luaentities table
lua_getglobal(L, "minetest");
@ -349,7 +351,7 @@ void scriptapi_luaentity_step(lua_State *L, u16 id,
int objectstable = lua_gettop(L);
// Get luaentities[id]
lua_pushnumber(L, cobj->getId()); // Push id
lua_pushnumber(L, id); // Push id
lua_gettable(L, objectstable);
// TODO: Call step function

@ -989,8 +989,14 @@ Server::Server(
// Export API
scriptapi_export(m_lua, this);
// Load and run scripts
script_load(m_lua, (porting::path_data + DIR_DELIM + "scripts"
+ DIR_DELIM + "default.lua").c_str());
std::string defaultscript = porting::path_data + DIR_DELIM
+ "scripts" + DIR_DELIM + "default.lua";
bool success = script_load(m_lua, defaultscript.c_str());
if(!success){
errorstream<<"Server: Failed to load and run "
<<defaultscript<<std::endl;
assert(0);
}
// Initialize Environment