BotGame.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. package GPN;
  2. import org.apache.logging.log4j.Level;
  3. import org.apache.logging.log4j.LogManager;
  4. import org.apache.logging.log4j.Logger;
  5. import org.apache.logging.log4j.core.LoggerContext;
  6. import org.apache.logging.log4j.core.config.LoggerConfig;
  7. import javax.swing.*;
  8. import java.io.*;
  9. import java.net.InetAddress;
  10. import java.net.InetSocketAddress;
  11. import java.net.Socket;
  12. import java.net.SocketAddress;
  13. import java.util.LinkedList;
  14. import java.util.Queue;
  15. import java.util.concurrent.ConcurrentLinkedQueue;
  16. public class BotGame {
  17. private static boolean respawn = true;
  18. private boolean kill = false;
  19. private boolean switchItharder = false;
  20. private Queue<String> returnMsgs = new ConcurrentLinkedQueue<>();
  21. Object syncer = new Object();
  22. static Logger l = LogManager.getLogger();
  23. private int[] []board = new int[1][1];
  24. int[][] boards;
  25. int[][] boardFF;
  26. int ticksSinceStart = 0;
  27. int[][] tickets = null;
  28. boolean danger = false;
  29. private int cx=0,cy=0,cw=1,ch=1;
  30. private String playerid;
  31. int playercount = 0;
  32. boolean[][] headBuffer;
  33. int lastDir = 0;
  34. String debugdir = " ";
  35. boolean gaming = false;
  36. boolean switcheroo=false;
  37. public static void main(String[] args) {
  38. setupLogging(true,2);
  39. l.info("shizzlemork");
  40. while(true)
  41. {
  42. try{
  43. l.info("LOOP, spawning new bot");
  44. new BotGame().game();
  45. }
  46. catch (Error e){} //H4x
  47. catch (Exception e){} //H4x
  48. }
  49. }
  50. int lastcase=-1;
  51. private int boardFF(int x, int y, int id)
  52. {
  53. int cni = y-1;
  54. if(cni==-1)
  55. cni=ch-1;
  56. int csi=(y+1)%ch;
  57. int cwi = x-1;
  58. if(cwi==-1)
  59. cwi=cw-1;
  60. int cei=(x+1)%cw;
  61. return boardFF(x,y,id,0);
  62. }
  63. Queue<int[]> FFQueue = new LinkedList<>();
  64. private int boardFF(int x, int y,int id,int g)
  65. {
  66. FFQueue.add(new int[]{x,y,g});
  67. return boardFF(id);
  68. }
  69. int heads_touched = 0;
  70. private int boardFF(int id)
  71. {
  72. int rv_r=0;
  73. while(!FFQueue.isEmpty())
  74. {
  75. int rv=0;
  76. int[] ffq = FFQueue.remove();
  77. int x=ffq[0];
  78. int y=ffq[1];
  79. int g=ffq[2];
  80. if(boardFF[y][x]!=0)
  81. {
  82. if(boardFF[y][x]>0&&boardFF[y][x]!=id&&g==0)
  83. {
  84. l.info("1337#d");
  85. rv_r += -1337;
  86. continue;
  87. }
  88. rv_r+=0;
  89. continue;
  90. }
  91. rv = 2 ;
  92. int cni = y-1;
  93. if(cni==-1)
  94. cni=ch-1;
  95. int csi=(y+1)%ch;
  96. int cwi = x-1;
  97. if(cwi==-1)
  98. cwi=cw-1;
  99. int cei=(x+1)%cw;
  100. if(headBuffer[y][cwi]||headBuffer[y][cei]||headBuffer[csi][x]||headBuffer[cni][x])
  101. {
  102. rv_r+= g==0?1:0;
  103. heads_touched++;
  104. continue;
  105. }
  106. boardFF[y][x]=id;
  107. if(g<15) {
  108. FFQueue.add(new int[]{x, cni, g + 1});
  109. FFQueue.add(new int[]{x, csi, g + 1});
  110. FFQueue.add(new int[]{cwi, y, g + 1});
  111. FFQueue.add(new int[]{cei, y, g + 1});
  112. }
  113. rv_r+=rv;
  114. }
  115. return rv_r;
  116. }
  117. static void setupLogging(boolean verbose,int verbosityLevel)
  118. {
  119. System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager"); //HACK to catch java.util.logging loggers
  120. l=LogManager.getLogger();
  121. LoggerContext cx = (LoggerContext) LogManager.getContext(false);
  122. org.apache.logging.log4j.core.config.Configuration config = cx.getConfiguration();
  123. LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
  124. LoggerConfig vlcj = config.getLoggerConfig("uk.co.caprica.vlcj");
  125. LoggerConfig externalloggerConfig = config.getLoggerConfig("External");
  126. LoggerConfig trueVFSlloggerConfig = config.getLoggerConfig("net.java");
  127. if (verbose)
  128. {
  129. loggerConfig.setLevel(Level.TRACE);
  130. if(verbosityLevel>1)
  131. {
  132. vlcj.setLevel(Level.TRACE);
  133. externalloggerConfig.setLevel(Level.TRACE);
  134. trueVFSlloggerConfig.setLevel(Level.TRACE);
  135. }
  136. else
  137. {
  138. vlcj.setLevel(Level.INFO);
  139. externalloggerConfig.setLevel(Level.INFO);
  140. trueVFSlloggerConfig.setLevel(Level.INFO);
  141. }
  142. }
  143. else
  144. {
  145. loggerConfig.setLevel(Level.INFO);
  146. externalloggerConfig.setLevel(Level.INFO);
  147. trueVFSlloggerConfig.setLevel(Level.INFO);
  148. }
  149. cx.updateLoggers();
  150. }
  151. public void game(){
  152. if(System.getProperties().containsKey("botdebug"))
  153. {
  154. returnMsgs.add("join|Red5-devel|mösenpilze-8=====D-fdewcugeww");
  155. }
  156. else
  157. {
  158. returnMsgs.add("join|Red5|mösenpilze-8=====D-fdewcugewgw");
  159. }
  160. try {
  161. SocketAddress scka=new InetSocketAddress(InetAddress.getByName("gpn-tron.duckdns.org"), 4000);
  162. Socket sck = new Socket();
  163. sck.connect(scka,1000);
  164. BufferedReader r = new BufferedReader(new InputStreamReader(sck.getInputStream()));
  165. PrintStream out = new PrintStream(sck.getOutputStream(),true,"UTF-8");
  166. String jm = returnMsgs.poll();
  167. System.out.print(jm+'\n');
  168. out.print(jm+'\n');
  169. out.flush();
  170. //Thread t = new Thread(()->{
  171. while(sck.isConnected())
  172. {
  173. if(sck.isClosed())
  174. {
  175. return;
  176. }
  177. if(kill)
  178. {
  179. return;
  180. }
  181. //l.trace("Munching ahead...");
  182. String request = "";
  183. try {
  184. String ln = r.readLine();
  185. //if(gaming)
  186. // l.trace("packet line = "+ln);
  187. if(ln!=null)
  188. {
  189. if(ln.contains("message"))
  190. {
  191. continue;
  192. }
  193. if(ln.contains("lose"))
  194. {
  195. if(boards==null)
  196. {
  197. l.info("Boards nulled?");
  198. kill=true;
  199. respawn=true;
  200. }
  201. String boardview = "\nDumping Core....\nDone:\n";
  202. for(int i=0;i<ch;i++)
  203. {
  204. String secondaryline = "";
  205. for(int j=0;j<cw;j++)
  206. {
  207. boolean self =false;
  208. if(i==cy&&j==cx)
  209. {
  210. self=true;
  211. boardview+="XXXX";
  212. }
  213. String number ="000"+boards[i][j]+"|";
  214. String ticket = "000"+tickets[i][j]+"|";
  215. String hbfr = headBuffer[i][j]?"#":"_";
  216. if(self)
  217. {
  218. hbfr=debugdir;
  219. }
  220. boardview+=hbfr+
  221. number.substring(number.length()-4,number.length());
  222. secondaryline+=ticket.substring(ticket.length()-5,ticket.length());
  223. }
  224. boardview+="\n"+secondaryline+"\n";
  225. }l.info("LOSER!!!1111elf");
  226. l.info(boardview+"\nEnd of Dump");
  227. gaming=false;
  228. }
  229. if(ln.contains("game"))
  230. {
  231. gaming=true;
  232. ticksSinceStart =0;
  233. lastcase=-1;
  234. l.info(ln);
  235. String[] ls = ln.split("\\|");
  236. int w = Integer.valueOf(ls[1]);
  237. int h = Integer.valueOf(ls[2]);
  238. board = new int[w][h];
  239. cw=w;ch=h;
  240. playerid=ls[3];
  241. boards=new int[h][w];
  242. boardFF=new int[h][w];
  243. headBuffer = new boolean[h][w];
  244. out.print("chat|Red 5 standing by"+'\n');
  245. out.flush();
  246. tickets = new int[h][w];
  247. }
  248. if(ln.contains("pos")&&gaming)
  249. {
  250. if(playercount==0)
  251. {
  252. headBuffer = new boolean[ch][cw];
  253. }
  254. playercount++;
  255. String[] ls = ln.split("\\|");
  256. int w = Integer.valueOf(ls[2]);
  257. int h = Integer.valueOf(ls[3]);
  258. if(ls[1].equals(playerid))
  259. {
  260. l.info("Self");
  261. cx=w;cy=h;
  262. int cni = cy-1;
  263. if(cni==-1)
  264. cni=ch-1;
  265. int csi=(cy+1)%ch;
  266. int cwi = cx-1;
  267. if(cwi==-1)
  268. cwi=cw-1;
  269. int cei=(cx+1)%cw;
  270. int c2ni = cni-1;
  271. if(c2ni==-1)
  272. c2ni=ch-1;
  273. int c2si=(csi+1)%ch;
  274. int c2wi = cwi-1;
  275. if(c2wi==-1)
  276. c2wi=cw-1;
  277. int c2ei=(cei+1)%cw;
  278. boardFF[cni][cwi]=-1;
  279. boardFF[cni][cei]=-1;
  280. boardFF[csi][cwi]=-1;
  281. boardFF[csi][cei]=-1;
  282. boardFF[c2ni][c2wi]=-1;
  283. boardFF[c2ni][c2ei]=-1;
  284. boardFF[c2si][c2wi]=-1;
  285. boardFF[c2si][c2ei]=-1;
  286. tickets[h][w]= ticksSinceStart;
  287. }
  288. else
  289. {
  290. headBuffer[h][w]=true;
  291. }
  292. boards[h][w]=Integer.valueOf(ls[1])+1;
  293. }
  294. if(ln.contains("die"))
  295. {
  296. l.info(ln);
  297. String[] lns = ln.split("\\|");
  298. for(int k=1;k<lns.length;k++)
  299. {
  300. int death = Integer.valueOf(lns[k])+1;
  301. l.info("wiping"+death);
  302. for(int i=0;i<ch;i++) {
  303. for (int j = 0; j < cw; j++) {
  304. if(boards[i][j]==death)
  305. {
  306. boards[i][j]=0;
  307. tickets[i][j]=-ticksSinceStart;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. if(ln.contains("tick")&&gaming) {
  314. heads_touched=0;
  315. String boardview = "";
  316. for(int i=0;i<ch;i++)
  317. {
  318. for(int j=0;j<cw;j++)
  319. {
  320. int cni = i-1;
  321. if(cni==-1)
  322. cni=ch-1;
  323. int csi=(i+1)%ch;
  324. int cwi = j-1;
  325. if(cwi==-1)
  326. cwi=cw-1;
  327. int cei=(j+1)%cw;
  328. board[i][j]=0;
  329. board[i][j]+=(boards[i][j]!=0)?10:0;//||(boards[cni][j]!=0&&boards[csi][j]!=0)||(boards[i][cwi]!=0&&boards[j][cei]!=0));
  330. boardFF[i][j]=(boards[i][j]!=0)?-1:0;//||(boards[cni][j]!=0&&boards[csi][j]!=0)||(boards[i][cwi]!=0&&boards[j][cei]!=0));
  331. if(j==cx&&i==cy)
  332. {
  333. //boardview+="X";
  334. }
  335. //boardview+=boardFF[i][j]+";"+boards[i][j]+"|";
  336. }
  337. //boardview+="\n";
  338. }
  339. int cn = cy-1;
  340. if(cn==-1)
  341. cn=ch-1;
  342. int cs=(cy+1)%ch;
  343. int cw_ = cx-1;
  344. if(cw_==-1)
  345. cw_=cw-1;
  346. int ce=(cx+1)%cw;
  347. boolean randomDone = false;
  348. String direction = "up";
  349. // AAAAAAA
  350. //l.info("\n"+boardview);
  351. if(board[cn][cx]>9&&board[cs][cx]>9&&board[cy][ce]>9&&board[cy][cw_]>9)
  352. {
  353. l.info("giving up?");
  354. randomDone=true;
  355. lastcase=3;
  356. }
  357. int west=0,east=0,south=0,north=0;
  358. if(ticksSinceStart >0) {
  359. if(ticksSinceStart <0)
  360. {east = boardFF(ce, cy,1);
  361. west = boardFF(cw_, cy,2);
  362. north = boardFF(cx, cn,3);
  363. south = boardFF(cx, cs,4);
  364. }
  365. else
  366. {
  367. //int rnd = (int) (Math.random()*6);
  368. //int rnd=(fuck/13)%4;
  369. int rnd=lastDir;
  370. if(ticksSinceStart %100==0) returnMsgs.add("chat|All Hail the User!!!!!!");
  371. if(ticksSinceStart %100==50) returnMsgs.add("chat|Red 5 standing by");
  372. if(ticksSinceStart %100==75) returnMsgs.add("chat|rm -rf ~/botarmee*");
  373. out.print(returnMsgs.poll()+'\n');
  374. out.flush();
  375. l.info("lastdir"+lastDir);
  376. //boolean switcheroo = new File("/home/lh/switch").exists();
  377. switch(rnd)
  378. {
  379. case 3://prev=east
  380. if(switcheroo) south = boardFF(cx, cs,1);
  381. else north = boardFF(cx, cn,2);
  382. east = boardFF(ce, cy,3);
  383. if (switcheroo) north = boardFF(cx, cn,2);
  384. else south = boardFF(cx, cs,1);
  385. west = boardFF(cw_, cy,4);
  386. l.info("lnesw"+lastDir+";"+north+";"+east+";"+south+";"+west);
  387. break;
  388. case 1: //prev=west
  389. if(switcheroo) north = boardFF(cx, cn,1);
  390. else south = boardFF(cx, cs,2);
  391. west = boardFF(cw_, cy,3);
  392. if (switcheroo) south = boardFF(cx, cs,2);
  393. else north = boardFF(cx, cn,1);
  394. east = boardFF(ce, cy,4);
  395. break;
  396. case 2://prev=down
  397. if(switcheroo)west = boardFF(cw_, cy,1);
  398. else east = boardFF(ce, cy,2);
  399. south = boardFF(cx, cs,3);
  400. if(switcheroo)east = boardFF(ce, cy,2);
  401. else west = boardFF(cw_, cy,1);
  402. north = boardFF(cx, cn, 4);
  403. break;
  404. case 0: //prev=up
  405. if(switcheroo)east = boardFF(ce, cy,1);
  406. else west = boardFF(cw_, cy,2);
  407. north = boardFF(cx, cn,3);
  408. if(switcheroo) west =boardFF(cw_, cy,2);
  409. else east =boardFF(ce, cy,1);
  410. south = boardFF(cx, cs,4);
  411. break;
  412. }
  413. }
  414. }
  415. else{
  416. if(ticksSinceStart <30) {
  417. south = boardFF(cx, cs,1);
  418. north = boardFF(cx, cn,2);
  419. west = boardFF(cw_, cy,3);
  420. east = boardFF(ce, cy,4);
  421. }
  422. }
  423. ticksSinceStart++;
  424. int highest=Math.max(Math.max(north,south),Math.max(west,east));
  425. l.info("hnesw"+highest+";"+north+";"+east+";"+south+";"+west);
  426. if(((ticksSinceStart <20||(ticksSinceStart >40&& ticksSinceStart <60)||(500+ ticksSinceStart -330)%500<30)||(playercount<3&&(ticksSinceStart %100<50)))&&highest>1000)
  427. {
  428. l.info(ticksSinceStart <20);
  429. l.info(((500+ ticksSinceStart -330)%500)<30);
  430. l.info("remappingparty");
  431. if(east==-1337)
  432. east=highest;
  433. if(west==-1337)
  434. west=highest;
  435. if(south==-1337)
  436. south=highest;
  437. if(north==-1337)
  438. north=highest;
  439. }
  440. else
  441. {
  442. {
  443. if(east==-1337)
  444. east=0;
  445. if(west==-1337)
  446. west=0;
  447. if(south==-1337)
  448. south=0;
  449. if(north==-1337)
  450. north=0;
  451. }
  452. }
  453. if((highest<((cw*ch)/(playercount+1))||playercount<3))
  454. {
  455. l.info("Switcheroo zone reached");
  456. if(ticksSinceStart %10==0)
  457. {
  458. l.info("chkFlick");
  459. if(heads_touched<2&&playercount>5&&ticksSinceStart>150)
  460. {
  461. l.info("headcount:"+heads_touched);
  462. switcheroo=!switcheroo;
  463. }
  464. if((ticksSinceStart /10)%3<2)
  465. {
  466. l.info("flick");
  467. switcheroo=!switcheroo;
  468. } {
  469. l.info("flick");
  470. switcheroo=!switcheroo;
  471. }
  472. }
  473. }
  474. l.info("switcheroo="+switcheroo);
  475. l.info("fnesw"+ ticksSinceStart +";"+north+";"+east+";"+south+";"+west);
  476. int rnd=(int)(Math.random()*4);
  477. if(ticksSinceStart %300<25&& ticksSinceStart >15)
  478. {
  479. rnd=0;
  480. }
  481. int lastchoice=-1;
  482. int treshold=1;
  483. while(!randomDone)
  484. {
  485. //rnd = (int) (Math.random()*4);
  486. //rnd=lastchoice;
  487. /*lastchoice+=1;
  488. if(lastchoice>3)
  489. {
  490. lastchoice=0;
  491. treshold++;
  492. }
  493. if(treshold>3)
  494. {
  495. }*/
  496. //l.info("rnd="+rnd+"lastcse="+lastcase);
  497. /*if(rnd==lastcase)
  498. {
  499. l.info("reroll");
  500. continue;
  501. }*/
  502. if(highest<10)
  503. {
  504. returnMsgs.add("chat|godmayFUCKingdamnit");
  505. out.print(returnMsgs.poll()+'\n');
  506. out.flush();
  507. }
  508. switch(rnd)
  509. {
  510. case 1:
  511. //if((board[cy][cw_]<treshold))
  512. if(west==highest)
  513. {
  514. lastDir=1;
  515. debugdir="<";
  516. direction="left";
  517. randomDone=true;
  518. lastcase=3;}
  519. break;
  520. case 3:
  521. //if((board[cy][ce]<treshold))
  522. if(east==highest)
  523. {
  524. lastDir=3;
  525. direction="right";
  526. randomDone=true;
  527. debugdir=">";
  528. lastcase=1;
  529. }
  530. break;
  531. case 0:
  532. lastDir=0;
  533. //if(((board[cn][cx]*2)<treshold)){
  534. if(north==highest){
  535. debugdir="^";
  536. direction="up";
  537. randomDone=true;
  538. lastcase=2;}
  539. break;
  540. case 2:
  541. lastDir=2;
  542. //if((board[cs][cx]<treshold))
  543. if(south==highest)
  544. {
  545. debugdir="V";
  546. direction="down";
  547. randomDone=true;
  548. lastcase=0;}
  549. break;
  550. }
  551. rnd=(rnd+1)%4;
  552. }
  553. //headBuffer = new boolean[ch][cw];
  554. l.trace("pc="+playercount);
  555. playercount=0;
  556. out.print("move|"+direction+'\n');
  557. System.out.print("move|"+direction+'\n');
  558. out.flush();
  559. }
  560. }
  561. else
  562. {
  563. l.info("SCK:");
  564. l.info(sck.isClosed());
  565. l.info(sck.isConnected());
  566. l.info(sck.isInputShutdown());
  567. kill=true;
  568. respawn=true;
  569. throw new Error();
  570. }
  571. } catch (IOException ex) {
  572. //l.warn("NETZAP");
  573. return;
  574. //break;
  575. }
  576. //l.trace(request);
  577. }
  578. //});
  579. //t.setName("InputCruncher");
  580. //t.start();
  581. /*Thread t2 = new Thread(()->{
  582. //System.out.println("Gefangen!");
  583. try {
  584. while(sck.isConnected())
  585. {
  586. if(kill)
  587. {
  588. l.info("time to say goodbye");
  589. return;
  590. }
  591. synchronized(syncer)
  592. {
  593. mösenpilze++;
  594. if(mösenpilze>300000000)
  595. {
  596. mösenpilze=0;
  597. l.info("WHY THZE FUDGE");
  598. }
  599. while(!returnMsgs.isEmpty())
  600. {
  601. mösenpilze=0;
  602. submöse++;
  603. if(submöse>3)
  604. {
  605. möseon=true;
  606. }
  607. // FUCK YOU DUMB IDIOT; NO THREAD.SLEEP() HERE!!!!!11!!11elf
  608. String returnMsg = returnMsgs.poll();
  609. if(returnMsg == null)
  610. {
  611. submöse2++;
  612. if(submöse2<3)
  613. {
  614. l.trace("22MP");
  615. }
  616. else
  617. {
  618. if(submöse2==4)
  619. {
  620. l.trace("Hodentasche");
  621. submöse2=0;
  622. returnMsgs = new ConcurrentLinkedQueue<>();
  623. //kill=true;
  624. //respawn=true;
  625. //sck.close();
  626. //return;
  627. }
  628. }
  629. continue;
  630. } out.print(returnMsg+'\n');
  631. out.flush();
  632. l.trace("PKT="+returnMsg);
  633. out.print(returnMsg+'\n');
  634. out.flush();
  635. }
  636. //l.trace("end of MessageStream");
  637. }
  638. }
  639. }
  640. catch (Exception e)
  641. {
  642. l.error("ZZMP");
  643. l.error(e);
  644. System.exit(1337);
  645. }
  646. System.out.println("Flucht ist zwecklos");
  647. System.exit(1337);
  648. });
  649. t2.setName("OutputCruncher");
  650. t2.start();*/
  651. l.info("asdfqwxix");
  652. }
  653. catch (IOException ex)
  654. {
  655. respawn=true;
  656. ex.printStackTrace();
  657. l.error(" Unable to connect to Host. Aborting now");
  658. try {
  659. Thread.sleep(199);
  660. } catch (InterruptedException e) {
  661. throw new RuntimeException(e);
  662. }
  663. }
  664. }
  665. //int submöse2=0;
  666. //int submöse=0;
  667. //boolean möseon=false;
  668. //long mösenpilze = 0;
  669. }