L2NpcWalkerAI.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. import org.apache.commons.logging.Log;
  28. import org.apache.commons.logging.LogFactory;
  29. public class L2NpcWalkerAI extends L2CharacterAI implements Runnable
  30. {
  31. private static final Log _log = LogFactory.getLog(L2NpcWalkerAI.class);
  32. private static final int DEFAULT_MOVE_DELAY = 0;
  33. private long _nextMoveTime;
  34. private boolean _walkingToNextPoint = false;
  35. /**
  36. * home points for xyz
  37. */
  38. int _homeX, _homeY, _homeZ;
  39. /**
  40. * route of the current npc
  41. */
  42. private final FastList<L2NpcWalkerNode> _route = NpcWalkerRoutesTable.getInstance().getRouteForNpc(getActor().getNpcId());
  43. /**
  44. * current node
  45. */
  46. private int _currentPos;
  47. /**
  48. * Constructor of L2CharacterAI.<BR><BR>
  49. *
  50. * @param accessor The AI accessor of the L2Character
  51. */
  52. public L2NpcWalkerAI(L2Character.AIAccessor accessor)
  53. {
  54. super(accessor);
  55. // Do we really need 2 minutes delay before start?
  56. // no we dont... :)
  57. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 0, 1000);
  58. }
  59. public void run()
  60. {
  61. onEvtThink();
  62. }
  63. protected void onEvtThink()
  64. {
  65. if(!Config.ALLOW_NPC_WALKERS)
  66. return;
  67. if(isWalkingToNextPoint())
  68. {
  69. checkArrived();
  70. return;
  71. }
  72. if(_nextMoveTime < System.currentTimeMillis())
  73. walkToLocation();
  74. }
  75. /**
  76. * If npc can't walk to it's target then just teleport to next point
  77. * @param blocked_at_pos ignoring it
  78. */
  79. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  80. {
  81. _log.error("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.equals(""))
  97. {
  98. try
  99. {
  100. getActor().broadcastChat(chat);
  101. }
  102. catch(ArrayIndexOutOfBoundsException e)
  103. {
  104. _log.info("L2NpcWalkerInstance: Error, " + 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.warn("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. 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. }