11 posts
Posted 25 June 2013 - 12:31 AM
The problem:
I presume this is a simple question that I just haven't come across the way to do it yet so my question is:
I am trying to create a system that will do a task but then be in "notFree" mode while that task is being done (notFree mode means 60 seconds wait basically) but during that "notFree" mode time I need the computer/turtle to send a message to another computer/turtle basically in code saying "Hey, I am currently doing this task so you do it or send a message to the computer saying try to send it again in this amount of time"
The way I am currently doing it:
The following code is very cut down as I have just typed it out, I have if statements and while loops keeping the program running constantly and breaking out of loops when certain parameters are not met.
rednet.receive() –He will wait forever to receive a message
turtle.suckUp()
turtle.dropDown() – He will suck from a chest and place in a machine below
if rednet.receive(60) then
rednet.send(101, "items") – If he receives another message within 60 seconds of receiving the first message he will send a message to ID 101 with the text "items" meaning I can't do this job can you.
The above is on 5 different machines as buffers but every time I send a message to number one if one, two, three and four are blocked it has to go through all of them before reaching number 5 restarting all of their timers!
So lets say I want 4 stacks of items processed, and then after 59 seconds I want another stack processing turtle one will be in notFree mode so he will ask two (two will ask three, three will ask four and finally four will ask five) after all of this even though turtle one was on 59 his timer will get reset back to 60 because I have no way of knowing how much time had elapsed or how much time was left before finishing the 60 seconds.
What I have tried?
I firstly tried to use sleep()/os.sleep() however when in this mode I cannot receive any rednet messages therefore it does not work.
I tried using the os.startTimer() but I have no idea how that works and do not know if that is what I need.
What is this for?
So far this is just for a furnace system that will suck items out of an ender chest as soon as the arrive (I will have a detection for items in there, then if that is the case I will send a rednet message to the turtles telling them they have work to do) I am using 5 turtles for every operation I do, furnace, macerator, everything, so 5 is my number to work with.
The idea is to have items processed by turtles done without any piping as I personally find it a little cheaty (I am currently using ender chests which I also find cheaty but they will be out of the end result, it is just something I need to use for them to share inventory)
Side note, if anyone has got a completely revamped idea on how to do this please don't hesitate to tell me as I am aware going through each turtle is a little inefficient but I wanted to make sure I was using them all as and when they are free.
Thanks in advanced!
7508 posts
Location
Australia
Posted 25 June 2013 - 12:58 AM
This is probably one of the simplest methods.
local function notFree()
local timeLeft = 60 --# define the wait time
local refresh = os.startTimer(1) --# start our timer
repeat --# start loop
local event, param = os.pullEvent() --# wait for event
if event == "rednet_message" then --# if it was a rednet message
rednet.send(param, "Do it yourself or send again in "..timeLeft.." seconds" ) --# tell the sending computer what to do
elseif event == "timer" and param == refresh then --# if the event was a timer and the timer was the one we started before
refresh = os.startTimer(1) --# restart the timer
timeLeft = timeLeft - 1 --# subtract 1 second
end
until timeLeft == 0 --# loop until there is no time left
end
Then just call the notFree function in your program.
EDIT: another, simpler, method
local function notFree()
local endTime = os.clock() + 60 --# figure out when it should end... os.clock is how long the computer has been running for, for a minecraft world time you use os.time
while os.clock() < endTime do
local event, param = os.pullEvent()
if event == "rednet_message" then
rednet.send(param, "Do it yourself or send again in "..(endTime - os.clock()).." seconds" ) --# this will be a more accurate time, supplying milliseconds as well
end
end
end
11 posts
Posted 25 June 2013 - 01:22 AM
This is probably one of the simplest methods.
local function notFree()
local timeLeft = 60 --# define the wait time
local refresh = os.startTimer(1) --# start our timer
repeat --# start loop
local event, param = os.pullEvent() --# wait for event
if event == "rednet_message" then --# if it was a rednet message
rednet.send(param, "Do it yourself or send again in "..timeLeft.." seconds" ) --# tell the sending computer what to do
elseif event == "timer" and param == refresh then --# if the event was a timer and the timer was the one we started before
refresh = os.startTimer(1) --# restart the timer
timeLeft = timeLeft - 1 --# subtract 1 second
end
until timeLeft == 0 --# loop until there is no time left
end
Then just call the notFree function in your program.
EDIT: another, simpler, method
local function notFree()
local endTime = os.clock() + 60 --# figure out when it should end... os.clock is how long the computer has been running for, for a minecraft world time you use os.time
while os.clock() < endTime do
local event, param = os.pullEvent()
if event == "rednet_message" then
rednet.send(param, "Do it yourself or send again in "..(endTime - os.clock()).." seconds" ) --# this will be a more accurate time, supplying milliseconds as well
end
end
end
Thank you! I will try it out and tell you the outcome. If it works with my program I will add a [SOLVED] tag to the title to stop any confusion and to help others find this post if they are having trouble.
11 posts
Posted 25 June 2013 - 01:46 AM
Okay, that was fantastic, setting it relative to something that will be changing anyway! Genius I tell you, honestly thank you!
I have one more quick question/addon before I 'close' this post, as I said in the original post I have 5 turtles lined up so they will pass the message on to each other respectively, I have a question that you may or may not know the answer to: If the very last turtle sends his message to the very first turtle and the first turtle is still not finished it passes it on infinitely until one of the turtles is free to do the task (I have checked and it does do that) my concern is; will this cause server lag that you are aware of? I mean it shouldnt from what I understand it is just a value being passed through machine to machine but I thought I should just check if it is something to beware of.
I also thought best to keep this here because it is a mini question and really only respective to those who understand the circumstances under which the question is being asked ;)/>
Thanks again man!
8543 posts
Posted 25 June 2013 - 02:14 AM
Please continue to use one topic for further questions on one piece of code. It is easier to consolidate the answers so part of a question won't be answered multiple times in multiple places, as well as making it easier for answer posters to follow the progress of development and therefore address their answers with the current context of the code a d some knowledge of the direction in which it is heading.
7508 posts
Location
Australia
Posted 25 June 2013 - 12:01 PM
Okay, that was fantastic, setting it relative to something that will be changing anyway! Genius I tell you, honestly thank you!
no problems, always here to help :)/>
I have one more quick question/addon before I 'close' this post, as I said in the original post I have 5 turtles lined up so they will pass the message on to each other respectively, I have a question that you may or may not know the answer to: If the very last turtle sends his message to the very first turtle and the first turtle is still not finished it passes it on infinitely until one of the turtles is free to do the task (I have checked and it does do that) my concern is; will this cause server lag that you are aware of? I mean it shouldnt from what I understand it is just a value being passed through machine to machine but I thought I should just check if it is something to beware of.
well the turtle that is doing something will only reply to the sending turtle when it receives a message, well actually it will send a response to anything that sends it a message, computers and all :)/> if your turtle/computer at the other end respects it's response and tries another turtle then that is good!!! however if it doesn't, and you're using an older version of CC, then yes, it can cause a server deadlock. It is probably a good idea to try and setup a maximum send time, i.e. the computer has to wait before sending another message, that way you can avoid this problem irrelevant of the CC version :)/>
I also thought best to keep this here because it is a mini question and really only respective to those who understand the circumstances under which the question is being asked ;)/>
definitely, as Lyqyd stated, please try and keep all questions even remotely related to the same piece of code in the same topic, it make it easier for us who answer your questions to keep up with your progress and problems so we can better help you next time. The most annoying thing is when someone keeps posting a new topic for each question they have that is even remotely related to the same code.
11 posts
Posted 25 June 2013 - 12:31 PM
Okay, that was fantastic, setting it relative to something that will be changing anyway! Genius I tell you, honestly thank you!
no problems, always here to help :)/>
I have one more quick question/addon before I 'close' this post, as I said in the original post I have 5 turtles lined up so they will pass the message on to each other respectively, I have a question that you may or may not know the answer to: If the very last turtle sends his message to the very first turtle and the first turtle is still not finished it passes it on infinitely until one of the turtles is free to do the task (I have checked and it does do that) my concern is; will this cause server lag that you are aware of? I mean it shouldnt from what I understand it is just a value being passed through machine to machine but I thought I should just check if it is something to beware of.
well the turtle that is doing something will only reply to the sending turtle when it receives a message, well actually it will send a response to anything that sends it a message, computers and all :)/> if your turtle/computer at the other end respects it's response and tries another turtle then that is good!!! however if it doesn't, and you're using an older version of CC, then yes, it can cause a server deadlock. It is probably a good idea to try and setup a maximum send time, i.e. the computer has to wait before sending another message, that way you can avoid this problem irrelevant of the CC version :)/>
I also thought best to keep this here because it is a mini question and really only respective to those who understand the circumstances under which the question is being asked ;)/>
definitely, as Lyqyd stated, please try and keep all questions even remotely related to the same piece of code in the same topic, it make it easier for us who answer your questions to keep up with your progress and problems so we can better help you next time. The most annoying thing is when someone keeps posting a new topic for each question they have that is even remotely related to the same code.
Hey thanks for all the help guys!
What I ended up doing for infinite loop of passing the task on to another turtle until one gets free is: Started to track how many times the computer sent the message if it was divisible by 5 with no remainder then it is either 5 or 0, (the number either ends in 5 or 0 meaning it has been sent a multiple of 5 times) if that is the case the computer just waits for the timer to run out on the first turtle (hard coded not using variables, as I know how long it takes) and then proceeds to send the amount of times I told it to! This should cut down on processes as for like 50 seconds he isnt doing anything and the code is probably looping for 1-2 seconds rather than a minute.
Thanks for the help guys, very nice to have that extra help when you hit a brick wall!
Going to 'close' the topic now, just so people don't waste their time trying to answer the question, it should become clear that it has been solved from the title.
7508 posts
Location
Australia
Posted 25 June 2013 - 12:37 PM
the number either ends in 5 or 0 meaning it has been sent a multiple of 5 times
Use the modulo/modulus operator…
if num % 5 == 0 then
print("it is divisible by 5")
end
this works as the modulus operator will divide the number on the left, by the number on the right, until it can no longer divide the number, and the remainder it will return, we are checking for 0, so it will only return when it is divisible by 5….
a practical example:9 % 4, four goes into nine 2 times with a remainder of 1, so 9 % 4 == 1
12 % 4, four goes into twelve 3 times with a remainder of 0, so 12 % 4 == 0
3 % 6, six goes into three no times with a remainder of 3, so 3 % 6 == 3
-3 % 6, six goes into negative three no times with a remainder of -3, so 3 % 6 == -3
11 posts
Posted 25 June 2013 - 07:35 PM
the number either ends in 5 or 0 meaning it has been sent a multiple of 5 times
Use the modulo/modulus operator…
if num % 5 == 0 then
print("it is divisible by 5")
end
this works as the modulus operator will divide the number on the left, by the number on the right, until it can no longer divide the number, and the remainder it will return, we are checking for 0, so it will only return when it is divisible by 5….
a practical example:9 % 4, four goes into nine 2 times with a remainder of 1, so 9 % 4 == 1
12 % 4, four goes into twelve 3 times with a remainder of 0, so 12 % 4 == 0
3 % 6, six goes into three no times with a remainder of 3, so 3 % 6 == 3
-3 % 6, six goes into negative three no times with a remainder of -3, so 3 % 6 == -3
You seemed to have mis-understood me, sorry if I was unclear but that is the bit of code I was using anyway to track if it was divisible by 5, I think you may of thought I was asking how to do it but that wasnt the case, either way thanks! ;)/>
7508 posts
Location
Australia
Posted 25 June 2013 - 10:51 PM
You seemed to have mis-understood me, sorry if I was unclear but that is the bit of code I was using anyway to track if it was divisible by 5, I think you may of thought I was asking how to do it but that wasnt the case, either way thanks! ;)/>
Nah I knew you weren't asking how to do it, but the way you said what you were doing it sounded to me that you weren't doing it this way, so I figured I'd tell you about it just in case.