RangedAttackHandler.java
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:12k
源码类别:

游戏

开发平台:

Java

  1. /*
  2. This file is part of the OdinMS Maple Story Server
  3. Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
  4. Matthias Butz <matze@odinms.de>
  5. Jan Christian Meyer <vimes@odinms.de>
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Affero General Public License version 3
  8. as published by the Free Software Foundation. You may not use, modify
  9. or distribute this program under any other version of the
  10. GNU Affero General Public License.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU Affero General Public License for more details.
  15. You should have received a copy of the GNU Affero General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18. package net.sf.odinms.net.channel.handler;
  19. import java.util.concurrent.ScheduledFuture;
  20. import net.sf.odinms.client.IItem;
  21. import net.sf.odinms.client.ISkill;
  22. import net.sf.odinms.client.MapleBuffStat;
  23. import net.sf.odinms.client.MapleCharacter;
  24. import net.sf.odinms.client.MapleCharacter.CancelCooldownAction;
  25. import net.sf.odinms.client.MapleClient;
  26. import net.sf.odinms.client.MapleInventory;
  27. import net.sf.odinms.client.MapleInventoryType;
  28. import net.sf.odinms.client.MapleJob;
  29. import net.sf.odinms.client.MapleWeaponType;
  30. import net.sf.odinms.client.SkillFactory;
  31. import net.sf.odinms.client.anticheat.CheatingOffense;
  32. import net.sf.odinms.net.MaplePacket;
  33. import net.sf.odinms.server.MapleInventoryManipulator;
  34. import net.sf.odinms.server.MapleItemInformationProvider;
  35. import net.sf.odinms.server.MapleStatEffect;
  36. import net.sf.odinms.server.TimerManager;
  37. import net.sf.odinms.tools.MaplePacketCreator;
  38. import net.sf.odinms.tools.data.input.SeekableLittleEndianAccessor;
  39. public class RangedAttackHandler extends AbstractDealDamageHandler {
  40.     private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RangedAttackHandler.class);
  41.     @Override
  42.     public void handlePacket(SeekableLittleEndianAccessor slea, MapleClient c) {
  43.         c.getPlayer().resetAfkTime();
  44.         AttackInfo attack = parseDamage(slea, true);
  45.         MapleCharacter player = c.getPlayer();
  46.         if (attack.skill == 5121002) {
  47.             player.getMap().broadcastMessage(player, MaplePacketCreator.rangedAttack(player.getId(), attack.skill, attack.stance, attack.numAttackedAndDamage, 0, attack.allDamage, attack.speed), false);
  48.             applyAttack(attack, player, 9999999, 1);
  49.         } else {
  50.             MapleInventory equip = player.getInventory(MapleInventoryType.EQUIPPED);
  51.             IItem weapon = equip.getItem((byte) -11);
  52.             MapleItemInformationProvider mii = MapleItemInformationProvider.getInstance();
  53.             MapleWeaponType type = mii.getWeaponType(weapon.getItemId());
  54.             if (type == MapleWeaponType.NOT_A_WEAPON) {
  55.                 throw new RuntimeException("[h4x] Player " + player.getName() + " is attacking with something that's not a weapon");
  56.             }
  57.             MapleInventory use = player.getInventory(MapleInventoryType.USE);
  58.             int projectile = 0;
  59.             int bulletCount = 1;
  60.             MapleStatEffect effect = null;
  61.             if (attack.skill != 0) {
  62.                 effect = attack.getAttackEffect(c.getPlayer());
  63.                 bulletCount = effect.getBulletCount();
  64.                 if (effect.getCooldown() > 0) {
  65.                     c.getSession().write(MaplePacketCreator.skillCooldown(attack.skill, effect.getCooldown()));
  66.                 }
  67.             }
  68.             boolean hasShadowPartner = player.getBuffedValue(MapleBuffStat.SHADOWPARTNER) != null;
  69.             int damageBulletCount = bulletCount;
  70.             if (hasShadowPartner) {
  71.                 bulletCount *= 2;
  72.             }
  73.             for (int i = 0; i < 255; i++) { // impose order...
  74.                 IItem item = use.getItem((byte) i);
  75.                 if (item != null) {
  76.                     boolean clawCondition = type == MapleWeaponType.CLAW && mii.isThrowingStar(item.getItemId()) && weapon.getItemId() != 1472063;
  77.                     boolean bowCondition = type == MapleWeaponType.BOW && mii.isArrowForBow(item.getItemId());
  78.                     boolean crossbowCondition = type == MapleWeaponType.CROSSBOW && mii.isArrowForCrossBow(item.getItemId());
  79.                     boolean gunCondition = type == MapleWeaponType.GUN && mii.isBullet(item.getItemId());
  80.                     boolean mittenCondition = weapon.getItemId() == 1472063 && (mii.isArrowForBow(item.getItemId()) || mii.isArrowForCrossBow(item.getItemId()));
  81.                     if ((clawCondition || bowCondition || crossbowCondition || mittenCondition || gunCondition) && item.getQuantity() >= bulletCount) {
  82.                         projectile = item.getItemId();
  83.                         break;
  84.                     }
  85.                 }
  86.             }
  87.             boolean soulArrow = player.getBuffedValue(MapleBuffStat.SOULARROW) != null;
  88.             boolean shadowClaw = player.getBuffedValue(MapleBuffStat.SHADOW_CLAW) != null;
  89.             if (!soulArrow && !shadowClaw && projectile != 0) {
  90.                 int bulletConsume = bulletCount;
  91.                 if (effect != null && effect.getBulletConsume() != 0) {
  92.                     bulletConsume = effect.getBulletConsume() * (hasShadowPartner ? 2 : 1);
  93.                 }
  94.                 MapleInventoryManipulator.removeById(c, MapleInventoryType.USE, projectile, bulletConsume, false, true);
  95.             }
  96.             if (projectile != 0 || soulArrow) {
  97.                 int visProjectile = projectile; //visible projectile sent to players
  98.                 if (mii.isThrowingStar(projectile)) {
  99.                     //see if player has cash stars
  100.                     MapleInventory cash = player.getInventory(MapleInventoryType.CASH);
  101.                     for (int i = 0; i < 255; i++) { // impose order...
  102.                         IItem item = cash.getItem((byte) i);
  103.                         if (item != null) {
  104.                             //cash stars have prefix 5021xxx
  105.                             if (item.getItemId() / 1000 == 5021) {
  106.                                 visProjectile = item.getItemId();
  107.                                 break;
  108.                             }
  109.                         }
  110.                     }
  111.                 } else { //bow, crossbow
  112.                     if (soulArrow || attack.skill == 3111004 || attack.skill == 3211004) {
  113.                         visProjectile = 0; //arrow rain/eruption show no arrows
  114.                     }
  115.                 }
  116.                 MaplePacket packet;
  117.                 try {
  118.                     switch (attack.skill) {
  119.                         case 3121004: // Hurricane
  120.                         case 3221001: // Pierce
  121.                         case 5221004: // Rapid Fire
  122.                             packet = MaplePacketCreator.rangedAttack(player.getId(), attack.skill, attack.direction, attack.numAttackedAndDamage, visProjectile, attack.allDamage, attack.speed);
  123.                             break;
  124.                         default:
  125.                             packet = MaplePacketCreator.rangedAttack(player.getId(), attack.skill, attack.stance, attack.numAttackedAndDamage, visProjectile, attack.allDamage, attack.speed);
  126.                             break;
  127.                     }
  128.                     player.getMap().broadcastMessage(player, packet, false, true);
  129.                 } catch (Exception e) {
  130.                     log.warn("Failed to handle ranged attack..", e);
  131.                 }
  132.                 int basedamage;
  133.                 int projectileWatk = 0;
  134.                 if (projectile != 0) {
  135.                     projectileWatk = mii.getWatkForProjectile(projectile);
  136.                 }
  137.                 if (attack.skill != 4001344) { // not lucky 7
  138.                     if (projectileWatk != 0) {
  139.                         basedamage = c.getPlayer().calculateMaxBaseDamage(c.getPlayer().getTotalWatk() + projectileWatk);
  140.                     } else {
  141.                         basedamage = c.getPlayer().getCurrentMaxBaseDamage();
  142.                     }
  143.                 } else { // l7 has a different formula :>
  144.                     basedamage = (int) (((c.getPlayer().getTotalLuk() * 5.0) / 100.0) * (c.getPlayer().getTotalWatk() + projectileWatk));
  145.                 }
  146.                 if (attack.skill == 3101005) { //arrowbomb is hardcore like that �.o
  147.                     basedamage *= effect.getX() / 100.0;
  148.                 }
  149.                 int maxdamage = basedamage;
  150.                 double critdamagerate = 0.0;
  151.                 if (player.getJob().isA(MapleJob.ASSASSIN)) {
  152.                     ISkill criticalthrow = SkillFactory.getSkill(4100001);
  153.                     int critlevel = player.getSkillLevel(criticalthrow);
  154.                     if (critlevel > 0) {
  155.                         critdamagerate = (criticalthrow.getEffect(player.getSkillLevel(criticalthrow)).getDamage() / 100.0);
  156.                     }
  157.                 } else if (player.getJob().isA(MapleJob.BOWMAN)) {
  158.                     ISkill criticalshot = SkillFactory.getSkill(3000001);
  159.                     int critlevel = player.getSkillLevel(criticalshot);
  160.                     if (critlevel > 0) {
  161.                         critdamagerate = (criticalshot.getEffect(critlevel).getDamage() / 100.0) - 1.0;
  162.                     }
  163.                 }
  164.                 int critdamage = (int) (basedamage * critdamagerate);
  165.                 if (effect != null) {
  166.                     maxdamage *= effect.getDamage() / 100.0;
  167.                 }
  168.                 maxdamage += critdamage;
  169.                 maxdamage *= damageBulletCount;
  170.                 if (hasShadowPartner) {
  171.                     ISkill shadowPartner = SkillFactory.getSkill(4111002);
  172.                     int shadowPartnerLevel = player.getSkillLevel(shadowPartner);
  173.                     MapleStatEffect shadowPartnerEffect = shadowPartner.getEffect(shadowPartnerLevel);
  174.                     if (attack.skill != 0) {
  175.                         maxdamage *= (1.0 + shadowPartnerEffect.getY() / 100.0);
  176.                     } else {
  177.                         maxdamage *= (1.0 + shadowPartnerEffect.getX() / 100.0);
  178.                     }
  179.                 }
  180.                 if (attack.skill == 4111004) {
  181.                     maxdamage = 35000;
  182.                 }
  183.                 if (effect != null) {
  184.                     int money = effect.getMoneyCon();
  185.                     if (money != 0) {
  186.                         double moneyMod = money * 0.5;
  187.                         money = (int) (money + Math.random() * moneyMod);
  188.                         if (money > player.getMeso()) {
  189.                             money = player.getMeso();
  190.                         }
  191.                         player.gainMeso(-money, false);
  192.                     }
  193.                 }
  194.                 if (attack.skill != 0) {
  195.                     ISkill skill = SkillFactory.getSkill(attack.skill);
  196.                     int skillLevel = c.getPlayer().getSkillLevel(skill);
  197.                     MapleStatEffect effect_ = skill.getEffect(skillLevel);
  198.                     if (effect_.getCooldown() > 0) {
  199.                         if (player.skillisCooling(attack.skill)) {
  200.                             player.getCheatTracker().registerOffense(CheatingOffense.COOLDOWN_HACK);
  201.                             return;
  202.                         } else {
  203.                             c.getSession().write(MaplePacketCreator.skillCooldown(attack.skill, effect_.getCooldown()));
  204.                             ScheduledFuture<?> timer = TimerManager.getInstance().schedule(new CancelCooldownAction(c.getPlayer(), attack.skill), effect_.getCooldown() * 1000);
  205.                             player.addCooldown(attack.skill, System.currentTimeMillis(), effect_.getCooldown() * 1000, timer);
  206.                         }
  207.                     }
  208.                 }
  209.                 applyAttack(attack, player, maxdamage, bulletCount);
  210.             }
  211.         }
  212.     }
  213. }