L2NpcWalkerAI.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.NpcWalkerRoutesData;
  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. import com.l2jserver.gameserver.network.NpcStringId;
  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.
  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. {
  51. return;
  52. }
  53. _route = NpcWalkerRoutesData.getInstance().getRouteForNpc(getActor().getNpcId());
  54. // Here we need 1 second initial delay cause getActor().hasAI() will return null...
  55. // Constructor of L2NpcWalkerAI is called faster then ai object is attached in L2NpcWalkerInstance
  56. if (_route != null)
  57. {
  58. ThreadPoolManager.getInstance().scheduleAiAtFixedRate(this, 1000, 1000);
  59. }
  60. else
  61. {
  62. _log.warning(getClass().getSimpleName() + ": Missing route data! Npc: " + _actor);
  63. }
  64. }
  65. @Override
  66. public void run()
  67. {
  68. onEvtThink();
  69. }
  70. @Override
  71. protected void onEvtThink()
  72. {
  73. if (!Config.ALLOW_NPC_WALKERS)
  74. {
  75. return;
  76. }
  77. if (isWalkingToNextPoint())
  78. {
  79. checkArrived();
  80. return;
  81. }
  82. if (_nextMoveTime < System.currentTimeMillis())
  83. {
  84. walkToLocation();
  85. }
  86. }
  87. /**
  88. * If npc can't walk to it's target then just teleport to next point
  89. * @param blocked_at_pos ignoring it
  90. */
  91. @Override
  92. protected void onEvtArrivedBlocked(L2CharPosition blocked_at_pos)
  93. {
  94. _log.warning(getClass().getSimpleName() + ": " + getActor().getNpcId() + ": Blocked at rote position [" + _currentPos + "], coords: " + blocked_at_pos.x + ", " + blocked_at_pos.y + ", " + blocked_at_pos.z + ". Teleporting to next point");
  95. int destinationX = _route.get(_currentPos).getMoveX();
  96. int destinationY = _route.get(_currentPos).getMoveY();
  97. int destinationZ = _route.get(_currentPos).getMoveZ();
  98. getActor().teleToLocation(destinationX, destinationY, destinationZ, false);
  99. super.onEvtArrivedBlocked(blocked_at_pos);
  100. }
  101. private void checkArrived()
  102. {
  103. int destinationX = _route.get(_currentPos).getMoveX();
  104. int destinationY = _route.get(_currentPos).getMoveY();
  105. int destinationZ = _route.get(_currentPos).getMoveZ();
  106. if (getActor().isInsideRadius(destinationX, destinationY, destinationZ, 5, false, false))
  107. {
  108. NpcStringId npcString = _route.get(_currentPos).getNpcString();
  109. String chat = null;
  110. if (npcString == null)
  111. {
  112. chat = _route.get(_currentPos).getChatText();
  113. }
  114. if ((npcString != null) || ((chat != null) && !chat.isEmpty()))
  115. {
  116. getActor().broadcastChat(chat, npcString);
  117. }
  118. // time in millis
  119. long delay = _route.get(_currentPos).getDelay() * 1000;
  120. // sleeps between each move
  121. if (delay < 0)
  122. {
  123. delay = DEFAULT_MOVE_DELAY;
  124. if (Config.DEVELOPER)
  125. {
  126. _log.warning(getClass().getSimpleName() + ": Wrong Delay Set in Npc Walker Functions = " + delay + " secs, using default delay: " + DEFAULT_MOVE_DELAY + " secs instead.");
  127. }
  128. }
  129. _nextMoveTime = System.currentTimeMillis() + delay;
  130. setWalkingToNextPoint(false);
  131. }
  132. }
  133. private void walkToLocation()
  134. {
  135. if (_currentPos < (_route.size() - 1))
  136. {
  137. _currentPos++;
  138. }
  139. else
  140. {
  141. _currentPos = 0;
  142. }
  143. boolean moveType = _route.get(_currentPos).getRunning();
  144. /**
  145. * false - walking true - Running
  146. */
  147. if (moveType)
  148. {
  149. getActor().setRunning();
  150. }
  151. else
  152. {
  153. getActor().setWalking();
  154. }
  155. // now we define destination
  156. int destinationX = _route.get(_currentPos).getMoveX();
  157. int destinationY = _route.get(_currentPos).getMoveY();
  158. int destinationZ = _route.get(_currentPos).getMoveZ();
  159. // notify AI of MOVE_TO
  160. setWalkingToNextPoint(true);
  161. setIntention(CtrlIntention.AI_INTENTION_MOVE_TO, new L2CharPosition(destinationX, destinationY, destinationZ, 0));
  162. }
  163. @Override
  164. public L2NpcWalkerInstance getActor()
  165. {
  166. return (L2NpcWalkerInstance) super.getActor();
  167. }
  168. public int getHomeX()
  169. {
  170. return _homeX;
  171. }
  172. public int getHomeY()
  173. {
  174. return _homeY;
  175. }
  176. public int getHomeZ()
  177. {
  178. return _homeZ;
  179. }
  180. public void setHomeX(int homeX)
  181. {
  182. _homeX = homeX;
  183. }
  184. public void setHomeY(int homeY)
  185. {
  186. _homeY = homeY;
  187. }
  188. public void setHomeZ(int homeZ)
  189. {
  190. _homeZ = homeZ;
  191. }
  192. public boolean isWalkingToNextPoint()
  193. {
  194. return _walkingToNextPoint;
  195. }
  196. public void setWalkingToNextPoint(boolean value)
  197. {
  198. _walkingToNextPoint = value;
  199. }
  200. }