L2Weapon.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server 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 GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.items;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import com.l2jserver.gameserver.handler.ISkillHandler;
  25. import com.l2jserver.gameserver.handler.SkillHandler;
  26. import com.l2jserver.gameserver.model.L2Object;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.model.actor.L2Character;
  29. import com.l2jserver.gameserver.model.actor.L2Npc;
  30. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  31. import com.l2jserver.gameserver.model.conditions.Condition;
  32. import com.l2jserver.gameserver.model.conditions.ConditionGameChance;
  33. import com.l2jserver.gameserver.model.effects.L2Effect;
  34. import com.l2jserver.gameserver.model.holders.SkillHolder;
  35. import com.l2jserver.gameserver.model.items.type.L2WeaponType;
  36. import com.l2jserver.gameserver.model.quest.Quest;
  37. import com.l2jserver.gameserver.model.quest.Quest.QuestEventType;
  38. import com.l2jserver.gameserver.model.skills.L2Skill;
  39. import com.l2jserver.gameserver.model.stats.Env;
  40. import com.l2jserver.gameserver.model.stats.Formulas;
  41. import com.l2jserver.util.StringUtil;
  42. /**
  43. * This class is dedicated to the management of weapons.
  44. */
  45. public final class L2Weapon extends L2Item
  46. {
  47. private final L2WeaponType _type;
  48. private final boolean _isMagicWeapon;
  49. private final int _rndDam;
  50. private final int _soulShotCount;
  51. private final int _spiritShotCount;
  52. private final int _mpConsume;
  53. /**
  54. * Skill that activates when item is enchanted +4 (for duals).
  55. */
  56. private SkillHolder _enchant4Skill = null;
  57. private final int _changeWeaponId;
  58. // Attached skills for Special Abilities
  59. private SkillHolder _skillsOnMagic;
  60. private Condition _skillsOnMagicCondition = null;
  61. private SkillHolder _skillsOnCrit;
  62. private Condition _skillsOnCritCondition = null;
  63. private final int _reducedSoulshot;
  64. private final int _reducedSoulshotChance;
  65. private final int _reducedMpConsume;
  66. private final int _reducedMpConsumeChance;
  67. private final boolean _isForceEquip;
  68. private final boolean _isAttackWeapon;
  69. private final boolean _useWeaponSkillsOnly;
  70. /**
  71. * Constructor for Weapon.
  72. * @param set the StatsSet designating the set of couples (key,value) characterizing the weapon.
  73. */
  74. public L2Weapon(StatsSet set)
  75. {
  76. super(set);
  77. _type = L2WeaponType.valueOf(set.getString("weapon_type", "none").toUpperCase());
  78. _type1 = L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE;
  79. _type2 = L2Item.TYPE2_WEAPON;
  80. _isMagicWeapon = set.getBool("is_magic_weapon", false);
  81. _soulShotCount = set.getInteger("soulshots", 0);
  82. _spiritShotCount = set.getInteger("spiritshots", 0);
  83. _rndDam = set.getInteger("random_damage", 0);
  84. _mpConsume = set.getInteger("mp_consume", 0);
  85. String[] reduced_soulshots = set.getString("reduced_soulshot", "").split(",");
  86. _reducedSoulshotChance = (reduced_soulshots.length == 2) ? Integer.parseInt(reduced_soulshots[0]) : 0;
  87. _reducedSoulshot = (reduced_soulshots.length == 2) ? Integer.parseInt(reduced_soulshots[1]) : 0;
  88. String[] reduced_mpconsume = set.getString("reduced_mp_consume", "").split(",");
  89. _reducedMpConsumeChance = (reduced_mpconsume.length == 2) ? Integer.parseInt(reduced_mpconsume[0]) : 0;
  90. _reducedMpConsume = (reduced_mpconsume.length == 2) ? Integer.parseInt(reduced_mpconsume[1]) : 0;
  91. String skill = set.getString("enchant4_skill", null);
  92. if (skill != null)
  93. {
  94. String[] info = skill.split("-");
  95. if ((info != null) && (info.length == 2))
  96. {
  97. int id = 0;
  98. int level = 0;
  99. try
  100. {
  101. id = Integer.parseInt(info[0]);
  102. level = Integer.parseInt(info[1]);
  103. }
  104. catch (Exception nfe)
  105. {
  106. // Incorrect syntax, dont add new skill
  107. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon enchant skills! item ", toString()));
  108. }
  109. if ((id > 0) && (level > 0))
  110. {
  111. _enchant4Skill = new SkillHolder(id, level);
  112. }
  113. }
  114. }
  115. skill = set.getString("onmagic_skill", null);
  116. if (skill != null)
  117. {
  118. String[] info = skill.split("-");
  119. final int chance = set.getInteger("onmagic_chance", 100);
  120. if ((info != null) && (info.length == 2))
  121. {
  122. int id = 0;
  123. int level = 0;
  124. try
  125. {
  126. id = Integer.parseInt(info[0]);
  127. level = Integer.parseInt(info[1]);
  128. }
  129. catch (Exception nfe)
  130. {
  131. // Incorrect syntax, don't add new skill
  132. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon onmagic skills! item ", toString()));
  133. }
  134. if ((id > 0) && (level > 0) && (chance > 0))
  135. {
  136. _skillsOnMagic = new SkillHolder(id, level);
  137. _skillsOnMagicCondition = new ConditionGameChance(chance);
  138. }
  139. }
  140. }
  141. skill = set.getString("oncrit_skill", null);
  142. if (skill != null)
  143. {
  144. String[] info = skill.split("-");
  145. final int chance = set.getInteger("oncrit_chance", 100);
  146. if ((info != null) && (info.length == 2))
  147. {
  148. int id = 0;
  149. int level = 0;
  150. try
  151. {
  152. id = Integer.parseInt(info[0]);
  153. level = Integer.parseInt(info[1]);
  154. }
  155. catch (Exception nfe)
  156. {
  157. // Incorrect syntax, don't add new skill
  158. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncrit skills! item ", toString()));
  159. }
  160. if ((id > 0) && (level > 0) && (chance > 0))
  161. {
  162. _skillsOnCrit = new SkillHolder(id, level);
  163. _skillsOnCritCondition = new ConditionGameChance(chance);
  164. }
  165. }
  166. }
  167. _changeWeaponId = set.getInteger("change_weaponId", 0);
  168. _isForceEquip = set.getBool("isForceEquip", false);
  169. _isAttackWeapon = set.getBool("isAttackWeapon", true);
  170. _useWeaponSkillsOnly = set.getBool("useWeaponSkillsOnly", false);
  171. }
  172. /**
  173. * @return the type of Weapon
  174. */
  175. @Override
  176. public L2WeaponType getItemType()
  177. {
  178. return _type;
  179. }
  180. /**
  181. * @return the ID of the Etc item after applying the mask.
  182. */
  183. @Override
  184. public int getItemMask()
  185. {
  186. return getItemType().mask();
  187. }
  188. /**
  189. * @return {@code true} if the weapon is magic, {@code false} otherwise.
  190. */
  191. public boolean isMagicWeapon()
  192. {
  193. return _isMagicWeapon;
  194. }
  195. /**
  196. * @return the quantity of SoulShot used.
  197. */
  198. public int getSoulShotCount()
  199. {
  200. return _soulShotCount;
  201. }
  202. /**
  203. * @return the quantity of SpiritShot used.
  204. */
  205. public int getSpiritShotCount()
  206. {
  207. return _spiritShotCount;
  208. }
  209. /**
  210. * @return the reduced quantity of SoultShot used.
  211. */
  212. public int getReducedSoulShot()
  213. {
  214. return _reducedSoulshot;
  215. }
  216. /**
  217. * @return the chance to use Reduced SoultShot.
  218. */
  219. public int getReducedSoulShotChance()
  220. {
  221. return _reducedSoulshotChance;
  222. }
  223. /**
  224. * @return the random damage inflicted by the weapon.
  225. */
  226. public int getRandomDamage()
  227. {
  228. return _rndDam;
  229. }
  230. /**
  231. * @return the MP consumption with the weapon.
  232. */
  233. public int getMpConsume()
  234. {
  235. return _mpConsume;
  236. }
  237. /**
  238. * @return the reduced MP consumption with the weapon.
  239. */
  240. public int getReducedMpConsume()
  241. {
  242. return _reducedMpConsume;
  243. }
  244. /**
  245. * @return the chance to use getReducedMpConsume()
  246. */
  247. public int getReducedMpConsumeChance()
  248. {
  249. return _reducedMpConsumeChance;
  250. }
  251. /**
  252. * @return the skill that player get when has equipped weapon +4 or more (for duals SA).
  253. */
  254. @Override
  255. public L2Skill getEnchant4Skill()
  256. {
  257. if (_enchant4Skill == null)
  258. {
  259. return null;
  260. }
  261. return _enchant4Skill.getSkill();
  262. }
  263. /**
  264. * @return the Id in which weapon this weapon can be changed.
  265. */
  266. public int getChangeWeaponId()
  267. {
  268. return _changeWeaponId;
  269. }
  270. /**
  271. * @return {@code true} if the weapon is force equip, {@code false} otherwise.
  272. */
  273. public boolean isForceEquip()
  274. {
  275. return _isForceEquip;
  276. }
  277. /**
  278. * @return {@code true} if the weapon is attack weapon, {@code false} otherwise.
  279. */
  280. public boolean isAttackWeapon()
  281. {
  282. return _isAttackWeapon;
  283. }
  284. /**
  285. * @return {@code true} if the weapon is skills only, {@code false} otherwise.
  286. */
  287. public boolean useWeaponSkillsOnly()
  288. {
  289. return _useWeaponSkillsOnly;
  290. }
  291. /**
  292. * @param caster the L2Character pointing out the caster
  293. * @param target the L2Character pointing out the target
  294. * @param crit the boolean tells whether the hit was critical
  295. * @return the effects of skills associated with the item to be triggered onHit.
  296. */
  297. public List<L2Effect> getSkillEffects(L2Character caster, L2Character target, boolean crit)
  298. {
  299. if ((_skillsOnCrit == null) || !crit)
  300. {
  301. return Collections.<L2Effect> emptyList();
  302. }
  303. final L2Skill onCritSkill = _skillsOnCrit.getSkill();
  304. if (_skillsOnCritCondition != null)
  305. {
  306. Env env = new Env();
  307. env.setCharacter(caster);
  308. env.setTarget(target);
  309. env.setSkill(onCritSkill);
  310. if (!_skillsOnCritCondition.test(env))
  311. {
  312. // Chance not met
  313. return Collections.<L2Effect> emptyList();
  314. }
  315. }
  316. if (!onCritSkill.checkCondition(caster, target, false))
  317. {
  318. // Skill condition not met
  319. return Collections.<L2Effect> emptyList();
  320. }
  321. final byte shld = Formulas.calcShldUse(caster, target, onCritSkill);
  322. if (!Formulas.calcSkillSuccess(caster, target, onCritSkill, shld, false, false, false))
  323. {
  324. // These skills should not work on RaidBoss
  325. return Collections.<L2Effect> emptyList();
  326. }
  327. if (target.getFirstEffect(onCritSkill.getId()) != null)
  328. {
  329. target.getFirstEffect(onCritSkill.getId()).exit();
  330. }
  331. final List<L2Effect> effects = new ArrayList<>();
  332. for (L2Effect e : onCritSkill.getEffects(caster, target, new Env(shld, false, false, false)))
  333. {
  334. effects.add(e);
  335. }
  336. return effects;
  337. }
  338. /**
  339. * @param caster the L2Character pointing out the caster
  340. * @param target the L2Character pointing out the target
  341. * @param trigger the L2Skill pointing out the skill triggering this action
  342. * @return the effects of skills associated with the item to be triggered onMagic.
  343. */
  344. public boolean getSkillEffects(L2Character caster, L2Character target, L2Skill trigger)
  345. {
  346. if (_skillsOnMagic == null)
  347. {
  348. return false;
  349. }
  350. final L2Skill onMagicSkill = _skillsOnMagic.getSkill();
  351. // Trigger only if both are good or bad magic.
  352. if (trigger.isBad() != onMagicSkill.isBad())
  353. {
  354. return false;
  355. }
  356. // No Trigger if not Magic Skill
  357. if (!trigger.isMagic() && !onMagicSkill.isMagic())
  358. {
  359. return false;
  360. }
  361. if (_skillsOnMagicCondition != null)
  362. {
  363. Env env = new Env();
  364. env.setCharacter(caster);
  365. env.setTarget(target);
  366. env.setSkill(onMagicSkill);
  367. if (!_skillsOnMagicCondition.test(env))
  368. {
  369. // Chance not met
  370. return false;
  371. }
  372. }
  373. if (!onMagicSkill.checkCondition(caster, target, false))
  374. {
  375. // Skill condition not met
  376. return false;
  377. }
  378. final byte shld = Formulas.calcShldUse(caster, target, onMagicSkill);
  379. if (onMagicSkill.isBad() && !Formulas.calcSkillSuccess(caster, target, onMagicSkill, shld, false, false, false))
  380. {
  381. return false;
  382. }
  383. L2Character[] targets =
  384. {
  385. target
  386. };
  387. // Launch the magic skill and calculate its effects
  388. // Get the skill handler corresponding to the skill type
  389. final ISkillHandler handler = SkillHandler.getInstance().getHandler(onMagicSkill.getSkillType());
  390. if (handler != null)
  391. {
  392. handler.useSkill(caster, onMagicSkill, targets);
  393. }
  394. else
  395. {
  396. onMagicSkill.useSkill(caster, targets);
  397. }
  398. // notify quests of a skill use
  399. if (caster instanceof L2PcInstance)
  400. {
  401. // Mobs in range 1000 see spell
  402. Collection<L2Object> objs = caster.getKnownList().getKnownObjects().values();
  403. for (L2Object spMob : objs)
  404. {
  405. if (spMob instanceof L2Npc)
  406. {
  407. L2Npc npcMob = (L2Npc) spMob;
  408. if (npcMob.getTemplate().getEventQuests(QuestEventType.ON_SKILL_SEE) != null)
  409. {
  410. for (Quest quest : npcMob.getTemplate().getEventQuests(QuestEventType.ON_SKILL_SEE))
  411. {
  412. quest.notifySkillSee(npcMob, caster.getActingPlayer(), onMagicSkill, targets, false);
  413. }
  414. }
  415. }
  416. }
  417. }
  418. return true;
  419. }
  420. }