AiManager.java 6.5 KB

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