Universe.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 net.sf.l2j.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 net.sf.l2j.Config;
  39. import net.sf.l2j.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. private static Universe _instance;
  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) return res;
  101. res = Integer.valueOf(_y).compareTo(obj._y);
  102. if (res != 0) return res;
  103. res = Integer.valueOf(_z).compareTo(obj._z);
  104. return res;
  105. }
  106. @Override
  107. public String toString()
  108. {
  109. return String.valueOf(_x) + " " + _y + " " + _z + " " + _flag;
  110. }
  111. }
  112. private class Coord implements Comparable<Position>, java.io.Serializable
  113. {
  114. /**
  115. * Comment for <code>serialVersionUID</code>
  116. */
  117. private static final long serialVersionUID = -558060332886829552L;
  118. protected int _x;
  119. protected int _y;
  120. protected int _z;
  121. public Coord(int x, int y, int z)
  122. {
  123. _x = x;
  124. _y = y;
  125. _z = z;
  126. }
  127. public Coord(L2CharPosition pos)
  128. {
  129. _x = pos.x;
  130. _y = pos.y;
  131. _z = pos.z;
  132. }
  133. public int compareTo(Position obj)
  134. {
  135. int res = Integer.valueOf(_x).compareTo(obj._x);
  136. if (res != 0) return res;
  137. res = Integer.valueOf(_y).compareTo(obj._y);
  138. if (res != 0) return res;
  139. res = Integer.valueOf(_z).compareTo(obj._z);
  140. return res;
  141. }
  142. @Override
  143. public String toString()
  144. {
  145. return String.valueOf(_x) + " " + _y + " " + _z;
  146. }
  147. }
  148. public static Universe getInstance()
  149. {
  150. if (_instance == null && Config.ACTIVATE_POSITION_RECORDER)
  151. {
  152. _instance = new Universe();
  153. }
  154. return _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. try
  196. {
  197. BufferedReader 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. r.close();
  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. }
  220. public void createMap()
  221. {
  222. int zoom = 100;
  223. int w = (MAX_X - MIN_X) / zoom;
  224. int h = (MAX_Y - MIN_Y) / zoom;
  225. BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_GRAY);
  226. Graphics2D gr = bi.createGraphics();
  227. int min_z = 0, max_z = 0;
  228. for (Coord pos : _coordList)
  229. {
  230. if (pos == null) continue;
  231. if (pos._z < min_z) min_z = pos._z;
  232. if (pos._z > max_z) max_z = pos._z;
  233. }
  234. for (Coord pos : _coordList)
  235. {
  236. if (pos == null) continue;
  237. int x = (pos._x - MIN_X) / zoom;
  238. int y = (pos._y - MIN_Y) / zoom;
  239. int color = (int) (((long) pos._z - MIN_Z) * 0xFFFFFF / (MAX_Z - MIN_Z));
  240. gr.setColor(new Color(color));
  241. gr.drawLine(x, y, x, y);
  242. }
  243. try
  244. {
  245. ImageIO.write(bi, "png", new File("universe.png"));
  246. }
  247. catch (Exception e)
  248. {
  249. _log.warning("cannot create universe.png: " + e);
  250. }
  251. }
  252. public class UniverseFilter implements FilenameFilter
  253. {
  254. String _ext = "";
  255. public UniverseFilter(String pExt)
  256. {
  257. _ext = pExt;
  258. }
  259. /* (non-Javadoc)
  260. * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
  261. */
  262. public boolean accept(File arg0, String name)
  263. {
  264. return name.startsWith("universe") && name.endsWith("." + _ext);
  265. }
  266. }
  267. public void load()
  268. {
  269. int total = 0;
  270. if (_coordList == null)
  271. {
  272. _coordList = new LinkedList<Coord>();
  273. }
  274. try
  275. {
  276. loadBinFiles();
  277. loadHexFiles();
  278. loadFinFiles();
  279. _log.info(_coordList.size() + " map vertices loaded in total.");
  280. }
  281. catch (Exception e)
  282. {
  283. e.printStackTrace();
  284. }
  285. _log.info("Total: " + total);
  286. }
  287. /**
  288. * @throws FileNotFoundException
  289. * @throws IOException
  290. */
  291. private void loadFinFiles() throws FileNotFoundException, IOException
  292. {
  293. FilenameFilter filter = new UniverseFilter("fin");
  294. File directory = new File("data");
  295. File[] files = directory.listFiles(filter);
  296. for (File file : files)
  297. {
  298. FileInputStream fos = new FileInputStream(file); // Save to file
  299. DataInputStream data = new DataInputStream(fos);
  300. int count = data.readInt();
  301. List<Coord> newMap = new LinkedList<Coord>();
  302. for (int i = 0; i < count; i++)
  303. {
  304. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  305. }
  306. data.close(); // Close the stream.
  307. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  308. _coordList.addAll(newMap);
  309. }
  310. }
  311. /**
  312. * @throws FileNotFoundException
  313. * @throws IOException
  314. */
  315. private void loadHexFiles() throws FileNotFoundException, IOException
  316. {
  317. FilenameFilter filter = new UniverseFilter("hex");
  318. File directory = new File("data");
  319. File[] files = directory.listFiles(filter);
  320. for (File file : files)
  321. {
  322. FileInputStream fos = new FileInputStream(file); // Save to file
  323. GZIPInputStream gzos = new GZIPInputStream(fos);
  324. DataInputStream data = new DataInputStream(gzos);
  325. int count = data.readInt();
  326. List<Coord> newMap = new LinkedList<Coord>();
  327. for (int i = 0; i < count; i++)
  328. {
  329. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  330. data.readInt();
  331. }
  332. data.close(); // Close the stream.
  333. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  334. _coordList.addAll(newMap);
  335. }
  336. }
  337. /**
  338. * @throws FileNotFoundException
  339. * @throws IOException
  340. * @throws ClassNotFoundException
  341. */
  342. @SuppressWarnings(value = {"unchecked"})
  343. private void loadBinFiles() throws FileNotFoundException, IOException, ClassNotFoundException
  344. {
  345. FilenameFilter filter = new UniverseFilter("bin");
  346. File directory = new File("data");
  347. File[] files = directory.listFiles(filter);
  348. for (File file : files)
  349. {
  350. //Create necessary input streams
  351. FileInputStream fis = new FileInputStream(file); // Read from file
  352. GZIPInputStream gzis = new GZIPInputStream(fis); // Uncompress
  353. ObjectInputStream in = new ObjectInputStream(gzis); // Read objects
  354. // Read in an object. It should be a vector of scribbles
  355. TreeSet<Position> temp = (TreeSet<Position>) in.readObject();
  356. _log.info(temp.size() + " map vertices loaded from file " + file.getName());
  357. in.close(); // Close the stream.
  358. for (Position p : temp)
  359. {
  360. _coordList.add(new Coord(p._x, p._y, p._z));
  361. }
  362. }
  363. }
  364. public class UniverseDump implements Runnable
  365. {
  366. /* (non-Javadoc)
  367. * @see java.lang.Runnable#run()
  368. */
  369. public void run()
  370. {
  371. int size = _coordList.size();
  372. //_log.info("Univere Map has " + _map.size() + " nodes.");
  373. if (size > 100000)
  374. {
  375. flush();
  376. }
  377. }
  378. }
  379. public void flush()
  380. {
  381. //_log.info("Size of dump: "+coordList.size());
  382. List<Coord> oldMap = _coordList;
  383. _coordList = new LinkedList<Coord>();
  384. int size = oldMap.size();
  385. dump(oldMap, true);
  386. _log.info("Universe Map : Dumped " + size + " vertices.");
  387. }
  388. public int size()
  389. {
  390. int size = 0;
  391. if (_coordList != null) size = _coordList.size();
  392. return size;
  393. }
  394. public void dump(List<Coord> _map, boolean b)
  395. {
  396. try
  397. {
  398. String pad = "";
  399. if (b) pad = "" + System.currentTimeMillis();
  400. FileOutputStream fos = new FileOutputStream("data/universe" + pad + ".fin"); // Save to file
  401. DataOutputStream data = new DataOutputStream(fos);
  402. int count = _map.size();
  403. //_log.info("Size of dump: "+count);
  404. data.writeInt(count);
  405. for (Coord p : _map)
  406. {
  407. if (p != null)
  408. {
  409. data.writeInt(p._x);
  410. data.writeInt(p._y);
  411. data.writeInt(p._z);
  412. }
  413. }
  414. data.flush();
  415. data.close();
  416. _log.info("Universe Map saved to: " + "data/universe" + pad + ".fin");
  417. }
  418. catch (Exception e)
  419. {
  420. e.printStackTrace();
  421. }
  422. }
  423. // prepare for shutdown
  424. public void implode(boolean b)
  425. {
  426. createMap();
  427. dump(_coordList, b);
  428. }
  429. }