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