Walking script help?

Posted by Zombero on Sun 05 Sep 2004 09:50 PM — 14 posts, 51,445 views.

#0
I have little to no scripting experience so I was wondering if someone could help me make a fairly simple script. I'm trying to make a script that will go in a random direction (east west south north) other than the direction that it came from. It would also need to somehow recognize a deadend and go in that case go back the direction it came from. The exits in the world show up between the colon and ] in [exits: ] and are seperated by commas.
example: [exits: west, north, south, east]
It would also need to go down when down is an availble exit and stop the script on a certain output from the world. Can anyone provide me with or help me make a script like this for Mush Client? Thanks.
USA #1
Trigger: [exits: *]
Label: Walk
Script: Walk
[exits: west, north, south, east]

in a *.vbs file

Dim LastDir as String

Sub Walk(a,b,directions)
dim s as string

s = split(directions,", ")
getnewdir = rnd * ubound(s)
if s(getnewdir) <> LastDir then
world.send s(getnewdir)
LastDir = s(getnewdir)
end if
end sub

or something similar to that.

To tired to think to hard....worked to much.
#2
Okay, it took a bit of tweeking but I got that to work :). Thanks.
Amended on Mon 06 Sep 2004 07:20 AM by Zombero
USA #3
The briliant minds and MS, in order to make things easier for them, rather than the coder, chose to impliment variables in the script as type 'variant'. This didn't need to be a major disaster, except that to simplify things even farther, they have the variables 'auto-cast' themselves into the new type. If you do:

a = 1
a = a & "g"

then instead of an error, since the internal type of 'a' is in fact integer, it instead converts 'a' into a string type internally and appends the new value. Oops! This gets even more fun when supposedly perfectly valid values like " 32" fail to cast to integer, bacause the extra space they contain automatically causes them to be cast to string. The following 'will' cause an error:

a = " 32"
a = a - 3

If you use '+' it simply assumes you meant & and concatinates them. Basically there are no explicit types and some fun things are missing, like conversion of any integer value into a date, though you can do the reverse. Why they left that one out I am not sure. It is bloody hard to add X days, hours, etc. to a date when the function doesn't exist. :( In any case, when it isn't 100% sure what you intended, it guesses and recasts to what it 'thinks' you wanted to have. If you want explicit casting, try something other than VBScript. :p It is brain dead in some respects imho, but it is still less cryptic than the alternatives in a lot of ways. You just have to learn to live with the stuff they chose to cut out of it to turn it into a script system.

Of course, VB does these odd things with variables too, but only if you intentionally leave off the 'as <type>' for use with OLE automation.
Amended on Mon 06 Sep 2004 07:52 AM by Shadowfyr
#4
Anyone know how to do this for Lua script??
Australia Forum Administrator #5
This trigger would do it, although it doesn't remember which way you walked, it shows the general idea ...



<triggers>
  <trigger
   enabled="y"
   match="[exits: *]"
   send_to="12"
   sequence="100"
  >
  <send>
-- split exits into a table at the comma

exits = {}

for w in string.gmatch ("%1", "[^,]+") do
  table.insert (exits, w)
end -- for

-- choose one randomly
which = (math.floor (MtRand () * table.getn (exits))) + 1

-- go that way
Send (Trim (exits [which]))

</send>
  </trigger>
</triggers>







[EDIT] Edited on 7 May 2008 to change string.gfind to string.gmatch. Version 3.80 of MUSHclient switched from Lua version 5.0 to Lua version 5.1, which requires this change.
Amended on Tue 06 May 2008 09:36 PM by Nick Gammon
#6
i'm just bouncing between 2 rooms.. and not moving in a random direction...
Australia Forum Administrator #7
What, it randomly just sends east, west, east, west, like that? And you keep bouncing back and forth? Strange.
#8
ok my exits are: for example..
[Exits: south, north, east]

i don't know what the trigger does but i see
south north east echoed below the [Exits: ... ...]

and so i will go south,
but if the next room's exits are
[Exits: north, south, east]

then what is echoed is: north south east

and i go north.. the same thing happens in the first room and so i end up going north and south
Australia Forum Administrator #9
How long did you try this for? Certainly, if you move randomly you may end up back where you came. If there are 3 exits you have a 1 in 3 chance of going back. However with a bit more complexity you can avoid that.


<triggers>
  <trigger
   enabled="y"
   match="[Exits: *]"
   send_to="12"
   sequence="100"
  >
  <send>
-- table of inverse directions

inverse = {
  north = "south",
  south = "north",
  east = "west",
  west = "east",
  up = "down",
  down = "up",
  ne = "sw",
  sw = "ne",
  se = "nw",
  nw = "se",
  } -- end of table of inverse directions

-- split exits into a table at the comma

exits = {}

for w in string.gmatch ("%1", "[^,]+") do
  table.insert (exits, w)
end -- for

number_of_exits = table.getn (exits)

repeat

  -- choose one randomly
  which = (math.floor (MtRand () * number_of_exits)) + 1

  direction = Trim (exits [which])
 
until direction ~= inverse [last_direction] or number_of_exits == 1

-- go that way
Send (direction)

-- remember it so we don't go back
last_direction = direction

</send>
  </trigger>
</triggers>



What this version does is remember the last direction it sent, and then make sure it doesn't follow it by the inverse direction (see table above).

In other words, if it sends "north" it won't follow it by "south" next time.

However there is a test for if the number of exits is 1, in which case you are in a dead-end room, and the only way out is the way you came.




[EDIT] Edited on 7 May 2008 to change string.gfind to string.gmatch. Version 3.80 of MUSHclient switched from Lua version 5.0 to Lua version 5.1, which requires this change.
Amended on Tue 06 May 2008 09:36 PM by Nick Gammon
#10
How do I go about using this? Do I put it in as a trigger or a script? If it is a script, what language is it? I'm somewhat new to all this... Thank you
Australia Forum Administrator #11
It is a trigger with scripting in the send box.

See http://www.mushclient.com/pasting for how to use the example there.

The language is Lua, which is the default scripting language.
#12
For reference, if you want to use this script, make sure to replace string.gfind with 'string.gmatch'. Calling string.gfind will raise an error.
Australia Forum Administrator #13
Thanks for the notification. I have amended the posts above to reflect this. The original posts were for Lua 5.0, however Lua 5.1 has been around for a while now.