AiManager.java 6.6 KB

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