From e04fe0622613bca435ce922765429b729f6873bc Mon Sep 17 00:00:00 2001 From: MajorCooke Date: Wed, 10 Feb 2016 17:13:50 -0600 Subject: [PATCH 1/3] GetDistance Non-Action (Double version) - Added GetDistance(bool checkz, ptr = aaptr_target). - Returns the distance of an actor. Must be target, master or tracer. --- src/thingdef/thingdef_codeptr.cpp | 35 +++++++++++++++++++++++++++++++ wadsrc/static/actors/actor.txt | 1 + 2 files changed, 36 insertions(+) diff --git a/src/thingdef/thingdef_codeptr.cpp b/src/thingdef/thingdef_codeptr.cpp index 1e15a5c33..9a8e02468 100644 --- a/src/thingdef/thingdef_codeptr.cpp +++ b/src/thingdef/thingdef_codeptr.cpp @@ -250,6 +250,41 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, CountInv) return 0; } +//========================================================================== +// +// GetDistance +// +// NON-ACTION function to get the distance in double. +// +//========================================================================== +DEFINE_ACTION_FUNCTION_PARAMS(AActor, GetDistance) +{ + if (numret > 0) + { + assert(ret != NULL); + PARAM_PROLOGUE; + PARAM_OBJECT(self, AActor); + PARAM_BOOL(checkz); + PARAM_INT_OPT(ptr) { ptr = AAPTR_TARGET; } + + AActor *target = COPY_AAPTR(self, ptr); + + if (!target || target == self) + { + ret->SetFloat(false); + } + else + { + fixedvec3 diff = self->Vec3To(target); + diff.z += (target->height - self->height) / 2; + const double lengthsquared = TVector3(diff.x, diff.y, diff.z).LengthSquared(); + ret->SetFloat(lengthsquared); + } + return 1; + } + return 0; +} + //========================================================================== // // A_RearrangePointers diff --git a/wadsrc/static/actors/actor.txt b/wadsrc/static/actors/actor.txt index 8e30693cf..48ee9f68f 100644 --- a/wadsrc/static/actors/actor.txt +++ b/wadsrc/static/actors/actor.txt @@ -41,6 +41,7 @@ ACTOR Actor native //: Thinker native bool CheckClass(class checkclass, int ptr_select = AAPTR_DEFAULT, bool match_superclass = false); native bool IsPointerEqual(int ptr_select1, int ptr_select2); native int CountInv(class itemtype, int ptr_select = AAPTR_DEFAULT); + native float GetDistance(bool checkz, int ptr = AAPTR_DEFAULT); // Action functions // Meh, MBF redundant functions. Only for DeHackEd support. From fefdb266cd048b02b676943f575d9c712c9f1593 Mon Sep 17 00:00:00 2001 From: MajorCooke Date: Wed, 10 Feb 2016 17:48:17 -0600 Subject: [PATCH 2/3] - Fixed: The coordinates need to be converted to doubles first. Also use length instead of length squared. --- src/thingdef/thingdef_codeptr.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/thingdef/thingdef_codeptr.cpp b/src/thingdef/thingdef_codeptr.cpp index 9a8e02468..0949dd6a6 100644 --- a/src/thingdef/thingdef_codeptr.cpp +++ b/src/thingdef/thingdef_codeptr.cpp @@ -277,8 +277,8 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, GetDistance) { fixedvec3 diff = self->Vec3To(target); diff.z += (target->height - self->height) / 2; - const double lengthsquared = TVector3(diff.x, diff.y, diff.z).LengthSquared(); - ret->SetFloat(lengthsquared); + const double length = TVector3(FIXED2DBL(diff.x), FIXED2DBL(diff.y), FIXED2DBL(diff.z)).Length(); + ret->SetFloat(length); } return 1; } From dcfdb503075bcddb92df97a91d32cf10147d98ad Mon Sep 17 00:00:00 2001 From: MajorCooke Date: Wed, 10 Feb 2016 18:04:59 -0600 Subject: [PATCH 3/3] - Fixed: checkz wasn't used. - Use 0 instead of false. --- src/thingdef/thingdef_codeptr.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/thingdef/thingdef_codeptr.cpp b/src/thingdef/thingdef_codeptr.cpp index 0949dd6a6..2eb53c87d 100644 --- a/src/thingdef/thingdef_codeptr.cpp +++ b/src/thingdef/thingdef_codeptr.cpp @@ -271,13 +271,15 @@ DEFINE_ACTION_FUNCTION_PARAMS(AActor, GetDistance) if (!target || target == self) { - ret->SetFloat(false); + ret->SetFloat(0); } else { fixedvec3 diff = self->Vec3To(target); - diff.z += (target->height - self->height) / 2; - const double length = TVector3(FIXED2DBL(diff.x), FIXED2DBL(diff.y), FIXED2DBL(diff.z)).Length(); + if (checkz) + diff.z += (target->height - self->height) / 2; + + const double length = TVector3(FIXED2DBL(diff.x), FIXED2DBL(diff.y), (checkz) ? FIXED2DBL(diff.z) : 0).Length(); ret->SetFloat(length); } return 1;