WalkInfo.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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;
  20. import java.util.concurrent.ScheduledFuture;
  21. import com.l2jserver.gameserver.instancemanager.WalkingManager;
  22. import com.l2jserver.gameserver.model.actor.L2Npc;
  23. import com.l2jserver.gameserver.model.quest.Quest;
  24. import com.l2jserver.util.Rnd;
  25. /**
  26. * Holds info about current walk progress
  27. * @author GKR, UnAfraid
  28. */
  29. public class WalkInfo
  30. {
  31. private final String _routeName;
  32. private ScheduledFuture<?> _walkCheckTask;
  33. private boolean _blocked = false;
  34. private boolean _suspended = false;
  35. private boolean _stoppedByAttack = false;
  36. private int _currentNode = 0;
  37. private boolean _forward = true; // Determines first --> last or first <-- last direction
  38. private long _lastActionTime; // Debug field
  39. public WalkInfo(String routeName)
  40. {
  41. _routeName = routeName;
  42. }
  43. /**
  44. * @return name of route of this WalkInfo.
  45. */
  46. public L2WalkRoute getRoute()
  47. {
  48. return WalkingManager.getInstance().getRoute(_routeName);
  49. }
  50. /**
  51. * @return current node of this WalkInfo.
  52. */
  53. public L2NpcWalkerNode getCurrentNode()
  54. {
  55. return getRoute().getNodeList().get(_currentNode);
  56. }
  57. /**
  58. * Calculate next node for this WalkInfo and send debug message from given npc
  59. * @param npc NPC to debug message to be sent from
  60. */
  61. public void calculateNextNode(L2Npc npc)
  62. {
  63. // Check this first, within the bounds of random moving, we have no conception of "first" or "last" node
  64. if (getRoute().getRepeatType() == WalkingManager.REPEAT_RANDOM)
  65. {
  66. int newNode = _currentNode;
  67. while (newNode == _currentNode)
  68. {
  69. newNode = Rnd.get(getRoute().getNodesCount());
  70. }
  71. _currentNode = newNode;
  72. npc.sendDebugMessage("Route: " + getRoute().getName() + ", next random node is " + _currentNode);
  73. }
  74. else
  75. {
  76. if (_forward)
  77. {
  78. _currentNode++;
  79. }
  80. else
  81. {
  82. _currentNode--;
  83. }
  84. if (_currentNode == getRoute().getNodesCount()) // Last node arrived
  85. {
  86. // Notify quest
  87. if (npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_ROUTE_FINISHED) != null)
  88. {
  89. for (Quest quest : npc.getTemplate().getEventQuests(Quest.QuestEventType.ON_ROUTE_FINISHED))
  90. {
  91. quest.notifyRouteFinished(npc);
  92. }
  93. }
  94. npc.sendDebugMessage("Route: " + getRoute().getName() + ", last node arrived");
  95. if (!getRoute().repeatWalk())
  96. {
  97. WalkingManager.getInstance().cancelMoving(npc);
  98. return;
  99. }
  100. switch (getRoute().getRepeatType())
  101. {
  102. case WalkingManager.REPEAT_GO_BACK:
  103. {
  104. _forward = false;
  105. _currentNode -= 2;
  106. break;
  107. }
  108. case WalkingManager.REPEAT_GO_FIRST:
  109. {
  110. _currentNode = 0;
  111. break;
  112. }
  113. case WalkingManager.REPEAT_TELE_FIRST:
  114. {
  115. npc.teleToLocation(npc.getSpawn().getLocation(), 0);
  116. _currentNode = 0;
  117. break;
  118. }
  119. }
  120. }
  121. else if (_currentNode == -1) // First node arrived, when direction is first <-- last
  122. {
  123. _currentNode = 1;
  124. _forward = true;
  125. }
  126. }
  127. }
  128. /**
  129. * @return {@code true} if walking task is blocked, {@code false} otherwise,
  130. */
  131. public boolean isBlocked()
  132. {
  133. return _blocked;
  134. }
  135. /**
  136. * @param val
  137. */
  138. public void setBlocked(boolean val)
  139. {
  140. _blocked = val;
  141. }
  142. /**
  143. * @return {@code true} if walking task is suspended, {@code false} otherwise,
  144. */
  145. public boolean isSuspended()
  146. {
  147. return _suspended;
  148. }
  149. /**
  150. * @param val
  151. */
  152. public void setSuspended(boolean val)
  153. {
  154. _suspended = val;
  155. }
  156. /**
  157. * @return {@code true} if walking task shall be stopped by attack, {@code false} otherwise,
  158. */
  159. public boolean isStoppedByAttack()
  160. {
  161. return _stoppedByAttack;
  162. }
  163. /**
  164. * @param val
  165. */
  166. public void setStoppedByAttack(boolean val)
  167. {
  168. _stoppedByAttack = val;
  169. }
  170. /**
  171. * @return the id of the current node in this walking task.
  172. */
  173. public int getCurrentNodeId()
  174. {
  175. return _currentNode;
  176. }
  177. /**
  178. * @return {@code long} last action time used only for debugging.
  179. */
  180. public long getLastAction()
  181. {
  182. return _lastActionTime;
  183. }
  184. /**
  185. * @param val
  186. */
  187. public void setLastAction(long val)
  188. {
  189. _lastActionTime = val;
  190. }
  191. /**
  192. * @return walking check task.
  193. */
  194. public ScheduledFuture<?> getWalkCheckTask()
  195. {
  196. return _walkCheckTask;
  197. }
  198. /**
  199. * @param val walking check task.
  200. */
  201. public void setWalkCheckTask(ScheduledFuture<?> val)
  202. {
  203. _walkCheckTask = val;
  204. }
  205. }