Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. m = peripheral.wrap("bottom")
  2. m.setTextScale(.5)
  3. rednet.open("top")
  4. os.loadAPI("button")
  5. button.setMon("bottom")
  6. turtles = {name={},ID={},Finshed={},Width={},Lenth={},Depth={},Doen={},PerCntDoen = {}}
  7.  
  8. function query()
  9.  
  10. local function send()
  11. for i = 0,3 do
  12. print()
  13. write("brodcast "..i.." times")
  14. rednet.broadcast("query quarry")
  15. sleep(5)
  16. end
  17. end
  18.  
  19. local function recv()
  20. local atempts = 0
  21. local senderId, message, distance
  22.  
  23. while true do
  24. senderId, msg, distance = rednet.receive()
  25. message = textutils.unserialize(msg).msg
  26. messageType = textutils.unserialize(msg).Type
  27. tmpWidth = textutils.unserialize(msg).Width
  28. tmpLenth = textutils.unserialize(msg).Lenth
  29. tmpDepth = textutils.unserialize(msg).Depth
  30. tmpDoen = textutils.unserialize(msg).Doen
  31.  
  32. if senderId == nil or message == nil then
  33. --print()
  34. --write("no responce after "..atempts.." atempts")
  35.  
  36. elseif messageType == "query Responce" and message ~= nil and tmpWidth ~= nil and tmpLenth~= nil and tmpDepth ~= nil then
  37. print()
  38. print("computer number "..senderId.." said "..message.." from "..distance.."m away")
  39. for i=1,#turtles.name do
  40. if turtles.ID[i] == senderId and not turtles.Finshed[i] then
  41. message = -1
  42. end
  43. end
  44. if message ~= -1 then
  45. --print(#turtles)
  46. table.insert(turtles.name,message)
  47. table.insert(turtles.ID,senderId)
  48. table.insert(turtles.Width,tmpWidth)
  49. table.insert(turtles.Lenth,tmpLenth)
  50. table.insert(turtles.Depth,tmpDepth)
  51. table.insert(turtles.Finshed,false)
  52. table.insert(turtles.Doen,tmpDoen)
  53. print("and added to table")
  54. end
  55. elseif message ~= nil then
  56. print(messageType)
  57. end
  58. end
  59. end
  60. parallel.waitForAny(send, recv)
  61. redraw()
  62. end
  63.  
  64. function list()
  65. print()
  66. print("computers found:")
  67. for i=1,#turtles.name do
  68. if turtles.PerCntDoen[i] == nil then
  69. turtles.PerCntDoen[i] = math.floor((turtles.Doen[i]/(-turtles.Depth[i])*100)*100) / 100
  70. end
  71. 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")
  72. end
  73. redraw()
  74. end
  75.  
  76. function FUNCstop(ID,index)
  77. m.setCursorPos(5,ID+1)
  78. parallel.waitForAny(function() FUNCgetnet(index) end,function() FUNCsendnet(ID,index) end)
  79. end
  80.  
  81. function FUNCgetnet(i)
  82. while message ~= "Finshed" do
  83. senderId, msg, distance = rednet.receive()
  84. message = textutils.unserialize(msg).msg
  85. messageType = textutils.unserialize(msg).Type
  86. m.setCursorPos(5,i+1)
  87. if messageType == "Stop Responce" and message ~= nil then
  88. m.write(message.." ")
  89. end
  90. end
  91. turtles.Finshed[i] = true
  92. end
  93.  
  94. function redraw()
  95. for i=1,#turtles.name do
  96. 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)
  97. end
  98. button.draw()
  99. for i=1,#turtles.name do
  100. m.setCursorPos(5,i+1)
  101. if not turtles.Finshed[i] then
  102. if turtles.PerCntDoen[i] == nil then
  103. turtles.PerCntDoen[i] = math.floor((turtles.Doen[i]/(-turtles.Depth[i])*100)*100) / 100
  104. end
  105. 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")
  106. else
  107. m.write(turtles.name[i].." ID:"..turtles.ID[i].." Finshed ")
  108. end
  109. end
  110. end
  111.  
  112. function FUNCsendnet(ID,i)
  113. if not turtles.Finshed[i] then
  114. m.setCursorPos(5,i+1)
  115. m.write("msg sent ")
  116. while not turtles.Finshed[i] do
  117. rednet.send(ID,"stop")
  118. sleep(0.5)
  119. end
  120. end
  121. end
  122.  
  123. function CheckButton()
  124. while true do
  125. button.check()
  126. end
  127. end
  128.  
  129. function Listen()
  130. local TblNum = 0
  131. while true do
  132. TblNum = 0
  133. local senderId, IN, distance = rednet.receive()
  134. --print("Got: "..IN) --De-bug
  135. local message = textutils.unserialize(IN).msg
  136. local messageType = textutils.unserialize(IN).Type
  137.  
  138. for i=1,#turtles.ID do
  139. if turtles.ID[i] == senderId then
  140. TblNum = i
  141. end
  142. end
  143.  
  144. if messageType ~= nil then
  145. if messageType == "Stop Responce" and TblNum ~= 0 then
  146. m.setCursorPos(5,TblNum+1)
  147. m.write(message.." ")
  148. if message == "Finshed" and TblNum ~= 0 then
  149. turtles.Finshed[TblNum] = true
  150. end
  151. elseif messageType == "Startup" and TblNum == 0 and IN ~= nil and message ~= nil then
  152. --print("Startup recived") --De-bug
  153. --print("Trying Width") --De-bug
  154. local tmpWidth = textutils.unserialize(IN).Width
  155. --print("Trying Lenth") --De-bug
  156. local tmpLenth = textutils.unserialize(IN).Lenth
  157. --print("Trying Depth") --De-bug
  158. local tmpDepth = textutils.unserialize(IN).Depth
  159. --print("Trying Doen") --De-bug
  160. local tmpDoen = textutils.unserialize(IN).Doen
  161. print()
  162. --print(message) --De-bug
  163. print("computer number "..senderId.." said "..message.." from "..distance.."m away")
  164. table.insert(turtles.name,message)
  165. table.insert(turtles.ID,senderId)
  166. table.insert(turtles.Width,tmpWidth)
  167. table.insert(turtles.Lenth,tmpLenth)
  168. table.insert(turtles.Depth,tmpDepth)
  169. table.insert(turtles.Finshed,false)
  170. table.insert(turtles.Doen,tmpDoen)
  171. if #turtles.Doen == nil then
  172. TblNum = 1
  173. else
  174. TblNum = tonumber(#turtles.Doen)
  175. end
  176. --print(#turtles.Doen)--De-bug
  177. --print(TblNum)--De-bug
  178. --print(turtles.Doen[TblNum])--De-bug
  179. --print(-turtles.Depth[TblNum])--De-bug
  180. table.insert(turtles.PerCntDoen,math.floor((turtles.Doen[TblNum]/(-turtles.Depth[TblNum])*100)*100) / 100)
  181. print("and added to table")
  182. rednet.send(senderId,"query quarry")
  183. redraw()
  184. elseif messageType == "PosUpdate" and TblNum ~= 0 then
  185. local tmpDoen = textutils.unserialize(IN).Doen
  186. turtles.Doen[TblNum] = tmpDoen
  187. turtles.PerCntDoen[TblNum] = math.floor((turtles.Doen[TblNum]/(-turtles.Depth[TblNum])*100)*100) / 100
  188. --print("now "..turtles.PerCntDoen[TblNum].."% Doen") --De-bug
  189. redraw()
  190. elseif message ~= nil then
  191. print("Unknowen message type: "..messageType.."; witch said: "..message)
  192. end
  193. end
  194. end
  195. end
  196.  
  197. query()
  198. list()
  199. button.add("query","Requery","flash",1,1,7,1,colors.gray,colors.green,colors.black,query)
  200. button.add("Relist","Relist","flash",9,1,15,1,colors.gray,colors.green,colors.black,list)
  201. redraw()
  202.  
  203. parallel.waitForAny(CheckButton,Listen)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement