L2Effect.java 18 KB

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