Universe.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. //if (Config.USE_3D_MAP) insertInto3DMap(p);
  169. }
  170. public void registerObstacle(int x, int y, int z)
  171. {
  172. //Position p = new Position(x, y, z, -1);
  173. //_map.add(p);
  174. _coordList.add(new Coord(x, y, z));
  175. //if (Config.USE_3D_MAP) insertInto3DMap(p);
  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. try
  198. {
  199. BufferedReader r = new BufferedReader(new FileReader("data/universe.txt"));
  200. String line;
  201. while ((line = r.readLine()) != null)
  202. {
  203. StringTokenizer st = new StringTokenizer(line);
  204. String x1 = st.nextToken();
  205. String y1 = st.nextToken();
  206. String z1 = st.nextToken();
  207. // String f1 = st.nextToken();
  208. int x = Integer.parseInt(x1);
  209. int y = Integer.parseInt(y1);
  210. int z = Integer.parseInt(z1);
  211. // int f = Integer.parseInt(f1);
  212. _coordList.add(new Coord(x, y, z));
  213. }
  214. r.close();
  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. }
  222. public void createMap()
  223. {
  224. int zoom = 100;
  225. int w = (MAX_X - MIN_X) / zoom;
  226. int h = (MAX_Y - MIN_Y) / zoom;
  227. BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_GRAY);
  228. Graphics2D gr = bi.createGraphics();
  229. int min_z = 0, max_z = 0;
  230. for (Coord pos : _coordList)
  231. {
  232. if (pos == null) continue;
  233. if (pos._z < min_z) min_z = pos._z;
  234. if (pos._z > max_z) max_z = pos._z;
  235. }
  236. for (Coord pos : _coordList)
  237. {
  238. if (pos == null) continue;
  239. int x = (pos._x - MIN_X) / zoom;
  240. int y = (pos._y - MIN_Y) / zoom;
  241. int color = (int) (((long) pos._z - MIN_Z) * 0xFFFFFF / (MAX_Z - MIN_Z));
  242. gr.setColor(new Color(color));
  243. gr.drawLine(x, y, x, y);
  244. }
  245. try
  246. {
  247. ImageIO.write(bi, "png", new File("universe.png"));
  248. }
  249. catch (Exception e)
  250. {
  251. _log.warning("cannot create universe.png: " + e);
  252. }
  253. }
  254. public class UniverseFilter implements FilenameFilter
  255. {
  256. String _ext = "";
  257. public UniverseFilter(String pExt)
  258. {
  259. _ext = pExt;
  260. }
  261. /* (non-Javadoc)
  262. * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
  263. */
  264. public boolean accept(@SuppressWarnings("unused")
  265. File arg0, String name)
  266. {
  267. return name.startsWith("universe") && name.endsWith("." + _ext);
  268. }
  269. }
  270. public void load()
  271. {
  272. int total = 0;
  273. if (_coordList == null)
  274. {
  275. _coordList = new LinkedList<Coord>();
  276. }
  277. try
  278. {
  279. loadBinFiles();
  280. loadHexFiles();
  281. loadFinFiles();
  282. _log.info(_coordList.size() + " map vertices loaded in total.");
  283. }
  284. catch (Exception e)
  285. {
  286. e.printStackTrace();
  287. }
  288. _log.info("Total: " + total);
  289. }
  290. /**
  291. * @throws FileNotFoundException
  292. * @throws IOException
  293. */
  294. private void loadFinFiles() throws FileNotFoundException, IOException
  295. {
  296. FilenameFilter filter = new UniverseFilter("fin");
  297. File directory = new File("data");
  298. File[] files = directory.listFiles(filter);
  299. for (File file : files)
  300. {
  301. FileInputStream fos = new FileInputStream(file); // Save to file
  302. DataInputStream data = new DataInputStream(fos);
  303. int count = data.readInt();
  304. List<Coord> newMap = new LinkedList<Coord>();
  305. for (int i = 0; i < count; i++)
  306. {
  307. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  308. }
  309. data.close(); // Close the stream.
  310. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  311. _coordList.addAll(newMap);
  312. }
  313. }
  314. /**
  315. * @throws FileNotFoundException
  316. * @throws IOException
  317. */
  318. private void loadHexFiles() throws FileNotFoundException, IOException
  319. {
  320. FilenameFilter filter = new UniverseFilter("hex");
  321. File directory = new File("data");
  322. File[] files = directory.listFiles(filter);
  323. for (File file : files)
  324. {
  325. FileInputStream fos = new FileInputStream(file); // Save to file
  326. GZIPInputStream gzos = new GZIPInputStream(fos);
  327. DataInputStream data = new DataInputStream(gzos);
  328. int count = data.readInt();
  329. List<Coord> newMap = new LinkedList<Coord>();
  330. for (int i = 0; i < count; i++)
  331. {
  332. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  333. data.readInt();
  334. }
  335. data.close(); // Close the stream.
  336. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  337. _coordList.addAll(newMap);
  338. }
  339. }
  340. /**
  341. * @throws FileNotFoundException
  342. * @throws IOException
  343. * @throws ClassNotFoundException
  344. */
  345. @SuppressWarnings(value = {"unchecked"})
  346. private void loadBinFiles() throws FileNotFoundException, IOException, ClassNotFoundException
  347. {
  348. FilenameFilter filter = new UniverseFilter("bin");
  349. File directory = new File("data");
  350. File[] files = directory.listFiles(filter);
  351. for (File file : files)
  352. {
  353. //Create necessary input streams
  354. FileInputStream fis = new FileInputStream(file); // Read from file
  355. GZIPInputStream gzis = new GZIPInputStream(fis); // Uncompress
  356. ObjectInputStream in = new ObjectInputStream(gzis); // Read objects
  357. // Read in an object. It should be a vector of scribbles
  358. TreeSet<Position> temp = (TreeSet<Position>) in.readObject();
  359. _log.info(temp.size() + " map vertices loaded from file " + file.getName());
  360. in.close(); // Close the stream.
  361. for (Position p : temp)
  362. {
  363. _coordList.add(new Coord(p._x, p._y, p._z));
  364. }
  365. }
  366. }
  367. public class UniverseDump implements Runnable
  368. {
  369. /* (non-Javadoc)
  370. * @see java.lang.Runnable#run()
  371. */
  372. public void run()
  373. {
  374. int size = _coordList.size();
  375. //_log.info("Univere Map has " + _map.size() + " nodes.");
  376. if (size > 100000)
  377. {
  378. flush();
  379. }
  380. }
  381. }
  382. public void flush()
  383. {
  384. //_log.info("Size of dump: "+coordList.size());
  385. List<Coord> oldMap = _coordList;
  386. _coordList = new LinkedList<Coord>();
  387. int size = oldMap.size();
  388. dump(oldMap, true);
  389. _log.info("Universe Map : Dumped " + size + " vertices.");
  390. }
  391. public int size()
  392. {
  393. int size = 0;
  394. if (_coordList != null) size = _coordList.size();
  395. return size;
  396. }
  397. public void dump(List<Coord> _map, boolean b)
  398. {
  399. try
  400. {
  401. String pad = "";
  402. if (b) pad = "" + System.currentTimeMillis();
  403. FileOutputStream fos = new FileOutputStream("data/universe" + pad + ".fin"); // Save to file
  404. DataOutputStream data = new DataOutputStream(fos);
  405. int count = _map.size();
  406. //_log.info("Size of dump: "+count);
  407. data.writeInt(count);
  408. if (_map != null)
  409. {
  410. for (Coord p : _map)
  411. {
  412. if (p != null)
  413. {
  414. data.writeInt(p._x);
  415. data.writeInt(p._y);
  416. data.writeInt(p._z);
  417. }
  418. }
  419. }
  420. data.flush();
  421. data.close();
  422. _log.info("Universe Map saved to: " + "data/universe" + pad + ".fin");
  423. }
  424. catch (Exception e)
  425. {
  426. e.printStackTrace();
  427. }
  428. }
  429. // prepare for shutdown
  430. public void implode(boolean b)
  431. {
  432. createMap();
  433. dump(_coordList, b);
  434. }
  435. }