From fee59601b3dd16c10e767cac44ee5197db7811e8 Mon Sep 17 00:00:00 2001 From: mekolat Date: Tue, 14 Jul 2015 15:04:47 -0400 Subject: add builtin_distance --- src/map/script-fun.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/map/script-fun.cpp') 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 @@ -528,6 +528,37 @@ void builtin_eltlvl(ScriptState *st) push_int(st->stack, element_lvl); } +/*========================================== + * + *------------------------------------------ + */ +static +void builtin_distance(ScriptState *st) +{ + dumb_ptr source = map_id2bl(wrap(conv_num(st, &AARG(0)))); + dumb_ptr target = map_id2bl(wrap(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(st->stack, distance); +} + /*========================================== * *------------------------------------------ @@ -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 -- cgit v1.2.3-70-g09d2