L2Effect.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 net.sf.l2j.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 javolution.util.FastList;
  22. import net.sf.l2j.gameserver.GameTimeController;
  23. import net.sf.l2j.gameserver.ThreadPoolManager;
  24. import net.sf.l2j.gameserver.datatables.SkillTable;
  25. import net.sf.l2j.gameserver.model.actor.L2Character;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  27. import net.sf.l2j.gameserver.model.actor.instance.L2SummonInstance;
  28. import net.sf.l2j.gameserver.network.SystemMessageId;
  29. import net.sf.l2j.gameserver.network.serverpackets.AbnormalStatusUpdate;
  30. import net.sf.l2j.gameserver.network.serverpackets.ExOlympiadSpelledInfo;
  31. import net.sf.l2j.gameserver.network.serverpackets.MagicSkillLaunched;
  32. import net.sf.l2j.gameserver.network.serverpackets.MagicSkillUse;
  33. import net.sf.l2j.gameserver.network.serverpackets.PartySpelled;
  34. import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;
  35. import net.sf.l2j.gameserver.skills.AbnormalEffect;
  36. import net.sf.l2j.gameserver.skills.Env;
  37. import net.sf.l2j.gameserver.skills.funcs.Func;
  38. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  39. import net.sf.l2j.gameserver.skills.funcs.Lambda;
  40. import net.sf.l2j.gameserver.templates.effects.EffectTemplate;
  41. import net.sf.l2j.gameserver.templates.skills.L2EffectType;
  42. import net.sf.l2j.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. // 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. _periodStartTicks = GameTimeController.getGameTicks() - newfirsttime * GameTimeController.TICKS_PER_SECOND;
  227. _currentFuture.cancel(false);
  228. _currentFuture = null;
  229. _currentTask = null;
  230. _periodfirsttime = newfirsttime;
  231. int duration = _period - _periodfirsttime;
  232. //_log.warning("Period: "+_period+"-"+_periodfirsttime+"="+duration);
  233. _currentTask = new EffectTask(duration * 1000, -1);
  234. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(_currentTask, duration * 1000);
  235. }
  236. }
  237. public boolean getShowIcon()
  238. {
  239. return _icon;
  240. }
  241. public int getPeriod()
  242. {
  243. return _period;
  244. }
  245. public int getTime()
  246. {
  247. return (GameTimeController.getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND;
  248. }
  249. /**
  250. * Returns the elapsed time of the task.
  251. * @return Time in seconds.
  252. */
  253. public int getTaskTime()
  254. {
  255. if (_count == _totalCount)
  256. return 0;
  257. return (Math.abs(_count - _totalCount + 1) * _period) + getTime() + 1;
  258. }
  259. public boolean getInUse()
  260. {
  261. return _inUse;
  262. }
  263. public boolean setInUse(boolean inUse)
  264. {
  265. _inUse = inUse;
  266. if (_inUse)
  267. _startConditionsCorrect = onStart();
  268. else
  269. onExit();
  270. return _startConditionsCorrect;
  271. }
  272. public String getStackType()
  273. {
  274. return _stackType;
  275. }
  276. public float getStackOrder()
  277. {
  278. return _stackOrder;
  279. }
  280. public final L2Skill getSkill()
  281. {
  282. return _skill;
  283. }
  284. public final L2Character getEffector()
  285. {
  286. return _effector;
  287. }
  288. public final L2Character getEffected()
  289. {
  290. return _effected;
  291. }
  292. public boolean isSelfEffect()
  293. {
  294. return _skill._effectTemplatesSelf != null;
  295. }
  296. public boolean isHerbEffect()
  297. {
  298. if (getSkill().getName().contains("Herb"))
  299. return true;
  300. return false;
  301. }
  302. public final double calc()
  303. {
  304. Env env = new Env();
  305. env.player = _effector;
  306. env.target = _effected;
  307. env.skill = _skill;
  308. return _lambda.calc(env);
  309. }
  310. private synchronized void startEffectTask(int duration)
  311. {
  312. if (duration >= 0)
  313. {
  314. stopEffectTask();
  315. _currentTask = new EffectTask(duration, -1);
  316. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(_currentTask, duration);
  317. }
  318. if (_state == EffectState.ACTING)
  319. {
  320. _effected.addEffect(this);
  321. }
  322. }
  323. private synchronized void startEffectTaskAtFixedRate(int delay, int rate)
  324. {
  325. stopEffectTask();
  326. _currentTask = new EffectTask(delay, rate);
  327. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_currentTask, delay, rate);
  328. if (_state == EffectState.ACTING)
  329. _effected.addEffect(this);
  330. }
  331. /**
  332. * Stop the L2Effect task and send Server->Client update packet.<BR><BR>
  333. *
  334. * <B><U> Actions</U> :</B><BR><BR>
  335. * <li>Cancel the effect in the the abnormal effect map of the L2Character </li>
  336. * <li>Stop the task of the L2Effect, remove it and update client magic icon </li><BR><BR>
  337. *
  338. */
  339. public final void exit()
  340. {
  341. this.exit(false);
  342. }
  343. public final void exit(boolean preventUpdate)
  344. {
  345. preventExitUpdate = preventUpdate;
  346. _state = EffectState.FINISHING;
  347. scheduleEffect();
  348. }
  349. /**
  350. * Stop the task of the L2Effect, remove it and update client magic icon.<BR><BR>
  351. *
  352. * <B><U> Actions</U> :</B><BR><BR>
  353. * <li>Cancel the task </li>
  354. * <li>Stop and remove L2Effect from L2Character and update client magic icon </li><BR><BR>
  355. *
  356. */
  357. public void stopEffectTask()
  358. {
  359. if (_currentFuture != null)
  360. {
  361. // Cancel the task
  362. _currentFuture.cancel(false);
  363. _currentFuture = null;
  364. _currentTask = null;
  365. if (getEffected() != null)
  366. getEffected().removeEffect(this);
  367. }
  368. }
  369. /** returns effect type */
  370. public abstract L2EffectType getEffectType();
  371. /** Notify started */
  372. public boolean onStart()
  373. {
  374. if (_abnormalEffect != AbnormalEffect.NULL)
  375. getEffected().startAbnormalEffect(_abnormalEffect);
  376. if (_specialEffect != AbnormalEffect.NULL)
  377. getEffected().startSpecialEffect(_specialEffect);
  378. return true;
  379. }
  380. /**
  381. * Cancel the effect in the the abnormal effect map of the effected L2Character.<BR><BR>
  382. */
  383. public void onExit()
  384. {
  385. if (_abnormalEffect != AbnormalEffect.NULL)
  386. getEffected().stopAbnormalEffect(_abnormalEffect);
  387. if (_specialEffect != AbnormalEffect.NULL)
  388. getEffected().stopSpecialEffect(_specialEffect);
  389. }
  390. /** Return true for continuation of this effect */
  391. public abstract boolean onActionTime();
  392. public final void rescheduleEffect()
  393. {
  394. if (_state != EffectState.ACTING)
  395. {
  396. scheduleEffect();
  397. }
  398. else
  399. {
  400. if (_count > 1)
  401. {
  402. startEffectTaskAtFixedRate(5, _period * 1000);
  403. return;
  404. }
  405. if (_period > 0)
  406. {
  407. startEffectTask(_period * 1000);
  408. return;
  409. }
  410. }
  411. }
  412. public final void scheduleEffect()
  413. {
  414. if (_state == EffectState.CREATED)
  415. {
  416. _state = EffectState.ACTING;
  417. if (_skill.isPvpSkill() && _icon && getEffected() instanceof L2PcInstance)
  418. {
  419. SystemMessage smsg = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  420. smsg.addSkillName(_skill);
  421. getEffected().sendPacket(smsg);
  422. }
  423. if (_count > 1)
  424. {
  425. startEffectTaskAtFixedRate(5, _period * 1000);
  426. return;
  427. }
  428. if (_period > 0 || _period == -1)
  429. {
  430. startEffectTask(_period * 1000);
  431. return;
  432. }
  433. // effects not having count or period should start
  434. _startConditionsCorrect = onStart();
  435. }
  436. if (_state == EffectState.ACTING)
  437. {
  438. if (_count-- > 0)
  439. {
  440. if (getInUse())
  441. { // effect has to be in use
  442. if (onActionTime() && _startConditionsCorrect)
  443. return; // false causes effect to finish right away
  444. }
  445. else if (_count > 0)
  446. { // do not finish it yet, in case reactivated
  447. return;
  448. }
  449. }
  450. _state = EffectState.FINISHING;
  451. }
  452. if (_state == EffectState.FINISHING)
  453. {
  454. // Cancel the effect in the the abnormal effect map of the L2Character
  455. if (getInUse() || !(_count > 1 || _period > 0))
  456. if (_startConditionsCorrect)
  457. onExit();
  458. //If the time left is equal to zero, send the message
  459. if (_count == 0 && _icon && getEffected() instanceof L2PcInstance)
  460. {
  461. SystemMessage smsg3 = new SystemMessage(SystemMessageId.S1_HAS_WORN_OFF);
  462. smsg3.addSkillName(_skill);
  463. getEffected().sendPacket(smsg3);
  464. }
  465. // if task is null - stopEffectTask does not remove effect
  466. if (_currentFuture == null && getEffected() != null)
  467. {
  468. getEffected().removeEffect(this);
  469. }
  470. // Stop the task of the L2Effect, remove it and update client magic icon
  471. stopEffectTask();
  472. if (_skill.getAfterEffectId() > 0)
  473. {
  474. L2Skill skill = SkillTable.getInstance().getInfo(_skill.getAfterEffectId(), _skill.getAfterEffectLvl());
  475. if (skill != null)
  476. {
  477. getEffected().broadcastPacket(new MagicSkillUse(_effected, skill.getId(), skill.getLevel(), 0, 0));
  478. getEffected().broadcastPacket(new MagicSkillLaunched(_effected, skill.getId(), skill.getLevel()));
  479. skill.getEffects(getEffected(), getEffected());
  480. }
  481. }
  482. }
  483. }
  484. public Func[] getStatFuncs()
  485. {
  486. if (_funcTemplates == null)
  487. return _emptyFunctionSet;
  488. List<Func> funcs = new FastList<Func>();
  489. for (FuncTemplate t : _funcTemplates)
  490. {
  491. Env env = new Env();
  492. env.player = getEffector();
  493. env.target = getEffected();
  494. env.skill = getSkill();
  495. Func f = t.getFunc(env, this); // effect is owner
  496. if (f != null)
  497. funcs.add(f);
  498. }
  499. if (funcs.isEmpty())
  500. return _emptyFunctionSet;
  501. return funcs.toArray(new Func[funcs.size()]);
  502. }
  503. public final void addIcon(AbnormalStatusUpdate mi)
  504. {
  505. EffectTask task = _currentTask;
  506. ScheduledFuture<?> future = _currentFuture;
  507. if (_state == EffectState.FINISHING || _state == EffectState.CREATED)
  508. return;
  509. L2Skill sk = getSkill();
  510. if (task != null && task._rate > 0)
  511. {
  512. if (sk.isPotion())
  513. mi.addEffect(sk.getId(), getLevel(), sk.getBuffDuration() - (getTaskTime() * 1000));
  514. else
  515. mi.addEffect(sk.getId(), getLevel(), -1);
  516. }
  517. else if (future != null)
  518. mi.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  519. else if (_period == -1)
  520. mi.addEffect(sk.getId(), getLevel(), _period);
  521. }
  522. public final void addPartySpelledIcon(PartySpelled ps)
  523. {
  524. ScheduledFuture<?> future = _currentFuture;
  525. if (_state == EffectState.FINISHING || _state == EffectState.CREATED)
  526. return;
  527. L2Skill sk = getSkill();
  528. if (future != null)
  529. ps.addPartySpelledEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  530. else if (_period == -1)
  531. ps.addPartySpelledEffect(sk.getId(), getLevel(), _period);
  532. }
  533. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  534. {
  535. ScheduledFuture<?> future = _currentFuture;
  536. if (_state == EffectState.FINISHING || _state == EffectState.CREATED)
  537. return;
  538. L2Skill sk = getSkill();
  539. if (future != null)
  540. os.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  541. else if (_period == -1)
  542. os.addEffect(sk.getId(), getLevel(), _period);
  543. }
  544. public int getLevel()
  545. {
  546. return getSkill().getLevel();
  547. }
  548. public int getPeriodfirsttime()
  549. {
  550. return _periodfirsttime;
  551. }
  552. public void setPeriodfirsttime(int periodfirsttime)
  553. {
  554. _periodfirsttime = periodfirsttime;
  555. }
  556. public int getPeriodStartTicks()
  557. {
  558. return _periodStartTicks;
  559. }
  560. public void setPeriodStartTicks(int periodStartTicks)
  561. {
  562. _periodStartTicks = periodStartTicks;
  563. }
  564. public EffectTemplate getEffectTemplate()
  565. {
  566. return _template;
  567. }
  568. public double getEffectPower()
  569. {
  570. return _effectPower;
  571. }
  572. public L2SkillType getSkillType()
  573. {
  574. return _effectSkillType;
  575. }
  576. }