MrCervelo

builder

Jan 24th, 2013
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.38 KB | None | 0 0
  1. function readFile(fileName)
  2.   file = assert(fs.open(fileName, "r"))
  3.   for i=1,9 do
  4.     qtysReq[i]=0
  5.   end
  6.   level=1
  7.   row=1
  8.   column=1
  9.   if ( file ) then
  10.     line = file.readAll()
  11.     plan[level] = {}
  12.     plan[level][row] = {}
  13.     i=1
  14.     while ( i <= string.len(line) ) do
  15.       char = string.sub(line, i,i)
  16.       if ( char == "\n" ) then
  17.         if ( string.sub(line, i+1,i+1) == "\n" ) then
  18.           level = level + 1
  19.           row = 1
  20.           column = 1
  21.           plan[level] = {}
  22.           plan[level][row] = {}
  23.           i=i+1
  24.         else
  25.           row=row+1
  26.           column = 1
  27.           plan[level][row] = {}
  28.         end
  29.       else
  30.         plan[level][row][column] = tonumber(char)
  31.         if ( tonumber(char) ~= 0 ) then
  32.           qtysReq[tonumber(char)] = qtysReq[tonumber(char)] + 1
  33.         end
  34.         column = column + 1
  35.       end
  36.       i = i + 1
  37.     end
  38.     file.close()
  39.   else
  40.     io.write("Error reading file")
  41.     os.exit()
  42.   end
  43.   getSubsitutes()
  44.   for i = 1,table.getn(qtysReq) do
  45.     if ( qtysReq[i] ~= 0 ) then
  46.       qty = 0
  47.       io.write("slot " .. i .. " requires " .. qtysReq[i] .. " blocks")
  48.       for j = 1,#subs[i] do
  49.         qty = qty + turtle.getItemCount(subs[i][j])
  50.       end
  51.       if ( qtysReq[i] > qty ) then
  52.         io.write(", only " .. qty .. " found, a refill will be required")
  53.       else
  54.         io.write(", sufficient blocks in subsitute slots")
  55.       end
  56.       io.write("\n")
  57.     end
  58.   end
  59.   return plan
  60. end
  61.  
  62. function getSubsitutes()
  63.   k=1
  64.   for i = 1,16 do
  65.     if ( qtysReq[i] == nil or qtysReq[i] == 0 ) then
  66.       if ( turtle.getItemCount(i) > 0 ) then
  67.         availSlots[k] = i
  68.         k = k + 1
  69.       else
  70.         if ( freeSlot == 0 ) then
  71.           freeSlot = i
  72.         end
  73.       end
  74.     else
  75.     end
  76.   end
  77.   for i = 1, #qtysReq do
  78.     if ( qtysReq[i] > 0 ) then
  79.       turtle.select(i)
  80.       k=2
  81.       subs[i]={}
  82.       subs[i][1]=i
  83.       for j = 1, #availSlots do
  84.         if ( turtle.compareTo(availSlots[j])) then
  85.           subs[i][k] = availSlots[j]
  86.           k = k + 1
  87.         end
  88.       end
  89.     end
  90.   end
  91. end
  92.  
  93. function move(direction, count)
  94.   if ( checkFuel() ) then
  95.     io.write("Insufficient Fuel, please place fuel in slot 16")
  96.     while ( not checkFuel() ) do
  97.       refuelTurtle()
  98.       sleep(1)
  99.     end
  100.   end
  101.   if ( direction == "forward" ) then
  102.     for i=1,count do
  103.       while ( not turtle.forward() ) do
  104.         turtle.dig()
  105.         checkInventory()
  106.       end
  107.     end
  108.   elseif ( direction == "up" ) then
  109.     for i=1,count do
  110.       while ( not turtle.up() ) do
  111.         turtle.digUp()
  112.         checkInventory()
  113.       end
  114.     end
  115.   elseif ( direction == "down" ) then
  116.     for i=1,count do
  117.       while ( not turtle.down() ) do
  118.         turtle.digDown()
  119.         checkInventory()
  120.       end
  121.     end
  122.   else
  123.     io.write("Unknown Direction Specified - " .. direction .. "\n")
  124.     os.exit(1)
  125.   end
  126. end
  127.  
  128. function turn(direction, count)
  129.   if ( direction == "left" ) then
  130.     for i=1,count do
  131.       turtle.turnLeft()
  132.     end
  133.   elseif ( direction == "right" ) then
  134.     for i=1,count do
  135.       turtle.turnRight()
  136.     end
  137.   else
  138.     io.write("Unknown Turn Direction Specified - " .. direction .. "\n")
  139.   end
  140. end
  141.  
  142. function checkInventory()
  143.   if ( invFull() ) then
  144.     io.write("Inventory Full, Please remove extra items\n")
  145.     while ( invFull() ) do
  146.       sleep(1)
  147.     end
  148.   end
  149. end
  150.  
  151. function invFull()
  152.   full=true
  153.   for i=freeSlot,16 do
  154.     full = (turtle.getItemCount(i) > 0 and full)
  155.   end
  156.   return full
  157. end
  158.  
  159. function getSlot(slot)
  160.   for i = 1,table.getn(subs[slot]) do
  161.     if ( turtle.getItemCount(subs[slot][i]) > 0 and qtysReq[slot] > 0 ) then
  162.       return subs[slot][i]
  163.     end
  164.   end
  165.   return 0
  166. end
  167.  
  168. function dig(direction)
  169.   if ( direction == "forward" ) then
  170.     while ( turtle.detect() ) do
  171.       turtle.dig()
  172.       checkInventory()
  173.     end
  174.   elseif ( direction == "up" ) then
  175.     while ( turtle.detectUp() ) do
  176.       turtle.digUp()
  177.       checkInventory()
  178.     end
  179.   elseif ( direction == "down" ) then
  180.     while ( turtle.detectDown() ) do
  181.       turtle.digDown()
  182.       checkInventory()
  183.     end
  184.   else
  185.     io.write("Unknown Dig Direction Specified - " .. direction .. "\n")
  186.     os.exit(1)
  187.   end
  188. end
  189.  
  190. function placeItem(slot)
  191.   slotNumber = getSlot(slot)
  192.   if ( slotNumber == 0 ) then
  193.     io.write("There is insufficient stock for slot " .. slot .. ". Please replenish stock in one of the following Slots\n")
  194.     for i = 1,table.getn(subs[slot]) do
  195.       io.write(subs[slot][i] .. "\n")
  196.     end
  197.     while ( slotNumber == 0 ) do
  198.       sleep(1)
  199.       slotNumber = getSlot(slot)
  200.     end
  201.   end
  202.   turtle.select(slotNumber)
  203.   dig("down")
  204.   turtle.placeDown()
  205.   qtysReq[slot] = qtysReq[slot] - 1
  206.   turtle.select(freeSlot)
  207. end
  208.  
  209. function checkFuel()
  210.   return (turtle.getFuelLevel() <= 1)
  211. end
  212.  
  213. function refuelTurtle()
  214.   turtle.select(16)
  215.   turtle.refuel()
  216. end
  217.  
  218. local args={ ... }
  219. availSlots={}
  220. plan={}
  221. qtysReq={}
  222. subs={}
  223. fileName=args[1]
  224. rowCount=1
  225. level=0
  226. freeSlot=0
  227. plan = readFile(fileName)
  228. turnDirection="right"
  229. for level = 1,#plan do
  230.   if ( level % 2 == 1 ) then
  231.     rowStart = 1
  232.     rowEnd = table.getn(plan[level])
  233.     rowStep = 1
  234.   else
  235.     rowStart = table.getn(plan[level])
  236.     rowEnd = 1
  237.     rowStep = -1
  238.   end
  239.   for row = rowStart,rowEnd,rowStep do
  240.     if ( row % 2 == level % 2 ) then
  241.       columnStart = 1
  242.       columnEnd = table.getn(plan[level][row])
  243.       columnStep = 1
  244.     else
  245.       columnStart = table.getn(plan[level][row])
  246.       columnEnd = 1
  247.       columnStep = -1
  248.     end
  249.     for column = columnStart,columnEnd, columnStep do
  250.       dig("down")
  251.       if ( plan[level][row][column] ~= 0 ) then
  252.         placeItem(plan[level][row][column])
  253.       end
  254.       if ( column ~= columnEnd ) then
  255.         move("forward", 1)
  256.       end
  257.     end
  258.     if ( row ~= rowEnd ) then
  259.       turn(turnDirection, 1)
  260.       move("forward", 1)
  261.       turn(turnDirection, 1)
  262.       if ( turnDirection == "right" ) then
  263.         turnDirection = "left"
  264.       else
  265.         turnDirection = "right"
  266.       end
  267.     end
  268.   end
  269.   if ( level ~= table.getn(plan) ) then
  270.     move("up", 1)
  271.     turn("left", 2)
  272.     if ( level % 2 == 1 ) then
  273.       if ( table.getn(plan[level]) % 2 == 0 ) then
  274.         turnDirection = "left"
  275.       else
  276.         turnDirection = "right"
  277.       end
  278.     else
  279.       turnDirection = "right"
  280.     end
  281.   end
  282. end
Advertisement
Add Comment
Please, Sign In to add comment