New Speedwalk function

Share your scripts and packages with other Mudlet users.
Post Reply
User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

New Speedwalk function

Post by Oneymus »

While helping Rydekull with an adaptation of the speedwalk function, I decided to flesh it out and offer it up for anyone interested.
Code: [show] | [select all] lua
function speedwalk (path, reverse, delay, start_delay)
	local direction_table = {}
	local reverse_table = {
		n 	= "s",
		e 	= "w",
		s 	= "n",
		w 	= "e",
		nw 	= "se",
		ne 	= "sw",
		se 	= "nw",
		sw 	= "ne",
		u 	= "d",
		d 	= "u"
	}

   for count, direction in rex.gmatch( path, [[([1-9]+)?([newsdu]|\[[\w\W]+?\])]] ) do
		count = (type(tonumber(count)) == "number" and count) or 1
		direction = direction:gsub( "[%[%]]", "" )

      for i=1, count do 
			direction_table[#direction_table+1] = direction
		end
   end

	local current_delay = start_delay or delay
	if not reverse then
		for _, direction in ipairs( direction_table ) do
			if not delay then
				send( direction )
			else
				tempTimer( current_delay, function () send( direction ) end )
				current_delay = current_delay + delay
			end
		end
	else
		for i = #direction_table, 1, -1 do
			local reversed = reverse_table[direction_table[i]] or direction_table[i]

			if not delay then
				send( reversed )
			else
				tempTimer( current_delay, function () send( reversed ) end )
				current_delay = current_delay + delay
			end
		end
	end	
end
Paths are a single string. No spaces or commas or anything. For instance, this was my test string: "2s[nw]3e[jump][hop]4e[row the boat 10 times]2wn[se]". Basically, any cardinal direction (n, e, s, w, d, u) is passed as a single character. Commands are passed in brackets. Optionally, you can prepend a number to any command to have it repeated that many times.

As you can see, in order to handle diagonals, you need to pass them in []'s. You can add anything you'd like to the reverse_table (like in = "out") and commands will be reversed.

The only real addition to the current, built-in speedwalk function is the ability to pass arbitrary commands. It loses some input flexibility, however.

This can easily be adapted to handle any sort of activity, not just walking.

Post Reply