Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- m = peripheral.wrap("bottom")
- m.setTextScale(.5)
- rednet.open("top")
- os.loadAPI("button")
- button.setMon("bottom")
- turtles = {name={},ID={},Finshed={},Width={},Lenth={},Depth={},Doen={},PerCntDoen = {}}
- function query()
- local function send()
- for i = 0,3 do
- print()
- write("brodcast "..i.." times")
- rednet.broadcast("query quarry")
- sleep(5)
- end
- end
- local function recv()
- local atempts = 0
- local senderId, message, distance
- while true do
- senderId, msg, distance = rednet.receive()
- message = textutils.unserialize(msg).msg
- messageType = textutils.unserialize(msg).Type
- tmpWidth = textutils.unserialize(msg).Width
- tmpLenth = textutils.unserialize(msg).Lenth
- tmpDepth = textutils.unserialize(msg).Depth
- tmpDoen = textutils.unserialize(msg).Doen
- if senderId == nil or message == nil then
- --print()
- --write("no responce after "..atempts.." atempts")
- elseif messageType == "query Responce" and message ~= nil and tmpWidth ~= nil and tmpLenth~= nil and tmpDepth ~= nil then
- print()
- print("computer number "..senderId.." said "..message.." from "..distance.."m away")
- for i=1,#turtles.name do
- if turtles.ID[i] == senderId and not turtles.Finshed[i] then
- message = -1
- end
- end
- if message ~= -1 then
- --print(#turtles)
- table.insert(turtles.name,message)
- table.insert(turtles.ID,senderId)
- table.insert(turtles.Width,tmpWidth)
- table.insert(turtles.Lenth,tmpLenth)
- table.insert(turtles.Depth,tmpDepth)
- table.insert(turtles.Finshed,false)
- table.insert(turtles.Doen,tmpDoen)
- print("and added to table")
- end
- elseif message ~= nil then
- print(messageType)
- end
- end
- end
- parallel.waitForAny(send, recv)
- redraw()
- end
- function list()
- print()
- print("computers found:")
- for i=1,#turtles.name do
- if turtles.PerCntDoen[i] == nil then
- turtles.PerCntDoen[i] = math.floor((turtles.Doen[i]/(-turtles.Depth[i])*100)*100) / 100
- end
- print(turtles.name[i].." ID:"..turtles.ID[i].." Diging: "..turtles.Width[i].."m X "..turtles.Lenth[i].."m X "..turtles.Depth[i].."m is "..turtles.PerCntDoen[i].."% doen")
- end
- redraw()
- end
- function FUNCstop(ID,index)
- m.setCursorPos(5,ID+1)
- parallel.waitForAny(function() FUNCgetnet(index) end,function() FUNCsendnet(ID,index) end)
- end
- function FUNCgetnet(i)
- while message ~= "Finshed" do
- senderId, msg, distance = rednet.receive()
- message = textutils.unserialize(msg).msg
- messageType = textutils.unserialize(msg).Type
- m.setCursorPos(5,i+1)
- if messageType == "Stop Responce" and message ~= nil then
- m.write(message.." ")
- end
- end
- turtles.Finshed[i] = true
- end
- function redraw()
- for i=1,#turtles.name do
- button.add(turtles.name[i],"Stop","flash",1,i+1,4,i+1,colors.gray,colors.green,colors.black,function() FUNCstop(turtles.ID[i],i) end)
- end
- button.draw()
- for i=1,#turtles.name do
- m.setCursorPos(5,i+1)
- if not turtles.Finshed[i] then
- if turtles.PerCntDoen[i] == nil then
- turtles.PerCntDoen[i] = math.floor((turtles.Doen[i]/(-turtles.Depth[i])*100)*100) / 100
- end
- m.write(turtles.name[i].." ID:"..turtles.ID[i].." Diging: "..turtles.Width[i].."m X "..turtles.Lenth[i].."m X "..turtles.Depth[i].."m is "..turtles.PerCntDoen[i].."% doen")
- else
- m.write(turtles.name[i].." ID:"..turtles.ID[i].." Finshed ")
- end
- end
- end
- function FUNCsendnet(ID,i)
- if not turtles.Finshed[i] then
- m.setCursorPos(5,i+1)
- m.write("msg sent ")
- while not turtles.Finshed[i] do
- rednet.send(ID,"stop")
- sleep(0.5)
- end
- end
- end
- function CheckButton()
- while true do
- button.check()
- end
- end
- function Listen()
- local TblNum = 0
- while true do
- TblNum = 0
- local senderId, IN, distance = rednet.receive()
- --print("Got: "..IN) --De-bug
- local message = textutils.unserialize(IN).msg
- local messageType = textutils.unserialize(IN).Type
- for i=1,#turtles.ID do
- if turtles.ID[i] == senderId then
- TblNum = i
- end
- end
- if messageType ~= nil then
- if messageType == "Stop Responce" and TblNum ~= 0 then
- m.setCursorPos(5,TblNum+1)
- m.write(message.." ")
- if message == "Finshed" and TblNum ~= 0 then
- turtles.Finshed[TblNum] = true
- end
- elseif messageType == "Startup" and TblNum == 0 and IN ~= nil and message ~= nil then
- --print("Startup recived") --De-bug
- --print("Trying Width") --De-bug
- local tmpWidth = textutils.unserialize(IN).Width
- --print("Trying Lenth") --De-bug
- local tmpLenth = textutils.unserialize(IN).Lenth
- --print("Trying Depth") --De-bug
- local tmpDepth = textutils.unserialize(IN).Depth
- --print("Trying Doen") --De-bug
- local tmpDoen = textutils.unserialize(IN).Doen
- print()
- --print(message) --De-bug
- print("computer number "..senderId.." said "..message.." from "..distance.."m away")
- table.insert(turtles.name,message)
- table.insert(turtles.ID,senderId)
- table.insert(turtles.Width,tmpWidth)
- table.insert(turtles.Lenth,tmpLenth)
- table.insert(turtles.Depth,tmpDepth)
- table.insert(turtles.Finshed,false)
- table.insert(turtles.Doen,tmpDoen)
- if #turtles.Doen == nil then
- TblNum = 1
- else
- TblNum = tonumber(#turtles.Doen)
- end
- --print(#turtles.Doen)--De-bug
- --print(TblNum)--De-bug
- --print(turtles.Doen[TblNum])--De-bug
- --print(-turtles.Depth[TblNum])--De-bug
- table.insert(turtles.PerCntDoen,math.floor((turtles.Doen[TblNum]/(-turtles.Depth[TblNum])*100)*100) / 100)
- print("and added to table")
- rednet.send(senderId,"query quarry")
- redraw()
- elseif messageType == "PosUpdate" and TblNum ~= 0 then
- local tmpDoen = textutils.unserialize(IN).Doen
- turtles.Doen[TblNum] = tmpDoen
- turtles.PerCntDoen[TblNum] = math.floor((turtles.Doen[TblNum]/(-turtles.Depth[TblNum])*100)*100) / 100
- --print("now "..turtles.PerCntDoen[TblNum].."% Doen") --De-bug
- redraw()
- elseif message ~= nil then
- print("Unknowen message type: "..messageType.."; witch said: "..message)
- end
- end
- end
- end
- query()
- list()
- button.add("query","Requery","flash",1,1,7,1,colors.gray,colors.green,colors.black,query)
- button.add("Relist","Relist","flash",9,1,15,1,colors.gray,colors.green,colors.black,list)
- redraw()
- parallel.waitForAny(CheckButton,Listen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement