Foro de elhacker.net

Programación => Java => Mensaje iniciado por: snake_linux en 1 Diciembre 2019, 22:04 pm



Título: Experimento de "truco" simple para Counter-strike en Java
Publicado por: snake_linux en 1 Diciembre 2019, 22:04 pm
Buenas, estoy escribiendo un "truco" para CSGO muy simple en Java, es para fines educativos exclusivamente, por cacharrear, no tengo intención de utilizarlo para ganar.

Básicamente lo que quiero es que cuando el enemigo se ponga en la mira (que siempre está justo en el centro de la pantalla) se produzca un disparo, para ello necesito que el programa haga una "captura" de una pequeña región en el centro de la pantalla y lo compare con otras capturas que hace constantemente para ver si se ha producido o no un cambio.

Además, utilizo una biblioteca externa para lanzar el evento (arrancar el bot para que se ponga a comparar), la comparación la hago pixel a pixel con una función que encontré en internet.

El problema es que creo que no se está centrando correctamente, ya que no me detecta el cambio en el centro de la pantalla pero si en otras areas.

Les dejo el código:

Código
  1.  
  2. import java.awt.AWTException;
  3. import java.awt.Dimension;
  4. import java.awt.Rectangle;
  5. import java.awt.Robot;
  6. import java.awt.Toolkit;
  7. import java.awt.image.BufferedImage;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. import org.jnativehook.GlobalScreen;
  12. import org.jnativehook.NativeHookException;
  13. import org.jnativehook.keyboard.NativeKeyEvent;
  14. import org.jnativehook.keyboard.NativeKeyListener;
  15.  
  16. public class AutoBot  implements NativeKeyListener {
  17.  
  18. public static void main(String[] args) {
  19.  
  20.  
  21. // Get the logger for "org.jnativehook" and set the level to off.
  22. Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
  23. logger.setLevel(Level.OFF);
  24.  
  25. try {
  26. GlobalScreen.registerNativeHook();
  27. } catch (NativeHookException ex) {
  28. System.err.println("Error Nativehook");
  29. System.err.println(ex.getMessage());
  30.  
  31. System.exit(1);
  32. }
  33.  
  34.  
  35.  
  36. GlobalScreen.addNativeKeyListener(new AutoBot());
  37. System.out.println("Usa / para lanzar autobot");
  38.  
  39. }
  40.  
  41. public static boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
  42.  
  43. int margen=0;
  44.  
  45.    if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight()) {
  46.        for (int x = 0; x < img1.getWidth(); x++) {
  47.            for (int y = 0; y < img1.getHeight(); y++) {
  48.                if (img1.getRGB(x, y) != img2.getRGB(x, y))
  49.                    return false;
  50.            }
  51.        }
  52.    } else {
  53.        return false;
  54.    }
  55.    return true;
  56. }
  57.  
  58. public static void autoBot() throws AWTException {
  59.  
  60. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  61. Robot robot = new Robot();
  62.  
  63. double altura = screenSize.getHeight()/2;
  64. double ancho = screenSize.getWidth()/2;
  65.  
  66. Rectangle area = new Rectangle();
  67. area.setBounds((int)altura-20, (int)ancho-20, 20, 20);
  68. BufferedImage bufferedImage = robot.createScreenCapture(area);
  69. boolean noDisparar= false;
  70.  
  71. while(!noDisparar) {
  72.  
  73. Rectangle area2 = new Rectangle();
  74. area2.setBounds((int)altura-20, (int)ancho-20, 20, 20);
  75. BufferedImage bufferedImage2 = robot.createScreenCapture(area2);
  76.  
  77. if(!bufferedImagesEqual(bufferedImage, bufferedImage2)) {
  78. // robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  79. // robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  80. System.out.println("ha cambiado");
  81. noDisparar = true;
  82. }
  83. }
  84. }
  85.  
  86. @Override
  87. public void nativeKeyPressed(NativeKeyEvent e) {
  88.  
  89. }
  90.  
  91.  
  92. @Override
  93. public void nativeKeyReleased(NativeKeyEvent e) {
  94.  
  95. String tecla = NativeKeyEvent.getKeyText(e.getKeyCode());
  96.  
  97. if (tecla.equals("Barra")) {
  98. System.out.println("dale");
  99. try {
  100. autoBot();
  101. } catch (AWTException e1) {
  102. // TODO Auto-generated catch block
  103. e1.printStackTrace();
  104. }
  105. }
  106. }
  107.  
  108. @Override
  109. public void nativeKeyTyped(NativeKeyEvent e) {
  110.  
  111. }
  112. }
  113.  

Gracias, un saludo.