charonme

2turtle quarry

Feb 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. -- 2turtle quarry
  2. -- http://pastebin.com/edit/12VN4iph
  3. -- for FTB infinity evolved 2.6.0 (minecraft 1.7.10)
  4. -- ----------- config ----------------
  5. local channel1 = 31067 -- channel number for turtle 1 (arbitrarily chosen)
  6. local channel2 = 31337 -- channel number for turtle 2
  7. local label1 = "t1" -- label of turtle 1 (type label set [label1] in the turtle interface)
  8. local label2 = "t2" -- label of turtle 2
  9. local modemSide1 = "right" -- which side the modem is attached on turtle 1
  10. local modemSide2 = "right"
  11. -- ------------ init -----------------
  12. local lastEvent = false
  13. local modem
  14. local label = os.getComputerLabel()
  15. local channel -- what channel the turtle will broadcast messages to
  16. local channelListen -- turtle 1 will listen on the channel set for turtle 2 and vice versa
  17. if label == label1 then
  18.     -- turtle 1
  19.     channel = channel1
  20.     channelListen = channel2
  21.     modem = peripheral.wrap(modemSide1)
  22. else
  23.     -- turtle 2
  24.     channel = channel2
  25.     channelListen = channel1
  26.     modem = peripheral.wrap(modemSide2)
  27. end
  28. modem.open(channelListen)
  29.  
  30. -- ----------------------------------
  31. local eventBuffer = {} -- 1=first index, 2=second index
  32. local eventBufferSize = 0 -- 0 means no events in buffer, 1=one event with index 1, 2=two events with indices 1 and 2, etc
  33. local eventBufferReading = false
  34. local eventBufferWriting = false
  35. local function eventBufferPush(event)
  36.     eventBufferWriting = true
  37.     while eventBufferReading do
  38.         os.sleep(0.05)
  39.     end
  40.     table.insert(eventBuffer,event)
  41.     eventBufferSize = eventBufferSize+1
  42.     eventBufferWriting = false
  43. end
  44. local function eventBufferPop() -- returns false if there was no event
  45.     local event = false
  46.     eventBufferReading = true
  47.    
  48.     if not eventBufferWriting and eventBufferSize>0 then
  49.  
  50.         event = table.remove(eventBuffer,1) -- this moves the rest of items, maybe "linked list" would be better for this
  51.  
  52.         eventBufferSize = eventBufferSize-1
  53.     end
  54.     eventBufferReading = false
  55.     return event
  56. end
  57.  
  58. local function processEvents()
  59.     local event = {}
  60.     while true do
  61.         event = {os.pullEvent("modem_message")}
  62.         eventBufferPush(event)
  63.     end
  64. end
  65. local function checkForMessages()
  66.     local noMoreMessages = false
  67.     local event = false
  68.     while not noMoreMessages do -- receive and process all messages waiting in queue
  69.         if eventBufferSize>0 then
  70.             --print("calling eventBufferPop()")
  71.             event = eventBufferPop()
  72.             if event~=false and event[1]=="modem_message" then
  73.                 print(event[5].." ["..event[6].."]")
  74.                 --print("event2: ",event[2]) -- side
  75.                 --print("event3: ",event[3]) -- channel "The frequency that the message has been received on."
  76.                 --print("event4: ",event[4]) -- channel "The frequency that the sender defined when sending the message."
  77.                 --print("event5: ",event[5]) -- message content
  78.                 --print("event6: ",event[6]) -- distance
  79.             else
  80.                 noMoreMessages = true
  81.             end
  82.         else
  83.             noMoreMessages = true
  84.         end
  85.     end
  86. end
  87.  
  88. -- --------------------------------- main
  89. local function main()
  90.     local cnt = 0
  91.     while true do
  92.         --print("calling checkForMessages()")
  93.         checkForMessages()
  94.         --print("sleeping")
  95.         if label == label1 then
  96.             os.sleep(2)
  97.         else
  98.             os.sleep(3)
  99.         end
  100.         print("transmitting message "..cnt.."...")
  101.         modem.transmit(channel,channelListen,"msg "..cnt.." from "..label)
  102.         cnt = cnt+1
  103.     end
  104. end
  105. -- ---------------------------------
  106. parallel.waitForAny(processEvents, main)
Add Comment
Please, Sign In to add comment