summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog8
-rw-r--r--data/scripts/test.lua26
-rw-r--r--src/scripting/lua.cpp142
3 files changed, 174 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index bd5bfaa1..51d96b2b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2008-11-02 Philipp Sehmisch <tmw@crushnet.org>
+
+ * src/scripting/lua.cpp: Added convenience wrappers for converting
+ STL lists, vectors, maps and sets to lua tables.
+ * data/scripts/test.lua: Added a dialog option to test npc which
+ retreives some stl containers from the c++ part and prints them to
+ the server console.
+
2008-11-06 David Athay <ko2fan@gmail.com>
* src/game-server/commandhandler.cpp, src/utils/string.cpp,
diff --git a/data/scripts/test.lua b/data/scripts/test.lua
index 98892951..70d21d76 100644
--- a/data/scripts/test.lua
+++ b/data/scripts/test.lua
@@ -57,7 +57,8 @@ function npc1_talk(npc, ch)
"To buy.",
"To sell.",
"To make a donation.",
- "Slowly count from one to ten.")
+ "Slowly count from one to ten.",
+ "Tablepush Test")
if v == 1 then
do_message(npc, ch, "Sorry, this is a heroic-fantasy game, I do not have any gun.")
elseif v == 2 then
@@ -98,6 +99,29 @@ function npc1_talk(npc, ch)
schedule_in(16, function() tmw.being_say(npc, "Eight") end)
schedule_in(18, function() tmw.being_say(npc, "Nine") end)
schedule_in(20, function() tmw.being_say(npc, "Ten") end)
+ elseif v == 7 then
+ local t1, t2, t3, t4 = tmw.test_tableget();
+ print("---------------");
+ print ("Table 1:");
+ for k,v in pairs(t1) do
+ print (k, ":", v)
+ end
+
+ print ("Table 2:");
+ for k,v in pairs(t2) do
+ print (k, ":", v)
+ end
+
+ print ("Table 3:");
+ for k,v in pairs(t3) do
+ print (k, ":", v)
+ end
+
+ print ("Table 4:");
+ for k,v in pairs(t4) do
+ print (k, ":", v)
+ end
+ print("---------------");
end
end
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 34e66427..38b044d0 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -22,6 +22,10 @@
*/
#include <cassert>
+#include <list>
+#include <map>
+#include <set>
+#include <vector>
extern "C" {
#include <lualib.h>
@@ -139,6 +143,94 @@ static Being *getBeing(lua_State *s, int p)
return static_cast<Being *>(t);
}
+/* Polymorphic wrapper for pushing variables.
+ Useful for templates.*/
+void push(lua_State *s, int val)
+{
+ lua_pushinteger(s, val);
+}
+void push(lua_State *s, const std::string &val)
+{
+ lua_pushstring(s, val.c_str());
+}
+void push(lua_State *s, Thing* val)
+{
+ lua_pushlightuserdata(s, val);
+}
+void push(lua_State *s, double val)
+{
+ lua_pushnumber(s, val);
+}
+
+/* Pushes an STL LIST */
+template <typename T> void pushSTLContainer(lua_State *s, const std::list<T> &container)
+{
+ int len = container.size();
+ lua_newtable(s);
+ int table = lua_gettop(s);
+ typename std::list<T>::const_iterator i;
+ i = container.begin();
+
+ for (int key = 1; key <= len; key++)
+ {
+ push(s, key);
+ push(s, *i);
+ lua_settable(s, table);
+ i++;
+ }
+}
+
+/* Pushes an STL VECTOR */
+template <typename T> void pushSTLContainer(lua_State *s, const std::vector<T> &container)
+{
+ int len = container.size();
+ lua_createtable(s, 0, len);
+ int table = lua_gettop(s);
+
+ for (int key = 0; key < len; key++)
+ {
+ push(s, key+1);
+ push(s, container.at(key).c_str());
+ lua_settable(s, table);
+ }
+}
+
+/* Pushes an STL MAP */
+template <typename Tkey, typename Tval> void pushSTLContainer(lua_State *s, const std::map<Tkey, Tval> &container)
+{
+ int len = container.size();
+ lua_createtable(s, 0, len);
+ int table = lua_gettop(s);
+ typename std::map<Tkey, Tval>::const_iterator i;
+ i = container.begin();
+
+ for (int key = 1; key <= len; key++)
+ {
+ push(s, i->first.c_str());
+ push(s, i->second.c_str());
+ lua_settable(s, table);
+ i++;
+ }
+}
+
+/* Pushes an STL SET */
+template <typename T> void pushSTLContainer(lua_State *s, const std::set<T> &container)
+{
+ int len = container.size();
+ lua_newtable(s);
+ int table = lua_gettop(s);
+ typename std::set<T>::const_iterator i;
+ i = container.begin();
+
+ for (int key = 1; key <= len; key++)
+ {
+ push(s, key);
+ push(s, *i);
+ lua_settable(s, table);
+ i++;
+ }
+}
+
/**
* Callback for sending a NPC_MESSAGE.
* tmw.npc_message(npc, character, string)
@@ -973,6 +1065,54 @@ static int LuaExpForLevel(lua_State *s)
return 1;
}
+/**
+ * Returns four useless tables for testing the STL container push wrappers.
+ * This function can be removed when there are more useful functions which use
+ * them.
+ */
+static int LuaTest_Tableget(lua_State *s)
+{
+
+ std::list<float> list;
+ std::vector<std::string> vector;
+ std::map<std::string, std::string> map;
+ std::set<int> set;
+
+ LOG_INFO("Pushing List");
+ list.push_back(12.636);
+ list.push_back(0.0000000045656);
+ list.push_back(185645445634566.346);
+ list.push_back(7835458.11);
+ pushSTLContainer<float>(s, list);
+
+ LOG_INFO("Pushing Vector");
+ vector.push_back("All");
+ vector.push_back("your");
+ vector.push_back("base");
+ vector.push_back("are");
+ vector.push_back("belong");
+ vector.push_back("to");
+ vector.push_back("us!");
+ pushSTLContainer<std::string>(s, vector);
+
+ LOG_INFO("Pushing Map");
+ map["Apple"] = "red";
+ map["Banana"] = "yellow";
+ map["Lime"] = "green";
+ map["Plum"] = "blue";
+ pushSTLContainer<std::string, std::string>(s, map);
+
+ LOG_INFO("Pushing Set");
+ set.insert(12);
+ set.insert(8);
+ set.insert(14);
+ set.insert(10);
+ pushSTLContainer<int>(s, set);
+
+
+ return 4;
+}
+
LuaScript::LuaScript():
nbArgs(-1)
@@ -1010,6 +1150,7 @@ LuaScript::LuaScript():
{ "get_beings_in_circle", &LuaGetBeingsInCircle },
{ "note_on_death", &LuaNoteOnDeath },
{ "effect_create", &LuaEffect_Create },
+ { "test_tableget", &LuaTest_Tableget },
{ NULL, NULL }
};
luaL_register(mState, "tmw", callbacks);
@@ -1049,7 +1190,6 @@ void LuaScript::push(std::string const &v)
++nbArgs;
}
-
void LuaScript::push(Thing *v)
{
assert(nbArgs >= 0);