L2Weapon.java 14 KB

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