Jump to content

Optimizing Code - Iterating over Objects/NPCS & Inventory Slots


Hel

Recommended Posts

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
Link to comment
Share on other sites

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
  • Like 1
Link to comment
Share on other sites

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
  • Like 1
Link to comment
Share on other sites

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 :)

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...