L2Effect.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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.model;
  16. import java.util.ArrayList;
  17. import java.util.concurrent.ScheduledFuture;
  18. import java.util.concurrent.TimeUnit;
  19. import java.util.logging.Level;
  20. import java.util.logging.Logger;
  21. import com.l2jserver.gameserver.GameTimeController;
  22. import com.l2jserver.gameserver.ThreadPoolManager;
  23. import com.l2jserver.gameserver.datatables.SkillTable;
  24. import com.l2jserver.gameserver.model.actor.L2Character;
  25. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  26. import com.l2jserver.gameserver.model.actor.instance.L2SummonInstance;
  27. import com.l2jserver.gameserver.network.SystemMessageId;
  28. import com.l2jserver.gameserver.network.serverpackets.AbnormalStatusUpdate;
  29. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadSpelledInfo;
  30. import com.l2jserver.gameserver.network.serverpackets.MagicSkillLaunched;
  31. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  32. import com.l2jserver.gameserver.network.serverpackets.PartySpelled;
  33. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  34. import com.l2jserver.gameserver.skills.AbnormalEffect;
  35. import com.l2jserver.gameserver.skills.Env;
  36. import com.l2jserver.gameserver.skills.funcs.Func;
  37. import com.l2jserver.gameserver.skills.funcs.FuncTemplate;
  38. import com.l2jserver.gameserver.skills.funcs.Lambda;
  39. import com.l2jserver.gameserver.templates.effects.EffectTemplate;
  40. import com.l2jserver.gameserver.templates.skills.L2EffectType;
  41. import com.l2jserver.gameserver.templates.skills.L2SkillType;
  42. /**
  43. * This class ...
  44. *
  45. * @version $Revision: 1.1.2.1.2.12 $ $Date: 2005/04/11 10:06:07 $
  46. */
  47. public abstract class L2Effect implements IChanceSkillTrigger
  48. {
  49. protected static final Logger _log = Logger.getLogger(L2Effect.class.getName());
  50. public static enum EffectState
  51. {
  52. CREATED,
  53. ACTING,
  54. FINISHING
  55. }
  56. private static final Func[] _emptyFunctionSet = new Func[0];
  57. //member _effector is the instance of L2Character that cast/used the spell/skill that is
  58. //causing this effect. Do not confuse with the instance of L2Character that
  59. //is being affected by this effect.
  60. private final L2Character _effector;
  61. //member _effected is the instance of L2Character that was affected
  62. //by this effect. Do not confuse with the instance of L2Character that
  63. //casted/used this effect.
  64. private final L2Character _effected;
  65. //the skill that was used.
  66. private final L2Skill _skill;
  67. private final boolean _isHerbEffect;
  68. //or the items that was used.
  69. //private final L2Item _item;
  70. // the value of an update
  71. private final Lambda _lambda;
  72. // the current state
  73. private EffectState _state;
  74. // period, seconds
  75. private final int _abnormalTime;
  76. private int _periodStartTicks;
  77. private int _periodFirstTime;
  78. private EffectTemplate _template;
  79. // function templates
  80. private final FuncTemplate[] _funcTemplates;
  81. //initial count
  82. private int _totalCount;
  83. // counter
  84. private int _count;
  85. // abnormal effect mask
  86. private AbnormalEffect _abnormalEffect;
  87. // special effect mask
  88. private AbnormalEffect[] _specialEffect;
  89. // event effect mask
  90. private AbnormalEffect _eventEffect;
  91. // show icon
  92. private boolean _icon;
  93. // is self effect?
  94. private boolean _isSelfEffect = false;
  95. // is passive effect?
  96. private boolean _isPassiveEffect = false;
  97. public boolean preventExitUpdate;
  98. private final class EffectTask implements Runnable
  99. {
  100. @Override
  101. public void run()
  102. {
  103. try
  104. {
  105. _periodFirstTime = 0;
  106. _periodStartTicks = GameTimeController.getGameTicks();
  107. L2Effect.this.scheduleEffect();
  108. }
  109. catch (Exception e)
  110. {
  111. _log.log(Level.SEVERE, "", e);
  112. }
  113. }
  114. }
  115. private ScheduledFuture<?> _currentFuture;
  116. /** The Identifier of the stack group */
  117. private final String _abnormalType;
  118. /** The position of the effect in the stack group */
  119. private final byte _abnormalLvl;
  120. private boolean _inUse = false;
  121. private boolean _startConditionsCorrect = true;
  122. /**
  123. * For special behavior. See Formulas.calcEffectSuccess
  124. */
  125. private double _effectPower;
  126. private L2SkillType _effectSkillType;
  127. /**
  128. * <font color="FF0000"><b>WARNING: scheduleEffect no longer inside constructor</b></font><br>
  129. * So you must call it explicitly
  130. * @param env
  131. * @param template
  132. */
  133. protected L2Effect(Env env, EffectTemplate template)
  134. {
  135. _state = EffectState.CREATED;
  136. _skill = env.skill;
  137. //_item = env._item == null ? null : env._item.getItem();
  138. _template = template;
  139. _effected = env.target;
  140. _effector = env.player;
  141. _lambda = template.lambda;
  142. _funcTemplates = template.funcTemplates;
  143. _count = template.counter;
  144. _totalCount = _count;
  145. // Support for retail herbs duration when _effected has a Summon
  146. int temp = template.abnormalTime;
  147. if ((_skill.getId() > 2277 && _skill.getId() < 2286) || (_skill.getId() >= 2512 && _skill.getId() <= 2514))
  148. {
  149. if (_effected instanceof L2SummonInstance ||
  150. (_effected instanceof L2PcInstance && ((L2PcInstance) _effected).getPet() instanceof L2SummonInstance))
  151. {
  152. temp /= 2;
  153. }
  154. }
  155. if (env.skillMastery)
  156. temp *= 2;
  157. _abnormalTime = temp;
  158. _abnormalEffect = template.abnormalEffect;
  159. _specialEffect = template.specialEffect;
  160. _eventEffect = template.eventEffect;
  161. _abnormalType = template.abnormalType;
  162. _abnormalLvl = template.abnormalLvl;
  163. _periodStartTicks = GameTimeController.getGameTicks();
  164. _periodFirstTime = 0;
  165. _icon = template.icon;
  166. _effectPower = template.effectPower;
  167. _effectSkillType = template.effectType;
  168. _isHerbEffect = _skill.getName().contains("Herb");
  169. /*
  170. * Commented out by DrHouse:
  171. * scheduleEffect can call onStart before effect is completly
  172. * initialized on constructor (child classes constructor)
  173. */
  174. //scheduleEffect();
  175. }
  176. /**
  177. * Special constructor to "steal" buffs. Must be implemented on
  178. * every child class that can be stolen.<br><br>
  179. *
  180. * <font color="FF0000"><b>WARNING: scheduleEffect nolonger inside constructor</b></font>
  181. * <br>So you must call it explicitly
  182. * @param env
  183. * @param effect
  184. */
  185. protected L2Effect(Env env, L2Effect effect)
  186. {
  187. _template = effect._template;
  188. _state = EffectState.CREATED;
  189. _skill = env.skill;
  190. _effected = env.target;
  191. _effector = env.player;
  192. _lambda = _template.lambda;
  193. _funcTemplates = _template.funcTemplates;
  194. _count = effect.getCount();
  195. _totalCount = _template.counter;
  196. _abnormalTime = _template.abnormalTime;
  197. _abnormalEffect = _template.abnormalEffect;
  198. _specialEffect = _template.specialEffect;
  199. _eventEffect = _template.eventEffect;
  200. _abnormalType = _template.abnormalType;
  201. _abnormalLvl = _template.abnormalLvl;
  202. _periodStartTicks = effect.getPeriodStartTicks();
  203. _periodFirstTime = effect.getTime();
  204. _icon = _template.icon;
  205. _isHerbEffect = _skill.getName().contains("Herb");
  206. /*
  207. * Commented out by DrHouse:
  208. * scheduleEffect can call onStart before effect is completly
  209. * initialized on constructor (child classes constructor)
  210. */
  211. //scheduleEffect();
  212. }
  213. public int getCount()
  214. {
  215. return _count;
  216. }
  217. public int getTotalCount()
  218. {
  219. return _totalCount;
  220. }
  221. public void setCount(int newcount)
  222. {
  223. _count = Math.min(newcount, _totalCount); // sanity check
  224. }
  225. public void setFirstTime(int newFirstTime)
  226. {
  227. _periodFirstTime = Math.min(newFirstTime, _abnormalTime);
  228. _periodStartTicks -= _periodFirstTime * GameTimeController.TICKS_PER_SECOND;
  229. }
  230. public boolean getShowIcon()
  231. {
  232. return _icon;
  233. }
  234. public int getAbnormalTime()
  235. {
  236. return _abnormalTime;
  237. }
  238. public int getTime()
  239. {
  240. return (GameTimeController.getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND;
  241. }
  242. /**
  243. * Returns the elapsed time of the task.
  244. * @return Time in seconds.
  245. */
  246. public int getTaskTime()
  247. {
  248. if (_count == _totalCount)
  249. return 0;
  250. return (Math.abs(_count - _totalCount + 1) * _abnormalTime) + getTime() + 1;
  251. }
  252. public boolean getInUse()
  253. {
  254. return _inUse;
  255. }
  256. public boolean setInUse(boolean inUse)
  257. {
  258. _inUse = inUse;
  259. if (_inUse)
  260. _startConditionsCorrect = onStart();
  261. else
  262. onExit();
  263. return _startConditionsCorrect;
  264. }
  265. public String getAbnormalType()
  266. {
  267. return _abnormalType;
  268. }
  269. public byte getAbnormalLvl()
  270. {
  271. return _abnormalLvl;
  272. }
  273. public final L2Skill getSkill()
  274. {
  275. return _skill;
  276. }
  277. public final L2Character getEffector()
  278. {
  279. return _effector;
  280. }
  281. public final L2Character getEffected()
  282. {
  283. return _effected;
  284. }
  285. public boolean isSelfEffect()
  286. {
  287. return _isSelfEffect;
  288. }
  289. public void setSelfEffect()
  290. {
  291. _isSelfEffect = true;
  292. }
  293. public boolean isPassiveEffect()
  294. {
  295. return _isPassiveEffect;
  296. }
  297. public void setPassiveEffect()
  298. {
  299. _isPassiveEffect = true;
  300. }
  301. public boolean isHerbEffect()
  302. {
  303. return _isHerbEffect;
  304. }
  305. public final double calc()
  306. {
  307. Env env = new Env();
  308. env.player = _effector;
  309. env.target = _effected;
  310. env.skill = _skill;
  311. return _lambda.calc(env);
  312. }
  313. private final synchronized void startEffectTask()
  314. {
  315. if (_abnormalTime > 0)
  316. {
  317. stopEffectTask();
  318. final int initialDelay = Math.max((_abnormalTime - _periodFirstTime) * 1000, 5);
  319. if (_count > 1)
  320. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(new EffectTask(), initialDelay, _abnormalTime * 1000);
  321. else
  322. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(new EffectTask(), initialDelay);
  323. }
  324. if (_state == EffectState.ACTING)
  325. {
  326. if (isSeflEffectType())
  327. _effector.addEffect(this);
  328. else
  329. _effected.addEffect(this);
  330. }
  331. }
  332. /**
  333. * Stop the L2Effect task and send Server->Client update packet.<BR><BR>
  334. *
  335. * <B><U> Actions</U> :</B><BR><BR>
  336. * <li>Cancel the effect in the the abnormal effect map of the L2Character </li>
  337. * <li>Stop the task of the L2Effect, remove it and update client magic icon </li><BR><BR>
  338. *
  339. */
  340. public final void exit()
  341. {
  342. this.exit(false);
  343. }
  344. public final void exit(boolean preventUpdate)
  345. {
  346. preventExitUpdate = preventUpdate;
  347. _state = EffectState.FINISHING;
  348. scheduleEffect();
  349. }
  350. /**
  351. * Stop the task of the L2Effect, remove it and update client magic icon.<BR><BR>
  352. *
  353. * <B><U> Actions</U> :</B><BR><BR>
  354. * <li>Cancel the task </li>
  355. * <li>Stop and remove L2Effect from L2Character and update client magic icon </li><BR><BR>
  356. *
  357. */
  358. public final synchronized void stopEffectTask()
  359. {
  360. if (_currentFuture != null)
  361. {
  362. // Cancel the task
  363. _currentFuture.cancel(false);
  364. //ThreadPoolManager.getInstance().removeEffect(_currentTask);
  365. _currentFuture = null;
  366. if (isSeflEffectType() && getEffector() != null)
  367. getEffector().removeEffect(this);
  368. else if (getEffected() != null)
  369. getEffected().removeEffect(this);
  370. }
  371. }
  372. /**
  373. * @return effect type
  374. */
  375. public abstract L2EffectType getEffectType();
  376. /**
  377. * Notify started
  378. * @return
  379. */
  380. public boolean onStart()
  381. {
  382. if (_abnormalEffect != AbnormalEffect.NULL)
  383. getEffected().startAbnormalEffect(_abnormalEffect);
  384. if (_specialEffect != null)
  385. getEffected().startSpecialEffect(_specialEffect);
  386. if (_eventEffect != AbnormalEffect.NULL && getEffected() instanceof L2PcInstance)
  387. getEffected().getActingPlayer().startEventEffect(_eventEffect);
  388. return true;
  389. }
  390. /**
  391. * Cancel the effect in the the abnormal effect map of the effected L2Character.<BR><BR>
  392. */
  393. public void onExit()
  394. {
  395. if (_abnormalEffect != AbnormalEffect.NULL)
  396. getEffected().stopAbnormalEffect(_abnormalEffect);
  397. if (_specialEffect != null)
  398. getEffected().stopSpecialEffect(_specialEffect);
  399. if (_eventEffect != AbnormalEffect.NULL && getEffected() instanceof L2PcInstance)
  400. getEffected().getActingPlayer().stopEventEffect(_eventEffect);
  401. }
  402. /**
  403. * @return true for continuation of this effect
  404. */
  405. public abstract boolean onActionTime();
  406. public final void scheduleEffect()
  407. {
  408. switch (_state)
  409. {
  410. case CREATED:
  411. {
  412. _state = EffectState.ACTING;
  413. if (_skill.isPvpSkill() && _icon && getEffected() instanceof L2PcInstance)
  414. {
  415. SystemMessage smsg = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  416. smsg.addSkillName(_skill);
  417. getEffected().sendPacket(smsg);
  418. }
  419. if (_abnormalTime != 0)
  420. {
  421. startEffectTask();
  422. return;
  423. }
  424. // effects not having count or period should start
  425. _startConditionsCorrect = onStart();
  426. }
  427. case ACTING:
  428. {
  429. if (_count > 0)
  430. {
  431. _count--;
  432. if (getInUse())
  433. { // effect has to be in use
  434. if (onActionTime() && _startConditionsCorrect && _count > 0)
  435. return; // false causes effect to finish right away
  436. }
  437. else if (_count > 0)
  438. { // do not finish it yet, in case reactivated
  439. return;
  440. }
  441. }
  442. _state = EffectState.FINISHING;
  443. }
  444. case FINISHING:
  445. {
  446. //If the time left is equal to zero, send the message
  447. if (_count == 0 && _icon && getEffected() instanceof L2PcInstance)
  448. {
  449. SystemMessage smsg3 = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_WORN_OFF);
  450. smsg3.addSkillName(_skill);
  451. getEffected().sendPacket(smsg3);
  452. }
  453. // if task is null - stopEffectTask does not remove effect
  454. if (_currentFuture == null && getEffected() != null)
  455. {
  456. getEffected().removeEffect(this);
  457. }
  458. // Stop the task of the L2Effect, remove it and update client magic icon
  459. stopEffectTask();
  460. // Cancel the effect in the the abnormal effect map of the L2Character
  461. if (getInUse() || !(_count > 1 || _abnormalTime > 0))
  462. if (_startConditionsCorrect)
  463. onExit();
  464. if (_skill.getAfterEffectId() > 0)
  465. {
  466. L2Skill skill = SkillTable.getInstance().getInfo(_skill.getAfterEffectId(), _skill.getAfterEffectLvl());
  467. if (skill != null)
  468. {
  469. getEffected().broadcastPacket(new MagicSkillUse(_effected, skill.getId(), skill.getLevel(), 0, 0));
  470. getEffected().broadcastPacket(new MagicSkillLaunched(_effected, skill.getId(), skill.getLevel()));
  471. skill.getEffects(getEffected(), getEffected());
  472. }
  473. }
  474. }
  475. }
  476. }
  477. public Func[] getStatFuncs()
  478. {
  479. if (_funcTemplates == null)
  480. return _emptyFunctionSet;
  481. ArrayList<Func> funcs = new ArrayList<Func>(_funcTemplates.length);
  482. Env env = new Env();
  483. env.player = getEffector();
  484. env.target = getEffected();
  485. env.skill = getSkill();
  486. Func f;
  487. for (FuncTemplate t : _funcTemplates)
  488. {
  489. f = t.getFunc(env, this); // effect is owner
  490. if (f != null)
  491. funcs.add(f);
  492. }
  493. if (funcs.isEmpty())
  494. return _emptyFunctionSet;
  495. return funcs.toArray(new Func[funcs.size()]);
  496. }
  497. public final void addIcon(AbnormalStatusUpdate mi)
  498. {
  499. if (_state != EffectState.ACTING)
  500. return;
  501. final ScheduledFuture<?> future = _currentFuture;
  502. final L2Skill sk = getSkill();
  503. if (_totalCount > 1)
  504. {
  505. if (sk.isPotion())
  506. mi.addEffect(sk.getId(), getLevel(), sk.getBuffDuration() - (getTaskTime() * 1000));
  507. else
  508. mi.addEffect(sk.getId(), getLevel(), -1);
  509. }
  510. else if (future != null)
  511. mi.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  512. else if (_abnormalTime == -1)
  513. mi.addEffect(sk.getId(), getLevel(), _abnormalTime);
  514. }
  515. public final void addPartySpelledIcon(PartySpelled ps)
  516. {
  517. if (_state != EffectState.ACTING)
  518. return;
  519. final ScheduledFuture<?> future = _currentFuture;
  520. final L2Skill sk = getSkill();
  521. if (future != null)
  522. ps.addPartySpelledEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  523. else if (_abnormalTime == -1)
  524. ps.addPartySpelledEffect(sk.getId(), getLevel(), _abnormalTime);
  525. }
  526. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  527. {
  528. if (_state != EffectState.ACTING)
  529. return;
  530. final ScheduledFuture<?> future = _currentFuture;
  531. final L2Skill sk = getSkill();
  532. if (future != null)
  533. os.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  534. else if (_abnormalTime == -1)
  535. os.addEffect(sk.getId(), getLevel(), _abnormalTime);
  536. }
  537. public int getLevel()
  538. {
  539. return getSkill().getLevel();
  540. }
  541. public int getPeriodStartTicks()
  542. {
  543. return _periodStartTicks;
  544. }
  545. public EffectTemplate getEffectTemplate()
  546. {
  547. return _template;
  548. }
  549. public double getEffectPower()
  550. {
  551. return _effectPower;
  552. }
  553. public L2SkillType getSkillType()
  554. {
  555. return _effectSkillType;
  556. }
  557. public boolean canBeStolen()
  558. {
  559. if(!effectCanBeStolen()
  560. || this.getEffectType() == L2EffectType.TRANSFORMATION
  561. || this.getSkill().isPassive()
  562. || this.getSkill().isToggle()
  563. || this.getSkill().isDebuff()
  564. || this.getSkill().isHeroSkill()
  565. || this.getSkill().isGMSkill()
  566. || (this.getSkill().isPotion() && (this.getSkill().getId() != 2274 && this.getSkill().getId() != 2341)) // Hardcode for now :<
  567. || this.isHerbEffect()
  568. || !this.getSkill().canBeDispeled())
  569. return false;
  570. return true;
  571. }
  572. /**
  573. * Return true if effect itself can be stolen
  574. * @return
  575. */
  576. protected boolean effectCanBeStolen()
  577. {
  578. return false;
  579. }
  580. /**
  581. * Return bit flag for current effect
  582. * @return int flag
  583. */
  584. public int getEffectFlags()
  585. {
  586. return 0;
  587. }
  588. @Override
  589. public String toString()
  590. {
  591. return "L2Effect [_skill=" + _skill + ", _state=" + _state + ", _period=" + _abnormalTime + "]";
  592. }
  593. public boolean isSeflEffectType()
  594. {
  595. return false;
  596. }
  597. public void decreaseForce() { }
  598. public void increaseEffect() { }
  599. public int getForceEffect() { return 0; }
  600. public boolean isBuffEffect() { return false; }
  601. public boolean isDebuffEffect() { return false; }
  602. @Override
  603. public boolean triggersChanceSkill() { return false; }
  604. @Override
  605. public int getTriggeredChanceId() { return 0; }
  606. @Override
  607. public int getTriggeredChanceLevel() { return 0; }
  608. @Override
  609. public ChanceCondition getTriggeredChanceCondition() { return null; }
  610. }