Improved Speedwalk Function from Other.lua

Post Reply
Jor'Mox
Posts: 1099
Joined: Wed Apr 03, 2013 2:19 am

Improved Speedwalk Function from Other.lua

Post by Jor'Mox » Sun Jun 30, 2013 2:23 am

I rewrote these functions so that they no longer set or use any global variables while still working exactly the same way.
Code: [show] | [select all] lua
--- <b><u>TODO</u></b> speedwalktimer()
function speedwalktimer(walklist, walkdelay)
	send(walklist[1])
	table.remove(walklist, 1)
	if #walklist>0 then
		tempTimer(walkdelay, function() speedwalktimer(walklist, walkdelay) end)
	end
end

--- <b><u>TODO</u></b> speedwalk(dirString, backwards, delay)
function speedwalk(dirString, backwards, delay)
	local dirString		= dirString:lower()
	local walklist			= {}
	local walkdelay			= delay
	local reversedir	= {
		n	= "s",
		en	= "sw",
		e	= "w",
		es	= "nw",
		s	= "n",
		ws	= "ne",
		w	= "e",
		wn	= "se",
		u	= "d",
		d	= "u",
		ni	= "out",
		tuo	= "in"
	}
	if not backwards then
		for count, direction in string.gmatch(dirString, "([0-9]*)([neswudio][ewnu]?t?)") do
			count = (count == "" and 1 or count)
			for i=1, count do
				if delay then walklist[#walklist+1] = direction
				else send(direction)
				end
			end
		end
	else
		for direction, count in string.gmatch(dirString:reverse(), "(t?[ewnu]?[neswudio])([0-9]*)") do
			count = (count == "" and 1 or count)
			for i=1, count do
				if delay then walklist[#walklist+1] = reversedir[direction]
				else send(reversedir[direction])
				end
			end
		end
	end
	if walkdelay then
		speedwalktimer(walklist, walkdelay)
	end
end

User avatar
Vadi
Posts: 4674
Joined: Sat Mar 14, 2009 3:13 pm

Re: Improved Speedwalk Function from Other.lua

Post by Vadi » Sun Jun 30, 2013 11:05 pm

Thank you! I've added it in, and wrote a suite of tests for speedwalk() while at it as well.

(I'll get to the improved db:create next when I can)

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests