L2Effect.java 18 KB

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