From a6a1e6ed1a8de1294970a5af6ba992c38d4022b8 Mon Sep 17 00:00:00 2001
From: Perttu Ahola <celeron55@gmail.com>
Date: Fri, 11 Nov 2011 20:50:09 +0200
Subject: [PATCH] random scripting work-in-progress

---
 data/scripts/default.lua  | 13 +++++++------
 src/content_inventory.cpp |  9 ++++++++-
 src/content_sao.cpp       | 17 +++++++++++++++--
 src/content_sao.h         |  1 +
 src/script.cpp            |  3 ++-
 src/scriptapi.cpp         | 26 ++++++++++++++------------
 src/server.cpp            | 10 ++++++++--
 7 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/data/scripts/default.lua b/data/scripts/default.lua
index 22486ac5a..c525ecf1e 100644
--- a/data/scripts/default.lua
+++ b/data/scripts/default.lua
@@ -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)
 
diff --git a/src/content_inventory.cpp b/src/content_inventory.cpp
index 1d5c6b355..51c6f751e 100644
--- a/src/content_inventory.cpp
+++ b/src/content_inventory.cpp
@@ -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;
diff --git a/src/content_sao.cpp b/src/content_sao.cpp
index d51e92a8c..d1303b471 100644
--- a/src/content_sao.cpp
+++ b/src/content_sao.cpp
@@ -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;
+}
+
 
diff --git a/src/content_sao.h b/src/content_sao.h
index 44c0b7172..f0eec29b6 100644
--- a/src/content_sao.h
+++ b/src/content_sao.h
@@ -211,6 +211,7 @@ class LuaEntitySAO : public ServerActiveObject
 	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;
diff --git a/src/script.cpp b/src/script.cpp
index edfc596b8..0059683b7 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -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;
 	}
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index fbe383075..d055c1976 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -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
diff --git a/src/server.cpp b/src/server.cpp
index 1c5d8d937..82671bf89 100644
--- a/src/server.cpp
+++ b/src/server.cpp
@@ -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
 	
-- 
GitLab