Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GPS Tower Creator
- -- Author: theonlycozzy
- -- BUILD 16
- -- Nov 10 2013
- local dir = {1,-1,-1,1}
- 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}
- local stats = {towerX=nil,towerZ=nil,towerHeight=0,blocksUsed=0,fuelConsumed=0,}
- local logBug = function(TEXT)
- local f = fs.open('debug.txt','a')
- f.writeLine(TEXT)
- f.close()
- end
- local walk = function(DIR,ITERATION)
- local tLeft = function()
- if turtle.turnLeft() then meta.fPos = (meta.fPos - 1)%4 end
- end
- local tRight = function()
- if turtle.turnRight() then meta.fPos = (meta.fPos + 1)%4 end
- end
- local tForward = function()
- if turtle.forward() then
- stats.fuelConsumed = stats.fuelConsumed + 1
- if meta.fPos == 0 or meta.fPos == 2 then -- NORTH or SOUTH along Z AXIS
- meta.zPos = meta.zPos + dir[meta.fPos+1]
- elseif meta.fPos == 1 or meta.fPos == 3 then -- EAST or WEST along X AXIS
- meta.xPos = meta.xPos + dir[meta.fPos+1]
- end
- return true
- else
- return false
- end
- end
- local tBack = function ()
- if turtle.back() then
- stats.fuelConsumed = stats.fuelConsumed + 1
- if meta.fPos == 0 or meta.fPos == 2 then -- NORTH or SOUTH along Z AXIS
- meta.zPos = meta.zPos - dir[meta.fPos+1]
- elseif meta.fPos == 1 or meta.fPos == 3 then -- EAST or WEST along X AXIS
- meta.xPos = meta.xPos - dir[meta.fPos+1]
- end
- return true
- else
- return false
- end
- end
- if ITERATION == nil then ITERATION = 1 end
- if DIR == "left" then tLeft() end
- if DIR == "right" then tRight() end
- for i=1,ITERATION do
- local isMoving = false
- while not(isMoving) do
- if DIR == "forward" or DIR == "left" or DIR == "right" then isMoving = tForward() end
- if DIR == "back" then isMoving = tBack() end
- if DIR == "up" then
- isMoving = turtle.up()
- if isMoving then
- meta.yPos = meta.yPos + 1
- end
- elseif DIR == "down" then
- isMoving = turtle.down()
- if isMoving then
- meta.yPos = meta.yPos - 1
- end
- end
- end
- stats.fuelConsumed = stats.fuelConsumed + 1
- end
- if DIR == "left" then tRight() end
- if DIR == "right" then tLeft() end
- end
- local placeBlock = function()
- if meta.useMats then
- turtle.select(meta.curSlot)
- if turtle.getItemCount(meta.curSlot) < 1 then
- if meta.curSlot > 7 then
- meta.useMats = false
- else
- meta.curSlot = meta.curSlot + 1
- turtle.select(meta.curSlot)
- end
- end
- if turtle.place() then stats.blocksUsed = stats.blocksUsed + 1 end
- end
- end
- local createInstall = function()
- -- Create Instance and get Facing in bounds of dir Array
- if meta.deBug then logBug('createInstall called') end
- local temp = {x=meta.xPos,y=meta.yPos,z=meta.zPos,f=meta.fPos+1}
- if temp.f == 1 or temp.f == 3 then -- North or South
- temp.z = temp.z + dir[temp.f]
- elseif temp.f == 2 or temp.f == 4 then -- East or West
- temp.x = temp.x + dir[temp.f]
- end
- local f = fs.open("disk/startup","w")
- f.writeLine("local f = fs.open('startup','w')")
- f.writeLine("f.writeLine(\"shell.run(\'gps host "..temp.x..' '..temp.y..' '..temp.z.."\')\")")
- f.writeLine("f.close()")
- f.close()
- if meta.deBug then logBug('createInstall returned') end
- end
- local activateGPS = function()
- if meta.deBug then logBug('activateGPS called') end
- turtle.select(16) turtle.place() -- Computer
- local comp = peripheral.wrap("front")
- comp.turnOn()
- walk("up")
- turtle.select(15) turtle.place() -- modem
- walk("down",2)
- turtle.select(14) turtle.place() -- disk drive
- turtle.select(13) turtle.drop() -- floppy
- walk("up")
- createInstall()
- sleep(2)
- comp.reboot()
- walk("down")
- turtle.suck()
- turtle.select(14) turtle.dig()
- walk("up")
- sleep(2)
- comp.reboot()
- if meta.deBug then logBug('activateGPS returned') end
- end
- local nextCluster = function()
- local safeRange = 284
- local positions = {NE = {X=stats.towerZ - safeRange, Z=stats.towerX + safeRange},
- SE = {X=stats.towerZ + safeRange, Z=stats.towerX + safeRange},
- SW = {X=stats.towerZ + safeRange, Z=stats.towerX - safeRange},
- NW = {X=stats.towerZ - safeRange, Z=stats.towerX - safeRange},}
- local f = fs.open('nextCluster.pos','a')
- f.writeLine(textutils.serialize(positions))
- f.close()
- end
- if not turtle then error("This program is for a turtle") end -- INIT
- if turtle.digDown() then
- turtle.placeDown()
- else
- if meta.deBug then logBug('ERROR: No Pickaxe equipped') end
- error("This turtle needs a pickaxe to operate")
- end
- if meta.pArgs then -- Applied Arguments
- if meta.pArgs[1] == "-d" or meta.pArgs[2] == "-d" then meta.deBug = true end
- if meta.pArgs[1] == "-u" then -- update argument
- if meta.deBug then logBug('-u argumnt passed') end
- if http then
- if meta.deBug then logBug('HTTP passed') end
- local get = http.get("http://pastebin.com/raw.php?i=xhyTFyZp")
- if get then -- on success
- if meta.deBug then logBug('HTTP get success') end
- local file = get.readAll()
- get.close()
- local f = fs.open(meta.name..".update","w")
- f.write(file)
- f.close()
- else
- error("updated failed")
- end
- -- create the startup file
- if meta.deBug then logBug('creating install file') end
- local file = fs.open('startup','w')
- file.writeLine("local file = '"..meta.name.."'")
- file.write([[if fs.exists(file) and fs.exists(file..'.update') then
- local old,new,oldV,newV = {},{},0,0
- local temp = fs.open(file,'r')
- repeat old[#old+1] = temp.readLine() until temp.readLine() == nil
- temp.close()
- local temp = fs.open(file..'.update','r')
- repeat new[#new+1] = temp.readLine() until temp.readLine() == nil
- temp.close()
- oldV = tonumber(string.sub(old[2],#old[2]-1)) print(oldV)
- newV = tonumber(string.sub(new[2],#new[2]-1)) print(newV)
- if newV > oldV then
- fs.delete(file)
- fs.copy(file..'.update',file)
- print('updated')
- end
- end
- fs.delete(file..'.update') fs.delete('/startup')]])
- file.close()
- if meta.deBug then logBug('rebooting') end
- print("rebooting to apply update")
- sleep(1.5)
- os.reboot()
- else error("HTTP must be enabled to update") end
- end
- end
- -- Initiate Step 1
- term.clear() term.setCursorPos(1,1) term.write(meta.name.." initializing..")
- if peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
- if meta.deBug then logBug('turtle has wireless modem') end
- meta.xPos, meta.yPos, meta.zPos = gps.locate(2)
- if meta.xPos then
- if meta.deBug then logBug('GPS Found') end
- meta.isFound = true
- term.clear() term.setCursorPos(1,1)
- print[[Setup: step 1/3 XXXX ---- ----
- GPS Found!
- +----------------------------+
- | Status: |
- +----------------------------+]]
- local x,z = meta.xPos, meta.zPos
- if turtle.detect() then turtle.dig() end
- turtle.forward()
- if meta.deBug then logBug('detecting Orientation via GPS') end
- term.setCursorPos(15,5) term.write("Checking Uplink ") sleep(0)
- meta.xPos, meta.yPos, meta.zPos = gps.locate(2)
- term.setCursorPos(15,5) term.write("calc Orientation ") sleep(0)
- if meta.xPos > x then -- EAST
- meta.fPos = 3
- elseif meta.xPos < x then -- WEST
- meta.fPos = 1
- elseif meta.zPos > z then -- SOUTH
- meta.fPos = 0
- elseif meta.zPos < z then -- NORTH
- meta.fPos = 2
- else
- if meta.deBug then logBug('ERROR: couldnt determine orientation') end
- error("couldn't determine orientation") -- Fix for this Later
- end
- term.setCursorPos(15,5) term.write("Orientation Found ") sleep(0)
- turtle.back()
- if meta.deBug then logBug('Orientation Success') end
- term.setCursorPos(15,5) term.write("Removing temp data ") sleep(0)
- meta.xPos,meta.zPos = x,z
- term.clear() term.setCursorPos(1,1)
- print[[S GPS Location Confirmation
- t
- e +----------------------------+
- p | X: Z: [Y: ] |
- | Orientation: |
- 1 | |
- / | |
- 3 +----------------------------+]]
- local X = tostring(meta.xPos) term.setCursorPos(9,4) term.write(X)
- local Z = tostring(meta.zPos) term.setCursorPos(18,4) term.write(Z)
- local Y = tostring(meta.yPos) term.setCursorPos(28,4) term.write(Y)
- local facing
- if meta.fPos == 2 then
- facing = "North [2]"
- elseif meta.fPos == 3 then
- facing = "East [3]"
- elseif meta.fPos == 0 then
- facing = "South [0]"
- elseif meta.fPos == 1 then
- facing = "West [1]"
- end
- term.setCursorPos(21,5) term.write(facing)
- term.setCursorPos(12,7) term.write("Confirm y/n? ")
- if meta.deBug then logBug('Waiting for GPS Verification') end
- local ans = string.lower(read())
- if ans == "y" or ans == "yes" then
- if meta.deBug then logBug('GPS Verified') end
- term.setCursorPos(7,9) print("proceeding") sleep(1.5)
- else meta.isFound = false end
- else meta.isFound = false end -- if meta.xPos then
- end
- while not meta.isFound do
- if meta.deBug then logBug('GPS manual Entry Activated') end
- term.clear() term.setCursorPos(1,1)
- print[[S
- t GPS Not Determined
- e Enter Location
- p +----------------------------+
- | X: Z: [Y: ] |
- 1 | Orientation: |
- / | |
- 3 +----------------------------+]]
- term.setCursorPos(10,5) local X = tonumber(read())
- term.setCursorPos(18,5) local Z = tonumber(read())
- term.setCursorPos(27,5) local Y = tonumber(read())
- term.setCursorPos(21,6) local F = read()
- local facing = string.lower(F)
- if facing == "north" or facing == "n" or facing == "2" then
- meta.fPos = 2
- elseif facing == "east" or facing == "e" or facing == "3" then
- meta.fPos = 3
- elseif facing == "south" or facing == "s" or facing == "0" then
- meta.fPos = 0
- elseif facing == "west" or facing == "w" or facing == "1" then
- meta.fPos = 1
- else
- if meta.deBug then logBug('ERROR: Orientation in incorrect format') end
- error("incorrect Orientation")
- end
- term.setCursorPos(8,9) term.write("Confirm[y/n] ")
- if meta.deBug then logBug('waiting for GPS manual entry verification') end
- local ans = string.lower(read())
- if ans == "y" or ans == "yes" then
- if type(X) == "number" and type(Y) == "number" and type(Z) == "number" then
- meta.xPos,meta.yPos,meta.zPos = X,Y,Z
- else term.setCursorPos(8,11) print("Coordinates must be numbers") sleep(1.5) end
- if meta.deBug then logBug('GPS manual entry verified') end
- meta.isFound = true
- end
- end
- -- Initiate Step 2
- if meta.deBug then logBug('Initial Fuel check systems') end
- term.clear() term.setCursorPos(1,1)
- meta.targHeight = math.ceil(253 - meta.yPos)
- meta.fuelConsumption = math.ceil((meta.targHeight * 2) + 53)
- print[[S
- t
- e Estimated Fuel Consumption
- p +------------------+-------+
- | Fuel Consumption | |
- 2 | Current Fuel | |
- / | Amount Needed | |
- 3 +------------------+-------+
- ]]
- term.setCursorPos(27,5) term.write(meta.fuelConsumption)
- term.setCursorPos(27,6) term.write(turtle.getFuelLevel())
- if turtle.getFuelLevel == "unlimited" then
- if meta.deBug then logBug('Fuel is set to unlimited') end
- term.setCursorPos(27,6) term.write("-infi-")
- elseif turtle.getFuelLevel() < meta.fuelConsumption then
- local amount = math.ceil(meta.fuelConsumption - turtle.getFuelLevel())
- local coalA = math.ceil(amount/80)
- local woodA = math.ceil(amount/15)
- local fueling = true
- term.setCursorPos(27,7) term.write(amount)
- term.setCursorPos(2,9) term.write("Coal/Charcoal = "..coalA.. " | Wood/Planks = "..woodA)
- term.setCursorPos(3,11) term.write("Commence Fuelling Process ")
- print("place Fuel in first slot or hit 'x' to terminate")
- turtle.select(1) os.startTimer(1)
- while fueling do
- local ev = { os.pullEvent() }
- if ev[1] == "key" then
- if ev[2] == 45 then -- 'X'
- print("terminating")
- if meta.deBug then logBug('Fuel manual override') end
- sleep(1.5)
- os.reboot()
- end
- elseif ev[1] == "timer" then
- os.startTimer(0.5)
- end
- if turtle.getFuelLevel() > meta.fuelConsumption then
- print("Enough Fuel")
- if meta.deBug then logBug('Fuel check passed!') end
- fueling = false
- else
- if turtle.getItemCount(1) > 0 then
- if meta.deBug then logBug('refueled') end
- turtle.refuel(1)
- end
- end
- end
- else term.setCursorPos(8,10) term.write("PASSED!") sleep(1.5) end
- -- Initiate Step 3
- term.clear() term.setCursorPos(1,1)
- print[[S Building Supplies
- t +-----------+------------------+
- e | 1 2 3 4| 13 Floppy |
- p | 5 6 7 8| 14 Disk Drive |
- | | 15 [4] Modems |
- 3 |13 14 15 16| 16 [4] Computers |
- / +-----------+ |
- 3 | 1-8 your building blocks |
- +------------------------------+]]
- term.setCursorPos(6,11) term.write("Build Column [y/n] ")
- local ans = string.lower(read())
- if ans == "y" or ans == "yes" then
- meta.useMats = true
- if meta.deBug then logBug('Center Column Enabled') end
- else
- meta.useMats = false
- if meta.deBug then logBug('Center Column Disabled') end
- end
- term.setCursorPos(6,12) term.write("Start Program [y/n] ")
- local ans = string.lower(read())
- if ans == "y" or ans == "yes" then
- if meta.deBug then logBug('--------PROGRAM INITIATED--------') end
- stats.towerX, stats.towerZ, stats.towerHeight = meta.xPos, meta.zPos, meta.targHeight
- if meta.pArgs[1] == "test" then meta.targHeight = 10 end
- if meta.deBug then logBug('moving up to build cluster at: '..meta.targHeight) end
- for i=1,meta.targHeight do
- if meta.deBug then logBug('I:'..i) end
- placeBlock() walk("up")
- end
- if meta.fPos == 3 or meta.fPos == 1 then
- if meta.deBug then logBug('Cluster Orientation: East/West Axis') end
- walk("left",4)
- activateGPS()
- for i=1,7 do walk("right") placeBlock() end
- walk("right")
- activateGPS()
- walk("left",4)
- for i=1,3 do walk("down") end
- if turtle.detect() then turtle.dig() end
- walk("forward",3)
- activateGPS()
- for i=1,5 do walk("back") placeBlock() end
- walk("back")
- activateGPS()
- walk("down")
- walk("forward",3)
- else
- if meta.deBug then logBug('Cluster Orientation: North/South Axis') end
- walk("forward",4)
- activateGPS()
- for i=1,7 do walk("back") placeBlock() end
- walk("back")
- activateGPS()
- walk("down")
- walk("forward",4) placeBlock()
- walk("down") placeBlock()
- walk("down")
- walk("left",3)
- activateGPS()
- for i=1,5 do walk("right") placeBlock() end
- walk("right")
- activateGPS()
- walk("left",3)
- end
- if meta.deBug then logBug('moving down to touchdown') end
- while not turtle.detectDown() do walk("down") end
- if meta.deBug then logBug('ground touchdown') end
- if fs.exists("gps.pos") then
- local temp = {}
- local f = fs.open("gps.pos","r")
- temp = textutils.unserialize(f.readAll())
- f.close()
- local f = fs.open("gps.pos","w")
- temp[#temp+1] = stats
- f.write(textutils.serialize(temp))
- f.close()
- else -- if file doesnt exist
- local temp = {}
- local f = fs.open("gps.pos","w")
- temp[#temp+1] = stats
- f.write(textutils.serialize(temp))
- f.close()
- end
- nextCluster()
- term.clear() term.setCursorPos(1,2) print("GPS Tower Finished")
- if meta.deBug then logBug('--------PROGRAM ENDED SUCCESSFULLY--------') end
- for k,v in pairs(stats) do print(k..": "..v) end
- else
- if meta.deBug then logBug('program terminated') end
- term.clear() term.setCursorPos(2,1) term.write(meta.name.." program terminated")
- term.setCursorPos(1,3)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement