2
0

L2Effect.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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 synchronized void stopEffectTask()
  367. {
  368. if (_currentFuture != null)
  369. {
  370. // Cancel the task
  371. _currentFuture.cancel(false);
  372. ThreadPoolManager.getInstance().removeEffect(_currentTask);
  373. _currentFuture = null;
  374. _currentTask = null;
  375. if (getEffected() != null)
  376. getEffected().removeEffect(this);
  377. }
  378. }
  379. /** returns effect type */
  380. public abstract L2EffectType getEffectType();
  381. /** Notify started */
  382. public boolean onStart()
  383. {
  384. if (_abnormalEffect != AbnormalEffect.NULL)
  385. getEffected().startAbnormalEffect(_abnormalEffect);
  386. if (_specialEffect != AbnormalEffect.NULL)
  387. getEffected().startSpecialEffect(_specialEffect);
  388. if (_eventEffect != AbnormalEffect.NULL && getEffected().getInstanceType() == InstanceType.L2PcInstance)
  389. getEffected().getActingPlayer().startEventEffect(_eventEffect);
  390. return true;
  391. }
  392. /**
  393. * Cancel the effect in the the abnormal effect map of the effected L2Character.<BR><BR>
  394. */
  395. public void onExit()
  396. {
  397. if (_abnormalEffect != AbnormalEffect.NULL)
  398. getEffected().stopAbnormalEffect(_abnormalEffect);
  399. if (_specialEffect != AbnormalEffect.NULL)
  400. getEffected().stopSpecialEffect(_specialEffect);
  401. if (_eventEffect != AbnormalEffect.NULL && getEffected().getInstanceType() == InstanceType.L2PcInstance)
  402. getEffected().getActingPlayer().stopEventEffect(_eventEffect);
  403. }
  404. /** Return true for continuation of this effect */
  405. public abstract boolean onActionTime();
  406. public final void rescheduleEffect()
  407. {
  408. if (_state != EffectState.ACTING)
  409. {
  410. scheduleEffect();
  411. }
  412. else
  413. {
  414. if (_count > 1)
  415. {
  416. startEffectTaskAtFixedRate(5, _period * 1000);
  417. return;
  418. }
  419. if (_period > 0)
  420. {
  421. startEffectTask(_period * 1000);
  422. return;
  423. }
  424. }
  425. }
  426. public final void scheduleEffect()
  427. {
  428. if (_state == EffectState.CREATED)
  429. {
  430. _state = EffectState.ACTING;
  431. if (_skill.isPvpSkill() && _icon && getEffected() instanceof L2PcInstance)
  432. {
  433. SystemMessage smsg = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  434. smsg.addSkillName(_skill);
  435. getEffected().sendPacket(smsg);
  436. }
  437. if (_count > 1)
  438. {
  439. startEffectTaskAtFixedRate(5, _period * 1000);
  440. return;
  441. }
  442. if (_period > 0 || _period == -1)
  443. {
  444. startEffectTask(_period * 1000);
  445. return;
  446. }
  447. // effects not having count or period should start
  448. _startConditionsCorrect = onStart();
  449. }
  450. if (_state == EffectState.ACTING)
  451. {
  452. if (_count-- > 0)
  453. {
  454. if (getInUse())
  455. { // effect has to be in use
  456. if (onActionTime() && _startConditionsCorrect)
  457. return; // false causes effect to finish right away
  458. }
  459. else if (_count > 0)
  460. { // do not finish it yet, in case reactivated
  461. return;
  462. }
  463. }
  464. _state = EffectState.FINISHING;
  465. }
  466. if (_state == EffectState.FINISHING)
  467. {
  468. // Cancel the effect in the the abnormal effect map of the L2Character
  469. if (getInUse() || !(_count > 1 || _period > 0))
  470. if (_startConditionsCorrect)
  471. onExit();
  472. //If the time left is equal to zero, send the message
  473. if (_count == 0 && _icon && getEffected() instanceof L2PcInstance)
  474. {
  475. SystemMessage smsg3 = new SystemMessage(SystemMessageId.S1_HAS_WORN_OFF);
  476. smsg3.addSkillName(_skill);
  477. getEffected().sendPacket(smsg3);
  478. }
  479. // if task is null - stopEffectTask does not remove effect
  480. if (_currentFuture == null && getEffected() != null)
  481. {
  482. getEffected().removeEffect(this);
  483. }
  484. // Stop the task of the L2Effect, remove it and update client magic icon
  485. stopEffectTask();
  486. if (_skill.getAfterEffectId() > 0)
  487. {
  488. L2Skill skill = SkillTable.getInstance().getInfo(_skill.getAfterEffectId(), _skill.getAfterEffectLvl());
  489. if (skill != null)
  490. {
  491. getEffected().broadcastPacket(new MagicSkillUse(_effected, skill.getId(), skill.getLevel(), 0, 0));
  492. getEffected().broadcastPacket(new MagicSkillLaunched(_effected, skill.getId(), skill.getLevel()));
  493. skill.getEffects(getEffected(), getEffected());
  494. }
  495. }
  496. }
  497. }
  498. public Func[] getStatFuncs()
  499. {
  500. if (_funcTemplates == null)
  501. return _emptyFunctionSet;
  502. ArrayList<Func> funcs = new ArrayList<Func>(_funcTemplates.length);
  503. Env env = new Env();
  504. env.player = getEffector();
  505. env.target = getEffected();
  506. env.skill = getSkill();
  507. Func f;
  508. for (FuncTemplate t : _funcTemplates)
  509. {
  510. f = t.getFunc(env, this); // effect is owner
  511. if (f != null)
  512. funcs.add(f);
  513. }
  514. if (funcs.isEmpty())
  515. return _emptyFunctionSet;
  516. return funcs.toArray(new Func[funcs.size()]);
  517. }
  518. public final void addIcon(AbnormalStatusUpdate mi)
  519. {
  520. EffectTask task = _currentTask;
  521. ScheduledFuture<?> future = _currentFuture;
  522. if (_state == EffectState.FINISHING || _state == EffectState.CREATED)
  523. return;
  524. L2Skill sk = getSkill();
  525. if (task != null && task._rate > 0)
  526. {
  527. if (sk.isPotion())
  528. mi.addEffect(sk.getId(), getLevel(), sk.getBuffDuration() - (getTaskTime() * 1000));
  529. else
  530. mi.addEffect(sk.getId(), getLevel(), -1);
  531. }
  532. else if (future != null)
  533. mi.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  534. else if (_period == -1)
  535. mi.addEffect(sk.getId(), getLevel(), _period);
  536. }
  537. public final void addPartySpelledIcon(PartySpelled ps)
  538. {
  539. ScheduledFuture<?> future = _currentFuture;
  540. if (_state == EffectState.FINISHING || _state == EffectState.CREATED)
  541. return;
  542. L2Skill sk = getSkill();
  543. if (future != null)
  544. ps.addPartySpelledEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  545. else if (_period == -1)
  546. ps.addPartySpelledEffect(sk.getId(), getLevel(), _period);
  547. }
  548. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  549. {
  550. ScheduledFuture<?> future = _currentFuture;
  551. if (_state == EffectState.FINISHING || _state == EffectState.CREATED)
  552. return;
  553. L2Skill sk = getSkill();
  554. if (future != null)
  555. os.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  556. else if (_period == -1)
  557. os.addEffect(sk.getId(), getLevel(), _period);
  558. }
  559. public int getLevel()
  560. {
  561. return getSkill().getLevel();
  562. }
  563. public int getPeriodfirsttime()
  564. {
  565. return _periodfirsttime;
  566. }
  567. public void setPeriodfirsttime(int periodfirsttime)
  568. {
  569. _periodfirsttime = periodfirsttime;
  570. }
  571. public int getPeriodStartTicks()
  572. {
  573. return _periodStartTicks;
  574. }
  575. public void setPeriodStartTicks(int periodStartTicks)
  576. {
  577. _periodStartTicks = periodStartTicks;
  578. }
  579. public EffectTemplate getEffectTemplate()
  580. {
  581. return _template;
  582. }
  583. public double getEffectPower()
  584. {
  585. return _effectPower;
  586. }
  587. public L2SkillType getSkillType()
  588. {
  589. return _effectSkillType;
  590. }
  591. }