L2Effect.java 18 KB

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