Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- 2turtle quarry
- -- http://pastebin.com/edit/12VN4iph
- -- for FTB infinity evolved 2.6.0 (minecraft 1.7.10)
- -- ----------- config ----------------
- local channel1 = 31067 -- channel number for turtle 1 (arbitrarily chosen)
- local channel2 = 31337 -- channel number for turtle 2
- local label1 = "t1" -- label of turtle 1 (type label set [label1] in the turtle interface)
- local label2 = "t2" -- label of turtle 2
- local modemSide1 = "right" -- which side the modem is attached on turtle 1
- local modemSide2 = "right"
- -- ------------ init -----------------
- local lastEvent = false
- local modem
- local label = os.getComputerLabel()
- local channel -- what channel the turtle will broadcast messages to
- local channelListen -- turtle 1 will listen on the channel set for turtle 2 and vice versa
- if label == label1 then
- -- turtle 1
- channel = channel1
- channelListen = channel2
- modem = peripheral.wrap(modemSide1)
- else
- -- turtle 2
- channel = channel2
- channelListen = channel1
- modem = peripheral.wrap(modemSide2)
- end
- modem.open(channelListen)
- -- ----------------------------------
- local eventBuffer = {} -- 1=first index, 2=second index
- local eventBufferSize = 0 -- 0 means no events in buffer, 1=one event with index 1, 2=two events with indices 1 and 2, etc
- local eventBufferReading = false
- local eventBufferWriting = false
- local function eventBufferPush(event)
- eventBufferWriting = true
- while eventBufferReading do
- os.sleep(0.05)
- end
- table.insert(eventBuffer,event)
- eventBufferSize = eventBufferSize+1
- eventBufferWriting = false
- end
- local function eventBufferPop() -- returns false if there was no event
- local event = false
- eventBufferReading = true
- if not eventBufferWriting and eventBufferSize>0 then
- event = table.remove(eventBuffer,1) -- this moves the rest of items, maybe "linked list" would be better for this
- eventBufferSize = eventBufferSize-1
- end
- eventBufferReading = false
- return event
- end
- local function processEvents()
- local event = {}
- while true do
- event = {os.pullEvent("modem_message")}
- eventBufferPush(event)
- end
- end
- local function checkForMessages()
- local noMoreMessages = false
- local event = false
- while not noMoreMessages do -- receive and process all messages waiting in queue
- if eventBufferSize>0 then
- --print("calling eventBufferPop()")
- event = eventBufferPop()
- if event~=false and event[1]=="modem_message" then
- print(event[5].." ["..event[6].."]")
- --print("event2: ",event[2]) -- side
- --print("event3: ",event[3]) -- channel "The frequency that the message has been received on."
- --print("event4: ",event[4]) -- channel "The frequency that the sender defined when sending the message."
- --print("event5: ",event[5]) -- message content
- --print("event6: ",event[6]) -- distance
- else
- noMoreMessages = true
- end
- else
- noMoreMessages = true
- end
- end
- end
- -- --------------------------------- main
- local function main()
- local cnt = 0
- while true do
- --print("calling checkForMessages()")
- checkForMessages()
- --print("sleeping")
- if label == label1 then
- os.sleep(2)
- else
- os.sleep(3)
- end
- print("transmitting message "..cnt.."...")
- modem.transmit(channel,channelListen,"msg "..cnt.." from "..label)
- cnt = cnt+1
- end
- end
- -- ---------------------------------
- parallel.waitForAny(processEvents, main)
Add Comment
Please, Sign In to add comment