summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorErik Schilling <ablu.erikschilling@googlemail.com>2013-05-04 23:48:20 +0200
committerErik Schilling <ablu.erikschilling@googlemail.com>2013-05-11 23:31:15 +0200
commit01df6f017d798a1973ab5c20d3d895cb7f86dcb5 (patch)
tree4af7dea96f2a365acc06820262e78d66f4fa858d
parent7e88a973c00bd96af1288399a4e6dab207e8683c (diff)
downloadmanaserv-01df6f017d798a1973ab5c20d3d895cb7f86dcb5.tar.gz
manaserv-01df6f017d798a1973ab5c20d3d895cb7f86dcb5.tar.xz
manaserv-01df6f017d798a1973ab5c20d3d895cb7f86dcb5.zip
Added function to check for lenght of a possible path
-rw-r--r--src/scripting/lua.cpp46
-rw-r--r--src/scripting/luautil.cpp16
-rw-r--r--src/scripting/luautil.h1
3 files changed, 54 insertions, 9 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 9ffe1f2..75cb749 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -1531,15 +1531,8 @@ static int entity_set_direction(lua_State *s)
static int entity_set_walkmask(lua_State *s)
{
Entity *being = checkActor(s, 1);
- const char *stringMask = luaL_checkstring(s, 2);
- unsigned char mask = 0x00;
- if (strchr(stringMask, 'w'))
- mask |= Map::BLOCKMASK_WALL;
- else if (strchr(stringMask, 'c'))
- mask |= Map::BLOCKMASK_CHARACTER;
- else if (strchr(stringMask, 'm'))
- mask |= Map::BLOCKMASK_MONSTER;
- being->getComponent<ActorComponent>()->setWalkMask(mask);
+ unsigned char walkmask = checkWalkMask(s, 2);
+ being->getComponent<ActorComponent>()->setWalkMask(walkmask);
return 0;
}
@@ -2492,6 +2485,40 @@ static int is_walkable(lua_State *s)
return 1;
}
+/** LUA get_path_length (mapinformation)
+ * get_path_lenght(int startX, int startY, int destX, int destY, int maxRange)
+ * get_path_lenght(int startX, int startY, int destX, int destY, int maxRange,
+ * string walkmask)
+ **
+ * Tries to find a path from the start coordinates to the target ones with a
+ * maximum of ''maxRange'' steps (in tiles).
+ *
+ * If no ''walkmask'' is passed '''w''' is used.
+ *
+ * **Return value:** The number of steps (in tiles) are required to reach
+ * the target or 0 if no path was found.
+ */
+static int get_path_length(lua_State *s)
+{
+ const int startX = luaL_checkint(s, 1);
+ const int startY = luaL_checkint(s, 2);
+ const int destX = luaL_checkint(s, 3);
+ const int destY = luaL_checkint(s, 4);
+ unsigned maxRange = luaL_checkint(s, 5);
+ unsigned char walkmask = BLOCKTYPE_WALL;
+ if (lua_gettop(s) > 5)
+ walkmask = checkWalkMask(s, 6);
+
+ Map *map = checkCurrentMap(s)->getMap();
+ Path path = map->findPath(startX / map->getTileWidth(),
+ startY / map->getTileHeight(),
+ destX / map->getTileWidth(),
+ destY / map->getTileHeight(),
+ walkmask, maxRange);
+ lua_pushinteger(s, path.size());
+ return 1;
+}
+
/** LUA map_get_pvp (mapinformation)
* map_get_pvp()
**
@@ -3304,6 +3331,7 @@ LuaScript::LuaScript():
{ "get_map_id", get_map_id },
{ "get_map_property", get_map_property },
{ "is_walkable", is_walkable },
+ { "get_path_length", get_path_length },
{ "map_get_pvp", map_get_pvp },
{ "item_drop", item_drop },
{ "log", log },
diff --git a/src/scripting/luautil.cpp b/src/scripting/luautil.cpp
index f889aa6..7dd2b19 100644
--- a/src/scripting/luautil.cpp
+++ b/src/scripting/luautil.cpp
@@ -21,6 +21,8 @@
#include "luautil.h"
+#include <string.h>
+
#include "game-server/character.h"
#include "game-server/itemmanager.h"
#include "game-server/monster.h"
@@ -235,6 +237,20 @@ AbilityManager::AbilityInfo *checkAbility(lua_State *s, int p)
return abilityInfo;
}
+unsigned char checkWalkMask(lua_State *s, int p)
+{
+ const char *stringMask = luaL_checkstring(s, p);
+ unsigned char mask = 0x00;
+ if (strchr(stringMask, 'w'))
+ mask |= Map::BLOCKMASK_WALL;
+ if (strchr(stringMask, 'c'))
+ mask |= Map::BLOCKMASK_CHARACTER;
+ if (strchr(stringMask, 'm'))
+ mask |= Map::BLOCKMASK_MONSTER;
+
+ return mask;
+}
+
MapComposite *checkCurrentMap(lua_State *s, Script *script /* = 0 */)
{
diff --git a/src/scripting/luautil.h b/src/scripting/luautil.h
index 653bf24..c747d2a 100644
--- a/src/scripting/luautil.h
+++ b/src/scripting/luautil.h
@@ -179,6 +179,7 @@ MonsterClass * checkMonsterClass(lua_State *s, int p);
Entity * checkNpc(lua_State *s, int p);
int checkSkill(lua_State *s, int p);
AbilityManager::AbilityInfo *checkAbility(lua_State *s, int p);
+unsigned char checkWalkMask(lua_State *s, int p);
MapComposite * checkCurrentMap(lua_State *s, Script *script = 0);
Script::Thread* checkCurrentThread(lua_State *s, Script *script = 0);