L2Weapon.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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.model.item;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import javolution.util.FastList;
  20. import com.l2jserver.gameserver.handler.ISkillHandler;
  21. import com.l2jserver.gameserver.handler.SkillHandler;
  22. import com.l2jserver.gameserver.model.L2Effect;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.L2Skill;
  25. import com.l2jserver.gameserver.model.StatsSet;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.L2Npc;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.item.instance.L2ItemInstance;
  30. import com.l2jserver.gameserver.model.item.type.L2WeaponType;
  31. import com.l2jserver.gameserver.model.quest.Quest;
  32. import com.l2jserver.gameserver.skills.Env;
  33. import com.l2jserver.gameserver.skills.Formulas;
  34. import com.l2jserver.gameserver.skills.SkillHolder;
  35. import com.l2jserver.gameserver.skills.conditions.Condition;
  36. import com.l2jserver.gameserver.skills.conditions.ConditionGameChance;
  37. import com.l2jserver.gameserver.skills.funcs.Func;
  38. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  39. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  40. import com.l2jserver.util.StringUtil;
  41. /**
  42. * This class is dedicated to the management of weapons.
  43. *
  44. * @version $Revision: 1.4.2.3.2.5 $ $Date: 2005/04/02 15:57:51 $
  45. */
  46. public final class L2Weapon extends L2Item
  47. {
  48. private final L2WeaponType _type;
  49. private final boolean _isMagicWeapon;
  50. private final int _rndDam;
  51. private final int _soulShotCount;
  52. private final int _spiritShotCount;
  53. private final int _mpConsume;
  54. private SkillHolder _enchant4Skill = null; // skill that activates when item is enchanted +4 (for duals)
  55. private final int _changeWeaponId;
  56. // private final String[] _skill;
  57. // Attached skills for Special Abilities
  58. private SkillHolder _skillsOnCast;
  59. private Condition _skillsOnCastCondition = null;
  60. private SkillHolder _skillsOnCrit;
  61. private Condition _skillsOnCritCondition = null;
  62. private final int _reuseDelay;
  63. private final boolean _isForceEquip;
  64. private final boolean _isAttackWeapon;
  65. private final boolean _useWeaponSkillsOnly;
  66. /**
  67. * Constructor for Weapon.<BR><BR>
  68. * <U><I>Variables filled :</I></U><BR>
  69. * <LI>_soulShotCount & _spiritShotCount</LI>
  70. * <LI>_pDam & _mDam & _rndDam</LI>
  71. * <LI>_critical</LI>
  72. * <LI>_hitModifier</LI>
  73. * <LI>_avoidModifier</LI>
  74. * <LI>_shieldDes & _shieldDefRate</LI>
  75. * <LI>_atkSpeed & _AtkReuse</LI>
  76. * <LI>_mpConsume</LI>
  77. * @param set : StatsSet designating the set of couples (key,value) characterizing the armor
  78. * @see L2Item constructor
  79. */
  80. public L2Weapon(StatsSet set)
  81. {
  82. super(set);
  83. _type = L2WeaponType.valueOf(set.getString("weapon_type", "none").toUpperCase());
  84. _type1 = L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE;
  85. _type2 = L2Item.TYPE2_WEAPON;
  86. _isMagicWeapon = set.getBool("is_magic_weapon", false);
  87. _soulShotCount = set.getInteger("soulshots", 0);
  88. _spiritShotCount = set.getInteger("spiritshots", 0);
  89. _rndDam = set.getInteger("random_damage", 0);
  90. _mpConsume = set.getInteger("mp_consume", 0);
  91. _reuseDelay = set.getInteger("reuse_delay", 0);
  92. String skill = set.getString("enchant4_skill", null);
  93. if (skill != null)
  94. {
  95. String[] info = skill.split("-");
  96. if (info != null && info.length == 2)
  97. {
  98. int id = 0;
  99. int level = 0;
  100. try
  101. {
  102. id = Integer.parseInt(info[0]);
  103. level = Integer.parseInt(info[1]);
  104. }
  105. catch (Exception nfe)
  106. {
  107. // Incorrect syntax, dont add new skill
  108. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon enchant skills! item ", this.toString()));
  109. }
  110. if (id > 0 && level > 0)
  111. _enchant4Skill = new SkillHolder(id, level);
  112. }
  113. }
  114. skill = set.getString("oncast_skill", null);
  115. if (skill != null)
  116. {
  117. String[] info = skill.split("-");
  118. String infochance = set.getString("oncast_chance", null);
  119. if (info != null && info.length == 2)
  120. {
  121. int id = 0;
  122. int level = 0;
  123. int chance = 0;
  124. try
  125. {
  126. id = Integer.parseInt(info[0]);
  127. level = Integer.parseInt(info[1]);
  128. if (infochance != null)
  129. chance = Integer.parseInt(infochance);
  130. }
  131. catch (Exception nfe)
  132. {
  133. // Incorrect syntax, dont add new skill
  134. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncast skills! item ", this.toString()));
  135. }
  136. if (id > 0 && level > 0 && chance > 0)
  137. {
  138. _skillsOnCast = new SkillHolder(id, level);
  139. if (infochance != null)
  140. _skillsOnCastCondition = new ConditionGameChance(chance);
  141. }
  142. }
  143. }
  144. skill = set.getString("oncrit_skill", null);
  145. if (skill != null)
  146. {
  147. String[] info = skill.split("-");
  148. String infochance = set.getString("oncrit_chance", null);
  149. if (info != null && info.length == 2)
  150. {
  151. int id = 0;
  152. int level = 0;
  153. int chance = 0;
  154. try
  155. {
  156. id = Integer.parseInt(info[0]);
  157. level = Integer.parseInt(info[1]);
  158. if (infochance != null)
  159. chance = Integer.parseInt(infochance);
  160. }
  161. catch (Exception nfe)
  162. {
  163. // Incorrect syntax, dont add new skill
  164. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncrit skills! item ", this.toString()));
  165. }
  166. if (id > 0 && level > 0 && chance > 0)
  167. {
  168. _skillsOnCrit = new SkillHolder(id, level);
  169. if (infochance != null)
  170. _skillsOnCritCondition = new ConditionGameChance(chance);
  171. }
  172. }
  173. }
  174. _changeWeaponId = set.getInteger("change_weaponId", 0);
  175. _isForceEquip = set.getBool("isForceEquip", false);
  176. _isAttackWeapon = set.getBool("isAttackWeapon", true);
  177. _useWeaponSkillsOnly = set.getBool("useWeaponSkillsOnly", false);
  178. }
  179. /**
  180. * Returns the type of Weapon
  181. * @return L2WeaponType
  182. */
  183. @Override
  184. public L2WeaponType getItemType()
  185. {
  186. return _type;
  187. }
  188. /**
  189. * Returns the ID of the Etc item after applying the mask.
  190. * @return int : ID of the Weapon
  191. */
  192. @Override
  193. public int getItemMask()
  194. {
  195. return getItemType().mask();
  196. }
  197. /**
  198. * Returns true if L2Weapon is magic type.
  199. * @return boolean
  200. */
  201. public boolean isMagicWeapon()
  202. {
  203. return _isMagicWeapon;
  204. }
  205. /**
  206. * Returns the quantity of SoulShot used.
  207. * @return int
  208. */
  209. public int getSoulShotCount()
  210. {
  211. return _soulShotCount;
  212. }
  213. /**
  214. * Returns the quatity of SpiritShot used.
  215. * @return int
  216. */
  217. public int getSpiritShotCount()
  218. {
  219. return _spiritShotCount;
  220. }
  221. /**
  222. * Returns the random damage inflicted by the weapon
  223. * @return int
  224. */
  225. public int getRandomDamage()
  226. {
  227. return _rndDam;
  228. }
  229. /**
  230. * Return the Reuse Delay of the L2Weapon.<BR><BR>
  231. * @return int
  232. */
  233. public int getReuseDelay()
  234. {
  235. return _reuseDelay;
  236. }
  237. /**
  238. * Returns the MP consumption with the weapon
  239. * @return int
  240. */
  241. public int getMpConsume()
  242. {
  243. return _mpConsume;
  244. }
  245. /**
  246. * Returns skill that player get when has equiped weapon +4 or more (for duals SA)
  247. * @return
  248. */
  249. public L2Skill getEnchant4Skill()
  250. {
  251. if (_enchant4Skill == null)
  252. return null;
  253. return _enchant4Skill.getSkill();
  254. }
  255. /**
  256. * Returns the Id in wich weapon this weapon can be changed
  257. * @return
  258. */
  259. public int getChangeWeaponId()
  260. {
  261. return _changeWeaponId;
  262. }
  263. public boolean isForceEquip()
  264. {
  265. return _isForceEquip;
  266. }
  267. public boolean isAttackWeapon()
  268. {
  269. return _isAttackWeapon;
  270. }
  271. public boolean useWeaponSkillsOnly()
  272. {
  273. return _useWeaponSkillsOnly;
  274. }
  275. /**
  276. * Returns array of Func objects containing the list of functions used by the weapon
  277. * @param instance : L2ItemInstance pointing out the weapon
  278. * @param player : L2Character pointing out the player
  279. * @return Func[] : array of functions
  280. */
  281. @Override
  282. public Func[] getStatFuncs(L2ItemInstance instance, L2Character player)
  283. {
  284. if (_funcTemplates == null || _funcTemplates.length == 0)
  285. return _emptyFunctionSet;
  286. ArrayList<Func> funcs = new ArrayList<Func>(_funcTemplates.length);
  287. Env env = new Env();
  288. env.player = player;
  289. env.item = instance;
  290. Func f;
  291. for (FuncTemplate t : _funcTemplates)
  292. {
  293. f = t.getFunc(env, instance);
  294. if (f != null)
  295. funcs.add(f);
  296. }
  297. return funcs.toArray(new Func[funcs.size()]);
  298. }
  299. /**
  300. * Returns effects of skills associated with the item to be triggered onHit.
  301. * @param caster : L2Character pointing out the caster
  302. * @param target : L2Character pointing out the target
  303. * @param crit : boolean tells whether the hit was critical
  304. * @return L2Effect[] : array of effects generated by the skill
  305. */
  306. public L2Effect[] getSkillEffects(L2Character caster, L2Character target, boolean crit)
  307. {
  308. if (_skillsOnCrit == null || !crit)
  309. return _emptyEffectSet;
  310. List<L2Effect> effects = new FastList<L2Effect>();
  311. if (_skillsOnCritCondition != null)
  312. {
  313. Env env = new Env();
  314. env.player = caster;
  315. env.target = target;
  316. env.skill = _skillsOnCrit.getSkill();
  317. if (!_skillsOnCritCondition.test(env))
  318. return _emptyEffectSet; // Skill condition not met
  319. }
  320. byte shld = Formulas.calcShldUse(caster, target, _skillsOnCrit.getSkill());
  321. if (!Formulas.calcSkillSuccess(caster, target, _skillsOnCrit.getSkill(), shld, false, false, false))
  322. return _emptyEffectSet; // These skills should not work on RaidBoss
  323. if (target.getFirstEffect(_skillsOnCrit.getSkill().getId()) != null)
  324. target.getFirstEffect(_skillsOnCrit.getSkill().getId()).exit();
  325. for (L2Effect e : _skillsOnCrit.getSkill().getEffects(caster, target, new Env(shld, false, false, false)))
  326. effects.add(e);
  327. if (effects.isEmpty())
  328. return _emptyEffectSet;
  329. return effects.toArray(new L2Effect[effects.size()]);
  330. }
  331. /**
  332. * Returns effects of skills associated with the item to be triggered onCast.
  333. * @param caster : L2Character pointing out the caster
  334. * @param target : L2Character pointing out the target
  335. * @param trigger : L2Skill pointing out the skill triggering this action
  336. * @return L2Effect[] : array of effects generated by the skill
  337. */
  338. public L2Effect[] getSkillEffects(L2Character caster, L2Character target, L2Skill trigger)
  339. {
  340. if (_skillsOnCast == null)
  341. return _emptyEffectSet;
  342. if (trigger.isOffensive() != _skillsOnCast.getSkill().isOffensive())
  343. return _emptyEffectSet; // Trigger only same type of skill
  344. if (trigger.isToggle() && _skillsOnCast.getSkill().getSkillType() == L2SkillType.BUFF)
  345. return _emptyEffectSet; // No buffing with toggle skills
  346. if (!trigger.isMagic() && _skillsOnCast.getSkill().getSkillType() == L2SkillType.BUFF)
  347. return _emptyEffectSet; // No buffing with not magic skills
  348. if (_skillsOnCastCondition != null)
  349. {
  350. Env env = new Env();
  351. env.player = caster;
  352. env.target = target;
  353. env.skill = _skillsOnCast.getSkill();
  354. if (!_skillsOnCastCondition.test(env))
  355. return _emptyEffectSet;
  356. }
  357. byte shld = Formulas.calcShldUse(caster, target, _skillsOnCast.getSkill());
  358. if (_skillsOnCast.getSkill().isOffensive() && !Formulas.calcSkillSuccess(caster, target, _skillsOnCast.getSkill(), shld, false, false, false))
  359. return _emptyEffectSet;
  360. // Get the skill handler corresponding to the skill type
  361. ISkillHandler handler = SkillHandler.getInstance().getHandler(_skillsOnCast.getSkill().getSkillType());
  362. L2Character[] targets = new L2Character[1];
  363. targets[0] = target;
  364. // Launch the magic skill and calculate its effects
  365. if (handler != null)
  366. handler.useSkill(caster, _skillsOnCast.getSkill(), targets);
  367. else
  368. _skillsOnCast.getSkill().useSkill(caster, targets);
  369. // notify quests of a skill use
  370. if (caster instanceof L2PcInstance)
  371. {
  372. // Mobs in range 1000 see spell
  373. Collection<L2Object> objs = caster.getKnownList().getKnownObjects().values();
  374. for (L2Object spMob : objs)
  375. {
  376. if (spMob instanceof L2Npc)
  377. {
  378. L2Npc npcMob = (L2Npc) spMob;
  379. if (npcMob.getTemplate().getEventQuests(Quest.QuestEventType.ON_SKILL_SEE) != null)
  380. {
  381. for (Quest quest : npcMob.getTemplate().getEventQuests(Quest.QuestEventType.ON_SKILL_SEE))
  382. {
  383. quest.notifySkillSee(npcMob, (L2PcInstance) caster, _skillsOnCast.getSkill(), targets, false);
  384. }
  385. }
  386. }
  387. }
  388. }
  389. return _emptyEffectSet;
  390. }
  391. }