L2Effect.java 17 KB

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