AiInstance.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package net.sf.l2j.gameserver.ai2;
  20. import java.util.Collection;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import javolution.util.FastMap;
  24. import net.sf.l2j.gameserver.TaskPriority;
  25. /**
  26. *
  27. * @author -Wooden-
  28. *
  29. */
  30. public class AiInstance
  31. {
  32. private Map<AiEventType,EventHandlerSet> _eventHandlers;
  33. private AiPlugingParameters _pluginigParams;
  34. public AiInstance(AiPlugingParameters params)
  35. {
  36. if(params.isConverted())
  37. throw new IllegalArgumentException("AiPluginingParameters of an Ai instance must be converted");
  38. _pluginigParams = params;
  39. //TODO:update the params (bottom-up)
  40. _eventHandlers = new FastMap<AiEventType,EventHandlerSet>();
  41. AiManager.getInstance().addAiInstance(this);
  42. }
  43. public AiInstance(AiInstance instance, AiPlugingParameters params)
  44. {
  45. this(params);
  46. this.copyHanlders(instance);
  47. }
  48. public void copyHanlders(AiInstance instance)
  49. {
  50. //then copy all the hanlders from 'instance'
  51. for(EventHandlerSet set :instance.getEventHandlerSets())
  52. {
  53. addHandlerSet(set.getEventType(), set);
  54. }
  55. }
  56. /**
  57. * <p>This methode add the handler to the {@link EventHandlerSet} associated with the specified{@link AiEventType}</p>
  58. * @param handler the handler to be added
  59. */
  60. public void addHandler(EventHandler handler)
  61. {
  62. EventHandlerSet set = _eventHandlers.get(handler.getEvenType());
  63. if(set == null)
  64. {
  65. set = new EventHandlerSet(handler, TaskPriority.PR_NORMAL);
  66. _eventHandlers.put(handler.getEvenType(), set);
  67. }
  68. else
  69. {
  70. set.addHandler(handler);
  71. }
  72. }
  73. public void addHandlerSet(AiEventType event, EventHandlerSet set)
  74. {
  75. _eventHandlers.put(event, set);
  76. }
  77. public class QueueEventRunner implements Runnable
  78. {
  79. private EventHandlerSet _set;
  80. private AiParameters _ai;
  81. private AiEvent _event;
  82. public QueueEventRunner(EventHandlerSet set, AiParameters ai, AiEvent event)
  83. {
  84. _set = set;
  85. _ai = ai;
  86. _event = event;
  87. }
  88. public void run()
  89. {
  90. for(EventHandler handler : _set.getHandlers())
  91. handler.runImpl(_ai, _event);
  92. AiInstance.this.launchNextEvent(_ai);
  93. }
  94. }
  95. /**
  96. * @param _aiParams
  97. *
  98. */
  99. public void launchNextEvent(AiParameters aiParams)
  100. {
  101. if(aiParams.hasEvents())
  102. {
  103. AiEvent event = aiParams.nextEvent();
  104. AiManager.getInstance().executeEventHandler(new QueueEventRunner(_eventHandlers.get(event.getType()), aiParams, event));
  105. }
  106. }
  107. public void triggerEvent(AiEvent event, AiParameters aiParams)
  108. {
  109. if(aiParams.isEventInhibited(event.getType()))
  110. return;
  111. boolean restart = false;
  112. synchronized(aiParams)
  113. {
  114. // if there was no events in the queue start processing events after we add them.
  115. if(!aiParams.hasEvents())
  116. {
  117. restart = true;
  118. }
  119. aiParams.queueEvents(event);
  120. if(restart)
  121. this.launchNextEvent(aiParams);
  122. }
  123. }
  124. public AiPlugingParameters getPluginingParamaters()
  125. {
  126. return _pluginigParams;
  127. }
  128. public Collection<EventHandlerSet> getEventHandlerSets()
  129. {
  130. return _eventHandlers.values();
  131. }
  132. /**
  133. * @return
  134. */
  135. public Set<Integer> getHandledNPCIds()
  136. {
  137. return _pluginigParams.getIDs();
  138. }
  139. }