diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2011-10-08 11:19:44 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2011-10-15 00:10:22 +0200 |
commit | 1e6b7a30b7232dce7b8240441bed04912e06f666 (patch) | |
tree | b5aa2d893c2bee4268a3cfc8172dc6ae7ea6ce09 /src/scripting/lua.cpp | |
parent | 9cd09f012ff7c01fbd51b972f9b716cfc8246b2c (diff) | |
download | manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.tar.gz manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.tar.bz2 manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.tar.xz manaserv-1e6b7a30b7232dce7b8240441bed04912e06f666.zip |
Added script bindings for reading map objects
map_get_objects([string filter]): returns all object of the current map optionally filtered by type.
map_get_object_property(handle object, string key): returns the value of the property of the object.
map_get_object_bounds(handle object): returns x, y, width, height of an object.
map_get_object_name(handle object): returns name of an object.
map_get_object_type(handle object): returns type of an object.
Mantis-issue: 397
Reviewed-by: Thorbjørn Lindeijer
Diffstat (limited to 'src/scripting/lua.cpp')
-rw-r--r-- | src/scripting/lua.cpp | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp index 7213bd09..16781937 100644 --- a/src/scripting/lua.cpp +++ b/src/scripting/lua.cpp @@ -2102,6 +2102,132 @@ static int get_distance(lua_State *s) return 1; } +/** + * mana.map_get_objects(): table of all objects + * mana.map_get_objects(string type): table of all objects of type + * Gets the objects of a map. + */ +static int map_get_objects(lua_State *s) +{ + const bool filtered = (lua_gettop(s) == 1); + std::string filter; + if (filtered) + filter = luaL_checkstring(s, 1); + + lua_pushlightuserdata(s, (void *)®istryKey); + lua_gettable(s, LUA_REGISTRYINDEX); + Script *t = static_cast<Script *>(lua_touserdata(s, -1)); + const std::vector<MapObject*> &objects = t->getMap()->getMap()->getObjects(); + + if (!filtered) + pushSTLContainer<MapObject*>(s, objects); + else + { + std::vector<MapObject*> filteredObjects; + for (std::vector<MapObject*>::const_iterator it = objects.begin(); + it != objects.end(); ++it) + { + if (utils::compareStrI((*it)->getType(), filter) == 0) + { + filteredObjects.push_back(*it); + } + } + pushSTLContainer<MapObject*>(s, filteredObjects); + } + return 1; +} + +/** + * mana.map_object_get_property(handle object, string key) + * Returns the value of the object property 'key'. + */ +static int map_object_get_property(lua_State *s) +{ + std::string key = luaL_checkstring(s, 2); + if (!lua_islightuserdata(s, 1)) + { + raiseScriptError(s, "map_object_get_property called with invalid" + "object handle"); + return 0; + } + MapObject *obj = static_cast<MapObject *>(lua_touserdata(s, 1)); + if (obj) + { + std::string property = obj->getProperty(key); + if (!property.empty()) + { + lua_pushstring(s, property.c_str()); + return 1; + } + else + { + // scripts can check for nil + return 0; + } + } + else + { + raiseScriptError(s, "map_object_get_property called with invalid" + "object handle"); + return 0; + } +} + +/** + * mana.map_object_get_bounds(object) + * Returns 4 int: x/y/width/height of object. + */ +static int map_object_get_bounds(lua_State *s) +{ + if (!lua_islightuserdata(s, 1)) + { + raiseScriptError(s, "map_object_get_bounds called with invalid" + "object handle"); + return 0; + } + MapObject *obj = static_cast<MapObject *>(lua_touserdata(s, 1)); + const Rectangle &bounds = obj->getBounds(); + lua_pushinteger(s, bounds.x); + lua_pushinteger(s, bounds.y); + lua_pushinteger(s, bounds.w); + lua_pushinteger(s, bounds.h); + return 4; +} + +/** + * mana.map_object_get_name(object) + * Returns the name of the object. + */ +static int map_object_get_name(lua_State *s) +{ + if (!lua_islightuserdata(s, 1)) + { + raiseScriptError(s, "map_object_get_name called with invalid" + "object handle"); + return 0; + } + MapObject *obj = static_cast<MapObject *>(lua_touserdata(s, 1)); + lua_pushstring(s, obj->getName().c_str()); + return 1; +} + +/** + * mana.map_object_get_type(object) + * Returns the type of the object. + */ +static int map_object_get_type(lua_State *s) +{ + if (!lua_islightuserdata(s, 1)) + { + raiseScriptError(s, "map_object_get_type called with invalid" + "object handle"); + return 0; + } + MapObject *obj = static_cast<MapObject *>(lua_touserdata(s, 1)); + lua_pushstring(s, obj->getType().c_str()); + return 1; +} + static int require_loader(lua_State *s) { // Add .lua extension (maybe only do this when it doesn't have it already) @@ -2209,6 +2335,11 @@ LuaScript::LuaScript(): { "npc_ask_string", &npc_ask_string }, { "log", &log }, { "get_distance", &get_distance }, + { "map_get_objects", &map_get_objects }, + { "map_object_get_property", &map_object_get_property }, + { "map_object_get_bounds", &map_object_get_bounds }, + { "map_object_get_name", &map_object_get_name }, + { "map_object_get_type", &map_object_get_type }, { NULL, NULL } }; luaL_register(mState, "mana", callbacks); |