L2Weapon.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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.item;
  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.L2Effect;
  23. import com.l2jserver.gameserver.model.L2Object;
  24. import com.l2jserver.gameserver.model.L2Skill;
  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.item.instance.L2ItemInstance;
  30. import com.l2jserver.gameserver.model.item.type.L2WeaponType;
  31. import com.l2jserver.gameserver.model.quest.Quest;
  32. import com.l2jserver.gameserver.skills.Env;
  33. import com.l2jserver.gameserver.skills.Formulas;
  34. import com.l2jserver.gameserver.skills.SkillHolder;
  35. import com.l2jserver.gameserver.skills.conditions.Condition;
  36. import com.l2jserver.gameserver.skills.conditions.ConditionGameChance;
  37. import com.l2jserver.gameserver.skills.funcs.Func;
  38. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  39. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  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 _skillsOnCast;
  59. private Condition _skillsOnCastCondition = null;
  60. private SkillHolder _skillsOnCrit;
  61. private Condition _skillsOnCritCondition = null;
  62. private final int _reuseDelay;
  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. * @see L2Item constructor
  70. */
  71. public L2Weapon(StatsSet set)
  72. {
  73. super(set);
  74. _type = L2WeaponType.valueOf(set.getString("weapon_type", "none").toUpperCase());
  75. _type1 = L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE;
  76. _type2 = L2Item.TYPE2_WEAPON;
  77. _isMagicWeapon = set.getBool("is_magic_weapon", false);
  78. _soulShotCount = set.getInteger("soulshots", 0);
  79. _spiritShotCount = set.getInteger("spiritshots", 0);
  80. _rndDam = set.getInteger("random_damage", 0);
  81. _mpConsume = set.getInteger("mp_consume", 0);
  82. _reuseDelay = set.getInteger("reuse_delay", 0);
  83. String skill = set.getString("enchant4_skill", null);
  84. if (skill != null)
  85. {
  86. String[] info = skill.split("-");
  87. if ((info != null) && (info.length == 2))
  88. {
  89. int id = 0;
  90. int level = 0;
  91. try
  92. {
  93. id = Integer.parseInt(info[0]);
  94. level = Integer.parseInt(info[1]);
  95. }
  96. catch (Exception nfe)
  97. {
  98. // Incorrect syntax, dont add new skill
  99. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon enchant skills! item ", toString()));
  100. }
  101. if ((id > 0) && (level > 0))
  102. {
  103. _enchant4Skill = new SkillHolder(id, level);
  104. }
  105. }
  106. }
  107. skill = set.getString("oncast_skill", null);
  108. if (skill != null)
  109. {
  110. String[] info = skill.split("-");
  111. String infochance = set.getString("oncast_chance", null);
  112. if ((info != null) && (info.length == 2))
  113. {
  114. int id = 0;
  115. int level = 0;
  116. int chance = 0;
  117. try
  118. {
  119. id = Integer.parseInt(info[0]);
  120. level = Integer.parseInt(info[1]);
  121. if (infochance != null)
  122. {
  123. chance = Integer.parseInt(infochance);
  124. }
  125. }
  126. catch (Exception nfe)
  127. {
  128. // Incorrect syntax, dont add new skill
  129. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncast skills! item ", toString()));
  130. }
  131. if ((id > 0) && (level > 0) && (chance > 0))
  132. {
  133. _skillsOnCast = new SkillHolder(id, level);
  134. if (infochance != null)
  135. {
  136. _skillsOnCastCondition = new ConditionGameChance(chance);
  137. }
  138. }
  139. }
  140. }
  141. skill = set.getString("oncrit_skill", null);
  142. if (skill != null)
  143. {
  144. String[] info = skill.split("-");
  145. String infochance = set.getString("oncrit_chance", null);
  146. if ((info != null) && (info.length == 2))
  147. {
  148. int id = 0;
  149. int level = 0;
  150. int chance = 0;
  151. try
  152. {
  153. id = Integer.parseInt(info[0]);
  154. level = Integer.parseInt(info[1]);
  155. if (infochance != null)
  156. {
  157. chance = Integer.parseInt(infochance);
  158. }
  159. }
  160. catch (Exception nfe)
  161. {
  162. // Incorrect syntax, dont add new skill
  163. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncrit skills! item ", toString()));
  164. }
  165. if ((id > 0) && (level > 0) && (chance > 0))
  166. {
  167. _skillsOnCrit = new SkillHolder(id, level);
  168. if (infochance != null)
  169. {
  170. _skillsOnCritCondition = new ConditionGameChance(chance);
  171. }
  172. }
  173. }
  174. }
  175. _changeWeaponId = set.getInteger("change_weaponId", 0);
  176. _isForceEquip = set.getBool("isForceEquip", false);
  177. _isAttackWeapon = set.getBool("isAttackWeapon", true);
  178. _useWeaponSkillsOnly = set.getBool("useWeaponSkillsOnly", false);
  179. }
  180. /**
  181. * @return the type of Weapon
  182. */
  183. @Override
  184. public L2WeaponType 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. 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 random damage inflicted by the weapon.
  219. */
  220. public int getRandomDamage()
  221. {
  222. return _rndDam;
  223. }
  224. /**
  225. * @return the Reuse Delay of the L2Weapon.
  226. */
  227. public int getReuseDelay()
  228. {
  229. return _reuseDelay;
  230. }
  231. /**
  232. * @return the MP consumption with the weapon.
  233. */
  234. public int getMpConsume()
  235. {
  236. return _mpConsume;
  237. }
  238. /**
  239. * @return the skill that player get when has equipped weapon +4 or more (for duals SA).
  240. */
  241. public L2Skill getEnchant4Skill()
  242. {
  243. if (_enchant4Skill == null)
  244. {
  245. return null;
  246. }
  247. return _enchant4Skill.getSkill();
  248. }
  249. /**
  250. * @return the Id in which weapon this weapon can be changed.
  251. */
  252. public int getChangeWeaponId()
  253. {
  254. return _changeWeaponId;
  255. }
  256. /**
  257. * @return {@code true} if the weapon is force equip, {@code false} otherwise.
  258. */
  259. public boolean isForceEquip()
  260. {
  261. return _isForceEquip;
  262. }
  263. /**
  264. * @return {@code true} if the weapon is attack weapon, {@code false} otherwise.
  265. */
  266. public boolean isAttackWeapon()
  267. {
  268. return _isAttackWeapon;
  269. }
  270. /**
  271. * @return {@code true} if the weapon is skills only, {@code false} otherwise.
  272. */
  273. public boolean useWeaponSkillsOnly()
  274. {
  275. return _useWeaponSkillsOnly;
  276. }
  277. /**
  278. * @param instance the L2ItemInstance pointing out the weapon.
  279. * @param player the L2Character pointing out the player.
  280. * @return an array of Func objects containing the list of functions used by the weapon.
  281. */
  282. @Override
  283. public Func[] getStatFuncs(L2ItemInstance instance, L2Character player)
  284. {
  285. if ((_funcTemplates == null) || (_funcTemplates.length == 0))
  286. {
  287. return _emptyFunctionSet;
  288. }
  289. ArrayList<Func> funcs = new ArrayList<Func>(_funcTemplates.length);
  290. Env env = new Env();
  291. env.player = player;
  292. env.item = instance;
  293. Func f;
  294. for (FuncTemplate t : _funcTemplates)
  295. {
  296. f = t.getFunc(env, instance);
  297. if (f != null)
  298. {
  299. funcs.add(f);
  300. }
  301. }
  302. return funcs.toArray(new Func[funcs.size()]);
  303. }
  304. /**
  305. * @param caster the L2Character pointing out the caster
  306. * @param target the L2Character pointing out the target
  307. * @param crit the boolean tells whether the hit was critical
  308. * @return the effects of skills associated with the item to be triggered onHit.
  309. */
  310. public L2Effect[] getSkillEffects(L2Character caster, L2Character target, boolean crit)
  311. {
  312. if ((_skillsOnCrit == null) || !crit)
  313. {
  314. return _emptyEffectSet;
  315. }
  316. List<L2Effect> effects = new FastList<L2Effect>();
  317. if (_skillsOnCritCondition != null)
  318. {
  319. Env env = new Env();
  320. env.player = caster;
  321. env.target = target;
  322. env.skill = _skillsOnCrit.getSkill();
  323. if (!_skillsOnCritCondition.test(env))
  324. {
  325. return _emptyEffectSet; // Skill condition not met
  326. }
  327. }
  328. byte shld = Formulas.calcShldUse(caster, target, _skillsOnCrit.getSkill());
  329. if (!Formulas.calcSkillSuccess(caster, target, _skillsOnCrit.getSkill(), shld, false, false, false))
  330. {
  331. return _emptyEffectSet; // These skills should not work on RaidBoss
  332. }
  333. if (target.getFirstEffect(_skillsOnCrit.getSkill().getId()) != null)
  334. {
  335. target.getFirstEffect(_skillsOnCrit.getSkill().getId()).exit();
  336. }
  337. for (L2Effect e : _skillsOnCrit.getSkill().getEffects(caster, target, new Env(shld, false, false, false)))
  338. {
  339. effects.add(e);
  340. }
  341. if (effects.isEmpty())
  342. {
  343. return _emptyEffectSet;
  344. }
  345. return effects.toArray(new L2Effect[effects.size()]);
  346. }
  347. /**
  348. * @param caster the L2Character pointing out the caster
  349. * @param target the L2Character pointing out the target
  350. * @param trigger the L2Skill pointing out the skill triggering this action
  351. * @return the effects of skills associated with the item to be triggered onCast.
  352. */
  353. public L2Effect[] getSkillEffects(L2Character caster, L2Character target, L2Skill trigger)
  354. {
  355. if (_skillsOnCast == null)
  356. {
  357. return _emptyEffectSet;
  358. }
  359. if (trigger.isOffensive() != _skillsOnCast.getSkill().isOffensive())
  360. {
  361. return _emptyEffectSet; // Trigger only same type of skill
  362. }
  363. if (trigger.isToggle() && (_skillsOnCast.getSkill().getSkillType() == L2SkillType.BUFF))
  364. {
  365. return _emptyEffectSet; // No buffing with toggle skills
  366. }
  367. if (!trigger.isMagic() && (_skillsOnCast.getSkill().getSkillType() == L2SkillType.BUFF))
  368. {
  369. return _emptyEffectSet; // No buffing with not magic skills
  370. }
  371. if (_skillsOnCastCondition != null)
  372. {
  373. Env env = new Env();
  374. env.player = caster;
  375. env.target = target;
  376. env.skill = _skillsOnCast.getSkill();
  377. if (!_skillsOnCastCondition.test(env))
  378. {
  379. return _emptyEffectSet;
  380. }
  381. }
  382. byte shld = Formulas.calcShldUse(caster, target, _skillsOnCast.getSkill());
  383. if (_skillsOnCast.getSkill().isOffensive() && !Formulas.calcSkillSuccess(caster, target, _skillsOnCast.getSkill(), shld, false, false, false))
  384. {
  385. return _emptyEffectSet;
  386. }
  387. // Get the skill handler corresponding to the skill type
  388. ISkillHandler handler = SkillHandler.getInstance().getHandler(_skillsOnCast.getSkill().getSkillType());
  389. L2Character[] targets = new L2Character[1];
  390. targets[0] = target;
  391. // Launch the magic skill and calculate its effects
  392. if (handler != null)
  393. {
  394. handler.useSkill(caster, _skillsOnCast.getSkill(), targets);
  395. }
  396. else
  397. {
  398. _skillsOnCast.getSkill().useSkill(caster, targets);
  399. }
  400. // notify quests of a skill use
  401. if (caster instanceof L2PcInstance)
  402. {
  403. // Mobs in range 1000 see spell
  404. Collection<L2Object> objs = caster.getKnownList().getKnownObjects().values();
  405. for (L2Object spMob : objs)
  406. {
  407. if (spMob instanceof L2Npc)
  408. {
  409. L2Npc npcMob = (L2Npc) spMob;
  410. if (npcMob.getTemplate().getEventQuests(Quest.QuestEventType.ON_SKILL_SEE) != null)
  411. {
  412. for (Quest quest : npcMob.getTemplate().getEventQuests(Quest.QuestEventType.ON_SKILL_SEE))
  413. {
  414. quest.notifySkillSee(npcMob, (L2PcInstance) caster, _skillsOnCast.getSkill(), targets, false);
  415. }
  416. }
  417. }
  418. }
  419. }
  420. return _emptyEffectSet;
  421. }
  422. }