Advertisement
akihex

rndFenceRect

Aug 19th, 2014
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. -- arguments: length width
  2. -- length and width of bounding fence rectangle: length width-2 length width-2
  3. -- starts along length with block in front of turtle and turns left after length fence blocks
  4. local args = {...}
  5. local t = turtle
  6. local ts = t.select
  7. local ic = t.getItemCount
  8.  
  9. print("Fuel: ",t.getFuelLevel()," / ",t.getFuelLimit())
  10.  
  11. local slotList = {}
  12. local function prepareRandomSlot()
  13.   slotList = {}
  14.   for i=1,16 do
  15.     if ic(i) > 0 then slotList[#slotList+1] = {["slot"]=i,["qty"]=ic(i)} end
  16.   end
  17. end
  18.  
  19. prepareRandomSlot()
  20.  
  21. local function updateRandomSlot(slot)
  22.   local index = nil
  23.   for i=1,#slotList do
  24.     if slotList[i].slot == slot then
  25.         slotList[i].qty = slotList[i].qty - 1
  26.         index = i
  27.       break
  28.     end
  29.     if index and slotList[index].qty <= 0 then table.remove(slotList,index) end
  30.   end
  31. end
  32.  
  33. local function randomSlot()
  34.   if #slotList == 0 then return 1
  35.   else
  36.     return slotList[math.random(#slotList)].slot
  37.   end
  38. end
  39.  
  40. local function placeFence(slot)
  41.   ts(slot)
  42.   t.digDown()
  43.   t.placeDown()
  44.   updateRandomSlot(slot)
  45. end
  46.  
  47. local length = args[1] and args[1] or 0
  48. local width = args[2] and args[2] or 0
  49.  
  50. local function move()
  51.   while t.detect() do
  52.     t.dig()
  53.     sleep(0.5)
  54.   end
  55.   t.forward()
  56. end
  57.  
  58. local function go()
  59.   local rect = {length,width-1,length-1,width-2}
  60.   for k=1,4 do
  61.     for i=1,rect[k] do
  62.       move()
  63.       placeFence(randomSlot())
  64.     end
  65.     t.turnLeft()
  66.   end
  67. end
  68.  
  69. go()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement