AiManager.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 gnu.trove.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.net.URL;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import java.util.jar.JarFile;
  24. import java.util.logging.Level;
  25. import java.util.logging.Logger;
  26. import com.l2jserver.gameserver.ThreadPoolManager;
  27. import com.l2jserver.gameserver.ai2.AiInstance.QueueEventRunner;
  28. import javolution.util.FastList;
  29. import javolution.util.FastMap;
  30. import javolution.util.FastSet;
  31. /**
  32. * This class will load all the AI's and retain all of them.
  33. * It is a singleton
  34. * @author -Wooden-
  35. *
  36. */
  37. public class AiManager
  38. {
  39. protected static final Logger _log = Logger.getLogger(AiManager.class.getName());
  40. private List<AiInstance> _aiList;
  41. private TIntObjectHashMap<AiInstance> _aiMap;
  42. private ThreadPoolManager _tpm;
  43. private Map<String, String> _paramcache;
  44. public static AiManager getInstance()
  45. {
  46. return SingletonHolder._instance;
  47. }
  48. private AiManager()
  49. {
  50. _aiList = new FastList<AiInstance>();
  51. _aiMap = new TIntObjectHashMap<AiInstance>();
  52. _tpm = ThreadPoolManager.getInstance();
  53. _paramcache = new FastMap<String, String>();
  54. load();
  55. }
  56. public void load()
  57. {
  58. try
  59. {
  60. //TODO redo all the SpecificAiManger loading it's completely messed up:
  61. @SuppressWarnings("unused")
  62. JarFile jar = new JarFile("./l2jserver.jar");
  63. URL url = Class.class.getResource("/com/l2jserver/gameserver/ai/managers");
  64. //jar.getJarEntry("/net/sf/l2j/gameserver/ai/managers").get;
  65. if (url == null)
  66. {
  67. _log.severe("Could not open the ai managers folder. No ai will be loaded!");
  68. return;
  69. }
  70. File directory = new File(url.getFile());
  71. for (String file : directory.list())
  72. {
  73. if (file.endsWith(".class"))
  74. {
  75. try
  76. {
  77. Class<?> managerClass = Class.forName("com.l2jserver.gameserver.ai.managers." + file.substring(0, file.length() - 6));
  78. Object managerObject = managerClass.newInstance();
  79. if (!(managerObject instanceof ISpecificAiManager))
  80. {
  81. _log.info("A class that was not a ISpecificAiManager was found in the ai managers folder.");
  82. continue;
  83. }
  84. ISpecificAiManager managerInstance = (ISpecificAiManager) managerObject;
  85. for (EventHandler handler : managerInstance.getEventHandlers())
  86. {
  87. AiPlugingParameters pparams = handler.getPlugingParameters();
  88. pparams.convertToIDs();
  89. boolean perfectMatch = false;
  90. // let's check if any previously created AiInstance is allready used for the NPC concerned by this handler
  91. List<Intersection> intersections = new FastList<Intersection>();
  92. for (AiInstance ai : _aiList)
  93. {
  94. if (ai.getPluginingParamaters().equals(pparams))
  95. {
  96. ai.addHandler(handler);
  97. perfectMatch = true;
  98. break;
  99. }
  100. Intersection intersection = new Intersection(ai);
  101. for (int id : pparams.getIDs())
  102. {
  103. if (ai.getPluginingParamaters().contains(id))
  104. {
  105. //intersection with this AI
  106. intersection.ids.add(id);
  107. }
  108. }
  109. if (!intersection.isEmpty())
  110. intersections.add(intersection);
  111. }
  112. if (perfectMatch)
  113. continue;
  114. for (Intersection i : intersections)
  115. {
  116. //remove secant ids on both AiInstances
  117. pparams.removeIDs(i.ids);
  118. i.ai.getPluginingParamaters().removeIDs(i.ids); //TODO if this is extracted to a more general purpose method, dont forget to update linkages to AiIntances
  119. //create a new instance with the secant ids that will inherit all the handlers from the secant Ai
  120. AiPlugingParameters newAiPparams = new AiPlugingParameters(null, null, null, i.ids, null);
  121. AiInstance newAi = new AiInstance(i.ai, newAiPparams);
  122. newAi.addHandler(handler);
  123. _aiList.add(newAi);
  124. }
  125. if (pparams.isEmpty())
  126. continue;
  127. // create a new instance with the remaining ids
  128. AiInstance newAi = new AiInstance(pparams);
  129. newAi.addHandler(handler);
  130. _aiList.add(newAi);
  131. }
  132. }
  133. catch (Exception e)
  134. {
  135. _log.log(Level.WARNING, "Error while loading AiManager: " + e.getMessage(), e);
  136. }
  137. }
  138. }
  139. }
  140. catch (IOException e1)
  141. {
  142. _log.log(Level.WARNING, e1.getMessage(), e1);
  143. }
  144. // build a mighty map
  145. for (AiInstance ai : _aiList)
  146. {
  147. for (Integer i : ai.getHandledNPCIds())
  148. {
  149. _aiMap.put(i, ai);
  150. }
  151. }
  152. }
  153. public void executeEventHandler(QueueEventRunner runner)
  154. {
  155. _tpm.executeAi(runner);
  156. }
  157. /**
  158. * @param instance
  159. */
  160. public void addAiInstance(AiInstance instance)
  161. {
  162. _aiList.add(instance);
  163. }
  164. /**
  165. * @param npcId
  166. * @return
  167. */
  168. public AiInstance getAiForNPCId(int npcId)
  169. {
  170. return _aiMap.get(npcId);
  171. }
  172. public String getParameter(String who, String paramsType, String param1, String param2)
  173. {
  174. String key = who + ":" + paramsType + ":" + param1 + ":" + param2;
  175. String cacheResult = _paramcache.get(key);
  176. if (cacheResult != null)
  177. return cacheResult;
  178. String result = null;
  179. // get from SQL
  180. _paramcache.put(key, result);
  181. return null;
  182. }
  183. private class Intersection
  184. {
  185. public AiInstance ai;
  186. public Set<Integer> ids;
  187. public Intersection(AiInstance instance)
  188. {
  189. ai = instance;
  190. ids = new FastSet<Integer>();
  191. }
  192. public boolean isEmpty()
  193. {
  194. return ids.isEmpty();
  195. }
  196. }
  197. @SuppressWarnings("synthetic-access")
  198. private static class SingletonHolder
  199. {
  200. protected static final AiManager _instance = new AiManager();
  201. }
  202. }