Universe.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; either version 2, or (at your option)
  4. * any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, write to the Free Software
  13. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  14. * 02111-1307, USA.
  15. *
  16. * http://www.gnu.org/copyleft/gpl.html
  17. */
  18. package net.sf.l2j.gameserver;
  19. import java.awt.Color;
  20. import java.awt.Graphics2D;
  21. import java.awt.image.BufferedImage;
  22. import java.io.BufferedReader;
  23. import java.io.DataInputStream;
  24. import java.io.DataOutputStream;
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileNotFoundException;
  28. import java.io.FileOutputStream;
  29. import java.io.FileReader;
  30. import java.io.FilenameFilter;
  31. import java.io.IOException;
  32. import java.io.ObjectInputStream;
  33. import java.util.HashSet;
  34. import java.util.LinkedList;
  35. import java.util.List;
  36. import java.util.StringTokenizer;
  37. import java.util.TreeSet;
  38. import java.util.logging.Logger;
  39. import java.util.zip.GZIPInputStream;
  40. import javax.imageio.ImageIO;
  41. import net.sf.l2j.Config;
  42. import net.sf.l2j.gameserver.model.L2CharPosition;
  43. public class Universe implements java.io.Serializable
  44. {
  45. /**
  46. * Comment for <code>serialVersionUID</code>
  47. */
  48. private static final long serialVersionUID = -2040223695811104704L;
  49. public static final int MIN_X = -127900;
  50. public static final int MAX_X = 194327;
  51. public static final int MIN_Y = -30000;
  52. public static final int MAX_Y = 259536;
  53. public static final int MIN_Z = -17000;
  54. public static final int MAX_Z = 17000;
  55. public static final int MIN_X_GRID = 60;
  56. public static final int MIN_Y_GRID = 60;
  57. public static final int MIN_Z_GRID = 60;
  58. public static final int MIN_GRID = 360;
  59. private static Universe _instance;
  60. protected static final Logger _log = Logger.getLogger(Universe.class.getName());
  61. protected List<Coord> _coordList;
  62. private HashSet<Integer> _logPlayers;
  63. private boolean _logAll = true;
  64. public static void main(String[] args)
  65. {
  66. Universe u = new Universe();
  67. u.load();
  68. //u.removeDoubles();
  69. u.implode(false);
  70. }
  71. private class Position implements Comparable<Position>, java.io.Serializable
  72. {
  73. /**
  74. * Comment for <code>serialVersionUID</code>
  75. */
  76. private static final long serialVersionUID = -8798746764450022287L;
  77. protected int _x;
  78. protected int _flag;
  79. protected int _y;
  80. protected int _z;
  81. public Position(int x, int y, int z, int flag)
  82. {
  83. _x = x;
  84. _y = y;
  85. _z = z;
  86. _flag = flag;
  87. }
  88. public Position(L2CharPosition pos)
  89. {
  90. _x = pos.x;
  91. _y = pos.y;
  92. _z = pos.z;
  93. _flag = 0;
  94. }
  95. @Deprecated
  96. public L2CharPosition l2CP()
  97. {
  98. return new L2CharPosition(_x, _y, _z, 0);
  99. }
  100. public int compareTo(Position obj)
  101. {
  102. int res = Integer.valueOf(_x).compareTo(obj._x);
  103. if (res != 0) return res;
  104. res = Integer.valueOf(_y).compareTo(obj._y);
  105. if (res != 0) 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. private class Coord implements Comparable<Position>, java.io.Serializable
  116. {
  117. /**
  118. * Comment for <code>serialVersionUID</code>
  119. */
  120. private static final long serialVersionUID = -558060332886829552L;
  121. protected int _x;
  122. protected int _y;
  123. protected int _z;
  124. public Coord(int x, int y, int z)
  125. {
  126. _x = x;
  127. _y = y;
  128. _z = z;
  129. }
  130. public Coord(L2CharPosition pos)
  131. {
  132. _x = pos.x;
  133. _y = pos.y;
  134. _z = pos.z;
  135. }
  136. public int compareTo(Position obj)
  137. {
  138. int res = Integer.valueOf(_x).compareTo(obj._x);
  139. if (res != 0) return res;
  140. res = Integer.valueOf(_y).compareTo(obj._y);
  141. if (res != 0) 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. if (_instance == null && Config.ACTIVATE_POSITION_RECORDER)
  154. {
  155. _instance = new Universe();
  156. }
  157. return _instance;
  158. }
  159. private Universe()
  160. {
  161. _coordList = new LinkedList<Coord>();
  162. _logPlayers = new HashSet<Integer>();
  163. ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new UniverseDump(), 30000, 30000);
  164. }
  165. public void registerHeight(int x, int y, int z)
  166. {
  167. // don't overwrite obstacle entries
  168. //Position p = new Position(x, y, z, 0);
  169. //_map.add(p);
  170. _coordList.add(new Coord(x, y, z));
  171. //if (Config.USE_3D_MAP) insertInto3DMap(p);
  172. }
  173. public void registerObstacle(int x, int y, int z)
  174. {
  175. //Position p = new Position(x, y, z, -1);
  176. //_map.add(p);
  177. _coordList.add(new Coord(x, y, z));
  178. //if (Config.USE_3D_MAP) insertInto3DMap(p);
  179. }
  180. public boolean shouldLog(Integer id)
  181. {
  182. return (_logPlayers.contains(id) || _logAll);
  183. }
  184. public void setLogAll(boolean flag)
  185. {
  186. _logAll = flag;
  187. }
  188. public void addLogPlayer(Integer id)
  189. {
  190. _logPlayers.add(id);
  191. _logAll = false;
  192. }
  193. public void removeLogPlayer(Integer id)
  194. {
  195. _logPlayers.remove(id);
  196. }
  197. public void loadAscii()
  198. {
  199. int initialSize = _coordList.size();
  200. try
  201. {
  202. BufferedReader r = new BufferedReader(new FileReader("data/universe.txt"));
  203. String line;
  204. while ((line = r.readLine()) != null)
  205. {
  206. StringTokenizer st = new StringTokenizer(line);
  207. String x1 = st.nextToken();
  208. String y1 = st.nextToken();
  209. String z1 = st.nextToken();
  210. // String f1 = st.nextToken();
  211. int x = Integer.parseInt(x1);
  212. int y = Integer.parseInt(y1);
  213. int z = Integer.parseInt(z1);
  214. // int f = Integer.parseInt(f1);
  215. _coordList.add(new Coord(x, y, z));
  216. }
  217. r.close();
  218. _log.info((_coordList.size() - initialSize) + " additional nodes loaded from text file.");
  219. }
  220. catch (Exception e)
  221. {
  222. _log.info("could not read text file universe.txt");
  223. }
  224. }
  225. public void createMap()
  226. {
  227. int zoom = 100;
  228. int w = (MAX_X - MIN_X) / zoom;
  229. int h = (MAX_Y - MIN_Y) / zoom;
  230. BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_GRAY);
  231. Graphics2D gr = bi.createGraphics();
  232. int min_z = 0, max_z = 0;
  233. for (Coord pos : _coordList)
  234. {
  235. if (pos == null) continue;
  236. if (pos._z < min_z) min_z = pos._z;
  237. if (pos._z > max_z) max_z = pos._z;
  238. }
  239. for (Coord pos : _coordList)
  240. {
  241. if (pos == null) continue;
  242. int x = (pos._x - MIN_X) / zoom;
  243. int y = (pos._y - MIN_Y) / zoom;
  244. int color = (int) (((long) pos._z - MIN_Z) * 0xFFFFFF / (MAX_Z - MIN_Z));
  245. gr.setColor(new Color(color));
  246. gr.drawLine(x, y, x, y);
  247. }
  248. try
  249. {
  250. ImageIO.write(bi, "png", new File("universe.png"));
  251. }
  252. catch (Exception e)
  253. {
  254. _log.warning("cannot create universe.png: " + e);
  255. }
  256. }
  257. public class UniverseFilter implements FilenameFilter
  258. {
  259. String _ext = "";
  260. public UniverseFilter(String pExt)
  261. {
  262. _ext = pExt;
  263. }
  264. /* (non-Javadoc)
  265. * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
  266. */
  267. public boolean accept(@SuppressWarnings("unused")
  268. File arg0, String name)
  269. {
  270. return name.startsWith("universe") && name.endsWith("." + _ext);
  271. }
  272. }
  273. public void load()
  274. {
  275. int total = 0;
  276. if (_coordList == null)
  277. {
  278. _coordList = new LinkedList<Coord>();
  279. }
  280. try
  281. {
  282. loadBinFiles();
  283. loadHexFiles();
  284. loadFinFiles();
  285. _log.info(_coordList.size() + " map vertices loaded in total.");
  286. }
  287. catch (Exception e)
  288. {
  289. e.printStackTrace();
  290. }
  291. System.out.println("Total: " + total);
  292. }
  293. /**
  294. * @throws FileNotFoundException
  295. * @throws IOException
  296. */
  297. private void loadFinFiles() throws FileNotFoundException, IOException
  298. {
  299. FilenameFilter filter = new UniverseFilter("fin");
  300. File directory = new File("data");
  301. File[] files = directory.listFiles(filter);
  302. for (File file : files)
  303. {
  304. FileInputStream fos = new FileInputStream(file); // Save to file
  305. DataInputStream data = new DataInputStream(fos);
  306. int count = data.readInt();
  307. List<Coord> newMap = new LinkedList<Coord>();
  308. for (int i = 0; i < count; i++)
  309. {
  310. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  311. }
  312. data.close(); // Close the stream.
  313. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  314. _coordList.addAll(newMap);
  315. }
  316. }
  317. /**
  318. * @throws FileNotFoundException
  319. * @throws IOException
  320. */
  321. private void loadHexFiles() throws FileNotFoundException, IOException
  322. {
  323. FilenameFilter filter = new UniverseFilter("hex");
  324. File directory = new File("data");
  325. File[] files = directory.listFiles(filter);
  326. for (File file : files)
  327. {
  328. FileInputStream fos = new FileInputStream(file); // Save to file
  329. GZIPInputStream gzos = new GZIPInputStream(fos);
  330. DataInputStream data = new DataInputStream(gzos);
  331. int count = data.readInt();
  332. List<Coord> newMap = new LinkedList<Coord>();
  333. for (int i = 0; i < count; i++)
  334. {
  335. newMap.add(new Coord(data.readInt(), data.readInt(), data.readInt()));
  336. data.readInt();
  337. }
  338. data.close(); // Close the stream.
  339. _log.info(newMap.size() + " map vertices loaded from file " + file.getName());
  340. _coordList.addAll(newMap);
  341. }
  342. }
  343. /**
  344. * @throws FileNotFoundException
  345. * @throws IOException
  346. * @throws ClassNotFoundException
  347. */
  348. @SuppressWarnings(value = {"unchecked"})
  349. private void loadBinFiles() throws FileNotFoundException, IOException, ClassNotFoundException
  350. {
  351. FilenameFilter filter = new UniverseFilter("bin");
  352. File directory = new File("data");
  353. File[] files = directory.listFiles(filter);
  354. for (File file : files)
  355. {
  356. //Create necessary input streams
  357. FileInputStream fis = new FileInputStream(file); // Read from file
  358. GZIPInputStream gzis = new GZIPInputStream(fis); // Uncompress
  359. ObjectInputStream in = new ObjectInputStream(gzis); // Read objects
  360. // Read in an object. It should be a vector of scribbles
  361. TreeSet<Position> temp = (TreeSet<Position>) in.readObject();
  362. _log.info(temp.size() + " map vertices loaded from file " + file.getName());
  363. in.close(); // Close the stream.
  364. for (Position p : temp)
  365. {
  366. _coordList.add(new Coord(p._x, p._y, p._z));
  367. }
  368. }
  369. }
  370. public class UniverseDump implements Runnable
  371. {
  372. /* (non-Javadoc)
  373. * @see java.lang.Runnable#run()
  374. */
  375. public void run()
  376. {
  377. int size = _coordList.size();
  378. //System.out.println("Univere Map has " + _map.size() + " nodes.");
  379. if (size > 100000)
  380. {
  381. flush();
  382. }
  383. }
  384. }
  385. public void flush()
  386. {
  387. //System.out.println("Size of dump: "+coordList.size());
  388. List<Coord> oldMap = _coordList;
  389. _coordList = new LinkedList<Coord>();
  390. int size = oldMap.size();
  391. dump(oldMap, true);
  392. _log.info("Universe Map : Dumped " + size + " vertices.");
  393. }
  394. public int size()
  395. {
  396. int size = 0;
  397. if (_coordList != null) size = _coordList.size();
  398. return size;
  399. }
  400. public void dump(List<Coord> _map, boolean b)
  401. {
  402. try
  403. {
  404. String pad = "";
  405. if (b) pad = "" + System.currentTimeMillis();
  406. FileOutputStream fos = new FileOutputStream("data/universe" + pad + ".fin"); // Save to file
  407. DataOutputStream data = new DataOutputStream(fos);
  408. int count = _map.size();
  409. //System.out.println("Size of dump: "+count);
  410. data.writeInt(count);
  411. if (_map != null)
  412. {
  413. for (Coord p : _map)
  414. {
  415. if (p != null)
  416. {
  417. data.writeInt(p._x);
  418. data.writeInt(p._y);
  419. data.writeInt(p._z);
  420. }
  421. }
  422. }
  423. data.flush();
  424. data.close();
  425. _log.info("Universe Map saved to: " + "data/universe" + pad + ".fin");
  426. }
  427. catch (Exception e)
  428. {
  429. e.printStackTrace();
  430. }
  431. }
  432. // prepare for shutdown
  433. public void implode(boolean b)
  434. {
  435. createMap();
  436. dump(_coordList, b);
  437. }
  438. }