resaloli

[OC] WitherFarm

Oct 20th, 2020 (edited)
2,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.07 KB | None | 0 0
  1.  
  2.  
  3. --======== Variables =========
  4. local rsSwitchID = 2 --Side of adapterA kill switch is on
  5. local rsclockID = 3 --Side of adapterA clock is on
  6. local rsCounterID = 5 --Side of adapterA witherCounter is on (will emit rs strength of # of wither can spawnj)
  7.  
  8. local rsLeftSand = 4 -- side of adapterB left sand sensor is on
  9. local rsLeftSkulls = 3 -- side of adapterB left skulls sensor is on
  10. local rsRightSand = 5 -- side of adapterB right sand sensor is on
  11. local rsRightSkulls = 2 -- side of adapterB right skulls sensor is on
  12.  
  13. local soulSandChest = 4 -- side of chestAdapter soul sand chest is on
  14. local skullsChest = 5 -- side of chestAdapter skulls chest is on
  15.  
  16. local adapterAAddress = "c9259acf-2345-4480-813d-a81137645cb3";
  17. local adapterBAddress = "b5f61c39-2ed7-42fe-ad80-1b7504a6582c";
  18.  
  19. local chestAdapter = "c949de15-d837-4187-b821-9669d5452921";
  20.  
  21. --======== Term stuff =========
  22. local term = require("term")
  23. term.clear()
  24. term.setCursor(1, 0)
  25. print("Starting Wither Spawner")
  26. print("By ResaloliPT")
  27.  
  28. --======== Requires =========
  29. local component = require("component")
  30. local rsAdapterA = component.proxy(adapterAAddress) --MainRSAdapter
  31. local rsAdapterB = component.proxy(adapterBAddress) --SlaveRSAdapter
  32. local kB = require("keyboard")
  33. local event = require("event")
  34. local chestAdapter = component.proxy(chestAdapter)
  35.  
  36. --======== Program Vars =========
  37. local shouldExit = false --if program should exit
  38. local rsTick = false -- if clock ticked
  39. local rsSwitch = false -- if lever is on
  40.  
  41. --======== Events =========
  42. function onRsChange(_, address, side, value)
  43.     if side == rsSwitchID and address == adapterAAddress then
  44.         rsSwitch = value > 0
  45.         --print("RSChange: [side="..side.."][rsSwitchID="..rsSwitchID.."][value="..value.."]")
  46.         return
  47.     end
  48.  
  49.     if side == rsclockID and value > 0 and address == adapterAAddress then
  50.         rsTick = true
  51.     end
  52. end
  53.  
  54. function onKeyPress(_, _, char, code, _)
  55.     if char == 32 or code == kB.keys.space then
  56.         if shouldExit == true then
  57.             print("Command to shutdown already sent! Wait for mandatory cycles to finish")
  58.             return
  59.         end
  60.  
  61.         print("Program will now terminate")
  62.         shouldExit = true
  63.     end
  64. end
  65.  
  66.  
  67.  
  68. --======== Lifecycle Pt.1========
  69. function preInit()
  70.     local L_rsSwitchValue = rsAdapterA.getInput(rsSwitchID)
  71.     --print("RSInit Value: "..L_rsSwitchValue)
  72.     if  L_rsSwitchValue > 0 then
  73.         rsSwitch = true
  74.     else
  75.         rsSwitch = false
  76.     end
  77.  
  78.     event.listen("redstone_changed", onRsChange)
  79.     event.listen("key_down", onKeyPress)
  80. end
  81.  
  82. function onShutdown()
  83.     rsAdapterA.setOutput(rsSwitchID, 0)
  84.     rsAdapterA.setOutput(rsclockID, 0)
  85.     rsAdapterA.setOutput(rsCounterID, 0)
  86.  
  87.     rsAdapterB.setOutput(rsLeftSand, 0)
  88.     rsAdapterB.setOutput(rsLeftSkulls, 0)
  89.     rsAdapterB.setOutput(rsRightSand, 0)
  90.     rsAdapterB.setOutput(rsRightSkulls, 0)
  91.  
  92.     event.ignore("redstone_changed", onRsChange)
  93.     event.ignore("key_down", onKeyPress)
  94.  
  95.     term.clear()
  96.     term.setCursor(1, 0)
  97.  
  98.     os.exit()
  99. end
  100.  
  101. --======== Internal vars ========
  102.  
  103. local currTotalSand = 0
  104. local currTotalSkulls = 0
  105. local witherCount = 0
  106. local cycle = 0
  107. local idleCounter = 0
  108.  
  109. --======== Lifecycle Pt.2 ========
  110.  
  111. function beforeTick()
  112.     local L_rsSwitchValue = rsAdapterA.getInput(rsSwitchID)
  113.     if  L_rsSwitchValue > 0 then
  114.         rsSwitch = true
  115.     else
  116.         rsSwitch = false
  117.     end
  118.    
  119.     local sandStack = chestAdapter.getStackInSlot(soulSandChest, 1)
  120.     if sandStack == nil then
  121.         currTotalSand = 0
  122.     else
  123.         currTotalSand = sandStack.size
  124.     end
  125.  
  126.     local skullsStack = chestAdapter.getStackInSlot(skullsChest, 1)
  127.     if skullsStack == nil then
  128.         currTotalSkulls = 0
  129.     else
  130.         currTotalSkulls = skullsStack.size
  131.     end
  132.  
  133.     witherCount = math.floor(math.min(currTotalSand/4, currTotalSkulls/3))
  134.  
  135.     rsAdapterA.setOutput(rsCounterID, witherCount)
  136.  
  137.     --[[
  138.         Cycles:
  139.         0 - idle
  140.         1 - left sand
  141.         2 - wait for sand
  142.         3 - left skulls (if cant spawn another wither will jump to idle)
  143.         4 - right sand
  144.         5 - wait for samd
  145.         6 - right skulls
  146.     --]]
  147.  
  148.     if witherCount <= 0 and cycle ~= 0 and (cycle == 3 or cycle == 6) then
  149.         cycle = 0
  150.         print("Out of wither materials (Sand = "..currTotalSand.." : Skulls = "..currTotalSkulls..")")
  151.         return
  152.     end
  153.    
  154.     --print("Before cycle: "..cycle.." : witherCount: "..witherCount.. " : RSSwitch"..(rsSwitch and "true" or "false") )
  155.     if cycle == 6 then
  156.         cycle = 0
  157.     elseif cycle == 3 and (witherCount <= 0 or rsSwitch == false) then
  158.         cycle = 0
  159.     elseif cycle == 6 and (witherCount <= 0 or rsSwitch == false) then
  160.         cycle = 0
  161.     end
  162.  
  163.     if cycle == 0 and (witherCount <= 0 or rsSwitch == false) then
  164.         return
  165.     end
  166.  
  167.     cycle = cycle + 1
  168.  
  169. end
  170.  
  171. function tick()
  172.     if (cycle == 0 and idleCounter == 0) or cycle ~= 0 then
  173.         print("Current Cycle: "..cycle)
  174.     end
  175.  
  176.     if cycle == 0 then
  177.         os.sleep(0.1)
  178.         idleCounter = idleCounter + 1
  179.         if idleCounter >= 10 then
  180.             print("Idling")
  181.             idleCounter = 1
  182.             if witherCount <= 0  then
  183.                 print("Out of wither materials (Sand = "..currTotalSand.." : Skulls = "..currTotalSkulls..")")
  184.             end
  185.             if rsSwitch == false then
  186.                 print("Farm Offline")
  187.             end
  188.         end
  189.         return
  190.     else
  191.         idleCounter = 0
  192.     end
  193.  
  194.     if cycle == 1 then
  195.         print("Setting left sand")
  196.         rsAdapterB.setOutput(rsLeftSand, 15)
  197.         os.sleep(1)
  198.         rsAdapterB.setOutput(rsLeftSand, 0)
  199.         return
  200.     end
  201.  
  202.     if cycle == 2 then
  203.         print("Waiting 3 seconds")
  204.         os.sleep(3)
  205.         return
  206.     end
  207.  
  208.     if cycle == 3 then
  209.         print("Setting left skulls")
  210.         rsAdapterB.setOutput(rsLeftSkulls, 15)
  211.         os.sleep(1)
  212.         rsAdapterB.setOutput(rsLeftSkulls, 0)
  213.         return
  214.     end
  215.  
  216.     if cycle == 4 then
  217.         print("Setting right sand")
  218.         rsAdapterB.setOutput(rsRightSand, 15)
  219.         os.sleep(1)
  220.         rsAdapterB.setOutput(rsRightSand, 0)
  221.         return
  222.     end
  223.  
  224.     if cycle == 5 then
  225.         print("Waiting 3 seconds")
  226.         os.sleep(3)
  227.         return
  228.     end
  229.  
  230.     if cycle == 6 then
  231.         print("Setting right skulls")
  232.         rsAdapterB.setOutput(rsRightSkulls, 15)
  233.         os.sleep(1)
  234.         rsAdapterB.setOutput(rsRightSkulls, 0)
  235.         return
  236.     end
  237. end
  238.  
  239. function afterTick()
  240.     os.sleep(0.1)
  241. end
  242.  
  243. --======== Program =========
  244. print("PreInit Start")
  245. preInit()
  246. print("PreInit Completed")
  247.  
  248. print("Loop Start")
  249.  
  250. while true do
  251.     ::repeat_loop::
  252.     if shouldExit == true and (cycle == 0 or cycle == 3 or cycle == 6) then
  253.         break
  254.     end
  255.  
  256.     if rsTick then
  257.         --print("Tick Start")
  258.         beforeTick()
  259.         tick()
  260.         afterTick()
  261.         --print("Tick end")
  262.  
  263.         goto repeat_loop        
  264.     end
  265.  
  266.     os.sleep(0.1)
  267. end
  268.  
  269. print("Shuting down")
  270. onShutdown()
  271.  
Add Comment
Please, Sign In to add comment