AiInstance.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.ai2;
  16. import java.util.Collection;
  17. import java.util.Map;
  18. import java.util.Set;
  19. import com.l2jserver.gameserver.TaskPriority;
  20. import javolution.util.FastMap;
  21. /**
  22. *
  23. * @author -Wooden-
  24. *
  25. */
  26. public class AiInstance
  27. {
  28. private Map<AiEventType, EventHandlerSet> _eventHandlers;
  29. private AiPlugingParameters _pluginigParams;
  30. public AiInstance(AiPlugingParameters params)
  31. {
  32. if (params.isConverted())
  33. throw new IllegalArgumentException("AiPluginingParameters of an Ai instance must be converted");
  34. _pluginigParams = params;
  35. //TODO:update the params (bottom-up)
  36. _eventHandlers = new FastMap<AiEventType, EventHandlerSet>();
  37. AiManager.getInstance().addAiInstance(this);
  38. }
  39. public AiInstance(AiInstance instance, AiPlugingParameters params)
  40. {
  41. this(params);
  42. this.copyHanlders(instance);
  43. }
  44. public void copyHanlders(AiInstance instance)
  45. {
  46. //then copy all the hanlders from 'instance'
  47. for (EventHandlerSet set : instance.getEventHandlerSets())
  48. {
  49. addHandlerSet(set.getEventType(), set);
  50. }
  51. }
  52. /**
  53. * <p>This methode add the handler to the {@link EventHandlerSet} associated with the specified{@link AiEventType}</p>
  54. * @param handler the handler to be added
  55. */
  56. public void addHandler(EventHandler handler)
  57. {
  58. EventHandlerSet set = _eventHandlers.get(handler.getEvenType());
  59. if (set == null)
  60. {
  61. set = new EventHandlerSet(handler, TaskPriority.PR_NORMAL);
  62. _eventHandlers.put(handler.getEvenType(), set);
  63. }
  64. else
  65. {
  66. set.addHandler(handler);
  67. }
  68. }
  69. public void addHandlerSet(AiEventType event, EventHandlerSet set)
  70. {
  71. _eventHandlers.put(event, set);
  72. }
  73. public class QueueEventRunner implements Runnable
  74. {
  75. private EventHandlerSet _set;
  76. private AiParameters _ai;
  77. private AiEvent _event;
  78. public QueueEventRunner(EventHandlerSet set, AiParameters ai, AiEvent event)
  79. {
  80. _set = set;
  81. _ai = ai;
  82. _event = event;
  83. }
  84. public void run()
  85. {
  86. for (EventHandler handler : _set.getHandlers())
  87. handler.runImpl(_ai, _event);
  88. AiInstance.this.launchNextEvent(_ai);
  89. }
  90. }
  91. /**
  92. * @param _aiParams
  93. *
  94. */
  95. public void launchNextEvent(AiParameters aiParams)
  96. {
  97. if (aiParams.hasEvents())
  98. {
  99. AiEvent event = aiParams.nextEvent();
  100. AiManager.getInstance().executeEventHandler(new QueueEventRunner(_eventHandlers.get(event.getType()), aiParams, event));
  101. }
  102. }
  103. public void triggerEvent(AiEvent event, AiParameters aiParams)
  104. {
  105. if (aiParams.isEventInhibited(event.getType()))
  106. return;
  107. boolean restart = false;
  108. synchronized (aiParams)
  109. {
  110. // if there was no events in the queue start processing events after we add them.
  111. if (!aiParams.hasEvents())
  112. {
  113. restart = true;
  114. }
  115. aiParams.queueEvents(event);
  116. if (restart)
  117. this.launchNextEvent(aiParams);
  118. }
  119. }
  120. public AiPlugingParameters getPluginingParamaters()
  121. {
  122. return _pluginigParams;
  123. }
  124. public Collection<EventHandlerSet> getEventHandlerSets()
  125. {
  126. return _eventHandlers.values();
  127. }
  128. /**
  129. * @return
  130. */
  131. public Set<Integer> getHandledNPCIds()
  132. {
  133. return _pluginigParams.getIDs();
  134. }
  135. }