L2NpcWalkerAI.java 5.6 KB

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