From c2610fcb60e2d6662ce626ce7931d52a23140663 Mon Sep 17 00:00:00 2001 From: Major Cooke Date: Sat, 9 May 2020 20:05:21 -0500 Subject: [PATCH] Merged https://github.com/coelckers/gzdoom/pull/1084 --- src/g_game.cpp | 4 +-- src/playsim/actor.h | 5 ++++ src/playsim/p_actionfunctions.cpp | 20 +++++++------ src/playsim/p_enemy.cpp | 50 +++++++++++++++++-------------- src/playsim/p_map.cpp | 19 +++++++----- src/playsim/p_mobj.cpp | 16 ++++++---- src/playsim/p_things.cpp | 2 +- 7 files changed, 70 insertions(+), 46 deletions(-) diff --git a/src/g_game.cpp b/src/g_game.cpp index b29f67d6b..354630fe9 100644 --- a/src/g_game.cpp +++ b/src/g_game.cpp @@ -1469,13 +1469,13 @@ double FLevelLocals::PlayersRangeFromSpot (FPlayerStart *spot) if (!playeringame[i] || !players[i].mo || players[i].health <= 0) continue; - distance = players[i].mo->Distance2D(spot->pos.X, spot->pos.Y); + distance = players[i].mo->Distance2DSquared(spot->pos.X, spot->pos.Y); if (distance < closest) closest = distance; } - return closest; + return sqrt(closest); } // [RH] Select the deathmatch spawn spot farthest from everyone. diff --git a/src/playsim/actor.h b/src/playsim/actor.h index bf7eca7cb..7ed69ba62 100644 --- a/src/playsim/actor.h +++ b/src/playsim/actor.h @@ -862,6 +862,11 @@ public: return (Pos().XY() - otherpos).LengthSquared(); } + double Distance2DSquared(double x, double y) const + { + return DVector2(X() - x, Y() - y).LengthSquared(); + } + double Distance2D(AActor *other, bool absolute = false) { DVector2 otherpos = absolute ? other->Pos() : other->PosRelative(this); diff --git a/src/playsim/p_actionfunctions.cpp b/src/playsim/p_actionfunctions.cpp index 705e7f9da..4de719d33 100644 --- a/src/playsim/p_actionfunctions.cpp +++ b/src/playsim/p_actionfunctions.cpp @@ -2241,8 +2241,9 @@ DEFINE_ACTION_FUNCTION(AActor, CheckLOF) { if (range > 0 && !(flags & CLOFF_CHECKPARTIAL)) { - double distance = self->Distance3D(target); - if (distance > range) + double distance_squared = self->Distance3DSquared(target); + + if (distance_squared > (range * range) ) { ACTION_RETURN_BOOL(false); } @@ -2458,13 +2459,14 @@ DEFINE_ACTION_FUNCTION(AActor, CheckIfTargetInLOS) { ACTION_RETURN_BOOL(false); } - double distance = self->Distance3D(target); + double distance_squared = self->Distance3DSquared(target); - if (dist_max && (distance > dist_max)) + if (dist_max && (distance_squared > (dist_max * dist_max) ) ) { ACTION_RETURN_BOOL(false); } - if (dist_close && (distance < dist_close)) + + if (dist_close && (distance_squared < (dist_close * dist_close) ) ) { if (flags & JLOSF_CLOSENOJUMP) { @@ -2549,16 +2551,16 @@ DEFINE_ACTION_FUNCTION(AActor, CheckIfInTargetLOS) ACTION_RETURN_BOOL(false); } - double distance = self->Distance3D(target); + double distance_squared = self->Distance3DSquared(target); - if (dist_max && (distance > dist_max)) + if (dist_max && dist_max >= 0 && (distance_squared > (dist_max * dist_max) ) ) { ACTION_RETURN_BOOL(false); } bool doCheckSight = !(flags & JLOSF_NOSIGHT); - if (dist_close && (distance < dist_close)) + if (dist_close && dist_close >= 0 && (distance_squared < (dist_close * dist_close) ) ) { if (flags & JLOSF_CLOSENOJUMP) { @@ -3413,7 +3415,7 @@ DEFINE_ACTION_FUNCTION(AActor, A_WolfAttack) // Target can dodge if it can see enemy DAngle angle = absangle(self->target->Angles.Yaw, self->target->AngleTo(self)); - bool dodge = (P_CheckSight(self->target, self) && angle < 30. * 256. / 360.); // 30 byteangles ~ 21° + bool dodge = (P_CheckSight(self->target, self) && angle < 30. * 256. / 360.); // 30 byteangles ~ 21� // Distance check is simplistic DVector2 vec = self->Vec2To(self->target); diff --git a/src/playsim/p_enemy.cpp b/src/playsim/p_enemy.cpp index 7089c74e2..5b925d4c1 100644 --- a/src/playsim/p_enemy.cpp +++ b/src/playsim/p_enemy.cpp @@ -140,8 +140,9 @@ static void NoiseMarkSector(sector_t *sec, AActor *soundtarget, bool splash, AAc // [RH] Set this in the actors in the sector instead of the sector itself. for (AActor *actor = sec->thinglist; actor != NULL; actor = actor->snext) { - if (actor != soundtarget && (!splash || !(actor->flags4 & MF4_NOSPLASHALERT)) && - (!maxdist || (actor->Distance2D(emitter) <= maxdist))) + if(actor != soundtarget && (!splash || !(actor->flags4 & MF4_NOSPLASHALERT)) && + (!maxdist || + (maxdist >= 0 && actor->Distance2DSquared(emitter) <= (maxdist * maxdist) ) ) ) { actor->LastHeard = soundtarget; } @@ -456,7 +457,7 @@ int P_Move (AActor *actor) if ((actor->flags6 & MF6_JUMPDOWN) && target && !(target->IsFriend(actor)) && - actor->Distance2D(target) < 144 && + actor->Distance2DSquared(target) < (144 * 144) && pr_dropoff() < 235) { dropoff = 2; @@ -944,16 +945,21 @@ void P_NewChaseDir(AActor * actor) if (actor->flags3 & MF3_AVOIDMELEE) { bool ismeleeattacker = false; - double dist = actor->Distance2D(target); + + double distance_squared = actor->Distance2DSquared(target); if (target->player == NULL) { - ismeleeattacker = (target->MissileState == NULL && dist < (target->meleerange + target->radius)*2); + //in its own variable because otherwise it looks bad + double meele_squared = (target->meleerange + target->radius) * 2 * (target->meleerange + target->radius) * 2; + ismeleeattacker = (target->MissileState == NULL && distance_squared < meele_squared); } else if (target->player->ReadyWeapon != NULL) { // melee range of player weapon is a parameter of the action function and cannot be checked here. // Add a new weapon property? - ismeleeattacker = ((target->player->ReadyWeapon->IntVar(NAME_WeaponFlags) & WIF_MELEEWEAPON) && dist < 192); + ismeleeattacker = ((target->player->ReadyWeapon->IntVar(NAME_WeaponFlags) & WIF_MELEEWEAPON) && + //192 is an unknown constant here + distance_squared < (192 * 192) ); } if (ismeleeattacker) { @@ -1163,12 +1169,12 @@ int P_IsVisible(AActor *lookee, AActor *other, INTBOOL allaround, FLookExParams fov = allaround ? 0. : 180.; } - double dist = lookee->Distance2D (other); - - if (maxdist && dist > maxdist) + double distance_squared = lookee->Distance2DSquared(other); + //square distance here... + if (maxdist && maxdist >= 0 && distance_squared > (maxdist * maxdist) ) return false; // [KS] too far - - if (mindist && dist < mindist) + //...and here because other number are squared + if (mindist && mindist >= 0 && distance_squared < (mindist * mindist) ) return false; // [KS] too close if (fov != 0) @@ -1179,7 +1185,7 @@ int P_IsVisible(AActor *lookee, AActor *other, INTBOOL allaround, FLookExParams { // if real close, react anyway // [KS] but respect minimum distance rules - if (mindist || dist > lookee->meleerange + lookee->radius) + if (mindist || distance_squared > ( (lookee->meleerange + lookee->radius) * (lookee->meleerange + lookee->radius) ) ) return false; // outside of fov } } @@ -1214,7 +1220,7 @@ int P_LookForMonsters (AActor *actor) { // Not a valid monster continue; } - if (mo->Distance2D (actor) > MONS_LOOK_RANGE) + if (mo->Distance2DSquared(actor) > (MONS_LOOK_RANGE * MONS_LOOK_RANGE) ) { // Out of range continue; } @@ -1721,7 +1727,7 @@ int P_LookForPlayers (AActor *actor, INTBOOL allaround, FLookExParams *params) if ((player->mo->flags & MF_SHADOW && !(actor->Level->i_compatflags & COMPATF_INVISIBILITY)) || player->mo->flags3 & MF3_GHOST) { - if (player->mo->Distance2D (actor) > 128 && player->mo->Vel.XY().LengthSquared() < 5*5) + if (player->mo->Distance2DSquared(actor) > (128 * 128) && player->mo->Vel.XY().LengthSquared() < (5 * 5) ) { // Player is sneaking - can't detect continue; } @@ -1882,7 +1888,7 @@ DEFINE_ACTION_FUNCTION(AActor, A_LookEx) PARAM_STATE (seestate) AActor *targ = NULL; // Shuts up gcc - double dist; + double distance_squared;//Not used anywhere, except distance checking if (fov == 0) fov = 180.; FLookExParams params = { fov, minseedist, maxseedist, maxheardist, flags, seestate }; @@ -1923,10 +1929,10 @@ DEFINE_ACTION_FUNCTION(AActor, A_LookEx) } else { - dist = self->Distance2D (targ); + distance_squared = self->Distance2DSquared(targ); // [KS] If the target is too far away, don't respond to the sound. - if (maxheardist && dist > maxheardist) + if (maxheardist && maxheardist > 0 && distance_squared > (maxheardist * maxheardist) ) { targ = NULL; self->LastHeard = nullptr; @@ -1994,10 +2000,10 @@ DEFINE_ACTION_FUNCTION(AActor, A_LookEx) { if (self->flags & MF_AMBUSH) { - dist = self->Distance2D (self->target); + distance_squared = self->Distance2D (self->target); if (P_CheckSight (self, self->target, SF_SEEPASTBLOCKEVERYTHING) && - (!minseedist || dist > minseedist) && - (!maxseedist || dist < maxseedist)) + (!minseedist || (minseedist >= 0 && distance_squared > (minseedist * minseedist) ) ) && + (!maxseedist || (maxseedist >= 0 && distance_squared < (maxseedist * maxseedist) ) ) ) { goto seeyou; } @@ -2430,8 +2436,8 @@ void A_DoChase (AActor *actor, bool fastchase, FState *meleestate, FState *missi { actor->FastChaseStrafeCount = 0; actor->Vel.X = actor->Vel.Y = 0; - double dist = actor->Distance2D (actor->target); - if (dist < CLASS_BOSS_STRAFE_RANGE) + double distance_squared = actor->Distance2DSquared(actor->target); + if (distance_squared < (CLASS_BOSS_STRAFE_RANGE * CLASS_BOSS_STRAFE_RANGE) ) { if (pr_chase() < 100) { diff --git a/src/playsim/p_map.cpp b/src/playsim/p_map.cpp index 8e7436a82..6ca26b4e5 100644 --- a/src/playsim/p_map.cpp +++ b/src/playsim/p_map.cpp @@ -1355,10 +1355,10 @@ bool PIT_CheckThing(FMultiBlockThingsIterator &it, FMultiBlockThingsIterator::Ch fabs(thing->Y() - oldpos.Y) < (thing->radius + tm.thing->radius)) { - double newdist = thing->Distance2D(cres.Position.X, cres.Position.Y); - double olddist = thing->Distance2D(oldpos.X, oldpos.Y); + double newdist_squared = thing->Distance2DSquared(cres.Position.X, cres.Position.Y); + double olddist_squared = thing->Distance2DSquared(oldpos.X, oldpos.Y); - if (newdist > olddist) + if (newdist_squared > olddist_squared) { // unblock only if there's already a vertical overlap (or both actors are flagged not to overlap) unblocking = (tm.thing->Top() > thing->Z() && tm.thing->Z() < topz) || (tm.thing->flags3 & thing->flags3 & MF3_DONTOVERLAP); @@ -2066,15 +2066,20 @@ void P_FakeZMovement(AActor *mo) { // float down towards target if too close if (!(mo->flags & MF_SKULLFLY) && !(mo->flags & MF_INFLOAT)) { - double dist = mo->Distance2D(mo->target); + double dist_squared = mo->Distance2DSquared(mo->target); + //still need delta intact, because it can be negative + //and square of a real number always positive double delta = mo->target->Center() - mo->Z(); - if (delta < 0 && dist < -(delta * 3)) + //why it multiplies by 3 in original function? + double delta_squared = delta * delta * 3 * 3; + if (delta < 0 && dist_squared < -(delta_squared) ) mo->AddZ(-mo->FloatSpeed); - else if (delta > 0 && dist < (delta * 3)) + + else if (delta > 0 && dist_squared < (delta_squared) ) mo->AddZ(mo->FloatSpeed); } } - if (mo->player && mo->flags&MF_NOGRAVITY && (mo->Z() > mo->floorz) && !mo->IsNoClip2()) + if (mo->player && mo->flags & MF_NOGRAVITY && (mo->Z() > mo->floorz) && !mo->IsNoClip2()) { mo->AddZ(DAngle(4.5 * mo->Level->maptime).Sin()); } diff --git a/src/playsim/p_mobj.cpp b/src/playsim/p_mobj.cpp index a55f653a2..944fe3223 100644 --- a/src/playsim/p_mobj.cpp +++ b/src/playsim/p_mobj.cpp @@ -2369,7 +2369,7 @@ void P_MonsterFallingDamage (AActor *mo) void P_ZMovement (AActor *mo, double oldfloorz) { - double dist; + double distance_squared;//not used anywhere except height adjusting block double delta; double oldz = mo->Z(); double grav = mo->GetGravity(); @@ -2481,11 +2481,17 @@ void P_ZMovement (AActor *mo, double oldfloorz) { // float down towards target if too close if (!(mo->flags & (MF_SKULLFLY | MF_INFLOAT))) { - dist = mo->Distance2D (mo->target); - delta = (mo->target->Center()) - mo->Z(); - if (delta < 0 && dist < -(delta*3)) + //same code block from P_FakeZMovement/vice versa??? + double dist_squared = mo->Distance2DSquared(mo->target); + //still need delta intact, because it can be negative + //and square of a real number always positive + double delta = mo->target->Center() - mo->Z(); + //why it multiplies by 3 in original function? + double delta_squared = delta * delta * 3 * 3; + if (delta < 0 && dist_squared < -(delta_squared) ) mo->AddZ(-mo->FloatSpeed); - else if (delta > 0 && dist < (delta*3)) + + else if (delta > 0 && dist_squared < (delta_squared) ) mo->AddZ(mo->FloatSpeed); } } diff --git a/src/playsim/p_things.cpp b/src/playsim/p_things.cpp index 7e000a74a..ba0cce969 100644 --- a/src/playsim/p_things.cpp +++ b/src/playsim/p_things.cpp @@ -613,7 +613,7 @@ int P_Thing_CheckProximity(FLevelLocals *Level, AActor *self, PClass *classname, // [MC]Make sure it's in range and respect the desire for Z or not. The function forces it to use // Z later for ensuring CLOSEST and FARTHEST flags are respected perfectly. // Ripped from sphere checking in A_RadiusGive (along with a number of things). - if ((ref->Distance2D(mo) < distance && + if ((ref->Distance2DSquared(mo) < (distance * distance) && ((flags & CPXF_NOZ) || ((ref->Z() > mo->Z() && ref->Z() - mo->Top() < distance) || (ref->Z() <= mo->Z() && mo->Z() - ref->Top() < distance)))))