L2Weapon.java 13 KB

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