L2Weapon.java 12 KB

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