Advertisement
kyle1320

Untitled

Mar 18th, 2013
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. os.loadAPI("file")
  2.  
  3. parent = 2044
  4. retriever = 1963
  5.  
  6. namesToIDs = file.read("turtleIDs")
  7.  
  8. rednet.open("right")
  9.  
  10. save = function()
  11. file.write("turtleIDs", namesToIDs)
  12. end
  13.  
  14. split = function(str, index)
  15. pos = string.find(str, " ")
  16.  
  17. if index == 1 then
  18. return string.sub(str, 0, pos-1)
  19. elseif index == 2 then
  20. return string.sub(str, pos + 1)
  21. end
  22. end
  23.  
  24. listenForCommands = function()
  25. while true do
  26. message = read()
  27. cmd = split(message, 1)
  28. args = split(message, 2)
  29.  
  30. if cmd == "get" then
  31. itemName = split(args, 1)
  32. itemCount = split(args, 2) + 0
  33. turtleID = namesToIDs[itemName]
  34.  
  35. if turtleID ~= nil then
  36. rednet.send(turtleID, textutils.serialize({["label"]="getItem", ["amount"]=itemCount}))
  37. else
  38. print("Unknown item!")
  39. end
  40. elseif cmd == "count" then
  41. itemName = split(args, 1)
  42. itemCount = split(args, 2)
  43. turtleID = namesToIDs[itemName]
  44.  
  45. if turtleID ~= nil then
  46. rednet.send(retriever, textutils.serialize({[1]=1}))
  47. else
  48. print("Unknown item!")
  49. end
  50. elseif cmd == "cmdall" then
  51. for name, turtle in ipairs(namesToIDs) do
  52. rednet.send(turtle, textutils.serialize({["label"]="command", ["command"]=args}))
  53. end
  54. end
  55. end
  56. end
  57.  
  58. listenForCalls = function()
  59. while true do
  60. ID, message = rednet.receive()
  61. msg = textutils.unserialize(message)
  62. if type(msg) ~= "table" then
  63. msg = {}
  64. end
  65.  
  66. if msg["label"] == "register" then
  67. namesToIDs[msg["itemname"]] = msg["turtleid"]
  68. save()
  69. elseif msg["label"] == "countReturn" then
  70. print(msg["count"])
  71. end
  72. end
  73. end
  74.  
  75. parallel.waitForAny(listenForCommands, listenForCalls)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement