Advertisement
theoriginalbit

My First Program: Flint Maker

Apr 15th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. --[[
  2. Author: TheOriginalBIT
  3. Version: 1.0
  4. Created: 14 Dec 2012
  5. ]]--
  6.  
  7.  
  8. -----------------------------------
  9. ----- UPDATE SERVER VARIABLES -----
  10. -----------------------------------
  11. creator = "TheOriginalBIT"
  12. version = 1.0
  13. progName = "Flint Maker"
  14. identifier = ("com.TOBIT."..progName)
  15. forSys = Turtle
  16. -----------------------------------
  17. ------------- UI CODE -------------
  18. ------ CODE FROM Cookiebal's ------
  19. ------ Cookie System program ------
  20. -----------------------------------
  21. sizeX, sizeY = term.getSize()
  22. local function formatTime(nTime)
  23.     local nHour = math.floor(nTime)
  24.     local nMinute = math.floor( (nTime - nHour) * 60 )
  25.     return string.format("%02d:%02d", nHour, nMinute)
  26. end
  27. local function drawTime(x, y)
  28.     sX, sY = term.getCursorPos()
  29.     if x == nil or y == nil then return end
  30.    
  31.     local nTime = os.time()
  32.     local currTime = formatTime(nTime)
  33.    
  34.     term.setCursorPos(x,y)
  35.     write(currTime)
  36.    
  37.     term.setCursorPos(sX, sY)
  38. end
  39. local function drawBorder(x1, y1, x2, y2, filled)
  40.     for y = y1, y2 do
  41.         for x = x1, x2 do
  42.             term.setCursorPos(x, y)
  43.             if (x == x1 and y == y1) or (x == x1 and y == y2) or (x == x2 and y == y1) or (x == x2 and y == y2) then
  44.                 write("+")
  45.             elseif x == x1 or x == x2 then
  46.                 write("|")
  47.             elseif y == y1 or y == y2 then
  48.                 write("-")
  49.             else
  50.                 if filled then write(" ") end
  51.             end
  52.         end
  53.     end
  54.    
  55.     term.setCursorPos(1, y2)
  56. end
  57. local function drawTextCenter(text, y)
  58.     if type(text) == "string" then
  59.         term.setCursorPos(sizeX / 2 - (string.len(text) / 2), y)
  60.         write(text)
  61.     elseif type(text) == "table" then
  62.         arr = {}
  63.         arr = text
  64.         for i = 0, table.getn(arr) - 1 do
  65.             term.setCursorPos(sizeX / 2 - (string.len(arr[i + 1]) / 2), y + i)
  66.             write(arr[i + 1])
  67.         end
  68.     end
  69. end
  70.  
  71. -- My own clear screen that makes draws default UI
  72. local function clearScreen()
  73.     term.clear()
  74.     drawBorder(1, 1, sizeX - 1, 3, false)
  75.     term.setCursorPos(1,1)
  76.     drawTextCenter(string.format("TOBIT %s v%f", progName, version), 2)
  77.     term.setCursorPos(1, 5)
  78. end
  79. -----------------------------------
  80. -------- FLINT MAKER CODE ---------
  81. -----------------------------------
  82. currSlot = 1
  83. showTime = true
  84. local function clearInvent()
  85.     pcall(sleep, 1)
  86.     clearScreen()
  87.     print("Found Items in inventory...")
  88.     pcall(sleep, 1)
  89.     print("Clearing Inventory for successfull program execution...")
  90.     for i = 1, 9 do
  91.         turtle.select(i)
  92.         turtle.drop()
  93.     end
  94.     turtle.select(1)
  95. end
  96. local function countSlotsWithItems()
  97.     local count = 0
  98.     for i = 1, 9 do
  99.         if turtle.getItemCount(i) > 0 then
  100.             count = count + 1
  101.         end
  102.     end
  103.    
  104.     return count
  105. end
  106. local function checkGravelCount()
  107.     x, y = term.getCursorPos()
  108.     while countSlotsWithItems() > 8 do
  109.         term.setCursorPos(x, y)
  110.         print("You gave me too much, I need at least one slot empty for when I make flint.")
  111.         sleep(0.5)
  112.     end
  113. end
  114. local function incrementSlot()
  115.     if currSlot ~= 9 then
  116.         currSlot = currSlot + 1
  117.         turtle.select(currSlot)
  118.         return true
  119.     else
  120.         return false
  121.     end
  122. end
  123. local function currSlotHasItems()
  124.     return (turtle.getItemCount(currSlot) > 0)
  125. end
  126. local function setup()
  127.     clearScreen()
  128.     write("Have you placed gravel in my inventory? ")
  129.     answer = read()
  130.     if string.upper(answer) == "YES" then
  131.         if countSlotsWithItems() == 0 then print("You did not supply me any items") end
  132.     elseif string.upper(answer) == "NO" then
  133.         if countSlotsWithItems() > 0 then clearInvent() end
  134.     else
  135.         print("Unknown response")
  136.         pcall(sleep, 1)
  137.         print("Quitting")
  138.         pcall(sleep, 1)
  139.         return false
  140.     end
  141.    
  142.     if countSlotsWithItems() == 0 then
  143.         clearScreen()
  144.         print("Please give me some gravel...")
  145.         x, y = term.getCursorPos()
  146.         i = 10      
  147.         while i > -1 do
  148.             term.setCursorPos(x, y) -- Overwrite the same line to appear that seconds are changing
  149.             term.clearLine()
  150.             print("Starting in "..i.."...")
  151.             if i == 10 then tx, ty = term.getCursorPos() end
  152.             pcall(sleep, 1)
  153.             i = i - 1
  154.         end
  155.         print()
  156.         if countSlotsWithItems() == 0 then
  157.             clearScreen()
  158.             print("You did not supply me any items")
  159.             print("Quitting")
  160.             return false
  161.         end
  162.     end
  163.    
  164.     checkGravelCount()
  165.    
  166.     return true
  167. end
  168. local function main()
  169.     while true do
  170.         if currSlotHasItems() then
  171.             turtle.select(currSlot)
  172.             if turtle.place() then -- checks if item is gravel (flint cant be placed)
  173.                 turtle.dig()
  174.             else
  175.                 incrementSlot()
  176.             end
  177.         else
  178.             clearScreen()
  179.             print("Converted all slots")
  180.             break
  181.         end
  182.     end
  183. end
  184. local function updateTime()
  185.     while true do
  186.         drawTime(sizeX - 5,  sizeY)
  187.         pcall(sleep, 1)
  188.     end
  189. end
  190. function progEntry()
  191.     if turtle then
  192.         if setup() then
  193.             clearScreen()
  194.             print("Converting gravel into flint")
  195.                 main()
  196.         end
  197.     else
  198.         clearScreen()
  199.         print("Sorry this program can only run on turtles.")
  200.     end
  201.    
  202.     pcall(sleep, 2)
  203.     term.clear()
  204.     term.setCursorPos(1,1)
  205.     print(os.version())
  206. end
  207. if showTime then
  208.     parallel.waitForAny(progEntry, updateTime)
  209. else
  210.     progEntry()
  211. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement