ksaw000

master turtle

Apr 5th, 2021 (edited)
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. network={}
  2. rednet.open("right")
  3. MAX_DIG_SLICE = 4
  4. num_turtles=0
  5.  
  6. function tablefind(tab,el)
  7. for index, value in pairs(tab) do
  8. if value == el then
  9. return index
  10. end
  11. end
  12. end
  13.  
  14. function addTurtle()
  15. turtle.suckUp()
  16. turtle.place()
  17. local new_turtle=peripheral.wrap("front")
  18. new_turtle.turnOn()
  19. local id,message = rednet.receive() --confirmation message upon successful startup
  20. if message == "added" then
  21. table.insert( network, id )
  22. print("added ".. id)
  23. os.sleep(1)
  24. rednet.broadcast(tostring(id) .. " moveTo 143 70 555")
  25. os.sleep(10)
  26. return id
  27. end
  28.  
  29. end
  30.  
  31. function removeTurtle(id)
  32. turtle.dig()
  33. table.remove(network, tablefind(network, id))
  34. end
  35.  
  36. function max(t, fn)
  37. if #t == 0 then return nil, nil end
  38. local key, value = 1, t[1]
  39. for i = 2, #t do
  40. if fn(value, t[i]) then
  41. key, value = i, t[i]
  42. end
  43. end
  44. return key
  45. end
  46.  
  47. function getNumTurtles(dimension)
  48. local n = math.ceil(dimension / MAX_DIG_SLICE)
  49. print(n)
  50. if n > 27 then
  51. MAX_DIG_SLICE=MAX_DIG_SLICE+1
  52. getNumTurtles(dimension)
  53. else
  54. return n
  55. end
  56. end
  57.  
  58. function assignDig(n, x1,y1,z1,x2,y2,z2, max_dim_index)
  59. while n>0 do
  60. print(x1,y1,z1,x2,y2,z2)
  61. local new_turtle_ID = addTurtle()
  62. n=n-1
  63. local new_turtle_ID=network[table.getn(network)]
  64. if max_dim_index == 1 then
  65. if x1+MAX_DIG_SLICE<x2 then
  66. rednet.broadcast(new_turtle_ID .. " mine " .. tostring(x1).." ".. tostring(y1).." ".. tostring(z1).." ".. tostring(x1+MAX_DIG_SLICE-1).." ".. tostring(y2).." ".. tostring(z2))
  67. x1=x1+MAX_DIG_SLICE
  68. else
  69. rednet.broadcast(new_turtle_ID .. " mine " .. tostring(x1).." ".. tostring(y1).." ".. tostring(z1).." ".. tostring(x2).." ".. tostring(y2).." ".. tostring(z2))
  70. end
  71. elseif max_dim_index == 2 then
  72. if y1-MAX_DIG_SLICE>y2 then
  73. rednet.broadcast(new_turtle_ID .. " mine " .. tostring(x1).." ".. tostring(y1).." ".. tostring(z1).." ".. tostring(x2).." ".. tostring(y1-MAX_DIG_SLICE+1).." ".. tostring(z2))
  74. y1=y1-MAX_DIG_SLICE
  75. else
  76. rednet.broadcast(new_turtle_ID .. " mine " .. tostring(x1).." ".. tostring(y1).." ".. tostring(z1).." ".. tostring(x2).." ".. tostring(y2).." ".. tostring(z2))
  77. end
  78. elseif max_dim_index == 3 then
  79. if z1+MAX_DIG_SLICE<z2 then
  80. rednet.broadcast(new_turtle_ID .. " mine " .. tostring(x1).." ".. tostring(y1).." ".. tostring(z1).." ".. tostring(x2).." ".. tostring(y2).." ".. tostring(z1+MAX_DIG_SLICE-1))
  81. z1=z1+MAX_DIG_SLICE
  82. else
  83. rednet.broadcast(new_turtle_ID .. " mine " .. tostring(x1).." ".. tostring(y1).." ".. tostring(z1).." ".. tostring(x2).." ".. tostring(y2).." ".. tostring(z2))
  84. end
  85. end
  86. end
  87. end
  88.  
  89. function quarry(x1,y1,z1,x2,y2,z2)
  90. rednet.broadcast("quarry called")
  91. local dims = {(math.abs(x1-x2)+1),(math.abs(y1-y2)+1),(math.abs(z1-z2)+1)}
  92. print(dims[1],dims[2],dims[3])
  93. local quarry_volume =dims[1]*dims[2]*dims[3]
  94. print(quarry_volume)
  95. local max_dim_index= max(dims, function(a,b) return a < b end)
  96. print(max_dim_index)
  97. local num_turtles=getNumTurtles(dims[max_dim_index])
  98. local topx,topy,topz,bottomx,bottomy,bottomz = math.min(x1,x2),math.max(y1,y2),math.min(z1,z2),math.max(x1,x2),math.min(y1,y2),math.max(z1,z2)
  99. rednet.broadcast("assigning ".. num_turtles.."turtles")
  100. print(topx,topy,topz)
  101. print(bottomx,bottomy,bottomz)
  102. assignDig(num_turtles , topx,topy,topz,bottomx,bottomy,bottomz, max_dim_index)
  103. end
  104.  
  105. --[[mainloop for master]]
  106. while true do
  107. local id,message = rednet.receive()
  108. command = {}
  109. for word in message:gmatch("%w+") do table.insert(command, word) end
  110. if command[1]=="master" then
  111. table.remove(command,1)
  112. if command[1]=="exit" then
  113. break
  114. elseif command[1]=="add" then
  115. addTurtle()
  116. elseif command[1]=="remove" then
  117. removeTurtle(id)
  118. elseif command[1]=="quarry" then
  119. rednet.broadcast("quarry requested")
  120. quarry(tonumber(command[2]),tonumber(command[3]),tonumber(command[4]),tonumber(command[5]),tonumber(command[6]),tonumber(command[7]))
  121. end
  122. end
  123. end
  124.  
Add Comment
Please, Sign In to add comment