2
0

AiManager.java 6.6 KB

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