L2NpcWalkerAI.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.ai;
  20. import java.util.List;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.ThreadPoolManager;
  23. import com.l2jserver.gameserver.datatables.NpcWalkerRoutesData;
  24. import com.l2jserver.gameserver.model.L2CharPosition;
  25. import com.l2jserver.gameserver.model.L2NpcWalkerNode;
  26. import com.l2jserver.gameserver.model.actor.L2Character;
  27. import com.l2jserver.gameserver.model.actor.instance.L2NpcWalkerInstance;
  28. import com.l2jserver.gameserver.network.NpcStringId;
  29. public class L2NpcWalkerAI extends L2CharacterAI implements Runnable
  30. {
  31. private static final int DEFAULT_MOVE_DELAY = 0;
  32. private long _nextMoveTime;
  33. private boolean _walkingToNextPoint = false;
  34. /**
  35. * home points for xyz
  36. */
  37. int _homeX, _homeY, _homeZ;
  38. /**
  39. * route of the current npc
  40. */
  41. private List<L2NpcWalkerNode> _route;
  42. /**
  43. * current node
  44. */
  45. private int _currentPos;
  46. /**
  47. * Constructor of L2CharacterAI.
  48. * @param accessor The AI accessor of the L2Character
  49. */
  50. public L2NpcWalkerAI(L2Character.AIAccessor accessor)
  51. {
  52. super(accessor);
  53. if (!Config.ALLOW_NPC_WALKERS)
  54. {
  55. return;
  56. }
  57. _route = NpcWalkerRoutesData.getInstance().getRouteForNpc(getActor().getNpcId());
  58. // Here we need 1 second initial delay cause getActor().hasAI() will return null...
  59. // Constructor of L2NpcWalkerAI is called faster then ai object is attached in L2NpcWalkerInstance
  60. if (_route != null)
  61. {
  62. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  63. }
  64. else
  65. {
  66. _log.warning(getClass().getSimpleName() + ": Missing route data! Npc: " + _actor);
  67. }
  68. }
  69. @Override
  70. public void run()
  71. {
  72. onEvtThink();
  73. }
  74. @Override
  75. protected void onEvtThink()
  76. {
  77. if (!Config.ALLOW_NPC_WALKERS)
  78. {
  79. return;
  80. }
  81. if (isWalkingToNextPoint())
  82. {
  83. checkArrived();
  84. return;
  85. }
  86. if (_nextMoveTime < System.currentTimeMillis())
  87. {
  88. walkToLocation();
  89. }
  90. }
  91. /**
  92. * If npc can't walk to it's target then just teleport to next point
  93. * @param blocked_at_pos ignoring it
  94. */
  95. @Override
  96. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  97. {
  98. _log.warning(getClass().getSimpleName() + ": " + getActor().getNpcId() + ": Blocked at rote position [" + _currentPos + "], coords: " + blocked_at_pos.x + ", " + blocked_at_pos.y + ", " + blocked_at_pos.z + ". Teleporting to next point");
  99. int destinationX = _route.get(_currentPos).getMoveX();
  100. int destinationY = _route.get(_currentPos).getMoveY();
  101. int destinationZ = _route.get(_currentPos).getMoveZ();
  102. getActor().teleToLocation(destinationX, destinationY, destinationZ, false);
  103. super.onEvtArrivedBlocked(blocked_at_pos);
  104. }
  105. private void checkArrived()
  106. {
  107. int destinationX = _route.get(_currentPos).getMoveX();
  108. int destinationY = _route.get(_currentPos).getMoveY();
  109. int destinationZ = _route.get(_currentPos).getMoveZ();
  110. if (getActor().isInsideRadius(destinationX, destinationY, destinationZ, 5, false, false))
  111. {
  112. NpcStringId npcString = _route.get(_currentPos).getNpcString();
  113. String chat = null;
  114. if (npcString == null)
  115. {
  116. chat = _route.get(_currentPos).getChatText();
  117. }
  118. if ((npcString != null) || ((chat != null) && !chat.isEmpty()))
  119. {
  120. getActor().broadcastChat(chat, npcString);
  121. }
  122. // time in millis
  123. long delay = _route.get(_currentPos).getDelay() * 1000;
  124. // sleeps between each move
  125. if (delay < 0)
  126. {
  127. delay = DEFAULT_MOVE_DELAY;
  128. if (Config.DEVELOPER)
  129. {
  130. _log.warning(getClass().getSimpleName() + ": Wrong Delay Set in Npc Walker Functions = " + delay + " secs, using default delay: " + DEFAULT_MOVE_DELAY + " secs instead.");
  131. }
  132. }
  133. _nextMoveTime = System.currentTimeMillis() + delay;
  134. setWalkingToNextPoint(false);
  135. }
  136. }
  137. private void walkToLocation()
  138. {
  139. if (_currentPos < (_route.size() - 1))
  140. {
  141. _currentPos++;
  142. }
  143. else
  144. {
  145. _currentPos = 0;
  146. }
  147. boolean moveType = _route.get(_currentPos).getRunning();
  148. /**
  149. * false - walking true - Running
  150. */
  151. if (moveType)
  152. {
  153. getActor().setRunning();
  154. }
  155. else
  156. {
  157. getActor().setWalking();
  158. }
  159. // now we define destination
  160. int destinationX = _route.get(_currentPos).getMoveX();
  161. int destinationY = _route.get(_currentPos).getMoveY();
  162. int destinationZ = _route.get(_currentPos).getMoveZ();
  163. // notify AI of MOVE_TO
  164. setWalkingToNextPoint(true);
  165. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(destinationX, destinationY, destinationZ, 0));
  166. }
  167. @Override
  168. public L2NpcWalkerInstance getActor()
  169. {
  170. return (L2NpcWalkerInstance) super.getActor();
  171. }
  172. public int getHomeX()
  173. {
  174. return _homeX;
  175. }
  176. public int getHomeY()
  177. {
  178. return _homeY;
  179. }
  180. public int getHomeZ()
  181. {
  182. return _homeZ;
  183. }
  184. public void setHomeX(int homeX)
  185. {
  186. _homeX = homeX;
  187. }
  188. public void setHomeY(int homeY)
  189. {
  190. _homeY = homeY;
  191. }
  192. public void setHomeZ(int homeZ)
  193. {
  194. _homeZ = homeZ;
  195. }
  196. public boolean isWalkingToNextPoint()
  197. {
  198. return _walkingToNextPoint;
  199. }
  200. public void setWalkingToNextPoint(boolean value)
  201. {
  202. _walkingToNextPoint = value;
  203. }
  204. }