L2Effect.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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.network.SystemMessageId;
  25. import net.sf.l2j.gameserver.serverpackets.AbnormalStatusUpdate;
  26. import net.sf.l2j.gameserver.serverpackets.ExOlympiadSpelledInfo;
  27. import net.sf.l2j.gameserver.serverpackets.PartySpelled;
  28. import net.sf.l2j.gameserver.serverpackets.SystemMessage;
  29. import net.sf.l2j.gameserver.skills.Env;
  30. import net.sf.l2j.gameserver.skills.effects.EffectTemplate;
  31. import net.sf.l2j.gameserver.skills.funcs.Func;
  32. import net.sf.l2j.gameserver.skills.funcs.FuncTemplate;
  33. import net.sf.l2j.gameserver.skills.funcs.Lambda;
  34. /**
  35. * This class ...
  36. *
  37. * @version $Revision: 1.1.2.1.2.12 $ $Date: 2005/04/11 10:06:07 $
  38. */
  39. public abstract class L2Effect
  40. {
  41. static final Logger _log = Logger.getLogger(L2Effect.class.getName());
  42. public static enum EffectState {
  43. CREATED, ACTING, FINISHING
  44. }
  45. public static enum EffectType
  46. {
  47. BUFF,
  48. DEBUFF,
  49. CHARGE,
  50. DMG_OVER_TIME,
  51. HEAL_OVER_TIME,
  52. COMBAT_POINT_HEAL_OVER_TIME,
  53. MANA_DMG_OVER_TIME,
  54. MANA_HEAL_OVER_TIME,
  55. MP_CONSUME_PER_LEVEL,
  56. RELAXING, STUN, ROOT,
  57. SLEEP,
  58. HATE,
  59. FAKE_DEATH,
  60. CONFUSION,
  61. CONFUSE_MOB_ONLY,
  62. MUTE,
  63. FEAR,
  64. SILENT_MOVE,
  65. SEED,
  66. PARALYZE,
  67. STUN_SELF,
  68. PHYSICAL_MUTE,
  69. PHYSICAL_ATTACK_MUTE,
  70. REMOVE_TARGET,
  71. TARGET_ME,
  72. SILENCE_MAGIC_PHYSICAL,
  73. BETRAY,
  74. NOBLESSE_BLESSING,
  75. PHOENIX_BLESSING,
  76. PETRIFICATION,
  77. BLUFF,
  78. CHARM_OF_LUCK,
  79. INVINCIBLE,
  80. TRANSFORMATION,
  81. IMMOBILEUNTILATTACKED,
  82. DISARM,
  83. CHARMOFCOURAGE,
  84. SIGNET_EFFECT,
  85. SIGNET_GROUND,
  86. WARP,
  87. SPOIL
  88. }
  89. private static final Func[] _emptyFunctionSet = new Func[0];
  90. //member _effector is the instance of L2Character that cast/used the spell/skill that is
  91. //causing this effect. Do not confuse with the instance of L2Character that
  92. //is being affected by this effect.
  93. private final L2Character _effector;
  94. //member _effected is the instance of L2Character that was affected
  95. //by this effect. Do not confuse with the instance of L2Character that
  96. //casted/used this effect.
  97. private final L2Character _effected;
  98. //the skill that was used.
  99. private final L2Skill _skill;
  100. //or the items that was used.
  101. //private final L2Item _item;
  102. // the value of an update
  103. private final Lambda _lambda;
  104. // the current state
  105. private EffectState _state;
  106. // period, seconds
  107. private final int _period;
  108. private int _periodStartTicks;
  109. private int _periodfirsttime;
  110. // function templates
  111. private final FuncTemplate[] _funcTemplates;
  112. //initial count
  113. private int _totalCount;
  114. // counter
  115. private int _count;
  116. // abnormal effect mask
  117. private int _abnormalEffect;
  118. // show icon
  119. private boolean _icon;
  120. public boolean preventExitUpdate;
  121. public final class EffectTask implements Runnable
  122. {
  123. protected final int _delay;
  124. protected final int _rate;
  125. EffectTask(int pDelay, int pRate)
  126. {
  127. _delay = pDelay;
  128. _rate = pRate;
  129. }
  130. public void run()
  131. {
  132. try
  133. {
  134. if (getPeriodfirsttime() == 0) setPeriodStartTicks(GameTimeController.getGameTicks());
  135. else setPeriodfirsttime(0);
  136. L2Effect.this.scheduleEffect();
  137. }
  138. catch (Throwable e)
  139. {
  140. _log.log(Level.SEVERE, "", e);
  141. }
  142. }
  143. }
  144. private ScheduledFuture<?> _currentFuture;
  145. private EffectTask _currentTask;
  146. /** The Identifier of the stack group */
  147. private final String _stackType;
  148. /** The position of the effect in the stack group */
  149. private final float _stackOrder;
  150. private boolean _inUse = false;
  151. protected L2Effect(Env env, EffectTemplate template)
  152. {
  153. _state = EffectState.CREATED;
  154. _skill = env.skill;
  155. //_item = env._item == null ? null : env._item.getItem();
  156. _effected = env.target;
  157. _effector = env.player;
  158. _lambda = template.lambda;
  159. _funcTemplates = template.funcTemplates;
  160. _count = template.counter;
  161. _totalCount = _count;
  162. _period = template.period;
  163. _abnormalEffect = template.abnormalEffect;
  164. _stackType = template.stackType;
  165. _stackOrder = template.stackOrder;
  166. _periodStartTicks = GameTimeController.getGameTicks();
  167. _periodfirsttime = 0;
  168. _icon = template.icon;
  169. scheduleEffect();
  170. }
  171. public int getCount()
  172. {
  173. return _count;
  174. }
  175. public int getTotalCount()
  176. {
  177. return _totalCount;
  178. }
  179. public void setCount(int newcount)
  180. {
  181. _count = newcount;
  182. }
  183. public void setFirstTime(int newfirsttime)
  184. {
  185. if (_currentFuture != null)
  186. {
  187. _periodStartTicks = GameTimeController.getGameTicks() - newfirsttime
  188. * GameTimeController.TICKS_PER_SECOND;
  189. _currentFuture.cancel(false);
  190. _currentFuture = null;
  191. _currentTask = null;
  192. _periodfirsttime = newfirsttime;
  193. int duration = _period - _periodfirsttime;
  194. //_log.warning("Period: "+_period+"-"+_periodfirsttime+"="+duration);
  195. _currentTask = new EffectTask(duration * 1000, -1);
  196. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(_currentTask,
  197. duration * 1000);
  198. }
  199. }
  200. public boolean getShowIcon()
  201. {
  202. return _icon;
  203. }
  204. public int getPeriod()
  205. {
  206. return _period;
  207. }
  208. public int getTime()
  209. {
  210. return (GameTimeController.getGameTicks() - _periodStartTicks)
  211. / GameTimeController.TICKS_PER_SECOND;
  212. }
  213. /**
  214. * Returns the elapsed time of the task.
  215. * @return Time in seconds.
  216. */
  217. public int getTaskTime()
  218. {
  219. if (_count == _totalCount) return 0;
  220. return (Math.abs(_count-_totalCount+1)*_period) + getTime()+1;
  221. }
  222. public boolean getInUse()
  223. {
  224. return _inUse;
  225. }
  226. public void setInUse(boolean inUse)
  227. {
  228. _inUse = inUse;
  229. if (_inUse) onStart();
  230. else onExit();
  231. }
  232. public String getStackType()
  233. {
  234. return _stackType;
  235. }
  236. public float getStackOrder()
  237. {
  238. return _stackOrder;
  239. }
  240. public final L2Skill getSkill()
  241. {
  242. return _skill;
  243. }
  244. public final L2Character getEffector()
  245. {
  246. return _effector;
  247. }
  248. public final L2Character getEffected()
  249. {
  250. return _effected;
  251. }
  252. public boolean isSelfEffect()
  253. {
  254. return _skill._effectTemplatesSelf != null;
  255. }
  256. public boolean isHerbEffect()
  257. {
  258. if (getSkill().getName().contains("Herb"))
  259. return true;
  260. return false;
  261. }
  262. public final double calc()
  263. {
  264. Env env = new Env();
  265. env.player = _effector;
  266. env.target = _effected;
  267. env.skill = _skill;
  268. return _lambda.calc(env);
  269. }
  270. private synchronized void startEffectTask(int duration)
  271. {
  272. stopEffectTask();
  273. _currentTask = new EffectTask(duration, -1);
  274. _currentFuture = ThreadPoolManager.getInstance().scheduleEffect(_currentTask, duration);
  275. if (_state == EffectState.ACTING) _effected.addEffect(this);
  276. }
  277. private synchronized void startEffectTaskAtFixedRate(int delay, int rate)
  278. {
  279. stopEffectTask();
  280. _currentTask = new EffectTask(delay, rate);
  281. _currentFuture = ThreadPoolManager.getInstance().scheduleEffectAtFixedRate(_currentTask, delay,
  282. rate);
  283. if (_state == EffectState.ACTING) _effected.addEffect(this);
  284. }
  285. /**
  286. * Stop the L2Effect task and send Server->Client update packet.<BR><BR>
  287. *
  288. * <B><U> Actions</U> :</B><BR><BR>
  289. * <li>Cancel the effect in the the abnormal effect map of the L2Character </li>
  290. * <li>Stop the task of the L2Effect, remove it and update client magic icon </li><BR><BR>
  291. *
  292. */
  293. public final void exit()
  294. {
  295. this.exit(false);
  296. }
  297. public final void exit(boolean preventUpdate)
  298. {
  299. preventExitUpdate = preventUpdate;
  300. _state = EffectState.FINISHING;
  301. scheduleEffect();
  302. }
  303. /**
  304. * Stop the task of the L2Effect, remove it and update client magic icon.<BR><BR>
  305. *
  306. * <B><U> Actions</U> :</B><BR><BR>
  307. * <li>Cancel the task </li>
  308. * <li>Stop and remove L2Effect from L2Character and update client magic icon </li><BR><BR>
  309. *
  310. */
  311. public synchronized void stopEffectTask()
  312. {
  313. if (_currentFuture != null)
  314. {
  315. // Cancel the task
  316. _currentFuture.cancel(false);
  317. _currentFuture = null;
  318. _currentTask = null;
  319. _effected.removeEffect(this);
  320. }
  321. }
  322. /** returns effect type */
  323. public abstract EffectType getEffectType();
  324. /** Notify started */
  325. public void onStart()
  326. {
  327. if (_abnormalEffect != 0) getEffected().startAbnormalEffect(_abnormalEffect);
  328. }
  329. /**
  330. * Cancel the effect in the the abnormal effect map of the effected L2Character.<BR><BR>
  331. */
  332. public void onExit()
  333. {
  334. if (_abnormalEffect != 0) getEffected().stopAbnormalEffect(_abnormalEffect);
  335. }
  336. /** Return true for continuation of this effect */
  337. public abstract boolean onActionTime();
  338. public final void rescheduleEffect()
  339. {
  340. if (_state != EffectState.ACTING)
  341. {
  342. scheduleEffect();
  343. }
  344. else
  345. {
  346. if (_count > 1)
  347. {
  348. startEffectTaskAtFixedRate(5, _period * 1000);
  349. return;
  350. }
  351. if (_period > 0)
  352. {
  353. startEffectTask(_period * 1000);
  354. return;
  355. }
  356. }
  357. }
  358. public final void scheduleEffect()
  359. {
  360. if (_state == EffectState.CREATED)
  361. {
  362. _state = EffectState.ACTING;
  363. if (_skill.isPvpSkill())
  364. {
  365. SystemMessage smsg = new SystemMessage(SystemMessageId.YOU_FEEL_S1_EFFECT);
  366. smsg.addString(_skill.getName());
  367. getEffected().sendPacket(smsg);
  368. }
  369. if (_count > 1)
  370. {
  371. startEffectTaskAtFixedRate(5, _period * 1000);
  372. return;
  373. }
  374. if (_period > 0)
  375. {
  376. startEffectTask(_period * 1000);
  377. return;
  378. }
  379. // effects not having count or period should start
  380. onStart();
  381. }
  382. if (_state == EffectState.ACTING)
  383. {
  384. if (_count-- > 0)
  385. {
  386. if (getInUse()) { // effect has to be in use
  387. if (onActionTime()) return; // false causes effect to finish right away
  388. }
  389. else if (_count > 0) { // do not finish it yet, in case reactivated
  390. return;
  391. }
  392. }
  393. _state = EffectState.FINISHING;
  394. }
  395. if (_state == EffectState.FINISHING)
  396. {
  397. // Cancel the effect in the the abnormal effect map of the L2Character
  398. if (getInUse() || !(_count > 1 || _period > 0))
  399. onExit();
  400. //If the time left is equal to zero, send the message
  401. if (_count == 0)
  402. {
  403. SystemMessage smsg3 = new SystemMessage(SystemMessageId.S1_HAS_WORN_OFF);
  404. smsg3.addString(_skill.getName());
  405. getEffected().sendPacket(smsg3);
  406. }
  407. // Stop the task of the L2Effect, remove it and update client magic icon
  408. stopEffectTask();
  409. }
  410. }
  411. public Func[] getStatFuncs()
  412. {
  413. if (_funcTemplates == null) return _emptyFunctionSet;
  414. List<Func> funcs = new FastList<Func>();
  415. for (FuncTemplate t : _funcTemplates)
  416. {
  417. Env env = new Env();
  418. env.player = getEffector();
  419. env.target = getEffected();
  420. env.skill = getSkill();
  421. Func f = t.getFunc(env, this); // effect is owner
  422. if (f != null) funcs.add(f);
  423. }
  424. if (funcs.size() == 0) return _emptyFunctionSet;
  425. return funcs.toArray(new Func[funcs.size()]);
  426. }
  427. public final void addIcon(AbnormalStatusUpdate mi)
  428. {
  429. EffectTask task = _currentTask;
  430. ScheduledFuture<?> future = _currentFuture;
  431. if (task == null || future == null) return;
  432. if (_state == EffectState.FINISHING || _state == EffectState.CREATED) return;
  433. L2Skill sk = getSkill();
  434. if (task._rate > 0)
  435. {
  436. if (sk.isPotion()) mi.addEffect(sk.getId(), getLevel(), sk.getBuffDuration()-(getTaskTime()*1000));
  437. else mi.addEffect(sk.getId(), getLevel(), -1);
  438. }
  439. else mi.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  440. }
  441. public final void addPartySpelledIcon(PartySpelled ps)
  442. {
  443. EffectTask task = _currentTask;
  444. ScheduledFuture<?> future = _currentFuture;
  445. if (task == null || future == null) return;
  446. if (_state == EffectState.FINISHING || _state == EffectState.CREATED) return;
  447. L2Skill sk = getSkill();
  448. ps.addPartySpelledEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  449. }
  450. public final void addOlympiadSpelledIcon(ExOlympiadSpelledInfo os)
  451. {
  452. EffectTask task = _currentTask;
  453. ScheduledFuture<?> future = _currentFuture;
  454. if (task == null || future == null) return;
  455. if (_state == EffectState.FINISHING || _state == EffectState.CREATED) return;
  456. L2Skill sk = getSkill();
  457. os.addEffect(sk.getId(), getLevel(), (int) future.getDelay(TimeUnit.MILLISECONDS));
  458. }
  459. public int getLevel()
  460. {
  461. return getSkill().getLevel();
  462. }
  463. public int getPeriodfirsttime()
  464. {
  465. return _periodfirsttime;
  466. }
  467. public void setPeriodfirsttime(int periodfirsttime)
  468. {
  469. _periodfirsttime = periodfirsttime;
  470. }
  471. public int getPeriodStartTicks()
  472. {
  473. return _periodStartTicks;
  474. }
  475. public void setPeriodStartTicks(int periodStartTicks)
  476. {
  477. _periodStartTicks = periodStartTicks;
  478. }
  479. }