L2Effect.java 18 KB

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