Jump to content

A method for interacting with items on tables.


Brainfree

Recommended Posts

Hello,

 

Being that the models for items that are elevated are broken(there heights are wrong), I have come up with a method to advert this pesky problem, it simply builds a Prism around were the model should be, and collides its corner vertices to derive it's midpoint, which in most cases, is were exactly the models center point is. Cool stuff.

 

Example:

 

32kSc.jpg

 

 

 

And the src code: 

package rdpv2;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.rs2.utility.Utilities;

import java.awt.*;

@ScriptManifest(
        author = "Brainfree",
        version = 1.00,
        info = "Generates a point of elevation were a item on table should be, roughly",
        name = "Elevated Point Grabber"
)
public class TableGrabbing extends Script {

    public Point[] getLid(int x, int y, int height, double compression) {
        Point A = getScreenPoint(x, y, 0, 0, height);
        if (A.x == -1) return null;
        Point B = getScreenPoint(x, y, 0, 1, height);
        if (B.x == -1) return null;
        Point C = getScreenPoint(x, y, 1, 1, height);
        if (C.x == -1) return null;
        Point D = getScreenPoint(x, y, 1, 0, height);
        if (D.x == -1) return null;

        Point c = getInterceptionPoint(A.x, A.y, C.x, C.y, B.x, B.y, D.x, D.y);

        Point Ac = getPointOnLineAtDistance(A.x, A.y, c.x, c.y, compression);
        Point Bc = getPointOnLineAtDistance(B.x, B.y, c.x, c.y, compression);
        Point Cc = getPointOnLineAtDistance(C.x, C.y, c.x, c.y, compression);
        Point Dc = getPointOnLineAtDistance(D.x, D.y, c.x, c.y, compression);

        return new Point[]{Ac, Bc, Cc, Dc};
    }

    private Point getScreenPoint(int x, int y, int sx, int sy, int height) {
        return Utilities.getScreenCoordinates(bot,
                (x - client.getMapBaseX() + sx) << 7,
                (y - client.getMapBaseY() + sy) << 7, height);
    }

    public static Point getInterceptionPoint(double x1, double y1, double x2, double y2,
                                             double x3, double y3, double x4, double y4) {
        double dot = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
        if (dot == 0) new Point(-1, -1);
        double X = ((x3 - x4) * (x1 * y2 - y1 * x2) - (x1 - x2) * (x3 * y4 - y3 * x4)) / dot;
        double Y = ((y3 - y4) * (x1 * y2 - y1 * x2) - (y1 - y2) * (x3 * y4 - y3 * x4)) / dot;
        return new Point((int) X, (int) Y);
    }

    public static Point getPointOnLineAtDistance(
            double OriginX, double OriginY,
            double EndLocationX, double EndLocationY,
            double scale) {
        final double LegXLength = (EndLocationX - OriginX);
        final double LegYLength = (EndLocationY - OriginY);
        double HypotenuseLength = Math.sqrt(LegXLength * LegXLength + LegYLength * LegYLength);
        double InverseX = HypotenuseLength * scale * LegXLength / HypotenuseLength;
        double InverseY = (HypotenuseLength * scale * LegYLength) / HypotenuseLength;
        return new Point(
                (int) (InverseX + OriginX),
                (int) (InverseY + OriginY)
        );
    }

    /**
     *
     * Builds a 3D prism around were the ground items model should be in cases
     * of were the model in a elevated height, and is client failed to interpret it.
     *
     * @param tileX The Position X for which the ground item is located.
     * @param tileY The Position Y for which the ground item is located.
     * @param heightMin  The lower elevation height of the prism.
     * @param heightMax The upper elevation height of the prism.
     * @param compression 1.0 = Full Tile bounds length, .75 = 3/4 of the side bounds length
     * @return The midpoint of the prism.
     */

    public Point getElevatedPoint(int tileX, int tileY,
                                  int heightMin, int heightMax,
                                  double compression) {
        Point[] bottom = getLid(tileX, tileX, heightMin, compression);
        if (bottom == null) return null;
        Point[] top = getLid(tileX, tileY, heightMax, compression);
        if (top == null) return null;
        return getInterceptionPoint(
                top[0].x, top[0].y, bottom[2].x, bottom[2].y,
                top[1].x, top[1].y, bottom[3].x, bottom[3].y
        );
    }

    public int onLoop() {
        Point wereTheMidPointOfTheModelOfThisItemShouldBe =
                getElevatedPoint(3232, 3399, 80, 120, .75);

        return 45;

    }
    private void drawLid(Point[] lid, Graphics graphics) {
        for (int i = 0; i < 4; i++) {
            graphics.drawLine(
                    lid[i].x, lid[i].y,
                    lid[i == 3 ? 0 : i + 1].x,
                    lid[i == 3 ? 0 : i + 1].y);
        }
    }

    public void drawBounds(int tileX, int tileY,
                           int heightMin, int heightMax,
                           double compression, Graphics g) {
        Point[] bottom = getLid(tileX, tileY, heightMin, compression);
        if (bottom == null) return;
        Point[] top = getLid(tileX, tileY, heightMax, compression);
        if (top == null) return;
        for (int i = 0; i < 4; i++) {
            g.drawLine(bottom[i].x, bottom[i].y, top[i].x, top[i].y);
        }
        drawLid(bottom, g);
        drawLid(top, g);
        Point center = getInterceptionPoint(
                top[0].x, top[0].y, bottom[2].x, bottom[2].y,
                top[1].x, top[1].y, bottom[3].x, bottom[3].y
        );
        g.setColor(Color.red);
        g.drawString(".", center.x, center.y);
    }

    public void onPaint(Graphics graphics) {
        drawBounds(3232, 3399, 80, 120, .75, graphics);
    }
}

Hope you can find great use of it. :)

  • Like 2
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...