Universe.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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;
  16. import java.awt.Color;
  17. import java.awt.Graphics2D;
  18. import java.awt.image.BufferedImage;
  19. import java.io.BufferedReader;
  20. import java.io.DataInputStream;
  21. import java.io.DataOutputStream;
  22. import java.io.File;
  23. import java.io.FileInputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.FileOutputStream;
  26. import java.io.FileReader;
  27. import java.io.FilenameFilter;
  28. import java.io.IOException;
  29. import java.io.ObjectInputStream;
  30. import java.util.HashSet;
  31. import java.util.LinkedList;
  32. import java.util.List;
  33. import java.util.StringTokenizer;
  34. import java.util.TreeSet;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. import java.util.zip.GZIPInputStream;
  38. import javax.imageio.ImageIO;
  39. import com.l2jserver.Config;
  40. import com.l2jserver.gameserver.model.L2CharPosition;
  41. public class Universe implements java.io.Serializable
  42. {
  43. /**
  44. * Comment for <code>serialVersionUID</code>
  45. */
  46. private static final long serialVersionUID = -2040223695811104704L;
  47. public static final int MIN_X = -127900;
  48. public static final int MAX_X = 194327;
  49. public static final int MIN_Y = -30000;
  50. public static final int MAX_Y = 259536;
  51. public static final int MIN_Z = -17000;
  52. public static final int MAX_Z = 17000;
  53. public static final int MIN_X_GRID = 60;
  54. public static final int MIN_Y_GRID = 60;
  55. public static final int MIN_Z_GRID = 60;
  56. public static final int MIN_GRID = 360;
  57. protected static final Logger _log = Logger.getLogger(Universe.class.getName());
  58. protected List<Coord> _coordList;
  59. private HashSet<Integer> _logPlayers;
  60. private boolean _logAll = true;
  61. public static void main(String[] args)
  62. {
  63. Universe u = new Universe();
  64. u.load();
  65. //u.removeDoubles();
  66. u.implode(false);
  67. }
  68. private class Position implements Comparable<Position>, java.io.Serializable
  69. {
  70. /**
  71. * Comment for <code>serialVersionUID</code>
  72. */
  73. private static final long serialVersionUID = -8798746764450022287L;
  74. protected int _x;
  75. protected int _flag;
  76. protected int _y;
  77. protected int _z;
  78. public Position(int x, int y, int z, int flag)
  79. {
  80. _x = x;
  81. _y = y;
  82. _z = z;
  83. _flag = flag;
  84. }
  85. public Position(L2CharPosition pos)
  86. {
  87. _x = pos.x;
  88. _y = pos.y;
  89. _z = pos.z;
  90. _flag = 0;
  91. }
  92. @Deprecated
  93. public L2CharPosition l2CP()
  94. {
  95. return new L2CharPosition(_x, _y, _z, 0);
  96. }
  97. public int compareTo(Position obj)
  98. {
  99. int res = Integer.valueOf(_x).compareTo(obj._x);
  100. if (res != 0)
  101. return res;
  102. res = Integer.valueOf(_y).compareTo(obj._y);
  103. if (res != 0)
  104. return res;
  105. res = Integer.valueOf(_z).compareTo(obj._z);
  106. return res;
  107. }
  108. @Override
  109. public String toString()
  110. {
  111. return String.valueOf(_x) + " " + _y + " " + _z + " " + _flag;
  112. }
  113. }
  114. private class Coord implements Comparable<Position>, java.io.Serializable
  115. {
  116. /**
  117. * Comment for <code>serialVersionUID</code>
  118. */
  119. private static final long serialVersionUID = -558060332886829552L;
  120. protected int _x;
  121. protected int _y;
  122. protected int _z;
  123. public Coord(int x, int y, int z)
  124. {
  125. _x = x;
  126. _y = y;
  127. _z = z;
  128. }
  129. public Coord(L2CharPosition pos)
  130. {
  131. _x = pos.x;
  132. _y = pos.y;
  133. _z = pos.z;
  134. }
  135. public int compareTo(Position obj)
  136. {
  137. int res = Integer.valueOf(_x).compareTo(obj._x);
  138. if (res != 0)
  139. return res;
  140. res = Integer.valueOf(_y).compareTo(obj._y);
  141. if (res != 0)
  142. return res;
  143. res = Integer.valueOf(_z).compareTo(obj._z);
  144. return res;
  145. }
  146. @Override
  147. public String toString()
  148. {
  149. return String.valueOf(_x) + " " + _y + " " + _z;
  150. }
  151. }
  152. public static Universe getInstance()
  153. {
  154. return SingletonHolder._instance;
  155. }
  156. private Universe()
  157. {
  158. _coordList = new LinkedList<Coord>();
  159. _logPlayers = new HashSet<Integer>();
  160. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new UniverseDump(), 30000, 30000);
  161. }
  162. public void registerHeight(int x, int y, int z)
  163. {
  164. // don't overwrite obstacle entries
  165. //Position p = new Position(x, y, z, 0);
  166. //_map.add(p);
  167. _coordList.add(new Coord(x, y, z));
  168. }
  169. public void registerObstacle(int x, int y, int z)
  170. {
  171. //Position p = new Position(x, y, z, -1);
  172. //_map.add(p);
  173. _coordList.add(new Coord(x, y, z));
  174. }
  175. public boolean shouldLog(Integer id)
  176. {
  177. return (_logPlayers.contains(id) || _logAll);
  178. }
  179. public void setLogAll(boolean flag)
  180. {
  181. _logAll = flag;
  182. }
  183. public void addLogPlayer(Integer id)
  184. {
  185. _logPlayers.add(id);
  186. _logAll = false;
  187. }
  188. public void removeLogPlayer(Integer id)
  189. {
  190. _logPlayers.remove(id);
  191. }
  192. public void loadAscii()
  193. {
  194. int initialSize = _coordList.size();
  195. BufferedReader r = null;
  196. try
  197. {
  198. r = new BufferedReader(new FileReader("data/universe.txt"));
  199. String line;
  200. while ((line = r.readLine()) != null)
  201. {
  202. StringTokenizer st = new StringTokenizer(line);
  203. String x1 = st.nextToken();
  204. String y1 = st.nextToken();
  205. String z1 = st.nextToken();
  206. // String f1 = st.nextToken();
  207. int x = Integer.parseInt(x1);
  208. int y = Integer.parseInt(y1);
  209. int z = Integer.parseInt(z1);
  210. // int f = Integer.parseInt(f1);
  211. _coordList.add(new Coord(x, y, z));
  212. }
  213. _log.info((_coordList.size() - initialSize) + " additional nodes loaded from text file.");
  214. }
  215. catch (Exception e)
  216. {
  217. _log.info("could not read text file universe.txt");
  218. }
  219. finally
  220. {
  221. try
  222. {
  223. r.close();
  224. }
  225. catch (Exception e)
  226. {
  227. }
  228. }
  229. }
  230. public void createMap()
  231. {
  232. int zoom = 100;
  233. int w = (MAX_X - MIN_X) / zoom;
  234. int h = (MAX_Y - MIN_Y) / zoom;
  235. BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_GRAY);
  236. Graphics2D gr = bi.createGraphics();
  237. int min_z = 0, max_z = 0;
  238. for (Coord pos : _coordList)
  239. {
  240. if (pos == null)
  241. continue;
  242. if (pos._z < min_z)
  243. min_z = pos._z;
  244. if (pos._z > max_z)
  245. max_z = pos._z;
  246. }
  247. for (Coord pos : _coordList)
  248. {
  249. if (pos == null)
  250. continue;
  251. int x = (pos._x - MIN_X) / zoom;
  252. int y = (pos._y - MIN_Y) / zoom;
  253. int color = (int) (((long) pos._z - MIN_Z) * 0xFFFFFF / (MAX_Z - MIN_Z));
  254. gr.setColor(new Color(color));
  255. gr.drawLine(x, y, x, y);
  256. }
  257. try
  258. {
  259. ImageIO.write(bi, "png", new File("universe.png"));
  260. }
  261. catch (Exception e)
  262. {
  263. _log.log(Level.WARNING, "Cannot create universe.png: " + e.getMessage(), e);
  264. }
  265. }
  266. public class UniverseFilter implements FilenameFilter
  267. {
  268. String _ext = "";
  269. public UniverseFilter(String pExt)
  270. {
  271. _ext = pExt;
  272. }
  273. /* (non-Javadoc)
  274. * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
  275. */
  276. public boolean accept(File arg0, String name)
  277. {
  278. return name.startsWith("universe") && name.endsWith("." + _ext);
  279. }
  280. }
  281. public void load()
  282. {
  283. int total = 0;
  284. if (_coordList == null)
  285. {
  286. _coordList = new LinkedList<Coord>();
  287. }
  288. try
  289. {
  290. loadBinFiles();
  291. loadHexFiles();
  292. loadFinFiles();
  293. _log.info(_coordList.size() + " map vertices loaded in total.");
  294. }
  295. catch (Exception e)
  296. {
  297. e.printStackTrace();
  298. }
  299. _log.info("Total: " + total);
  300. }
  301. /**
  302. * @throws FileNotFoundException
  303. * @throws IOException
  304. */
  305. private void loadFinFiles() throws FileNotFoundException, IOException
  306. {
  307. FilenameFilter filter = new UniverseFilter("fin");
  308. File directory = new File("data");
  309. File[] files = directory.listFiles(filter);
  310. for (File file : files)
  311. {
  312. FileInputStream fos = new FileInputStream(file); // Save to file
  313. DataInputStream data = new DataInputStream(fos);
  314. int count = data.readInt();
  315. List<Coord> newMap = new LinkedList<Coord>();
  316. for (int i = 0; i < count; i++)
  317. {
  318. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  319. }
  320. data.close(); // Close the stream.
  321. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  322. _coordList.addAll(newMap);
  323. }
  324. }
  325. /**
  326. * @throws FileNotFoundException
  327. * @throws IOException
  328. */
  329. private void loadHexFiles() throws FileNotFoundException, IOException
  330. {
  331. FilenameFilter filter = new UniverseFilter("hex");
  332. File directory = new File("data");
  333. File[] files = directory.listFiles(filter);
  334. for (File file : files)
  335. {
  336. FileInputStream fos = new FileInputStream(file); // Save to file
  337. GZIPInputStream gzos = new GZIPInputStream(fos);
  338. DataInputStream data = new DataInputStream(gzos);
  339. int count = data.readInt();
  340. List<Coord> newMap = new LinkedList<Coord>();
  341. for (int i = 0; i < count; i++)
  342. {
  343. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  344. data.readInt();
  345. }
  346. data.close(); // Close the stream.
  347. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  348. _coordList.addAll(newMap);
  349. }
  350. }
  351. /**
  352. * @throws FileNotFoundException
  353. * @throws IOException
  354. * @throws ClassNotFoundException
  355. */
  356. @SuppressWarnings(value = { "unchecked" })
  357. private void loadBinFiles() throws FileNotFoundException, IOException, ClassNotFoundException
  358. {
  359. FilenameFilter filter = new UniverseFilter("bin");
  360. File directory = new File("data");
  361. File[] files = directory.listFiles(filter);
  362. for (File file : files)
  363. {
  364. //Create necessary input streams
  365. FileInputStream fis = new FileInputStream(file); // Read from file
  366. GZIPInputStream gzis = new GZIPInputStream(fis); // Uncompress
  367. ObjectInputStream in = new ObjectInputStream(gzis); // Read objects
  368. // Read in an object. It should be a vector of scribbles
  369. TreeSet<Position> temp = (TreeSet<Position>) in.readObject();
  370. _log.info(temp.size() + " map vertices loaded from file " + file.getName());
  371. in.close(); // Close the stream.
  372. for (Position p : temp)
  373. {
  374. _coordList.add(new Coord(p._x, p._y, p._z));
  375. }
  376. }
  377. }
  378. public class UniverseDump implements Runnable
  379. {
  380. /* (non-Javadoc)
  381. * @see java.lang.Runnable#run()
  382. */
  383. public void run()
  384. {
  385. int size = _coordList.size();
  386. //_log.info("Univere Map has " + _map.size() + " nodes.");
  387. if (size > 100000)
  388. {
  389. flush();
  390. }
  391. }
  392. }
  393. public void flush()
  394. {
  395. //_log.info("Size of dump: "+coordList.size());
  396. List<Coord> oldMap = _coordList;
  397. _coordList = new LinkedList<Coord>();
  398. int size = oldMap.size();
  399. dump(oldMap, true);
  400. _log.info("Universe Map : Dumped " + size + " vertices.");
  401. }
  402. public int size()
  403. {
  404. int size = 0;
  405. if (_coordList != null)
  406. size = _coordList.size();
  407. return size;
  408. }
  409. public void dump(List<Coord> _map, boolean b)
  410. {
  411. FileOutputStream fos = null;
  412. DataOutputStream data = null;
  413. try
  414. {
  415. String pad = "";
  416. if (b)
  417. pad = "" + System.currentTimeMillis();
  418. fos = new FileOutputStream("data/universe" + pad + ".fin"); // Save to file
  419. data = new DataOutputStream(fos);
  420. int count = _map.size();
  421. //_log.info("Size of dump: "+count);
  422. data.writeInt(count);
  423. for (Coord p : _map)
  424. {
  425. if (p != null)
  426. {
  427. data.writeInt(p._x);
  428. data.writeInt(p._y);
  429. data.writeInt(p._z);
  430. }
  431. }
  432. _log.info("Universe Map saved to: " + "data/universe" + pad + ".fin");
  433. }
  434. catch (Exception e)
  435. {
  436. e.printStackTrace();
  437. }
  438. finally
  439. {
  440. try
  441. {
  442. data.close();
  443. }
  444. catch (Exception e)
  445. {
  446. }
  447. try
  448. {
  449. fos.close();
  450. }
  451. catch (Exception e)
  452. {
  453. }
  454. }
  455. }
  456. // prepare for shutdown
  457. public void implode(boolean b)
  458. {
  459. createMap();
  460. dump(_coordList, b);
  461. }
  462. @SuppressWarnings("synthetic-access")
  463. private static class SingletonHolder
  464. {
  465. protected static final Universe _instance = Config.ACTIVATE_POSITION_RECORDER ? new Universe() : null;
  466. }
  467. }