L2Effect.java 18 KB

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