L2Transformation.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.concurrent.ScheduledFuture;
  17. import net.sf.l2j.gameserver.ThreadPoolManager;
  18. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  19. /**
  20. *
  21. * @author KenM
  22. */
  23. public abstract class L2Transformation implements Cloneable, Runnable
  24. {
  25. private final int _id;
  26. private final int _graphicalId;
  27. private final double _collisionRadius;
  28. private final double _collisionHeight;
  29. private long _duration;
  30. public static final int TRANSFORM_ZARICHE = 301;
  31. public static final int TRANSFORM_AKAMANAH = 302;
  32. private L2PcInstance _player;
  33. private long _startTime;
  34. private ScheduledFuture<?> _future;
  35. protected boolean _canDoMeleeAttack = true;
  36. protected boolean _startFollowToCast = true;
  37. /**
  38. *
  39. * @param id Internal id that server will use to associate this transformation
  40. * @param graphicalId Client visible transformation id
  41. * @param duration Transformation duration in seconds
  42. * @param collisionRadius Collision Radius of the player while transformed
  43. * @param collisionHeight Collision Height of the player while transformed
  44. */
  45. public L2Transformation(int id, int graphicalId, int duration, double collisionRadius, double collisionHeight)
  46. {
  47. _id = id;
  48. _graphicalId = graphicalId;
  49. _collisionRadius = collisionRadius;
  50. _collisionHeight = collisionHeight;
  51. this.setDuration(duration * 1000);
  52. }
  53. /**
  54. *
  55. * @param id Internal id(will be used also as client graphical id) that server will use to associate this transformation
  56. * @param duration Transformation duration in seconds
  57. * @param collisionRadius Collision Radius of the player while transformed
  58. * @param collisionHeight Collision Height of the player while transformed
  59. */
  60. public L2Transformation(int id, int duration, double collisionRadius, double collisionHeight)
  61. {
  62. this(id, id, duration, collisionRadius, collisionHeight);
  63. }
  64. /**
  65. * @return Returns the id.
  66. */
  67. public int getId()
  68. {
  69. return _id;
  70. }
  71. /**
  72. * @return Returns the graphicalId.
  73. */
  74. public int getGraphicalId()
  75. {
  76. return _graphicalId;
  77. }
  78. /**
  79. * @return Returns the collisionRadius.
  80. */
  81. public double getCollisionRadius()
  82. {
  83. if (getId()>=312 && getId()<=318)
  84. return _player.getBaseTemplate().collisionRadius;
  85. return _collisionRadius;
  86. }
  87. /**
  88. * @return Returns the collisionHeight.
  89. */
  90. public double getCollisionHeight()
  91. {
  92. if (getId()>=312 && getId()<=318)
  93. return _player.getBaseTemplate().collisionHeight;
  94. return _collisionHeight;
  95. }
  96. /**
  97. * @param duration The duration to set.
  98. */
  99. public void setDuration(long duration)
  100. {
  101. _duration = duration;
  102. }
  103. /**
  104. * @return Returns the total duration in miliseconds.
  105. */
  106. public long getDuration()
  107. {
  108. return _duration;
  109. }
  110. /**
  111. * @return The remaining transformed time in miliseconds. An zero or negative value if the transformation has already ended.
  112. */
  113. public long getRemainingTime()
  114. {
  115. return (getStartTime() + this.getDuration()) - System.currentTimeMillis();
  116. }
  117. // Scriptable Events
  118. public abstract void onTransform();
  119. public abstract void onUntransform();
  120. /**
  121. * @param player The player to set.
  122. */
  123. private void setPlayer(L2PcInstance player)
  124. {
  125. _player = player;
  126. }
  127. /**
  128. * @return Returns the player.
  129. */
  130. public L2PcInstance getPlayer()
  131. {
  132. return _player;
  133. }
  134. /**
  135. * @param startTime The startTime to set.
  136. */
  137. public void setStartTime(long startTime)
  138. {
  139. _startTime = startTime;
  140. }
  141. /**
  142. * @return Returns the startTime.
  143. */
  144. private long getStartTime()
  145. {
  146. return _startTime;
  147. }
  148. /**
  149. * @param future The future to set.
  150. */
  151. public void setFuture(ScheduledFuture<?> future)
  152. {
  153. _future = future;
  154. }
  155. /**
  156. * @return Returns the future.
  157. */
  158. public ScheduledFuture<?> getFuture()
  159. {
  160. return _future;
  161. }
  162. public void start()
  163. {
  164. this.setStartTime(System.currentTimeMillis());
  165. this.resume();
  166. }
  167. public void resume()
  168. {
  169. this.setFuture(ThreadPoolManager.getInstance().scheduleGeneral(this, this.getRemainingTime()));
  170. this.getPlayer().transform(this);
  171. }
  172. public void run()
  173. {
  174. this.stop();
  175. }
  176. public void stop()
  177. {
  178. if (this.getRemainingTime() > 0)
  179. {
  180. this.getPlayer().untransform();
  181. }
  182. }
  183. public L2Transformation createTransformationForPlayer(L2PcInstance player)
  184. {
  185. try
  186. {
  187. L2Transformation transformation = (L2Transformation) this.clone();
  188. transformation.setPlayer(player);
  189. return transformation;
  190. }
  191. catch (CloneNotSupportedException e)
  192. {
  193. // should never happen
  194. return null;
  195. }
  196. }
  197. // Override if necessary
  198. public void onLevelUp()
  199. {
  200. }
  201. /**
  202. * Returns true if transformation can do melee attack
  203. */
  204. public boolean canDoMeleeAttack()
  205. {
  206. return _canDoMeleeAttack;
  207. }
  208. /**
  209. * Returns true if transformation can start follow target when trying to cast an skill out of range
  210. */
  211. public boolean canStartFollowToCast()
  212. {
  213. return _startFollowToCast;
  214. }
  215. }