LogBlockHeatmapper.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2015 iZc
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package de.nplusc.izc.logblockheatmapper;
  18. import ar.com.hjg.pngj.ImageInfo;
  19. import ar.com.hjg.pngj.ImageLineHelper;
  20. import ar.com.hjg.pngj.ImageLineInt;
  21. import ar.com.hjg.pngj.PngWriter;
  22. import java.awt.*;
  23. import java.awt.geom.Point2D;
  24. import java.awt.image.BufferedImage;
  25. import java.io.BufferedReader;
  26. import java.io.File;
  27. import java.io.FileOutputStream;
  28. import java.io.FileReader;
  29. import javax.imageio.ImageIO;
  30. /**
  31. *
  32. * @author iZc <nplusc.de>
  33. */
  34. public class LogBlockHeatmapper
  35. {
  36. //x y
  37. static int[ ][ ] dataField ;
  38. static int max=0;
  39. static int maxx = 0;
  40. static int maxy=0;
  41. static int targetBlockID=-1;
  42. static boolean removalsOnly=false;
  43. static boolean placemode = false;
  44. static BufferedImage gradient = new BufferedImage(1024,1,BufferedImage.TYPE_4BYTE_ABGR);
  45. /**
  46. * @param args the command line arguments
  47. * @throws java.lang.Exception
  48. */
  49. public static void main(String[] args)throws Exception
  50. {
  51. if(args.length<2)
  52. {
  53. throw new UnsupportedOperationException("not enough parameters");
  54. }
  55. if(args.length>2)
  56. {
  57. if(args[2].equals("place"))
  58. {
  59. placemode=true;
  60. }
  61. else
  62. {
  63. targetBlockID = Integer.valueOf(args[2]);
  64. if(args.length>3)
  65. {
  66. removalsOnly = Boolean.valueOf(args[3]);
  67. }
  68. }
  69. }
  70. int size = Integer.valueOf(args[1]);
  71. Point2D start = new Point2D.Float(0, 0);
  72. Point2D end = new Point2D.Float(1023, 0);
  73. Color[] colors = {Color.blue, Color.green, Color.YELLOW, Color.RED};
  74. float[] steps = {0.0f,0.2f,0.6f,1.0f};
  75. Paint p = new LinearGradientPaint(start,end,steps,colors);
  76. Graphics2D lw = (Graphics2D) gradient.getGraphics();
  77. lw.setPaint(p);
  78. lw.fillRect(0, 0, 1023, 1);
  79. ImageIO.write(gradient,"png",new File("Debug.png"));
  80. System.out.println("Initializing Datastructures, this may take a while....");
  81. dataField = new int[size][size];
  82. BufferedReader br = new BufferedReader(new FileReader(args[0]));
  83. br.lines().forEach((xx)->
  84. {
  85. //4 als 5 und nen bisschen arithmetik gibt seitwärts-schnittansicht
  86. //id_0 oldblox_1 newblox_2 newdv_3 X_4 height_5 y_6
  87. //10 5 0 0 -1373 27 4008
  88. xx = xx.replace("\"", "");
  89. xx = xx.replace(",", "\t");
  90. String[] xes = xx.split("\\t");
  91. int l = Integer.parseInt(xes[0]);
  92. boolean register=false;
  93. int f = 0;//Integer.parseInt(xes[1]);
  94. int t = 1;//Integer.parseInt(xes[2]);
  95. if((targetBlockID<0||targetBlockID==f||targetBlockID==t))
  96. {
  97. register=true;
  98. }
  99. if(removalsOnly&&t==targetBlockID)
  100. {
  101. register=false;
  102. }
  103. int x= Integer.parseInt(xes[4])+size/2,y= Integer.parseInt(xes[6])+size/2;
  104. //-y=up
  105. if(x<size&&x>=0&&y<size&&y>=0&&register)
  106. {
  107. //minecraftlogik.....
  108. dataField[y][x]+=1;
  109. if(dataField[y][x]>max)
  110. {
  111. max=dataField[y][x];
  112. maxx= Integer.parseInt(xes[4]);
  113. maxy = Integer.parseInt(xes[6]);
  114. }
  115. }
  116. if(l%10000==0)
  117. {
  118. System.out.print(l+"changes so far\r");
  119. }
  120. });
  121. System.out.print("Database crunched to 100%\n maximum changes on coordinates were:" +max + "at x:"+maxx + " y:"+maxy +"\n");
  122. //max=Math.min(max, 1000);
  123. //int steps=max/256;
  124. //BufferedWriter bw = new BufferedWriter(new FileWriter(new File("D:/emc2/arca_slash/database/crunched.pgm")));
  125. //bw.write("P6");
  126. //bw.newLine();
  127. //bw.write("20000 20000");
  128. //bw.newLine();
  129. //bw.write("255");
  130. //bw.newLine();
  131. //BufferedImage bi = new BufferedImage(size,size, BufferedImage.TYPE_4BYTE_ABGR);
  132. //SELECT id, replaced, `type`, `data`, x, y, z
  133. //FROM `lb-surv`
  134. //INTO OUTFILE 'D:/emc2/arca_slash/database/lbcrunchsurvival.csv';
  135. ImageInfo imi = new ImageInfo(dataField.length, dataField.length, 8, false); // 8 bits per channel, no alpha
  136. // open image for writing to a output stream
  137. PngWriter png = new PngWriter(new FileOutputStream("map.png"), imi);
  138. // add some optional metadata (chunks)
  139. png.getMetadata().setDpi(100.0);
  140. png.getMetadata().setTimeNow(0); // 0 seconds fron now = now
  141. //0-49 1 stufe=1 farbschritt 50 -1049: 10 stufen pro farbschritt: rest gleichmäßig aufgeteilt bis maximum
  142. for (int i = 0; i < dataField.length; i++)
  143. {
  144. ImageLineInt iline = new ImageLineInt(imi);
  145. int[] is = dataField[i];
  146. for (int j = 0; j < is.length; j++)
  147. {
  148. int color=Color.white.getRGB();
  149. int k = is[j];
  150. if(k==0)
  151. {
  152. color=Color.white.getRGB();
  153. //bw.write("255 255 255");
  154. //bw.write(255);bw.write(255);bw.write(255);
  155. }
  156. else
  157. {
  158. if(placemode)
  159. {
  160. k=k/10;
  161. //k/=steps;
  162. if(k<50)
  163. {
  164. color= gradient.getRGB(k*16, 0);
  165. }
  166. else
  167. {
  168. if(k<1050)
  169. {
  170. k-=50;
  171. color= gradient.getRGB(800+k/10, 0);
  172. }
  173. else
  174. {
  175. k-=1050;
  176. int stepWidth = (max-1050)/(123);
  177. if(stepWidth==0)
  178. stepWidth++;
  179. color= gradient.getRGB(Math.min(1023,900+k/stepWidth), 0);
  180. }
  181. int r=(color&0xFF0000)>>16;
  182. int g=(color&0xff00)>>8;
  183. int b=color&0xff;
  184. //bw.write(r);bw.write(g);bw.write(b);
  185. //writeBy(r+" "+g+" "+b);
  186. }
  187. }
  188. else if(max>1050)
  189. {
  190. //k/=steps;
  191. if(k<50)
  192. {
  193. color= gradient.getRGB(k*16, 0);
  194. }
  195. else
  196. {
  197. if(k<1050)
  198. {
  199. k-=50;
  200. color= gradient.getRGB(800+k/10, 0);
  201. }
  202. else
  203. {
  204. k-=1050;
  205. int stepWidth = (max-1050)/(123);
  206. if(stepWidth==0)
  207. stepWidth++;
  208. color= gradient.getRGB(Math.min(1023,900+k/stepWidth), 0);
  209. }
  210. int r=(color&0xFF0000)>>16;
  211. int g=(color&0xff00)>>8;
  212. int b=color&0xff;
  213. //bw.write(r);bw.write(g);bw.write(b);
  214. //writeBy(r+" "+g+" "+b);
  215. }
  216. }
  217. else
  218. {
  219. if(k<25)
  220. {
  221. color= gradient.getRGB(k*32, 0);
  222. }
  223. else
  224. {
  225. int stepWidth = (max-50)/(223);
  226. if(stepWidth==0)
  227. stepWidth++;
  228. color= gradient.getRGB(Math.min(1023,800+k/stepWidth+1), 0);
  229. }
  230. }
  231. }
  232. ImageLineHelper.setPixelRGB8(iline, j,color);
  233. //bi.setRGB(i,j,k<<16&k<<8&k);
  234. //if(j<19999)
  235. // {
  236. //bw.write(" ");
  237. // }
  238. }
  239. //if(i<19999)
  240. // bw.newLine();
  241. png.writeRow(iline);
  242. System.out.print(i+"lines rasterized\r");
  243. }
  244. //bw.close();
  245. png.end();
  246. //ImageIO.write(bi, "png",new File( "map.png"));
  247. }
  248. }
  249. //use mcserver;
  250. //mysql> SELECT id,replaced,type,data,x,y,z from `lb-surv` into outfile D:\\emc2\\arca_slash\\heatmap.tsv FIELDS TERMINATED BY `\t` LINES TERMINATED BY `\n`