Jump to content

World hopping class


Botre

Recommended Posts


package worldhopping.Botrepreneur;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Collections;

import java.util.List;

import java.util.Random;

import org.osbot.rs07.script.Script;

public class WorldHopper {

/**

* @author Botrepreneur

* @Version: 00.10

*/

private List<Integer> future = new ArrayList<Integer>();

private List<Integer> past = new ArrayList<Integer>();

private int worldHops = 0;

private final Script script;

public WorldHopper(final Script script, Collection<World> list) {

this.script = script;

for (World world : list) {

if (world == null) {

continue;

}

this.future.add(world.getWorldNumber());

}

}

public void toRandomWorldNumber() {

int index = new Random().nextInt(future.size());

int world = future.get(index);

this.script.log("Hopping to world: " + world);

this.script.worldHopper.hop(world);

this.worldHops++;

this.future.remove(index);

this.past.add(world);

this.resetList();

}

public void toLowestWorldNumber() {

int world = Collections.min(future);

int index = future.indexOf(world);

this.script.log("Hopping to world: " + world);

this.script.worldHopper.hop(world);

this.worldHops++;

this.future.remove(index);

this.past.add(world);

this.resetList();

}

public void toHighestWorldNumber() {

int world = Collections.max(future);

int index = future.indexOf(world);

this.script.log("Hopping to world: " + world);

this.script.worldHopper.hop(world);

this.worldHops++;

this.future.remove(index);

this.past.add(world);

this.resetList();

}

private void resetList() {

if (future.isEmpty() && !past.isEmpty()) {

List<Integer> temp = this.future;

this.future = this.past;

this.past = temp;

this.past.clear();

}

}

public int getWorldHops() {

return this.worldHops;

}

public void resetWorldHops() {

this.worldHops = 0;

}

}

Edited by Botrepreneur
Link to comment
Share on other sites

This won't work..

 

Test Program

  Reveal hidden contents

 

Output

  Reveal hidden contents

 

 

 

 

Here's the problem..

private void resetList() {
		if (future.isEmpty() && !past.isEmpty()) {
			future = past;
			past.clear();
		}
	}

You are making future reference the same object as past and then clearing it so let's change that first

private void resetList() {
		if (future.isEmpty() && !past.isEmpty()) {
                        List<Integer> temp = future;
			future = past;
                        past = temp;
			past.clear();
		}
	}

Here you add world values to the future list which is fine..

public WorldHopping(boolean p2pworlds, final boolean f2pworlds, final boolean pvpworlds) {
        if (p2pworlds) {
            future.addAll(P2P_WORLDS);
        }
        if (f2pworlds) {
            future.addAll(F2P_WORLDS);
        }
        if (pvpworlds) {
            future.addAll(PVP_WORLDS);
        }
    }

But..

public void toRandomWorldNumber(){
		int i = new Random().nextInt(future.size());
		int w = future.get(i);
                System.out.println(w);
		future.remove(i);
		past.add(i);
		resetList();
	}

In each hopping method you add the index of the world in future to past and then remove the value from the future? Why would you do that? The index is useless now right?

 

I think you meant to add the world value to the past list, like so

public void toRandomWorldNumber(){
		int i = new Random().nextInt(future.size());
		int w = future.get(i);
                System.out.println(w);
		future.remove(i);
		past.add(w);
		resetList();
	}

Now we store the past world values rather than their obsolete indexes in the future list

 

 

Updated program:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @author Botrepreneur
 */

public class WorldHopping {

	private final List<Integer> P2P_WORLDS =  Arrays.asList(301, 302, 303, 304, 305, 306, 309, 310, 311, 312,
		313, 314, 317, 318, 319, 320, 321, 322, 326, 327, 328, 329, 330, 333, 334,
		335, 336, 338, 341, 342, 343, 344, 345, 346, 349, 350, 351, 352, 353, 354, 357,
		358, 359, 360, 361, 362, 365, 366, 367, 368, 369, 370, 373, 374, 375, 376, 377, 378);
	
	private final List<Integer> F2P_WORLDS =  Arrays.asList(308, 316);
	
	private final List<Integer> PVP_WORLDS =  Arrays.asList(325, 337);
	
	private List<Integer> future = new ArrayList<>();
	private List<Integer> past = new ArrayList<>();

	public WorldHopping(boolean p2pworlds, final boolean f2pworlds, final boolean pvpworlds) {
		if (p2pworlds) {
			future.addAll(P2P_WORLDS);
		}
		if (f2pworlds) {
			future.addAll(F2P_WORLDS);
		}
		if (pvpworlds) {
			future.addAll(PVP_WORLDS);
		}
	}
	
	public void toRandomWorldNumber(){
		int i = new Random().nextInt(future.size());
		int w = future.get(i);
                System.out.println(w);
		future.remove(i);
		past.add(w);
		resetList();
	}
	
	public void toLowestWorldNumber(){
		int w = Collections.min(future);
		int i = future.indexOf(w);
		System.out.println(w);
		future.remove(i);
		past.add(w);
		resetList();
	}
	
	public void toHighestWorldNumber(){
		int w = Collections.max(future);
		int i = future.indexOf(w);
		System.out.println(w);
		future.remove(i);
		past.add(w);
		resetList();
	}
	
	private void resetList() {
		if (future.isEmpty() && !past.isEmpty()) {
                        List<Integer> temp = future;
			future = past;
                        past = temp;
			past.clear();
		}
	}
        
        public static void main(String args[])
        {
            WorldHopping hopper = new WorldHopping(true, true, true);
            for(int i = 0; i < 1000; i++)
            {
                hopper.toRandomWorldNumber();
            }
        }
	
}

Output (it ran to 1000 iterations but these are the first few)

  Reveal hidden contents

 

Note: You will have to put the world hopping method back from the API

Edited by Ziy
  • Like 1
Link to comment
Share on other sites

Perhaps something like this?

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class WorldHandler {

	public static void main(String[] arg0) throws InterruptedException {
		WorldHandler wh = new WorldHandler();
		while (true) {
			World w = wh.getNext();
			if (w.getUsed() > 20)
				w.resetUsed();
			System.out.println(w.toString());
			Thread.sleep(100);
		}
	}
	
	public static final Comparator<World> SORT_BY_WORLD_USED_OR_WORLD_NUMBER = new Comparator<World>() {
		@Override
		public int compare(World o1, World o2) {
			if (o1.getUsed() > 0 && o2.getUsed() > 0)
				return Integer.compare(o1.getUsed(), o2.getUsed()); // Compare worlds by how often it's been used
			else
				return Integer.compare(o1.getNumber(), o2.getNumber()); // Compare worlds by world number
		}
	};
	
	public static final int[] WORLD_NUMBERS = { 301, 302, 303, 304, 305, 306,
			309, 310, 311, 312, 313, 314, 317, 318, 319, 320, 321, 322, 326,
			327, 328, 329, 330, 333, 334, 335, 336, 338, 341, 342, 343, 344,
			345, 346, 349, 350, 351, 352, 353, 354, 357, 358, 359, 360, 361,
			362, 365, 366, 367, 368, 369, 370, 373, 374, 375, 376, 377, 378 };

	private final World[] worlds;

	public WorldHandler() {

		// Initialise a new array of World objects using data from our world number cache
		worlds = new World[WORLD_NUMBERS.length];
		for (int i = 0; i < worlds.length; i++)
			worlds[i] = new World(WORLD_NUMBERS[i]);
	}
	
	public World getNext() {
		Arrays.sort(worlds, SORT_BY_WORLD_USED_OR_WORLD_NUMBER);
		World next = worlds[new Random().nextInt(worlds.length / 4)]; // Out of the X amount of worlds, we will pick from the lowest quarter
		next.incrementUsed(); // Our newly chosen world will have it's 'used' counter incremented to indicate we've used it previously
		return next; // Return this world
	}
	
	public static class World {

		private final int num;
		private int used;

		public World(int num) {
			this.num = num;
		}

		@Override
		public String toString() {
			return String.format("World %s used %s", num, used);
		}
		
		@Override
		public boolean equals(Object obj) {
			return obj != null && obj instanceof World && ((World) obj).num == this.num;
		}

		public int getNumber() { return num; }
		public int getUsed() { return used; }
		public void incrementUsed() { used++; }
		public void resetUsed() { used = 0; }
	}
}
Link to comment
Share on other sites

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

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