L2Effect.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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.Collections;
  22. import java.util.List;
  23. import java.util.concurrent.ScheduledFuture;
  24. import java.util.concurrent.TimeUnit;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.l2jserver.gameserver.GameTimeController;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.datatables.SkillTable;
  30. import com.l2jserver.gameserver.model.ChanceCondition;
  31. import com.l2jserver.gameserver.model.actor.L2Character;
  32. import com.l2jserver.gameserver.model.interfaces.IChanceSkillTrigger;
  33. import com.l2jserver.gameserver.model.skills.L2Skill;
  34. import com.l2jserver.gameserver.model.skills.funcs.Func;
  35. import com.l2jserver.gameserver.model.skills.funcs.FuncTemplate;
  36. import com.l2jserver.gameserver.model.skills.funcs.Lambda;
  37. import com.l2jserver.gameserver.model.stats.Env;
  38. import com.l2jserver.gameserver.model.stats.Formulas;
  39. import com.l2jserver.gameserver.network.SystemMessageId;
  40. import com.l2jserver.gameserver.network.serverpackets.AbnormalStatusUpdate;
  41. import com.l2jserver.gameserver.network.serverpackets.ExOlympiadSpelledInfo;
  42. import com.l2jserver.gameserver.network.serverpackets.MagicSkillLaunched;
  43. import com.l2jserver.gameserver.network.serverpackets.MagicSkillUse;
  44. import com.l2jserver.gameserver.network.serverpackets.PartySpelled;
  45. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  46. /**
  47. * Abstract effect implementation.
  48. * @author Zoey76
  49. */
  50. public abstract class L2Effect implements IChanceSkillTrigger
  51. {
  52. protected static final Logger _log = Logger.getLogger(L2Effect.class.getName());
  53. /** The character that creates this effect. */
  54. private final L2Character _effector;
  55. /** The character that is affected by this effect. */
  56. private final L2Character _effected;
  57. /** The skill that launched this effect. */
  58. private final L2Skill _skill;
  59. /** The value on an update. */
  60. private final Lambda _lambda;
  61. /** The current state. */
  62. private EffectState _state;
  63. /** The game ticks at the start of this effect. */
  64. protected int _periodStartTicks;
  65. protected int _periodFirstTime;
  66. /** The effect template. */
  67. private final EffectTemplate _template;
  68. /** Effect tick count. */
  69. private int _tickCount;
  70. /** Effect's abnormal time. */
  71. private final int _abnormalTime;
  72. /** If {@code true} then it's a self-effect. */
  73. private boolean _isSelfEffect = false;
  74. /** If {@code true} then prevent exit update. */
  75. private boolean _preventExitUpdate;
  76. private volatile ScheduledFuture<?> _currentFuture;
  77. /** If {@code true} then this effect is in use. */
  78. private boolean _inUse = false;
  79. /** If {@code true} then this effect's start condition are meet. */
  80. private boolean _startConditionsCorrect = true;
  81. /** If {@code true} then this effect has been cancelled. */
  82. private boolean _isRemoved = false;
  83. protected final class EffectTask implements Runnable
  84. {
  85. @Override
  86. public void run()
  87. {
  88. try
  89. {
  90. _periodFirstTime = 0;
  91. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  92. scheduleEffect();
  93. }
  94. catch (Exception e)
  95. {
  96. _log.log(Level.SEVERE, "", e);
  97. }
  98. }
  99. }
  100. /**
  101. * @param env DTO with required data
  102. * @param template the effect template
  103. */
  104. protected L2Effect(Env env, EffectTemplate template)
  105. {
  106. _state = EffectState.CREATED;
  107. _skill = env.getSkill();
  108. _template = template;
  109. _effected = env.getTarget();
  110. _effector = env.getCharacter();
  111. _lambda = template.getLambda();
  112. _tickCount = 0;
  113. _abnormalTime = Formulas.calcEffectAbnormalTime(env, template);
  114. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  115. _periodFirstTime = 0;
  116. }
  117. /**
  118. * Special constructor to "steal" buffs.<br>
  119. * Must be implemented on every child class that can be stolen.
  120. * @param env DTO with required data
  121. * @param effect the stolen effect, used as "template"
  122. */
  123. protected L2Effect(Env env, L2Effect effect)
  124. {
  125. _template = effect._template;
  126. _state = EffectState.CREATED;
  127. _skill = env.getSkill();
  128. _effected = env.getTarget();
  129. _effector = env.getCharacter();
  130. _lambda = _template.getLambda();
  131. _tickCount = effect.getTickCount();
  132. _abnormalTime = effect.getAbnormalTime();
  133. _periodStartTicks = effect.getPeriodStartTicks();
  134. _periodFirstTime = effect.getTime();
  135. }
  136. /**
  137. * Get the current tick count.
  138. * @return the tick count
  139. */
  140. public int getTickCount()
  141. {
  142. return _tickCount;
  143. }
  144. public void setCount(int newTickCount)
  145. {
  146. _tickCount = Math.min(newTickCount, _template.getTotalTickCount());
  147. }
  148. public void setFirstTime(int newFirstTime)
  149. {
  150. _periodFirstTime = Math.min(newFirstTime, _abnormalTime);
  151. _periodStartTicks -= _periodFirstTime * GameTimeController.TICKS_PER_SECOND;
  152. }
  153. /**
  154. * Verify if this effect display an icon.
  155. * @return {@code true} if this effect display an icon, {@code false} otherwise
  156. */
  157. public boolean isIconDisplay()
  158. {
  159. return _template.isIconDisplay();
  160. }
  161. /**
  162. * Get this effect's calculated abnormal time.
  163. * @return the abnormal time
  164. */
  165. public int getAbnormalTime()
  166. {
  167. return _abnormalTime;
  168. }
  169. /**
  170. * Get the elapsed time from the beginning of this effect.
  171. * @return the elapsed time
  172. */
  173. public int getTime()
  174. {
  175. return (GameTimeController.getInstance().getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND;
  176. }
  177. /**
  178. * Get the remaining time.
  179. * @return the remaining time
  180. */
  181. public int getTimeLeft()
  182. {
  183. if (_template.getTotalTickCount() > 1)
  184. {
  185. return (((_template.getTotalTickCount() - _tickCount) + 1) * (_abnormalTime / _template.getTotalTickCount())) - getTime();
  186. }
  187. return _abnormalTime - getTime();
  188. }
  189. /**
  190. * Verify if the effect is in use.
  191. * @return {@code true} if the effect is in use, {@code false} otherwise
  192. */
  193. public boolean isInUse()
  194. {
  195. return _inUse;
  196. }
  197. /**
  198. * Set the effect in use.<br>
  199. * If is set to {@code true}, {@link #onStart()} is invoked, otherwise {@link #onExit()} is invoked.
  200. * @param inUse the value to set
  201. * @return {@link #_startConditionsCorrect}
  202. */
  203. public boolean setInUse(boolean inUse)
  204. {
  205. _inUse = inUse;
  206. if (_inUse)
  207. {
  208. _startConditionsCorrect = onStart();
  209. }
  210. else
  211. {
  212. onExit();
  213. }
  214. return _startConditionsCorrect;
  215. }
  216. /**
  217. * Get the skill that launched this effect.
  218. * @return the skill related to this effect
  219. */
  220. public final L2Skill getSkill()
  221. {
  222. return _skill;
  223. }
  224. /**
  225. * Get the character that evoked this effect.
  226. * @return the effector
  227. */
  228. public final L2Character getEffector()
  229. {
  230. return _effector;
  231. }
  232. /**
  233. * Get the character that received this effect.
  234. * @return the effected
  235. */
  236. public final L2Character getEffected()
  237. {
  238. return _effected;
  239. }
  240. public boolean isSelfEffect()
  241. {
  242. return _isSelfEffect;
  243. }
  244. public void setSelfEffect()
  245. {
  246. _isSelfEffect = true;
  247. }
  248. public final double calc()
  249. {
  250. final Env env = new Env();
  251. env.setCharacter(_effector);
  252. env.setTarget(_effected);
  253. env.setSkill(_skill);
  254. return _lambda.calc(env);
  255. }
  256. /**
  257. * Calculates whether this effects land or not.<br>
  258. * If it lands will be scheduled and added to the character effect list.<br>
  259. * Override in effect implementation to change behavior. <br>
  260. * <b>Warning:</b> Must be used only for instant effects continuous effects will <br>
  261. * not call this they have their success handled by activate_rate
  262. * @return {@code true} if this effect land, {@code false} otherwise
  263. */
  264. public boolean calcSuccess()
  265. {
  266. return true;
  267. }
  268. /**
  269. * Start the effect task.<br>
  270. * If the effect has ticks defined it will be scheduled.<br>
  271. * If abnormal time is defined (greater than 1) the period will be calculated like abnormal time divided total tick count.<br>
  272. * Otherwise it each tick will represent 1 second (1000 milliseconds).
  273. */
  274. private final void startEffectTask()
  275. {
  276. stopEffectTask();
  277. if (isInstant())
  278. {
  279. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(new EffectTask(), 5);
  280. return;
  281. }
  282. final int delay = Math.max((_abnormalTime - _periodFirstTime) * 1000, 5); // Sanity check
  283. if (_template.getTotalTickCount() > 0)
  284. {
  285. // TODO: If default abnormal time is changed to 0, the first check below must be updated as well.
  286. final int period = ((_abnormalTime > 1) ? Math.max(_abnormalTime / _template.getTotalTickCount(), 1) : _template.getTotalTickCount()) * 1000;
  287. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(new EffectTask(), delay / period, period);
  288. }
  289. else
  290. {
  291. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(new EffectTask(), delay);
  292. }
  293. }
  294. /**
  295. * Exit this effect without preventing an update.
  296. */
  297. public final void exit()
  298. {
  299. exit(false);
  300. }
  301. /**
  302. * Exit this effect.
  303. * @param preventExitUpdate
  304. */
  305. public final void exit(boolean preventExitUpdate)
  306. {
  307. _preventExitUpdate = preventExitUpdate;
  308. _state = EffectState.FINISHING;
  309. scheduleEffect();
  310. }
  311. /**
  312. * Stop the task of this effect, remove it and update client magic icon.
  313. */
  314. public final void stopEffectTask()
  315. {
  316. if (_currentFuture != null)
  317. {
  318. // Cancel the task
  319. _currentFuture.cancel(false);
  320. _currentFuture = null;
  321. if (getEffected() != null)
  322. {
  323. getEffected().getEffectList().remove(this);
  324. }
  325. }
  326. }
  327. /**
  328. * Get this effect's type.
  329. * @return the effect type
  330. */
  331. public abstract L2EffectType getEffectType();
  332. /**
  333. * Notify started.
  334. * @return {@code true} if all the start conditions are meet, {@code false} otherwise
  335. */
  336. public boolean onStart()
  337. {
  338. if (_template.getAbnormalEffect() != AbnormalEffect.NULL)
  339. {
  340. getEffected().startAbnormalEffect(_template.getAbnormalEffect());
  341. }
  342. if (_template.getSpecialEffect() != null)
  343. {
  344. getEffected().startSpecialEffect(_template.getSpecialEffect());
  345. }
  346. if ((_template.getEventEffect() != AbnormalEffect.NULL) && getEffected().isPlayer())
  347. {
  348. getEffected().getActingPlayer().startEventEffect(_template.getEventEffect());
  349. }
  350. return true;
  351. }
  352. /**
  353. * Cancel the effect in the the abnormal effect map of the effected L2Character.
  354. */
  355. public void onExit()
  356. {
  357. if (_template.getAbnormalEffect() != AbnormalEffect.NULL)
  358. {
  359. getEffected().stopAbnormalEffect(_template.getAbnormalEffect());
  360. }
  361. if (_template.getSpecialEffect() != null)
  362. {
  363. getEffected().stopSpecialEffect(_template.getSpecialEffect());
  364. }
  365. if ((_template.getEventEffect() != AbnormalEffect.NULL) && getEffected().isPlayer())
  366. {
  367. getEffected().getActingPlayer().stopEventEffect(_template.getEventEffect());
  368. }
  369. }
  370. /**
  371. * Method called on each tick.<br>
  372. * By default if the abnormal time is lesser than zero it will return {@code true}, this means the effect will last forever.
  373. * @return if {@code true} this effect will continue forever, if {@code false} it will stop after tick count is reached
  374. */
  375. public boolean onActionTime()
  376. {
  377. return getAbnormalTime() < 0;
  378. }
  379. /**
  380. * Schedule this effect.
  381. */
  382. public final void scheduleEffect()
  383. {
  384. switch (_state)
  385. {
  386. case CREATED:
  387. {
  388. _state = isInstant() ? EffectState.FINISHING : EffectState.ACTING;
  389. if (_skill.isBad() && isIconDisplay() && getEffected().isPlayer())
  390. {
  391. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  392. sm.addSkillName(_skill);
  393. getEffected().sendPacket(sm);
  394. }
  395. if (_abnormalTime != 0)
  396. {
  397. startEffectTask();
  398. return;
  399. }
  400. _startConditionsCorrect = onStart();
  401. }
  402. case ACTING:
  403. {
  404. if (isInUse())
  405. {
  406. _tickCount++; // Increase tick count.
  407. if (onActionTime() && _startConditionsCorrect)
  408. {
  409. return; // Do not finish.
  410. }
  411. }
  412. if (_tickCount <= _template.getTotalTickCount())
  413. {
  414. return; // Do not finish it yet, has remaining ticks.
  415. }
  416. _state = EffectState.FINISHING;
  417. }
  418. case FINISHING:
  419. {
  420. // Message
  421. if (getEffected().isPlayer() && isIconDisplay())
  422. {
  423. SystemMessageId smId = null;
  424. if (getSkill().isToggle())
  425. {
  426. smId = SystemMessageId.S1_HAS_BEEN_ABORTED;
  427. }
  428. else if (isRemoved())
  429. {
  430. smId = SystemMessageId.EFFECT_S1_DISAPPEARED;
  431. }
  432. else if (_tickCount >= _template.getTotalTickCount())
  433. {
  434. smId = SystemMessageId.S1_HAS_WORN_OFF;
  435. }
  436. if (smId != null)
  437. {
  438. final SystemMessage sm = SystemMessage.getSystemMessage(smId);
  439. sm.addSkillName(getSkill());
  440. getEffected().sendPacket(sm);
  441. }
  442. }
  443. // if task is null - stopEffectTask does not remove effect
  444. if ((_currentFuture == null) && (getEffected() != null))
  445. {
  446. getEffected().getEffectList().remove(this);
  447. }
  448. // Stop the task of this effect, remove it and update client magic icon.
  449. stopEffectTask();
  450. // Cancel the effect in the the abnormal effect list of the character.
  451. if (isInUse() || !((_tickCount > 1) || (_abnormalTime > 0)))
  452. {
  453. if (_startConditionsCorrect)
  454. {
  455. onExit();
  456. }
  457. }
  458. if (_skill.getAfterEffectId() > 0)
  459. {
  460. final L2Skill skill = SkillTable.getInstance().getInfo(_skill.getAfterEffectId(), _skill.getAfterEffectLvl());
  461. if (skill != null)
  462. {
  463. getEffected().broadcastPacket(new MagicSkillUse(_effected, skill.getId(), skill.getLevel(), 0, 0));
  464. getEffected().broadcastPacket(new MagicSkillLaunched(_effected, skill.getId(), skill.getLevel()));
  465. skill.getEffects(getEffected(), getEffected());
  466. }
  467. }
  468. }
  469. }
  470. }
  471. /**
  472. * Get this effect's stats functions.
  473. * @return a list of stat functions.
  474. */
  475. public List<Func> getStatFuncs()
  476. {
  477. if (_template.getFuncTemplates() == null)
  478. {
  479. return Collections.<Func> emptyList();
  480. }
  481. final List<Func> funcs = new ArrayList<>(_template.getFuncTemplates().size());
  482. final Env env = new Env();
  483. env.setCharacter(_effector);
  484. env.setTarget(_effected);
  485. env.setSkill(_skill);
  486. for (FuncTemplate t : _template.getFuncTemplates())
  487. {
  488. final Func f = t.getFunc(env, this);
  489. if (f != null)
  490. {
  491. funcs.add(f);
  492. }
  493. }
  494. return funcs;
  495. }
  496. /**
  497. * Add the abnormal status update data for this effect.
  498. * @param mi the abnormal status packet
  499. */
  500. public final void addIcon(AbnormalStatusUpdate mi)
  501. {
  502. if (_state != EffectState.ACTING)
  503. {
  504. return;
  505. }
  506. if (_abnormalTime == -1)
  507. {
  508. mi.addEffect(getSkill(), -1);
  509. }
  510. else
  511. {
  512. mi.addEffect(getSkill(), getTimeLeft());
  513. }
  514. }
  515. /**
  516. * Add the party spelled data for this effect.
  517. * @param ps the party spelled packet
  518. */
  519. public final void addPartySpelledIcon(PartySpelled ps)
  520. {
  521. if (_state != EffectState.ACTING)
  522. {
  523. return;
  524. }
  525. final ScheduledFuture<?> future = _currentFuture;
  526. if (future != null)
  527. {
  528. ps.addPartySpelledEffect(getSkill(), (int) future.getDelay(TimeUnit.SECONDS));
  529. }
  530. else if (_abnormalTime == -1)
  531. {
  532. ps.addPartySpelledEffect(getSkill(), -1);
  533. }
  534. }
  535. /**
  536. * Add the olympiad spelled data for this effect.
  537. * @param os the olympiad spelled packet
  538. */
  539. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  540. {
  541. if (_state != EffectState.ACTING)
  542. {
  543. return;
  544. }
  545. final ScheduledFuture<?> future = _currentFuture;
  546. if (future != null)
  547. {
  548. os.addEffect(getSkill(), (int) future.getDelay(TimeUnit.SECONDS));
  549. }
  550. else if (_abnormalTime == -1)
  551. {
  552. os.addEffect(getSkill(), -1);
  553. }
  554. }
  555. public int getPeriodStartTicks()
  556. {
  557. return _periodStartTicks;
  558. }
  559. /**
  560. * Get the effect template.
  561. * @return the effect template
  562. */
  563. public EffectTemplate getEffectTemplate()
  564. {
  565. return _template;
  566. }
  567. /**
  568. * TODO: Unhardcode skill Id.
  569. * @return {@code true} if effect itself can be stolen, {@code false} otherwise
  570. */
  571. public boolean canBeStolen()
  572. {
  573. return !getSkill().isPassive() && !getSkill().isToggle() && !getSkill().isDebuff() && !getSkill().isHeroSkill() && !getSkill().isGMSkill() && !(getSkill().isStatic() && (getSkill().getId() != 2341)) && getSkill().canBeDispeled();
  574. }
  575. /**
  576. * Get the effect flags.
  577. * @return bit flag for current effect
  578. */
  579. public int getEffectFlags()
  580. {
  581. return EffectFlag.NONE.getMask();
  582. }
  583. @Override
  584. public String toString()
  585. {
  586. return "Effect " + getClass().getSimpleName() + ", " + _skill + ", State: " + _state + ", Time: " + _abnormalTime + ", Remaining: " + getTimeLeft();
  587. }
  588. public void decreaseForce()
  589. {
  590. }
  591. public void increaseEffect()
  592. {
  593. }
  594. public boolean checkCondition(Object obj)
  595. {
  596. return true;
  597. }
  598. @Override
  599. public boolean triggersChanceSkill()
  600. {
  601. return false;
  602. }
  603. @Override
  604. public int getTriggeredChanceId()
  605. {
  606. return 0;
  607. }
  608. @Override
  609. public int getTriggeredChanceLevel()
  610. {
  611. return 0;
  612. }
  613. @Override
  614. public ChanceCondition getTriggeredChanceCondition()
  615. {
  616. return null;
  617. }
  618. public boolean isPreventExitUpdate()
  619. {
  620. return _preventExitUpdate;
  621. }
  622. public void setPreventExitUpdate(boolean val)
  623. {
  624. _preventExitUpdate = val;
  625. }
  626. /**
  627. * Verify if this effect is an instant effect.
  628. * @return {@code true} if this effect is instant, {@code false} otherwise
  629. */
  630. public boolean isInstant()
  631. {
  632. return false;
  633. }
  634. /**
  635. * Verify if this effect has been cancelled.
  636. * @return {@code true} if this effect has been cancelled, {@code false} otherwise
  637. */
  638. public boolean isRemoved()
  639. {
  640. return _isRemoved;
  641. }
  642. /**
  643. * Set the effect to removed.
  644. * @param val the value to set
  645. */
  646. public void setRemoved(boolean val)
  647. {
  648. _isRemoved = val;
  649. }
  650. }