Factor out [l]dist() implementations into static inline functions in common.h.

And use these in jmact/mathutil.c's FindDistance2D()/FindDistance3D().
The main use is to allow passing dx/dy instead of sprite positions; the code
that actually uses this is not committed.

git-svn-id: https://svn.eduke32.com/eduke32@4579 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2014-08-31 11:15:17 +00:00
parent 1eaf60b222
commit 77dc743f09
3 changed files with 45 additions and 42 deletions

View file

@ -7,8 +7,12 @@
#ifndef EDUKE32_COMMON_H_
#define EDUKE32_COMMON_H_
#include <stdint.h>
#include "scriptfile.h"
#include "cache1d.h"
#include "pragmas.h" // klabs
#include "build.h"
#ifdef EXTERNC
extern "C" {
@ -82,6 +86,42 @@ int32_t fnlist_getnames(fnlist_t *fnl, const char *dirname, const char *pattern,
char *dup_filename(const char *fn);
int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char *ext);
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
// in jmact/mathutil.c: "Ken's reverse-engineering job".
// Note that jmact/mathutil.c contains practically the same code, but where the
// individual x/y(/z) distances are passed instead.
static inline int32_t sepldist(const int32_t dx, const int32_t dy)
{
int32_t x = klabs(dx);
int32_t y = klabs(dy);
if (x < y)
swaplong(&x, &y);
{
int32_t t = y + (y>>1);
return x - (x>>5) - (x>>7) + (t>>2) + (t>>6);
}
}
// dz: in Build coordinates
static inline int32_t sepdist(int32_t dx, int32_t dy, int32_t dz)
{
int32_t x = klabs(dx);
int32_t y = klabs(dy);
int32_t z = klabs(dz>>4);
if (x < y)
swaplong(&x, &y);
if (x < z)
swaplong(&x, &z);
{
int32_t t = y + z;
return x - (x>>4) + (t>>2) + (t>>3);
}
}
int32_t ldist(const spritetype *s1, const spritetype *s2);
int32_t dist(const spritetype *s1, const spritetype *s2);

View file

@ -197,36 +197,14 @@ int32_t maybe_append_ext(char *wbuf, int32_t wbufsiz, const char *fn, const char
}
// Approximations to 2D and 3D Euclidean distances. Initial EDuke32 SVN import says
// in jmact/mathutil.c: "Ken's reverse-engineering job".
// Note that jmact/mathutil.c contains practically the same code, but where the
// individual x/y(/z) distances are passed instead.
int32_t ldist(const spritetype *s1, const spritetype *s2)
{
int32_t x = klabs(s1->x-s2->x);
int32_t y = klabs(s1->y-s2->y);
if (x<y) swaplong(&x,&y);
{
int32_t t = y + (y>>1);
return (x - (x>>5) - (x>>7) + (t>>2) + (t>>6));
}
return sepldist(s1->x-s2->x, s1->y-s2->y);
}
int32_t dist(const spritetype *s1, const spritetype *s2)
{
int32_t x = klabs(s1->x-s2->x);
int32_t y = klabs(s1->y-s2->y);
int32_t z = klabs((s1->z-s2->z)>>4);
if (x<y) swaplong(&x,&y);
if (x<z) swaplong(&x,&z);
{
int32_t t = y + z;
return (x - (x>>4) + (t>>2) + (t>>3));
}
return sepdist(s1->x-s2->x, s1->y-s2->y, s1->z-s2->z);
}

View file

@ -31,29 +31,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
//-------------------------------------------------------------------------
#include <limits.h>
#include "compat.h"
#include "pragmas.h"
// I wonder if it's faster to use Ken's functions here...
#include "common.h"
int32_t FindDistance2D(int32_t x, int32_t y)
{
if ((x=klabs(x)) < (y=klabs(y))) swaplong(&x,&y);
{
int32_t t = y + (y>>1);
return (x - (x>>5) - (x>>7) + (t>>2) + (t>>6));
}
return sepldist(x, y);
}
int32_t FindDistance3D(int32_t x, int32_t y, int32_t z)
{
if ((x=klabs(x)) < (y=klabs(y))) swaplong(&x,&y);
if (x < (z=klabs(z))) swaplong(&x,&z);
{
int32_t t = y + z;
return (x - (x>>4) + (t>>2) + (t>>3));
}
return sepdist(x, y, z);
}