L2NpcWalkerAI.java 5.6 KB

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