L2Weapon.java 14 KB

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