Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Platformer.lua
- -- A customizable platform builder
- -- Author: Austin Holbrook
- -- pastebin get hcWhNJZM platformer
- local CONFIG_FILE = '/etc/platformer.conf'
- local args = {...}
- local length = tonumber(args[1])
- local width = tonumber(args[2]) or length
- local height = tonumber(args[3]) or 1
- function table.contains(t, v)
- for _,data in pairs(t) do
- if data == v then
- return true
- end
- end
- return false
- end
- function showUsage()
- print("Usage:")
- print(" platformer LENGTH [WIDTH] [HEIGHT]")
- print()
- print("Build a platform that is LENGTH by")
- print("WIDTH using any materials in the")
- print("turtle's inventory. If WIDTH is omitted")
- print("the value of LENGTH is used. If height")
- print("is omitted, 1 is assumed.")
- end
- function conservativeRefuel()
- for i=1,16 do
- if turtle.getItemCount(i) ~= 0 then
- if table.contains(conf['fuelname'], turtle.getItemDetail(i)['name']) then
- turtle.select(i)
- while turtle.getFuelLevel() < tonumber(conf['lowfuel']) do
- turtle.refuel(1)
- if turtle.getItemCount() == 0 then
- break
- end
- end
- end
- end
- end
- end
- function nextNonFuel()
- for i=1,16 do
- if turtle.getItemCount(i) ~= 0 then
- if not table.contains(conf['fuelname'], turtle.getItemDetail(i)['name']) then
- return i
- end
- end
- end
- return nil
- end
- function loadConfig()
- local conf = {}
- if fs.exists(CONFIG_FILE) then
- local file = io.open(CONFIG_FILE, 'r')
- for line in file:lines() do
- line = line:match('%s*(.+)')
- if line and line:sub(1, 1) ~= '#' and line:sub(1, 1) ~= ";" then
- local option = line:match('%S+'):lower()
- local value = line:match('%S*%s*(.*)')
- if not value then
- conf[option] = true --If no value provided, it's a boolean
- else
- if not value:find(',') then
- conf[option] = value
- else
- value = value .. ','
- conf[option] = {}
- for entry in value:gmatch('%s*(.-),') do
- conf[option][#conf[option]+1] = entry
- end
- end
- end
- end
- end
- file:close()
- return conf
- else
- local time = textutils.formatTime( os.time(), false )
- local date = os.day()
- local file = fs.open(CONFIG_FILE, 'w')
- file.writeLine(
- [[## Config for platformer.lua
- ## Generated at ]] .. time .. ' on Day ' .. date .. '\n' .. [[
- # List of item names the turtle
- # considers fuel
- FUELNAME minecraft:coal, minecraft:coal_block
- # What should the turtle consider low
- # fuel? 1 fuel is 1 movement.
- LOWFUEL 100
- # Print debug output?
- ; DEBUG
- ]]
- )
- file.close()
- return loadConfig()
- end
- end
- conf = loadConfig()
- if conf['debug'] then
- print('FUELNAME:')
- for _, entry in pairs(conf['fuelname']) do
- print(' ' .. entry)
- end
- print('LOWFUEL: ' .. conf['lowfuel'])
- end
- if type(length) ~= 'number' or type(width) ~= 'number' or type(height) ~= 'number' then
- showUsage()
- error()
- end
- local normal = 1
- for h=1,height do
- for i=1,width do
- for j=1,length do
- if turtle.getFuelLevel() < tonumber(conf['lowfuel']) then conservativeRefuel() end
- if turtle.getItemCount() == 0 or table.contains(conf['fuelname'], turtle.getItemDetail()['name']) then
- turtle.select(nextNonFuel())
- end
- turtle.placeDown()
- if j ~= length then
- turtle.forward()
- end
- end
- if i % 2 == normal then
- turtle.turnRight()
- if i ~= width then
- turtle.forward()
- end
- turtle.turnRight()
- else
- turtle.turnLeft()
- if i ~= width then
- turtle.forward()
- end
- turtle.turnLeft()
- end
- end
- if h ~= height then
- if width % 2 == 0 and h % 2 == 1 then normal = 0
- else normal = 1
- end
- turtle.up()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment