Wanderer
Author: Mastaba
Created: 2013-03-07T10:37:50-0500
Edited: 2014-02-21T00:34:29-0500
Denizen Version: 0.9
Views: 93
Downloads: 1282
Likes: 1
Description: Follows city paths or streets randomly in a direction until he can't and starts again in a random direction. Useful in a city or village environment. Adjust the Searchwidth and Searchlength to smaller values if you have narrow or very curvy roads. Adjust the amount of retries if you want the npc to change direction more often.
Let me say this script is lacking some major features, which is why I am already working on a rewrite. Right now, the npc will not purposefully take side streets, nor can he cope with multiple path materials or roads that are not flat. On the other hand, it is very unlikely that he will get permanently stuck. I set him loose in my world and he eventually made it everywhere. Right now he pauses for a little less than a second between walkto commands due to the fact that his calculations are done after he stops moving, rather than while walking. This will change during my rewrite in the next week. I suggest setting his speed slower using /npc speed 0.8, this will make his walking seem smoother for now.
Edit 5/25/13: Quick fix to make this script work again... assigned a default direction.
Edit[mcmonkey] 2/13/14: Patch flag.n:whatever.asint to the newer npc.flag[whatever].asint, to allow running on 0.9 (Fix a missed change on 2/20/14 as well)
Edit[mcmonkey] 2/17/14: Patch npc.location.world to the proper npc.location.world.name
Edit[mcmonkey] 2/20/14: patch runtask -> inject
# for Denizen 0.9
"Wanderer":
type: assignment
default constants:
Searchwidth: 4
Searchlength: 10
Searchlengthmin: 5
Retries: 4
Pathmaterial: 'WOOL'
actions:
on spawn:
- trigger name:click toggle:false
- trigger name:chat toggle:false
- trigger name:proximity toggle:false
- ^flag npc "wander_spawned:true"
- ^flag npc "wander_attention:false"
- ^flag npc "wander_defaultx:<npc.location.x>"
- ^flag npc "wander_defaultz:<npc.location.z>"
- ^flag npc "wander_defaulty:<npc.location.y>"
- ^flag npc "wander_currentdir:north"
- ^flag npc "wander_triescounter:0"
- ^inject "s@wander_choosedirection"
on despawn:
- ^flag npc "wander_spawned:false"
on complete navigation:
- ^if "<flag.n:wander_currentdir>" == "north" inject "s@wander_scannorth"
else if "<flag.n:wander_currentdir>" == "east" inject "s@wander_scaneast"
else if "<flag.n:wander_currentdir>" == "west" inject "s@wander_scanwest"
else if "<flag.n:wander_currentdir>" == "south" inject "s@wander_scansouth"
on begin navigation:
"wander_choosedirection":
type: task
script:
- ^flag npc "wander_triescounter:0"
- ^flag npc "wander_defaultx:<npc.location.x>"
- ^flag npc "wander_defaultz:<npc.location.z>"
- ^flag npc "wander_defaulty:<npc.location.y>"
- ^if "<flag.n:wander_spawned>" == "false" inject "s@wander_despawn"
else inject "s@wander_randomdir"
"wander_randomdir":
type: task
script:
- ^flag npc "wander_triescounter:0"
- ^if <flag.n:wander_currentdir> == 'north' inject "s@wander_wasnorthsouth"
else if <flag.n:wander_currentdir> == 'east' inject "s@wander_waseastwest"
else if <flag.n:wander_currentdir> == 'west' inject "s@wander_waseastwest"
else if <flag.n:wander_currentdir> == 'south' inject "s@wander_wasnorthsouth"
"wander_wasnorthsouth":
type: task
script:
- ^random 2
- ^inject "s@wander_scaneast"
- ^inject "s@wander_scanwest"
"wander_waseastwest":
type: task
script:
- ^random 2
- ^inject "s@wander_scannorth"
- ^inject "s@wander_scansouth"
#North is -z
"wander_scannorth":
type: task
script:
- ^flag npc "wander_currentdir:north"
- ^flag npc "wander_defaultx:<npc.location.x>"
- ^flag npc "wander_defaultz:<npc.location.z>"
- ^flag npc "wander_defaulty:<npc.location.y>"
- ^flag npc "wander_randomxA:<util.random.int[0].to[<cons:Searchwidth>]>"
- ^flag npc "wander_randomzA:<util.random.int[<cons:Searchlengthmin>].to[<cons:Searchlength>]>"
- ^flag npc "wander_randomposxA:<flag.n:wander_defaultx>"
- ^random 2
- ^flag npc "wander_randomposxA:-:<flag.n:wander_randomxA>"
- ^flag npc "wander_randomposxA:+:<flag.n:wander_randomxA>"
- ^flag npc "wander_randomposxA:<npc.flag[wander_randomposxA].asint>"
- ^flag npc "wander_randomposzA:<flag.n:wander_defaultz>"
- ^flag npc "wander_randomposzA:-:<flag.n:wander_randomzA>"
- ^flag npc "wander_randomposzA:<npc.flag[wander_randomposzA].asint>"
- ^flag npc "wander_blockchecky:<flag.n:wander_defaulty>"
- ^flag npc "wander_blockchecky:-:1"
- ^flag npc "wander_blockchecky:<npc.flag[wander_blockchecky].asint>"
- ^if "<location[<flag.n:wander_randomposxA>,<flag.n:wander_blockchecky>,<flag.n:wander_randomposzA>,<npc.location.world.name>].block.material.bukkit_enum>" == '<cons:Pathmaterial>'
inject "s@wander_walknorth"
else if "<flag.n:wander_triescounter>" >= <cons:Retries> inject "s@wander_choosedirection"
else inject "s@wander_tryagainnorth"
"wander_walknorth":
type: task
script:
- ^walkto "location:<flag.n:wander_randomposxA>,<flag.n:wander_defaulty>,<flag.n:wander_randomposzA>,<npc.location.world.name>"
"wander_tryagainnorth":
type: task
script:
- ^flag npc "wander_triescounter:+:1"
- ^flag npc "wander_triescounter:<npc.flag[wander_triescounter].asint>"
- ^inject "s@wander_scannorth"
#East
"wander_scaneast":
type: task
script:
- ^flag npc "wander_currentdir:east"
- ^flag npc "wander_defaultx:<npc.location.x>"
- ^flag npc "wander_defaultz:<npc.location.z>"
- ^flag npc "wander_defaulty:<npc.location.y>"
- ^flag npc "wander_randomzA:<util.random.int[0].to[<cons:Searchwidth>]>"
- ^flag npc "wander_randomxA:<util.random.int[<cons:Searchlengthmin>].to[<cons:Searchlength>]>"
- ^flag npc "wander_randomposxA:<flag.n:wander_defaultx>"
- ^flag npc "wander_randomposxA:+:<flag.n:wander_randomxA>"
- ^flag npc "wander_randomposxA:<npc.flag[wander_randomposxA].asint>"
- ^flag npc "wander_randomposzA:<flag.n:wander_defaultz>"
- ^random 2
- ^flag npc "wander_randomposzA:+:<flag.n:wander_randomzA>"
- ^flag npc "wander_randomposzA:-:<flag.n:wander_randomzA>"
- ^flag npc "wander_randomposzA:<npc.flag[wander_randomposzA].asint>"
- ^flag npc "wander_blockchecky:<flag.n:wander_defaulty>"
- ^flag npc "wander_blockchecky:-:1"
- ^flag npc "wander_blockchecky:<npc.flag[wander_blockchecky].asint>"
- ^if "<location[<flag.n:wander_randomposxA>,<flag.n:wander_blockchecky>,<flag.n:wander_randomposzA>,<npc.location.world.name>].block.material.bukkit_enum>" == '<cons:Pathmaterial>'
inject "s@wander_walkeast"
else if "<flag.n:wander_triescounter>" >= <cons:Retries> inject "s@wander_choosedirection"
else inject "s@wander_tryagaineast"
"wander_walkeast":
type: task
script:
- ^walkto "location:<flag.n:wander_randomposxA>,<flag.n:wander_defaulty>,<flag.n:wander_randomposzA>,<npc.location.world.name>"
"wander_tryagaineast":
type: task
script:
- ^flag npc "wander_triescounter:+:1"
- ^flag npc "wander_triescounter:<npc.flag[wander_triescounter].asint>"
- ^inject "s@wander_scaneast"
#West
"wander_scanwest":
type: task
script:
- ^flag npc "wander_currentdir:west"
- ^flag npc "wander_defaultx:<npc.location.x>"
- ^flag npc "wander_defaultz:<npc.location.z>"
- ^flag npc "wander_defaulty:<npc.location.y>"
- ^flag npc "wander_randomzA:<util.random.int[0].to[<cons:Searchwidth>]>"
- ^flag npc "wander_randomxA:<util.random.int[<cons:Searchlengthmin>].to[<cons:Searchlength>]>"
- ^flag npc "wander_randomposxA:<flag.n:wander_defaultx>"
- ^flag npc "wander_randomposxA:-:<flag.n:wander_randomxA>"
- ^flag npc "wander_randomposxA:<npc.flag[wander_randomposxA].asint>"
- ^flag npc "wander_randomposzA:<flag.n:wander_defaultz>"
- ^random 2
- ^flag npc "wander_randomposzA:-:<flag.n:wander_randomzA>"
- ^flag npc "wander_randomposzA:+:<flag.n:wander_randomzA>"
- ^flag npc "wander_randomposzA:<npc.flag[wander_randomposzA].asint>"
- ^flag npc "wander_blockchecky:<flag.n:wander_defaulty>"
- ^flag npc "wander_blockchecky:-:1"
- ^flag npc "wander_blockchecky:<npc.flag[wander_blockchecky].asint>"
- ^if "<location[<flag.n:wander_randomposxA>,<flag.n:wander_blockchecky>,<flag.n:wander_randomposzA>,<npc.location.world.name>].block.material.bukkit_enum>" == '<cons:Pathmaterial>'
inject "s@wander_walkwest"
else if "<flag.n:wander_triescounter>" >= <cons:Retries> inject "s@wander_choosedirection"
else inject "s@wander_tryagainwest"
"wander_walkwest":
type: task
script:
- ^walkto "location:<flag.n:wander_randomposxA>,<flag.n:wander_defaulty>,<flag.n:wander_randomposzA>,<npc.location.world.name>"
"wander_tryagainwest":
type: task
script:
- ^flag npc "wander_triescounter:+:1"
- ^flag npc "wander_triescounter:<npc.flag[wander_triescounter].asint>"
- ^inject "s@wander_scanwest"
#South
"wander_scansouth":
type: task
script:
- ^flag npc "wander_currentdir:south"
- ^flag npc "wander_defaultx:<npc.location.x>"
- ^flag npc "wander_defaultz:<npc.location.z>"
- ^flag npc "wander_defaulty:<npc.location.y>"
- ^flag npc "wander_randomxA:<util.random.int[0].to[<cons:Searchwidth>]>"
- ^flag npc "wander_randomzA:<util.random.int[<cons:Searchlengthmin>].to[<cons:Searchlength>]>"
- ^flag npc "wander_randomposxA:<flag.n:wander_defaultx>"
- ^random 2
- ^flag npc "wander_randomposxA:-:<flag.n:wander_randomxA>"
- ^flag npc "wander_randomposxA:+:<flag.n:wander_randomxA>"
- ^flag npc "wander_randomposxA:<npc.flag[wander_randomposxA].asint>"
- ^flag npc "wander_randomposzA:<flag.n:wander_defaultz>"
- ^flag npc "wander_randomposzA:+:<flag.n:wander_randomzA>"
- ^flag npc "wander_randomposzA:<npc.flag[wander_randomposzA].asint>"
- ^flag npc "wander_blockchecky:<flag.n:wander_defaulty>"
- ^flag npc "wander_blockchecky:-:1"
- ^flag npc "wander_blockchecky:<npc.flag[wander_blockchecky].asint>"
- ^if "<location[<flag.n:wander_randomposxA>,<flag.n:wander_blockchecky>,<flag.n:wander_randomposzA>,<npc.location.world.name>].block.material.bukkit_enum>" == '<cons:Pathmaterial>'
inject "s@wander_walksouth"
else if "<flag.n:wander_triescounter>" >= <cons:Retries> inject "s@wander_choosedirection"
else inject "s@wander_tryagainsouth"
"wander_walksouth":
type: task
script:
- ^walkto "location:<flag.n:wander_randomposxA>,<flag.n:wander_defaulty>,<flag.n:wander_randomposzA>,<npc.location.world.name>"
"wander_tryagainsouth":
type: task
script:
- ^flag npc "wander_triescounter:+:1"
- ^flag npc "wander_triescounter:<npc.flag[wander_triescounter].asint>"
- ^inject "s@wander_scansouth"
#East is +x
"wander_despawn":
type: task
script:
- chat 'bye'