nSun

CC MobspawnerMgr

Aug 15th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.04 KB | None | 0 0
  1. local role = ...
  2. if not role or (role~="kill" and role~="sort" and role~="craft") then
  3.     error(string.format([[Usage:
  4. %s [kill|sort|craft] ]],shell.getRunningProgram()),-1)
  5.     return
  6. end
  7.  
  8. local function dots(n)
  9.     return string.rep(".",math.floor(os.clock()*10)%n)
  10. end
  11.  
  12. local function echo(str,line)
  13.     term.setCursorPos(1,line)
  14.     term.clearLine()
  15.     term.write(str)
  16. end
  17.  
  18. local blink
  19. local function runningTime()
  20.     blink = not blink
  21.     echo("running: "..textutils.formatTime(os.clock()/3600,true)..(blink and "." or " "),2)
  22. end
  23.  
  24. local function select(slot)
  25.     if turtle.getSelectedSlot() ~= slot then
  26.         turtle.select(slot)
  27.     end
  28. end
  29.  
  30. --
  31. -- GRINDER
  32. --
  33. local function grinder_step()
  34.     echo("Attack!",3)
  35.     local getSomething = false
  36.     while turtle.attack() do
  37.         getSomething = true
  38.     end
  39.     local slot=1
  40.     if getSomething then
  41.         echo("Manage items",3)
  42.         while slot<=16 do
  43.             if turtle.getItemCount(slot)>0 then
  44.                 select(slot)
  45.                 turtle.dropDown(64)
  46.             end
  47.             slot=slot+1
  48.         end
  49.     else
  50.         echo("Nothing"..dots(5),3)
  51.     end
  52. end
  53.  
  54.  
  55. local function grinder_main()
  56.     local timer = os.startTimer(.2)
  57.     local continue = true
  58.     while continue do
  59.         local e,k = os.pullEvent()
  60.         if e=="key" and k==keys.t then
  61.             break
  62.         elseif e=="timer" then
  63.             grinder_step()
  64.             timer = os.startTimer(.2)
  65.             runningTime()
  66.         end
  67.     end
  68. end
  69.  
  70. --
  71. -- TRIEUR
  72. --
  73. local function sort_faceInputChest()
  74.     local test,data = turtle.inspect()
  75.     if not test then
  76.         turtle.turnRight()
  77.     elseif data.name~="minecraft:chest" or data.state.facing~="west"then
  78.         if data.name=="minecraft:chest" then
  79.             turtle.turnLeft()
  80.         end
  81.         turtle.turnLeft()
  82.     end
  83. end
  84. local function sort_faceOutputChest()
  85.     local test,data = turtle.inspect()
  86.     if not test then
  87.         turtle.turnLeft()
  88.     elseif data.name~="minecraft:trapped_chest" then
  89.         if data.name=="minecraft:chest" then
  90.             turtle.turnRight()
  91.         end
  92.         turtle.turnRight()
  93.     end
  94. end
  95.  
  96.  
  97. local function sort_hasBowToRepair()
  98.     local count,space = turtle.getItemCount(2),turtle.getItemSpace(2)
  99.     if count==1 and space==0 then return true
  100.     elseif count==0 then return false
  101.     else
  102.         sort_faceOutputChest()
  103.         select(2)
  104.         turtle.drop(64)
  105.         turtle.select(1)
  106.         return false
  107.     end
  108. end
  109.  
  110. local function sort_suck()
  111.     sort_faceInputChest()
  112.     select(1)
  113.     return turtle.suck(64)
  114.        
  115. end
  116.  
  117. local function sort_manageItem()
  118.     select(1)
  119.     local data = turtle.getItemDetail(1)
  120.     if not data then
  121.         echo("Manage item : nothing",3)
  122.         return
  123.     elseif data.name=="minecraft:bone"
  124.     or data.name=="minecraft:arrow" then
  125.         echo("Manage item : deposit mainstream",3)
  126.         sort_faceOutputChest()
  127.         turtle.drop(64)
  128.     elseif data.name=="minecraft:bow" then
  129.         if data.damage > 0 then
  130.             if sort_hasBowToRepair() then
  131.                 echo("Manage item : attempt to repair",3)
  132.                 turtle.craft()
  133.                 sort_manageItem()
  134.             else
  135.                 echo("Manage item : keep scrap aside",3)
  136.                 turtle.transferTo(2)
  137.             end
  138.         else
  139.             echo("Manage item : stock bow",3)
  140.             turtle.dropDown()
  141.         end
  142.     else
  143.         echo("Manage item : stock "..data.name,3)
  144.         turtle.dropDown()
  145.     end
  146. end
  147.  
  148. local function sort_step()
  149.     if turtle.getItemCount(1)>0 then
  150.         echo("Item management",3)
  151.         sort_manageItem()
  152.     else
  153.         echo("Suck item",3)
  154.         if not sort_suck() then
  155.             echo("Nothing"..dots(5),3)
  156.             sleep(.5)
  157.         end
  158.     end
  159. end
  160.  
  161. local function sort_main()
  162.     local timer = os.startTimer(.2)
  163.     local continue,blink = true,false
  164.     while continue do
  165.         local e,k = os.pullEvent()
  166.         if e=="key" and k==keys.t then
  167.             print("Terminated")
  168.             break
  169.         elseif e=="timer" then
  170.             sort_step()
  171.             runningTime()
  172.             timer = os.startTimer(.2)
  173.         end
  174.     end
  175. end
  176.  
  177.  
  178.  
  179. --
  180. -- CRAFT (dispenser)
  181. --
  182. function craft_isBowInputChest(data)
  183.     return data
  184.     and data.name=="minecraft:chest"
  185.     and data.state.facing=="south"
  186. end
  187. function craft_isRedstoneInputChest(data)
  188.     return data
  189.     and data.name=="minecraft:chest"
  190.     and data.state.facing=="west"
  191. end
  192. function craft_isOuputChest(data)
  193.     return data
  194.     and data.name=="minecraft:trapped_chest"
  195.     and data.state.facing=="west"
  196. end
  197. function craft_isTrashSide(data)
  198.     return data
  199.     and data.name=="minecraft:wall_sign"
  200. end
  201.  
  202. function craft_faceBowChest()
  203.     echo("Facing Bow chest",3)
  204.     local test,data = turtle.inspect()
  205.     if craft_isBowInputChest(data) then return true
  206.     elseif craft_isOuputChest(data) then return turtle.turnRight()
  207.     elseif craft_isRedstoneInputChest(data) then turtle.turnLeft()
  208.     end
  209.     return turtle.turnLeft()
  210. end
  211. function craft_faceRedstoneChest()
  212.     echo("Facing redstone chest",3)
  213.     local test,data = turtle.inspect()
  214.     if craft_isRedstoneInputChest(data) then return true
  215.     elseif craft_isTrashSide(data) then return turtle.turnRight()
  216.     elseif craft_isBowInputChest(data) then turtle.turnLeft()
  217.     end
  218.     return turtle.turnLeft()
  219. end
  220. function craft_faceOutputChest()
  221.     echo("Facing output chest",3)
  222.     local test,data = turtle.inspect()
  223.     if craft_isOuputChest(data) then return true
  224.     elseif craft_isRedstoneInputChest(data) then return turtle.turnRight()
  225.     elseif craft_isTrashSide(data) then turtle.turnLeft()
  226.     end
  227.     return turtle.turnLeft()
  228. end
  229. function craft_faceTrashSide()
  230.     echo("Facing trash side",3)
  231.     local test,data = turtle.inspect()
  232.     if craft_isTrashSide(data) then return true
  233.     elseif craft_isBowInputChest(data) then return turtle.turnRight()
  234.     elseif craft_isOuputChest(data) then turtle.turnLeft()
  235.     end
  236.     return turtle.turnLeft()
  237. end
  238.  
  239. local function craft_restockRedstone(slot)
  240.     echo("Restock redstone",3)
  241.     craft_faceRedstoneChest()
  242.     turtle.select(slot)
  243.     turtle.drop(64)
  244.     turtle.transferTo(10)
  245. end
  246.  
  247. local function craft_trash(slot)
  248.     echo("Trash",3)
  249.     craft_faceTrashSide()
  250.     turtle.select(slot)
  251.     turtle.drop(64)
  252. end
  253.  
  254. local function craft_fillRedstoneSlot()
  255.     echo("Fill redstone slot",3)
  256.     local data = turtle.getItemDetail(10)
  257.     if data and data.name~="minecraft:redstone" then
  258.         craft_trash(10)
  259.     end
  260.     local count = turtle.getItemSpace(10)
  261.     if count>0 then
  262.         craft_faceRedstoneChest()
  263.         turtle.select(10)
  264.         turtle.suck(count)
  265.     end
  266. end
  267.  
  268. local function craft_fillCobblestone()
  269.     echo("Fill cobblestone slots",3)
  270.     local slots,s,i = {1,2,3,5,7,9,11}
  271.     for i,s in ipairs(slots) do
  272.         local data = turtle.getItemDetail(s)
  273.         if data and data.name~="minecraft:cobblestone" then
  274.             if data.name=="redstone" then
  275.                 craft_restockRedstone(s)
  276.             end
  277.             if turtle.getItemCount(s)>0 then
  278.                 craft_trash(s)
  279.             end
  280.         end
  281.         turtle.select(s)
  282.         while turtle.getItemCount(s)<=0 do
  283.             if turtle.detectDown() and not turtle.digDown() then
  284.                 sleep(.3)
  285.             end
  286.             sleep(.1)
  287.         end
  288.     end
  289. end
  290.  
  291. local function craft_fastCountCobblestone()
  292.     if turtle.getItemSpace(1)>0 then return 1
  293.     elseif turtle.getItemSpace(2)>0 then return 2
  294.     elseif turtle.getItemSpace(3)>0 then return 3
  295.     elseif turtle.getItemSpace(5)>0 then return 5
  296.     elseif turtle.getItemSpace(7)>0 then return 7
  297.     elseif turtle.getItemSpace(9)>0 then return 9
  298.     elseif turtle.getItemSpace(11)>0 then return 11
  299.     else return false end
  300. end
  301.  
  302. local slowFillCobblestoneCount = 0
  303. local function slowcraft_fillCobblestone()
  304.     echo("Slow fill cobblestone slots",3)
  305.     local slots = {1,2,3,5,7,9,11}
  306.     local s = slots[(slowFillCobblestoneCount%#slots)+1]
  307.     slowFillCobblestoneCount = slowFillCobblestoneCount+1>=#slots and 0 or slowFillCobblestoneCount + 1
  308.  
  309.     local data = turtle.getItemDetail(s)
  310.     if data and data.name~="minecraft:cobblestone" then
  311.         if data.name=="redstone" then
  312.             craft_restockRedstone(s)
  313.         end
  314.         if turtle.getItemCount(s)>0 then
  315.             craft_trash(s)
  316.         end
  317.     end
  318.     turtle.select(s)
  319.     while not turtle.detectDown() do
  320.         echo("Waiting cobblestone generator",3)
  321.         sleep(.1)
  322.     end
  323.     if turtle.getItemSpace(s)>0 then
  324.         turtle.digDown()
  325.     end
  326. end
  327.  
  328. local function craft_fillBow()
  329.     echo("Fill bow slot",3)
  330.     local data = turtle.getItemDetail(6)
  331.     if data and (data.name~="minecraft:bow" or data.damage>0) then
  332.         if data.name=="redstone" then
  333.             craft_restockRedstone(6)
  334.         end
  335.         if turtle.getItemCount(6)>0 then
  336.             craft_trash(6)
  337.         end
  338.     end
  339.     craft_faceBowChest()
  340.     turtle.select(6)
  341.     local continue = true
  342.     while continue do
  343.         if not turtle.suck(1) then
  344.             term.setCursorPos(1,3)
  345.             term.clearLine()
  346.             print("No more bows")
  347.             return false
  348.         end
  349.         local data = turtle.getItemDetail(6)
  350.         if data.name ~= "minecraft:bow" or (data.name=="minecraft:bow" and data.damage>0) then
  351.             craft_trash(6)
  352.             craft_faceBowChest()
  353.             turtle.select(6)
  354.             sleep(.3)
  355.         else return true
  356.         end
  357.         sleep(.1)
  358.     end
  359.     return false
  360. end
  361.  
  362. local function confirmCraft()
  363.     local rsSlot = turtle.getItemDetail(10)
  364.     local bowSlot = turtle.getItemDetail(6)
  365.     local confirm = turtle.craft(0)
  366.         and bowSlot
  367.         and rsSlot.name=="minecraft:redstone"
  368.         and bowSlot.name=="minecraft:bow"
  369.         and bowSlot.damage==0
  370.    
  371.     echo(confirm and "Craft recipe confirm" or "Craft : missing items",3)
  372.     return confirm
  373. end
  374.  
  375. local function rsInput()
  376.     echo("RS check",3)
  377.     return rs.getInput("left")
  378.     or rs.getInput("right")
  379.     or rs.getInput("front")
  380.     or rs.getInput("back")
  381. end
  382.  
  383. local function craft_step()
  384.     echo("Process",3)
  385.     craft_fillRedstoneSlot()
  386.     craft_fillCobblestone()
  387.     if not craft_fillBow() then
  388.         return
  389.     end
  390.     turtle.select(6)
  391.     while confirmCraft() and turtle.craft(1) do
  392.         craft_faceOutputChest()
  393.         turtle.select(6)
  394.         turtle.drop(64)
  395.         if not craft_fillBow() then
  396.             return
  397.         end
  398.     end
  399. end
  400.  
  401. local function craft_main()
  402.     local timer = os.startTimer(.5)
  403.     local continue = true
  404.     while continue do
  405.         local e,k = os.pullEvent()
  406.         if e=="key" and k==keys.t then
  407.             print("Terminated")
  408.             continue=false
  409.             break
  410.         elseif e=="redstone" and rsInput() then
  411.             while rsInput() do
  412.                 sleep(.1)
  413.             end
  414.             craft_step()
  415.             timer = os.startTimer(.5)
  416.         elseif e=="timer" then
  417.             if craft_fastCountCobblestone() then
  418.                 slowcraft_fillCobblestone()
  419.             end
  420.             timer = os.startTimer(.5)
  421.             runningTime()
  422.         end
  423.     end
  424. end
  425.  
  426. --
  427. -- MAIN
  428. --
  429. term.clear()
  430. echo(string.format("#%d %s - Press [T]",os.getComputerID(),os.getComputerLabel()),1)
  431. if role=="kill" then
  432.     rs.setOutput("top",true)
  433.     grinder_main()
  434.     rs.setOutput("top",false)
  435. elseif role=="sort" then
  436.     sort_main()
  437. elseif role=="craft" then
  438.     craft_main()
  439. end
  440. term.clearLine()
  441. print("Terminated")
  442. sleep(.5)
Advertisement
Add Comment
Please, Sign In to add comment