Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This program is free software: you can redistribute it and/or modify it under
- * the terms of the GNU General Public License as published by the Free Software
- * Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.l2jserver.gameserver.ai.ext.util;
- import com.l2jserver.gameserver.ai.L2CharacterAI;
- import com.l2jserver.gameserver.ext.util.ExtUtil;
- import com.l2jserver.gameserver.model.L2Skill;
- import com.l2jserver.gameserver.model.actor.L2Character;
- import com.l2jserver.gameserver.templates.skills.L2SkillType;
- import com.l2jserver.util.Rnd;
- import javolution.util.FastList;
- import l2n.commons.math.random.RndSelector;
- import java.util.logging.Logger;
- /**
- * ==============================================<br>
- * SkillSelectors - Insert description...<br>
- * ==============================================<br>
- */
- public class SkillSelectors
- {
- protected static final Logger _log = Logger.getLogger(SkillSelectors.class.getName());
- public final static L2Skill[] selectUsableSkills(final L2CharacterAI ai, final L2Character target, final double distance, final L2Skill... skills)
- {
- if(skills == null || skills.length == 0 || target == null)
- return null;
- L2Skill[] ret = null;
- int usable = 0;
- for(final L2Skill skill : skills)
- if(canUseSkill(ai, skill, target, distance))
- {
- if(ret == null)
- ret = new L2Skill[skills.length];
- ret[usable++] = skill;
- }
- if(ret == null || usable == skills.length)
- return ret;
- if(usable == 0)
- return null;
- final L2Skill[] ret_resized = new L2Skill[usable];
- System.arraycopy(ret, 0, ret_resized, 0, usable);
- return ret_resized;
- }
- public final static L2Skill selectTopSkillByDamage(final L2Character actor, final L2Character target, final double distance, final L2Skill... skills)
- {
- if(skills == null || skills.length == 0)
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
- double weight;
- for(final L2Skill skill : skills)
- {
- weight = skill.getSimpleDamage(actor, target) * skill.getCastRange() / distance;
- if(weight <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public final static L2Skill selectTopSkillByDebuff(final L2Character actor, final L2Character target, final double distance, final L2Skill... skills)
- {
- if(skills == null || skills.length == 0)
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
- double weight;
- for(final L2Skill skill : skills)
- {
- if (skill.getEffects(actor, target, null, true) == null || skill.getEffects(actor, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
- continue;
- if((weight = 100f * skill.getCastRange() / distance) <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public final static L2Skill selectTopSkillByBuff(final L2Character target, final L2Skill... skills)
- {
- if(skills == null || skills.length == 0)
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
- double weight;
- for(final L2Skill skill : skills)
- {
- if (skill.getEffects(target, target, null, true) == null || skill.getEffects(target, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
- continue;
- if((weight = skill.getPower()) <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public final static L2Skill selectTopSkillByHeal(final L2Character target, final L2Skill... skills)
- {
- if(skills == null || skills.length == 0)
- return null;
- final double hp_reduced = target.getMaxHp() - target.getCurrentHp();
- if(hp_reduced < 1)
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.length);
- double weight;
- for(final L2Skill skill : skills)
- {
- if((weight = Math.abs(skill.getPower() - hp_reduced)) <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public final static boolean canUseSkill(final L2CharacterAI ai, final L2Skill sk, final L2Character target, final double distance)
- {
- final L2Character actor = ai.getActor() != null ? ai.getActor() : null;
- if(actor == null || sk == null || actor.isSkillDisabled(sk))
- return false;
- if(sk.getSkillType() == L2SkillType.NOTDONE)
- {
- _log.warning("L2CharacterAI: actor={"+actor.getNpcId()+"}-{"+ai.getClass().getSimpleName()+"} use not implemented skill {"+sk.toString()+"}");
- }
- if(actor.getCurrentMp() < actor.getStat().getMpConsume(sk))
- return false;
- if((sk.isMagic() && actor.isMuted()) || (!sk.isMagic() && actor.isPhysicalMuted()))
- return false;
- final L2SkillType st = sk.getSkillType();
- if(st == L2SkillType.CANCEL && (actor == target || Rnd.qchance(target.getAllEffects().length)))
- return false;
- if(!sk.isUseableWithoutTarget() && !actor.equals(target))
- {
- if(sk.getCastRange() + actor.getRangesCalc().getCombinedCollision(target) < distance)
- return false;
- }
- if(target.getFirstEffect(sk) != null && !sk.hasManyTargets(actor))
- return false;
- L2Skill.SkillTargetType[] disallowedTargetTypesNpc = new L2Skill.SkillTargetType[]
- {
- // L2Skill.SkillTargetType.TARGET_ALLY,
- // L2Skill.SkillTargetType.TARGET_PET,
- // L2Skill.SkillTargetType.TARGET_SUMMON,
- // L2Skill.SkillTargetType.TARGET_AURA_CORPSE_MOB,
- // L2Skill.SkillTargetType.TARGET_CORPSE,
- // L2Skill.SkillTargetType.TARGET_UNDEAD,
- // L2Skill.SkillTargetType.TARGET_AREA_UNDEAD,
- // L2Skill.SkillTargetType.TARGET_CORPSE_ALLY, // XXX add support for faction ally target
- // L2Skill.SkillTargetType.TARGET_CORPSE_PLAYER,
- // L2Skill.SkillTargetType.TARGET_CORPSE_PET,
- // L2Skill.SkillTargetType.TARGET_AREA_CORPSE_MOB,
- // L2Skill.SkillTargetType.TARGET_CORPSE_MOB,
- L2Skill.SkillTargetType.TARGET_UNLOCKABLE,
- L2Skill.SkillTargetType.TARGET_HOLY,
- L2Skill.SkillTargetType.TARGET_FLAGPOLE,
- // L2Skill.SkillTargetType.TARGET_PARTY_MEMBER,
- // L2Skill.SkillTargetType.TARGET_PARTY_OTHER,
- // L2Skill.SkillTargetType.TARGET_ENEMY_SUMMON,
- L2Skill.SkillTargetType.TARGET_OWNER_PET,
- L2Skill.SkillTargetType.TARGET_GROUND,
- L2Skill.SkillTargetType.TARGET_GROUND_PHYSICS,
- // L2Skill.SkillTargetType.TARGET_PARTY_NOTME,
- // L2Skill.SkillTargetType.TARGET_AREA_SUMMON,
- };
- return !ExtUtil.contains(disallowedTargetTypesNpc, sk.getTargetType()) && sk.checkCondition(actor, target, false);
- }
- public final static L2Skill getTopSkill(final L2CharacterAI ai, final L2Character target, final double distance, final L2Skill... skills)
- {
- if(skills == null || skills.length == 0 || target == null)
- return null;
- final RndSelector<L2Skill> selector = new RndSelector<>(skills.length);
- for(L2Skill skill : skills)
- {
- if(skill == null || !canUseSkill(ai, skill, target, distance))
- continue;
- int weight = (int) -Math.abs(skill.getCastRange() - distance);
- if(skill.getCastRange() >= distance)
- weight += 1000000;
- else if(skill.isUseableWithoutTarget() && skill.getTargetList(ai.getActor(), false, target).length == 0)
- continue;
- selector.add(skill, weight);
- }
- return selector.select();
- }
- public final static L2Skill getTopSkill(final L2CharacterAI ai, final L2Character target, final double distance, final FastList<L2Skill> skills)
- {
- if(skills == null || skills.isEmpty() || target == null)
- return null;
- final RndSelector<L2Skill> selector = new RndSelector<>(skills.size());
- for(L2Skill skill : skills)
- {
- if(skill == null || !canUseSkill(ai, skill, target, distance))
- continue;
- int weight = (int) -Math.abs(skill.getCastRange() - distance);
- if(skill.getCastRange() >= distance)
- weight += 1000000;
- else if(skill.isUseableWithoutTarget() && skill.getTargetList(ai.getActor(), false, target).length == 0)
- continue;
- selector.add(skill, weight);
- }
- return selector.select();
- }
- public static L2Skill selectTopSkillByBuff(final L2Character target, final FastList<L2Skill> skills)
- {
- if(skills == null || skills.isEmpty())
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
- double weight;
- for(final L2Skill skill : skills)
- {
- if(skill.getEffects(target, target, null, true) == null || skill.getEffects(target, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
- continue;
- if((weight = skill.getPower()) <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public static L2Skill selectTopSkillByHeal(L2Character target, FastList<L2Skill> skills)
- {
- if(skills == null || skills.isEmpty())
- return null;
- final double hp_reduced = target.getMaxHp() - target.getCurrentHp();
- if(hp_reduced < 1)
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
- double weight;
- for(final L2Skill skill : skills)
- {
- if((weight = Math.abs(skill.getPower() - hp_reduced)) <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public static FastList<L2Skill> selectUsableSkills(L2CharacterAI ai, L2Character target, double distance, FastList<L2Skill> skills)
- {
- if (skills == null || skills.isEmpty() || target == null)
- return null;
- FastList<L2Skill> ret = new FastList<>();
- int usable = 0;
- for(final L2Skill skill : skills)
- {
- if(canUseSkill(ai, skill, target, distance))
- {
- ret.add(skill);
- usable++;
- }
- }
- return ret;
- }
- public static L2Skill selectTopSkillByDamage(L2Character actor, L2Character target, double distance, FastList<L2Skill> skills)
- {
- if(skills == null || skills.isEmpty())
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
- double weight;
- for(final L2Skill skill : skills)
- {
- weight = skill.getSimpleDamage(actor, target) * skill.getCastRange() / distance;
- if(weight <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- public static L2Skill selectTopSkillByDebuff(L2Character actor, L2Character target, double distance, FastList<L2Skill> skills)
- {
- if(skills == null || skills.isEmpty())
- return null;
- final RndSelector<L2Skill> rnd = new RndSelector<>(skills.size());
- double weight;
- for(final L2Skill skill : skills)
- {
- if(skill.getEffects(actor, target, null, true) == null || skill.getEffects(actor, target, null, true).length < 1 || target.getFirstEffect(skill) != null)
- continue;
- if((weight = 100f * skill.getCastRange() / distance) <= 0)
- weight = 1;
- rnd.add(skill, (int) weight);
- }
- return rnd.select();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement