random scripting work-in-progress
authorPerttu Ahola <celeron55@gmail.com>
Fri, 11 Nov 2011 18:50:09 +0000 (20:50 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 29 Nov 2011 17:13:39 +0000 (19:13 +0200)
data/scripts/default.lua
src/content_inventory.cpp
src/content_sao.cpp
src/content_sao.h
src/script.cpp
src/scriptapi.cpp
src/server.cpp

index 22486ac5ac092d2ca37e8a608fbd2a7815b92f01..c525ecf1e4331a0ef7962b690d12796f600ee1a8 100644 (file)
@@ -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)
 
index 1d5c6b35594214f483c5bbc981ec40eb8dc627a1..51c6f751efa2e60615b4759d9655aea820d31ea7 100644 (file)
@@ -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;
index d51e92a8c50738bafb9e6a83a8325df8f39c50e1..d1303b4712686b46fe005e82538240601b52b5dd 100644 (file)
@@ -1576,9 +1576,22 @@ std::string LuaEntitySAO::getStaticData()
        // name
        os<<serializeString(m_init_name);
        // state
-       std::string state = scriptapi_luaentity_get_state(L, m_id);
-       os<<serializeString(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<<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;
+}
+
 
index 44c0b7172c77d7c274bea18bb087d2e5db97cce4..f0eec29b66a44c1cfe6406b909cbe17d15beb473 100644 (file)
@@ -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;
index edfc596b8f4a114f045c32565d323695fa9359c5..0059683b7de17db2ceb06cc9809e1716d8452c5f 100644 (file)
@@ -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;
        }
index fbe383075ed7494c97bbb8ed9efbf330586227cf..d055c19766ffe55d90cf8526ca5ccaffc3790d3f 100644 (file)
@@ -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
index 1c5d8d937b070c5943abf23e311cb7e9fc146106..82671bf896e1ee3e9f6df322e28dfafb2a060ca9 100644 (file)
@@ -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