L2Effect.java 19 KB

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