Addition of newer distance calculation methods.

This commit is contained in:
MobiusDevelopment 2021-03-28 09:17:49 +00:00
parent 2c295b9b24
commit d3c54bde15
23 changed files with 220 additions and 210 deletions

View File

@ -521,7 +521,7 @@ public class AttackableAI extends CreatureAI
_actor.setWalking(); _actor.setWalking();
} }
if (_actor.getPlanDistanceSq(((MinionInstance) _actor).getLeader()) > (offset * offset)) if (_actor.calculateDistanceSq2D(((MinionInstance) _actor).getLeader()) > (offset * offset))
{ {
int x1; int x1;
int y1; int y1;
@ -556,7 +556,7 @@ public class AttackableAI extends CreatureAI
z1 = p[2]; z1 = p[2];
// Calculate the distance between the current position of the Creature and the target (x,y) // Calculate the distance between the current position of the Creature and the target (x,y)
final double distance2 = _actor.getPlanDistanceSq(x1, y1); final double distance2 = _actor.calculateDistanceSq2D(x1, y1, z1);
if (distance2 > (Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE)) if (distance2 > (Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE))
{ {
npc.setReturningToSpawnPoint(true); npc.setReturningToSpawnPoint(true);
@ -810,7 +810,7 @@ public class AttackableAI extends CreatureAI
if ((weapon != null) && (weapon.getItemType() == WeaponType.BOW)) if ((weapon != null) && (weapon.getItemType() == WeaponType.BOW))
{ {
// Micht: kepping this one otherwise we should do 2 sqrt // Micht: kepping this one otherwise we should do 2 sqrt
final double distance2 = _actor.getPlanDistanceSq(originalAttackTarget.getX(), originalAttackTarget.getY()); final double distance2 = _actor.calculateDistanceSq2D(originalAttackTarget);
if (Math.sqrt(distance2) <= (60 + combinedCollision)) if (Math.sqrt(distance2) <= (60 + combinedCollision))
{ {
final int chance = 5; final int chance = 5;
@ -861,7 +861,7 @@ public class AttackableAI extends CreatureAI
setAttackTarget(hated); setAttackTarget(hated);
} }
// We should calculate new distance cuz mob can have changed the target // We should calculate new distance cuz mob can have changed the target
dist2 = _actor.getPlanDistanceSq(hated.getX(), hated.getY()); dist2 = _actor.calculateDistanceSq2D(hated);
if (hated.isMoving()) if (hated.isMoving())
{ {
range += 50; range += 50;

View File

@ -189,7 +189,7 @@ public class ControllableMobAI extends AttackableAI
ctrlAi.forceAttack(_actor); ctrlAi.forceAttack(_actor);
final Skill[] skills = _actor.getAllSkills(); final Skill[] skills = _actor.getAllSkills();
final double dist2 = _actor.getPlanDistanceSq(target.getX(), target.getY()); final double dist2 = _actor.calculateDistanceSq2D(target);
final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius(); final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius();
int maxRange = range; int maxRange = range;
if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20)))) if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
@ -226,7 +226,7 @@ public class ControllableMobAI extends AttackableAI
_actor.setTarget(getForcedTarget()); _actor.setTarget(getForcedTarget());
final Skill[] skills = _actor.getAllSkills(); final Skill[] skills = _actor.getAllSkills();
final double dist2 = _actor.getPlanDistanceSq(getForcedTarget().getX(), getForcedTarget().getY()); final double dist2 = _actor.calculateDistanceSq2D(getForcedTarget());
final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius(); final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius();
int maxRange = range; int maxRange = range;
if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20)))) if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
@ -293,7 +293,7 @@ public class ControllableMobAI extends AttackableAI
_actor.setTarget(getAttackTarget()); _actor.setTarget(getAttackTarget());
final Skill[] skills = _actor.getAllSkills(); final Skill[] skills = _actor.getAllSkills();
final double dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY()); final double dist2 = _actor.calculateDistanceSq2D(getAttackTarget());
final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius(); final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
int maxRange = range; int maxRange = range;
if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20)))) if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))

View File

@ -596,7 +596,7 @@ public class FortSiegeGuardAI extends CreatureAI implements Runnable
{ {
_actor.setTarget(attackTarget); _actor.setTarget(attackTarget);
skills = _actor.getAllSkills(); skills = _actor.getAllSkills();
dist2 = _actor.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY()); dist2 = _actor.calculateDistanceSq2D(attackTarget);
range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius(); range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
if (attackTarget.isMoving()) if (attackTarget.isMoving())
{ {

View File

@ -317,7 +317,7 @@ public class SiegeGuardAI extends CreatureAI implements Runnable
{ {
_actor.setTarget(attackTarget); _actor.setTarget(attackTarget);
skills = _actor.getAllSkills(); skills = _actor.getAllSkills();
dist2 = _actor.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY()); dist2 = _actor.calculateDistanceSq2D(attackTarget);
range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius(); range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
} }
catch (NullPointerException e) catch (NullPointerException e)

View File

@ -475,6 +475,94 @@ public abstract class WorldObject
return false; return false;
} }
/**
* Calculates 2D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistance2D(int x, int y, int z)
{
return Math.sqrt(Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2));
}
/**
* Calculates the 2D distance between this WorldObject and given WorldObject.
* @param object the target object
* @return distance between object and given object.
*/
public double calculateDistance2D(WorldObject object)
{
return calculateDistance2D(object.getX(), object.getY(), object.getZ());
}
/**
* Calculates the 3D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistance3D(int x, int y, int z)
{
return Math.sqrt(Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + Math.pow(z - getZ(), 2));
}
/**
* Calculates 3D distance between this WorldObject and given location.
* @param loc the location object
* @return distance between object and given location.
*/
public double calculateDistance3D(Location loc)
{
return calculateDistance3D(loc.getX(), loc.getY(), loc.getZ());
}
/**
* Calculates the non squared 2D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistanceSq2D(int x, int y, int z)
{
return Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2);
}
/**
* Calculates the non squared 2D distance between this WorldObject and given WorldObject.
* @param object the target object
* @return distance between object and given object.
*/
public double calculateDistanceSq2D(WorldObject object)
{
return calculateDistanceSq2D(object.getX(), object.getY(), object.getZ());
}
/**
* Calculates the non squared 3D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistanceSq3D(int x, int y, int z)
{
return Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + Math.pow(z - getZ(), 2);
}
/**
* Calculates the non squared 3D distance between this WorldObject and given WorldObject.
* @param object the target object
* @return distance between object and given object.
*/
public double calculateDistanceSq3D(WorldObject object)
{
return calculateDistanceSq3D(object.getX(), object.getY(), object.getZ());
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -617,7 +617,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder
} }
// This function is called too often from movement code. // This function is called too often from movement code.
if (!force && (getDistanceSq(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < (isNpc() && !isInCombat() ? Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE : 10000))) if (!force && (calculateDistanceSq3D(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < (isNpc() && !isInCombat() ? Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE : 10000)))
{ {
return; return;
} }
@ -5797,88 +5797,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder
return result; return result;
} }
/**
* Return the distance between the current position of the Creature and the target (x,y).
* @param x X position of the target
* @param y Y position of the target
* @return the plan distance
* @deprecated use getPlanDistanceSq(int x, int y, int z)
*/
@Deprecated
public double getDistance(int x, int y)
{
final double dx = x - getX();
final double dy = y - getY();
return Math.sqrt((dx * dx) + (dy * dy));
}
/**
* Return the distance between the current position of the Creature and the target (x,y).
* @param x X position of the target
* @param y Y position of the target
* @param z the z
* @return the plan distance
* @deprecated use getPlanDistanceSq(int x, int y, int z)
*/
@Deprecated
public double getDistance(int x, int y, int z)
{
final double dx = x - getX();
final double dy = y - getY();
final double dz = z - getZ();
return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz));
}
/**
* Return the squared distance between the current position of the Creature and the given object.
* @param object WorldObject
* @return the squared distance
*/
public double getDistanceSq(WorldObject object)
{
return getDistanceSq(object.getX(), object.getY(), object.getZ());
}
/**
* Return the squared distance between the current position of the Creature and the given x, y, z.
* @param x X position of the target
* @param y Y position of the target
* @param z Z position of the target
* @return the squared distance
*/
public double getDistanceSq(int x, int y, int z)
{
final double dx = x - getX();
final double dy = y - getY();
final double dz = z - getZ();
return (dx * dx) + (dy * dy) + (dz * dz);
}
/**
* Return the squared plan distance between the current position of the Creature and the given object.<br>
* (check only x and y, not z)
* @param object WorldObject
* @return the squared plan distance
*/
public double getPlanDistanceSq(WorldObject object)
{
return getPlanDistanceSq(object.getX(), object.getY());
}
/**
* Return the squared plan distance between the current position of the Creature and the given x, y, z.<br>
* (check only x and y, not z)
* @param x X position of the target
* @param y Y position of the target
* @return the squared plan distance
*/
public double getPlanDistanceSq(int x, int y)
{
final double dx = x - getX();
final double dy = y - getY();
return (dx * dx) + (dy * dy);
}
/** /**
* Check if this object is inside the given radius around the given object. Warning: doesn't cover collision radius! * Check if this object is inside the given radius around the given object. Warning: doesn't cover collision radius!
* @param object the target * @param object the target

View File

@ -1727,7 +1727,7 @@ public class PlayerInstance extends Playable
} }
// This function is called too often from movement code. // This function is called too often from movement code.
if (!force && (getDistanceSq(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < 10000)) if (!force && (calculateDistanceSq3D(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < 10000))
{ {
return; return;
} }

View File

@ -1166,7 +1166,6 @@ public class Formulas
return (creature.calcStat(Stat.REGENERATE_CP_RATE, init, null, null) * cpRegenMultiplier) + cpRegenBonus; return (creature.calcStat(Stat.REGENERATE_CP_RATE, init, null, null) * cpRegenMultiplier) + cpRegenBonus;
} }
@SuppressWarnings("deprecation")
public static final double calcFestivalRegenModifier(PlayerInstance player) public static final double calcFestivalRegenModifier(PlayerInstance player)
{ {
final int[] festivalInfo = SevenSignsFestival.getInstance().getFestivalForPlayer(player); final int[] festivalInfo = SevenSignsFestival.getInstance().getFestivalForPlayer(player);
@ -1191,7 +1190,7 @@ public class Formulas
} }
// Check the distance between the player and the player spawn point, in the center of the arena. // Check the distance between the player and the player spawn point, in the center of the arena.
final double distToCenter = player.getDistance(festivalCenter[0], festivalCenter[1]); final double distToCenter = player.calculateDistance2D(festivalCenter[0], festivalCenter[1], 0);
return 1.0 - (distToCenter * 0.0005); // Maximum Decreased Regen of ~ -65%; return 1.0 - (distToCenter * 0.0005); // Maximum Decreased Regen of ~ -65%;
} }

View File

@ -77,7 +77,7 @@ public class RequestGetItemFromPet extends GameClientPacket
return; return;
} }
if (player.getDistanceSq(pet) > 40000) // 200*200 if (player.calculateDistanceSq3D(pet) > 40000) // 200*200
{ {
player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE); player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE);
sendPacket(ActionFailed.STATIC_PACKET); sendPacket(ActionFailed.STATIC_PACKET);

View File

@ -187,7 +187,7 @@ public class TradeRequest extends GameClientPacket
return; return;
} }
if (player.getDistanceSq(partner) > 22500) // 150 if (player.calculateDistanceSq3D(partner) > 22500) // 150
{ {
player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE); player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE);
player.sendPacket(ActionFailed.STATIC_PACKET); player.sendPacket(ActionFailed.STATIC_PACKET);

View File

@ -165,7 +165,7 @@ public class Broadcast
for (PlayerInstance player : creature.getKnownList().getKnownPlayers().values()) for (PlayerInstance player : creature.getKnownList().getKnownPlayers().values())
{ {
if ((player != null) && (creature.getDistanceSq(player) <= radiusSq)) if ((player != null) && (creature.calculateDistanceSq3D(player) <= radiusSq))
{ {
player.sendPacket(mov); player.sendPacket(mov);
} }

View File

@ -1418,7 +1418,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_vanHalter) <= DISTANCE) if (pc.calculateDistanceSq2D(_vanHalter) <= DISTANCE)
{ {
_vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 50, 90, 0, 0, 15000)); _vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 50, 90, 0, 0, 15000));
} }
@ -1437,7 +1437,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(5)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(5)) <= DISTANCE)
{ {
_cameraMarker.get(5).broadcastPacket(new SpecialCamera(_cameraMarker.get(5).getObjectId(), 1842, 100, -3, 0, 15000)); _cameraMarker.get(5).broadcastPacket(new SpecialCamera(_cameraMarker.get(5).getObjectId(), 1842, 100, -3, 0, 15000));
} }
@ -1456,7 +1456,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(5)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(5)) <= DISTANCE)
{ {
_cameraMarker.get(5).broadcastPacket(new SpecialCamera(_cameraMarker.get(5).getObjectId(), 1861, 97, -10, 1500, 15000)); _cameraMarker.get(5).broadcastPacket(new SpecialCamera(_cameraMarker.get(5).getObjectId(), 1861, 97, -10, 1500, 15000));
} }
@ -1475,7 +1475,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(4)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(4)) <= DISTANCE)
{ {
_cameraMarker.get(4).broadcastPacket(new SpecialCamera(_cameraMarker.get(4).getObjectId(), 1876, 97, 12, 0, 15000)); _cameraMarker.get(4).broadcastPacket(new SpecialCamera(_cameraMarker.get(4).getObjectId(), 1876, 97, 12, 0, 15000));
} }
@ -1494,7 +1494,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(4)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(4)) <= DISTANCE)
{ {
_cameraMarker.get(4).broadcastPacket(new SpecialCamera(_cameraMarker.get(4).getObjectId(), 1839, 94, 0, 1500, 15000)); _cameraMarker.get(4).broadcastPacket(new SpecialCamera(_cameraMarker.get(4).getObjectId(), 1839, 94, 0, 1500, 15000));
} }
@ -1513,7 +1513,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(3)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(3)) <= DISTANCE)
{ {
_cameraMarker.get(3).broadcastPacket(new SpecialCamera(_cameraMarker.get(3).getObjectId(), 1872, 94, 15, 0, 15000)); _cameraMarker.get(3).broadcastPacket(new SpecialCamera(_cameraMarker.get(3).getObjectId(), 1872, 94, 15, 0, 15000));
} }
@ -1532,7 +1532,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(3)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(3)) <= DISTANCE)
{ {
_cameraMarker.get(3).broadcastPacket(new SpecialCamera(_cameraMarker.get(3).getObjectId(), 1839, 92, 0, 1500, 15000)); _cameraMarker.get(3).broadcastPacket(new SpecialCamera(_cameraMarker.get(3).getObjectId(), 1839, 92, 0, 1500, 15000));
} }
@ -1551,7 +1551,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(2)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(2)) <= DISTANCE)
{ {
_cameraMarker.get(2).broadcastPacket(new SpecialCamera(_cameraMarker.get(2).getObjectId(), 1872, 92, 15, 0, 15000)); _cameraMarker.get(2).broadcastPacket(new SpecialCamera(_cameraMarker.get(2).getObjectId(), 1872, 92, 15, 0, 15000));
} }
@ -1570,7 +1570,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(2)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(2)) <= DISTANCE)
{ {
_cameraMarker.get(2).broadcastPacket(new SpecialCamera(_cameraMarker.get(2).getObjectId(), 1839, 90, 5, 1500, 15000)); _cameraMarker.get(2).broadcastPacket(new SpecialCamera(_cameraMarker.get(2).getObjectId(), 1839, 90, 5, 1500, 15000));
} }
@ -1589,7 +1589,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(1)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(1)) <= DISTANCE)
{ {
_cameraMarker.get(1).broadcastPacket(new SpecialCamera(_cameraMarker.get(1).getObjectId(), 1872, 90, 5, 0, 15000)); _cameraMarker.get(1).broadcastPacket(new SpecialCamera(_cameraMarker.get(1).getObjectId(), 1872, 90, 5, 0, 15000));
} }
@ -1608,7 +1608,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_cameraMarker.get(1)) <= DISTANCE) if (pc.calculateDistanceSq2D(_cameraMarker.get(1)) <= DISTANCE)
{ {
_cameraMarker.get(1).broadcastPacket(new SpecialCamera(_cameraMarker.get(1).getObjectId(), 2002, 90, 2, 1500, 15000)); _cameraMarker.get(1).broadcastPacket(new SpecialCamera(_cameraMarker.get(1).getObjectId(), 2002, 90, 2, 1500, 15000));
} }
@ -1627,7 +1627,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_vanHalter) <= DISTANCE) if (pc.calculateDistanceSq2D(_vanHalter) <= DISTANCE)
{ {
_vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 50, 90, 10, 0, 15000)); _vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 50, 90, 10, 0, 15000));
} }
@ -1679,7 +1679,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_vanHalter) <= DISTANCE) if (pc.calculateDistanceSq2D(_vanHalter) <= DISTANCE)
{ {
_vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 100, 90, 15, 1500, 15000)); _vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 100, 90, 15, 1500, 15000));
} }
@ -1698,7 +1698,7 @@ public class VanHalter extends Quest
// Set camera. // Set camera.
for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values()) for (PlayerInstance pc : _vanHalter.getKnownList().getKnownPlayers().values())
{ {
if (pc.getPlanDistanceSq(_vanHalter) <= DISTANCE) if (pc.calculateDistanceSq2D(_vanHalter) <= DISTANCE)
{ {
_vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 5200, 90, -10, 9500, 6000)); _vanHalter.broadcastPacket(new SpecialCamera(_vanHalter.getObjectId(), 5200, 90, -10, 9500, 6000));
} }

View File

@ -521,7 +521,7 @@ public class AttackableAI extends CreatureAI
_actor.setWalking(); _actor.setWalking();
} }
if (_actor.getPlanDistanceSq(((MinionInstance) _actor).getLeader()) > (offset * offset)) if (_actor.calculateDistanceSq2D(((MinionInstance) _actor).getLeader()) > (offset * offset))
{ {
int x1; int x1;
int y1; int y1;
@ -556,7 +556,7 @@ public class AttackableAI extends CreatureAI
z1 = p[2]; z1 = p[2];
// Calculate the distance between the current position of the Creature and the target (x,y) // Calculate the distance between the current position of the Creature and the target (x,y)
final double distance2 = _actor.getPlanDistanceSq(x1, y1); final double distance2 = _actor.calculateDistanceSq2D(x1, y1, z1);
if (distance2 > (Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE)) if (distance2 > (Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE))
{ {
npc.setReturningToSpawnPoint(true); npc.setReturningToSpawnPoint(true);
@ -810,7 +810,7 @@ public class AttackableAI extends CreatureAI
if ((weapon != null) && (weapon.getItemType() == WeaponType.BOW)) if ((weapon != null) && (weapon.getItemType() == WeaponType.BOW))
{ {
// Micht: kepping this one otherwise we should do 2 sqrt // Micht: kepping this one otherwise we should do 2 sqrt
final double distance2 = _actor.getPlanDistanceSq(originalAttackTarget.getX(), originalAttackTarget.getY()); final double distance2 = _actor.calculateDistanceSq2D(originalAttackTarget);
if (Math.sqrt(distance2) <= (60 + combinedCollision)) if (Math.sqrt(distance2) <= (60 + combinedCollision))
{ {
final int chance = 5; final int chance = 5;
@ -861,7 +861,7 @@ public class AttackableAI extends CreatureAI
setAttackTarget(hated); setAttackTarget(hated);
} }
// We should calculate new distance cuz mob can have changed the target // We should calculate new distance cuz mob can have changed the target
dist2 = _actor.getPlanDistanceSq(hated.getX(), hated.getY()); dist2 = _actor.calculateDistanceSq2D(hated);
if (hated.isMoving()) if (hated.isMoving())
{ {
range += 50; range += 50;

View File

@ -189,7 +189,7 @@ public class ControllableMobAI extends AttackableAI
ctrlAi.forceAttack(_actor); ctrlAi.forceAttack(_actor);
final Skill[] skills = _actor.getAllSkills(); final Skill[] skills = _actor.getAllSkills();
final double dist2 = _actor.getPlanDistanceSq(target.getX(), target.getY()); final double dist2 = _actor.calculateDistanceSq2D(target);
final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius(); final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + target.getTemplate().getCollisionRadius();
int maxRange = range; int maxRange = range;
if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20)))) if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
@ -226,7 +226,7 @@ public class ControllableMobAI extends AttackableAI
_actor.setTarget(getForcedTarget()); _actor.setTarget(getForcedTarget());
final Skill[] skills = _actor.getAllSkills(); final Skill[] skills = _actor.getAllSkills();
final double dist2 = _actor.getPlanDistanceSq(getForcedTarget().getX(), getForcedTarget().getY()); final double dist2 = _actor.calculateDistanceSq2D(getForcedTarget());
final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius(); final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getForcedTarget().getTemplate().getCollisionRadius();
int maxRange = range; int maxRange = range;
if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20)))) if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))
@ -293,7 +293,7 @@ public class ControllableMobAI extends AttackableAI
_actor.setTarget(getAttackTarget()); _actor.setTarget(getAttackTarget());
final Skill[] skills = _actor.getAllSkills(); final Skill[] skills = _actor.getAllSkills();
final double dist2 = _actor.getPlanDistanceSq(getAttackTarget().getX(), getAttackTarget().getY()); final double dist2 = _actor.calculateDistanceSq2D(getAttackTarget());
final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius(); final int range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + getAttackTarget().getTemplate().getCollisionRadius();
int maxRange = range; int maxRange = range;
if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20)))) if (!_actor.isMuted() && (dist2 > ((range + 20) * (range + 20))))

View File

@ -596,7 +596,7 @@ public class FortSiegeGuardAI extends CreatureAI implements Runnable
{ {
_actor.setTarget(attackTarget); _actor.setTarget(attackTarget);
skills = _actor.getAllSkills(); skills = _actor.getAllSkills();
dist2 = _actor.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY()); dist2 = _actor.calculateDistanceSq2D(attackTarget);
range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius(); range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
if (attackTarget.isMoving()) if (attackTarget.isMoving())
{ {

View File

@ -317,7 +317,7 @@ public class SiegeGuardAI extends CreatureAI implements Runnable
{ {
_actor.setTarget(attackTarget); _actor.setTarget(attackTarget);
skills = _actor.getAllSkills(); skills = _actor.getAllSkills();
dist2 = _actor.getPlanDistanceSq(attackTarget.getX(), attackTarget.getY()); dist2 = _actor.calculateDistanceSq2D(attackTarget);
range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius(); range = _actor.getPhysicalAttackRange() + _actor.getTemplate().getCollisionRadius() + attackTarget.getTemplate().getCollisionRadius();
} }
catch (NullPointerException e) catch (NullPointerException e)

View File

@ -475,6 +475,94 @@ public abstract class WorldObject
return false; return false;
} }
/**
* Calculates 2D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistance2D(int x, int y, int z)
{
return Math.sqrt(Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2));
}
/**
* Calculates the 2D distance between this WorldObject and given WorldObject.
* @param object the target object
* @return distance between object and given object.
*/
public double calculateDistance2D(WorldObject object)
{
return calculateDistance2D(object.getX(), object.getY(), object.getZ());
}
/**
* Calculates the 3D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistance3D(int x, int y, int z)
{
return Math.sqrt(Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + Math.pow(z - getZ(), 2));
}
/**
* Calculates 3D distance between this WorldObject and given location.
* @param loc the location object
* @return distance between object and given location.
*/
public double calculateDistance3D(Location loc)
{
return calculateDistance3D(loc.getX(), loc.getY(), loc.getZ());
}
/**
* Calculates the non squared 2D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistanceSq2D(int x, int y, int z)
{
return Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2);
}
/**
* Calculates the non squared 2D distance between this WorldObject and given WorldObject.
* @param object the target object
* @return distance between object and given object.
*/
public double calculateDistanceSq2D(WorldObject object)
{
return calculateDistanceSq2D(object.getX(), object.getY(), object.getZ());
}
/**
* Calculates the non squared 3D distance between this WorldObject and given x, y, z.
* @param x the X coordinate
* @param y the Y coordinate
* @param z the Z coordinate
* @return distance between object and given x, y, z.
*/
public double calculateDistanceSq3D(int x, int y, int z)
{
return Math.pow(x - getX(), 2) + Math.pow(y - getY(), 2) + Math.pow(z - getZ(), 2);
}
/**
* Calculates the non squared 3D distance between this WorldObject and given WorldObject.
* @param object the target object
* @return distance between object and given object.
*/
public double calculateDistanceSq3D(WorldObject object)
{
return calculateDistanceSq3D(object.getX(), object.getY(), object.getZ());
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -619,7 +619,7 @@ public abstract class Creature extends WorldObject implements ISkillsHolder
} }
// This function is called too often from movement code. // This function is called too often from movement code.
if (!force && (getDistanceSq(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < (isNpc() && !isInCombat() ? Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE : 10000))) if (!force && (calculateDistanceSq3D(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < (isNpc() && !isInCombat() ? Config.MAX_DRIFT_RANGE * Config.MAX_DRIFT_RANGE : 10000)))
{ {
return; return;
} }
@ -5843,88 +5843,6 @@ public abstract class Creature extends WorldObject implements ISkillsHolder
return result; return result;
} }
/**
* Return the distance between the current position of the Creature and the target (x,y).
* @param x X position of the target
* @param y Y position of the target
* @return the plan distance
* @deprecated use getPlanDistanceSq(int x, int y, int z)
*/
@Deprecated
public double getDistance(int x, int y)
{
final double dx = x - getX();
final double dy = y - getY();
return Math.sqrt((dx * dx) + (dy * dy));
}
/**
* Return the distance between the current position of the Creature and the target (x,y).
* @param x X position of the target
* @param y Y position of the target
* @param z the z
* @return the plan distance
* @deprecated use getPlanDistanceSq(int x, int y, int z)
*/
@Deprecated
public double getDistance(int x, int y, int z)
{
final double dx = x - getX();
final double dy = y - getY();
final double dz = z - getZ();
return Math.sqrt((dx * dx) + (dy * dy) + (dz * dz));
}
/**
* Return the squared distance between the current position of the Creature and the given object.
* @param object WorldObject
* @return the squared distance
*/
public double getDistanceSq(WorldObject object)
{
return getDistanceSq(object.getX(), object.getY(), object.getZ());
}
/**
* Return the squared distance between the current position of the Creature and the given x, y, z.
* @param x X position of the target
* @param y Y position of the target
* @param z Z position of the target
* @return the squared distance
*/
public double getDistanceSq(int x, int y, int z)
{
final double dx = x - getX();
final double dy = y - getY();
final double dz = z - getZ();
return (dx * dx) + (dy * dy) + (dz * dz);
}
/**
* Return the squared plan distance between the current position of the Creature and the given object.<br>
* (check only x and y, not z)
* @param object WorldObject
* @return the squared plan distance
*/
public double getPlanDistanceSq(WorldObject object)
{
return getPlanDistanceSq(object.getX(), object.getY());
}
/**
* Return the squared plan distance between the current position of the Creature and the given x, y, z.<br>
* (check only x and y, not z)
* @param x X position of the target
* @param y Y position of the target
* @return the squared plan distance
*/
public double getPlanDistanceSq(int x, int y)
{
final double dx = x - getX();
final double dy = y - getY();
return (dx * dx) + (dy * dy);
}
/** /**
* Check if this object is inside the given radius around the given object. Warning: doesn't cover collision radius! * Check if this object is inside the given radius around the given object. Warning: doesn't cover collision radius!
* @param object the target * @param object the target

View File

@ -1741,7 +1741,7 @@ public class PlayerInstance extends Playable
} }
// This function is called too often from movement code. // This function is called too often from movement code.
if (!force && (getDistanceSq(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < 10000)) if (!force && (calculateDistanceSq3D(_lastZoneValidateLocation.getX(), _lastZoneValidateLocation.getY(), _lastZoneValidateLocation.getZ()) < 10000))
{ {
return; return;
} }

View File

@ -1166,7 +1166,6 @@ public class Formulas
return (creature.calcStat(Stat.REGENERATE_CP_RATE, init, null, null) * cpRegenMultiplier) + cpRegenBonus; return (creature.calcStat(Stat.REGENERATE_CP_RATE, init, null, null) * cpRegenMultiplier) + cpRegenBonus;
} }
@SuppressWarnings("deprecation")
public static final double calcFestivalRegenModifier(PlayerInstance player) public static final double calcFestivalRegenModifier(PlayerInstance player)
{ {
final int[] festivalInfo = SevenSignsFestival.getInstance().getFestivalForPlayer(player); final int[] festivalInfo = SevenSignsFestival.getInstance().getFestivalForPlayer(player);
@ -1191,7 +1190,7 @@ public class Formulas
} }
// Check the distance between the player and the player spawn point, in the center of the arena. // Check the distance between the player and the player spawn point, in the center of the arena.
final double distToCenter = player.getDistance(festivalCenter[0], festivalCenter[1]); final double distToCenter = player.calculateDistance2D(festivalCenter[0], festivalCenter[1], 0);
return 1.0 - (distToCenter * 0.0005); // Maximum Decreased Regen of ~ -65%; return 1.0 - (distToCenter * 0.0005); // Maximum Decreased Regen of ~ -65%;
} }

View File

@ -77,7 +77,7 @@ public class RequestGetItemFromPet extends GameClientPacket
return; return;
} }
if (player.getDistanceSq(pet) > 40000) // 200*200 if (player.calculateDistanceSq3D(pet) > 40000) // 200*200
{ {
player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE); player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE);
sendPacket(ActionFailed.STATIC_PACKET); sendPacket(ActionFailed.STATIC_PACKET);

View File

@ -201,7 +201,7 @@ public class TradeRequest extends GameClientPacket
return; return;
} }
if (player.getDistanceSq(partner) > 22500) // 150 if (player.calculateDistanceSq3D(partner) > 22500) // 150
{ {
player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE); player.sendPacket(SystemMessageId.YOUR_TARGET_IS_OUT_OF_RANGE);
player.sendPacket(ActionFailed.STATIC_PACKET); player.sendPacket(ActionFailed.STATIC_PACKET);

View File

@ -165,7 +165,7 @@ public class Broadcast
for (PlayerInstance player : creature.getKnownList().getKnownPlayers().values()) for (PlayerInstance player : creature.getKnownList().getKnownPlayers().values())
{ {
if ((player != null) && (creature.getDistanceSq(player) <= radiusSq)) if ((player != null) && (creature.calculateDistanceSq3D(player) <= radiusSq))
{ {
player.sendPacket(mov); player.sendPacket(mov);
} }