Guest User

Untitled

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