Advertisement
Deedlit

rndselector

Mar 22nd, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.02 KB | None | 0 0
  1. /*
  2.  * This program is free software: you can redistribute it and/or modify it under
  3.  * the terms of the GNU General Public License as published by the Free Software
  4.  * Foundation, either version 3 of the License, or (at your option) any later
  5.  * version.
  6.  *
  7.  * This program is distributed in the hope that it will be useful, but WITHOUT
  8.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9.  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10.  * details.
  11.  *
  12.  * You should have received a copy of the GNU General Public License along with
  13.  * this program. If not, see <http://www.gnu.org/licenses/>.
  14.  */
  15. package com.l2jserver.gameserver.ai.ext.util;
  16.  
  17. import com.l2jserver.gameserver.ai.L2CharacterAI;
  18. import com.l2jserver.gameserver.ext.util.ExtUtil;
  19. import com.l2jserver.gameserver.model.L2Skill;
  20. import com.l2jserver.gameserver.model.actor.L2Character;
  21. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  22. import com.l2jserver.util.Rnd;
  23. import javolution.util.FastList;
  24. import l2n.commons.math.random.RndSelector;
  25.  
  26. import java.util.logging.Logger;
  27.  
  28. /**
  29.  * ==============================================<br>
  30.  * SkillSelectors - Insert description...<br>
  31.  * ==============================================<br>
  32.  */
  33. public class SkillSelectors
  34. {
  35.     protected static final Logger _log = Logger.getLogger(SkillSelectors.class.getName());
  36.  
  37.     public final static L2Skill[] selectUsableSkills(final L2CharacterAI ai, final L2Character target, final double distance, final L2Skill... skills)
  38.     {
  39.         if(skills == null || skills.length == 0 || target == null)
  40.             return null;
  41.  
  42.         L2Skill[] ret = null;
  43.  
  44.         int usable = 0;
  45.         for(final L2Skill skill : skills)
  46.             if(canUseSkill(ai, skill, target, distance))
  47.             {
  48.                 if(ret == null)
  49.                     ret = new L2Skill[skills.length];
  50.                 ret[usable++] = skill;
  51.             }
  52.  
  53.         if(ret == null || usable == skills.length)
  54.             return ret;
  55.  
  56.         if(usable == 0)
  57.             return null;
  58.  
  59.         final L2Skill[] ret_resized = new L2Skill[usable];
  60.         System.arraycopy(ret, 0, ret_resized, 0, usable);
  61.         return ret_resized;
  62.     }
  63.  
  64.     public final static L2Skill selectTopSkillByDamage(final L2Character actor, final L2Character target, final double distance, final L2Skill... skills)
  65.     {
  66.         if(skills == null || skills.length == 0)
  67.             return null;
  68.  
  69.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
  70.         double weight;
  71.         for(final L2Skill skill : skills)
  72.         {
  73.             weight = skill.getSimpleDamage(actor, target) * skill.getCastRange() / distance;
  74.             if(weight <= 0)
  75.                 weight = 1;
  76.  
  77.             rnd.add(skill, (int) weight);
  78.         }
  79.         return rnd.select();
  80.     }
  81.  
  82.     public final static L2Skill selectTopSkillByDebuff(final L2Character actor, final L2Character target, final double distance, final L2Skill... skills)
  83.     {
  84.         if(skills == null || skills.length == 0)
  85.             return null;
  86.  
  87.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
  88.         double weight;
  89.         for(final L2Skill skill : skills)
  90.         {
  91.             if (skill.getEffects(actor, target, null, true) == null || skill.getEffects(actor, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
  92.                 continue;
  93.             if((weight = 100f * skill.getCastRange() / distance) <= 0)
  94.                 weight = 1;
  95.             rnd.add(skill, (int) weight);
  96.         }
  97.         return rnd.select();
  98.     }
  99.  
  100.     public final static L2Skill selectTopSkillByBuff(final L2Character target, final L2Skill... skills)
  101.     {
  102.         if(skills == null || skills.length == 0)
  103.             return null;
  104.  
  105.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
  106.         double weight;
  107.         for(final L2Skill skill : skills)
  108.         {
  109.             if (skill.getEffects(target, target, null, true) == null || skill.getEffects(target, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
  110.                 continue;
  111.             if((weight = skill.getPower()) <= 0)
  112.                 weight = 1;
  113.             rnd.add(skill, (int) weight);
  114.         }
  115.         return rnd.select();
  116.     }
  117.  
  118.     public final static L2Skill selectTopSkillByHeal(final L2Character target, final L2Skill... skills)
  119.     {
  120.         if(skills == null || skills.length == 0)
  121.             return null;
  122.  
  123.         final double hp_reduced = target.getMaxHp() - target.getCurrentHp();
  124.         if(hp_reduced < 1)
  125.             return null;
  126.  
  127.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
  128.         double weight;
  129.         for(final L2Skill skill : skills)
  130.         {
  131.             if((weight = Math.abs(skill.getPower() - hp_reduced)) <= 0)
  132.                 weight = 1;
  133.             rnd.add(skill, (int) weight);
  134.         }
  135.         return rnd.select();
  136.     }
  137.  
  138.     public final static boolean canUseSkill(final L2CharacterAI ai, final L2Skill sk, final L2Character target, final double distance)
  139.     {
  140.         final L2Character actor = ai.getActor() != null ? ai.getActor() : null;
  141.         if(actor == null || sk == null || actor.isSkillDisabled(sk))
  142.             return false;
  143.  
  144.         if(sk.getSkillType() == L2SkillType.NOTDONE)
  145.         {
  146.             _log.warning("L2CharacterAI: actor={"+actor.getNpcId()+"}-{"+ai.getClass().getSimpleName()+"} use not implemented skill {"+sk.toString()+"}");
  147.         }
  148.  
  149.         if(actor.getCurrentMp() < actor.getStat().getMpConsume(sk))
  150.             return false;
  151.  
  152.         if((sk.isMagic() && actor.isMuted()) || (!sk.isMagic() && actor.isPhysicalMuted()))
  153.             return false;
  154.  
  155.         final L2SkillType st = sk.getSkillType();
  156.         if(st == L2SkillType.CANCEL && (actor == target || Rnd.qchance(target.getAllEffects().length)))
  157.             return false;
  158.  
  159.         if(!sk.isUseableWithoutTarget() && !actor.equals(target))
  160.         {
  161.             if(sk.getCastRange() + actor.getRangesCalc().getCombinedCollision(target) < distance)
  162.                 return false;
  163.         }
  164.  
  165.         if(target.getFirstEffect(sk) != null && !sk.hasManyTargets(actor))
  166.             return false;
  167.  
  168.         L2Skill.SkillTargetType[] disallowedTargetTypesNpc = new L2Skill.SkillTargetType[]
  169.         {
  170. //          L2Skill.SkillTargetType.TARGET_ALLY,
  171. //          L2Skill.SkillTargetType.TARGET_PET,
  172. //          L2Skill.SkillTargetType.TARGET_SUMMON,
  173. //          L2Skill.SkillTargetType.TARGET_AURA_CORPSE_MOB,
  174. //          L2Skill.SkillTargetType.TARGET_CORPSE,
  175. //          L2Skill.SkillTargetType.TARGET_UNDEAD,
  176. //          L2Skill.SkillTargetType.TARGET_AREA_UNDEAD,
  177. //          L2Skill.SkillTargetType.TARGET_CORPSE_ALLY, // XXX add support for faction ally target
  178. //          L2Skill.SkillTargetType.TARGET_CORPSE_PLAYER,
  179. //          L2Skill.SkillTargetType.TARGET_CORPSE_PET,
  180. //          L2Skill.SkillTargetType.TARGET_AREA_CORPSE_MOB,
  181. //          L2Skill.SkillTargetType.TARGET_CORPSE_MOB,
  182.             L2Skill.SkillTargetType.TARGET_UNLOCKABLE,
  183.             L2Skill.SkillTargetType.TARGET_HOLY,
  184.             L2Skill.SkillTargetType.TARGET_FLAGPOLE,
  185. //          L2Skill.SkillTargetType.TARGET_PARTY_MEMBER,
  186. //          L2Skill.SkillTargetType.TARGET_PARTY_OTHER,
  187. //          L2Skill.SkillTargetType.TARGET_ENEMY_SUMMON,
  188.             L2Skill.SkillTargetType.TARGET_OWNER_PET,
  189.             L2Skill.SkillTargetType.TARGET_GROUND,
  190.             L2Skill.SkillTargetType.TARGET_GROUND_PHYSICS,
  191. //          L2Skill.SkillTargetType.TARGET_PARTY_NOTME,
  192. //          L2Skill.SkillTargetType.TARGET_AREA_SUMMON,
  193.         };
  194.  
  195.         return !ExtUtil.contains(disallowedTargetTypesNpc, sk.getTargetType()) && sk.checkCondition(actor, target, false);
  196.     }
  197.  
  198.     public final static L2Skill getTopSkill(final L2CharacterAI ai, final L2Character target, final double distance, final L2Skill... skills)
  199.     {
  200.         if(skills == null || skills.length == 0 || target == null)
  201.             return null;
  202.  
  203.         final RndSelector<L2Skill> selector = new RndSelector<>(skills.length);
  204.         for(L2Skill skill : skills)
  205.         {
  206.             if(skill == null || !canUseSkill(ai, skill, target, distance))
  207.                 continue;
  208.  
  209.             int weight = (int) -Math.abs(skill.getCastRange() - distance);
  210.             if(skill.getCastRange() >= distance)
  211.                 weight += 1000000;
  212.             else if(skill.isUseableWithoutTarget() && skill.getTargetList(ai.getActor(), false, target).length == 0)
  213.                 continue;
  214.  
  215.             selector.add(skill, weight);
  216.         }
  217.  
  218.         return selector.select();
  219.     }
  220.  
  221.     public final static L2Skill getTopSkill(final L2CharacterAI ai, final L2Character target, final double distance, final FastList<L2Skill> skills)
  222.     {
  223.         if(skills == null || skills.isEmpty() || target == null)
  224.             return null;
  225.  
  226.         final RndSelector<L2Skill> selector = new RndSelector<>(skills.size());
  227.         for(L2Skill skill : skills)
  228.         {
  229.             if(skill == null || !canUseSkill(ai, skill, target, distance))
  230.                 continue;
  231.  
  232.             int weight = (int) -Math.abs(skill.getCastRange() - distance);
  233.             if(skill.getCastRange() >= distance)
  234.                 weight += 1000000;
  235.             else if(skill.isUseableWithoutTarget() && skill.getTargetList(ai.getActor(), false, target).length == 0)
  236.                 continue;
  237.  
  238.             selector.add(skill, weight);
  239.         }
  240.  
  241.         return selector.select();
  242.     }
  243.  
  244.     public static L2Skill selectTopSkillByBuff(final L2Character target, final FastList<L2Skill> skills)
  245.     {
  246.         if(skills == null || skills.isEmpty())
  247.             return null;
  248.  
  249.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
  250.         double weight;
  251.         for(final L2Skill skill : skills)
  252.         {
  253.             if(skill.getEffects(target, target, null, true) == null || skill.getEffects(target, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
  254.                 continue;
  255.             if((weight = skill.getPower()) <= 0)
  256.                 weight = 1;
  257.             rnd.add(skill, (int) weight);
  258.         }
  259.         return rnd.select();
  260.     }
  261.  
  262.     public static L2Skill selectTopSkillByHeal(L2Character target, FastList<L2Skill> skills)
  263.     {
  264.         if(skills == null || skills.isEmpty())
  265.             return null;
  266.  
  267.         final double hp_reduced = target.getMaxHp() - target.getCurrentHp();
  268.         if(hp_reduced < 1)
  269.             return null;
  270.  
  271.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
  272.         double weight;
  273.         for(final L2Skill skill : skills)
  274.         {
  275.             if((weight = Math.abs(skill.getPower() - hp_reduced)) <= 0)
  276.                 weight = 1;
  277.             rnd.add(skill, (int) weight);
  278.         }
  279.         return rnd.select();
  280.     }
  281.  
  282.     public static FastList<L2Skill> selectUsableSkills(L2CharacterAI ai, L2Character target, double distance, FastList<L2Skill> skills)
  283.     {
  284.         if (skills == null || skills.isEmpty() || target == null)
  285.             return null;
  286.  
  287.         FastList<L2Skill> ret = new FastList<>();
  288.  
  289.         int usable = 0;
  290.         for(final L2Skill skill : skills)
  291.         {
  292.             if(canUseSkill(ai, skill, target, distance))
  293.             {
  294.                 ret.add(skill);
  295.                 usable++;
  296.             }
  297.         }
  298.         return ret;
  299.     }
  300.  
  301.     public static L2Skill selectTopSkillByDamage(L2Character actor, L2Character target, double distance, FastList<L2Skill> skills)
  302.     {
  303.         if(skills == null || skills.isEmpty())
  304.             return null;
  305.  
  306.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
  307.         double weight;
  308.         for(final L2Skill skill : skills)
  309.         {
  310.             weight = skill.getSimpleDamage(actor, target) * skill.getCastRange() / distance;
  311.             if(weight <= 0)
  312.                 weight = 1;
  313.  
  314.             rnd.add(skill, (int) weight);
  315.         }
  316.         return rnd.select();
  317.     }
  318.  
  319.     public static L2Skill selectTopSkillByDebuff(L2Character actor, L2Character target, double distance, FastList<L2Skill> skills)
  320.     {
  321.         if(skills == null || skills.isEmpty())
  322.             return null;
  323.  
  324.         final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
  325.         double weight;
  326.         for(final L2Skill skill : skills)
  327.         {
  328.             if(skill.getEffects(actor, target, null, true) == null || skill.getEffects(actor, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
  329.                 continue;
  330.             if((weight = 100f * skill.getCastRange() / distance) <= 0)
  331.                 weight = 1;
  332.             rnd.add(skill, (int) weight);
  333.         }
  334.         return rnd.select();
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement