L2Weapon.java 12 KB

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