Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --======== Variables =========
- local rsSwitchID = 2 --Side of adapterA kill switch is on
- local rsclockID = 3 --Side of adapterA clock is on
- local rsCounterID = 5 --Side of adapterA witherCounter is on (will emit rs strength of # of wither can spawnj)
- local rsLeftSand = 4 -- side of adapterB left sand sensor is on
- local rsLeftSkulls = 3 -- side of adapterB left skulls sensor is on
- local rsRightSand = 5 -- side of adapterB right sand sensor is on
- local rsRightSkulls = 2 -- side of adapterB right skulls sensor is on
- local soulSandChest = 4 -- side of chestAdapter soul sand chest is on
- local skullsChest = 5 -- side of chestAdapter skulls chest is on
- local adapterAAddress = "c9259acf-2345-4480-813d-a81137645cb3";
- local adapterBAddress = "b5f61c39-2ed7-42fe-ad80-1b7504a6582c";
- local chestAdapter = "c949de15-d837-4187-b821-9669d5452921";
- --======== Term stuff =========
- local term = require("term")
- term.clear()
- term.setCursor(1, 0)
- print("Starting Wither Spawner")
- print("By ResaloliPT")
- --======== Requires =========
- local component = require("component")
- local rsAdapterA = component.proxy(adapterAAddress) --MainRSAdapter
- local rsAdapterB = component.proxy(adapterBAddress) --SlaveRSAdapter
- local kB = require("keyboard")
- local event = require("event")
- local chestAdapter = component.proxy(chestAdapter)
- --======== Program Vars =========
- local shouldExit = false --if program should exit
- local rsTick = false -- if clock ticked
- local rsSwitch = false -- if lever is on
- --======== Events =========
- function onRsChange(_, address, side, value)
- if side == rsSwitchID and address == adapterAAddress then
- rsSwitch = value > 0
- --print("RSChange: [side="..side.."][rsSwitchID="..rsSwitchID.."][value="..value.."]")
- return
- end
- if side == rsclockID and value > 0 and address == adapterAAddress then
- rsTick = true
- end
- end
- function onKeyPress(_, _, char, code, _)
- if char == 32 or code == kB.keys.space then
- if shouldExit == true then
- print("Command to shutdown already sent! Wait for mandatory cycles to finish")
- return
- end
- print("Program will now terminate")
- shouldExit = true
- end
- end
- --======== Lifecycle Pt.1========
- function preInit()
- local L_rsSwitchValue = rsAdapterA.getInput(rsSwitchID)
- --print("RSInit Value: "..L_rsSwitchValue)
- if L_rsSwitchValue > 0 then
- rsSwitch = true
- else
- rsSwitch = false
- end
- event.listen("redstone_changed", onRsChange)
- event.listen("key_down", onKeyPress)
- end
- function onShutdown()
- rsAdapterA.setOutput(rsSwitchID, 0)
- rsAdapterA.setOutput(rsclockID, 0)
- rsAdapterA.setOutput(rsCounterID, 0)
- rsAdapterB.setOutput(rsLeftSand, 0)
- rsAdapterB.setOutput(rsLeftSkulls, 0)
- rsAdapterB.setOutput(rsRightSand, 0)
- rsAdapterB.setOutput(rsRightSkulls, 0)
- event.ignore("redstone_changed", onRsChange)
- event.ignore("key_down", onKeyPress)
- term.clear()
- term.setCursor(1, 0)
- os.exit()
- end
- --======== Internal vars ========
- local currTotalSand = 0
- local currTotalSkulls = 0
- local witherCount = 0
- local cycle = 0
- local idleCounter = 0
- --======== Lifecycle Pt.2 ========
- function beforeTick()
- local L_rsSwitchValue = rsAdapterA.getInput(rsSwitchID)
- if L_rsSwitchValue > 0 then
- rsSwitch = true
- else
- rsSwitch = false
- end
- local sandStack = chestAdapter.getStackInSlot(soulSandChest, 1)
- if sandStack == nil then
- currTotalSand = 0
- else
- currTotalSand = sandStack.size
- end
- local skullsStack = chestAdapter.getStackInSlot(skullsChest, 1)
- if skullsStack == nil then
- currTotalSkulls = 0
- else
- currTotalSkulls = skullsStack.size
- end
- witherCount = math.floor(math.min(currTotalSand/4, currTotalSkulls/3))
- rsAdapterA.setOutput(rsCounterID, witherCount)
- --[[
- Cycles:
- 0 - idle
- 1 - left sand
- 2 - wait for sand
- 3 - left skulls (if cant spawn another wither will jump to idle)
- 4 - right sand
- 5 - wait for samd
- 6 - right skulls
- --]]
- if witherCount <= 0 and cycle ~= 0 and (cycle == 3 or cycle == 6) then
- cycle = 0
- print("Out of wither materials (Sand = "..currTotalSand.." : Skulls = "..currTotalSkulls..")")
- return
- end
- --print("Before cycle: "..cycle.." : witherCount: "..witherCount.. " : RSSwitch"..(rsSwitch and "true" or "false") )
- if cycle == 6 then
- cycle = 0
- elseif cycle == 3 and (witherCount <= 0 or rsSwitch == false) then
- cycle = 0
- elseif cycle == 6 and (witherCount <= 0 or rsSwitch == false) then
- cycle = 0
- end
- if cycle == 0 and (witherCount <= 0 or rsSwitch == false) then
- return
- end
- cycle = cycle + 1
- end
- function tick()
- if (cycle == 0 and idleCounter == 0) or cycle ~= 0 then
- print("Current Cycle: "..cycle)
- end
- if cycle == 0 then
- os.sleep(0.1)
- idleCounter = idleCounter + 1
- if idleCounter >= 10 then
- print("Idling")
- idleCounter = 1
- if witherCount <= 0 then
- print("Out of wither materials (Sand = "..currTotalSand.." : Skulls = "..currTotalSkulls..")")
- end
- if rsSwitch == false then
- print("Farm Offline")
- end
- end
- return
- else
- idleCounter = 0
- end
- if cycle == 1 then
- print("Setting left sand")
- rsAdapterB.setOutput(rsLeftSand, 15)
- os.sleep(1)
- rsAdapterB.setOutput(rsLeftSand, 0)
- return
- end
- if cycle == 2 then
- print("Waiting 3 seconds")
- os.sleep(3)
- return
- end
- if cycle == 3 then
- print("Setting left skulls")
- rsAdapterB.setOutput(rsLeftSkulls, 15)
- os.sleep(1)
- rsAdapterB.setOutput(rsLeftSkulls, 0)
- return
- end
- if cycle == 4 then
- print("Setting right sand")
- rsAdapterB.setOutput(rsRightSand, 15)
- os.sleep(1)
- rsAdapterB.setOutput(rsRightSand, 0)
- return
- end
- if cycle == 5 then
- print("Waiting 3 seconds")
- os.sleep(3)
- return
- end
- if cycle == 6 then
- print("Setting right skulls")
- rsAdapterB.setOutput(rsRightSkulls, 15)
- os.sleep(1)
- rsAdapterB.setOutput(rsRightSkulls, 0)
- return
- end
- end
- function afterTick()
- os.sleep(0.1)
- end
- --======== Program =========
- print("PreInit Start")
- preInit()
- print("PreInit Completed")
- print("Loop Start")
- while true do
- ::repeat_loop::
- if shouldExit == true and (cycle == 0 or cycle == 3 or cycle == 6) then
- break
- end
- if rsTick then
- --print("Tick Start")
- beforeTick()
- tick()
- afterTick()
- --print("Tick end")
- goto repeat_loop
- end
- os.sleep(0.1)
- end
- print("Shuting down")
- onShutdown()
Add Comment
Please, Sign In to add comment