summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--example/serverdata/scripts/maps/desert.lua2
-rw-r--r--src/scripting/lua.cpp32
2 files changed, 34 insertions, 0 deletions
diff --git a/example/serverdata/scripts/maps/desert.lua b/example/serverdata/scripts/maps/desert.lua
index b192b83..7449192 100644
--- a/example/serverdata/scripts/maps/desert.lua
+++ b/example/serverdata/scripts/maps/desert.lua
@@ -85,6 +85,8 @@ function Harmony_update(npc)
end
function Tamer(npc, ch, list)
+ mana.being_say(npc, string.format("You are %s pixel away.",
+ mana.get_distance(npc, ch)))
mana.being_say(npc, "I will now spawn a monster for your training session.")
local m1 = mana.monster_create(1, mana.posX(ch), mana.posY(ch))
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index d55fb13..d461bf4 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -53,6 +53,7 @@ extern "C" {
#include "utils/speedconv.h"
#include <string.h>
+#include <math.h>
/*
* This file includes all script bindings available to LUA scripts.
@@ -1834,6 +1835,36 @@ static int log(lua_State *s)
return 0;
}
+/**
+ * Gets the distance between two beings or two points
+ */
+static int get_distance(lua_State *s)
+{
+ int x1, y1, x2, y2;
+ if (lua_gettop(s) == 2)
+ {
+ Being *being1 = getBeing(s, 1);
+ Being *being2 = getBeing(s, 2);
+ x1 = being1->getPosition().x;
+ y1 = being1->getPosition().y;
+ x2 = being2->getPosition().x;
+ y2 = being2->getPosition().y;
+ }
+ else
+ {
+ x1 = luaL_checkint(s, 1);
+ y1 = luaL_checkint(s, 2);
+ x2 = luaL_checkint(s, 3);
+ y2 = luaL_checkint(s, 4);
+ }
+ const int dx = x1 - x2;
+ const int dy = y1 - y2;
+ const float dist = sqrt((dx * dx) + (dy * dy));
+ lua_pushinteger(s, dist);
+
+ return 1;
+}
+
static int require_loader(lua_State *s)
{
// Add .lua extension (maybe only do this when it doesn't have it already)
@@ -1934,6 +1965,7 @@ LuaScript::LuaScript():
{ "npc_end", &npc_end },
{ "npc_ask_string", &npc_ask_string },
{ "log", &log },
+ { "get_distance", &get_distance },
{ NULL, NULL }
};
luaL_register(mState, "mana", callbacks);