Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CONFIG
- local selection = 1
- local selections = 0
- local event, p1, p2, p3, p4, p5 = 0, 0, 0, 0, 0, 0
- local state = "init"
- local menu = {}
- local work = {}
- local fuel = 0
- -- Colors
- local isAdvanced = term.isColor and term.isColor()
- local tBarBg = 0
- local tBarTx = 0
- if isAdvanced then
- -- Color scheme for advanced computers
- else
- -- Color scheme for basic computers and turtles
- tBarBg = colors.white
- tBarTx = colors.black
- stdBg = colors.black
- stdTx = colors.white
- btnSelectBg = colors.white
- btnSelectTx = colors.black
- btnNormBg = colors.black
- btnNormTx = colors.white
- end
- function clearScreen()
- term.setBackgroundColor(stdBg)
- term.setTextColor(stdTx)
- term.clear()
- term.setCursorPos(1,1)
- end
- function drawTitleBar(extraLabel)
- term.setBackgroundColor(tBarBg)
- term.setTextColor(tBarTx)
- term.clearLine()
- term.setCursorPos(3,1)
- if extraLabel ~= "" then
- print("Automaton 5000 - " .. extraLabel)
- else
- print("Automaton 5000")
- end
- end
- function drawStatusBar(message)
- term.setBackgroundColor(tBarBg)
- term.setTextColor(tBarTx)
- term.setCursorPos(1,13)
- term.clearLine()
- io.write(message)
- end
- function drawButton(pos, label)
- if pos == selection then
- term.setBackgroundColor(btnSelectBg)
- term.setTextColor(btnSelectTx)
- else
- term.setBackgroundColor(btnNormBg)
- term.setTextColor(btnNormTx)
- end
- if pos == 1 then
- term.setCursorPos(3,3)
- end
- if pos == 2 then
- term.setCursorPos(3,5)
- end
- if pos == 3 then
- term.setCursorPos(3,7)
- end
- if pos == 4 then
- term.setCursorPos(3,9)
- end
- if pos == 5 then
- term.setCursorPos(3,11)
- end
- print("> " .. label)
- end
- function drawConfirmation(pos, label)
- if pos == selection then
- term.setBackgroundColor(btnSelectBg)
- term.setTextColor(btnSelectTx)
- else
- term.setBackgroundColor(btnNormBg)
- term.setTextColor(btnNormTx)
- end
- if pos == 1 then
- term.setCursorPos(3,11)
- end
- if pos == 2 then
- term.setCursorPos(15,11)
- end
- print("> " .. label)
- end
- function drawDescription()
- term.setBackgroundColor(stdBg)
- term.setTextColor(stdTx)
- term.setCursorPos(1,2)
- print(menu.description)
- end
- function toggleConfirmation()
- if selection == 1 then
- selection = 2
- else
- selection = 1
- end
- end
- function place(slotNum)
- turtle.select(slotNum)
- turtle.placeDown()
- end
- function forward()
- checkFuel()
- if not turtle.forward() then
- turtle.dig()
- return forward()
- end
- end
- function up()
- checkFuel()
- if not turtle.up() then
- turtle.digUp()
- return up()
- end
- end
- function turnRight()
- turtle.turnRight()
- end
- function turnLeft()
- turtle.turnLeft()
- end
- function checkFuel()
- fuel = turtle.getFuelLevel()
- --print(fuel)
- if fuel == 0 then
- turtle.select(16)
- turtle.refuel(1)
- end
- fuel = turtle.getFuelLevel()
- if fuel == 0 then
- error("Ran out of fuel")
- end
- if turtle.getItemCount(16) < 2 then
- fuelLow = 1
- else
- fuelLow = 0
- end
- if fuelLow == 1 then
- print("Fuel is low...")
- end
- end
- function placeRectangle(width, length, slot)
- local dir = true
- for i = 1,width do
- for j = 1,length do
- place(slot)
- if j ~= length then
- forward()
- end
- end
- if i ~= width then
- dir = moveOver(dir)
- end
- end
- end
- function moveOver(dir)
- if dir then
- turnRight()
- forward()
- turnRight()
- else
- turnLeft()
- forward()
- turnLeft()
- end
- return not dir
- end
- function mineForwards()
- while turtle.detect() do
- turtle.dig()
- sleep(0.9)
- end
- forward()
- while turtle.detectUp() do
- turtle.digUp()
- sleep(0.9)
- end
- turtle.digDown()
- end
- function compare(slot)
- turtle.select(slot)
- return turtle.compare()
- end
- -- Mining functions
- function tunnel(tunnelWidth)
- tunnelWidth = tunnelWidth or 2
- while true do
- mineForwards()
- if tunnelWidth > 1 then
- turnRight()
- for i = 2,tunnelWidth do
- mineForwards()
- end
- turnLeft()
- mineForwards()
- turnLeft()
- for i = 2,tunnelWidth do
- mineForwards()
- end
- turnRight()
- end
- end
- end
- -- Tree Farm
- function killTree()
- local offset = 1
- mineForwards()
- up()
- while turtle.detectUp() do
- for i = 1, 4 do
- turnRight()
- turtle.dig()
- end
- up()
- offset = offset + 1
- end
- while offset > 0 do
- turtle.down()
- offset = offset - 1
- end
- place(14) -- replant
- turnRight()
- turnRight()
- forward()
- turnRight()
- turnRight()
- end
- function patrolForTrees()
- up()
- up()
- turnLeft()
- forward()
- forward()
- turnRight()
- forward()
- forward()
- --first nodes
- for i = 1, 5 do
- turnLeft()
- if compare(15) then
- killTree()
- end
- turnRight()
- turnRight()
- if compare(15) then
- killTree()
- end
- turnLeft()
- forward()
- forward()
- end
- turnRight()
- for i = 1, 4 do
- forward()
- end
- turnRight()
- forward()
- forward()
- for i = 1, 5 do
- turnLeft()
- if compare(15) then
- killTree()
- end
- turnRight()
- turnRight()
- if compare(15) then
- killTree()
- end
- turnLeft()
- forward()
- forward()
- end
- turnRight()
- forward()
- forward()
- turtle.down()
- turtle.down()
- turnRight()
- end
- function farmTrees()
- while true do
- patrolForTrees()
- sleep(100)
- end
- end
- -- Building functions
- function buildHut()
- up()
- -- Floor
- placeRectangle(7,7,1)
- up()
- -- Walls
- turtle.turnRight()
- turtle.turnRight()
- for i = 1,3 do
- for j = 1,4 do
- place(2)
- forward()
- placeRectangle(1,5,3)
- forward()
- place(2)
- turtle.turnRight()
- end
- up()
- end
- -- Ceiling
- placeRectangle(7,7,4)
- end
- function buildFarm()
- -- Floor
- up()
- placeRectangle(11,5,1)
- forward()
- turnLeft()
- placeRectangle(5,11,2)
- turnRight()
- forward()
- turnRight()
- placeRectangle(1,9,1)
- forward()
- placeRectangle(1,2,2)
- -- Dirt/water layer
- up()
- turnRight()
- for i = 1, 4 do
- placeRectangle(1,11,3)
- turnRight()
- end
- turnRight()
- forward()
- turnLeft()
- forward()
- placeRectangle(4,9,4)
- turnLeft()
- forward()
- turnLeft()
- placeRectangle(5,4,4)
- forward()
- turnLeft()
- placeRectangle(5,4,5)
- forward()
- turnLeft()
- placeRectangle(1,4,5)
- forward()
- place(6)
- -- Walls/fences
- up()
- for i = 1, 5 do
- forward()
- end
- turnRight()
- for i = 1, 5 do
- forward()
- end
- turnRight()
- for i = 1, 4 do
- placeRectangle(1,11,7)
- turnRight()
- end
- -- Wall/fence cap
- up()
- for i = 1, 4 do
- placeRectangle(1,11,8)
- turnRight()
- end
- end
- function buildTreeFarm()
- -- Main layer
- up()
- for i = 1, 4 do
- placeRectangle(1,11,1)
- turnRight()
- end
- turnRight()
- forward()
- turnLeft()
- forward()
- placeRectangle(7,9,2)
- turnRight()
- forward()
- placeRectangle(9,2,3)
- end
- local menus = {
- -- high level options
- [0] = {
- title = "Main Menu",
- type = "menu",
- options = {1, 2, 3, 4}
- },
- [1] = {
- title = "Resource Gathering",
- type = "menu",
- options = {11, 12, 13, 0}
- },
- [2] = {
- title = "Buildings",
- type = "menu",
- options = {21, 22, 23, 24, 0}
- },
- [3] = {
- title = "Miscellaneous",
- type = "menu",
- options = {31, 0}
- },
- [4] = {
- title = "Exit",
- type = "menu",
- options = {-1}
- },
- -- resource options
- [11] = {
- title = "Mining",
- type = "menu",
- options = {111, 112, 113, 1}
- },
- [12] = {
- title = "Tree Farming",
- type = "menu",
- options = {121, 1}
- },
- [121] = {
- title = "Chopping trees",
- type = "work",
- options = {4},
- work = farmTrees
- },
- [13] = {
- title = "Crops (no)",
- type = "menu",
- options = {0}
- },
- -- buildings options
- [21] = {
- title = "Hut",
- type = "menu",
- options = {211, 2}
- },
- [22] = {
- title = "Farm",
- type = "menu",
- options = {221, 2}
- },
- [23] = {
- title = "Tree Farm",
- type = "menu",
- options = {231, 2}
- },
- [24] = {
- title = "Tower",
- type = "menu",
- options = {241, 2}
- },
- -- miscellaneous
- [31] = {
- title = "Miscellaneous",
- type = "menu",
- options = {0}
- },
- -- Mining options
- [111] = {
- title = "Branch (no)",
- type = "menu",
- options = {0}
- },
- [112] = {
- title = "Excavate (no)",
- type = "menu",
- options = {0}
- },
- [113] = {
- title = "Tunnel",
- type = "confirm",
- options = {1131,11},
- description = [[
- Gonna dig me a tunnel
- a nice long tunnel...
- gonna be 2x3 and go on
- forever...
- ]]
- },
- [1131] = {
- title = "Tunnelling",
- type = "work",
- options = {4},
- work = tunnel
- },
- -- Buildings to build
- [211] = {
- title = "Build Hut",
- type = "confirm",
- options = {2111, 21},
- description = [[
- This will build a simple 7x7x5 hut.
- Fuel goes in slot #16.
- Floor pieces go in slot #1.
- Corner pieces go in slot #2.
- Wall pieces go in slot #3.
- Ceiling pieces go in slot #4.
- Door goes in slot #5.
- Turtle starts in lower-left corner.
- ]]
- },
- [2111] = {
- title = "Building a hut...",
- type = "work",
- options = {4},
- work = buildHut
- },
- [221] = {
- title = "Build Farm",
- type = "confirm",
- options = {2211, 21},
- description = [[
- This will build a 11x11x5 farm.
- Materials needed:
- 64F| 57F| 44W| 64D
- 64D| 1 W| 64W| 64W
- - | - | - | -
- - | - | - | *
- Turtle starts in lower-left corner.
- ]]
- },
- [2211] = {
- title = "Building a farm...",
- type = "work",
- options = {4},
- work = buildFarm
- },
- [231] = {
- title = "Build Tree Farm",
- type = "confirm",
- options = {2311, 21},
- description = [[
- This will build a 11x11x1 tree farm.
- Materials needed:
- 64W| 64D| 64D| -
- - | - | - | -
- - | - | - | -
- - | - | - | *
- Turtle starts in lower-left corner.
- ]]
- },
- [2311] = {
- title = "Building a tree farm...",
- type = "work",
- options = {4},
- work = buildTreeFarm
- },
- [241] = {
- title = "Build Tower",
- type = "confirm",
- options = {2411, 21},
- description = [[
- This will build a 5x5x10 tower.
- Materials needed:
- - | - | - | -
- - | - | - | -
- - | - | - | -
- - | - | - | *
- Turtle starts in lower-left corner.
- ]]
- },
- [2411] = {
- title = "Building a tower...",
- type = "work",
- options = {4},
- work = buildTower
- }
- }
- local gui = {
- menu = function(m)
- return menus[m]
- end
- }
- function tableLength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function redrawGui()
- -- Settings
- selections = tableLength(menu.options)
- -- Reset colors
- clearScreen()
- -- Title bar
- drawTitleBar(menu.title)
- -- Buttons
- if menu.type == "menu" then
- for i,v in ipairs(menu.options) do
- drawButton(i, menus[v].title)
- end
- end
- if menu.type == "confirm" then
- drawDescription(menu.description)
- drawConfirmation(1, "Yes")
- drawConfirmation(2, "No")
- end
- if menu.type == "work" then
- end
- -- Status bar
- drawStatusBar(menu.title .. " " .. selection .. " / " .. selections)
- end
- function getMenuKeys()
- while true do
- event, p1, p2, p3, p4, p5 = os.pullEvent()
- if event == "key" then
- if p1 == 200 then --Up arrow
- if selection > 1 then
- selection = selection - 1
- else
- selection = selections
- end
- break
- end
- if p1 == 203 then --Left arrow
- if menu.type == "confirm" then
- toggleConfirmation()
- end
- break
- end
- if p1 == 205 then --Right arrow
- if menu.type == "confirm" then
- toggleConfirmation()
- end
- break
- end
- if p1 == 208 then --Down arrow
- if selection < selections then
- selection = selection + 1
- else
- selection = 1
- end
- break
- end
- if p1 == 28 then --Return
- if menu.options[selection] == 4 then
- return false
- end
- menu = menus[menu.options[selection]]
- selection = 1
- if menu.type == "work" then
- --state = "working"
- menu.work()
- end
- break
- end
- end
- end
- return true
- end
- -- Main loop
- function main()
- while true do
- if state == "init" then
- menu = gui.menu(0)
- redrawGui()
- selection = 1
- state = "menus"
- end
- if state == "menus" then
- redrawGui()
- if getMenuKeys() == false then
- break
- end
- end
- end
- end
- -- Cleanup function - Leaves the user with a clear screen
- function cleanup()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- end
- -- mainMenu()
- -- Call Main Loop and cleanup once it has finished
- main()
- cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement