L2Effect.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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.
  260. * @return {@code true} if this effect land, {@code false} otherwise
  261. */
  262. public boolean calcSuccess()
  263. {
  264. final Env env = new Env();
  265. env.setSkillMastery(Formulas.calcSkillMastery(getEffector(), getSkill()));
  266. env.setCharacter(getEffector());
  267. env.setTarget(getEffected());
  268. env.setSkill(getSkill());
  269. env.setEffect(this);
  270. return Formulas.calcEffectSuccess(env);
  271. }
  272. /**
  273. * Start the effect task.<br>
  274. * If the effect has ticks defined it will be scheduled.<br>
  275. * If abnormal time is defined (greater than 1) the period will be calculated like abnormal time divided total tick count.<br>
  276. * Otherwise it each tick will represent 1 second (1000 milliseconds).
  277. */
  278. private final void startEffectTask()
  279. {
  280. stopEffectTask();
  281. if (isInstant())
  282. {
  283. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(new EffectTask(), 5);
  284. return;
  285. }
  286. final int delay = Math.max((_abnormalTime - _periodFirstTime) * 1000, 5); // Sanity check
  287. if (_template.getTotalTickCount() > 0)
  288. {
  289. // TODO: If default abnormal time is changed to 0, the first check below must be updated as well.
  290. final int period = ((_abnormalTime > 1) ? (_abnormalTime / _template.getTotalTickCount()) : _template.getTotalTickCount()) * 1000;
  291. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(new EffectTask(), delay / period, period);
  292. }
  293. else
  294. {
  295. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(new EffectTask(), delay);
  296. }
  297. }
  298. /**
  299. * Exit this effect without preventing an update.
  300. */
  301. public final void exit()
  302. {
  303. exit(false);
  304. }
  305. /**
  306. * Exit this effect.
  307. * @param preventExitUpdate
  308. */
  309. public final void exit(boolean preventExitUpdate)
  310. {
  311. _preventExitUpdate = preventExitUpdate;
  312. _state = EffectState.FINISHING;
  313. scheduleEffect();
  314. }
  315. /**
  316. * Stop the task of this effect, remove it and update client magic icon.
  317. */
  318. public final void stopEffectTask()
  319. {
  320. if (_currentFuture != null)
  321. {
  322. // Cancel the task
  323. _currentFuture.cancel(false);
  324. _currentFuture = null;
  325. if (getEffected() != null)
  326. {
  327. getEffected().getEffectList().remove(this);
  328. }
  329. }
  330. }
  331. /**
  332. * Get this effect's type.
  333. * @return the effect type
  334. */
  335. public abstract L2EffectType getEffectType();
  336. /**
  337. * Notify started.
  338. * @return {@code true} if all the start conditions are meet, {@code false} otherwise
  339. */
  340. public boolean onStart()
  341. {
  342. if (_template.getAbnormalEffect() != AbnormalEffect.NULL)
  343. {
  344. getEffected().startAbnormalEffect(_template.getAbnormalEffect());
  345. }
  346. if (_template.getSpecialEffect() != null)
  347. {
  348. getEffected().startSpecialEffect(_template.getSpecialEffect());
  349. }
  350. if ((_template.getEventEffect() != AbnormalEffect.NULL) && getEffected().isPlayer())
  351. {
  352. getEffected().getActingPlayer().startEventEffect(_template.getEventEffect());
  353. }
  354. return true;
  355. }
  356. /**
  357. * Cancel the effect in the the abnormal effect map of the effected L2Character.
  358. */
  359. public void onExit()
  360. {
  361. if (_template.getAbnormalEffect() != AbnormalEffect.NULL)
  362. {
  363. getEffected().stopAbnormalEffect(_template.getAbnormalEffect());
  364. }
  365. if (_template.getSpecialEffect() != null)
  366. {
  367. getEffected().stopSpecialEffect(_template.getSpecialEffect());
  368. }
  369. if ((_template.getEventEffect() != AbnormalEffect.NULL) && getEffected().isPlayer())
  370. {
  371. getEffected().getActingPlayer().stopEventEffect(_template.getEventEffect());
  372. }
  373. }
  374. /**
  375. * Method called on each tick.<br>
  376. * By default if the abnormal time is lesser than zero it will return {@code true}, this means the effect will last forever.
  377. * @return if {@code true} this effect will continue forever, if {@code false} it will stop after tick count is reached
  378. */
  379. public boolean onActionTime()
  380. {
  381. return getAbnormalTime() < 0;
  382. }
  383. /**
  384. * Schedule this effect.
  385. */
  386. public final void scheduleEffect()
  387. {
  388. switch (_state)
  389. {
  390. case CREATED:
  391. {
  392. _state = isInstant() ? EffectState.FINISHING : EffectState.ACTING;
  393. if (_skill.isPVP() && isIconDisplay() && getEffected().isPlayer())
  394. {
  395. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  396. sm.addSkillName(_skill);
  397. getEffected().sendPacket(sm);
  398. }
  399. if (_abnormalTime != 0)
  400. {
  401. startEffectTask();
  402. return;
  403. }
  404. _startConditionsCorrect = onStart();
  405. }
  406. case ACTING:
  407. {
  408. if (isInUse())
  409. {
  410. _tickCount++; // Increase tick count.
  411. if (onActionTime() && _startConditionsCorrect)
  412. {
  413. return; // Do not finish.
  414. }
  415. }
  416. if (_tickCount <= _template.getTotalTickCount())
  417. {
  418. return; // Do not finish it yet, has remaining ticks.
  419. }
  420. _state = EffectState.FINISHING;
  421. }
  422. case FINISHING:
  423. {
  424. // Message
  425. if (getEffected().isPlayer() && isIconDisplay())
  426. {
  427. SystemMessageId smId = null;
  428. if (getSkill().isToggle())
  429. {
  430. smId = SystemMessageId.S1_HAS_BEEN_ABORTED;
  431. }
  432. else if (isRemoved())
  433. {
  434. smId = SystemMessageId.EFFECT_S1_DISAPPEARED;
  435. }
  436. else if (_tickCount >= _template.getTotalTickCount())
  437. {
  438. smId = SystemMessageId.S1_HAS_WORN_OFF;
  439. }
  440. if (smId != null)
  441. {
  442. final SystemMessage sm = SystemMessage.getSystemMessage(smId);
  443. sm.addSkillName(getSkill());
  444. getEffected().sendPacket(sm);
  445. }
  446. }
  447. // if task is null - stopEffectTask does not remove effect
  448. if ((_currentFuture == null) && (getEffected() != null))
  449. {
  450. getEffected().getEffectList().remove(this);
  451. }
  452. // Stop the task of this effect, remove it and update client magic icon.
  453. stopEffectTask();
  454. // Cancel the effect in the the abnormal effect list of the character.
  455. if (isInUse() || !((_tickCount > 1) || (_abnormalTime > 0)))
  456. {
  457. if (_startConditionsCorrect)
  458. {
  459. onExit();
  460. }
  461. }
  462. if (_skill.getAfterEffectId() > 0)
  463. {
  464. final L2Skill skill = SkillTable.getInstance().getInfo(_skill.getAfterEffectId(), _skill.getAfterEffectLvl());
  465. if (skill != null)
  466. {
  467. getEffected().broadcastPacket(new MagicSkillUse(_effected, skill.getId(), skill.getLevel(), 0, 0));
  468. getEffected().broadcastPacket(new MagicSkillLaunched(_effected, skill.getId(), skill.getLevel()));
  469. skill.getEffects(getEffected(), getEffected());
  470. }
  471. }
  472. }
  473. }
  474. }
  475. /**
  476. * Get this effect's stats functions.
  477. * @return a list of stat functions.
  478. */
  479. public List<Func> getStatFuncs()
  480. {
  481. if (_template.getFuncTemplates() == null)
  482. {
  483. return Collections.<Func> emptyList();
  484. }
  485. final List<Func> funcs = new ArrayList<>(_template.getFuncTemplates().size());
  486. final Env env = new Env();
  487. env.setCharacter(_effector);
  488. env.setTarget(_effected);
  489. env.setSkill(_skill);
  490. for (FuncTemplate t : _template.getFuncTemplates())
  491. {
  492. final Func f = t.getFunc(env, this);
  493. if (f != null)
  494. {
  495. funcs.add(f);
  496. }
  497. }
  498. return funcs;
  499. }
  500. /**
  501. * Add the abnormal status update data for this effect.
  502. * @param mi the abnormal status packet
  503. */
  504. public final void addIcon(AbnormalStatusUpdate mi)
  505. {
  506. if (_state != EffectState.ACTING)
  507. {
  508. return;
  509. }
  510. if (_abnormalTime == -1)
  511. {
  512. mi.addEffect(getSkill(), -1);
  513. }
  514. else
  515. {
  516. mi.addEffect(getSkill(), getTimeLeft());
  517. }
  518. }
  519. /**
  520. * Add the party spelled data for this effect.
  521. * @param ps the party spelled packet
  522. */
  523. public final void addPartySpelledIcon(PartySpelled ps)
  524. {
  525. if (_state != EffectState.ACTING)
  526. {
  527. return;
  528. }
  529. final ScheduledFuture<?> future = _currentFuture;
  530. if (future != null)
  531. {
  532. ps.addPartySpelledEffect(getSkill(), (int) future.getDelay(TimeUnit.SECONDS));
  533. }
  534. else if (_abnormalTime == -1)
  535. {
  536. ps.addPartySpelledEffect(getSkill(), -1);
  537. }
  538. }
  539. /**
  540. * Add the olympiad spelled data for this effect.
  541. * @param os the olympiad spelled packet
  542. */
  543. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  544. {
  545. if (_state != EffectState.ACTING)
  546. {
  547. return;
  548. }
  549. final ScheduledFuture<?> future = _currentFuture;
  550. if (future != null)
  551. {
  552. os.addEffect(getSkill(), (int) future.getDelay(TimeUnit.SECONDS));
  553. }
  554. else if (_abnormalTime == -1)
  555. {
  556. os.addEffect(getSkill(), -1);
  557. }
  558. }
  559. public int getPeriodStartTicks()
  560. {
  561. return _periodStartTicks;
  562. }
  563. /**
  564. * Get the effect template.
  565. * @return the effect template
  566. */
  567. public EffectTemplate getEffectTemplate()
  568. {
  569. return _template;
  570. }
  571. /**
  572. * TODO: Unhardcode skill Id.
  573. * @return {@code true} if effect itself can be stolen, {@code false} otherwise
  574. */
  575. public boolean canBeStolen()
  576. {
  577. return !getSkill().isPassive() && !getSkill().isToggle() && !getSkill().isDebuff() && !getSkill().isHeroSkill() && !getSkill().isGMSkill() && !(getSkill().isStatic() && (getSkill().getId() != 2341)) && getSkill().canBeDispeled();
  578. }
  579. /**
  580. * Get the effect flags.
  581. * @return bit flag for current effect
  582. */
  583. public int getEffectFlags()
  584. {
  585. return EffectFlag.NONE.getMask();
  586. }
  587. @Override
  588. public String toString()
  589. {
  590. return "Effect " + getClass().getSimpleName() + ", " + _skill + ", State: " + _state + ", Time: " + _abnormalTime + ", Remaining: " + getTimeLeft();
  591. }
  592. public void decreaseForce()
  593. {
  594. }
  595. public void increaseEffect()
  596. {
  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. }