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

Easier way of writing all this code

Started by xbsktball10x, 28 June 2013 - 12:15 PM
xbsktball10x #1
Posted 28 June 2013 - 02:15 PM
Im writing a code for a banking system using turtles and I am using the minefactory reloaded deep storage unit as the accounts so i can only pull out stacks of 64 at a time(this storage unit can hold nearly 2 billion blocks). I was wondering if there was a way to loop the program to pull out the correct number of stacks in order to fulfill the request for money out of the account. ie. lets say i wanted to pull out 300 dollars then i would have to pull out 5 stacks and then drop 300 then put the remainder back in the account. right now i have it set up so that it checks the number they want and if its with in a certain range then it pulls out the right number of stacks but it is a quite lengthy process the code goes


rednet.open("right")
repeat
    turtle.turnLeft()
until turtle.compareTo(16) == true
local senderId, message, distance = rednet.receive()
if message <= 64 then
    turtle.suck()
elseif message > 64 and <=128 then
    turtle.suck()
    turtle.suck()  
elseif message >128 and <=192 then
    turtle.suck()
    turtle.suck()
    turtle.suck()
elseif message >192 and <=256 then
    for i = 1,4 do
        turtle.suck()
    end   
elseif message >256 and <=320 then
    for i = 1,5 do
        turtle.suck()
    end
elseif message >320 and <= 384 then
    for i = 1,6 do
        turtle.suck()
    end
elseif message >384 and <=448 then
    for i = 1,7 do
        turtle.suck()
    end
elseif message >448 and <= 512 then
    for i= 1,8 do
        turtle.suck()
    end
elseif message >512 and <= 576 then
    for i= 1,9 do
        turtle.suck()
    end
elseif message >576 and <= 640 then
    for i = 1,10 do
        turtle.suck()
    end
end

is there anyway to make this into a loop?
Edited by
Lyqyd #2
Posted 29 June 2013 - 12:11 AM
Split into new topic.

Something like this might do it:


rednet.open("right")
repeat
    turtle.turnLeft()
until turtle.compareTo(16) == true
local senderId, message, distance = rednet.receive()
if message <= 640 then
    for i = 1,math.ciel(message / 64) do
        turtle.suck()
    end
end

This divides the number by 64 (to get the number of stacks, I assume), then rounds the result up to the nearest integer and sucks that many times.
xbsktball10x #3
Posted 29 June 2013 - 05:50 PM
thats awesome thanks i will try it out saves me alot of typing XD
Apfeldstrudel #4
Posted 30 June 2013 - 09:15 AM
That will work, its ceil- not ciel though… Typo