TarBeetleSpawn.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * Copyright (C) 2004-2015 L2J DataPack
  3. *
  4. * This file is part of L2J DataPack.
  5. *
  6. * L2J DataPack is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J DataPack is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package ai.npc.ForgeOfTheGods;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.concurrent.CopyOnWriteArrayList;
  23. import java.util.concurrent.ScheduledFuture;
  24. import org.w3c.dom.Document;
  25. import org.w3c.dom.NamedNodeMap;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.gameserver.GeoData;
  28. import com.l2jserver.gameserver.ThreadPoolManager;
  29. import com.l2jserver.gameserver.model.L2Spawn;
  30. import com.l2jserver.gameserver.model.L2Territory;
  31. import com.l2jserver.gameserver.model.Location;
  32. import com.l2jserver.gameserver.model.actor.L2Npc;
  33. import com.l2jserver.util.Rnd;
  34. import com.l2jserver.util.data.xml.IXmlReader;
  35. /**
  36. * Tar Beetle zone spawn
  37. * @author malyelfik
  38. */
  39. public class TarBeetleSpawn implements IXmlReader
  40. {
  41. private final List<SpawnZone> zones = new ArrayList<>();
  42. private ScheduledFuture<?> spawnTask;
  43. private ScheduledFuture<?> shotTask;
  44. public TarBeetleSpawn()
  45. {
  46. load();
  47. }
  48. @Override
  49. public void load()
  50. {
  51. parseDatapackFile("data/spawnZones/tar_beetle.xml");
  52. if (!zones.isEmpty())
  53. {
  54. spawnTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(() -> zones.forEach(SpawnZone::refreshSpawn), 1000, 60000);
  55. shotTask = ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(() -> zones.forEach(SpawnZone::refreshShots), 300000, 300000);
  56. }
  57. }
  58. @Override
  59. public void parseDocument(Document doc)
  60. {
  61. int i = 0;
  62. for (Node d = doc.getFirstChild(); d != null; d = d.getNextSibling())
  63. {
  64. if (d.getNodeName().equals("list"))
  65. {
  66. for (Node r = d.getFirstChild(); r != null; r = r.getNextSibling())
  67. {
  68. if (r.getNodeName().equals("spawnZone"))
  69. {
  70. NamedNodeMap attrs = r.getAttributes();
  71. final int npcCount = parseInteger(attrs, "maxNpcCount");
  72. final SpawnZone sp = new SpawnZone(npcCount, i);
  73. for (Node b = r.getFirstChild(); b != null; b = b.getNextSibling())
  74. {
  75. if (b.getNodeName().equals("zone"))
  76. {
  77. attrs = b.getAttributes();
  78. final int minZ = parseInteger(attrs, "minZ");
  79. final int maxZ = parseInteger(attrs, "maxZ");
  80. final Zone zone = new Zone();
  81. for (Node c = b.getFirstChild(); c != null; c = c.getNextSibling())
  82. {
  83. attrs = c.getAttributes();
  84. if (c.getNodeName().equals("point"))
  85. {
  86. final int x = parseInteger(attrs, "x");
  87. final int y = parseInteger(attrs, "y");
  88. zone.add(x, y, minZ, maxZ, 0);
  89. }
  90. else if (c.getNodeName().equals("bannedZone"))
  91. {
  92. final Zone bannedZone = new Zone();
  93. final int bMinZ = parseInteger(attrs, "minZ");
  94. final int bMaxZ = parseInteger(attrs, "maxZ");
  95. for (Node f = c.getFirstChild(); f != null; f = f.getNextSibling())
  96. {
  97. if (f.getNodeName().equals("point"))
  98. {
  99. attrs = f.getAttributes();
  100. int x = parseInteger(attrs, "x");
  101. int y = parseInteger(attrs, "y");
  102. bannedZone.add(x, y, bMinZ, bMaxZ, 0);
  103. }
  104. }
  105. zone.addBannedZone(bannedZone);
  106. }
  107. }
  108. sp.addZone(zone);
  109. }
  110. }
  111. zones.add(i++, sp);
  112. }
  113. }
  114. }
  115. }
  116. }
  117. public final void unload()
  118. {
  119. if (spawnTask != null)
  120. {
  121. spawnTask.cancel(false);
  122. }
  123. if (shotTask != null)
  124. {
  125. shotTask.cancel(false);
  126. }
  127. zones.forEach(SpawnZone::unload);
  128. zones.clear();
  129. }
  130. public final void removeBeetle(L2Npc npc)
  131. {
  132. zones.get(npc.getVariables().getInt("zoneIndex", 0)).removeSpawn(npc);
  133. npc.deleteMe();
  134. }
  135. private final class Zone extends L2Territory
  136. {
  137. private List<Zone> _bannedZones;
  138. public Zone()
  139. {
  140. super(1);
  141. }
  142. @Override
  143. public Location getRandomPoint()
  144. {
  145. Location location = super.getRandomPoint();
  146. while ((location != null) && isInsideBannedZone(location))
  147. {
  148. location = super.getRandomPoint();
  149. }
  150. return location;
  151. }
  152. public final void addBannedZone(Zone bZone)
  153. {
  154. if (_bannedZones == null)
  155. {
  156. _bannedZones = new ArrayList<>();
  157. }
  158. _bannedZones.add(bZone);
  159. }
  160. private final boolean isInsideBannedZone(Location location)
  161. {
  162. if (_bannedZones != null)
  163. {
  164. for (Zone z : _bannedZones)
  165. {
  166. if (z.isInside(location.getX(), location.getY()))
  167. {
  168. return true;
  169. }
  170. }
  171. }
  172. return false;
  173. }
  174. }
  175. private final class SpawnZone
  176. {
  177. private final List<Zone> _zones = new ArrayList<>();
  178. private final List<L2Npc> _spawn = new CopyOnWriteArrayList<>();
  179. private final int _maxNpcCount;
  180. private final int _index;
  181. public SpawnZone(int maxNpcCount, int index)
  182. {
  183. _maxNpcCount = maxNpcCount;
  184. _index = index;
  185. }
  186. public final void addZone(Zone zone)
  187. {
  188. _zones.add(zone);
  189. }
  190. public final void removeSpawn(L2Npc obj)
  191. {
  192. _spawn.remove(obj);
  193. }
  194. public final void unload()
  195. {
  196. _spawn.forEach(L2Npc::deleteMe);
  197. _spawn.clear();
  198. _zones.clear();
  199. }
  200. public final void refreshSpawn()
  201. {
  202. try
  203. {
  204. while (_spawn.size() < _maxNpcCount)
  205. {
  206. final Location location = _zones.get(Rnd.get(_zones.size())).getRandomPoint();
  207. if (location != null)
  208. {
  209. final L2Spawn spawn = new L2Spawn(18804);
  210. spawn.setHeading(Rnd.get(65535));
  211. spawn.setX(location.getX());
  212. spawn.setY(location.getY());
  213. spawn.setZ(GeoData.getInstance().getSpawnHeight(location));
  214. final L2Npc npc = spawn.doSpawn();
  215. npc.setIsNoRndWalk(true);
  216. npc.setIsImmobilized(true);
  217. npc.setIsInvul(true);
  218. npc.disableCoreAI(true);
  219. npc.setScriptValue(5);
  220. npc.getVariables().set("zoneIndex", _index);
  221. _spawn.add(npc);
  222. }
  223. }
  224. }
  225. catch (Exception e)
  226. {
  227. LOGGER.warn("{}: Could not refresh spawns!", getClass().getSimpleName(), e);
  228. }
  229. }
  230. public final void refreshShots()
  231. {
  232. if (_spawn.size() > 0)
  233. {
  234. for (L2Npc npc : _spawn)
  235. {
  236. final int val = npc.getScriptValue();
  237. if (val == 5)
  238. {
  239. npc.deleteMe();
  240. _spawn.remove(npc);
  241. }
  242. else
  243. {
  244. npc.setScriptValue(val + 1);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }