L2Weapon.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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.templates.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.L2ItemInstance;
  24. import com.l2jserver.gameserver.model.L2Object;
  25. import com.l2jserver.gameserver.model.L2Skill;
  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.quest.Quest;
  30. import com.l2jserver.gameserver.skills.Env;
  31. import com.l2jserver.gameserver.skills.Formulas;
  32. import com.l2jserver.gameserver.skills.SkillHolder;
  33. import com.l2jserver.gameserver.skills.conditions.Condition;
  34. import com.l2jserver.gameserver.skills.conditions.ConditionGameChance;
  35. import com.l2jserver.gameserver.skills.funcs.Func;
  36. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  37. import com.l2jserver.gameserver.templates.StatsSet;
  38. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  39. import com.l2jserver.util.StringUtil;
  40. /**
  41. * This class is dedicated to the management of weapons.
  42. *
  43. * @version $Revision: 1.4.2.3.2.5 $ $Date: 2005/04/02 15:57:51 $
  44. */
  45. public final class L2Weapon extends L2Item
  46. {
  47. private final L2WeaponType _type;
  48. private final boolean _isMagicWeapon;
  49. private final int _rndDam;
  50. private final int _soulShotCount;
  51. private final int _spiritShotCount;
  52. private final int _mpConsume;
  53. private SkillHolder _enchant4Skill = null; // skill that activates when item is enchanted +4 (for duals)
  54. private final int _changeWeaponId;
  55. // private final String[] _skill;
  56. private SkillHolder[] _skillHolder;
  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. /**
  64. * Constructor for Weapon.<BR><BR>
  65. * <U><I>Variables filled :</I></U><BR>
  66. * <LI>_soulShotCount & _spiritShotCount</LI>
  67. * <LI>_pDam & _mDam & _rndDam</LI>
  68. * <LI>_critical</LI>
  69. * <LI>_hitModifier</LI>
  70. * <LI>_avoidModifier</LI>
  71. * <LI>_shieldDes & _shieldDefRate</LI>
  72. * <LI>_atkSpeed & _AtkReuse</LI>
  73. * <LI>_mpConsume</LI>
  74. * @param type : L2ArmorType designating the type of armor
  75. * @param set : StatsSet designating the set of couples (key,value) caracterizing the armor
  76. * @see L2Item constructor
  77. */
  78. public L2Weapon(StatsSet set)
  79. {
  80. super(set);
  81. _type = L2WeaponType.valueOf(set.getString("weapon_type", "none").toUpperCase());
  82. _type1 = L2Item.TYPE1_WEAPON_RING_EARRING_NECKLACE;
  83. _type2 = L2Item.TYPE2_WEAPON;
  84. _isMagicWeapon = set.getBool("is_magic_weapon", false);
  85. _soulShotCount = set.getInteger("soulshots", 0);
  86. _spiritShotCount = set.getInteger("spiritshots", 0);
  87. _rndDam = set.getInteger("random_damage", 0);
  88. _mpConsume = set.getInteger("mp_consume", 0);
  89. _reuseDelay = set.getInteger("reuse_delay", 0);
  90. String skill = set.getString("item_skill", null);
  91. if (skill != null)
  92. {
  93. String[] skills = skill.split(";");
  94. _skillHolder = new SkillHolder[skills.length];
  95. byte iterator = 0;
  96. for (String st : skills)
  97. {
  98. String[] info = st.split("-");
  99. if (info == null || info.length != 2)
  100. continue;
  101. int id = 0;
  102. int level = 0;
  103. try
  104. {
  105. id = Integer.parseInt(info[0]);
  106. level = Integer.parseInt(info[1]);
  107. }
  108. catch (Exception nfe)
  109. {
  110. // Incorrect syntax, dont add new skill
  111. _log.info(StringUtil.concat("> Couldnt parse ", st, " in weapon skills! item ", this.toString()));
  112. continue;
  113. }
  114. // If skill can exist, add it
  115. if (id > 0 && level > 0)
  116. {
  117. _skillHolder[iterator] = new SkillHolder(id, level);
  118. iterator++;
  119. }
  120. }
  121. }
  122. skill = set.getString("enchant4_skill", null);
  123. if (skill != null)
  124. {
  125. String[] info = skill.split("-");
  126. if (info != null && info.length == 2)
  127. {
  128. int id = 0;
  129. int level = 0;
  130. try
  131. {
  132. id = Integer.parseInt(info[0]);
  133. level = Integer.parseInt(info[1]);
  134. }
  135. catch (Exception nfe)
  136. {
  137. // Incorrect syntax, dont add new skill
  138. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon enchant skills! item ", this.toString()));
  139. }
  140. if (id > 0 && level > 0)
  141. _enchant4Skill = new SkillHolder(id, level);
  142. }
  143. }
  144. skill = set.getString("oncast_skill", null);
  145. if (skill != null)
  146. {
  147. String[] info = skill.split("-");
  148. String infochance = set.getString("oncast_chance", null);
  149. if (info != null && info.length == 2)
  150. {
  151. int id = 0;
  152. int level = 0;
  153. int chance = 0;
  154. try
  155. {
  156. id = Integer.parseInt(info[0]);
  157. level = Integer.parseInt(info[1]);
  158. if (infochance != null)
  159. chance = Integer.parseInt(infochance);
  160. }
  161. catch (Exception nfe)
  162. {
  163. // Incorrect syntax, dont add new skill
  164. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncast skills! item ", this.toString()));
  165. }
  166. if (id > 0 && level > 0 && chance > 0)
  167. {
  168. _skillsOnCast = new SkillHolder(id, level);
  169. if (infochance != null)
  170. _skillsOnCastCondition = new ConditionGameChance(chance);
  171. }
  172. }
  173. }
  174. skill = set.getString("oncrit_skill", null);
  175. if (skill != null)
  176. {
  177. String[] info = skill.split("-");
  178. String infochance = set.getString("oncrit_chance", null);
  179. if (info != null && info.length == 2)
  180. {
  181. int id = 0;
  182. int level = 0;
  183. int chance = 0;
  184. try
  185. {
  186. id = Integer.parseInt(info[0]);
  187. level = Integer.parseInt(info[1]);
  188. if (infochance != null)
  189. chance = Integer.parseInt(infochance);
  190. }
  191. catch (Exception nfe)
  192. {
  193. // Incorrect syntax, dont add new skill
  194. _log.info(StringUtil.concat("> Couldnt parse ", skill, " in weapon oncrit skills! item ", this.toString()));
  195. }
  196. if (id > 0 && level > 0 && chance > 0)
  197. {
  198. _skillsOnCrit = new SkillHolder(id, level);
  199. if (infochance != null)
  200. _skillsOnCritCondition = new ConditionGameChance(chance);
  201. }
  202. }
  203. }
  204. _changeWeaponId = set.getInteger("change_weaponId", 0);
  205. }
  206. /**
  207. * Returns the type of Weapon
  208. * @return L2WeaponType
  209. */
  210. @Override
  211. public L2WeaponType getItemType()
  212. {
  213. return _type;
  214. }
  215. /**
  216. * Returns the ID of the Etc item after applying the mask.
  217. * @return int : ID of the Weapon
  218. */
  219. @Override
  220. public int getItemMask()
  221. {
  222. return getItemType().mask();
  223. }
  224. /**
  225. * Returns true if L2Weapon is magic type.
  226. * @return boolean
  227. */
  228. public boolean isMagicWeapon()
  229. {
  230. return _isMagicWeapon;
  231. }
  232. /**
  233. * Returns the quantity of SoulShot used.
  234. * @return int
  235. */
  236. public int getSoulShotCount()
  237. {
  238. return _soulShotCount;
  239. }
  240. /**
  241. * Returns the quatity of SpiritShot used.
  242. * @return int
  243. */
  244. public int getSpiritShotCount()
  245. {
  246. return _spiritShotCount;
  247. }
  248. /**
  249. * Returns the random damage inflicted by the weapon
  250. * @return int
  251. */
  252. public int getRandomDamage()
  253. {
  254. return _rndDam;
  255. }
  256. /**
  257. * Return the Reuse Delay of the L2Weapon.<BR><BR>
  258. * @return int
  259. */
  260. public int getReuseDelay()
  261. {
  262. return _reuseDelay;
  263. }
  264. /**
  265. * Returns the MP consumption with the weapon
  266. * @return int
  267. */
  268. public int getMpConsume()
  269. {
  270. return _mpConsume;
  271. }
  272. /**
  273. * Returns passive skill linked to that weapon
  274. * @return
  275. */
  276. @Override
  277. public SkillHolder[] getSkills()
  278. {
  279. return _skillHolder;
  280. }
  281. /**
  282. * Returns skill that player get when has equiped weapon +4 or more (for duals SA)
  283. * @return
  284. */
  285. public L2Skill getEnchant4Skill()
  286. {
  287. if (_enchant4Skill == null)
  288. return null;
  289. return _enchant4Skill.getSkill();
  290. }
  291. /**
  292. * Returns the Id in wich weapon this weapon can be changed
  293. * @return
  294. */
  295. public int getChangeWeaponId()
  296. {
  297. return _changeWeaponId;
  298. }
  299. /**
  300. * Returns array of Func objects containing the list of functions used by the weapon
  301. * @param instance : L2ItemInstance pointing out the weapon
  302. * @param player : L2Character pointing out the player
  303. * @return Func[] : array of functions
  304. */
  305. @Override
  306. public Func[] getStatFuncs(L2ItemInstance instance, L2Character player)
  307. {
  308. if (_funcTemplates == null || _funcTemplates.length == 0)
  309. return _emptyFunctionSet;
  310. ArrayList<Func> funcs = new ArrayList<Func>(_funcTemplates.length);
  311. Env env = new Env();
  312. env.player = player;
  313. env.item = instance;
  314. Func f;
  315. for (FuncTemplate t : _funcTemplates)
  316. {
  317. f = t.getFunc(env, instance);
  318. if (f != null)
  319. funcs.add(f);
  320. }
  321. return funcs.toArray(new Func[funcs.size()]);
  322. }
  323. /**
  324. * Returns effects of skills associated with the item to be triggered onHit.
  325. * @param caster : L2Character pointing out the caster
  326. * @param target : L2Character pointing out the target
  327. * @param crit : boolean tells whether the hit was critical
  328. * @return L2Effect[] : array of effects generated by the skill
  329. */
  330. public L2Effect[] getSkillEffects(L2Character caster, L2Character target, boolean crit)
  331. {
  332. if (_skillsOnCrit == null || !crit)
  333. return _emptyEffectSet;
  334. List<L2Effect> effects = new FastList<L2Effect>();
  335. if (_skillsOnCritCondition != null)
  336. {
  337. Env env = new Env();
  338. env.player = caster;
  339. env.target = target;
  340. env.skill = _skillsOnCrit.getSkill();
  341. if (!_skillsOnCritCondition.test(env))
  342. return _emptyEffectSet; // Skill condition not met
  343. }
  344. byte shld = Formulas.calcShldUse(caster, target, _skillsOnCrit.getSkill());
  345. if (!Formulas.calcSkillSuccess(caster, target, _skillsOnCrit.getSkill(), shld, false, false, false))
  346. return _emptyEffectSet; // These skills should not work on RaidBoss
  347. if (target.getFirstEffect(_skillsOnCrit.getSkill().getId()) != null)
  348. target.getFirstEffect(_skillsOnCrit.getSkill().getId()).exit();
  349. for (L2Effect e : _skillsOnCrit.getSkill().getEffects(caster, target, new Env(shld, false, false, false)))
  350. effects.add(e);
  351. if (effects.isEmpty())
  352. return _emptyEffectSet;
  353. return effects.toArray(new L2Effect[effects.size()]);
  354. }
  355. /**
  356. * Returns effects of skills associated with the item to be triggered onCast.
  357. * @param caster : L2Character pointing out the caster
  358. * @param target : L2Character pointing out the target
  359. * @param trigger : L2Skill pointing out the skill triggering this action
  360. * @return L2Effect[] : array of effects generated by the skill
  361. */
  362. public L2Effect[] getSkillEffects(L2Character caster, L2Character target, L2Skill trigger)
  363. {
  364. if (_skillsOnCast == null)
  365. return _emptyEffectSet;
  366. if (trigger.isOffensive() != _skillsOnCast.getSkill().isOffensive())
  367. return _emptyEffectSet; // Trigger only same type of skill
  368. if (trigger.isToggle() && _skillsOnCast.getSkill().getSkillType() == L2SkillType.BUFF)
  369. return _emptyEffectSet; // No buffing with toggle skills
  370. if (!trigger.isMagic() && _skillsOnCast.getSkill().getSkillType() == L2SkillType.BUFF)
  371. return _emptyEffectSet; // No buffing with not magic skills
  372. if (_skillsOnCastCondition != null)
  373. {
  374. Env env = new Env();
  375. env.player = caster;
  376. env.target = target;
  377. env.skill = _skillsOnCast.getSkill();
  378. if (!_skillsOnCastCondition.test(env))
  379. return _emptyEffectSet;
  380. }
  381. byte shld = Formulas.calcShldUse(caster, target, _skillsOnCast.getSkill());
  382. if (_skillsOnCast.getSkill().isOffensive() && !Formulas.calcSkillSuccess(caster, target, _skillsOnCast.getSkill(), shld, false, false, false))
  383. return _emptyEffectSet;
  384. // Get the skill handler corresponding to the skill type
  385. ISkillHandler handler = SkillHandler.getInstance().getSkillHandler(_skillsOnCast.getSkill().getSkillType());
  386. L2Character[] targets = new L2Character[1];
  387. targets[0] = target;
  388. // Launch the magic skill and calculate its effects
  389. if (handler != null)
  390. handler.useSkill(caster, _skillsOnCast.getSkill(), targets);
  391. else
  392. _skillsOnCast.getSkill().useSkill(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. //synchronized (caster.getKnownList().getKnownObjects())
  399. {
  400. for (L2Object spMob : objs)
  401. {
  402. if (spMob instanceof L2Npc)
  403. {
  404. L2Npc npcMob = (L2Npc) spMob;
  405. if (npcMob.getTemplate().getEventQuests(Quest.QuestEventType.ON_SKILL_SEE) != null)
  406. for (Quest quest : npcMob.getTemplate().getEventQuests(Quest.QuestEventType.ON_SKILL_SEE))
  407. quest.notifySkillSee(npcMob, (L2PcInstance) caster, _skillsOnCast.getSkill(), targets, false);
  408. }
  409. }
  410. }
  411. }
  412. return _emptyEffectSet;
  413. }
  414. }