Jump to content




Easier way of writing all this code


  • You cannot reply to this topic
3 replies to this topic

#1 xbsktball10x

  • Members
  • 26 posts

Posted 28 June 2013 - 01: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, 28 June 2013 - 11:09 PM.
added code tags


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 28 June 2013 - 11:11 PM

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.

#3 xbsktball10x

  • Members
  • 26 posts

Posted 29 June 2013 - 04:50 PM

thats awesome thanks i will try it out saves me alot of typing XD

#4 Apfeldstrudel

  • Members
  • 161 posts

Posted 30 June 2013 - 08:15 AM

That will work, its ceil- not ciel though... Typo





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users