Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Structuring code with lots of pauses and states?

Structuring code with lots of pauses and states?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Victorious   (89 posts)  Bio
Date Wed 22 Jun 2016 10:44 AM (UTC)

Amended on Thu 23 Jun 2016 02:39 PM (UTC) by Victorious

Message
Hi,

I'm trying to achieve something like the following.

-- a queue of items that i need to process in-game by using skills/spells 
while (queue is not empty) do
	local head = get first item
	getItemFromContainer() -- may fail
	processItem(head) -- this takes time, can fail, and uses mana. I also want to pause execution when I run out of mana to regenerate
	putItemInContainer() -- this step may fail as well
	removeCompletedItemFromQueue()
end


I've tried implementing it using wait.regexp to pause execution, but I'm finding that this is really messy. Is there a better way? Here's a simplified example. Aside from messiness, it is also difficult to shut it down because it relies on having a stack of functions (i'm not sure if that would even work) which are all paused at the appropriate times.


function mainLoop()
	wait.make(function()
		while true do
			local isQueueEmpty = #queue == 0
			
			if isQueueEmpty then
				print("Queue processing complete.")
				return
			end
			
			local head = queue[1]
			local getItemSuccess, getItemError = getItem(head.item, head.sourceContainer)
			
			if getItemSuccess, == false then
				print(getItemError)
				return
			end
			
			local processSuccess processError = processingLoop(head.item, head.sourceContainer, head.destContainer)
			if processSuccess, == false then
				print(processError)
				return
			end
			
		end -- while
	end) --wait
end --func

-- gets an item from the specified container
-- returns: a boolean indicating success, and an error message on failure
function getItem(item, container)
	-- send get command to mud
	-- use wait.regexp with 5 second timeout to determine success or error
end

-- processes an item
function processingLoop(item, sourceContainer, destContainer)
	while true do
		if regen required then 
			local regenSuccess, errorMsg = regenLoop(item, sourceContainer)
			-- return if regen not successful
		end
		
		-- send commands to mud, use wait.regexp again to determine whether processing is complete or not. Return true once item done
	end	
end

function regenLoop(item, sourceContainer)
	-- send command to put item back in container
	while regenRequired do
		wait.time(2)
	end
	
	-- send command to get item from container
end
Top

Posted by Fiendish   USA  (2,555 posts)  Bio   Global Moderator
Date Reply #1 on Wed 22 Jun 2016 03:39 PM (UTC)

Amended on Wed 22 Jun 2016 03:40 PM (UTC) by Fiendish

Message
Can you edit that post to use code tags and indenting?

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 22 Jun 2016 07:46 PM (UTC)
Message
Template:codetag To make your code more readable please use [code] tags as described here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Victorious   (89 posts)  Bio
Date Reply #3 on Thu 23 Jun 2016 02:43 PM (UTC)
Message
Post edited - my apologies.

I did manual indentation this time; does putting it between the code tags do it automatically?
Top

Posted by Nick Gammon   Australia  (23,173 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 23 Jun 2016 08:00 PM (UTC)
Message
No it doesn't (unlike some more modern sites).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


21,883 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.