L2Effect.java 18 KB

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