Skip to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Optimizing Code - Iterating over Objects/NPCS & Inventory Slots

Featured Replies

Hi, 

I have two pieces of code which I've been trying to optimize for the last couple of hours, I've looked through the API, I've fiddled in eclipse and I just can't seem to find a logical answer. 

In this first piece, I'm grabbing all the NPCs around the player, checking if they're a fishing spot, & if they are, I'm adding them to an arraylist called fishingSpots. (I'm doing this to detect which kind of fishing spot is the type I want to fish at, where I then iterate over the fishingSpots & check to see if they have the correct interactions)

List<NPC> objects = main.getNpcs().getAll();
ArrayList<NPC> fishingSpots = new ArrayList<NPC>();
for (int i = 0; i < objects.size(); i++) {
	if (objects.get(i).getName().equalsIgnoreCase("fishing spot")) {
		fishingSpots.add(objects.get(i));
	}
		main.randSleep(8, 12); // This is just a void that sleeps(random(min,max)), I used this as an attempt to reduce CPU load, but when iterating over objects, there are some 6000, having a sleep causes incredible delay
}

& Inventory slots -- I'm checking to see if an item is in the slot x, where x is a random whole number from 0-27 inclusive.

 

int i = (int) Math.round(Math.random() * 27);
		while (main.getInventory().getItemInSlot(i) == null) {
			i = (int) Math.round(Math.random() * 27);
			main.randSleep(8, 12);
		}

These loops cause an incredible amount of CPU load, jumping from some 8% up to, what I've seen, 60% load.

Any and all help would be appreciated. smile.png

Edited by viri

  • Author

I'm unsure if you're just upset with me because I named the list of NPCs "objects" (Originally was using bank booths, hence the name)  or if it actually bothers the CPU?

CNF Formula? Never heard of that before. Is this something I should be investing my time into researching?

I'm unsure if you're just upset with me because I named the list of NPCs "objects" (Originally was using bank booths, hence the name) or if it actually bothers the CPU?

CNF Formula? Never heard of that before. Is this something I should be investing my time into researching?

Use Filters to filter npcs:

Filter class: http://osbot.org/api/org/osbot/rs07/api/filter/Filter.html

Filter API: http://osbot.org/api/org/osbot/rs07/api/filter/FilterAPI.html

Example usage:

List<NPC> fishingSpots = getNpcs().filter(getNpcs().getAll(), new Filter....);
There is also a .singleFilter method that will return a single NPC

Edited by Explv

  • Author

Alright, thankyou both for your help :)

Hi, 

I have two pieces of code which I've been trying to optimize for the last couple of hours, I've looked through the API, I've fiddled in eclipse and I just can't seem to find a logical answer. 

In this first piece, I'm grabbing all the NPCs around the player, checking if they're a fishing spot, & if they are, I'm adding them to an arraylist called fishingSpots. (I'm doing this to detect which kind of fishing spot is the type I want to fish at, where I then iterate over the fishingSpots & check to see if they have the correct interactions)

List<NPC> objects = main.getNpcs().getAll();
ArrayList<NPC> fishingSpots = new ArrayList<NPC>();
for (int i = 0; i < objects.size(); i++) {
	if (objects.get(i).getName().equalsIgnoreCase("fishing spot")) {
		fishingSpots.add(objects.get(i));
	}
		main.randSleep(8, 12); // This is just a void that sleeps(random(min,max)), I used this as an attempt to reduce CPU load, but when iterating over objects, there are some 6000, having a sleep causes incredible delay
}

& Inventory slots -- I'm checking to see if an item is in the slot x, where x is a random whole number from 0-27 inclusive.

 

int i = (int) Math.round(Math.random() * 27);
		while (main.getInventory().getItemInSlot(i) == null) {
			i = (int) Math.round(Math.random() * 27);
			main.randSleep(8, 12);
		}

These loops cause an incredible amount of CPU load, jumping from some 8% up to, what I've seen, 60% load.

Any and all help would be appreciated. smile.png

 

For the last loop, hard to understand what you're trying to accomplish. It seems you're trying to wait until you have caught a fish? If so, you method currently would continuously loop until it found a non-empty inventory slot, then I assume go right back into that loop again. This will ofc use a lot of CPU.

 

Try using a conditional sleep:

int lastSlots = getInventory().getEmptySlots();
new ConditionalSleep(5000) { // Sleeps are in milliseconds, sleep for 5 secs
 
   @0verride
   public boolean condition() throws InterruptedException {
      return getInventory().getEmptySlots() == lastSlots;
   }
};

Edited by Lemons

  • Author

What I was trying to do with that was generate a random whole number from 0 to 27 inclusive (the inventory slots)

Then check if that random number (random item slot) had an item in it, if it didn't, it entered a while loop & kept regenerating a random inventory slot until there was an item in the randomly generated slot number.

I'd rather if it wasn't spoon fed to me either, I'm actually interested in this and would prefer to just get a directional nudge, so I can still benefit from this, thanks :)

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.