AiManager.java 6.6 KB

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