Universe.java 12 KB

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