summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormekolat <mekolat@users.noreply.github.com>2015-07-14 15:04:47 -0400
committermekolat <mekolat@users.noreply.github.com>2016-04-15 11:45:34 -0400
commitfee59601b3dd16c10e767cac44ee5197db7811e8 (patch)
tree33791e29cbe15c90ca9206cfa813ef98e786dd41
parentcc4e5c99f17846611db09e40b244e693b78ee94a (diff)
downloadtmwa-fee59601b3dd16c10e767cac44ee5197db7811e8.tar.gz
tmwa-fee59601b3dd16c10e767cac44ee5197db7811e8.tar.bz2
tmwa-fee59601b3dd16c10e767cac44ee5197db7811e8.tar.xz
tmwa-fee59601b3dd16c10e767cac44ee5197db7811e8.zip
add builtin_distance
-rw-r--r--src/map/script-fun.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/map/script-fun.cpp b/src/map/script-fun.cpp
index 6004072..53b4526 100644
--- a/src/map/script-fun.cpp
+++ b/src/map/script-fun.cpp
@@ -533,6 +533,37 @@ void builtin_eltlvl(ScriptState *st)
*------------------------------------------
*/
static
+void builtin_distance(ScriptState *st)
+{
+ dumb_ptr<block_list> source = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(0))));
+ dumb_ptr<block_list> target = map_id2bl(wrap<BlockId>(conv_num(st, &AARG(1))));
+ int distance = 0;
+ int mode = HARG(2) ? conv_num(st, &AARG(2)) : 0;
+
+ switch (mode)
+ {
+ // TODO implement case 1 (walk distance)
+ case 0:
+ default:
+ if (source->bl_m->name_ != target->bl_m->name_)
+ {
+ // FIXME make it work even if source and target are not in the same map
+ distance = 0x7fffffff;
+ break;
+ }
+ int dx = abs(source->bl_x - target->bl_x);
+ int dy = abs(source->bl_y - target->bl_y);
+ distance = sqrt((dx * dx) + (dy * dy)); // Pythagoras' theorem
+ }
+
+ push_int<ScriptDataInt>(st->stack, distance);
+}
+
+/*==========================================
+ *
+ *------------------------------------------
+ */
+static
void builtin_target(ScriptState *st)
{
// TODO maybe scrap all this and make it use battle_ functions? (add missing functions to battle)
@@ -4254,6 +4285,7 @@ BuiltinFunction builtin_functions[] =
BUILTIN(cbrt, "i"_s, 'i'),
BUILTIN(pow, "ii"_s, 'i'),
BUILTIN(target, "iii"_s, 'i'),
+ BUILTIN(distance, "ii?"_s, 'i'),
{nullptr, ""_s, ""_s, '\0'},
};
} // namespace map