Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Build-a-Tank Turtle v. 2.0
- -- Will build a tank (iron or steel)
- -- by Surferpup
- -- Creative Commons License
- -- Build Date January 8, 2014
- local version="2.0"
- local menu ={" Help "," Set Tank Size "," Move Turtle "," Refuel "," Start Building "," Quit & Power Off "}
- local selectedOption = 1
- local length = 3
- local width = 3
- local height = 4
- local maxOption = 6
- local quit = 0
- local wallSlot=1
- local wallSlotLimit = 4
- local guageSlot=5
- local guageSlotLimit = 8
- local valveSlot=9
- local valveSlotLimit = 12
- local readyToBuild = 0
- local progress = 0
- local goal = 0
- local level = "Bottom"
- local needWall=0
- local needGuage=0
- local needValve=0
- local needFuel=0
- local placedWall=0
- local placedGuage=0
- local placedValve=0
- local usedFuel = 0
- -- MOVE FUNCTIONS
- function forward(arg)
- arg = arg or 0
- local success = 1
- while not turtle.forward() do
- if turtle.getFuelLevel()==0 then
- success=0
- refuel("Please add more fuel.")
- elseif arg == -1 then
- break
- end
- end
- printNeed()
- if success==0 then
- if arg==0 then
- drawBuildDisplay()
- updateProgress(0)
- printNeed()
- elseif arg==-1 then
- displayMove()
- end
- end
- end
- function back(arg)
- arg = arg or 0
- local success = 1
- while not turtle.back() do
- if turtle.getFuelLevel()==0 then
- success=0
- refuel("Please add more fuel.")
- elseif arg == -1 then
- break
- end
- end
- printNeed()
- if success==0 then
- if arg==0 then
- drawBuildDisplay()
- updateProgress(0)
- printNeed()
- elseif arg==-1 then
- displayMove()
- end
- end
- end
- function up(arg)
- arg = arg or 0
- local success = 1
- while not turtle.up() do
- if turtle.getFuelLevel()==0 then
- success=0
- refuel("Please add more fuel.")
- elseif arg == -1 then
- break
- end
- end
- printNeed()
- if success==0 then
- if arg==0 then
- drawBuildDisplay()
- updateProgress(0)
- printNeed()
- elseif arg==-1 then
- displayMove()
- end
- end
- end
- function down(arg)
- arg = arg or 0
- local success = 1
- while (not turtle.down()) do
- if turtle.getFuelLevel()==0 then
- success=0
- refuel("Please add more fuel.")
- elseif arg == -1 then
- break
- end
- end
- printNeed()
- if success==0 then
- if arg==0 then
- drawBuildDisplay()
- updateProgress(0)
- printNeed()
- elseif arg==-1 then
- displayMove()
- end
- end
- end
- function turnRight()
- repeat
- until turtle.turnRight()
- end
- function turnLeft()
- repeat
- until turtle.turnLeft()
- end
- -- USER MOVE
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- function move()
- repeat
- local event,key = os.pullEvent("key")
- term.setCursorPos(36,1)
- term.write(key)
- if key == 17 then --w
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(8,4)
- term.write("Forward")
- forward(-1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(8,4)
- term.write("Forward")
- printNeed()
- elseif key == 30 then --a
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(7,6)
- term.write("L")
- turnLeft()
- os.sleep(.05)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(7,6)
- term.write("L")
- elseif key == 31 then --s
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(10,8)
- term.write("Back")
- back(-1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(10,8)
- term.write("Back")
- printNeed()
- elseif key == 32 then --d
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(15,6)
- term.write("R")
- turnRight()
- os.sleep (.05)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(15,6)
- term.write("R")
- elseif key == 57 or key==200 then --space or up arrow
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(20,4)
- term.write("Up")
- up(-1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(20,4)
- term.write("Up")
- printNeed()
- elseif key == 42 or key==208 then --shift or down arrow
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(18,8)
- term.write("Down")
- down(-1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(18,8)
- term.write("Down")
- printNeed()
- end
- until (key == 28) --enter
- drawBox(0)
- end
- -- BUILD FUNCTIONS
- function place(slot,limit)
- while(turtle.getItemCount(slot) == 0) do
- slot=slot+1
- if slot > limit then
- break
- end
- end
- if slot > limit then
- print ("Out of material")
- return -1
- else
- turtle.select(slot)
- turtle.placeDown()
- printNeed()
- updateProgress()
- end
- return slot
- end
- function buildTop(length,width)
- for i=1,length do
- for j=1,width do
- if (i == math.ceil(length/2) and j == math.ceil(width/2)) then
- placedValve=placedValve+1
- valveSlot=place(valveSlot,valveSlotLimit)
- else
- placedWall=placedWall+1
- wallSlot=place(wallSlot,wallSlotLimit)
- end
- if j<width then
- usedFuel=usedFuel+1
- forward()
- end
- end
- if i<length then
- if (math.floor(i/2)==math.ceil(i/2)) then
- turnRight()
- usedFuel=usedFuel+1
- forward()
- turnRight()
- else
- turnLeft()
- usedFuel=usedFuel+1
- forward()
- turnLeft()
- end
- else
- turnRight()
- end
- end
- end
- function placeWallSegment(distance)
- placedWall=placedWall+1
- wallSlot=place(wallSlot,wallSlotLimit)
- if wallSlot<0 then return end
- for i=1,distance-2 do
- usedFuel=usedFuel+1
- forward()
- placedGuage=placedGuage+1
- guageSlot=place(guageSlot,guageSlotLimit)
- end
- usedFuel=usedFuel+1
- forward()
- end
- function refuel(arg)
- drawBox(1)
- displayMessage("Refueling ...",nil,-1,nil)
- fuelStart = turtle.getFuelLevel()
- for i = 1,16 do
- turtle.select(i)
- turtle.refuel()
- end
- if fuelStart == turtle.getFuelLevel() then
- displayMessage("No Fuel in Inventory","ERROR",0,arg)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- drawBox(0)
- end
- function build()
- progress=0
- placedWall=0
- placedGuage=0
- placedValve=0
- usedFuel=0
- level="Bottom"
- goal = needWall+needGuage+2
- drawBuildDisplay()
- usedFuel=usedFuel+1
- up()
- updateProgress(0)
- buildTop(length,width)
- for i=1,height-2 do
- level = string.format("%1d of %1d",i+1,height)
- updateProgress(0)
- usedFuel=usedFuel+1
- up()
- placeWallSegment(length)
- turnRight()
- placeWallSegment(width)
- turnRight()
- placeWallSegment(length)
- turnRight()
- placeWallSegment(width)
- if i<height-2 then
- turnRight()
- end
- end
- turnRight()
- turnRight()
- usedFuel=usedFuel+1
- up()
- level = "Top "
- updateProgress(0)
- buildTop(length,width)
- turnRight()
- drawBox(0)
- placedWall=0
- placedGuage=0
- placedValve=0
- usedFuel=0
- printNeed()
- end
- -- USER INTERFACE AND CONTROL FUNCTIONS
- function updateProgress(arg)
- arg = arg or 1
- progress = progress+arg
- term.setCursorPos(7,6)
- term.write("On Level: "..level)
- term.setCursorPos(7,8)
- temp=string.format("Placed %d of %d",progress,goal)
- term.setCursorPos(6+11-math.floor((#(temp))/2),8)
- term.write(temp)
- term.setBackgroundColor(colors.white)
- term.setCursorPos(7,7)
- term.write(string.rep(" ",math.floor((progress/goal)*100/5)))
- term.setBackgroundColor(colors.black)
- end
- function centerPrint(text,ypos,highlight)
- text = text or ""
- ypos = ypos or 1
- highlight = highlight or 0
- term.setCursorPos(39/2-(#text)/2,ypos)
- if highlight==1 then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- term.write(text)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function menuOptionPrint(number,text,xpos,highlight)
- number = number or 1
- text = text or ""
- xpos = xpos or 1
- highlight = highlight or 0
- term.setCursorPos(5,xpos)
- term.write(tostring(number)..")")
- term.setCursorPos(8,xpos)
- if highlight==1 then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- term.write(text)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function displaySizes(argLength,argHeight,column)
- argLength = argLength or 3
- argHeight = argHeight or 6
- column = column or 1
- for i = 1,4 do
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(7,i+3)
- if argLength==(i*2+1) then
- term.write("> <")
- else
- term.write(" ")
- end
- if argLength==(i*2+1) and column == 1 then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- term.setCursorPos(8,i+3)
- term.write(string.format("%1dx%1d",i*2+1,i*2+1))
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(13,5)
- term.write("X")
- for i = 1,5 do
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(15,i+3)
- if argHeight==(i+3) then
- term.write("> <")
- else
- term.write(" ")
- end
- if argHeight==(i+3) and column == 2 then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- term.setCursorPos(16,i+3)
- term.write(string.format("%1d",i+3))
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(19,5)
- term.write("Press")
- term.setCursorPos(19,6)
- term.write("\"Enter\"")
- term.setCursorPos(19,7)
- term.write("when")
- term.setCursorPos(19,8)
- term.write("done.")
- end
- function rollSize(argLength,argHeight,argColumn,argKey)
- argLength = argLength or 3
- argHeight = argHeight or 4
- argHeight = argHeight or 1
- argKey = argKey or 203
- if (argKey == 200 or argKey==208) then
- if argColumn==1 then
- if argKey==200 then
- argLength = argLength-2
- else
- argLength = argLength+2
- end
- if math.floor((argLength-1)/2)<1 then
- argLength = 9
- elseif math.floor((argLength-1)/2)>4 then
- argLength = 3
- end
- else
- if argKey==200 then
- argHeight = argHeight-1
- else
- argHeight = argHeight+1
- end
- if argHeight > 8 then
- argHeight = 4
- elseif argHeight < 4 then
- argHeight = 8
- end
- end
- elseif (argKey == 203 or argKey==205 or argKey == 15) then
- if argColumn == 1 then
- argColumn = 2
- else
- argColumn = 1
- end
- end
- length = argLength
- width = argLength
- height = argHeight
- return argColumn
- end
- function printMenu(option)
- option = option or selectedOption
- selectedOption = option
- if selectedOption < 1 then
- selectedOption = maxOption
- elseif selectedOption>maxOption then
- selectedOption = 1
- end
- for i=1,#menu do
- if i == selectedOption then highlight = 1 else highlight = 0 end
- menuOptionPrint(i,menu[i],2+i,highlight)
- end
- end
- function printNeed()
- readyToBuild=1
- --usedFuel=0
- needWall = (length*width*2-2)+((height-2)*4)-placedWall
- needGuage = 2*(length-2 + width - 2)*(height-2)-placedGuage
- needFuel = 2*(length*width + 1) + (length+width-2)*2*(height-2)
- currentWall=0
- for i=1,4 do
- currentWall=currentWall+turtle.getItemCount(i)
- end
- term.setCursorPos(9,12)
- term.write(string.format("%3d",needWall))
- term.setCursorPos(9,13)
- if (currentWall>=needWall) then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- wallString=" OK"
- else
- readyToBuild = 0
- wallString=string.format("%3d",currentWall)
- end
- term.write(wallString)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- currentGuage=0
- for i=5,8 do
- currentGuage=currentGuage+turtle.getItemCount(i)
- end
- term.setCursorPos(17,12)
- term.write(string.format("%3d",needGuage))
- term.setCursorPos(17,13)
- if (currentGuage>=needGuage) then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- guageString=" OK"
- else
- readyToBuild = 0
- guageString=string.format("%3d",currentGuage)
- end
- term.write(guageString)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- currentValve=0
- term.setCursorPos(25,12)
- term.write(string.format("%3d",2-placedValve))
- term.setCursorPos(25,13)
- for i=9,12 do
- currentValve=currentValve+turtle.getItemCount(i)
- end
- if (currentValve>=2-placedValve) then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- valveString=" OK"
- else
- readyToBuild = 0
- valveString=string.format("%3d",currentValve)
- end
- term.write(valveString)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- currentFuelLevel = turtle.getFuelLevel()
- term.setCursorPos(33,12)
- term.write(string.format("%3d",needFuel-usedFuel))
- term.setCursorPos(33,13)
- if (currentFuelLevel>=needFuel-usedFuel) then
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- fuelString=" OK"
- else
- readyToBuild = 0
- fuelString=string.format("%3d",currentFuelLevel)
- end
- term.write(fuelString)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function printSelectedSize()
- term.setCursorPos(34,3)
- term.write("Size=")
- term.setCursorPos(34,4)
- term.write(string.format("%1dx%1dx%1d",length,width,height))
- term.setCursorPos(34,5)
- term.write("L W H")
- end
- function printMainScreen()
- term.clear()
- centerPrint(" Build-a-Tank Turtle v. "..version,1,1)
- printMenu(selectedOption)
- printSelectedSize()
- term.setCursorPos(1,11)
- term.write(" Wall Guage Valve Fuel")
- term.setCursorPos(1,12)
- term.write("NEED:")
- term.setCursorPos(1,13)
- term.write("HAVE:")
- printNeed()
- end
- function printKey(arg)
- term.setCursorPos(36,13)
- term.write(string.format("%3d",arg))
- end
- function drawBuildDisplay()
- drawBox(1)
- local temp = string.format("Building %1dx%1dx%1d Tank",length,width,height)
- term.setCursorPos(6+11-math.floor((#(temp))/2),4)
- term.write(temp)
- end
- function rollMenu(arg)
- if arg==200 then
- selectedOption = selectedOption-1
- else
- selectedOption=selectedOption+1
- end
- printMenu()
- end
- function selectSize()
- local result = 1
- drawBox(1)
- displaySizes(length,height,1)
- while true do
- local event,key = os.pullEvent("key")
- if key == 28 then
- return
- end
- result = rollSize(length,height,result,key)
- printNeed()
- printSelectedSize()
- displaySizes(length,height,result)
- end
- drawBox(0)
- end
- function doMenu(arg)
- if arg==1 then
- displayHelp()
- printMainScreen()
- printMenu()
- printNeed()
- elseif arg==2 then
- selectSize()
- printMainScreen()
- printMenu()
- printNeed()
- elseif arg==3 then
- displayMove()
- move()
- printMenu()
- elseif arg == 4 then
- refuel()
- printMenu()
- printNeed()
- elseif arg == 5 then
- if readyToBuild == 1 then
- build()
- printMenu()
- else
- displayMessage("Need Items/Fuel","ERROR",0)
- printMenu()
- end
- elseif arg == 6 then
- term.clear()
- term.setCursorPos(math.floor(39/2 - #"Shutting down turtle ..."/2),6)
- textutils.slowWrite("Shutting down turtle ...", 10)
- os.shutdown()
- return 1
- else
- displayHelp()
- printMainScreen()
- printMenu()
- printNeed()
- end
- return 0
- end
- function drawBox(arg)
- arg = arg or 1
- if arg== 1 then -- Draw box
- term.setCursorPos(5,3)
- term.setBackgroundColor(colors.white)
- term.write(string.rep(" ",24))
- for i = 1,6 do
- term.setBackgroundColor(colors.white)
- term.setCursorPos(5,3+i)
- term.write(" ")
- term.setCursorPos(28,3+i)
- term.write(" ")
- term.setCursorPos(6,3+i)
- term.setBackgroundColor(colors.black)
- term.write(string.rep(" ",22))
- end
- term.setCursorPos(5,10)
- term.setBackgroundColor(colors.white)
- term.write(string.rep(" ",24))
- term.setBackgroundColor(colors.black)
- else -- Erase box
- for i = 3,10 do
- term.setCursorPos(5,i)
- term.write(string.rep(" ",24))
- end
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function displayMessage(message,title,timeout,message2)
- title = title or ""
- title = string.sub(title,1,22):find'^%s*$' and '' or string.sub(title,1,22):match'^%s*(.*%S)'
- message = message or " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 "
- message = string.sub(message,1,22):find'^%s*$' and '' or string.sub(message,1,22):match'^%s*(.*%S)'
- message2 = message2 or ""
- message2 = string.sub(message2,1,22):find'^%s*$' and '' or string.sub(message2,1,22):match'^%s*(.*%S)'
- timeout = timeout or 0
- drawBox(1)
- term.setCursorPos(6+11-math.floor((#(title))/2),4)
- term.write(title)
- term.setCursorPos(6+11-math.floor((#(message))/2),6)
- term.write(message)
- term.setCursorPos(6+11-math.floor((#(message2))/2),7)
- term.write(message2)
- if timeout == 0 then
- term.setCursorPos(8,9)
- term.write("(Press any key ...)")
- os.pullEvent("key")
- drawBox(0)
- elseif timeout>0 then
- os.sleep(timeout)
- drawBox(0)
- end
- end
- function displayMove()
- drawBox()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(8,4)
- term.write("Forward")
- term.setCursorPos(20,4)
- term.write("Up")
- term.setCursorPos(7,6)
- term.write("L")
- term.setCursorPos(15,6)
- term.write("R")
- term.setCursorPos(10,8)
- term.write("Back")
- term.setCursorPos(18,8)
- term.write("Down")
- term.setCursorPos(6,9)
- term.write("(Return key when done)")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.setCursorPos(10,5)
- term.write(" W ")
- term.setCursorPos(16,5)
- term.write(" Up Arrow ")
- term.setCursorPos(9,6)
- term.write(" A ")
- term.setCursorPos(12,6)
- term.write("D ")
- term.setCursorPos(10,7)
- term.write(" S ")
- term.setCursorPos(16,7)
- term.write(" Dn Arrow ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function displayHelp()
- term.clear()
- term.setCursorPos(5,1)
- print("This turtle will build an iron or steel tank for you of any legal size. "..
- "Building starts from its current position and continues forward and to the left. "..
- "The turtle must be stocked with enough items and fuel to build the selected tank size (amounts are shown).")
- term.setCursorPos(5,8)
- print("Stock Inventory slots 1-4 with walls, 5-8 with walls or guages, 9-12 with valves and 13-16 with fuel items. "..
- "You can set the tank size, position the turtle or refuel it as necessary.")
- centerPrint("Press any key to return to menu.",13,1)
- os.pullEvent(key)
- term.clear()
- end
- --Begin Main Program
- printMainScreen()
- while quit==0 do
- local event, key = os.pullEvent("key")
- printNeed()
- if key==2 then
- printMenu(1)
- elseif key==3 then
- printMenu(2)
- elseif key==4 then
- printMenu(3)
- elseif key==5 then
- printMenu(4)
- elseif key==6 or key == 16 then
- printMenu(5)
- elseif key == 200 or key == 208 then
- rollMenu(key)
- elseif key == 28 then
- quit = doMenu(selectedOption)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment