L2Effect.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. public boolean preventExitUpdate;
  94. public final class EffectTask implements Runnable
  95. {
  96. protected final int _delay;
  97. protected final int _rate;
  98. EffectTask(int pDelay, int pRate)
  99. {
  100. _delay = pDelay;
  101. _rate = pRate;
  102. }
  103. public void run()
  104. {
  105. try
  106. {
  107. if (getPeriodfirsttime() == 0)
  108. setPeriodStartTicks(GameTimeController.getGameTicks());
  109. else
  110. setPeriodfirsttime(0);
  111. L2Effect.this.scheduleEffect();
  112. }
  113. catch (Exception e)
  114. {
  115. _log.log(Level.SEVERE, "", e);
  116. }
  117. }
  118. }
  119. private ScheduledFuture<?> _currentFuture;
  120. private EffectTask _currentTask;
  121. /** The Identifier of the stack group */
  122. private final String _stackType;
  123. /** The position of the effect in the stack group */
  124. private final float _stackOrder;
  125. private boolean _inUse = false;
  126. private boolean _startConditionsCorrect = true;
  127. /**
  128. * For special behavior. See Formulas.calcEffectSuccess
  129. */
  130. private double _effectPower;
  131. private L2SkillType _effectSkillType;
  132. /**
  133. * <font color="FF0000"><b>WARNING: scheduleEffect nolonger inside constructor</b></font><br>
  134. * So you must call it explicitly
  135. */
  136. protected L2Effect(Env env, EffectTemplate template)
  137. {
  138. _state = EffectState.CREATED;
  139. _skill = env.skill;
  140. //_item = env._item == null ? null : env._item.getItem();
  141. _template = template;
  142. _effected = env.target;
  143. _effector = env.player;
  144. _lambda = template.lambda;
  145. _funcTemplates = template.funcTemplates;
  146. _count = template.counter;
  147. _totalCount = _count;
  148. // Support for retail herbs duration when _effected has a Summon
  149. int temp = template.period;
  150. if ((_skill.getId() > 2277 && _skill.getId() < 2286) || (_skill.getId() >= 2512 && _skill.getId() <= 2514))
  151. {
  152. if (_effected instanceof L2SummonInstance ||
  153. (_effected instanceof L2PcInstance && ((L2PcInstance) _effected).getPet() instanceof L2SummonInstance))
  154. {
  155. temp /= 2;
  156. }
  157. }
  158. if (env.skillMastery)
  159. temp *= 2;
  160. _period = temp;
  161. _abnormalEffect = template.abnormalEffect;
  162. _specialEffect = template.specialEffect;
  163. _eventEffect = template.eventEffect;
  164. _stackType = template.stackType;
  165. _stackOrder = template.stackOrder;
  166. _periodStartTicks = GameTimeController.getGameTicks();
  167. _periodfirsttime = 0;
  168. _icon = template.icon;
  169. _effectPower = template.effectPower;
  170. _effectSkillType = template.effectType;
  171. /*
  172. * Commented out by DrHouse:
  173. * scheduleEffect can call onStart before effect is completly
  174. * initialized on constructor (child classes constructor)
  175. */
  176. //scheduleEffect();
  177. }
  178. /**
  179. * Special constructor to "steal" buffs. Must be implemented on
  180. * every child class that can be stolen.<br><br>
  181. *
  182. * <font color="FF0000"><b>WARNING: scheduleEffect nolonger inside constructor</b></font>
  183. * <br>So you must call it explicitly
  184. * @param env
  185. * @param effect
  186. */
  187. protected L2Effect(Env env, L2Effect effect)
  188. {
  189. _template = effect._template;
  190. _state = EffectState.CREATED;
  191. _skill = env.skill;
  192. _effected = env.target;
  193. _effector = env.player;
  194. _lambda = _template.lambda;
  195. _funcTemplates = _template.funcTemplates;
  196. _count = effect.getCount();
  197. _totalCount = _template.counter;
  198. _period = _template.period - effect.getTime();
  199. _abnormalEffect = _template.abnormalEffect;
  200. _specialEffect = _template.specialEffect;
  201. _eventEffect = _template.eventEffect;
  202. _stackType = _template.stackType;
  203. _stackOrder = _template.stackOrder;
  204. _periodStartTicks = effect.getPeriodStartTicks();
  205. _periodfirsttime = effect.getPeriodfirsttime();
  206. _icon = _template.icon;
  207. /*
  208. * Commented out by DrHouse:
  209. * scheduleEffect can call onStart before effect is completly
  210. * initialized on constructor (child classes constructor)
  211. */
  212. //scheduleEffect();
  213. }
  214. public int getCount()
  215. {
  216. return _count;
  217. }
  218. public int getTotalCount()
  219. {
  220. return _totalCount;
  221. }
  222. public void setCount(int newcount)
  223. {
  224. _count = newcount;
  225. }
  226. public void setFirstTime(int newfirsttime)
  227. {
  228. if (_currentFuture != null)
  229. {
  230. if (newfirsttime > _period) // sanity check
  231. newfirsttime = _period;
  232. _periodStartTicks = GameTimeController.getGameTicks() - newfirsttime * GameTimeController.TICKS_PER_SECOND;
  233. _currentFuture.cancel(false);
  234. _currentFuture = null;
  235. _currentTask = null;
  236. _periodfirsttime = newfirsttime;
  237. int duration = _period - _periodfirsttime;
  238. //_log.warning("Period: "+_period+"-"+_periodfirsttime+"="+duration);
  239. _currentTask = new EffectTask(duration * 1000, -1);
  240. if (_count > 1)
  241. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_currentTask, 5, duration * 1000);
  242. else
  243. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(_currentTask, duration * 1000);
  244. }
  245. }
  246. public boolean getShowIcon()
  247. {
  248. return _icon;
  249. }
  250. public int getPeriod()
  251. {
  252. return _period;
  253. }
  254. public int getTime()
  255. {
  256. return (GameTimeController.getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND;
  257. }
  258. /**
  259. * Returns the elapsed time of the task.
  260. * @return Time in seconds.
  261. */
  262. public int getTaskTime()
  263. {
  264. if (_count == _totalCount)
  265. return 0;
  266. return (Math.abs(_count - _totalCount + 1) * _period) + getTime() + 1;
  267. }
  268. public boolean getInUse()
  269. {
  270. return _inUse;
  271. }
  272. public boolean setInUse(boolean inUse)
  273. {
  274. _inUse = inUse;
  275. if (_inUse)
  276. _startConditionsCorrect = onStart();
  277. else
  278. onExit();
  279. return _startConditionsCorrect;
  280. }
  281. public String getStackType()
  282. {
  283. return _stackType;
  284. }
  285. public float getStackOrder()
  286. {
  287. return _stackOrder;
  288. }
  289. public final L2Skill getSkill()
  290. {
  291. return _skill;
  292. }
  293. public final L2Character getEffector()
  294. {
  295. return _effector;
  296. }
  297. public final L2Character getEffected()
  298. {
  299. return _effected;
  300. }
  301. public boolean isSelfEffect()
  302. {
  303. return _skill._effectTemplatesSelf != null;
  304. }
  305. public boolean isHerbEffect()
  306. {
  307. if (getSkill().getName().contains("Herb"))
  308. return true;
  309. return false;
  310. }
  311. public final double calc()
  312. {
  313. Env env = new Env();
  314. env.player = _effector;
  315. env.target = _effected;
  316. env.skill = _skill;
  317. return _lambda.calc(env);
  318. }
  319. private synchronized void startEffectTask(int duration)
  320. {
  321. if (duration >= 0)
  322. {
  323. stopEffectTask();
  324. _currentTask = new EffectTask(duration, -1);
  325. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(_currentTask, duration);
  326. }
  327. if (_state == EffectState.ACTING)
  328. {
  329. _effected.addEffect(this);
  330. }
  331. }
  332. private synchronized void startEffectTaskAtFixedRate(int delay, int rate)
  333. {
  334. stopEffectTask();
  335. _currentTask = new EffectTask(delay, rate);
  336. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_currentTask, delay, rate);
  337. if (_state == EffectState.ACTING)
  338. _effected.addEffect(this);
  339. }
  340. /**
  341. * Stop the L2Effect task and send Server->Client update packet.<BR><BR>
  342. *
  343. * <B><U> Actions</U> :</B><BR><BR>
  344. * <li>Cancel the effect in the the abnormal effect map of the L2Character </li>
  345. * <li>Stop the task of the L2Effect, remove it and update client magic icon </li><BR><BR>
  346. *
  347. */
  348. public final void exit()
  349. {
  350. this.exit(false);
  351. }
  352. public final void exit(boolean preventUpdate)
  353. {
  354. preventExitUpdate = preventUpdate;
  355. _state = EffectState.FINISHING;
  356. scheduleEffect();
  357. }
  358. /**
  359. * Stop the task of the L2Effect, remove it and update client magic icon.<BR><BR>
  360. *
  361. * <B><U> Actions</U> :</B><BR><BR>
  362. * <li>Cancel the task </li>
  363. * <li>Stop and remove L2Effect from L2Character and update client magic icon </li><BR><BR>
  364. *
  365. */
  366. public void stopEffectTask()
  367. {
  368. if (_currentFuture != null)
  369. {
  370. synchronized (_currentFuture)
  371. {
  372. if (_currentFuture != null)
  373. {
  374. // Cancel the task
  375. _currentFuture.cancel(false);
  376. ThreadPoolManager.getInstance().removeEffect(_currentTask);
  377. }
  378. }
  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. }