Universe.java 12 KB

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