L2Weapon.java 14 KB

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