summaryrefslogtreecommitdiff
path: root/src
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-08-26 22:56:46 +0200
commit5612cc82a1287df05b3298437f64f6dcc378a0f4 (patch)
tree4c337b65358ef5ce5a7f74bffb1aadf3e4b38f50 /src
parent13cfbb7bb8b59ea31bebe0960e0dd3b054be3ba5 (diff)
downloadmanaserv-5612cc82a1287df05b3298437f64f6dcc378a0f4.tar.gz
manaserv-5612cc82a1287df05b3298437f64f6dcc378a0f4.tar.bz2
manaserv-5612cc82a1287df05b3298437f64f6dcc378a0f4.tar.xz
manaserv-5612cc82a1287df05b3298437f64f6dcc378a0f4.zip
Added function to check for length of a possible path
Diffstat (limited to 'src')
-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 fb6990e5..726c14e0 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -1530,15 +1530,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;
}
@@ -2491,6 +2484,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()
**
@@ -3309,6 +3336,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 f889aa6c..7dd2b199 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 653bf24a..c747d2a1 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);