WalkInfo.java 5.2 KB

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