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

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(i);
		resetList();
	}
	
	public void toLowestWorldNumber(){
		int w = Collections.min(future);
		int i = future.indexOf(w);
		System.out.println(w);
		future.remove(i);
		past.add(i);
		resetList();
	}
	
	public void toHighestWorldNumber(){
		int w = Collections.max(future);
		int i = future.indexOf(w);
		System.out.println(w);
		future.remove(i);
		past.add(i);
		resetList();
	}
	
	private void resetList() {
		if (future.isEmpty() && !past.isEmpty()) {
			future = past;
			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

302
309
354
377
333
310
343
367
368
327
370
312
346
361
303
311
326
337
306
321
360
376
305
329
304
336
359
301
374
351
365
338
320
350
358
349
353
316
378
317
366
335
369
344
308
375
330
334
313
319
345
341
325
322
362
314
318
373
328
342
357
352
Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
	at java.util.Random.nextInt(Random.java:300)
	at WorldHopping.toRandomWorldNumber(WorldHopping.java:38)
	at WorldHopping.main(WorldHopping.java:76) 

 

 

 

 

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)

327
345
326
360
377
349
313
321
328
311
333
368
344
309
308
334
318
354
325
312
301
310
343
317
365
361
336
369
302
342
362
303
338
341
352
366
367
306
319
376
353
314
316
359
346
329
370
330
351
320
373
305
374
322
304
358
378
350
357
337
375
335
361
365
357
312
351
343
368
333
377
308
303
349
378
376
341
366
344
326
350
304
320
338
329
345
309
317
342
354
322
375
314
325
367
318
321
369
334
352
353
362
346
373
313
306
330
359
358
336
327
337
316
311
302
301
374
319
370
305
360
328
310
335
308
327
354
358
375
368
336
310
367
301

 

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