Difference between revisions of "Cobble Generator"

From ComputerCraft Wiki
Jump to: navigation, search
(Advanced Generator)
Line 1: Line 1:
Tutorial written by Temploit/NewFletcher.
 
 
 
== Colors ==
 
== Colors ==
 
Run Loop: <span style="color:blue">while true do</span><br>
 
Run Loop: <span style="color:blue">while true do</span><br>
If: <span style="color:green">if a=b then</span><br>
+
If: <span style="color:green">if a==b then</span><br>
 
Action: <span style="color:red">turtle.dig()</span><br>
 
Action: <span style="color:red">turtle.dig()</span><br>
 
Var: <span style="color:DarkViolet">a = turtle.getItemCount(1)</span><br>
 
Var: <span style="color:DarkViolet">a = turtle.getItemCount(1)</span><br>
Line 68: Line 66:
 
== Advanced Generator ==
 
== Advanced Generator ==
 
This is the generator I created (for use with (height:4 width:2) monitor):
 
This is the generator I created (for use with (height:4 width:2) monitor):
 +
 +
function counter()
 +
  <span style="color:DarkViolet">local i = 0</span>
 +
  return function() i = i + 1 return i end
 +
end
 +
function modStr(num)
 +
  <span style="color:green">if num < 10 then</span>
 +
    return "0" .. num
 +
  <span style="color:green">else</span>
 +
    return num
 +
  <span style="color:green">end</span>
 +
end
 
  <span style="color:blue">while true do</span>
 
  <span style="color:blue">while true do</span>
 
   <span style="color:green">if turtle.detect() then</span>
 
   <span style="color:green">if turtle.detect() then</span>
Line 73: Line 83:
 
   <span style="color:red">term.clear()</span>
 
   <span style="color:red">term.clear()</span>
 
   <span style="color:red">term.setCursorPos(1,1)</span>
 
   <span style="color:red">term.setCursorPos(1,1)</span>
   <span style="color:DarkViolet">a = turtle.getItemCount(1)</span>
+
   <span style="color:DarkViolet">local total = 0</span>
   <span style="color:DarkViolet">b = turtle.getItemCount(2)</span>
+
   <span style="color:DarkCyan">for i=1,9,1 do</span>
  <span style="color:DarkViolet">c = turtle.getItemCount(3)</span>
+
    <span style="color:DarkViolet">total = total + turtle.getItemCount(i)</span>
   <span style="color:DarkViolet">d = turtle.getItemCount(4)</span>
+
   <span style="color:DarkCyan">end</span>
   <span style="color:DarkViolet">e = turtle.getItemCount(5)</span>
+
   <span style="color:DarkCyan">for i=1,9,1 do</span>
  <span style="color:DarkViolet">f = turtle.getItemCount(6)</span>
+
    <span style="color:OrangeRed">print("Row " .. i .. ": " .. turtle.getItemCount(i) .. " (" .. math.floor((turtle.getItemCount(i)/total)*100) .. "%)")</span>
  <span style="color:DarkViolet">g = turtle.getItemCount(7)</span>
+
   <span style="color:DarkCyan">end</span>
  <span style="color:DarkViolet">h = turtle.getItemCount(8)</span>
+
   <span style="color:OrangeRed">print("Total: " .. total)</span>
  <span style="color:DarkViolet">i = turtle.getItemCount(9)</span>
+
  <span style="color:OrangeRed">print("Row 1: " .. a .. " (" .. math.floor(((a/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 2: " .. b .. " (" .. math.floor(((b/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 3: " .. c .. " (" .. math.floor(((c/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 4: " .. d .. " (" .. math.floor(((d/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 5: " .. e .. " (" .. math.floor(((e/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 6: " .. f .. " (" .. math.floor(((f/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
   <span style="color:OrangeRed">print("Row 7: " .. g .. " (" .. math.floor(((g/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 8: " .. h .. " (" .. math.floor(((h/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:OrangeRed">print("Row 9: " .. i .. " (" .. math.floor(((i/(a+b+c+d+e+f+g+h+i))*100)) .. "%)")</span>
+
  <span style="color:DarkViolet">totalc = a+b+c+d+e+f+g+h+i</span>
+
   <span style="color:OrangeRed">print("Total: " .. totalc)</span>
+
 
   <span style="color:OrangeRed">print("-----------------")</span>
 
   <span style="color:OrangeRed">print("-----------------")</span>
  <span style="color:green">if a<10 then</span>
+
   <span style="color:DarkViolet">local iterator = counter()</span>
   <span style="color:DarkViolet">a = "0"..a</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if b<10 then</span>
+
  <span style="color:DarkViolet">b = "0"..b</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if c<10 then</span>
+
  <span style="color:DarkViolet">c = "0"..c</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if d<10 then</span>
+
  <span style="color:DarkViolet">d = "0"..d</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if e<10 then</span>
+
  <span style="color:DarkViolet">e = "0"..e</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if f<10 then</span>
+
  <span style="color:DarkViolet">f = "0"..f</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if g<10 then</span>
+
  <span style="color:DarkViolet">g = "0"..g</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if h<10 then</span>
+
  <span style="color:DarkViolet">h = "0"..h</span>
+
  <span style="color:green">end</span>
+
  <span style="color:green">if i<10 then</span>
+
  <span style="color:DarkViolet">i = "0"..i</span>
+
  <span style="color:green">end</span>
+
 
   <span style="color:OrangeRed">print(" ___ ___ ___ ")</span>
 
   <span style="color:OrangeRed">print(" ___ ___ ___ ")</span>
   <span style="color:OrangeRed">print("|" .. a .. " |" .. b .. " |" .. c .. " |")</span>
+
   <span style="color:OrangeRed">print("|" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |")</span>
 
   <span style="color:OrangeRed">print("|___|___|___|")</span>
 
   <span style="color:OrangeRed">print("|___|___|___|")</span>
   <span style="color:OrangeRed">print("|" .. d .. " |" .. e .. " |" .. f .. " |")</span>
+
   <span style="color:OrangeRed">print("|" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |")</span>
 
   <span style="color:OrangeRed">print("|---|---|---|")</span>
 
   <span style="color:OrangeRed">print("|---|---|---|")</span>
   <span style="color:OrangeRed">print("|" .. g .. " |" .. h .. " |" .. i .. " |")</span>
+
   <span style="color:OrangeRed">print("|" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator()))) .. " |")</span>
 
   <span style="color:OrangeRed">print("|___|___|___|")</span>
 
   <span style="color:OrangeRed">print("|___|___|___|")</span>
 
   <span style="color:OrangeRed">print(" ")</span>
 
   <span style="color:OrangeRed">print(" ")</span>

Revision as of 10:45, 14 July 2012

Colors

Run Loop: while true do
If: if a==b then
Action: turtle.dig()
Var: a = turtle.getItemCount(1)
Print: Print("Cobble Farm")
For: for i=1,9 do

Image

SmallGen.png

Coding

First, you have to make a loop so the machine keeps running:

while true do
 'Command
end

Now, inside of the loop, you have to add something to detect the block:

while true do
 if turtle.detect() then
 'Command
 end
end

After that, the block must be mined:

while true do
 if turtle.detect() then
  turtle.dig()
 end
end

This detects if a block is present, and then mines it. However, when all the 9 block spaces are filled with blocks (9*64=576 blocks), they must be dropped to collect new blocks:

while true do
 if turtle.detect() then
  turtle.dig()
 end
 itemcount = turtle.getItemCount(9)
 if itemcount == 64 then
 'Drop all stacks
 end
end

To drop all stacks, another loop must be added:

while true do
 if turtle.detect() then
  turtle.dig()
 end
 itemcount = turtle.getItemCount(9)
 if itemcount == 64 then
  for i=1,9 do
  'Select each stack and then drops it
  end
 end
end

To select each stack and drop it, a turtle.select() and a turtle.drop() must be added:

while true do
 if turtle.detect() then
  turtle.dig()
 end
 itemcount = turtle.getItemCount(9)
 if itemcount == 64 then
  for i=1,9 do
   turtle.select(i)
   turtle.drop()
  end
  turtle.select(1)
 end
end

Advanced Generator

This is the generator I created (for use with (height:4 width:2) monitor):

function counter()
 local i = 0
 return function() i = i + 1 return i end
end
function modStr(num)
 if num < 10 then
   return "0" .. num
 else
   return num
 end
end
while true do
 if turtle.detect() then
  turtle.dig()
  term.clear()
  term.setCursorPos(1,1)
  local total = 0
  for i=1,9,1 do
   total = total + turtle.getItemCount(i)
  end
  for i=1,9,1 do
   print("Row " .. i .. ": " .. turtle.getItemCount(i) .. " (" .. math.floor((turtle.getItemCount(i)/total)*100) .. "%)")
  end
  print("Total: " .. total)
  print("-----------------")
  local iterator = counter()
  print(" ___ ___ ___ ")
  print("|" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |")
  print("|___|___|___|")
  print("|" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |")
  print("|---|---|---|")
  print("|" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator())) .. " |" .. modStr(turtle.getItemCount(iterator()))) .. " |")
  print("|___|___|___|")
  print(" ")
  print("-----------------")
  print("Cobble Farmer")
  print("By: NewFletcher")
 end
 totalcount = turtle.getItemCount(9)
 if totalcount == 64 then
  term.clear()
  term.setCursorPos(1,1)
  print("Dropping Stacks")
  print("...Please wait.")
  sleep(1)
  for i=1,9 do
   turtle.select(i)
   turtle.drop()
  end
  turtle.select(1)
 end
end

Images:
MyCob.png

MyGen.png