Jump to content

NPC Spawn Dumper Script


Allen

Recommended Posts

This script will dump all local npcs in the format "npcid x y".
It would benefit anyone making an OS private server(since one's in development maybe they'll come across this and use it).

package com.kinztech.scripts.npcdump;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.*;
import org.osbot.utility.Logger.MessageType;

/**
 * A script to dump local NPC spawns.
 * @author Allen K.
 *
 */
@ScriptManifest(author = "Allen", info = "A script to dump npc spawn locations.", logo = "", name = "Npc Spawn Dump", version = 1.0)
public class NpcDumpScript extends Script {

	/**
	 * A cached list of the npcs, to keep from dumping multiple NPC's of the same reference.
	 */
	ArrayList<NPC> cachedNpcs = new ArrayList<NPC>();
	
	@Override
	public int onLoop() throws InterruptedException {
		for(NPC npc : this.getNpcs().getAll()) {
			if(!cachedNpcs.contains(npc)) {
				addNpcToDump(npc);
			}
		}
		return 600;
	}

	/**
	 * Add an npc to the file & cache it.
	 * @param npc
	 */
	public void addNpcToDump(final NPC npc) {
		try {
			this.logger.log(MessageType.INFO, "Dumping Npc: " + npc.getId() + " Loc: " + npc.getPosition().getX() + "," + npc.getPosition().getY());
			File file = new File("npc_spawns.txt");
			if(!file.exists())
				file.createNewFile();
			BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
			writer.newLine();
			writer.write(npc.getId() + " " + npc.getPosition().getX() + " " + npc.getPosition().getY());
			writer.flush();
			writer.close();
			cachedNpcs.add(npc);
		} catch (Exception e) {
			this.logger.error("ERROR dumping npc spawn for: " + npc.getId() + " Loc: " + npc.getPosition().getX() + "," + npc.getPosition().getY());
			e.printStackTrace();
		}
	}
	
}

JAR Download(open source): http://kinztech.com/downloads/scripts/NpcDumpScript.jar
 
Enjoy wink.png
 
 
EDIT: It will stay updated here: https://github.com/kinztechcom/OSBot/blob/master/src/com/kinztech/scripts/npcdump/NpcDumpScript.java

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

Cool idea, but this is absolutely terrible IO performance wise.

 

Change the array list from npc to string, and just add

npc.getId() + " " + npc.getPosition().getX() + " " + npc.getPosition().getY()

to the array list in addNpcToDump.

 

Then in onFinish (or whatever it's called), write all of those strings to a file.

 

Opening, writing, and closing a file stream for EVERY npc is very, very slow.

  • Like 4
Link to comment
Share on other sites

Cool idea, but this is absolutely terrible IO performance wise.

 

Change the array list from npc to string, and just add

npc.getId() + " " + npc.getPosition().getX() + " " + npc.getPosition().getY()
to the array list in addNpcToDump.

 

Then in onFinish (or whatever it's called), write all of those strings to a file.

 

Opening, writing, and closing a file stream for EVERY npc is very, very slow.

 

I considered doing this and having a UI button on the screen to dump/update the file. The method to write is also not called unless a new NPC appears in the scene that has not already been dumped.

 

Regarding the string suggestion, yes it may be more efficient(although its just a list of references to the object) however, the npc's do walk. A more ideal alteration would be to save the npc index in an ArrayList<Integer> if I were not going to list NPC's. 

 

I also considered instantiating the writer upon start and writing to it when necessary(or on close/stop), and closing the writer on script finish, but I wasnt sure of how efficient that would be vs how it is now. 

i.e. this: https://gist.github.com/kinztechcom/3b0a2fa5cf284fe830bb

edit: project is on github now so if more changes are made i don't have to keep submitting just the class:

https://github.com/kinztechcom/OSBot/blob/master/src/com/kinztech/scripts/npcdump/NpcDumpScript.java

 

 

Edited by Allen
Link to comment
Share on other sites

Newbs, npcs have an index as well. You can use that and not worry about adding the same one 50 times, Index only usually changes between f2p and p2p worlds or sometimes after large updates.

 

I considered doing this and having a UI button on the screen to dump/update the file. The method to write is also not called unless a new NPC appears in the scene that has not already been dumped.

 

Regarding the string suggestion, yes it may be more efficient(although its just a list of references to the object) however, the npc's do walk. A more ideal alteration would be to save the npc index in an ArrayList<Integer> if I were not going to list NPC's. 

 

I also considered instantiating the writer upon start and writing to it when necessary(or on close/stop), and closing the writer on script finish, but I wasnt sure of how efficient that would be vs how it is now. 

i.e. this: https://gist.github.com/kinztechcom/3b0a2fa5cf284fe830bb

edit: project is on github now so if more changes are made i don't have to keep submitting just the class:

https://github.com/kinztechcom/OSBot/blob/master/src/com/kinztech/scripts/npcdump/NpcDumpScript.java

 

Although you're not going to see any difference in performance storing the reference to the NPC object over an Integer object with its index. In fact, it'd actually force to to add to the code because I'd have to reference to the NPC object from NPCS#getLocalNPC(index) when writing.

 

Thanks for the suggestion though, all feedback is appreciated :)

Edited by Allen
Link to comment
Share on other sites

Although you're not going to see any difference in performance storing the reference to the NPC object over an Integer object with its index. In fact, it'd actually force to to add to the code because I'd have to reference to the NPC object from NPCS#getLocalNPC(index) when writing.

 

Thanks for the suggestion though, all feedback is appreciated smile.png

 

I was referring to the global index of the npc, not the local index.

  • Like 1
Link to comment
Share on other sites

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

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