Advertisement
cozzimoto

GPS Tower Builder B:16

Nov 7th, 2013
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.40 KB | None | 0 0
  1. -- GPS Tower Creator
  2. -- Author: theonlycozzy
  3. -- BUILD 16
  4. -- Nov 10 2013
  5.  
  6. local dir = {1,-1,-1,1}
  7. local meta = {xPos = nil,yPos = nil,zPos = nil,fPos = nil,targHeight = 0,curSlot = 1,fuelConsumption = 0,pArgs = {...},name=shell.getRunningProgram(),useMats = false,isFound=false,deBug=false}
  8. local stats = {towerX=nil,towerZ=nil,towerHeight=0,blocksUsed=0,fuelConsumed=0,}
  9.  
  10. local logBug = function(TEXT)
  11.   local f = fs.open('debug.txt','a')
  12.   f.writeLine(TEXT)
  13.   f.close()
  14. end
  15. local walk = function(DIR,ITERATION)
  16.   local tLeft = function()
  17.     if turtle.turnLeft() then meta.fPos = (meta.fPos - 1)%4 end
  18.   end
  19.  
  20.   local tRight = function()
  21.     if turtle.turnRight() then meta.fPos = (meta.fPos + 1)%4 end
  22.   end
  23.  
  24.   local tForward = function()
  25.     if turtle.forward() then
  26.       stats.fuelConsumed = stats.fuelConsumed + 1
  27.       if meta.fPos == 0 or meta.fPos == 2 then -- NORTH or SOUTH along Z AXIS
  28.         meta.zPos = meta.zPos + dir[meta.fPos+1]
  29.        
  30.       elseif meta.fPos == 1 or meta.fPos == 3 then -- EAST or WEST along X AXIS
  31.         meta.xPos = meta.xPos + dir[meta.fPos+1]
  32.       end
  33.       return true
  34.     else
  35.       return false
  36.     end
  37.   end
  38.  
  39.   local tBack = function ()
  40.     if turtle.back() then
  41.       stats.fuelConsumed = stats.fuelConsumed + 1
  42.       if meta.fPos == 0 or meta.fPos == 2 then -- NORTH or SOUTH along Z AXIS
  43.         meta.zPos = meta.zPos - dir[meta.fPos+1]
  44.        
  45.       elseif meta.fPos == 1 or meta.fPos == 3 then -- EAST or WEST along X AXIS
  46.         meta.xPos = meta.xPos - dir[meta.fPos+1]
  47.       end
  48.       return true
  49.     else
  50.       return false
  51.     end
  52.   end
  53.  
  54.   if ITERATION == nil then ITERATION = 1 end
  55.  
  56.   if DIR == "left" then tLeft() end
  57.   if DIR == "right" then tRight() end
  58.   for i=1,ITERATION do
  59.     local isMoving = false
  60.     while not(isMoving) do
  61.       if DIR == "forward" or DIR == "left" or DIR == "right" then isMoving = tForward() end
  62.       if DIR == "back" then isMoving = tBack() end
  63.       if DIR == "up" then
  64.         isMoving = turtle.up()
  65.         if isMoving then
  66.           meta.yPos = meta.yPos + 1
  67.         end
  68.       elseif DIR == "down" then
  69.         isMoving = turtle.down()
  70.         if isMoving then
  71.           meta.yPos = meta.yPos - 1
  72.         end
  73.       end      
  74.     end
  75.     stats.fuelConsumed = stats.fuelConsumed + 1
  76.   end
  77.   if DIR == "left" then tRight() end
  78.   if DIR == "right" then tLeft() end
  79. end
  80. local placeBlock = function()
  81.   if meta.useMats then
  82.     turtle.select(meta.curSlot)
  83.     if turtle.getItemCount(meta.curSlot) < 1 then
  84.       if meta.curSlot > 7 then
  85.         meta.useMats = false
  86.       else
  87.         meta.curSlot = meta.curSlot + 1
  88.         turtle.select(meta.curSlot)
  89.       end
  90.     end
  91.   if turtle.place() then stats.blocksUsed = stats.blocksUsed + 1 end
  92.   end
  93. end
  94. local createInstall = function()
  95.   -- Create Instance and get Facing in bounds of dir Array
  96.   if meta.deBug then logBug('createInstall called') end
  97.   local temp = {x=meta.xPos,y=meta.yPos,z=meta.zPos,f=meta.fPos+1}
  98.  
  99.   if temp.f == 1 or temp.f == 3 then -- North or South
  100.     temp.z = temp.z + dir[temp.f]
  101.    
  102.   elseif temp.f == 2 or temp.f == 4 then -- East or West
  103.     temp.x = temp.x + dir[temp.f]
  104.   end
  105.   local f = fs.open("disk/startup","w")
  106.     f.writeLine("local f = fs.open('startup','w')")
  107.     f.writeLine("f.writeLine(\"shell.run(\'gps host "..temp.x..' '..temp.y..' '..temp.z.."\')\")")
  108.     f.writeLine("f.close()")
  109.   f.close()
  110.   if meta.deBug then logBug('createInstall returned') end
  111. end
  112. local activateGPS = function()
  113.   if meta.deBug then logBug('activateGPS called') end
  114.   turtle.select(16) turtle.place() -- Computer
  115.   local comp = peripheral.wrap("front")
  116.   comp.turnOn()
  117.   walk("up")
  118.   turtle.select(15) turtle.place() -- modem
  119.   walk("down",2)
  120.   turtle.select(14) turtle.place() -- disk drive
  121.   turtle.select(13) turtle.drop()  -- floppy
  122.   walk("up")
  123.   createInstall()
  124.   sleep(2)
  125.   comp.reboot()
  126.   walk("down")
  127.   turtle.suck()
  128.   turtle.select(14) turtle.dig()
  129.   walk("up")
  130.   sleep(2)
  131.   comp.reboot()
  132.   if meta.deBug then logBug('activateGPS returned') end
  133. end
  134. local nextCluster = function()
  135.   local safeRange = 284
  136.   local positions = {NE = {X=stats.towerZ - safeRange, Z=stats.towerX + safeRange},
  137.                      SE = {X=stats.towerZ + safeRange, Z=stats.towerX + safeRange},
  138.                      SW = {X=stats.towerZ + safeRange, Z=stats.towerX - safeRange},
  139.                      NW = {X=stats.towerZ - safeRange, Z=stats.towerX - safeRange},}
  140.  
  141.   local f = fs.open('nextCluster.pos','a')
  142.   f.writeLine(textutils.serialize(positions))
  143.   f.close()
  144. end
  145.  
  146. if not turtle then error("This program is for a turtle") end -- INIT
  147. if turtle.digDown() then
  148.   turtle.placeDown()
  149. else
  150.   if meta.deBug then logBug('ERROR: No Pickaxe equipped') end
  151.   error("This turtle needs a pickaxe to operate")
  152. end
  153.  
  154. if meta.pArgs then -- Applied Arguments
  155.   if meta.pArgs[1] == "-d" or meta.pArgs[2] == "-d" then meta.deBug = true end
  156.  
  157.   if meta.pArgs[1] == "-u" then -- update argument
  158.     if meta.deBug then logBug('-u argumnt passed') end
  159.     if http then
  160.       if meta.deBug then logBug('HTTP passed') end
  161.       local get = http.get("http://pastebin.com/raw.php?i=xhyTFyZp")
  162.  
  163.       if get then -- on success
  164.         if meta.deBug then logBug('HTTP get success') end
  165.         local file = get.readAll()
  166.         get.close()
  167.         local f = fs.open(meta.name..".update","w")
  168.          f.write(file)
  169.         f.close()
  170.       else
  171.         error("updated failed")
  172.       end
  173.       -- create the startup file
  174.       if meta.deBug then logBug('creating install file') end
  175.       local file = fs.open('startup','w')
  176.       file.writeLine("local file = '"..meta.name.."'")
  177.       file.write([[if fs.exists(file) and fs.exists(file..'.update') then
  178.                     local old,new,oldV,newV = {},{},0,0
  179.                     local temp = fs.open(file,'r')
  180.                     repeat old[#old+1] = temp.readLine() until temp.readLine() == nil
  181.                     temp.close()
  182.                     local temp = fs.open(file..'.update','r')
  183.                     repeat new[#new+1] = temp.readLine() until temp.readLine() == nil
  184.                     temp.close()
  185.                     oldV = tonumber(string.sub(old[2],#old[2]-1)) print(oldV)
  186.                     newV = tonumber(string.sub(new[2],#new[2]-1)) print(newV)
  187.                     if newV > oldV then
  188.                       fs.delete(file)
  189.                       fs.copy(file..'.update',file)
  190.                       print('updated')
  191.                     end
  192.                   end
  193.                   fs.delete(file..'.update') fs.delete('/startup')]])
  194.       file.close()
  195.       if meta.deBug then logBug('rebooting') end
  196.       print("rebooting to apply update")
  197.       sleep(1.5)
  198.       os.reboot()
  199.     else error("HTTP must be enabled to update") end
  200.   end
  201. end
  202.  
  203. -- Initiate Step 1
  204. term.clear() term.setCursorPos(1,1) term.write(meta.name.." initializing..")
  205. if peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  206.   if meta.deBug then logBug('turtle has wireless modem') end
  207.   meta.xPos, meta.yPos, meta.zPos = gps.locate(2)
  208.  
  209.   if meta.xPos then
  210.     if meta.deBug then logBug('GPS Found') end
  211.     meta.isFound = true
  212.     term.clear() term.setCursorPos(1,1)
  213.     print[[Setup: step 1/3  XXXX ---- ----
  214.  
  215.               GPS Found!
  216.     +----------------------------+
  217.     | Status:                    |
  218.     +----------------------------+]]
  219.    
  220.     local x,z = meta.xPos, meta.zPos
  221.     if turtle.detect() then turtle.dig() end
  222.     turtle.forward()
  223.     if meta.deBug then logBug('detecting Orientation via GPS') end
  224.     term.setCursorPos(15,5) term.write("Checking Uplink    ") sleep(0)
  225.     meta.xPos, meta.yPos, meta.zPos = gps.locate(2)
  226.     term.setCursorPos(15,5) term.write("calc Orientation   ") sleep(0)
  227.    
  228.     if meta.xPos > x then -- EAST
  229.       meta.fPos = 3
  230.     elseif meta.xPos < x then -- WEST
  231.       meta.fPos = 1
  232.     elseif meta.zPos > z then -- SOUTH
  233.       meta.fPos = 0
  234.     elseif meta.zPos < z then -- NORTH
  235.       meta.fPos = 2
  236.     else
  237.       if meta.deBug then logBug('ERROR: couldnt determine orientation') end
  238.       error("couldn't determine orientation") -- Fix for this Later
  239.     end
  240.    
  241.     term.setCursorPos(15,5) term.write("Orientation Found  ") sleep(0)
  242.     turtle.back()
  243.     if meta.deBug then logBug('Orientation Success') end
  244.    
  245.     term.setCursorPos(15,5) term.write("Removing temp data ") sleep(0)
  246.     meta.xPos,meta.zPos = x,z
  247.  
  248.     term.clear() term.setCursorPos(1,1)
  249.     print[[S     GPS Location Confirmation
  250. t
  251. e   +----------------------------+
  252. p   | X:       Z:       [Y:    ] |
  253.     |  Orientation:              |
  254. 1   |                            |
  255. /   |                            |
  256. 3   +----------------------------+]]
  257.     local X = tostring(meta.xPos) term.setCursorPos(9,4) term.write(X)
  258.     local Z = tostring(meta.zPos) term.setCursorPos(18,4) term.write(Z)
  259.     local Y = tostring(meta.yPos) term.setCursorPos(28,4) term.write(Y)
  260.    
  261.     local facing
  262.     if meta.fPos == 2 then
  263.       facing = "North [2]"
  264.     elseif meta.fPos == 3 then
  265.       facing = "East [3]"
  266.     elseif meta.fPos == 0 then
  267.       facing = "South [0]"
  268.     elseif meta.fPos == 1 then
  269.       facing = "West [1]"
  270.     end
  271.    
  272.     term.setCursorPos(21,5) term.write(facing)
  273.     term.setCursorPos(12,7) term.write("Confirm y/n? ")
  274.     if meta.deBug then logBug('Waiting for GPS Verification') end
  275.     local ans = string.lower(read())
  276.    
  277.     if ans == "y" or ans == "yes" then
  278.       if meta.deBug then logBug('GPS Verified') end
  279.       term.setCursorPos(7,9) print("proceeding") sleep(1.5)
  280.     else meta.isFound = false end
  281.   else meta.isFound = false end -- if meta.xPos then
  282. end
  283.  
  284. while not meta.isFound do
  285.   if meta.deBug then logBug('GPS manual Entry Activated') end
  286.   term.clear() term.setCursorPos(1,1)
  287.   print[[S
  288. t     GPS Not Determined
  289. e                 Enter Location
  290. p   +----------------------------+
  291.     |  X:      Z:      [Y:   ]   |
  292. 1   |  Orientation:              |
  293. /   |                            |
  294. 3   +----------------------------+]]
  295.  
  296.   term.setCursorPos(10,5) local X = tonumber(read())
  297.   term.setCursorPos(18,5) local Z = tonumber(read())
  298.   term.setCursorPos(27,5) local Y = tonumber(read())
  299.   term.setCursorPos(21,6) local F = read()
  300.  
  301.   local facing = string.lower(F)
  302.   if facing == "north" or facing == "n" or facing == "2" then
  303.     meta.fPos = 2
  304.   elseif facing == "east" or facing == "e" or facing == "3" then
  305.     meta.fPos = 3
  306.   elseif facing == "south" or facing == "s" or facing == "0" then
  307.     meta.fPos = 0
  308.   elseif facing == "west" or facing == "w" or facing == "1" then
  309.     meta.fPos = 1
  310.   else
  311.     if meta.deBug then logBug('ERROR: Orientation in incorrect format') end
  312.     error("incorrect Orientation")
  313.   end
  314.  
  315.   term.setCursorPos(8,9) term.write("Confirm[y/n] ")
  316.   if meta.deBug then logBug('waiting for GPS manual entry verification') end
  317.   local ans = string.lower(read())
  318.  
  319.   if ans == "y" or ans == "yes" then
  320.     if type(X) == "number" and type(Y) == "number" and type(Z) == "number" then
  321.       meta.xPos,meta.yPos,meta.zPos = X,Y,Z
  322.     else term.setCursorPos(8,11) print("Coordinates must be numbers") sleep(1.5) end
  323.     if meta.deBug then logBug('GPS manual entry verified') end
  324.     meta.isFound = true
  325.   end
  326. end
  327.  
  328. -- Initiate Step 2
  329. if meta.deBug then logBug('Initial Fuel check systems') end
  330. term.clear() term.setCursorPos(1,1)
  331. meta.targHeight = math.ceil(253 - meta.yPos)
  332. meta.fuelConsumption = math.ceil((meta.targHeight * 2) + 53)
  333.  
  334. print[[S
  335. t
  336. e      Estimated Fuel Consumption
  337. p     +------------------+-------+
  338.       | Fuel Consumption |       |
  339. 2     | Current Fuel     |       |
  340. /     | Amount Needed    |       |
  341. 3     +------------------+-------+
  342. ]]
  343. term.setCursorPos(27,5) term.write(meta.fuelConsumption)
  344. term.setCursorPos(27,6) term.write(turtle.getFuelLevel())
  345.  
  346. if turtle.getFuelLevel == "unlimited" then
  347.   if meta.deBug then logBug('Fuel is set to unlimited') end
  348.   term.setCursorPos(27,6) term.write("-infi-")
  349.  
  350. elseif turtle.getFuelLevel() < meta.fuelConsumption then
  351.   local amount = math.ceil(meta.fuelConsumption - turtle.getFuelLevel())
  352.   local coalA = math.ceil(amount/80)
  353.   local woodA = math.ceil(amount/15)
  354.   local fueling = true
  355.  
  356.   term.setCursorPos(27,7) term.write(amount)
  357.   term.setCursorPos(2,9) term.write("Coal/Charcoal = "..coalA.. " | Wood/Planks = "..woodA)
  358.   term.setCursorPos(3,11) term.write("Commence Fuelling Process ")
  359.   print("place Fuel in first slot or hit 'x' to terminate")
  360.   turtle.select(1) os.startTimer(1)
  361.  
  362.   while fueling do
  363.     local ev = { os.pullEvent() }
  364.     if ev[1] == "key" then
  365.       if ev[2] == 45 then -- 'X'
  366.         print("terminating")
  367.         if meta.deBug then logBug('Fuel manual override') end
  368.         sleep(1.5)
  369.         os.reboot()
  370.       end
  371.     elseif ev[1] == "timer" then
  372.       os.startTimer(0.5)
  373.     end
  374.    
  375.     if turtle.getFuelLevel() > meta.fuelConsumption then
  376.       print("Enough Fuel")
  377.       if meta.deBug then logBug('Fuel check passed!') end
  378.       fueling = false
  379.     else
  380.       if turtle.getItemCount(1) > 0 then
  381.         if meta.deBug then logBug('refueled') end
  382.         turtle.refuel(1)
  383.       end
  384.     end
  385.   end
  386. else term.setCursorPos(8,10) term.write("PASSED!") sleep(1.5) end
  387.  
  388. -- Initiate Step 3
  389. term.clear() term.setCursorPos(1,1)
  390. print[[S     Building Supplies
  391. t   +-----------+------------------+
  392. e   | 1  2  3  4| 13 Floppy        |
  393. p   | 5  6  7  8| 14 Disk Drive    |
  394.     |           | 15 [4] Modems    |
  395. 3   |13 14 15 16| 16 [4] Computers |
  396. /   +-----------+                  |
  397. 3   | 1-8 your building blocks     |
  398.     +------------------------------+]]
  399.  
  400. term.setCursorPos(6,11) term.write("Build Column  [y/n] ")
  401. local ans = string.lower(read())
  402.  
  403. if ans == "y" or ans == "yes" then
  404.   meta.useMats = true
  405.   if meta.deBug then logBug('Center Column Enabled') end
  406. else
  407.   meta.useMats = false
  408.   if meta.deBug then logBug('Center Column Disabled') end
  409. end
  410.  
  411. term.setCursorPos(6,12) term.write("Start Program [y/n] ")
  412. local ans = string.lower(read())
  413.  
  414. if ans == "y" or ans == "yes" then
  415.   if meta.deBug then logBug('--------PROGRAM INITIATED--------') end
  416.   stats.towerX, stats.towerZ, stats.towerHeight = meta.xPos, meta.zPos, meta.targHeight
  417.   if meta.pArgs[1] == "test" then meta.targHeight = 10 end
  418.  
  419.   if meta.deBug then logBug('moving up to build cluster at: '..meta.targHeight) end
  420.   for i=1,meta.targHeight do
  421.     if meta.deBug then logBug('I:'..i) end
  422.     placeBlock() walk("up")
  423.   end
  424.  
  425.   if meta.fPos == 3 or meta.fPos == 1 then
  426.     if meta.deBug then logBug('Cluster Orientation: East/West Axis') end
  427.     walk("left",4)
  428.     activateGPS()
  429.     for i=1,7 do walk("right") placeBlock() end
  430.     walk("right")
  431.     activateGPS()
  432.     walk("left",4)
  433.     for i=1,3 do walk("down") end
  434.     if turtle.detect() then turtle.dig() end
  435.     walk("forward",3)
  436.     activateGPS()
  437.     for i=1,5 do walk("back") placeBlock() end
  438.     walk("back")
  439.     activateGPS()
  440.     walk("down")
  441.     walk("forward",3)
  442.   else
  443.     if meta.deBug then logBug('Cluster Orientation: North/South Axis') end
  444.     walk("forward",4)
  445.     activateGPS()
  446.     for i=1,7 do walk("back") placeBlock() end
  447.     walk("back")
  448.     activateGPS()
  449.     walk("down")
  450.     walk("forward",4) placeBlock()
  451.     walk("down") placeBlock()
  452.     walk("down")
  453.     walk("left",3)
  454.     activateGPS()
  455.     for i=1,5 do walk("right") placeBlock() end
  456.     walk("right")
  457.     activateGPS()
  458.     walk("left",3)
  459.   end
  460.   if meta.deBug then logBug('moving down to touchdown') end
  461.   while not turtle.detectDown() do walk("down") end
  462.   if meta.deBug then logBug('ground touchdown') end
  463.  
  464.   if fs.exists("gps.pos") then
  465.     local temp = {}
  466.     local f = fs.open("gps.pos","r")
  467.       temp = textutils.unserialize(f.readAll())
  468.     f.close()
  469.    
  470.     local f = fs.open("gps.pos","w")
  471.       temp[#temp+1] = stats
  472.       f.write(textutils.serialize(temp))
  473.     f.close()
  474.  
  475.   else -- if file doesnt exist
  476.     local temp = {}
  477.     local f = fs.open("gps.pos","w")
  478.       temp[#temp+1] = stats
  479.       f.write(textutils.serialize(temp))
  480.     f.close()
  481.   end
  482.  
  483.   nextCluster()
  484.   term.clear() term.setCursorPos(1,2) print("GPS Tower Finished")
  485.   if meta.deBug then logBug('--------PROGRAM ENDED SUCCESSFULLY--------') end
  486.   for k,v in pairs(stats) do print(k..": "..v) end
  487. else
  488.   if meta.deBug then logBug('program terminated') end
  489.   term.clear() term.setCursorPos(2,1) term.write(meta.name.." program terminated")
  490.   term.setCursorPos(1,3)
  491. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement