L2NpcWalkerAI.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. public void run()
  61. {
  62. onEvtThink();
  63. }
  64. @Override
  65. protected void onEvtThink()
  66. {
  67. if (!Config.ALLOW_NPC_WALKERS)
  68. return;
  69. if (isWalkingToNextPoint())
  70. {
  71. checkArrived();
  72. return;
  73. }
  74. if (_nextMoveTime < System.currentTimeMillis())
  75. walkToLocation();
  76. }
  77. /**
  78. * If npc can't walk to it's target then just teleport to next point
  79. * @param blocked_at_pos ignoring it
  80. */
  81. @Override
  82. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  83. {
  84. _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");
  85. int destinationX = _route.get(_currentPos).getMoveX();
  86. int destinationY = _route.get(_currentPos).getMoveY();
  87. int destinationZ = _route.get(_currentPos).getMoveZ();
  88. getActor().teleToLocation(destinationX, destinationY, destinationZ, false);
  89. super.onEvtArrivedBlocked(blocked_at_pos);
  90. }
  91. private void checkArrived()
  92. {
  93. int destinationX = _route.get(_currentPos).getMoveX();
  94. int destinationY = _route.get(_currentPos).getMoveY();
  95. int destinationZ = _route.get(_currentPos).getMoveZ();
  96. if (getActor().isInsideRadius(destinationX, destinationY, destinationZ, 5, false, false))
  97. {
  98. NpcStringId npcString = _route.get(_currentPos).getNpcString();
  99. String chat = null;
  100. if (npcString == null)
  101. chat = _route.get(_currentPos).getChatText();
  102. if ((npcString != null) || (chat != null && !chat.isEmpty()))
  103. getActor().broadcastChat(chat, npcString);
  104. //time in millis
  105. long delay = _route.get(_currentPos).getDelay() * 1000;
  106. //sleeps between each move
  107. if (delay <= 0)
  108. {
  109. delay = DEFAULT_MOVE_DELAY;
  110. if (Config.DEVELOPER)
  111. _log.warning("Wrong Delay Set in Npc Walker Functions = " + delay + " secs, using default delay: " + DEFAULT_MOVE_DELAY + " secs instead.");
  112. }
  113. _nextMoveTime = System.currentTimeMillis() + delay;
  114. setWalkingToNextPoint(false);
  115. }
  116. }
  117. private void walkToLocation()
  118. {
  119. if (_currentPos < (_route.size() - 1))
  120. _currentPos++;
  121. else
  122. _currentPos = 0;
  123. boolean moveType = _route.get(_currentPos).getRunning();
  124. /**
  125. * false - walking
  126. * true - Running
  127. */
  128. if (moveType)
  129. getActor().setRunning();
  130. else
  131. getActor().setWalking();
  132. //now we define destination
  133. int destinationX = _route.get(_currentPos).getMoveX();
  134. int destinationY = _route.get(_currentPos).getMoveY();
  135. int destinationZ = _route.get(_currentPos).getMoveZ();
  136. //notify AI of MOVE_TO
  137. setWalkingToNextPoint(true);
  138. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(destinationX, destinationY, destinationZ, 0));
  139. }
  140. @Override
  141. public L2NpcWalkerInstance getActor()
  142. {
  143. return (L2NpcWalkerInstance) super.getActor();
  144. }
  145. public int getHomeX()
  146. {
  147. return _homeX;
  148. }
  149. public int getHomeY()
  150. {
  151. return _homeY;
  152. }
  153. public int getHomeZ()
  154. {
  155. return _homeZ;
  156. }
  157. public void setHomeX(int homeX)
  158. {
  159. _homeX = homeX;
  160. }
  161. public void setHomeY(int homeY)
  162. {
  163. _homeY = homeY;
  164. }
  165. public void setHomeZ(int homeZ)
  166. {
  167. _homeZ = homeZ;
  168. }
  169. public boolean isWalkingToNextPoint()
  170. {
  171. return _walkingToNextPoint;
  172. }
  173. public void setWalkingToNextPoint(boolean value)
  174. {
  175. _walkingToNextPoint = value;
  176. }
  177. }