BotGame.java 29 KB

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