Jump to content

Combat xp tracker from xp-drops


maximiliano1

Recommended Posts

Hi, I've had problem the ExperienceTracker not getting the combat xp fastly enough, so I've decided to implement my own combat xp tracker which tracks the experience via using the widgets, 
INSTRUCTIONS:

In order to use the Xp drop tracker  change the xp drop settings to following:

1. change the xp text to pink 2. don't use combined xp drop

package State;

import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.util.ExperienceTracker;
import org.osbot.rs07.script.MethodProvider;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class XpDropWidgetStateManager {
    MethodProvider mp;
    int latest_xp_drop = 0;
    ExperienceTracker range_xp_tracker;
    int gained_xp = 0;
    public ArrayList<Integer> values_widget = new ArrayList<>();
    public ArrayList<Integer> values_skilltracker = new ArrayList<>();
    int num_drops_to_save = 500;

    public XpDropWidgetStateManager(MethodProvider mp) {
        this.mp = mp;
    }

    public void initializeXPTracker() {
        this.range_xp_tracker = mp.getExperienceTracker();
        this.range_xp_tracker.start(Skill.RANGED);
        this.gained_xp = this.range_xp_tracker.getGainedXP(Skill.RANGED);
        latest_xp_drop = 0;
    }

    private int RS2WidgetHeightComparator(RS2Widget lhs, RS2Widget rhs) {
        int lhs_height = lhs.getPosition().y;
        int rhs_height = rhs.getPosition().y;
        return Integer.compare(rhs_height, lhs_height);
    }
    private double getYDistance(RS2Widget src, RS2Widget target) {
        return Math.abs(src.getPosition().getY() - target.getPosition().getY());
    }
    private RS2Widget getMyWidget() {

        List<RS2Widget> combat_icon_widgets = mp.getWidgets().filter(new Filter<RS2Widget>() {
            @Override
            public boolean match(RS2Widget rw) {
                return rw.getRootId() == 122 && rw.isVisible() && rw.getSpriteIndex1() != 203
                        && rw.getSpriteIndex1() != -1;
            }
        });

        combat_icon_widgets.sort(this::RS2WidgetHeightComparator);

        if (combat_icon_widgets.size() > 0) {
            RS2Widget lowest_combat_icon_widget = combat_icon_widgets.get(0);

            List<RS2Widget> xp_drop_widgets = mp.getWidgets().filter(rw -> rw.getRootId() == 122
                    && rw.isVisible() && rw.getTextColor() == 16763080).stream().filter(o ->
                            getYDistance(o, lowest_combat_icon_widget) < 1.0)
                    .collect(Collectors.toList());

//            mp.log(String.format("Size of xp widgets: %d", xp_drop_widgets.size()));
            if (xp_drop_widgets.size() == 1) {
                return xp_drop_widgets.get(0);
            }
        }
        return null;
    }

    public void print_array(ArrayList<Integer> array, String prefix) {
        mp.log("--------");
        mp.log(prefix);
        String string = "";
        for (int elem : array) {
            string += String.valueOf(elem) + ", ";
        }
        mp.log(string);
    }

    private boolean isMyWidgetWorking(RS2Widget widget) {
        return widget != null && widget.isVisible();
    }

    public void saveToFile(String fileName, ArrayList<Integer> list) throws FileNotFoundException {
        PrintWriter pw = new PrintWriter(new FileOutputStream(fileName));
        for (int club : list)
            pw.println(club);
        pw.close();
    }


    public /*JSONObject*/ int getXPDropInfo() {
        int dmg = 0;
        RS2Widget widget = getMyWidget();
        if (isMyWidgetWorking(widget)) {
            String message = widget.getMessage();
            dmg = Integer.parseInt(message) / 4;
//            mp.log(dmg);
            int curr_xp_drop = dmg * 4;
            if (latest_xp_drop != curr_xp_drop) {
                this.values_widget.add(curr_xp_drop);
                latest_xp_drop = curr_xp_drop;
            }
            int xp_gained_2 = this.range_xp_tracker.getGainedXP(Skill.RANGED);
            if (this.gained_xp != xp_gained_2) {
                num_drops_to_save += 1;
                int xp_change = xp_gained_2 - this.gained_xp;
                this.gained_xp = xp_gained_2;
                this.values_skilltracker.add(xp_change);

            }
            if (num_drops_to_save % 5 == 0) {
                this.print_array(this.values_widget, "Widget values");
                this.print_array(this.values_skilltracker, "SkillTracker values");
            }

        }
        return dmg;
    }
}

 

This code is using ExperienceTracker for debug purposes, so feel free to delete it/refractor the code to your needs :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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