KaosKlaus

gps-deploy (modified)

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