PSquishyP

CC GPS Rollout

Sep 3rd, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.58 KB | None | 0 0
  1. --
  2. --GPS Deploy by neonerZ v1.1
  3. --http://youtube.com/neonerz
  4. --
  5. --This script is originally from BigSHinyToys from ComputerCraft.info
  6. --Original link can be found here:
  7. --http://www.computercraft.info/forums2/index.php?/topic/3088-how-to-guide-gps-global-position-system/page__p__28333#entry28333
  8. --Edited by neonerZ
  9. --
  10. -- Modifications included:
  11. --
  12. -- happydude11209: Line 209 - added logic to check if the server is set to no fuel mode
  13. -- http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-10/page__view__findpost__p__123411
  14. --
  15. -- kittykiller: Line 110 - Bug in the locate code if you were using an existing GPS system to deploy a new one
  16. -- http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-10/page__view__findpost__p__80323
  17. --
  18. -- Mad_Professor: Line 296 - Bug calling computers, monitors. Making people think they needed to load 4 monitors
  19. -- http://www.computercraft.info/forums2/index.php?/topic/9528-gps-deploy-10/page__view__findpost__p__150291
  20. --
  21. -- noahc3: Startup template - Added rednet calling to have option to confirm GPS' systems are active and working
  22. --
  23. --
  24. --
  25. -- In order to use this script you need to setup
  26. -- either a mining turtle, or a standard turtle
  27. -- Mining Turtle: Slot 1 - Fuel
  28. --                Slot 2 - At least 4 computers
  29. --                Slot 3 - At least 4 modems
  30. --                Slot 4 - At lease 1 disk drive
  31. --                Slot 5 - 1 Disk
  32. -- Standard Turtle: Slot 1 - Fuel
  33. --                  Slot 2 - At least 4 computers
  34. --                  Slot 3 - At least 4 modems
  35. --                  Slot 4 - At lease 4 disk drives
  36. --                  Slot 5 - 1 Disk    
  37. --
  38. -- (mining turtles will be able to reuse the same
  39. --  disk drive, where-as a standard turtle will leave
  40. --  them and deploy a separate disk drive for each
  41. --  GPS host)
  42. --
  43. -- Default Usage: Place the turtle where you want it
  44. --      deployed facing the SOUTH or 0 direction.
  45. --      Fill the turtle with the required materials
  46. --      above. Then use the command
  47. --
  48. --      gps-deploy x y z
  49. --
  50. --      Where x, y, z is the *absolute* positions of the deploment
  51. --      turtle. By default the turtle will deploy the
  52. --      the GPS array at around y = 254. Add a fourth
  53. --      value to specify the height offset.
  54. --      IMPORTANT: It's crucial you use your absolute coordinates,
  55. --       (the ones inside the parentheses next to your realitive coordinates)
  56. --       For Example: If F3 shows X = -6.43534 (-7) or Z = -15.542 (-16)
  57. --       you'd use -7 and -16 respectively. If you use -6 and -15 all coordinates
  58. --       that go past 0 in the opposite direction will be off by 1.)
  59. --
  60. --      Example: gps-deploy 1 1 1 20
  61. --
  62. --      Would assume the turtle's start position is
  63. --      x=1, y=1, z=1 and will deploy the satelite
  64. --      array at y= 21
  65. --
  66. --neonerZ added features
  67. --  Smart Refilling: Fuel should go in slot 1.
  68. --      If not enough fuel is available script
  69. --      will prompt user for more fuel and wait 30.
  70. --      Script will estimate how much fuel is needed
  71. --      and try to take only that much (in coal)
  72. --  Item Detection: Script will check slots 2-5
  73. --      for the correct quantity of items. It does
  74. --      *not* check if items are valid
  75. --  GPS Host Coordinates: The script now requires
  76. --      you to enter in the coordinates of the
  77. --      turtle before launching. This will set
  78. --      the GPS host computers to the correct
  79. --      coordinates.
  80. --  Satelite Array Location: The script allows
  81. --      the user to set an offset for the placement
  82. --      of the four satelites
  83.  
  84. -- How heigh up should the satellite be deployed?
  85. -- This is the default value if you don't pass a
  86. -- value to the script. There is a check down below
  87. -- to make sure the user entered values isn't > 254
  88. height = 255
  89.  
  90. -- need to enable rednet first incase using locate
  91. rednet.open( "right" )
  92.  
  93. local function printUsage()
  94.     print("")
  95.     print( "Usages:" )
  96.     print( "gps-deploy <x> <y> <z> [height]" )
  97.     print( "Example: gps-deploy 1 1 1 20")
  98.     print( "would deploy the satelite to y=21")
  99.     print( "gps-deploy locate [height]")
  100.     print( "if you have working GPS use this")
  101.     print( "to find out the coords over GPS")
  102. end
  103.  
  104. -- confirm default height isn't set above 254
  105. -- Check to see if a minimum of 3 values were
  106. -- passed to the script
  107. local tArgs = { ... }
  108.  
  109. if tArgs[1] == "locate" then
  110.     print ("")
  111.     print ("Locating GPS signal...")
  112.     xcord, ycord, zcord = gps.locate(5, false)
  113.     if xcord==nil then
  114.         print("")
  115.         print ("No GPS signal detected, please rerun manually")
  116.         return
  117.     end
  118.     if tArgs[2] == nil then
  119.         height = tonumber(height)
  120.     else
  121.         height = tonumber(tArgs[2])
  122.     end
  123.     print ("gps-deploy ",xcord," ",ycord," ",zcord," height: ",height)
  124.     xcord = tonumber(xcord)
  125.     ycord = tonumber(ycord)
  126.     zcord = tonumber(zcord)
  127. else
  128.     if #tArgs <= 2 then
  129.         printUsage()
  130.         return
  131.     else
  132.         xcord = tonumber(tArgs[1])
  133.         ycord = tonumber(tArgs[2])
  134.         zcord = tonumber(tArgs[3]) 
  135.         if tArgs[4] == nil then
  136.             height = tonumber(height)
  137.         else
  138.             if tonumber(tArgs[4]) > 254 then
  139.                 height = tonumber(height)
  140.             else
  141.                 height = tonumber(tArgs[4])
  142.             end
  143.         end
  144.     end
  145.  
  146. end
  147.  
  148. if height > ycord and height < 256 then
  149.     height = height-ycord
  150. end
  151.  
  152. if height > 255 then
  153.     height = 255
  154. end
  155.  
  156. -- check if the script is running on a turtle
  157. -- (original code)
  158. if not turtle then
  159.     print("")
  160.     print("Error: not a turtle")
  161.     return
  162. end
  163.  
  164. -- move functions
  165. -- (original code)
  166. local mov = {}
  167.  
  168. mov.forward = function ()
  169.     while not turtle.forward() do
  170.         sleep(1)
  171.     end
  172.     return true
  173. end
  174. mov.back = function ()
  175.     while not turtle.back() do
  176.         sleep(1)
  177.     end
  178.     return true
  179. end
  180. mov.up = function ()
  181.     while not turtle.up() do
  182.         sleep(1)
  183.     end
  184.     return true
  185. end
  186. mov.down = function ()
  187.     while not turtle.down() do
  188.         sleep(1)
  189.     end
  190.     return true
  191. end
  192. mov.place = function ()
  193.     while not turtle.place() do
  194.         sleep(1)
  195.     end
  196. end
  197.  
  198. local base = nil
  199.  
  200. -- Check if we have enough fuel
  201. -- we estimate the fuel usage ((height*2)+70) needed to
  202. -- complete the deoployment and then see if we have enough
  203. -- fuel loaded. If we don't, it checks the first slot for
  204. -- available fuel and tries to fill up on it. If it doesn't
  205. -- have enough fuel in there, it prompts the user for more
  206. -- fuel. It allows 30 seconds for the user to add  fuel
  207. -- (trying to refuel and verify fuel level every second)
  208. -- and if it doesn't get it's fill within 30 seconds
  209. -- it exits with a message to the user
  210. -- neonerZ
  211. if type(turtle.getFuelLevel()) == "string" then
  212.         print("No-fuel mode")
  213. else
  214.     if turtle.getFuelLevel() < (tonumber(height)*2)+70 then
  215.         while turtle.getFuelLevel() < (tonumber(height)*2)+70 do
  216.             turtle.select(1)
  217.             realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
  218.             if realcoal>=64 then
  219.                 coal=64
  220.             else
  221.                 coal=math.ceil(realcoal)
  222.             end
  223.             if turtle.refuel(tonumber(coal)) == false then
  224.                 fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
  225.                 print("")
  226.                 print("You ran out of fuel in slot 1")
  227.                 print("Please insert "..fuel.." fuel or "..realcoal.." coal to continue")
  228.                 print("Waiting 30 seconds for fuel or exiting")
  229.                 i=0
  230.                 while i<=30 do
  231.                     sleep(1)
  232.                     realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
  233.                     if realcoal>=64 then
  234.                         coal=64
  235.                     else
  236.                         coal=math.ceil(realcoal)
  237.                     end
  238.                     turtle.refuel(tonumber(coal))
  239.                     if turtle.getFuelLevel() >= (tonumber(height)*2)+70 then
  240.                         print("")
  241.                         print("Turtle Fueled")
  242.                         i=31
  243.                     end
  244.                     if i==30 then
  245.                         fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
  246.                         print("")
  247.                         print("Not enough fuel provided")
  248.                         print("Please provide "..fuel.." fuel or "..realcoal.." coal and try again")
  249.                         return
  250.                     end
  251.                     i=i+1
  252.                 end
  253.             end
  254.         end
  255.     end
  256. end
  257. --  if fs.exists("custom") then
  258. --      print("custom program detected")
  259. --      print("please Enter base Station number")
  260. --      local failFlag = false
  261. --      repeat
  262. --          if failFlag then
  263. --              print("Error Not number")
  264. --              print("try again")
  265. --          end
  266. --          write(" > ")
  267. --          base = tonumber(read())
  268. --          failFlag = true
  269. --      until type(base) == "number"
  270. --  end
  271. --  print("Please Place 4 computers in slot two")
  272. --  print("4 modems in slot three")
  273. --  print("if mineing turtle then")
  274. --  print("1 disk drive in slot four")
  275. --  print("if not mineing then")
  276. --  print("4 disk drive in slot four")
  277. --  print("blank floopy disk in slot five")
  278. --  print("press Enter key to continue")
  279. --  while true do
  280. --      local event , key = os.pullEvent("key")
  281. --      if key == 28 then break end
  282. --  end
  283. --  print("Launching")
  284. --end
  285.  
  286. -- check if the required quantity of items
  287. -- are in the appropriate spots. I'm sure
  288. -- there's a more elegant way of doing this.
  289. -- I don't believe there's a way to check if
  290. -- the items are correct without using compare
  291. monitor=0
  292. modem=0
  293. diskdrive=0
  294. disk=0
  295. print("")
  296. turtle.select(2)
  297. if turtle.getItemCount(2) < 4 then
  298.     print("Please place at least 4 computers into slot two")
  299.     monitor=1
  300. end
  301. turtle.select(3)
  302. if turtle.getItemCount(2) < 4 then
  303.     print("Please place at least 4 modems into slot three")
  304.     modem=1
  305. end
  306. turtle.select(4)
  307. if turtle.getItemCount(2) < 1 then
  308.     print("Please place 1 disk drive into slot four if a -mining turtle-")
  309.     print("Please place 4 disk drives into slot four if a -standard turtle-")
  310.     diskdrive=1
  311. end
  312. turtle.select(5)
  313. if turtle.getItemCount(2) < 1 then
  314.     print("Please place 1 disk into slot five")
  315.     disk=1
  316. end
  317.  
  318. if monitor == 1 or modem == 1 or diskdrive == 1 or disk == 1 then
  319.     print("Please fix above issues to continue")
  320.     return
  321. end
  322.  
  323. -- calculate the coordinates of the 4 satelite arrays
  324.  
  325. newycord=tonumber(ycord)+tonumber(height)
  326.  
  327. if newycord > 255 then newycord = 255 end
  328.  
  329. toycordns=newycord
  330. toycordwe=newycord-3
  331.  
  332. if toycordns >= 255 or toycordwe >= 255 then
  333.     toycordns=255
  334.     toycordwe=252
  335. end
  336.  
  337. local set = {}
  338. set[1] = {x = tonumber(xcord),z = tonumber(zcord)+3,y = tonumber(toycordns)}
  339. set[2] = {x = tonumber(xcord)-3,z = tonumber(zcord),y = tonumber(toycordwe)}
  340. set[3] = {x = tonumber(xcord),z = tonumber(zcord)-3,y = tonumber(toycordns)}
  341. set[4] = {x = tonumber(xcord)+3,z = tonumber(zcord),y = tonumber(toycordwe)}
  342.  
  343. -- start the climb up to the correct ycord
  344. while not turtle.up() do
  345.     term.clear()
  346.     term.setCursorPos(1,1)
  347.     term.write("Please get off me")
  348. end
  349. if ycord+tonumber(height) >= 255 then
  350.     while turtle.up() do -- sends it up till it hits max hight
  351.     end
  352. else
  353.     for i = 3,tonumber(height) do
  354.     turtle.up()
  355.     end
  356. end
  357.  
  358. -- once at the correct height, deploy GPS array
  359. -- this is a mixture of my own code and the
  360. -- original code
  361.  
  362.  
  363.  
  364. for a = 1,4 do
  365.     --forward two
  366.     for i = 1,2 do
  367.         mov.forward()
  368.     end
  369.     turtle.select(2)
  370.     mov.place()
  371.     mov.back()
  372.     turtle.select(3)
  373.     mov.place()
  374.     mov.down()
  375.     mov.forward()
  376.     turtle.select(4)
  377.     mov.place()
  378.     turtle.select(5)
  379.     turtle.drop()
  380.     -- make a custom disk that starts up the gps host application
  381.     -- with the correct coordinates and copy it over. also makes
  382.     -- makes it a startup script so the computers will
  383.     -- start back up properly after chunk unloading
  384.     fs.delete("disk/startup")
  385.     file = fs.open("disk/startup","w")
  386.     file.write([[
  387. fs.copy("disk/install","startup")
  388. fs.delete("disk/startup")
  389. if fs.exists("disk/custom") then
  390.     fs.copy("disk/custom","custom")
  391.     fs.delete("disk/custom")
  392. end
  393. print("sleep in 10")
  394. sleep(10)
  395. os.reboot()
  396. ]])
  397.     file.close()
  398.     if fs.exists("custom") then
  399.         fs.copy("custom","disk/custom")
  400.     end
  401.         file = fs.open("disk/install","w") -- the following is custom code for NCGPS systems only until rednet.close()
  402.         file.write([[
  403.  
  404. rednet.open("bottom")
  405.  
  406. myID = "id-err"
  407. rolloutWave = 1
  408.  
  409. myID = os.getComputerID()
  410. rednet.broadcast("Computer ID " .. myID .. " registered as an NCGPS System from wave " .. rolloutWave, "ncgpswav01prnt")
  411. rednet.broadcast("wave-01-ncgps", "ncgpswav01conf")
  412. rednet.broadcast(myID, "ncgpswav01id")
  413. rednet.close("bottom")
  414.  
  415. if fs.exists("custom") then
  416.     shell.run("custom","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..","..(base or "nil")..[[)
  417. else
  418.     shell.run("gps","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..[[)
  419. end
  420. ]])
  421.         file.close()
  422.     turtle.turnLeft()
  423.     mov.forward()
  424.     mov.up()
  425.     turtle.turnRight()
  426.     mov.forward()
  427.     turtle.turnRight()
  428.     peripheral.call("front","turnOn")
  429.     mov.down()
  430.     turtle.suck()
  431.     turtle.select(3)
  432.     turtle.dig()
  433.     mov.up()
  434.     -- reboot would be here
  435.     turtle.turnRight()
  436.     --back 3
  437.     for i = 1,3 do
  438.         mov.forward()
  439.     end
  440.     turtle.turnLeft()
  441.     mov.forward()
  442.     if a == 1 or a == 3 then
  443.         for i = 1,3 do
  444.         mov.down()
  445.         end
  446.     elseif a == 2 or a == 4 then
  447.         for i = 1,3 do
  448.         mov.up()
  449.         end
  450.     end
  451. end
  452.  
  453. -- goes back down. this is the original code
  454. -- might be worth editing to come down the same
  455. -- amount of spaces it went up, but this should
  456. -- do the job
  457. while turtle.down() do
  458. end
  459. turtle = tMove
  460. print("")
  461. print("Finished")
Add Comment
Please, Sign In to add comment