L2Weapon.java 13 KB

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