L2NpcWalkerAI.java 5.7 KB

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