浏览代码

Botting on heavens door

LH 1 年之前
父节点
当前提交
74a03cc9b7
共有 1 个文件被更改,包括 497 次插入0 次删除
  1. 497 0
      QuickStuff/src/main/java/GPN/BotGame.java

+ 497 - 0
QuickStuff/src/main/java/GPN/BotGame.java

@@ -0,0 +1,497 @@
+package GPN;
+
+import de.nplusc.izc.tools.baseTools.Detectors;
+import de.nplusc.izc.tools.baseTools.Tools;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.yaml.snakeyaml.Yaml;
+
+import javax.swing.*;
+import java.io.*;
+import java.net.Socket;
+import java.nio.charset.Charset;
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class BotGame {
+    private boolean switchItharder = false;
+    private Queue<String> returnMsgs = new LinkedList<>();
+    Object syncer = new Object();
+    static Logger l = LogManager.getLogger();
+    private int[] []board = new int[1][1];
+    int[][] boards;
+    int[][] boardFF;
+    int fuck = 0;
+    private int cx=0,cy=0,cw=1,ch=1;
+    private String playerid;
+    boolean[][] headBuffer;
+    int lastDir = 0;
+    boolean gaming = false;
+    public static void main(String[] args) {
+        setupLogging(true,2);
+        l.info("shizzlemork");
+        new BotGame().game();
+    }
+    int lastcase=-1;
+
+    private int boardFF(int x, int y)
+    {
+        if(boardFF[y][x]!=0)
+        {
+            return 0;
+        }
+
+        int rv = 2 ;
+        int cni = y-1;
+        if(cni==-1)
+            cni=ch-1;
+        int csi=(y+1)%ch;
+
+        int cwi = x-1;
+        if(cwi==-1)
+            cwi=cw-1;
+        int cei=(x+1)%cw;
+        if(headBuffer[y][cwi]||headBuffer[y][cei]||headBuffer[csi][x]||headBuffer[cni][x])
+        {
+            return 1;
+        }
+        boardFF[y][x]=1;
+        rv+=boardFF(x,cni);
+        rv+=boardFF(x,csi);
+        rv+=boardFF(cwi,y);
+        rv+=boardFF(cei,y);
+
+        return rv;
+    }
+
+    static void setupLogging(boolean verbose,int verbosityLevel)
+    {
+        System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager"); //HACK to catch java.util.logging loggers
+        l=LogManager.getLogger();
+        LoggerContext cx = (LoggerContext) LogManager.getContext(false);
+        org.apache.logging.log4j.core.config.Configuration config = cx.getConfiguration();
+        LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
+        LoggerConfig vlcj = config.getLoggerConfig("uk.co.caprica.vlcj");
+        LoggerConfig externalloggerConfig = config.getLoggerConfig("External");
+        LoggerConfig trueVFSlloggerConfig = config.getLoggerConfig("net.java");
+
+        if (verbose)
+        {
+            loggerConfig.setLevel(Level.TRACE);
+
+            if(verbosityLevel>1)
+            {
+                vlcj.setLevel(Level.TRACE);
+                externalloggerConfig.setLevel(Level.TRACE);
+                trueVFSlloggerConfig.setLevel(Level.TRACE);
+            }
+            else
+            {
+                vlcj.setLevel(Level.INFO);
+                externalloggerConfig.setLevel(Level.INFO);
+                trueVFSlloggerConfig.setLevel(Level.INFO);
+            }
+        }
+        else
+        {
+            loggerConfig.setLevel(Level.INFO);
+            externalloggerConfig.setLevel(Level.INFO);
+            trueVFSlloggerConfig.setLevel(Level.INFO);
+        }
+        cx.updateLoggers();
+    }
+
+    public void game(){
+        new Thread(()->{
+            while(true)
+            {
+                Tools.dlg(false,"Toggle","any");
+                switchItharder=!switchItharder;
+            }
+        });//.start();
+        returnMsgs.add("join|masterX244|öoewiuvföewugewgw");
+        try {
+            Socket sck=new Socket("gpn-tron.duckdns.org", 4000);
+            BufferedReader r = new BufferedReader(new InputStreamReader(sck.getInputStream()));
+            PrintStream out = new PrintStream(sck.getOutputStream(),true,"UTF-8");
+            Thread t = new Thread(()->{
+                while(sck.isConnected())
+                {
+                    //l.trace("Munching ahead...");
+                    String request = "";
+                    try {
+                        String ln = r.readLine();
+                        //if(gaming)
+                        //    l.trace("packet line = "+ln);
+                        if(ln!=null)
+                        {
+                            if(ln.contains("message"))
+                            {
+                                continue;
+                            }
+                            if(ln.contains("lose"))
+                            {
+                                l.info("LOSER!!!1111elf");
+                                gaming=false;
+                            }
+                            if(ln.contains("game"))
+                            {
+
+                                gaming=true;
+                                fuck=0;
+                                lastcase=-1;
+                                l.info(ln);
+                                String[] ls = ln.split("\\|");
+                                int w = Integer.valueOf(ls[1]);
+                                int h = Integer.valueOf(ls[2]);
+                                board = new int[w][h];
+                                cw=w;ch=h;
+                                playerid=ls[3];
+                                boards=new int[h][w];
+                                boardFF=new int[h][w];
+                                headBuffer = new boolean[h][w];
+                                returnMsgs.add("chat|Red 5 standing by");
+                            }
+                            if(ln.contains("pos")&&gaming)
+                            {
+                                String[] ls = ln.split("\\|");
+                                int w = Integer.valueOf(ls[2]);
+                                int h = Integer.valueOf(ls[3]);
+                                if(ls[1].equals(playerid))
+                                {
+                                    l.info("Self");
+                                    cx=w;cy=h;
+                                    int cni = cy-1;
+                                    if(cni==-1)
+                                        cni=ch-1;
+                                    int csi=(cy+1)%ch;
+
+                                    int cwi = cx-1;
+                                    if(cwi==-1)
+                                        cwi=cw-1;
+                                    int cei=(cx+1)%cw;
+                                    boardFF[cni][cwi]=-1;
+                                    boardFF[cni][cei]=-1;
+                                    boardFF[csi][cwi]=-1;
+                                    boardFF[csi][cei]=-1;
+
+                                }
+                                else
+                                {
+                                    headBuffer[h][w]=true;
+                                }
+                                boards[h][w]=Integer.valueOf(ls[1])+1;
+                            }
+                            if(ln.contains("die"))
+                            {
+                                l.info(ln);
+                                String[] lns = ln.split("\\|");
+                                for(int k=1;k<lns.length;k++)
+                                {
+                                    int death = Integer.valueOf(lns[k])+1;
+                                    l.info("wiping"+death);
+                                    for(int i=0;i<cw;i++) {
+                                        for (int j = 0; j < ch; j++) {
+                                            if(boards[i][j]==death)
+                                            {
+                                                boards[i][j]=0;
+
+                                            }
+                                        }
+                                    }
+                                }
+
+                            }
+                            if(ln.contains("tick")&&gaming) {
+                                String boardview = "";
+                                for(int i=0;i<ch;i++)
+                                {
+                                    for(int j=0;j<cw;j++)
+                                    {
+
+                                        int cni = i-1;
+                                        if(cni==-1)
+                                            cni=ch-1;
+                                        int csi=(i+1)%ch;
+
+                                        int cwi = j-1;
+                                        if(cwi==-1)
+                                            cwi=cw-1;
+                                        int cei=(j+1)%cw;
+
+                                        board[i][j]=0; //wi
+                                        //l.info(i+"|"+cni+"|"+csi+"|");
+                                        boolean denseField = (boards[cni][j]!=0&&boards[csi][j]!=0);
+                                        denseField |= (boards[cwi][j]!=0&&boards[cei][j]!=0);
+
+                                        denseField |= (boards[cni][j]!=0&&boards[cei][j]!=0);
+                                        denseField |= (boards[cei][j]!=0&&boards[csi][j]!=0);
+                                        denseField |= (boards[csi][j]!=0&&boards[cwi][j]!=0);
+                                        denseField |= (boards[cwi][j]!=0&&boards[cni][j]!=0);
+                                        if(denseField)
+                                        {
+                                           // l.info("Dense spot...");
+                                            //board[i][j]=1;
+                                        }
+                                        boolean denserField = false;
+                                        denserField |= (boards[cni][j]!=0&&boards[cei][j]!=0&&boards[csi][j]!=0);
+                                        denserField |= (boards[cei][j]!=0&&boards[csi][j]!=0&&boards[cwi][j]!=0);
+                                        denserField |= (boards[csi][j]!=0&&boards[cwi][j]!=0&&boards[cni][j]!=0);
+                                        denserField |= (boards[cwi][j]!=0&&boards[cni][j]!=0&&boards[cei][j]!=0);
+                                        if(denserField)
+                                        {
+                                            // l.info("Dense spot...");
+                                            //board[i][j]=2;
+                                        }
+                                        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));
+                                        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));
+                                        if(j==cx&&i==cy)
+                                        {
+                                            boardview+="X";
+                                        }
+                                        boardview+=boardFF[i][j]+";"+boards[i][j]+"|";
+                                    }
+                                    boardview+="\n";
+                                }
+                                int cn = cy-1;
+                                if(cn==-1)
+                                    cn=ch-1;
+                                int cs=(cy+1)%ch;
+
+                                int cw_ = cx-1;
+                                if(cw_==-1)
+                                    cw_=cw-1;
+                                int ce=(cx+1)%cw;
+                                boolean randomDone = false;
+                                String direction = "up";
+                                // AAAAAAA
+                                //l.info("\n"+boardview);
+                                if(board[cn][cx]>9&&board[cs][cx]>9&&board[cy][ce]>9&&board[cy][cw_]>9)
+                                {
+                                    l.info("giving up?");
+                                    randomDone=true;
+                                    lastcase=3;
+                                }
+                                int west=0,east=0,south=0,north=0;
+                                if(fuck>0) {
+                                    if(fuck<0)
+                                    {east = boardFF(ce, cy);
+                                     west = boardFF(cw_, cy);
+
+                                     north = boardFF(cx, cn);
+                                     south = boardFF(cx, cs);
+                                    }
+                                    else
+                                    {
+                                        //int rnd = (int) (Math.random()*6);
+                                        //int rnd=(fuck/13)%4;
+
+                                        int rnd=lastDir;
+                                        if(fuck<15||fuck%100==0||new File("/home/lh/random").exists())
+                                        {
+                                            //rnd = (int) (Math.random()*4);
+                                        }
+                                        if(fuck<10)
+                                        {
+                                            //rnd=3;
+                                        }
+                                        if(fuck%100==0) returnMsgs.add("chat|All Hail the User!!!!!!");
+                                        if(fuck%100==50) returnMsgs.add("chat|Red 5 standing by");
+                                        if(fuck%100==75) returnMsgs.add("chat|rm -rf ~/botarmee*");
+                                        l.info("lastdir"+lastDir);
+                                        boolean switcheroo = new File("/home/lh/switch").exists();
+
+                                        switch(rnd)
+                                        {
+                                            case 3://prev=east
+                                                if(switcheroo) south = boardFF(cx, cs);
+                                                else north = boardFF(cx, cn);
+                                                east = boardFF(ce, cy);
+                                                north += boardFF(cx, cn);
+                                                south += boardFF(cx, cs);
+                                                west = boardFF(cw_, cy);
+                                                l.info("lnesw"+lastDir+";"+north+";"+east+";"+south+";"+west);
+
+                                                break;
+                                            case 1: //prev=west
+                                                if(switcheroo) north = boardFF(cx, cn);
+                                                else south = boardFF(cx, cs);
+                                                west = boardFF(cw_, cy);
+                                                south += boardFF(cx, cs);
+                                                north += boardFF(cx, cn);
+                                                east = boardFF(ce, cy);
+                                                break;
+                                            case 2://prev=down
+                                                if(switcheroo)west = boardFF(cw_, cy);
+                                                else    east = boardFF(ce, cy);
+                                                south = boardFF(cx, cs);
+                                                east += boardFF(ce, cy);
+                                                west += boardFF(cw_, cy);
+                                                north = boardFF(cx, cn);
+                                                break;
+                                            case 0: //prev=up
+                                                if(switcheroo)east = boardFF(ce, cy);
+                                                else west = boardFF(cw_, cy);
+                                                north = boardFF(cx, cn);
+                                                west  +=boardFF(cw_, cy);
+                                                east  +=boardFF(ce, cy);
+                                                south = boardFF(cx, cs);
+
+
+
+
+                                                break;
+                                        }
+                                    }
+                                }
+                                else{
+                                    if(fuck<30) {
+                                        south = boardFF(cx, cs);
+                                        north = boardFF(cx, cn);
+
+                                        west = boardFF(cw_, cy);
+                                        east = boardFF(ce, cy);
+                                    }
+
+                                }
+                                fuck++;
+
+                                int highest=Math.max(Math.max(north,south),Math.max(west,east));
+                                l.info("hnesw"+highest+";"+north+";"+east+";"+south+";"+west);
+                                l.info("fnesw"+fuck+";"+north+";"+east+";"+south+";"+west);
+                                int rnd=-2;
+                                int lastchoice=-1;
+                                int treshold=1;
+                                while(!randomDone)
+                                {
+                                    //rnd = (int) (Math.random()*4);
+                                    rnd=lastchoice;
+                                    lastchoice+=1;
+                                    if(lastchoice>3)
+                                    {
+                                        lastchoice=0;
+                                        treshold++;
+                                    }
+                                    if(treshold>3)
+                                    {
+
+                                    }
+                                    //l.info("rnd="+rnd+"lastcse="+lastcase);
+                                    /*if(rnd==lastcase)
+                                    {
+                                        l.info("reroll");
+                                        continue;
+                                    }*/
+                                    if(highest<10)
+                                    {
+                                        returnMsgs.add("chat|godmayFUCKingdamnit");
+                                    }
+                                    switch(rnd)
+                                    {
+                                        case 1:
+                                            //if((board[cy][cw_]<treshold))
+                                            if(west==highest)
+                                            {
+                                                lastDir=1;
+                                                direction="left";
+                                                randomDone=true;
+                                                lastcase=3;}
+                                            break;
+                                        case 3:
+                                            //if((board[cy][ce]<treshold))
+                                            if(east==highest)
+                                            {
+                                                lastDir=3;
+                                                direction="right";
+                                            randomDone=true;
+                                            lastcase=1;}
+                                            break;
+                                        case 0:
+                                            lastDir=0;
+                                            //if(((board[cn][cx]*2)<treshold)){
+                                            if(north==highest){
+                                                direction="up";
+                                                randomDone=true;
+                                                lastcase=2;}
+                                            break;
+                                        case 2:
+                                            lastDir=2;
+                                            //if((board[cs][cx]<treshold))
+                                            if(south==highest)
+                                            {
+                                                direction="down";
+                                                 randomDone=true;
+                                                 lastcase=0;}
+                                            break;
+
+                                    }
+                                }
+                                headBuffer = new boolean[ch][cw];
+                                returnMsgs.add("move|"+direction);
+
+                            }
+                        }
+                        else
+                        {
+                            return;
+                        }
+
+                    } catch (IOException ex) {
+                        //l.warn("NETZAP");
+                        return;
+                        //break;
+                    }
+                    //l.trace(request);
+
+                }
+            });
+            t.setName("InputCruncher");
+            t.start();
+            Thread t2 = new Thread(()->{
+                //System.out.println("Gefangen!");
+                while(sck.isConnected())
+                {
+                    synchronized(syncer)
+                    {
+                        try {
+                            Thread.sleep(50);
+                        } catch (InterruptedException e) {
+                            throw new RuntimeException(e);
+                        }
+                        //l.trace("MSG sent");
+                        while(!returnMsgs.isEmpty())
+                        {
+                            String returnMsg = returnMsgs.poll();
+                            l.trace("PKT="+returnMsg);
+                            out.print(returnMsg+'\n');
+                            out.flush();
+                        }
+                        //l.trace("end of MessageStream");
+                    }
+                }
+                //System.out.println("Flucht ist zwecklos");
+            });
+            t2.setName("OutputCruncher");
+            t2.start();
+            l.info("asdfqwxix");
+        }
+        catch (IOException ex)
+    {
+        ex.printStackTrace();
+        l.error(" Unable to connect to Host. Aborting now");
+        try {
+            Thread.sleep(199);
+        } catch (InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+        game();
+        if(Detectors.isJavaw())
+        {
+            JOptionPane.showMessageDialog(null, "Host given to Program is invalid", "ERROR", JOptionPane.ERROR_MESSAGE);
+        }
+    }
+    }
+}