Advertisement
Guest User

startup

a guest
Jul 24th, 2016
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. --IC2 Reinforced Stone automation
  2. --CF Turtle master program
  3. local modem = peripheral.wrap("right")
  4. modem.open(2)
  5.  
  6. --create a table to store all 10 blocks' states
  7. local cfBlockState = {}
  8.  
  9. --infinite loop to check if a block exists
  10. while true do
  11.  
  12. --loop until all 10 blocks match
  13.   local cfBlocksMatch = false
  14.   while not cfBlocksMatch do
  15.     cfBlocksMatch = true
  16. --inspect the block in front of master and store its name
  17.     local inspectSuccess, inspectTable = turtle.inspect()
  18.  
  19.     if inspectSuccess then
  20.       local blockName = inspectTable["name"]
  21.       cfBlockState["CF Turtle 10"] = blockName
  22.     else
  23.       cfBlockState["CF Turtle 10"] = "empty"
  24.     end
  25.  
  26. --compare the other 9 turtles' blocks with master's block
  27.     for i = 1, 9 do
  28. --request block state for turtle i
  29.       modem.transmit(1, 2, "cfBlockInspect: CF Turtle "..i)
  30.  
  31. --loop event listener in case of timeout until we get a response
  32.       local responseAck = false
  33.       while not responseAck do
  34.         local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  35.         if event then
  36.           responseAck = true
  37. --parse the response from the turtle
  38.           local senderLabel, senderBlock = string.match(message, '([%w%s%d]*):%s([%w%s%d%p]*)')
  39.  
  40. --store the response in the block state table and compare with master's block
  41.           cfBlockState[senderLabel] = senderBlock
  42.           if senderBlock ~= cfBlockState["CF Turtle 10"] then
  43.             cfBlocksMatch = false
  44.           end
  45.  
  46.         else --no modem event
  47.           print("No resposne from CF Turtle "..i)
  48.           print("os.pullEvent timeout. No modem event detected. Reinitializing event listener...")
  49.         end --modem event exists
  50.       end --responseAck
  51.     end --cfBlockInspect for loop
  52.  
  53. --print all block states for each batch of block checks
  54.     if cfBlocksMatch then
  55.       print("All blocks match: "..cfBlockState["CF Turtle 10"])
  56. --trigger CF sprayer redstone
  57.       if cfBlockState["CF Turtle 10"] == "IC2:blockIronScaffold" then
  58.         rs.setBundledOutput("left", colors.white)
  59. --trigger sand autonomous activator redstone
  60.       elseif cfBlockState["CF Turtle 10"] == "IC2:blockReinforcedFoam" then
  61.         rs.setBundledOutput("left", colors.orange)
  62. --trigger annihilation plane redstone
  63.       elseif cfBlockState["CF Turtle 10"] == "IC2:blockAlloy" then
  64.         rs.setBundledOutput("left", colors.black)
  65. --trigger formation plane redstone
  66.       elseif cfBlockState["CF Turtle 10"] == "empty" then
  67.         rs.setBundledOutput("left", colors.blue)
  68. --wait a tick so we don't spam processing as much when idle
  69.         sleep(0.05)
  70. --enable non-CF block detection redstone. You may wish to use this to trigger an alarm or other alert
  71.       else
  72.         rs.setBundledOutput("left", colors.red)
  73.       end
  74.     else --blocks don't match
  75.       print("1 or more blocks don't match")
  76.       local nonCFBlock = false
  77.       local partialAnnihilation = true
  78.       for k, v in pairs(cfBlockState) do
  79.         if v ~= "IC2:blockIronScaffold" and v ~= "IC2:blockReinforcedFoam" and v ~= "IC2:blockAlloy" and v ~= "empty" then
  80.           nonCFBlock = true
  81. --enable non-CF block detection redstone
  82.           rs.setBundledOutput("left", colors.red)
  83.         end
  84.         print(k..": "..v)
  85.         if v ~= "IC2:blockAlloy" and v ~= "empty" then
  86.           partialAnnihilation = false
  87.         end
  88.       end
  89. --enable annihilation plane redstone if all blocks are either reinforced stone or empty - in case annihilation does not happen simultaneously
  90.       if partialAnnihilation then
  91.         rs.setBundledOutput("left", colors.black)
  92.       end
  93.       if nonCFBlock then
  94.         print("**WARNING: non-CF block detected**")
  95.       end
  96.     end --blocks match
  97.  
  98.   end --cfBlocksMatch
  99.  
  100. end --infinite while loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement