2
0

Universe.java 12 KB

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