L2NpcWalkerAI.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; either version 2, or (at your option)
  4. * any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14. * 02111-1307, USA.
  15. *
  16. * http://www.gnu.org/copyleft/gpl.html
  17. */
  18. package net.sf.l2j.gameserver.ai;
  19. import javolution.util.FastList;
  20. import net.sf.l2j.Config;
  21. import net.sf.l2j.gameserver.ThreadPoolManager;
  22. import net.sf.l2j.gameserver.datatables.NpcWalkerRoutesTable;
  23. import net.sf.l2j.gameserver.model.L2CharPosition;
  24. import net.sf.l2j.gameserver.model.L2Character;
  25. import net.sf.l2j.gameserver.model.L2NpcWalkerNode;
  26. import net.sf.l2j.gameserver.model.actor.instance.L2NpcWalkerInstance;
  27. public class L2NpcWalkerAI extends L2CharacterAI implements Runnable
  28. {
  29. private static final int DEFAULT_MOVE_DELAY = 0;
  30. private long _nextMoveTime;
  31. private boolean _walkingToNextPoint = false;
  32. /**
  33. * home points for xyz
  34. */
  35. int _homeX, _homeY, _homeZ;
  36. /**
  37. * route of the current npc
  38. */
  39. private final FastList<L2NpcWalkerNode> _route = NpcWalkerRoutesTable.getInstance().getRouteForNpc(getActor().getNpcId());
  40. /**
  41. * current node
  42. */
  43. private int _currentPos;
  44. /**
  45. * Constructor of L2CharacterAI.<BR><BR>
  46. *
  47. * @param accessor The AI accessor of the L2Character
  48. */
  49. public L2NpcWalkerAI(L2Character.AIAccessor accessor)
  50. {
  51. super(accessor);
  52. // Do we really need 2 minutes delay before start?
  53. // no we dont... :)
  54. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 0, 1000);
  55. }
  56. public void run()
  57. {
  58. onEvtThink();
  59. }
  60. protected void onEvtThink()
  61. {
  62. if(!Config.ALLOW_NPC_WALKERS)
  63. return;
  64. if(isWalkingToNextPoint())
  65. {
  66. checkArrived();
  67. return;
  68. }
  69. if(_nextMoveTime < System.currentTimeMillis())
  70. walkToLocation();
  71. }
  72. /**
  73. * If npc can't walk to it's target then just teleport to next point
  74. * @param blocked_at_pos ignoring it
  75. */
  76. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  77. {
  78. _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");
  79. int destinationX = _route.get(_currentPos).getMoveX();
  80. int destinationY = _route.get(_currentPos).getMoveY();
  81. int destinationZ = _route.get(_currentPos).getMoveZ();
  82. getActor().teleToLocation(destinationX, destinationY, destinationZ, false);
  83. super.onEvtArrivedBlocked(blocked_at_pos);
  84. }
  85. private void checkArrived()
  86. {
  87. int destinationX = _route.get(_currentPos).getMoveX();
  88. int destinationY = _route.get(_currentPos).getMoveY();
  89. int destinationZ = _route.get(_currentPos).getMoveZ();
  90. if(getActor().getX() == destinationX && getActor().getY() == destinationY && getActor().getZ() == destinationZ)
  91. {
  92. String chat = _route.get(_currentPos).getChatText();
  93. if(chat != null && !chat.equals(""))
  94. {
  95. try
  96. {
  97. getActor().broadcastChat(chat);
  98. }
  99. catch(ArrayIndexOutOfBoundsException e)
  100. {
  101. _log.info("L2NpcWalkerInstance: Error, " + e);
  102. }
  103. }
  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. 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. }