L2Weapon.java 13 KB

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