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

Need help with a program

Started by Vampire142, 11 February 2016 - 02:09 AM
Vampire142 #1
Posted 11 February 2016 - 03:09 AM
Hello, I need some help with a program i tried to code

This program have for mission to sort "Ore berries" on a FTB server (For those who do not know, it's like a nugget but not only of gold), and

The code is here (some part in french ;)/> )


-- Variable
local Berry = {1,2,3,4,5}
-- Fonctions
function Trie()
for j = 1,5 do
  if turtle.compareTo(Berry[j]) == true then
   turtle.transferTo(Berry[j])
  else
  turtle.drop()
  end
end
end
function Modulo()
for i = 1,5 do
  if turtle.getItemCount(Berry[i])%10 == 0 then
   turtle.select(Berry[i])
   turtle.dropDown(9)
  end
end
end
-- Programme :
term.clear()
term.setCursorPos(1,1)
print ("Veuillez insérer les objets suivants dans les bons slots :")
print ("  - Slot 1 : Iron Oreberry")
print ("  - Slot 2 : Gold Oreberry")
print ("  - Slot 3 : Copper Oreberry")
print ("  - Slot 4 : Tin Oreberry")
print ("  - Slot 5 : Aluminum Oreberry")
read()
term.clear()
term.setCursorPos(1,1)
for c = 1,5 do
  print("Vérification... Veuillez patienter...")
  sleep(5)
  write("Slot n°")
  write (c)
   if turtle.getItemCount(c) == 1 then
	write(" - OK \n")
   else
	print("Erreur, redémarrage en cours")
	sleep(10)
	os.reboot()
   end
end
print("")
print("Vérification completer sans problême")
read()
term.clear()
term.setCursorPos(1,1)
while true do
  turtle.select(16)
  turtle.suckUp(1)
  Trie()
  Modulo()
end

I didn't found any other way for just put 9 by 9, if someone have better idea and solution let me know :wub:/>
I found after a couple of times the turtle only drop items can you explain were is my mistake ? :unsure:/>

Thanks you very much and sorry for some mistake on the topic :rolleyes:/>
See you soon :)/>
Bomb Bloke #2
Posted 11 February 2016 - 06:04 AM
I found after a couple of times the turtle only drop items can you explain were is my mistake ? :unsure:/>

This here:

function Trie()
for j = 1,5 do
  if turtle.compareTo(Berry[j]) == true then
   turtle.transferTo(Berry[j])
  else
  turtle.drop()
  end
end

When j == 1, it compares the sucked berry to your iron berry slot. If it is not an iron berry, it drops it. You need to compare to all five types of berries and then drop if there are no matches:

local function Trie()
  local found = false

  for j = 1,5 do
    if turtle.compareTo(Berry[j]) then
      turtle.transferTo(Berry[j])
      found = true
      break
    end
  end

  if not found then turtle.drop() end
end
Vampire142 #3
Posted 11 February 2016 - 11:12 AM
Thanks you very much :)/>
Now i'm sure programming at 4AM is not a great idea :rolleyes:/>
Vampire142 #4
Posted 11 February 2016 - 11:23 AM
Sorry for double post I cannot edit earlier post, but I wanted know why you use "break" after "found = true", break doesn't leave the loop ?
Bomb Bloke #5
Posted 11 February 2016 - 11:56 AM
break doesn't leave the loop ?

Yes it does - it breaks the loop, but not the whole function.

Come to think of it, though, there's no need to stay in the function… so this'd also work:

local function Trie()
  for j = 1,5 do
    if turtle.compareTo(Berry[j]) then
      turtle.transferTo(Berry[j])
      return
    end
  end

  turtle.drop()
end
Vampire142 #6
Posted 18 February 2016 - 09:07 PM
Hello, another problem here :P/>

Always on the same server, now i tried to program a "egg sorter", let me explain :wub:/>
At the right of the turtle is a chest, when this chest is full, turtle have to fill a dispenser above, and finally when it's fulled drop it in a trash at the left this time

In my mind this code seems to work, but not for the turtle :mellow:/>


-- Variable :
local OeufChest = 0
local OeufDispenser = 0
local OeufTrash = 0
-- Fonctions :
local function Total()
local q = 0
for i = 1,16 do
  q = turtle.getItemCount(i) + q
end
return q
end
local function Chest()
local entrer = true
turtle.turnRight()
for i = 1,16 do
  turtle.select(i)
  OeufChest = turtle.getItemCount(i) + OeufChest
  if (turtle.getItemCount(i) > 0 and turtle.drop()) == false then
   entrer = false
   break
  end
end
turtle.turnLeft()
return entrer
end

local function Dispenser()
local entrer = true
for i = 1,16 do
  turtle.select(i)
  OeufDispenser = turtle.getItemCount(i) + OeufDispenser
  if (turtle.getItemCount(i) > 0 and turtle.dropDown()) == false then
   entrer = false
   break
  end
end
return entrer
end
local function Trash()
turtle.turnLeft()
for i = 1,16 do
  turtle.select(i)
  OeufTrash = turtle.getItemCount(i) + OeufTrash
  if (turtle.getItemCount(i) > 0 and turtle.drop()) == false then
   entrer = false
   break
  end
end
turtle.turnRight()
end
local function Classment()
if (Chest() and Dispenser()) == false then
  Trash()
end
end
-- Programme :
term.clear()
while true do
term.setCursorPos(1,1)
if (Total() > 0) then
  Classment()
end
print(" ~ Rangement des Oeufs ~ ")
print("")
write(OeufChest)
write(" oeufs dans le coffre\n\n")
write(OeufDispenser)
write(" oeufs dans le dispenser\n\n")
write(OeufTrash)
write(" oeufs dans la poubelle\n\n")
sleep(0.5)
end
So I don't what my mind is made of…

The programs do what I what when he start but after the end of the endless loop (while true do) the turtle seems to be possessed, help me to curse my little turtle :unsure:/>
Edited on 18 February 2016 - 08:10 PM
Bomb Bloke #7
Posted 18 February 2016 - 11:59 PM
These lines:

if (turtle.getItemCount(i) > 0 and turtle.drop()) == false then

… are saying "if the turtle EITHER has no items in the slot, OR cannot drop them, then…". Both values have to be true to prevent entrer from flipping to false, which means that if the turtle simply doesn't have items in the selected slot it'll incorrectly decide the target container is full.

You meant:

if turtle.getItemCount(i) > 0 and not turtle.drop() then

Likewise, this line:

if (Chest() and Dispenser()) == false then

… should be written as:

if not (Chest() or Dispenser()) then