Making an NPC walk from Lumbridge to Varrock?
Posted: Wed Mar 08, 2023 2:33 pm
Now that I have a basic script going that spawns a few NPC instances, I'd like to make them move about. I've made two attempts, with their own problems.
1. Use - The NPC doesn't path beyond the first region (tile? area?) that they're currently in. Using locDestination, which should take them to Varrock, has the NPCs walk around Lumbridge castle and stand facing the castle wall on the exact same tile.
2. Copy the functions from the Adventurer bot - Nothing happens when I added this. No errors, the NPCs just path about their spawn point as they would without being made to pathfind to a given location.
For the second method, here's the code (redid the function names, and changed the entity type to NPC):
Code: Select all
val locSpawn = Location.create(3225, 3220, 0) //Lumbridge
val locDestination = Location.create(3211, 3386, 0) //Varrock
Code: Select all
NPC.configureMovementPath(location)
2. Copy the
Code: Select all
randomWalkTo()
For the second method, here's the code (redid the function names, and changed the entity type to NPC):
Code: Select all
/**
* @param loc the location to walk to.
* @param radius tiles around the location the bot could walk to.
* @author Kermit
*/
fun npcWalkToWithinRadius(npc: NPC, loc: Location, radius: Int) {
if(!npc.walkingQueue.isMoving) {
GlobalScope.launch {
val newLoc = loc.transform(
RandomFunction.random(radius,-radius),
RandomFunction.random(radius,-radius), 0)
npcWalkToIterator(npc, newLoc)
}
}
}
/**
* The iterator for long-distance walking. Limited by doors and large obstacles like mountains.
* @param loc the location to find a path to.
* @author Ceikry
*/
private fun npcWalkToIterator(npc: NPC, loc: Location){
var diffX = loc.x - npc.location.x
var diffY = loc.y - npc.location.y
while(!npc.location.transform(diffX, diffY, 0).withinDistance(npc.location)) {
diffX /= 2
diffY /= 2
}
GameWorld.Pulser.submit(object : MovementPulse(npc, npc.location.transform(diffX, diffY, 0), Pathfinder.SMART) {
override fun pulse(): Boolean {
return true
}
})
}
fun FyTySpawnNPC()
{
for (iCounter in 0..2)
{
listNPCs.add(spawnNPC(1, locSpawn, Direction.NORTH, false, true, false))
listTickCounters.add(RandomFunction.random(1, 10))
npcWalkToWithinRadius(listNPCs[iCounter], locDestination, 5)
}
}
/code]
My guess is that the code for making bots pathfind doesn't apply to NPC entities. So, what's the approach here?