L2Weapon.java 13 KB

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