BuffInfo.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (C) 2004-2013 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.model.skills;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Map.Entry;
  24. import java.util.concurrent.ConcurrentHashMap;
  25. import java.util.concurrent.ScheduledFuture;
  26. import com.l2jserver.gameserver.GameTimeController;
  27. import com.l2jserver.gameserver.ThreadPoolManager;
  28. import com.l2jserver.gameserver.model.CharEffectList;
  29. import com.l2jserver.gameserver.model.actor.L2Character;
  30. import com.l2jserver.gameserver.model.effects.AbstractEffect;
  31. import com.l2jserver.gameserver.model.effects.EffectTaskInfo;
  32. import com.l2jserver.gameserver.model.effects.EffectTickTask;
  33. import com.l2jserver.gameserver.model.stats.Env;
  34. import com.l2jserver.gameserver.model.stats.Formulas;
  35. import com.l2jserver.gameserver.network.SystemMessageId;
  36. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  37. /**
  38. * Buff Info.<br>
  39. * Complex DTO that holds all the information for a given buff (or debuff or dance/song) set of effects issued by an skill.
  40. * @author Zoey76
  41. */
  42. public final class BuffInfo
  43. {
  44. // Data
  45. /** Data. */
  46. private final Env _env;
  47. /** The effects. */
  48. private final List<AbstractEffect> _effects = new ArrayList<>(1);
  49. // Tasks
  50. /** Effect tasks for ticks. */
  51. private Map<AbstractEffect, EffectTaskInfo> _tasks;
  52. /** Task for effect ending. */
  53. private BuffTimeTask _effectTimeTask;
  54. /** Scheduled future. */
  55. private ScheduledFuture<?> _scheduledFutureTimeTask;
  56. // Time and ticks
  57. /** Abnormal time. */
  58. private int _abnormalTime;
  59. /** The game ticks at the start of this effect. */
  60. private final int _periodStartTicks;
  61. // Misc
  62. /** If {@code true} then this effect has been cancelled. */
  63. private boolean _isRemoved = false;
  64. /** If {@code true} then this effect is in use (or has been stop because an Herb took place). */
  65. private boolean _isInUse = true;
  66. /**
  67. * Buff Info constructor.
  68. * @param env the cast data
  69. */
  70. public BuffInfo(Env env)
  71. {
  72. _env = env;
  73. _abnormalTime = Formulas.calcEffectAbnormalTime(env);
  74. _periodStartTicks = GameTimeController.getInstance().getGameTicks();
  75. }
  76. /**
  77. * Gets the effects on this buff info.
  78. * @return the effects
  79. */
  80. public List<AbstractEffect> getEffects()
  81. {
  82. return _effects;
  83. }
  84. /**
  85. * Adds an effect to this buff info.
  86. * @param effect the effect to add
  87. */
  88. public void addEffect(AbstractEffect effect)
  89. {
  90. _effects.add(effect);
  91. }
  92. /**
  93. * Adds an effect task to this buff info.<br>
  94. * Uses double-checked locking to initialize the map if it's necessary.
  95. * @param effect the effect that owns the task
  96. * @param effectTaskInfo the task info
  97. */
  98. private void addTask(AbstractEffect effect, EffectTaskInfo effectTaskInfo)
  99. {
  100. if (_tasks == null)
  101. {
  102. synchronized (this)
  103. {
  104. if (_tasks == null)
  105. {
  106. _tasks = new ConcurrentHashMap<>();
  107. }
  108. }
  109. }
  110. _tasks.put(effect, effectTaskInfo);
  111. }
  112. /**
  113. * Gets the task for the given effect.
  114. * @param effect the effect
  115. * @return the task
  116. */
  117. private EffectTaskInfo getEffectTask(AbstractEffect effect)
  118. {
  119. return (_tasks == null) ? null : _tasks.get(effect);
  120. }
  121. /**
  122. * Gets the skill that created this buff info.
  123. * @return the skill
  124. */
  125. public L2Skill getSkill()
  126. {
  127. return _env.getSkill();
  128. }
  129. public Env getEnv()
  130. {
  131. return _env;
  132. }
  133. /**
  134. * Gets the calculated abnormal time.
  135. * @return the abnormal time
  136. */
  137. public int getAbnormalTime()
  138. {
  139. return _abnormalTime;
  140. }
  141. /**
  142. * Sets the abnormal time.
  143. * @param abnormalTime the abnormal time to set
  144. */
  145. public void setAbnormalTime(int abnormalTime)
  146. {
  147. _abnormalTime = abnormalTime;
  148. }
  149. /**
  150. * Gets the period start ticks.
  151. * @return the period start
  152. */
  153. public int getPeriodStartTicks()
  154. {
  155. return _periodStartTicks;
  156. }
  157. /**
  158. * Get the remaining time in seconds for this buff info.
  159. * @return the elapsed time
  160. */
  161. public int getTime()
  162. {
  163. return _abnormalTime - ((GameTimeController.getInstance().getGameTicks() - _periodStartTicks) / GameTimeController.TICKS_PER_SECOND);
  164. }
  165. /**
  166. * Verify if this buff info has been cancelled.
  167. * @return {@code true} if this buff info has been cancelled, {@code false} otherwise
  168. */
  169. public boolean isRemoved()
  170. {
  171. return _isRemoved;
  172. }
  173. /**
  174. * Set the buff info to removed.
  175. * @param val the value to set
  176. */
  177. public void setRemoved(boolean val)
  178. {
  179. _isRemoved = val;
  180. }
  181. /**
  182. * Verify if this buff info is in use.
  183. * @return {@code true} if this buff info is in use, {@code false} otherwise
  184. */
  185. public boolean isInUse()
  186. {
  187. return _isInUse;
  188. }
  189. /**
  190. * Set the buff info to in use.
  191. * @param val the value to set
  192. */
  193. public void setInUse(boolean val)
  194. {
  195. _isInUse = val;
  196. }
  197. /**
  198. * Gets the character that launched the buff.
  199. * @return the effector
  200. */
  201. public L2Character getEffector()
  202. {
  203. return _env.getCharacter();
  204. }
  205. /**
  206. * Gets the target of the skill.
  207. * @return the effected
  208. */
  209. public L2Character getEffected()
  210. {
  211. return _env.getTarget();
  212. }
  213. /**
  214. * Stops all the effects for this buff info.<br>
  215. * Removes effects stats.<br>
  216. * <b>It will not remove the buff info from the effect list</b>.<br>
  217. * Instead call {@link CharEffectList#stopSkillEffects(boolean, L2Skill)}
  218. * @param removed if {@code true} the skill will be handled as removed
  219. */
  220. public void stopAllEffects(boolean removed)
  221. {
  222. setRemoved(removed);
  223. // Cancels the task that will end this buff info
  224. if ((_scheduledFutureTimeTask != null) && !_scheduledFutureTimeTask.isCancelled())
  225. {
  226. _scheduledFutureTimeTask.cancel(false);
  227. }
  228. // Remove stats
  229. removeStats();
  230. finishEffects();
  231. }
  232. public void initializeEffects()
  233. {
  234. // When effects are initialized, the successfully landed.
  235. if (_env.getTarget().isPlayer())
  236. {
  237. final SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  238. sm.addSkillName(_env.getSkill());
  239. _env.getTarget().sendPacket(sm);
  240. }
  241. // Creates a task that will stop all the effects.
  242. if (_abnormalTime > 0)
  243. {
  244. _effectTimeTask = new BuffTimeTask(this);
  245. _scheduledFutureTimeTask = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_effectTimeTask, 0, 1000L);
  246. }
  247. for (AbstractEffect effect : _effects)
  248. {
  249. if (effect.isInstant())
  250. {
  251. // TODO: Log, instant effects shouldn't be added.
  252. continue;
  253. }
  254. // Call on start.
  255. effect.onStart(this);
  256. // If it's a continuous effect, if has ticks schedule a task with period, otherwise schedule a simple task to end it.
  257. if (effect.getTicks() > 0)
  258. {
  259. // The task for the effect ticks
  260. final EffectTickTask effectTask = new EffectTickTask(this, effect);
  261. final ScheduledFuture<?> scheduledFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(effectTask, effect.getTicks() * 1000L, effect.getTicks() * 1000L);
  262. // Adds the task for ticking
  263. addTask(effect, new EffectTaskInfo(effectTask, scheduledFuture));
  264. }
  265. // Add stats.
  266. _env.getTarget().addStatFuncs(effect.getStatFuncs(_env));
  267. applyAbnormalVisualEffects(_env.getTarget(), effect);
  268. }
  269. }
  270. /**
  271. * Called on each tick.<br>
  272. * Verify if the effect should end and the effect task should be cancelled.
  273. * @param effect the effect that is ticking
  274. * @param tickCount the tick count
  275. */
  276. public void onTick(AbstractEffect effect, int tickCount)
  277. {
  278. boolean continueForever = false;
  279. // If the effect is in use, allow it to affect the effected.
  280. if (_isInUse)
  281. {
  282. // Callback for on action time event.
  283. continueForever = effect.onActionTime(this);
  284. }
  285. // Skills without abnormal time should tick indefinitely, unless they fail on action time.
  286. if (_env.getSkill().getAbnormalTime() > 0)
  287. {
  288. // Checks made in seconds, although tasks run in milliseconds, it'll increase margin for effects that run indefinitely.
  289. final long elapsedTime = effect.getTicks() * tickCount;
  290. if (elapsedTime >= _env.getSkill().getAbnormalTime())
  291. {
  292. final EffectTaskInfo task = getEffectTask(effect);
  293. if (task != null)
  294. {
  295. task.getScheduledFuture().cancel(true); // Allow to finish current run.
  296. _env.getTarget().getEffectList().remove(this); // Remove the buff from the effect list.
  297. }
  298. }
  299. }
  300. else if (!continueForever)
  301. {
  302. final EffectTaskInfo task = getEffectTask(effect);
  303. if (task != null)
  304. {
  305. task.getScheduledFuture().cancel(true); // Allow to finish current run.
  306. _env.getTarget().getEffectList().remove(this); // Remove the buff from the effect list.
  307. }
  308. }
  309. }
  310. public void finishEffects()
  311. {
  312. for (AbstractEffect effect : _effects)
  313. {
  314. removeAbnormalVisualEffects(_env.getTarget(), effect); // TODO: Implement correctly.
  315. // Instant effects shouldn't call onExit(..).
  316. if ((effect != null) && !effect.isInstant())
  317. {
  318. effect.onExit(this);
  319. }
  320. }
  321. // Cancels the ticking task.
  322. if (_tasks != null)
  323. {
  324. for (Entry<AbstractEffect, EffectTaskInfo> entry : _tasks.entrySet())
  325. {
  326. entry.getValue().getScheduledFuture().cancel(true);
  327. }
  328. }
  329. // Sends the proper system message.
  330. SystemMessageId smId = null;
  331. if (_env.getSkill().isToggle())
  332. {
  333. smId = SystemMessageId.S1_HAS_BEEN_ABORTED;
  334. }
  335. else if (isRemoved())
  336. {
  337. smId = SystemMessageId.EFFECT_S1_HAS_BEEN_REMOVED;
  338. }
  339. else if (!_env.getSkill().isPassive())
  340. {
  341. smId = SystemMessageId.S1_HAS_WORN_OFF;
  342. }
  343. if (smId != null)
  344. {
  345. final SystemMessage sm = SystemMessage.getSystemMessage(smId);
  346. sm.addSkillName(_env.getSkill());
  347. _env.getTarget().sendPacket(sm);
  348. }
  349. if (this == _env.getTarget().getEffectList().getShortBuff())
  350. {
  351. _env.getTarget().getEffectList().shortBuffStatusUpdate(null);
  352. }
  353. }
  354. /**
  355. * Applies all the abnormal visual effects to the effected.<br>
  356. * TODO: Shouldn't be read from effect template, but from skill template.
  357. * @param effected the target of the skill
  358. * @param effect the effect
  359. */
  360. private static void applyAbnormalVisualEffects(L2Character effected, AbstractEffect effect)
  361. {
  362. if (effect.getAbnormalEffect() != AbnormalVisualEffect.NULL)
  363. {
  364. effected.startAbnormalEffect(effect.getAbnormalEffect());
  365. }
  366. if (effect.getSpecialEffect() != null)
  367. {
  368. effected.startSpecialEffect(effect.getSpecialEffect());
  369. }
  370. if ((effect.getEventEffect() != AbnormalVisualEffect.NULL) && effected.isPlayer())
  371. {
  372. effected.getActingPlayer().startEventEffect(effect.getEventEffect());
  373. }
  374. }
  375. /**
  376. * Removes all the abnormal visual effects from the effected.<br>
  377. * TODO: Shouldn't be read from effect template, but from skill template.
  378. * @param effected the target of the skill
  379. * @param effect the effect
  380. */
  381. private static void removeAbnormalVisualEffects(L2Character effected, AbstractEffect effect)
  382. {
  383. if (effect.getAbnormalEffect() != AbnormalVisualEffect.NULL)
  384. {
  385. effected.stopAbnormalEffect(effect.getAbnormalEffect());
  386. }
  387. if (effect.getSpecialEffect() != null)
  388. {
  389. effected.stopSpecialEffect(effect.getSpecialEffect());
  390. }
  391. if ((effect.getEventEffect() != AbnormalVisualEffect.NULL) && effected.isPlayer())
  392. {
  393. effected.getActingPlayer().stopEventEffect(effect.getEventEffect());
  394. }
  395. }
  396. public void addStats()
  397. {
  398. for (AbstractEffect effect : _effects)
  399. {
  400. _env.getTarget().addStatFuncs(effect.getStatFuncs(_env));
  401. }
  402. }
  403. public void removeStats()
  404. {
  405. for (AbstractEffect effect : _effects)
  406. {
  407. _env.getTarget().removeStatsOwner(effect);
  408. }
  409. // TODO: This should be removed when all effects are properly managed.
  410. _env.getTarget().removeStatsOwner(_env.getSkill());
  411. }
  412. /**
  413. * Gets the effect tick count.
  414. * @param effect the effect
  415. * @return the current tick count
  416. */
  417. public int getTickCount(AbstractEffect effect)
  418. {
  419. if (_tasks != null)
  420. {
  421. final EffectTaskInfo effectTaskInfo = _tasks.get(effect);
  422. if (effectTaskInfo != null)
  423. {
  424. return effectTaskInfo.getEffectTask().getTickCount();
  425. }
  426. }
  427. return 0;
  428. }
  429. }