Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --IC2 Reinforced Stone automation
- --CF Turtle master program
- local modem = peripheral.wrap("right")
- modem.open(2)
- --create a table to store all 10 blocks' states
- local cfBlockState = {}
- --infinite loop to check if a block exists
- while true do
- --loop until all 10 blocks match
- local cfBlocksMatch = false
- while not cfBlocksMatch do
- cfBlocksMatch = true
- --inspect the block in front of master and store its name
- local inspectSuccess, inspectTable = turtle.inspect()
- if inspectSuccess then
- local blockName = inspectTable["name"]
- cfBlockState["CF Turtle 10"] = blockName
- else
- cfBlockState["CF Turtle 10"] = "empty"
- end
- --compare the other 9 turtles' blocks with master's block
- for i = 1, 9 do
- --request block state for turtle i
- modem.transmit(1, 2, "cfBlockInspect: CF Turtle "..i)
- --loop event listener in case of timeout until we get a response
- local responseAck = false
- while not responseAck do
- local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
- if event then
- responseAck = true
- --parse the response from the turtle
- local senderLabel, senderBlock = string.match(message, '([%w%s%d]*):%s([%w%s%d%p]*)')
- --store the response in the block state table and compare with master's block
- cfBlockState[senderLabel] = senderBlock
- if senderBlock ~= cfBlockState["CF Turtle 10"] then
- cfBlocksMatch = false
- end
- else --no modem event
- print("No resposne from CF Turtle "..i)
- print("os.pullEvent timeout. No modem event detected. Reinitializing event listener...")
- end --modem event exists
- end --responseAck
- end --cfBlockInspect for loop
- --print all block states for each batch of block checks
- if cfBlocksMatch then
- print("All blocks match: "..cfBlockState["CF Turtle 10"])
- --trigger CF sprayer redstone
- if cfBlockState["CF Turtle 10"] == "IC2:blockIronScaffold" then
- rs.setBundledOutput("left", colors.white)
- --trigger sand autonomous activator redstone
- elseif cfBlockState["CF Turtle 10"] == "IC2:blockReinforcedFoam" then
- rs.setBundledOutput("left", colors.orange)
- --trigger annihilation plane redstone
- elseif cfBlockState["CF Turtle 10"] == "IC2:blockAlloy" then
- rs.setBundledOutput("left", colors.black)
- --trigger formation plane redstone
- elseif cfBlockState["CF Turtle 10"] == "empty" then
- rs.setBundledOutput("left", colors.blue)
- --wait a tick so we don't spam processing as much when idle
- sleep(0.05)
- --enable non-CF block detection redstone. You may wish to use this to trigger an alarm or other alert
- else
- rs.setBundledOutput("left", colors.red)
- end
- else --blocks don't match
- print("1 or more blocks don't match")
- local nonCFBlock = false
- local partialAnnihilation = true
- for k, v in pairs(cfBlockState) do
- if v ~= "IC2:blockIronScaffold" and v ~= "IC2:blockReinforcedFoam" and v ~= "IC2:blockAlloy" and v ~= "empty" then
- nonCFBlock = true
- --enable non-CF block detection redstone
- rs.setBundledOutput("left", colors.red)
- end
- print(k..": "..v)
- if v ~= "IC2:blockAlloy" and v ~= "empty" then
- partialAnnihilation = false
- end
- end
- --enable annihilation plane redstone if all blocks are either reinforced stone or empty - in case annihilation does not happen simultaneously
- if partialAnnihilation then
- rs.setBundledOutput("left", colors.black)
- end
- if nonCFBlock then
- print("**WARNING: non-CF block detected**")
- end
- end --blocks match
- end --cfBlocksMatch
- end --infinite while loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement