Guest User

Untitled

a guest
Jan 25th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. --Soup by stylishbob
  2. --Variables
  3. tArgs = {...}
  4. local version = 0.2
  5. local turtleSide = "right"
  6. local turtleMineDiameter = 3
  7. local turtleAreaMine = turtleMineDiameter * turtleMineDiameter
  8. print(turtleAreaMine)
  9. local maxSize = 500
  10. local screenX, screenY = term.getSize()
  11. local startCoords = {x = 0, y = 0, z =0, direction = "north"}
  12. local coord = {x = startCoords.x, y = startCoords.y, z = startCoords.z, direction = startCoords.direction}
  13. local turtleCoords = {}
  14. --Functions
  15. local smart = {
  16. dig = function()
  17. while turtle.detect() == true do
  18. turtle.dig()
  19. os.sleep(0.2)
  20. if turtle.detect() == false then
  21. break
  22. end
  23. end
  24. end,
  25. digUp = function()
  26. while turtle.detectUp() == true do
  27. turtle.digUp()
  28. os.sleep(0.2)
  29. if turtle.detectUp() == false then
  30. break
  31. end
  32. end
  33. end,
  34. forward = function(distance)
  35. if distance == nil then
  36. distance = 1
  37. end
  38. for i = 1, distance do
  39. while turtle.forward() == false do
  40. turtle.dig()
  41. os.sleep(0.2)
  42. if turtle.forward() == true then
  43. break
  44. end
  45. end
  46. if direction == "north" then
  47. coord.y = coord.y + 1
  48. elseif direction == "east" then
  49. coord.x = coord.x + 1
  50. elseif direction == "south" then
  51. coord.y = coord.y - 1
  52. elseif direction == "west" then
  53. coord.x = coord.x - 1
  54. else
  55. print("Invalid Direction")
  56. end
  57. end
  58. return coord.x,coord.y
  59. end,
  60. turn = function(turn)
  61. if turn == "left" then
  62. turtle.turnLeft()
  63. if coord.direction == "north" then
  64. coord.direction = "west"
  65. elseif coord.direction == "east" then
  66. coord.direction = "north"
  67. elseif coord.direction == "south" then
  68. coord.direction = "east"
  69. elseif coord.direction == "west" then
  70. coord.direction = "south"
  71. else
  72. print("Error: Invalid Direction")
  73. end
  74. elseif turn == "right" then
  75. turtle.turnRight()
  76. if direction == "north" then
  77. direction = "east"
  78. elseif direction == "east" then
  79. direction = "south"
  80. elseif direction == "south" then
  81. direction = "west"
  82. elseif direction == "west" then
  83. direction = "north"
  84. else
  85. print("Error: Invalid Direction")
  86. end
  87. else
  88. print("Invalid Direction")
  89. end
  90. end,
  91. setDirection = function(strFacing)
  92. repeat
  93. self.setDirection()
  94. until coord.direction == strFacing
  95. end,
  96. up = function(strAmount)
  97. amount = tonumber(strAmount)
  98. if amount == nil then
  99. amount = 1
  100. end
  101. for i = 1, amount do
  102. turtle.up()
  103. end
  104. end,
  105. down = function(strAmount)
  106. amount = tonumber(strAmount)
  107. if amount == nil then
  108. amount = 1
  109. end
  110. for i = 1, amount do
  111. turtle.down()
  112. end
  113. end,
  114. goto = function(x, y, z)
  115. local function alignForward()
  116. repeat
  117. self.forward()
  118. until coord.x == x
  119. end
  120. if coord.x > x then
  121. alignForward()
  122. elseif coord.x < x then
  123. alignForward()
  124. end
  125. end
  126. }
  127. function openModem()
  128. print("Opening Modem...")
  129. rednet.open(turtleSide)
  130. end
  131. function processNumber(number)
  132. --Makes number divisible by three.
  133. if number == nil then
  134. print("Error: No Value")
  135. return false
  136. end
  137. if number % turtleMineDiameter ~= 0 then
  138. print("Desired size invalid! Auto-Correcting...")
  139. repeat
  140. number = number + 1
  141. until number % turtleMineDiameter == 0
  142. end
  143. return number
  144. end
  145. function estimateTime()
  146. --5 Seconds for 1 layer
  147. local argTime = tArgs[2]
  148. local t = tonumber(argTime)
  149. local time = 5 * argTime
  150. local min = time / 60
  151. return round(min,1)
  152. end
  153. function round(num, idp)
  154. local mult = 10^(idp or 0)
  155. return math.floor(num * mult + 0.5) / mult
  156. end
  157. function waitInput()
  158. print("Press Any Key to Continue")
  159. bRead = true
  160. while(bRead) do
  161. local sEvent = os.pullEvent("key")
  162. if(sEvent == "key") then
  163. break
  164. end
  165. end
  166. end
  167. function checkStock(item,strNum,strSlot)
  168. num = tonumber(strNum)
  169. slot = tonumber(strSlot)
  170. turtle.select(slot)
  171. if turtle.getItemCount(slot) < num then
  172. while turtle.getItemCount(slot) <= num do
  173. print("Insert "..num.." "..item.." in Slot #"..slot)
  174. os.sleep(0.4)
  175. if turtle.getItemCount(slot) >= num then
  176. shell.run("clear")
  177. break
  178. end
  179. shell.run("clear")
  180. end
  181. else
  182. print("Slot "..slot.." Supply: OK")
  183. os.sleep(0.6)
  184. shell.run("clear")
  185. end
  186. turtle.select(1)
  187. end
  188. function turtleOn(side)
  189. peripheral.call(side, 'turnOn')
  190. end
  191. function tprint (tbl, indent)
  192. if not indent then indent = 0 end
  193. for k, v in pairs(tbl) do
  194. formatting = string.rep(" ", indent) .. k .. ": "
  195. if type(v) == "table" then
  196. print(formatting)
  197. tprint(v, indent+1)
  198. else
  199. print(formatting .. v)
  200. end
  201. end
  202. end
  203. --Scripts
  204. function deploy(x, y, numTurtle)
  205. --Calculates the lanes in the quarry job.
  206. local lanesX = x / turtleMineDiameter
  207. local lanesY = y / turtleMineDiameter
  208. print(lanesX.." Lanes of Turtles")
  209. local origin = {x= 1,y = 1}
  210. y = y + 1
  211. x = x + 1
  212. for i = 1, lanesX do
  213. local test = y - lanesY * i
  214. table.insert(turtleCoords,i, test)
  215. end
  216. for i = 1, lanesY do
  217. local test = x - lanesX * i
  218. table.insert(turtleCoords,i,test)
  219. end
  220. tprint(turtleCoords)
  221. end
  222. --The Client portion of the program.
  223. function clientMode()
  224. print("Started in Client Mode!")
  225. openModem()
  226. print("Waiting for command!")
  227. local senderID, message, distance = rednet.receive()
  228. end
  229. function masterMode()
  230. --Local Variables
  231. --Simple task variables.
  232. function checkNumber(entry)
  233. if tonumber(entry) == nil then
  234. print("Invalid Entry! Please enter a number!")
  235. return false
  236. elseif tonumber(entry) == 0 then
  237. print("Please enter a number greater than 0")
  238. return false
  239. elseif tonumber(entry) > maxSize then
  240. print("Too Large! Must be under "..maxSize)
  241. return false
  242. end
  243. return true
  244. end
  245. print("Started in Master Mode!")
  246. openModem()
  247. --Gets desired size of quarry.
  248. for i = 1,2 do
  249. while true do
  250. if i == 1 then
  251. term.write("Insert Desired Quarry X size: ")
  252. quarrySizeX = read()
  253. if checkNumber(quarrySizeX) == true then
  254. xSize = processNumber(quarrySizeX)
  255. print("Selected X = "..xSize)
  256. break
  257. end
  258. elseif i == 2 then
  259. term.write("Insert Desired Quarry Y size: ")
  260. quarrySizeY = read()
  261. if checkNumber(quarrySizeY) ~= false then
  262. ySize = processNumber(quarrySizeY)
  263. print("Selected Y = "..ySize)
  264. break
  265. end
  266. end
  267. end
  268. end
  269. shell.run("clear")
  270. print("Quarry Size will be "..xSize.." X "..ySize..".")
  271. --Calculating things...
  272. print("Calculating Setup!")
  273. print(string.rep("=",screenX))
  274. --Turtle mines 3x3
  275. quarryArea = xSize * ySize
  276. print("Area of Quarry:"..quarryArea)
  277. requiredTurtles = quarryArea / turtleAreaMine
  278. print("Amount of Turtles required for Job:"..requiredTurtles)
  279. if tArgs[2] ~= nil then
  280. print("Estimated Time:"..estimateTime().." Minutes.")
  281. end
  282. --Checks whether user wishes to continue or not.
  283. print(string.rep("=",screenX))
  284. waitInput()
  285. layout(quarryArea, xSize, ySize)
  286. for i = 1, requiredTurtles do
  287.  
  288.  
  289. end
  290. end
  291. --Main Program
  292. shell.run("clear")
  293. print("You are running:")
  294. print("Soup v"..version.." Alpha by stylishbob")
  295. --Mode Selector / Selects the mode to run in
  296. if tArgs[1] == "client" then
  297. deploy(9, 9, 9)
  298. elseif tArgs[1] == "master" then
  299. masterMode()
  300. elseif tArgs[1] == "test" then
  301. smart.setDirection("south")
  302. else
  303. print("Invalid Entry! Client or Master Mode!")
  304. end
Advertisement
Add Comment
Please, Sign In to add comment