Resource Simple Mouse Target Renderer for debugging

Discussion in 'Tutorials & Resources' started by Fontkodo, Jun 21, 2020.

  1. Fontkodo

    Joined:
    Jun 21, 2014
    Messages:
    352
    Likes Received:
    111
    This version doesn't block your bot from interacting, but you can override the path generator move function to do nothing to do so.

    See an example of this working here:

    WFcaCPIiCt

    Code (Text):
    1. /* Originally written by ? and shared by Swatarianess
    2. * Modified by Furor June 2020
    3. * Renders the current and previous target interaction box in the background
    4. * Issues: Currently does not close created windows on generation
    5. */
    6.  
    7. package transfer;
    8.  
    9. import com.runemate.game.api.hybrid.Environment;
    10. import com.runemate.game.api.hybrid.entities.details.Interactable;
    11. import com.runemate.game.api.hybrid.entities.details.Modeled;
    12. import com.runemate.game.api.hybrid.entities.details.Renderable;
    13. import com.runemate.game.api.hybrid.input.Mouse;
    14. import com.runemate.game.api.hybrid.local.Screen;
    15. import com.runemate.game.api.hybrid.local.hud.InteractablePoint;
    16. import com.runemate.game.api.hybrid.local.hud.InteractableRectangle;
    17. import com.runemate.game.api.hybrid.local.hud.InteractableShape;
    18. import com.runemate.game.api.hybrid.local.hud.Model;
    19. import com.runemate.game.api.script.framework.AbstractBot;
    20. import com.runemate.game.api.script.framework.core.LoopingThread;
    21. import javafx.application.Platform;
    22. import javafx.scene.Group;
    23. import javafx.scene.Scene;
    24. import javafx.scene.canvas.Canvas;
    25. import javafx.scene.canvas.GraphicsContext;
    26. import javafx.scene.paint.Color;
    27. import javafx.stage.Stage;
    28. import javafx.stage.StageStyle;
    29.  
    30. import java.awt.*;
    31. import java.util.concurrent.Future;
    32.  
    33. public class RenderMouseTargets extends Stage {
    34.     private String id;
    35.     private Canvas pane;
    36.     private GraphicsContext graphics;
    37.     private InteractableRectangle bounds;
    38.     private InteractablePoint location;
    39.     private Future<?> renderLoop;
    40.  
    41.     //invoke manually in onStop to close the screen generated with your bot
    42.     public void stop(){
    43.         renderLoop.cancel(false);
    44.         Platform.runLater(() -> {
    45.                     this.close();
    46.                 }
    47.         );
    48.     }
    49.  
    50.     public Future<?> getRenderLoop() {
    51.         return renderLoop;
    52.     }
    53.  
    54.     //1 prev, 2 next
    55.     private Renderable[] renderModels = new Renderable[2];
    56.  
    57.     public RenderMouseTargets(AbstractBot bot) {
    58.         pane = new Canvas();
    59.  
    60.         Scene scene = new Scene(new Group(pane));
    61.         setScene(scene);
    62.  
    63.         graphics = pane.getGraphicsContext2D();
    64.  
    65.         graphics.setLineWidth(1);
    66.         graphics.setStroke(Color.RED);
    67.  
    68.         this.renderLoop = (bot.getPlatform().invokeLater(() -> new LoopingThread(() -> {
    69.             bounds = Screen.getBounds();
    70.             location = Screen.getLocation();
    71.  
    72.             //move with window
    73.             if (bounds != null && location != null) {
    74.                 Platform.runLater(() -> {
    75.                     setX(location.x);
    76.                     setY(location.y);
    77.                     pane.setWidth(bounds.width);
    78.                     pane.setHeight(bounds.width);
    79.                     setWidth(bounds.width);
    80.                     setHeight(bounds.height);
    81.                 });
    82.             }
    83.             renderModels();
    84.         }, 100).start()));
    85.  
    86.         //can be used for visibility
    87.         bot.getPlatform().invokeLater(() -> id = Environment.getRuneScapeProcessId());
    88.  
    89.         setAlwaysOnTop(true);
    90.  
    91.         initStyle(StageStyle.TRANSPARENT);
    92.  
    93.         scene.setFill(null);
    94.  
    95.         show();
    96.     }
    97.  
    98.     private void renderModels() {
    99.         Interactable [] targs = new Interactable[]{ Mouse.getPreviousTarget(), Mouse.getTarget() };
    100.  
    101.         for(int i = 0; i < targs.length; i++){
    102.             Interactable target = targs[i];
    103.  
    104.             //Should typically be modeled
    105.             if(target instanceof Modeled){
    106.                 Model model = ((Modeled) target).getModel();
    107.                 if(model != null){
    108.                     //Interaction area
    109.                     Polygon hull = model.projectConvexHull();
    110.                     if(hull != null){
    111.                         renderModels[i] = new InteractableShape(hull);
    112.                     } else {
    113.                         renderModels[i] = null;
    114.                     }
    115.                 } else {
    116.                     renderModels[i] = null;
    117.                 }
    118.             } else if (target instanceof Renderable) {
    119.                 renderModels[i] = (Renderable)target;
    120.             } else {
    121.                 renderModels[i] = null;
    122.             }
    123.         }
    124.  
    125.         try {
    126.             graphics = pane.getGraphicsContext2D();
    127.             if (graphics != null) {
    128.                 Canvas canvas = graphics.getCanvas();
    129.                 double width = canvas.getWidth();
    130.                 double height = canvas.getHeight();
    131.                 if (width > 0 && height > 0) {
    132.                     //canvas is now 1920 * 1080, so it works.
    133.                     graphics.clearRect(0, 0, width, height);
    134.                     if(renderModels[0] != null){
    135.                         graphics.setStroke(Color.BLUE);
    136.                         renderModels[0].render(graphics);
    137.                     }
    138.                     if(renderModels[1] != null){
    139.                         graphics.setStroke(Color.RED);
    140.                         renderModels[1].render(graphics);
    141.                     }
    142.                 }
    143.             }
    144.         } catch (Exception ex) {
    145.             ex.printStackTrace();
    146.         }
    147.     }
    148. }
    Example use:

    Code (Text):
    1. package transfer;
    2.  
    3. import com.runemate.game.api.hybrid.entities.GameObject;
    4. import com.runemate.game.api.hybrid.input.Mouse;
    5. import com.runemate.game.api.hybrid.region.GameObjects;
    6. import com.runemate.game.api.script.framework.LoopingBot;
    7. import javafx.application.Platform;
    8.  
    9.  
    10. public class SelectRandomTarget extends LoopingBot {
    11.     @Override
    12.     public void onStart(String... arguments) {
    13.         Platform.runLater(() -> new RenderMouseTargets(this));
    14.     }
    15.  
    16.     @Override
    17.     public void onLoop() {
    18.         GameObject rand = GameObjects.newQuery().names("Bank booth").visible().results().random();
    19.         if(rand != null && rand.isVisible()){
    20.             Mouse.move(rand);
    21.         }
    22.     }
    23. }
    24.  
     
    Swatarinaess likes this.

Share This Page

Loading...