L2Weapon.java 12 KB

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