From 8cd136cc2773a3dc2f47cc4d0999dd42a14dc179 Mon Sep 17 00:00:00 2001 From: Adry85 <adrya85@hotmail.it> Date: Tue, 3 Oct 2017 21:44:21 +0200 Subject: [PATCH] Optimized code for EnemyCharge and ThrowUp effects. --- .../handlers/effecthandlers/EnemyCharge.java | 30 ++++++++------ .../handlers/effecthandlers/ThrowUp.java | 41 ++++++++++--------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/dist/game/data/scripts/handlers/effecthandlers/EnemyCharge.java b/dist/game/data/scripts/handlers/effecthandlers/EnemyCharge.java index 9c195353a1..7af28f1614 100644 --- a/dist/game/data/scripts/handlers/effecthandlers/EnemyCharge.java +++ b/dist/game/data/scripts/handlers/effecthandlers/EnemyCharge.java @@ -21,6 +21,7 @@ package handlers.effecthandlers; import com.l2jserver.gameserver.GeoData; import com.l2jserver.gameserver.model.Location; import com.l2jserver.gameserver.model.StatsSet; +import com.l2jserver.gameserver.model.actor.L2Character; import com.l2jserver.gameserver.model.conditions.Condition; import com.l2jserver.gameserver.model.effects.AbstractEffect; import com.l2jserver.gameserver.model.skills.BuffInfo; @@ -52,19 +53,22 @@ public final class EnemyCharge extends AbstractEffect return; } + L2Character target = info.getEffected(); + L2Character activeChar = info.getEffector(); + // Get current position of the L2Character - final int curX = info.getEffector().getX(); - final int curY = info.getEffector().getY(); - final int curZ = info.getEffector().getZ(); + final int curX = activeChar.getX(); + final int curY = activeChar.getY(); + final int curZ = activeChar.getZ(); // Calculate distance (dx,dy) between current position and destination - double dx = info.getEffected().getX() - curX; - double dy = info.getEffected().getY() - curY; - double dz = info.getEffected().getZ() - curZ; - double distance = Math.sqrt((dx * dx) + (dy * dy)); + double dx = target.getX() - curX; + double dy = target.getY() - curY; + double dz = target.getZ() - curZ; + double distance = Math.hypot(dx, dy); if (distance > 2000) { - _log.info("EffectEnemyCharge was going to use invalid coordinates for characters, getEffector: " + curX + "," + curY + " and getEffected: " + info.getEffected().getX() + "," + info.getEffected().getY()); + _log.info("EffectEnemyCharge was going to use invalid coordinates for characters, getEffector: " + curX + "," + curY + " and getEffected: " + target.getX() + "," + target.getY()); return; } @@ -91,14 +95,14 @@ public final class EnemyCharge extends AbstractEffect // Calculate the new destination with offset included int x = curX + (int) ((distance - offset) * cos); int y = curY + (int) ((distance - offset) * sin); - int z = info.getEffected().getZ(); + int z = target.getZ(); - final Location destination = GeoData.getInstance().moveCheck(info.getEffector().getX(), info.getEffector().getY(), info.getEffector().getZ(), x, y, z, info.getEffector().getInstanceId()); + final Location destination = GeoData.getInstance().moveCheck(curX, curY, curZ, x, y, z, activeChar.getInstanceId()); - info.getEffector().broadcastPacket(new FlyToLocation(info.getEffector(), destination, FlyType.CHARGE)); + activeChar.broadcastPacket(new FlyToLocation(activeChar, destination, FlyType.CHARGE)); // maybe is need force set X,Y,Z - info.getEffector().setXYZ(destination); - info.getEffector().broadcastPacket(new ValidateLocation(info.getEffector())); + activeChar.setXYZ(destination); + activeChar.broadcastPacket(new ValidateLocation(activeChar)); } } diff --git a/dist/game/data/scripts/handlers/effecthandlers/ThrowUp.java b/dist/game/data/scripts/handlers/effecthandlers/ThrowUp.java index ae7bbd6bae..b34a1aefd4 100644 --- a/dist/game/data/scripts/handlers/effecthandlers/ThrowUp.java +++ b/dist/game/data/scripts/handlers/effecthandlers/ThrowUp.java @@ -21,6 +21,7 @@ package handlers.effecthandlers; import com.l2jserver.gameserver.GeoData; import com.l2jserver.gameserver.model.Location; import com.l2jserver.gameserver.model.StatsSet; +import com.l2jserver.gameserver.model.actor.L2Character; import com.l2jserver.gameserver.model.conditions.Condition; import com.l2jserver.gameserver.model.effects.AbstractEffect; import com.l2jserver.gameserver.model.effects.EffectFlag; @@ -54,26 +55,26 @@ public final class ThrowUp extends AbstractEffect @Override public void onStart(BuffInfo info) { + L2Character target = info.getEffected(); + L2Character activeChar = info.getEffector(); + // Get current position of the L2Character - final int curX = info.getEffected().getX(); - final int curY = info.getEffected().getY(); - final int curZ = info.getEffected().getZ(); + final int curX = target.getX(); + final int curY = target.getY(); + final int curZ = target.getZ(); // Calculate distance between effector and effected current position - double dx = info.getEffector().getX() - curX; - double dy = info.getEffector().getY() - curY; - double dz = info.getEffector().getZ() - curZ; - double distance = Math.sqrt((dx * dx) + (dy * dy)); + double dx = activeChar.getX() - curX; + double dy = activeChar.getY() - curY; + double dz = activeChar.getZ() - curZ; + double distance = Math.hypot(dx, dy); if (distance > 2000) { - _log.info("EffectThrow was going to use invalid coordinates for characters, getEffected: " + curX + "," + curY + " and getEffector: " + info.getEffector().getX() + "," + info.getEffector().getY()); + _log.info("EffectThrow was going to use invalid coordinates for characters, getEffected: " + curX + "," + curY + " and getEffector: " + activeChar.getX() + "," + activeChar.getY()); return; } int offset = Math.min((int) distance + info.getSkill().getFlyRadius(), 1400); - double cos; - double sin; - // approximation for moving futher when z coordinates are different // TODO: handle Z axis movement better offset += Math.abs(dz); @@ -89,19 +90,19 @@ public final class ThrowUp extends AbstractEffect } // Calculate movement angles needed - sin = dy / distance; - cos = dx / distance; + double sin = dy / distance; + double cos = dx / distance; // Calculate the new destination with offset included - int x = info.getEffector().getX() - (int) (offset * cos); - int y = info.getEffector().getY() - (int) (offset * sin); - int z = info.getEffected().getZ(); + int x = activeChar.getX() - (int) (offset * cos); + int y = activeChar.getY() - (int) (offset * sin); + int z = target.getZ(); - final Location destination = GeoData.getInstance().moveCheck(info.getEffected().getX(), info.getEffected().getY(), info.getEffected().getZ(), x, y, z, info.getEffected().getInstanceId()); + final Location destination = GeoData.getInstance().moveCheck(curX, curY, curZ, x, y, z, target.getInstanceId()); - info.getEffected().broadcastPacket(new FlyToLocation(info.getEffected(), destination, FlyType.THROW_UP)); + target.broadcastPacket(new FlyToLocation(target, destination, FlyType.THROW_UP)); // TODO: Review. - info.getEffected().setXYZ(destination); - info.getEffected().broadcastPacket(new ValidateLocation(info.getEffected())); + target.setXYZ(destination); + target.broadcastPacket(new ValidateLocation(target)); } } -- GitLab