mah17

Platformer

Apr 8th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.89 KB | None | 0 0
  1. -- Platformer.lua
  2. -- A customizable platform builder
  3.  
  4. -- Author: Austin Holbrook
  5. -- pastebin get hcWhNJZM platformer
  6.  
  7. local CONFIG_FILE = '/etc/platformer.conf'
  8.  
  9. local args = {...}
  10. local length = tonumber(args[1])
  11. local width = tonumber(args[2]) or length
  12. local height = tonumber(args[3]) or 1
  13.  
  14. function table.contains(t, v)
  15.   for _,data in pairs(t) do
  16.     if data == v then
  17.       return true
  18.     end
  19.   end
  20.   return false
  21. end
  22.  
  23. function showUsage()
  24.   print("Usage:")
  25.   print("  platformer LENGTH [WIDTH] [HEIGHT]")
  26.   print()
  27.   print("Build a platform that is LENGTH by")
  28.   print("WIDTH using any materials in the")
  29.   print("turtle's inventory. If WIDTH is omitted")
  30.   print("the value of LENGTH is used. If height")
  31.   print("is omitted, 1 is assumed.")
  32. end
  33.  
  34. function conservativeRefuel()
  35.   for i=1,16 do
  36.     if turtle.getItemCount(i) ~= 0 then
  37.       if table.contains(conf['fuelname'], turtle.getItemDetail(i)['name']) then
  38.         turtle.select(i)
  39.         while turtle.getFuelLevel() < tonumber(conf['lowfuel']) do
  40.           turtle.refuel(1)
  41.           if turtle.getItemCount() == 0 then
  42.             break
  43.           end
  44.         end
  45.       end
  46.     end
  47.   end
  48. end
  49.  
  50. function nextNonFuel()
  51.   for i=1,16 do
  52.     if turtle.getItemCount(i) ~= 0 then
  53.       if not table.contains(conf['fuelname'], turtle.getItemDetail(i)['name']) then
  54.         return i
  55.       end
  56.     end
  57.   end
  58.   return nil
  59. end
  60.  
  61. function loadConfig()
  62.   local conf = {}
  63.   if fs.exists(CONFIG_FILE) then
  64.     local file = io.open(CONFIG_FILE, 'r')
  65.  
  66.     for line in file:lines() do
  67.       line = line:match('%s*(.+)')
  68.       if line and line:sub(1, 1) ~= '#' and line:sub(1, 1) ~= ";" then
  69.         local option = line:match('%S+'):lower()
  70.         local value = line:match('%S*%s*(.*)')
  71.  
  72.         if not value then
  73.           conf[option] = true --If no value provided, it's a boolean
  74.         else
  75.           if not value:find(',') then
  76.             conf[option] = value
  77.           else
  78.             value = value .. ','
  79.             conf[option] = {}
  80.             for entry in value:gmatch('%s*(.-),') do
  81.               conf[option][#conf[option]+1] = entry
  82.             end
  83.           end
  84.         end
  85.       end
  86.     end
  87.     file:close()
  88.  
  89.     return conf
  90.  
  91.   else
  92.     local time = textutils.formatTime( os.time(), false )
  93.     local date = os.day()
  94.     local file = fs.open(CONFIG_FILE, 'w')
  95.     file.writeLine(
  96. [[## Config for platformer.lua
  97. ## Generated at ]] .. time .. ' on Day ' .. date .. '\n' .. [[
  98.  
  99. # List of item names the turtle
  100. # considers fuel
  101. FUELNAME minecraft:coal, minecraft:coal_block
  102.  
  103. # What should the turtle consider low
  104. # fuel? 1 fuel is 1 movement.
  105. LOWFUEL 100
  106.  
  107. # Print debug output?
  108. ; DEBUG
  109. ]]
  110.     )
  111.     file.close()
  112.     return loadConfig()
  113.   end
  114. end
  115.  
  116. conf = loadConfig()
  117.  
  118. if conf['debug'] then
  119.   print('FUELNAME:')
  120.   for _, entry in pairs(conf['fuelname']) do
  121.     print('  ' .. entry)
  122.   end
  123.   print('LOWFUEL: ' .. conf['lowfuel'])
  124. end
  125.  
  126. if type(length) ~= 'number' or type(width) ~= 'number' or type(height) ~= 'number' then
  127.   showUsage()
  128.   error()
  129. end
  130.  
  131. local normal = 1
  132. for h=1,height do
  133.   for i=1,width do
  134.     for j=1,length do
  135.       if turtle.getFuelLevel() < tonumber(conf['lowfuel']) then conservativeRefuel() end
  136.       if turtle.getItemCount() == 0 or table.contains(conf['fuelname'], turtle.getItemDetail()['name']) then
  137.         turtle.select(nextNonFuel())
  138.       end
  139.       turtle.placeDown()
  140.       if j ~= length then
  141.         turtle.forward()
  142.       end
  143.     end
  144.     if i % 2 == normal then
  145.       turtle.turnRight()
  146.       if i ~= width then
  147.         turtle.forward()
  148.       end
  149.       turtle.turnRight()
  150.     else
  151.       turtle.turnLeft()
  152.       if i ~= width then
  153.         turtle.forward()
  154.       end
  155.       turtle.turnLeft()
  156.     end
  157.   end
  158.   if h ~= height then
  159.     if width % 2 == 0 and h % 2 == 1 then normal = 0
  160.     else normal = 1
  161.     end
  162.     turtle.up()
  163.   end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment