L2Effect.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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. protected final class EffectTask implements Runnable
  82. {
  83. @Override
  84. public void run()
  85. {
  86. try
  87. {
  88. _periodFirstTime = 0;
  89. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  90. scheduleEffect();
  91. }
  92. catch (Exception e)
  93. {
  94. _log.log(Level.SEVERE, "", e);
  95. }
  96. }
  97. }
  98. /**
  99. * @param env DTO with required data
  100. * @param template the effect template
  101. */
  102. protected L2Effect(Env env, EffectTemplate template)
  103. {
  104. _state = EffectState.CREATED;
  105. _skill = env.getSkill();
  106. _template = template;
  107. _effected = env.getTarget();
  108. _effector = env.getCharacter();
  109. _lambda = template.getLambda();
  110. _tickCount = 1;
  111. _abnormalTime = Formulas.calcEffectAbnormalTime(env, template);
  112. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  113. _periodFirstTime = 0;
  114. }
  115. /**
  116. * Special constructor to "steal" buffs.<br>
  117. * Must be implemented on every child class that can be stolen.
  118. * @param env DTO with required data
  119. * @param effect the stolen effect, used as "template"
  120. */
  121. protected L2Effect(Env env, L2Effect effect)
  122. {
  123. _template = effect._template;
  124. _state = EffectState.CREATED;
  125. _skill = env.getSkill();
  126. _effected = env.getTarget();
  127. _effector = env.getCharacter();
  128. _lambda = _template.getLambda();
  129. _tickCount = effect.getTickCount();
  130. _abnormalTime = effect.getAbnormalTime();
  131. _periodStartTicks = effect.getPeriodStartTicks();
  132. _periodFirstTime = effect.getTime();
  133. }
  134. public int getTickCount()
  135. {
  136. return _tickCount;
  137. }
  138. public int getTotalTickCount()
  139. {
  140. return _template.getTotalTickCount();
  141. }
  142. public void setCount(int newTickCount)
  143. {
  144. _tickCount = Math.min(newTickCount, _template.getTotalTickCount());
  145. }
  146. public void setFirstTime(int newFirstTime)
  147. {
  148. _periodFirstTime = Math.min(newFirstTime, _abnormalTime);
  149. _periodStartTicks -= _periodFirstTime * GameTimeController.TICKS_PER_SECOND;
  150. }
  151. /**
  152. * @return {@code true} if this effect display an icon, {@code false} otherwise
  153. */
  154. public boolean isIconDisplay()
  155. {
  156. return _template.isIconDisplay();
  157. }
  158. /**
  159. * @return this effect's calculated abnormal time
  160. */
  161. public int getAbnormalTime()
  162. {
  163. return _abnormalTime;
  164. }
  165. public int getTime()
  166. {
  167. return (GameTimeController.getInstance().getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND;
  168. }
  169. /**
  170. * Get the elapsed time.
  171. * @return the elapsed time of the task in seconds
  172. */
  173. public int getTaskTime()
  174. {
  175. return (_tickCount * _abnormalTime) + getTime();
  176. }
  177. /**
  178. * @return {@code true} if the effect is in use, {@code false} otherwise
  179. */
  180. public boolean isInUse()
  181. {
  182. return _inUse;
  183. }
  184. public boolean setInUse(boolean inUse)
  185. {
  186. _inUse = inUse;
  187. if (_inUse)
  188. {
  189. _startConditionsCorrect = onStart();
  190. }
  191. else
  192. {
  193. onExit();
  194. }
  195. return _startConditionsCorrect;
  196. }
  197. /**
  198. * Get the skill that launched this effect.
  199. * @return the skill related to this effect
  200. */
  201. public final L2Skill getSkill()
  202. {
  203. return _skill;
  204. }
  205. /**
  206. * Get the character that evoked this effect.
  207. * @return the effector
  208. */
  209. public final L2Character getEffector()
  210. {
  211. return _effector;
  212. }
  213. /**
  214. * Get the character that received this effect.
  215. * @return the effected
  216. */
  217. public final L2Character getEffected()
  218. {
  219. return _effected;
  220. }
  221. public boolean isSelfEffect()
  222. {
  223. return _isSelfEffect;
  224. }
  225. public void setSelfEffect()
  226. {
  227. _isSelfEffect = true;
  228. }
  229. public final double calc()
  230. {
  231. final Env env = new Env();
  232. env.setCharacter(_effector);
  233. env.setTarget(_effected);
  234. env.setSkill(_skill);
  235. return _lambda.calc(env);
  236. }
  237. /**
  238. * Calculates whether this effects land or not.<br>
  239. * If it lands will be scheduled and added to the character effect list.<br>
  240. * Override in effect implementation to change behavior.
  241. * @return {@code true} if this effect land, {@code false} otherwise
  242. */
  243. public boolean calcSuccess()
  244. {
  245. final Env env = new Env();
  246. env.setSkillMastery(Formulas.calcSkillMastery(getEffector(), getSkill()));
  247. env.setCharacter(getEffector());
  248. env.setTarget(getEffected());
  249. env.setSkill(getSkill());
  250. env.setEffect(this);
  251. return Formulas.calcEffectSuccess(env);
  252. }
  253. /**
  254. * Start the effect task.<br>
  255. * If the effect has ticks defined it will be scheduled.<br>
  256. * If abnormal time is defined (greater than 1) the period will be calculated like abnormal time divided total tick count.<br>
  257. * Otherwise it each tick will represent 1 second (1000 milliseconds).
  258. */
  259. private final void startEffectTask()
  260. {
  261. stopEffectTask();
  262. final int initialDelay = Math.max((_abnormalTime - _periodFirstTime) * 1000, 5); // Sanity check
  263. if (_template.getTotalTickCount() > 1)
  264. {
  265. // TODO: If default abnormal time is changed to 0, the first check below must be updated as well.
  266. final int period = ((_abnormalTime > 1) ? (_abnormalTime / _template.getTotalTickCount()) : _template.getTotalTickCount()) * 1000;
  267. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(new EffectTask(), initialDelay, period);
  268. }
  269. else
  270. {
  271. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(new EffectTask(), initialDelay);
  272. }
  273. }
  274. /**
  275. * Exit this effect without preventing an update.
  276. */
  277. public final void exit()
  278. {
  279. exit(false);
  280. }
  281. /**
  282. * Exit this effect.
  283. * @param preventExitUpdate
  284. */
  285. public final void exit(boolean preventExitUpdate)
  286. {
  287. _preventExitUpdate = preventExitUpdate;
  288. _state = EffectState.FINISHING;
  289. scheduleEffect();
  290. }
  291. /**
  292. * Stop the task of this effect, remove it and update client magic icon.
  293. */
  294. public final void stopEffectTask()
  295. {
  296. if (_currentFuture != null)
  297. {
  298. // Cancel the task
  299. _currentFuture.cancel(false);
  300. _currentFuture = null;
  301. if (getEffected() != null)
  302. {
  303. getEffected().getEffectList().remove(this);
  304. }
  305. }
  306. }
  307. /**
  308. * @return the effect type
  309. */
  310. public abstract L2EffectType getEffectType();
  311. /**
  312. * Notify started.
  313. * @return {@code true} if all the start conditions are meet, {@code false} otherwise
  314. */
  315. public boolean onStart()
  316. {
  317. if (_template.getAbnormalEffect() != AbnormalEffect.NULL)
  318. {
  319. getEffected().startAbnormalEffect(_template.getAbnormalEffect());
  320. }
  321. if (_template.getSpecialEffect() != null)
  322. {
  323. getEffected().startSpecialEffect(_template.getSpecialEffect());
  324. }
  325. if ((_template.getEventEffect() != AbnormalEffect.NULL) && getEffected().isPlayer())
  326. {
  327. getEffected().getActingPlayer().startEventEffect(_template.getEventEffect());
  328. }
  329. return true;
  330. }
  331. /**
  332. * Cancel the effect in the the abnormal effect map of the effected L2Character.
  333. */
  334. public void onExit()
  335. {
  336. if (_template.getAbnormalEffect() != AbnormalEffect.NULL)
  337. {
  338. getEffected().stopAbnormalEffect(_template.getAbnormalEffect());
  339. }
  340. if (_template.getSpecialEffect() != null)
  341. {
  342. getEffected().stopSpecialEffect(_template.getSpecialEffect());
  343. }
  344. if ((_template.getEventEffect() != AbnormalEffect.NULL) && getEffected().isPlayer())
  345. {
  346. getEffected().getActingPlayer().stopEventEffect(_template.getEventEffect());
  347. }
  348. }
  349. /**
  350. * Method called on each tick.
  351. * @return {@code true} for continuation of this effect, {@code false} otherwise
  352. */
  353. public boolean onActionTime()
  354. {
  355. return false;
  356. }
  357. public final void scheduleEffect()
  358. {
  359. switch (_state)
  360. {
  361. case CREATED:
  362. {
  363. _state = EffectState.ACTING;
  364. if (_skill.isPVP() && isIconDisplay() && getEffected().isPlayer())
  365. {
  366. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  367. sm.addSkillName(_skill);
  368. getEffected().sendPacket(sm);
  369. }
  370. if (_abnormalTime != 0)
  371. {
  372. startEffectTask();
  373. return;
  374. }
  375. _startConditionsCorrect = onStart();
  376. }
  377. case ACTING:
  378. {
  379. if (isInUse())
  380. {
  381. if (onActionTime() && _startConditionsCorrect && (_tickCount <= _template.getTotalTickCount()))
  382. {
  383. return; // false causes effect to finish right away
  384. }
  385. _tickCount++; // Increase tick count.
  386. }
  387. if (_tickCount <= _template.getTotalTickCount())
  388. {
  389. return; // Do not finish it yet, has remaining ticks.
  390. }
  391. _state = EffectState.FINISHING;
  392. }
  393. case FINISHING:
  394. {
  395. // If the time left is equal to zero, send the message
  396. if ((_tickCount >= _template.getTotalTickCount()) && isIconDisplay() && getEffected().isPlayer())
  397. {
  398. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.S1_HAS_WORN_OFF);
  399. sm.addSkillName(_skill);
  400. getEffected().sendPacket(sm);
  401. }
  402. // if task is null - stopEffectTask does not remove effect
  403. if ((_currentFuture == null) && (getEffected() != null))
  404. {
  405. getEffected().getEffectList().remove(this);
  406. }
  407. // Stop the task of the L2Effect, remove it and update client magic icon
  408. stopEffectTask();
  409. // Cancel the effect in the the abnormal effect map of the L2Character
  410. if (isInUse() || !((_tickCount > 1) || (_abnormalTime > 0)))
  411. {
  412. if (_startConditionsCorrect)
  413. {
  414. onExit();
  415. }
  416. }
  417. if (_skill.getAfterEffectId() > 0)
  418. {
  419. L2Skill skill = SkillTable.getInstance().getInfo(_skill.getAfterEffectId(), _skill.getAfterEffectLvl());
  420. if (skill != null)
  421. {
  422. getEffected().broadcastPacket(new MagicSkillUse(_effected, skill.getId(), skill.getLevel(), 0, 0));
  423. getEffected().broadcastPacket(new MagicSkillLaunched(_effected, skill.getId(), skill.getLevel()));
  424. skill.getEffects(getEffected(), getEffected());
  425. }
  426. }
  427. }
  428. }
  429. }
  430. public List<Func> getStatFuncs()
  431. {
  432. if (_template.getFuncTemplates() == null)
  433. {
  434. return Collections.<Func> emptyList();
  435. }
  436. final List<Func> funcs = new ArrayList<>(_template.getFuncTemplates().size());
  437. final Env env = new Env();
  438. env.setCharacter(_effector);
  439. env.setTarget(_effected);
  440. env.setSkill(_skill);
  441. for (FuncTemplate t : _template.getFuncTemplates())
  442. {
  443. final Func f = t.getFunc(env, this);
  444. if (f != null)
  445. {
  446. funcs.add(f);
  447. }
  448. }
  449. return funcs;
  450. }
  451. public final void addIcon(AbnormalStatusUpdate mi)
  452. {
  453. if (_state != EffectState.ACTING)
  454. {
  455. return;
  456. }
  457. final ScheduledFuture<?> future = _currentFuture;
  458. final L2Skill sk = getSkill();
  459. if (_template.getTotalTickCount() > 1)
  460. {
  461. if (sk.isStatic())
  462. {
  463. mi.addEffect(sk.getDisplayId(), sk.getDisplayLevel(), (_abnormalTime - getTaskTime()) * 1000);
  464. }
  465. else
  466. {
  467. mi.addEffect(sk.getDisplayId(), sk.getDisplayLevel(), -1);
  468. }
  469. }
  470. else if (future != null)
  471. {
  472. mi.addEffect(sk.getDisplayId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  473. }
  474. else if (_abnormalTime == -1)
  475. {
  476. mi.addEffect(sk.getDisplayId(), getLevel(), _abnormalTime);
  477. }
  478. }
  479. public final void addPartySpelledIcon(PartySpelled ps)
  480. {
  481. if (_state != EffectState.ACTING)
  482. {
  483. return;
  484. }
  485. final ScheduledFuture<?> future = _currentFuture;
  486. final L2Skill sk = getSkill();
  487. if (future != null)
  488. {
  489. ps.addPartySpelledEffect(sk.getDisplayId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  490. }
  491. else if (_abnormalTime == -1)
  492. {
  493. ps.addPartySpelledEffect(sk.getDisplayId(), getLevel(), _abnormalTime);
  494. }
  495. }
  496. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  497. {
  498. if (_state != EffectState.ACTING)
  499. {
  500. return;
  501. }
  502. final ScheduledFuture<?> future = _currentFuture;
  503. final L2Skill sk = getSkill();
  504. if (future != null)
  505. {
  506. os.addEffect(sk.getDisplayId(), sk.getDisplayLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  507. }
  508. else if (_abnormalTime == -1)
  509. {
  510. os.addEffect(sk.getDisplayId(), sk.getDisplayLevel(), _abnormalTime);
  511. }
  512. }
  513. public int getLevel()
  514. {
  515. return getSkill().getLevel();
  516. }
  517. public int getPeriodStartTicks()
  518. {
  519. return _periodStartTicks;
  520. }
  521. public EffectTemplate getEffectTemplate()
  522. {
  523. return _template;
  524. }
  525. public double getEffectPower()
  526. {
  527. return _template.getEffectPower();
  528. }
  529. /**
  530. * TODO: Unhardcode skill Id.
  531. * @return {@code true} if effect itself can be stolen, {@code false} otherwise
  532. */
  533. public boolean canBeStolen()
  534. {
  535. return (getEffectType() != L2EffectType.TRANSFORMATION) && !getSkill().isPassive() && !getSkill().isToggle() && !getSkill().isDebuff() && !getSkill().isHeroSkill() && !getSkill().isGMSkill() && !(getSkill().isStatic() && ((getSkill().getId() != 2274) && (getSkill().getId() != 2341))) && getSkill().canBeDispeled();
  536. }
  537. /**
  538. * @return bit flag for current effect
  539. */
  540. public int getEffectFlags()
  541. {
  542. return EffectFlag.NONE.getMask();
  543. }
  544. @Override
  545. public String toString()
  546. {
  547. return "Effect " + getClass().getSimpleName() + ", " + _skill + ", State: " + _state + ", Abnormal time: " + _abnormalTime;
  548. }
  549. public void decreaseForce()
  550. {
  551. }
  552. public void increaseEffect()
  553. {
  554. }
  555. public int getForceEffect()
  556. {
  557. return 0;
  558. }
  559. @Override
  560. public boolean triggersChanceSkill()
  561. {
  562. return false;
  563. }
  564. @Override
  565. public int getTriggeredChanceId()
  566. {
  567. return 0;
  568. }
  569. @Override
  570. public int getTriggeredChanceLevel()
  571. {
  572. return 0;
  573. }
  574. @Override
  575. public ChanceCondition getTriggeredChanceCondition()
  576. {
  577. return null;
  578. }
  579. public boolean isPreventExitUpdate()
  580. {
  581. return _preventExitUpdate;
  582. }
  583. public void setPreventExitUpdate(boolean val)
  584. {
  585. _preventExitUpdate = val;
  586. }
  587. }