Ejemplo n.º 1
0
 // send a shot message to execute a shot in the fast mode
 public byte[] shootFast(
     byte[] fx, byte[] fy, byte[] dx, byte[] dy, byte[] t1, byte[] t2, boolean polar) {
   byte[] inbuffer = new byte[16];
   try {
     if (polar) out.write(ClientMessageEncoder.pFastshoot(fx, fy, dx, dy, t1, t2));
     else out.write(ClientMessageEncoder.cFastshoot(fx, fy, dx, dy, t1, t2));
     out.flush();
     in.read(inbuffer);
     return inbuffer;
   } catch (IOException e) {
     e.printStackTrace();
   }
   return new byte[] {0};
 }
Ejemplo n.º 2
0
 // send a message to restart the level
 public synchronized byte restartLevel() {
   try {
     out.write(ClientMessageEncoder.restart());
     return (byte) in.read();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return 0;
 }
Ejemplo n.º 3
0
  public synchronized BufferedImage doScreenShot_() {
    BufferedImage bfImage = null;
    try {
      // 2. get Input and Output streams
      byte[] doScreenShot = ClientMessageEncoder.encodeDoScreenShot();
      out.write(doScreenShot);
      out.flush();
      // System.out.println("client executes command: screen shot");

      // Read the message head : 4-byte width and 4-byte height, respectively
      byte[] bytewidth = new byte[4];
      byte[] byteheight = new byte[4];
      int width, height;
      in.read(bytewidth);
      width = bytesToInt(bytewidth);
      in.read(byteheight);
      height = bytesToInt(byteheight);

      // initialize total bytes of the screenshot message
      // not include the head
      int totalBytes = width * height * 3;

      // read the raw RGB data
      byte[] bytebuffer;
      // System.out.println(width + "  " + height);
      byte[] imgbyte = new byte[totalBytes];
      int hasReadBytes = 0;
      while (hasReadBytes < totalBytes) {
        bytebuffer = new byte[2048];
        int nBytes = in.read(bytebuffer);
        if (nBytes != -1) System.arraycopy(bytebuffer, 0, imgbyte, hasReadBytes, nBytes);
        else break;
        hasReadBytes += nBytes;
      }

      // set RGB data using BufferedImage
      bfImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
          int R = imgbyte[(y * width + x) * 3] & 0xff;
          int G = imgbyte[(y * width + x) * 3 + 1] & 0xff;
          int B = imgbyte[(y * width + x) * 3 + 2] & 0xff;
          Color color = new Color(R, G, B);
          int rgb;
          rgb = color.getRGB();
          bfImage.setRGB(x, y, rgb);
        }
      }

    } catch (IOException ioException) {
      ioException.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    }
    return bfImage;
  }
Ejemplo n.º 4
0
  // load a certain level
  public synchronized byte loadLevel(byte... i) {
    try {

      out.write(ClientMessageEncoder.loadLevel(i));
      return (byte) in.read();
    } catch (IOException e) {

      e.printStackTrace();
    }
    return 0;
  }
Ejemplo n.º 5
0
  public byte clickInCenter() {
    try {
      out.write(ClientMessageEncoder.clickInCenter());
      out.flush();
      return (byte) in.read();
    } catch (IOException e) {

      e.printStackTrace();
    }
    return 0;
  }
Ejemplo n.º 6
0
  // send message to fully zoom in
  public synchronized byte fullyZoomIn() {
    try {
      out.write(ClientMessageEncoder.fullyZoomIn());
      out.flush();
      return (byte) in.read();
    } catch (IOException e) {

      e.printStackTrace();
    }
    return 0;
  }
Ejemplo n.º 7
0
  // send a message to get the current state
  public byte getState() {
    try {
      out.write(ClientMessageEncoder.getState());
      out.flush();
      // System.out.println("IN READ  " + in.read());
      return (byte) in.read();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return 0;
  }
Ejemplo n.º 8
0
 // register team id
 public synchronized byte[] configure(byte[] team_id) {
   try {
     out.write(ClientMessageEncoder.configure(team_id));
     out.flush();
     byte[] result = new byte[4];
     in.read(result);
     return result;
   } catch (IOException e) {
     e.printStackTrace();
   }
   return null;
 }
Ejemplo n.º 9
0
  // send a sequence of shots message
  public byte[] cshootSequence(byte[]... shots) {
    byte[] inbuffer = new byte[16];

    byte[] msg =
        ClientMessageEncoder.mergeArray(
            new byte[] {ClientMessageTable.getValue(ClientMessageTable.shootSeq)},
            new byte[] {(byte) shots.length});
    for (byte[] shot : shots) {
      msg =
          ClientMessageEncoder.mergeArray(
              msg, new byte[] {ClientMessageTable.getValue(ClientMessageTable.cshoot)}, shot);
    }

    try {
      out.write(msg);

      in.read(inbuffer);
      return inbuffer;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return new byte[] {0};
  }
Ejemplo n.º 10
0
  // send a message to score of each level
  public byte[] getMyScore() {
    int level = 21;
    int totalBytes = level * 4;
    byte[] buffer = new byte[totalBytes];
    try {
      out.write(ClientMessageEncoder.getMyScore());
      out.flush();

      in.read(buffer);
      return buffer;

    } catch (IOException e) {
      e.printStackTrace();
    }
    return buffer;
  }