- converted all dist() calls to vector math and deleted the utilities

This commit is contained in:
Christoph Oelckers 2022-09-13 20:01:17 +02:00
parent 2bcc552ce0
commit 0432930e5b
8 changed files with 30 additions and 61 deletions

View file

@ -62,22 +62,3 @@ double fFindDistance2D(int x, int y)
return (x - (x * (1. / 32.)) - (x * (1. / 128.)) + (t * (1. / 4.)) + (t * (1. / 64.)));
}
int FindDistance3D(int x, int y, int z)
{
x= abs(x); /* absolute values */
y= abs(y);
z= abs(z >> 4);
if (x<y)
std::swap(x,y);
if (x<z)
std::swap(x,z);
int t = y + z;
return (x - (x>>4) + (t>>2) + (t>>3));
}

View file

@ -2,14 +2,8 @@
int FindDistance2D(int x, int y);
double fFindDistance2D(int x, int y);
int FindDistance3D(int x, int y, int z);
inline int FindDistance3D(const vec3_t& vec)
{
return FindDistance3D(vec.X, vec.Y, vec.Z);
}
inline int FindDistance2D(const vec2_t& vec)
{
return FindDistance2D(vec.X, vec.Y);
}
}

View file

@ -525,7 +525,6 @@ void moveplayers(void)
void movefx(void)
{
int p;
int x, ht;
DukeStatIterator iti(STAT_FX);
while (auto act = iti.Next())
@ -550,8 +549,8 @@ void movefx(void)
break;
case MUSICANDSFX:
ht = act->spr.hitag;
{
double maxdist = act->spr.hitag * inttoworld;
if (act->temp_data[1] != (int)SoundEnabled())
{
@ -561,13 +560,13 @@ void movefx(void)
if (act->spr.lotag >= 1000 && act->spr.lotag < 2000)
{
x = ldist(ps[screenpeek].GetActor(), act);
if (x < ht && act->temp_data[0] == 0)
double dist = (ps[screenpeek].GetActor()->spr.pos.XY() - act->spr.pos.XY()).Length();
if (dist < maxdist && act->temp_data[0] == 0)
{
FX_SetReverb(act->spr.lotag - 1100);
act->temp_data[0] = 1;
}
if (x >= ht && act->temp_data[0] == 1)
if (dist >= maxdist && act->temp_data[0] == 1)
{
FX_SetReverb(0);
FX_SetReverbDelay(0);
@ -579,15 +578,15 @@ void movefx(void)
int flags = S_GetUserFlags(act->spr.lotag);
if (flags & SF_MSFX)
{
int distance = dist(ps[screenpeek].GetActor(), act);
double distance = (ps[screenpeek].GetActor()->spr.pos - act->spr.pos).Length();
if (distance < ht && act->temp_data[0] == 0)
if (distance < maxdist && act->temp_data[0] == 0)
{
// Start playing an ambience sound.
S_PlayActorSound(act->spr.lotag, act, CHAN_AUTO, CHANF_LOOP);
act->temp_data[0] = 1; // AMBIENT_SFX_PLAYING
}
else if (distance >= ht && act->temp_data[0] == 1)
else if (distance >= maxdist && act->temp_data[0] == 1)
{
// Stop playing ambience sound because we're out of its range.
S_StopSound(act->spr.lotag, act);
@ -606,6 +605,7 @@ void movefx(void)
}
}
break;
}
}
}
}

View file

@ -259,6 +259,7 @@ bool ifsquished(DDukeActor* actor, int p)
void hitradius_d(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int hp4)
{
double radius = r * inttoworld;
static const uint8_t statlist[] = { STAT_DEFAULT, STAT_ACTOR, STAT_STANDABLE, STAT_PLAYER, STAT_FALLER, STAT_ZOMBIEACTOR, STAT_MISC };
if(actor->spr.picnum != SHRINKSPARK && !(actor->spr.picnum == RPG && actor->spr.xrepeat < 11))
@ -326,7 +327,7 @@ void hitradius_d(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int h
if (x == 0 || x >= 5 || actorflag(act2, SFLAG_HITRADIUS_FLAG1))
{
if (actor->spr.picnum != SHRINKSPARK || (act2->spr.cstat & CSTAT_SPRITE_BLOCK_ALL))
if (dist(actor, act2) < r)
if ((actor->spr.pos - act2->spr.pos).Length() < radius)
{
if (badguy(act2) && !cansee(act2->spr.pos.plusZ(q), act2->sector(), actor->spr.pos.plusZ(q), actor->sector()))
continue;
@ -345,10 +346,10 @@ void hitradius_d(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int h
}
if (act2->spr.picnum == APLAYER) act2->spr.pos.Z -= gs.playerheight;
int d = dist(actor, act2);
double dist = (actor->spr.pos - act2->spr.pos).Length();
if (act2->spr.picnum == APLAYER) act2->spr.pos.Z += gs.playerheight;
if (d < r && cansee(act2->spr.pos.plusZ(-8), act2->sector(), actor->spr.pos.plusZ(-12), actor->sector()))
if (dist < radius && cansee(act2->spr.pos.plusZ(-8), act2->sector(), actor->spr.pos.plusZ(-12), actor->sector()))
{
act2->hitang = VecToAngle(act2->spr.pos - actor->spr.pos);
@ -377,17 +378,17 @@ void hitradius_d(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int h
if (actor->spr.picnum != SHRINKSPARK && (!isWorldTour() || actor->spr.picnum != LAVAPOOL))
{
if (d < r / 3)
if (dist < radius / 3)
{
if (hp4 == hp3) hp4++;
act2->hitextra = hp3 + (krand() % (hp4 - hp3));
}
else if (d < 2 * r / 3)
else if (dist < 2 * radius / 3)
{
if (hp3 == hp2) hp3++;
act2->hitextra = hp2 + (krand() % (hp3 - hp2));
}
else if (d < r)
else if (dist < radius)
{
if (hp2 == hp1) hp2++;
act2->hitextra = hp1 + (krand() % (hp2 - hp1));
@ -1043,14 +1044,14 @@ static void movefireext(DDukeActor* actor)
static void moveviewscreen(DDukeActor* actor)
{
const int VIEWSCR_DIST = 8192; // was originally 2048, was increased to this by EDuke32 and RedNukem.
const double VIEWSCR_DIST = 1024; // was originally 2048, was increased to 8192 by EDuke32 and RedNukem, but with high resolutions the resulting 512 map units are still too low.
if (actor->spr.xrepeat == 0) deletesprite(actor);
else
{
int x;
int p = findplayer(actor, &x);
double a;
int p = findplayer(actor, &a);
x = dist(actor, ps[p].GetActor()); // the result from findplayer is not really useful.
double x = (actor->spr.pos - ps[p].GetActor()->spr.pos).Length(); // the result from findplayer is not really useful.
if (x >= VIEWSCR_DIST && camsprite == actor)
{
camsprite = nullptr;

View file

@ -217,6 +217,7 @@ void addweapon_r(player_struct* p, int weapon)
void hitradius_r(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int hp4)
{
double radius = r * inttoworld;
static const uint8_t statlist[] = { STAT_DEFAULT, STAT_ACTOR, STAT_STANDABLE, STAT_PLAYER, STAT_FALLER, STAT_ZOMBIEACTOR, STAT_MISC };
if (actor->spr.xrepeat >= 11 || !(actor->spr.picnum == RPG || ((isRRRA()) && actor->spr.picnum == RPG2)))
@ -271,7 +272,7 @@ void hitradius_r(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int h
if (x == 0 || x >= 5 || actorflag(act2, SFLAG_HITRADIUS_FLAG1))
{
if (act2->spr.cstat & CSTAT_SPRITE_BLOCK_ALL)
if (dist(actor, act2) < r)
if ((actor->spr.pos - act2->spr.pos).Length() < radius)
{
if (badguy(act2) && !cansee(act2->spr.pos.plusZ(q), act2->sector(), actor->spr.pos.plusZ(q), actor->sector()))
continue;
@ -291,10 +292,10 @@ void hitradius_r(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int h
}
if (act2->spr.picnum == APLAYER) act2->spr.pos.Z -= gs.playerheight;
int d = dist(actor, act2);
double dist = (actor->spr.pos - act2->spr.pos).Length();
if (act2->spr.picnum == APLAYER) act2->spr.pos.Z += gs.playerheight;
if (d < r && cansee(act2->spr.pos.plusZ(-8), act2->sector(), actor->spr.pos.plusZ(-12), actor->sector()))
if (dist < radius && cansee(act2->spr.pos.plusZ(-8), act2->sector(), actor->spr.pos.plusZ(-12), actor->sector()))
{
if ((isRRRA()) && act2->spr.picnum == MINION && act2->spr.pal == 19)
{
@ -310,17 +311,17 @@ void hitradius_r(DDukeActor* actor, int r, int hp1, int hp2, int hp3, int h
else
act2->attackertype = RADIUSEXPLOSION;
if (d < r / 3)
if (dist < radius / 3)
{
if (hp4 == hp3) hp4++;
act2->hitextra = hp3 + (krand() % (hp4 - hp3));
}
else if (d < 2 * r / 3)
else if (dist < 2 * radius / 3)
{
if (hp3 == hp2) hp3++;
act2->hitextra = hp2 + (krand() % (hp3 - hp2));
}
else if (d < r)
else if (dist < radius)
{
if (hp2 == hp1) hp2++;
act2->hitextra = hp1 + (krand() % (hp2 - hp1));

View file

@ -109,14 +109,6 @@ inline int ldist(const DDukeActor* s1, const tspritetype* s2)
vy = s1->int_pos().Y - s2->int_pos().Y;
return(FindDistance2D(vx, vy) + 1);
}
inline int dist(DDukeActor* s1, DDukeActor* s2)
{
int vx, vy, vz;
vx = s1->int_pos().X - s2->int_pos().X;
vy = s1->int_pos().Y - s2->int_pos().Y;
vz = s1->int_pos().Z - s2->int_pos().Z;
return(FindDistance3D(vx, vy, vz));
}
inline bool isIn(int value, int first)
{

View file

@ -506,7 +506,7 @@ static void shootweapon(DDukeActor *actor, int p, int sx, int sy, int sz, int sa
while (auto l = it.Next())
{
if (l->spr.picnum == BULLETHOLE)
if (dist(l, spark) < (12 + (krand() & 7)))
if ((l->spr.pos - spark->spr.pos).Length() < 0.75 + krandf(0.5))
goto SKIPBULLETHOLE;
}
auto hole = spawn(spark, BULLETHOLE);

View file

@ -403,7 +403,7 @@ static void shootweapon(DDukeActor* actor, int p, int sx, int sy, int sz, int sa
while (auto l = it.Next())
{
if (l->spr.picnum == BULLETHOLE)
if (dist(l, spark) < (12 + (krand() & 7)))
if ((l->spr.pos - spark->spr.pos).Length() < 0.75 + krandf(0.5))
goto SKIPBULLETHOLE;
}
auto hole = spawn(spark, BULLETHOLE);