happydude11209

GPS Deploy 1.01- Fuel Fix

Jun 3rd, 2013
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.28 KB | None | 0 0
  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 type(turtle.getFuelLevel()) == "string" then
  197.     print("No-fuel mode")
  198. else
  199.     if turtle.getFuelLevel() < (tonumber(height)*2)+70 then
  200.         while turtle.getFuelLevel() < (tonumber(height)*2)+70 do
  201.             turtle.select(1)
  202.             realcoal=(((tonumber(height)*2)+70)-turtle.getFuelLevel())/80
  203.             if realcoal>=64 then
  204.                 coal=64
  205.             else
  206.                 coal=math.ceil(realcoal)
  207.             end
  208.             if turtle.refuel(tonumber(coal)) == false then
  209.                 fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
  210.                 print("")
  211.                 print("You ran out of fuel in slot 1")
  212.                 print("Please insert "..fuel.." fuel or "..realcoal.." coal to continue")
  213.                 print("Waiting 30 seconds for fuel or exiting")
  214.                 i=0
  215.                 while i<=30 do
  216.                     sleep(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.                     turtle.refuel(tonumber(coal))
  224.                     if turtle.getFuelLevel() >= (tonumber(height)*2)+70 then
  225.                         print("")
  226.                         print("Turtle Fueled")
  227.                         i=31
  228.                     end
  229.                     if i==30 then
  230.                         fuel=((tonumber(height)*2)+70)-turtle.getFuelLevel()
  231.                         print("")
  232.                         print("Not enough fuel provided")
  233.                         print("Please provide "..fuel.." fuel or "..realcoal.." coal and try again")
  234.                         return
  235.                     end
  236.                     i=i+1
  237.                 end
  238.             end
  239.         end
  240.     end
  241. end
  242. --  if fs.exists("custom") then
  243. --      print("custom program detected")
  244. --      print("please Enter base Station number")
  245. --      local failFlag = false
  246. --      repeat
  247. --          if failFlag then
  248. --              print("Error Not number")
  249. --              print("try again")
  250. --          end
  251. --          write(" > ")
  252. --          base = tonumber(read())
  253. --          failFlag = true
  254. --      until type(base) == "number"
  255. --  end
  256. --  print("Please Place 4 computers in slot two")
  257. --  print("4 modems in slot three")
  258. --  print("if mineing turtle then")
  259. --  print("1 disk drive in slot four")
  260. --  print("if not mineing then")
  261. --  print("4 disk drive in slot four")
  262. --  print("blank floopy disk in slot five")
  263. --  print("press Enter key to continue")
  264. --  while true do
  265. --      local event , key = os.pullEvent("key")
  266. --      if key == 28 then break end
  267. --  end
  268. --  print("Launching")
  269. --end
  270.  
  271. -- check if the required quantity of items
  272. -- are in the appropriate spots. I'm sure
  273. -- there's a more elegant way of doing this.
  274. -- I don't believe there's a way to check if
  275. -- the items are correct without using compare
  276. monitor=0
  277. modem=0
  278. diskdrive=0
  279. disk=0
  280. print("")
  281. turtle.select(2)
  282. if turtle.getItemCount(2) < 4 then
  283.     print("Please place at least 4 monitors into slot two")
  284.     monitor=1
  285. end
  286. turtle.select(3)
  287. if turtle.getItemCount(2) < 4 then
  288.     print("Please place at least 4 modems into slot three")
  289.     modem=1
  290. end
  291. turtle.select(4)
  292. if turtle.getItemCount(2) < 1 then
  293.     print("Please place 1 disk drive into slot four if a -mining turtle-")
  294.     print("Please place 4 disk drives into slot four if a -standard turtle-")
  295.     diskdrive=1
  296. end
  297. turtle.select(5)
  298. if turtle.getItemCount(2) < 1 then
  299.     print("Please place 1 disk into slot five")
  300.     disk=1
  301. end
  302.  
  303. if monitor == 1 or modem == 1 or diskdrive == 1 or disk == 1 then
  304.     print("Please fix above issues to continue")
  305.     return
  306. end
  307.  
  308. -- calculate the coordinates of the 4 satelite arrays
  309.  
  310. newycord=tonumber(ycord)+tonumber(height)
  311.  
  312. if newycord > 254 then newycord = 254 end
  313.  
  314. toycordns=newycord
  315. toycordwe=newycord-3
  316.  
  317. if toycordns >= 255 or toycordwe >= 255 then
  318.     toycordns=254
  319.     toycordwe=251
  320. end
  321.  
  322. local set = {}
  323. set[1] = {x = tonumber(xcord),z = tonumber(zcord)+9,y = tonumber(toycordns)}
  324. set[2] = {x = tonumber(xcord)-9,z = tonumber(zcord),y = tonumber(toycordwe)}
  325. set[3] = {x = tonumber(xcord),z = tonumber(zcord)-9,y = tonumber(toycordns)}
  326. set[4] = {x = tonumber(xcord)+9,z = tonumber(zcord),y = tonumber(toycordwe)}
  327.  
  328. -- start the climb up to the correct ycord
  329. if ycord+tonumber(height) >= 254 then
  330.     while turtle.up() do -- sends it up till it hits max hight
  331.     end
  332. else
  333.     for i = 1,tonumber(height) do
  334.     turtle.up()
  335.     end
  336. end
  337.  
  338. -- once at the correct height, deploy GPS array
  339. -- this is a mixture of my own code and the
  340. -- original code
  341. for a = 1,4 do
  342.     --forward two
  343.     for i = 1,8 do
  344.         mov.forward()
  345.     end
  346.     turtle.select(2)
  347.     mov.place()
  348.     mov.back()
  349.     turtle.select(3)
  350.     mov.place()
  351.     mov.down()
  352.     mov.forward()
  353.     turtle.select(4)
  354.     mov.place()
  355.     turtle.select(5)
  356.     turtle.drop()
  357.     -- make a custom disk that starts up the gps host application
  358.     -- with the correct coordinates and copy it over. also makes
  359.     -- makes it a startup script so the computers will
  360.     -- start back up properly after chunk unloading
  361.     fs.delete("disk/startup")
  362.     file = fs.open("disk/startup","w")
  363.     file.write([[
  364. fs.copy("disk/install","startup")
  365. fs.delete("disk/startup")
  366. if fs.exists("disk/custom") then
  367.     fs.copy("disk/custom","custom")
  368.     fs.delete("disk/custom")
  369. end
  370. print("sleep in 10")
  371. sleep(10)
  372. os.reboot()
  373. ]])
  374.     file.close()
  375.     if fs.exists("custom") then
  376.         fs.copy("custom","disk/custom")
  377.     end
  378.         file = fs.open("disk/install","w")
  379.         file.write([[
  380. if fs.exists("custom") then
  381.     shell.run("custom","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..","..(base or "nil")..[[)
  382. else
  383.     shell.run("gps","host",]]..set[a]["x"]..","..set[a]["y"]..","..set[a]["z"]..[[)
  384. end
  385. ]])
  386.         file.close()
  387.     turtle.turnLeft()
  388.     mov.forward()
  389.     mov.up()
  390.     turtle.turnRight()
  391.     mov.forward()
  392.     turtle.turnRight()
  393.     peripheral.call("front","turnOn")
  394.     mov.down()
  395.     turtle.suck()
  396.     turtle.select(3)
  397.     turtle.dig()
  398.     mov.up()
  399.     -- reboot would be here
  400.     turtle.turnRight()
  401.     --back 3
  402.     for i = 1,9 do
  403.         mov.forward()
  404.     end
  405.     turtle.turnLeft()
  406.     mov.forward()
  407.     if a == 1 or a == 3 then
  408.         for i = 1,3 do
  409.         mov.down()
  410.         end
  411.     elseif a == 2 or a == 4 then
  412.         for i = 1,3 do
  413.         mov.up()
  414.         end
  415.     end
  416. end
  417.  
  418. -- goes back down. this is the original code
  419. -- might be worth editing to come down the same
  420. -- amount of spaces it went up, but this should
  421. -- do the job
  422. while turtle.down() do
  423. end
  424. turtle = tMove
  425. print("")
  426. print("Finished")
Add Comment
Please, Sign In to add comment