This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
stabby's profile picture

Ability to Queue actions from my program

Started by stabby, 09 January 2014 - 05:08 PM
stabby #1
Posted 09 January 2014 - 06:08 PM
Hello!


I started to work on a program(s) today. Basically I've made a storage system that uses 4 "GPS hosts", 1 server computer and one Turtle.
I'm done with the main idea and I'm just working on simplyfying/debugging the program.

What i want to do is:

Queue orders up so the turtle can collect another item when its done with the first one.

I know this shouldn't be too hard to figure out but right now I'm smashing my head against the wall in frustration.

Server computer and turtle code:

Server http://pastebin.com/RZVgKdYJ

Turtle http://pastebin.com/fZtVd0aE

Ignore the rest of the code, it's not finished.

Thanks for your help!
subzero22 #2
Posted 09 January 2014 - 06:28 PM
just an idea but maybe make it so it writes to a file for the next queue? Like like lets say put cobble in chest, then move somewhere to do something else and so on. Once that queue is done it deletes it from the file and goes on to the next queue. Also as you get more queues it will write them to the file?

I'm not too advanced in cc and still trying to learn more on how to make cc edit and read files. But it's an idea that popped into my head when reading your post. So not sure how hard/easy it will be to implement.
CometWolf #3
Posted 09 January 2014 - 06:30 PM
Add the command arguments (block and units) to a table, then send the commands and remove the table indexes as needed.

local tQueue
function commandQueue(block,units)
  table.insert(tQueue, {
    ["block"] = block,
    ["units"] = units
  })
end
 
function send() -- Sends to storage drone
  if #tQueue <= 0 then --checks queue
    return -- if noting in queue, cancels
  end
  local block = tQueue[1]["block"]
  local units = tQueue[1]["units"]
  local content = loadBlock(block)
  blockId = textutils.unserialize(content)
  blockId[4] = units
  blockId = textutils.serialize(blockId)
  print(blockId)
  rednet.send(sId, blockId)
  table.remove(tQueue,1) --removes queued order from table
end
You will however need to adapt your code to check for queued orders, either periodically with timers or whenever the turtle responds with a not busy. Noramlly you would just use rednet_message events instead of a paralell for this.
gezepi #4
Posted 10 January 2014 - 02:56 AM
From what it sounds like, I've got a pretty similar set up. Requests for items are sent to a queue computer and if the turtle is idle they are sent there, otherwise they are added to the queue. I'm using a table for it.
Here's the important bit of the program:

--[[ Adds a request to the queue.  If its empty, sends the request to retriever ]]
local function addToQueue(thing)
--bug("queue: "..textutils.serialize(queue))
--bug("Turtle status:"..turtleStatus)
if turtleStatus=="idle" then
  bug(thing)
  rednet.broadcast(thing)
else--turtle busy, add to queue
  table.insert(queue, thing)
  printQueue()
end
end
And then when the turtle reports that it is idle again I send it its next job with

rednet.broadcast(table.remove(queue,1))
(table.remove() takes the last item off and returns it)

Here's a link to the whole kit and caboodle if you're interested:
http://www.computerc...ieve-craft-etc/

EDIT
Adding this to your code it would be something like

...
if busy == true then   -- I want to be able to queue x blocks.
  table.insert(queuedBlocks, newBlock)
end
...
f id == sId and msg == "done" then
  busy = false
  rednet.send(sId, table.remove(queuedBlocks, 1))
end
...

table.remove can remove either the first or last element in the table depending on the arguments, I chose First In, First Out.

Oh, and this obviously fails if the chunk is unloaded or the server shut down.
Edited on 10 January 2014 - 02:03 AM
stabby #5
Posted 10 January 2014 - 08:05 AM
Add the command arguments (block and units) to a table, then send the commands and remove the table indexes as needed.
 local tQueue function commandQueue(block,units) table.insert(tQueue, { ["block"] = block, ["units"] = units }) end function send() -- Sends to storage drone if #tQueue <= 0 then --checks queue return -- if noting in queue, cancels end local block = tQueue[1]["block"] local units = tQueue[1]["units"] local content = loadBlock(block) blockId = textutils.unserialize(content) blockId[4] = units blockId = textutils.serialize(blockId) print(blockId) rednet.send(sId, blockId) table.remove(tQueue,1) --removes queued order from table end 
You will however need to adapt your code to check for queued orders, either periodically with timers or whenever the turtle responds with a not busy. Noramlly you would just use rednet_message events instead of a paralell for this.

Thank you!

Took me some time to figure out how you did it but i think i got it know, clever! :)/>

ps. Awesome building program you got there!