billysback

billy API

Aug 20th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.17 KB | None | 0 0
  1. billy = {}
  2. billy.time = 0
  3.  
  4. billy.keys = {}
  5. billy.keys.used = {}
  6. billy.keys.used[29] = true
  7. billy.keys.quit = 29
  8.  
  9. --[[
  10.     miscellaneous functions and values.
  11. ]]
  12. local misc = {}
  13. misc.flipTable = function(table)
  14.     local ntable = {}
  15.     for i=1,#table do
  16.         ntable[i] = table[#table - i + 1]
  17.     end
  18.     return ntable
  19. end
  20. misc.split = function(str, pat)
  21.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  22.     if str ~= nil then
  23.        local fpat = "(.-)" .. pat
  24.        local last_end = 1
  25.        local s, e, cap = str:find(fpat, 1)
  26.        while s do
  27.           if s ~= 1 or cap ~= "" then
  28.          table.insert(t,cap)
  29.           end
  30.           last_end = e+1
  31.           s, e, cap = str:find(fpat, last_end)
  32.        end
  33.        if last_end <= #str then
  34.           cap = str:sub(last_end)
  35.           table.insert(t, cap)
  36.        end
  37.     else
  38.         print("##ERROR failed to split ["..str.."] by:"..pat)
  39.     end
  40.     return t
  41. end
  42. billy.misc = misc
  43.  
  44. --[[
  45.     Dump table;
  46.     not used inside billy - basically just an alternative to using the _G table if you don't want
  47.     to clutter in it, as cluttering the _G table is more of a problem in CC than it is in LOVE
  48. ]]
  49. billy.dump = {}
  50.  
  51. --[[
  52.     All the standard override functions,
  53.     override these and they get called at the appropriate time.
  54. ]]
  55. billy.load = function() end --called when the program starts
  56. billy.update = function() end --called every 0.05 seconds
  57. billy.draw = function() return false end --called every time the game is drawn, after update, every 0.05 seconds, return true/false depending on if changes have/haven't been made
  58. billy.event = function() end --called every time an event is called
  59. billy.quit = function() end --called when the game is quit using the quit button
  60.  
  61. --[[
  62.     graphics table for billy;
  63.     contains all the graphics functions
  64. ]]
  65. local graphics = {}
  66.  
  67. --[[
  68.     data to be set in graphics functions for the graphics functions,
  69.     It's safer to ignore this table, but you don't have to.
  70. ]]
  71. graphics.LOCAL_DATA = {}
  72. graphics.LOCAL_DATA.background = colors.black
  73. graphics.LOCAL_DATA.text = colors.white
  74. graphics.LOCAL_DATA.size = 1
  75. graphics.LOCAL_DATA.char = " "
  76.  
  77. --[[
  78.     Function to set the background colour in LOCAL_DATA, can be called by both:
  79.         setBackgroundColor(colour)
  80.         setBackgroundColour(colour)
  81.     returns:
  82.         old, colour
  83.     where:
  84.         old is the old background colour that was used
  85.         colour is the new background colour being used
  86. ]]
  87. graphics.setBackgroundColor = function(color)
  88.     local old = graphics.LOCAL_DATA.background
  89.     graphics.LOCAL_DATA.background = color
  90.     return old, color
  91. end
  92. graphics.setBackgroundColour = function(color) return graphics.setBackgroundColor(color) end
  93.  
  94. --[[
  95.     Function to set the text colour in LOCAL_DATA, can be called by both:
  96.         setTextColor(colour)
  97.         setTextColour(colour)
  98.     returns:
  99.         old, colour
  100.     where:
  101.         old is the old text colour that was used
  102.         colour is the new text colour being used
  103. ]]
  104. graphics.setTextColor = function(color)
  105.     local old = graphics.LOCAL_DATA.background
  106.     graphics.LOCAL_DATA.text = color
  107.     return old, color
  108. end
  109. graphics.setTextColour = function(color) return graphics.setTextColor(color) end
  110.  
  111. --[[
  112.     Function to set the size value in LOCAL_DATA,
  113.     returns:
  114.         old, size
  115.     where:
  116.         old is the old size value that was used
  117.         size is the new size value being used
  118. ]]
  119. graphics.setSize = function(size)
  120.     local old = graphics.LOCAL_DATA.size
  121.     graphics.LOCAL_DATA.size = size
  122.     return old, size
  123. end
  124.  
  125. --[[
  126.     Function to set the char value in LOCAL_DATA,
  127.     returns:
  128.         old, char
  129.     where:
  130.         old is the old char value that was used
  131.         char is the new char value being used
  132. ]]
  133. graphics.setChar = function(char)
  134.     local old = graphics.LOCAL_DATA.char
  135.     graphics.LOCAL_DATA.char = char
  136.     return old, char
  137. end
  138.  
  139. --[[
  140.     Function to draw a line between to points;
  141.         x1,y1 are the x,y co-ordinates of the first point on the line
  142.         x2,y2 are the x,y co-ordinates of the last point on the line
  143.     returns:
  144.         a table containing all positions of the line in the format:
  145.             table[i] = {x, y}
  146.         where:
  147.             i = index of table, a number
  148.             x,y are the x,y positions of the point on the line
  149.     note:
  150.         the table returned always has the first point as x1,y2 and the last point as x2,y2;
  151.         so it keeps it's order.
  152. ]]
  153. graphics.drawLine = function(x1, y1, x2, y2)
  154.     local pixs = {}
  155.    
  156.     x1 = math.floor(x1)
  157.     y1 = math.floor(y1)
  158.     x2 = math.floor(x2)
  159.     y2 = math.floor(y2)
  160.    
  161.     if x1 == x2 and y1 == y2 then
  162.         pixs[#pixs + 1] = {x1, y1}
  163.         return pixs
  164.     end
  165.    
  166.     local minX = math.min( x1, x2 )
  167.     if minX == x1 then
  168.             minY = y1
  169.             maxX = x2
  170.             maxY = y2
  171.     else
  172.             minY = y2
  173.             maxX = x1
  174.             maxY = y1
  175.     end
  176.            
  177.     local xDiff = maxX - minX
  178.     local yDiff = maxY - minY
  179.                    
  180.     if xDiff > math.abs(yDiff) then
  181.             local y = minY
  182.             local dy = yDiff / xDiff
  183.             for x=minX,maxX do
  184.                     if #pixs > 0 then
  185.                         if pixs[#pixs][1] == x and pixs[#pixs][2] == math.floor(y + 0.5) then else
  186.                             pixs[#pixs + 1] = {x, math.floor(y + 0.5)}
  187.                         end
  188.                     else
  189.                         pixs[#pixs + 1] = {x, math.floor(y + 0.5)}
  190.                     end
  191.                     y = y + dy
  192.             end
  193.     else
  194.             local x = minX
  195.             local dx = xDiff / yDiff
  196.             if maxY >= minY then
  197.                     for y=minY,maxY do
  198.                             if #pixs > 0 then
  199.                                 if pixs[#pixs][1] == math.floor(x+0.5) and pixs[#pixs][2] == y then else
  200.                                     pixs[#pixs + 1] = {math.floor(x+0.5), y}
  201.                                 end
  202.                             else
  203.                                 pixs[#pixs + 1] = {math.floor(x+0.5), y}
  204.                             end
  205.                             x = x + dx
  206.                     end
  207.             else
  208.                     for y=minY,maxY,-1 do
  209.                             if #pixs > 0 then
  210.                                 if pixs[#pixs][1] == math.floor(x+0.5) and pixs[#pixs][2] == y then else
  211.                                     pixs[#pixs + 1] = {math.floor(x+0.5), y}
  212.                                 end
  213.                             else
  214.                                 pixs[#pixs + 1] = {math.floor(x+0.5), y}
  215.                             end
  216.                             x = x - dx
  217.                     end
  218.             end
  219.     end
  220.     if pixs[1][1] == x2 and pixs[1][2] == y2 then pixs = billy.misc.flipTable(pixs) end
  221.     return pixs
  222. end
  223.  
  224. --[[
  225.     Creates a polygon,
  226.         poly is a table in the format:
  227.             table[i] = {x,y}
  228.         where:
  229.             i = index of table, a number
  230.             x,y = x,y co-ordinates of one of the corners of the polygon
  231.     returns:
  232.         shape
  233.     where:
  234.         shape is a table containing the following values and functions:
  235.             shape.det = a string containing all the points of the corners of the polygon on it
  236.             shape.poly = the table you used to create the shape
  237.             shape.back = background colour of the shape
  238.             shape.text = text colour of the shape
  239.             shape.char = character used to draw the shape
  240.             shape:draw = function that draws the polygon, no parameters or returns.
  241. ]]
  242. graphics.createShape = function(poly)
  243.     local shape = {}
  244.     local l = ""
  245.     for i=1,#poly do
  246.         local p1 = poly[i]
  247.         local p2 = poly[i+1]
  248.         if i == #poly then p2 = poly[1] end
  249.         local line = drawLine(p1[1], p1[2], p2[1], p2[2])
  250.         for j=1,#line do
  251.             shape[#shape + 1] = line[j]
  252.         end
  253.         l = l..p1[1]..","..p1[2].." "
  254.     end
  255.     shape.det = l
  256.     shape.poly = poly
  257.     shape.back = billy.graphics.LOCAL_DATA.background
  258.     shape.text = billy.graphics.LOCAL_DATA.text
  259.     shape.char = billy.graphics.LOCAL_DATA.char
  260.     shape.draw = function(self)
  261.         for i=1,#self do
  262.             local p = self[i]
  263.             billy.buffer.setCursorPos(p[1], p[2])
  264.             billy.buffer.setBackgroundColor(self.back)
  265.             billy.buffer.setTextColor(self.text)
  266.             billy.buffer.write(self.char)
  267.         end
  268.         --[[
  269.         for i=1,#self.poly do
  270.             local p = self.poly[i]
  271.             billy.buffer.setCursorPos(p[1], p[2])
  272.             billy.buffer.setBackgroundColor(self.back)
  273.             billy.buffer.setTextColor(self.text)
  274.             billy.buffer.write(tostring(i))
  275.         end
  276.         ]]
  277.         --billy.buffer.setCursorPos(1, sh-1)
  278.         --billy.buffer.write(self.det)
  279.     end
  280.     return shape
  281. end
  282.  
  283. --[[
  284.     Function that draws a shape with the current LOCAL_DATA,
  285.     exactly the same as shape:draw but uses LOCAL_DATA for colours instead of the shape's colours.
  286.    
  287.     can be used to draw Triangles and Squares (Filled in ones)
  288. ]]
  289. graphics.drawShape = function(shape)
  290.     for i=1,#shape do
  291.         local p = shape[i]
  292.         billy.buffer.setCursorPos(p[1], p[2])
  293.         billy.buffer.setBackgroundColor(graphics.LOCAL_DATA.background)
  294.         billy.buffer.setTextColor(graphics.LOCAL_DATA.text)
  295.         billy.buffer.write(graphics.LOCAL_DATA.char)
  296.     end
  297. end
  298.  
  299. --[[
  300.     Creates a Triangle, for use with fillTriangle
  301.         p1,p2,p3 - the 3 co-ordinates of the triangle
  302.     returns:
  303.         table in format:
  304.             table[x][y] = true 
  305. ]]
  306. graphics.createTriangle = function(p1, p2, p3)
  307.     local tab1 = billy.graphics.drawLine(p1[1], p1[2], p2[1], p2[2])
  308.     local tab2 = billy.graphics.drawLine(p1[1], p1[2], p3[1], p3[2])
  309.     local tab3 = billy.graphics.drawLine(p2[1], p2[2], p3[1], p3[2])
  310.     local tabs = {tab1, tab2, tab3}
  311.     local triangle = {}
  312.     for i=1,#tabs do
  313.         local tab = tabs[i]
  314.         for i=1,#tab do
  315.             local pos = tab[i]
  316.             if triangle[x] == nil then triangle[x] = {} end
  317.             triangle[pos[1]][pos[2]] = true
  318.         end
  319.     end
  320.     return triangle
  321. end
  322.  
  323. --[[
  324.     Fills in a triangle, must be a triangle returned from createTriangle, NOT createShape
  325.         tri - a triangle returned from createTriangle
  326.     returns:
  327.         table containing all the points in the triangle in format:
  328.             table[i] = {x,y}
  329.         where:
  330.             i = index of table, a number
  331.             x,y = the x,y coordinates of the point on the triangle
  332. ]]
  333. graphics.fillTriangle = function(tri)
  334.     local minX, maxX = nil
  335.     local minys = {}
  336.     for x,v in pairs(tri) do
  337.         if minX == nil then
  338.             minX = x
  339.         elseif x < minX then
  340.             minX = x
  341.         end
  342.        
  343.         if maxX == nil then
  344.             maxX = x
  345.         elseif x > maxX then
  346.             maxX = x
  347.         end
  348.        
  349.         if v ~= nil then
  350.             local minY = nil
  351.             for y,r in pairs(v) do
  352.                 if minY == nil then
  353.                     minY = y
  354.                 elseif y < minY then
  355.                     minY = y
  356.                 end
  357.             end
  358.             if minY ~= nil then
  359.                 if minys[x] == nil then minys[x] = {} end
  360.                 minys[x] = minY
  361.             end
  362.         end
  363.     end
  364.    
  365.     local ntri = {}
  366.     for x,v in pairs(tri) do
  367.         if v ~= nil then
  368.             for y,r in pairs(v) do
  369.                 ntri[#ntri + 1] = {x,y}
  370.             end
  371.         end
  372.     end
  373.     if minX ~= nil and maxX ~= nil then
  374.         for x=minX, maxX do
  375.             if minys[x] ~= nil then
  376.                 local y = minys[x]
  377.                 if tri[x] ~= nil then
  378.                     if getSize(tri[x]) > 1 then
  379.                         local check = false
  380.                         while check == false do
  381.                             y = y + 1
  382.                            
  383.                             if tri[x] ~= nil then
  384.                                 if tri[x][y] ~= nil then check = true end
  385.                             end
  386.                             if y >= h then check = true end
  387.                            
  388.                             --term.setCursorPos(1, h-2)
  389.                             --print("y = "..y.."; x = "..x)
  390.                            
  391.                             --if check then
  392.                                 ntri[#ntri + 1] = {x,y}
  393.                             --end
  394.                         end
  395.                     end
  396.                 end
  397.             end
  398.         end
  399.     end
  400.     return ntri
  401. end
  402.  
  403. --[[
  404.     function that creates a quadrilateral,
  405.         p1,p2,p3,p4 = the 4 points of the quadrilateral
  406.     returns:
  407.         table containing all the points of the square in format:
  408.             table[i] = {x,y}
  409.         where:
  410.             i = index of table, a number
  411.             x,y = the x,y coordinates of the point on the square
  412. ]]
  413. graphics.fillQuad = function(p1, p2, p3, p4)
  414.     local tri1 = billy.graphics.fillTriangle(getTriangle(p1, p2, p3))
  415.     local tri2 = billy.graphics.fillTriangle(getTriangle(p1, p4, p3))
  416.     local tris = {tri1, tri2}
  417.     local square = {}
  418.     for i=1,#tris do
  419.         local tri = tris[i]
  420.         for i=1,#tri do
  421.             local pos = tri[i]
  422.             local x = pos[1]
  423.             local y = pos[2]
  424.             square[#square + 1] = {x,y}
  425.         end
  426.     end
  427.     return square
  428. end
  429.  
  430. --[[
  431.     Creates a circle;
  432.         center = table containing {x,y} coordinates of the centre point
  433.         radius = the radius of the circle
  434.         fill = boolean as to whether or not the circle is filled in
  435.     returns:
  436.         table containing all the points in the circle on it, format:
  437.             table[i] = {x,y}
  438.         where:
  439.             i = index of table, a number
  440.             x,y = the x,y coordinates of the point
  441. ]]
  442. graphics.createCircle = function(center, radius, fill)
  443.         -- (x-a)^2 + (y-B)^2 = r^2 (formula, where r is radius, x and y are coordinates and a and b is center)
  444.         local pixs = {}
  445.         local rads = radius^2
  446.         local topright = {center[1]-radius, center[2]-radius}
  447.         local bottomleft = {center[1]+radius, center[2]+radius}
  448.         for cx=topright[1],bottomleft[1],1 do
  449.                 for cy=topright[2],bottomleft[2],1 do
  450.                         local xb = (cx-center[1])^2
  451.                         local yb = (cy-center[2])^2
  452.                         if (xb+yb) == rads then
  453.                                 pixs[#pixs + 1] = {cx, cy}
  454.                         elseif fill and (xb+yb) <= rads then
  455.                                 pixs[#pixs + 1] = {cx, cy}
  456.                         end
  457.                 end
  458.         end
  459.         return pixs
  460. end
  461.  
  462. billy.graphics = graphics
  463.  
  464. local IO = {}
  465. --[[
  466.     gets all of the lines out of a file
  467.         dir - the directory of the file
  468.     returns:
  469.         a table containing all the lines, in format:
  470.             table[i] = line
  471.         where:
  472.             i = index of the table, a number
  473.             line = the line of the file
  474. ]]
  475. IO.getLines = function(dir)
  476.     local lines = {}
  477.     if fs.exists(dir) then
  478.         local file = fs.open(dir, "r")
  479.         local line = file.readLine()
  480.         while line ~= nil do
  481.             lines[#lines + 1] = line
  482.             line = file.readLine()
  483.         end
  484.         file.close()
  485.     end
  486.     return lines
  487. end
  488. --[[
  489.     re-writes a file (or creates a new one) with the lines in it
  490.         dir - the directory of the file
  491.         lines - a table containing all the lines to write in the format:
  492.             table[i] = line
  493.         where:
  494.             i = index of the table, a number
  495.             line = the line of the file
  496.     no returns.
  497. ]]
  498. IO.writeLines = function(dir, lines)
  499.     local file = fs.open("w")
  500.     for i=1,#lines do
  501.         local line = lines[i]
  502.         file.writeLine(line)
  503.     end
  504.     file.close()
  505. end
  506. --[[
  507.     adds lines to a file at certain index, adds them before the line at that index
  508.         dir - the directory of the file
  509.         lines - a table containing all of the lines and their indexes, in the format:
  510.             table[i] = {index, line}
  511.         where:
  512.             i = index of the table, a number
  513.             index = index of the file to put the line in
  514.             line = line to add to the file
  515.     no returns.
  516. ]]
  517. IO.addLines = function(dir, lines)
  518.     local lns = billy.IO.getLines(dir)
  519.     local nlines = {}
  520.     for i=1,#lns do
  521.         local line = lines[i]
  522.         for i=1,#lines do
  523.             if lines[i][1] == i then nlines[#nlines + 1] = lines[i][2] end
  524.         end
  525.         nlines[#nlines + 1] = line
  526.     end
  527.     billy.IO.writeLines(nlines)
  528. end
  529.  
  530. billy.IO = IO
  531.  
  532. local image = {}
  533. --[[
  534.     creates a table in image-table format from a file
  535.         dir - the directory of the file
  536.     returns:
  537.         a table containing all of the characters in the file in the following format:
  538.             table[x][y] = character
  539.         where:
  540.             x = the x position of the character
  541.             y = the y position of the character
  542.             character = the character
  543.         the table also contains another table called "data", this contains significant data
  544.         about the images and formating of the image-table.
  545. ]]
  546. image.createImageTable = function(dir)
  547.     local lines = billy.IO.getLines(file)
  548.     local imgtab = {}
  549.     for y=1,#lines do
  550.         local line = lines[y]
  551.         if line:sub(1, 5) ~= "DAT::" then
  552.             for x=1,string.len(line) do
  553.                 local char = line:sub(x, y)
  554.                 if imgtab[x] == nil then imgtab[x] = {} end
  555.                 imgtab[x][y] = char
  556.             end
  557.         else
  558.             imgtab.data[#imgtab.data] = billy.misc.split(string.lower(line:sub(6, -1)), "::")
  559.         end
  560.     end
  561.     return imgtab
  562. end
  563. --[[
  564.     creates an image table out of a imgtab table using the appropriate name
  565.         imgtab - the image-table
  566.         name - the name of the image in the image-table
  567.     returns:
  568.         a table containing all of the characters in the image in the following format:
  569.             table[x][y] = character
  570.         where:
  571.             x = the x position of the character
  572.             y = the y position of the character
  573.             character = the character
  574.         the table also contains the following values:
  575.             table.x = the x coordinate of the image
  576.             table.y = the y coordinate of the image
  577.             table.width = the width of the image
  578.             table.height = the height of the image
  579.     NOTE:
  580.         if you want to get the true coordinate of one of the image coordinates it can be done via:
  581.             c+image.c-1
  582.         where:
  583.             c = the x or y coordinate inside the image from 1 to width
  584.             image = the image table
  585.             image.c = image.x/y depending on what c is
  586.         the -1 must be there as the inner-image co-ords start at 1, not 0.
  587. ]]
  588. image.getImage = function(imgtab, name)
  589.     local data = imgtab.data
  590.     for i=1,#data do
  591.         local tab = data[i]
  592.         if tab[1] == "image" then
  593.             if tab[2] == name then
  594.                 local x1 = tonumber(tab[3])
  595.                 local y1 = tonumber(tab[4])
  596.                 local x2 = tonumber(tab[5])
  597.                 local y2 = tonumber(tab[6])
  598.                 local img = {}
  599.                 img.x = x1
  600.                 img.y = y1
  601.                 img.width = x2-x1+1
  602.                 img.height = y2-y1+1
  603.                 for x=x1,x2 do
  604.                     for y=y1,y2 do
  605.                         if imgtab[x] ~= nil then
  606.                             local char = imgtab[x][y]
  607.                             if char ~= nil then
  608.                                 img[x-x1+1][y-y1+1] = char
  609.                             end
  610.                         end
  611.                     end
  612.                 end
  613.                 return img
  614.             end
  615.         end
  616.     end
  617.     return nil
  618. end
  619.  
  620. billy.image = image
  621.  
  622. local bufferAPI = {}
  623. bufferAPI.curCols = {} --almost useless, saves presents of "colour" combinations when they are used - character, background and text colour.
  624.  
  625. bufferAPI.buffer_details = {1, 1, colors.black, colors.white, nil}
  626. local sw,sh = term.getSize()
  627.  
  628. bufferAPI.getColorTab = function(char, background, text)
  629.         if billy.bufferAPI.curCols[char] ~= nil then
  630.                 if billy.bufferAPI.curCols[char][background] ~= nil then
  631.                         if billy.bufferAPI.curCols[char][background][text] ~= nil then
  632.                                 return billy.bufferAPI.curCols[char][background][text]
  633.                         end
  634.                 end
  635.         end
  636.         return nil
  637. end
  638.  
  639. bufferAPI.addColorTab = function(col)
  640.         local char = col.char
  641.         local background = col.background
  642.         local text = col.text
  643.         if billy.bufferAPI.getColorTab(char, background, text) == nil then
  644.                 if billy.bufferAPI.curCols[char] == nil then billy.bufferAPI.curCols[char] = {} end
  645.                 if billy.bufferAPI.curCols[char][background] == nil then billy.bufferAPI.curCols[char][background] = {} end
  646.                 billy.bufferAPI.curCols[char][background][text] = col
  647.                 return true
  648.         end
  649.         return false
  650. end
  651.  
  652. bufferAPI.createColor = function(char, background, text)
  653.         --sleep(0)
  654.         local col = billy.bufferAPI.getColorTab(char, background, text)
  655.         if col == nil then
  656.                 col = {}
  657.                 col.char = char
  658.                 col.background = background
  659.                 col.text = text
  660.                 billy.bufferAPI.addColorTab(col)
  661.         end
  662.         return col
  663. end
  664.  
  665. bufferAPI.createPixel = function(x, y, col)
  666.         local pixel = {}
  667.         pixel.x = x
  668.         pixel.y = y
  669.         pixel.col = col
  670.         pixel.compare = function(self, col)
  671.                 return self.col == col
  672.         end
  673.         return pixel
  674. end
  675.  
  676.  
  677. bufferAPI.createBuffer = function()
  678.         local buffer = {}
  679.         buffer.width = sw
  680.         buffer.height = height
  681.         buffer.changes = {}
  682.         buffer.changed = {}
  683.         buffer.term = term
  684.        
  685.         billy.bufferAPI.buffer_details[5] = buffer
  686.         for x=1,sw do
  687.                 buffer[x] = {}
  688.                 buffer.changes[x] = {}
  689.                 for y=1,sh do
  690.                         --print("x="..x..", y="..y)
  691.                         buffer[x][y] = billy.bufferAPI.createPixel(x, y, billy.bufferAPI.createColor(" ", colors.black, colors.white))
  692.                         buffer.changes[x][y] = false
  693.                 end
  694.         end
  695.        
  696.         buffer.get = function(self, x, y)
  697.                 if self[x] ~= nil then
  698.                         if self[x][y] ~= nil then
  699.                                 return self[x][y]
  700.                         end
  701.                 end
  702.                 return nil
  703.         end
  704.        
  705.         buffer.check = function(self, x, y)
  706.                 if self.changes[x] ~= nil then
  707.                         if self.changes[x][y] ~= nil then
  708.                                 return self.changes[x][y]
  709.                         end
  710.                 end
  711.                 return false
  712.         end
  713.        
  714.         buffer.set = function(self, x, y, col)
  715.                 local pixel = self:get(x,y)
  716.                 if pixel ~= nil then
  717.                         if not pixel:compare(col) then
  718.                                 pixel.col = col
  719.                                 if not self:check(x,y) then
  720.                                         self.changed[#self.changed + 1] = {x,y}
  721.                                         if self.changes[x] == nil then self.changes[x] = {} end
  722.                                         self.changes[x][y] = true
  723.                                 end
  724.                         end
  725.                 end
  726.         end
  727.        
  728.         buffer.render = function(self)
  729.                 for i=1,#self.changed do
  730.                         local pos = self.changed[i]
  731.                         self.changes[pos[1]][pos[2]] = false
  732.                         local pixel = self[pos[1]][pos[2]]
  733.                         self.term.setCursorPos(pixel.x, pixel.y)
  734.                         self.term.setBackgroundColor(pixel.col.background)
  735.                         self.term.setTextColor(pixel.col.text)
  736.                         self.term.write(pixel.col.char)
  737.                 end
  738.                 term.setCursorPos(1, 13)
  739.                 term.setBackgroundColor(colors.black)
  740.                 term.setTextColor(colors.white)
  741.                 print(#self.changed.." pixels changed")
  742.                 self.changed = {}
  743.         end
  744.        
  745.         buffer.setBuffer = function(self)
  746.                 buffer_detials[5] = self
  747.         end
  748.        
  749.         buffer.setCursorPos = function(x, y)
  750.                 billy.bufferAPI.buffer_details[1] = x
  751.                 billy.bufferAPI.buffer_details[2] = y
  752.         end    
  753.        
  754.         buffer.setBackgroundColor = function(col)
  755.                 billy.bufferAPI.buffer_details[3] = col
  756.         end
  757.        
  758.         buffer.setTextColor = function(col)
  759.                 billy.bufferAPI.buffer_details[4] = col
  760.         end
  761.        
  762.         buffer.getSize = function()
  763.                 local buffer = billy.bufferAPI.buffer_details[5]
  764.                 if buffer == nil then print("NIL_BUFFER") end
  765.                 return billy.bufferAPI.buffer_details[5].width, billy.bufferAPI.buffer_details[5].height
  766.         end
  767.        
  768.         buffer.write = function(char)
  769.                 char = char:sub(1,1)
  770.                 local col = billy.bufferAPI.createColor(char, billy.bufferAPI.buffer_details[3], billy.bufferAPI.buffer_details[4])
  771.                 local buffer = billy.bufferAPI.buffer_details[5]
  772.                 if buffer ~= nil then
  773.                         buffer:set(billy.bufferAPI.buffer_details[1], billy.bufferAPI.buffer_details[2], col)
  774.                 end
  775.         end
  776.        
  777.         buffer.clear = function()
  778.                 billy.bufferAPI.buffer_details[5]:reset()
  779.         end
  780.        
  781.         buffer.reset = function(self)
  782.                 local buffer = self
  783.                 for x=1,self.width do
  784.                         buffer[x] = {}
  785.                         buffer.changes[x] = {}
  786.                         for y=1,sh do
  787.                                 --print("x="..x..", y="..y)
  788.                                 local pixel = billy.bufferAPI.createPixel(x, y, billy.bufferAPI.createColor(" ", billy.bufferAPI.buffer_details[3], colors.white))
  789.                                 term.setCursorPos(x, y)
  790.                                 term.setBackgroundColor(pixel.col.background)
  791.                                 term.setTextColor(pixel.col.text)
  792.                                 term.write(pixel.col.char)
  793.                                 buffer[x][y] = pixel
  794.                                
  795.                                 buffer.changes[x][y] = false
  796.                         end
  797.                 end
  798.                 buffer.changes = {}
  799.                 buffer.changed = {}
  800.         end
  801.        
  802.         return buffer
  803. end
  804.  
  805. billy.bufferAPI = bufferAPI
  806. billy.buffer = billy.bufferAPI.createBuffer()
  807.  
  808. local args = {...}
  809. if #args > 0 then
  810.    
  811.     shell.run(unpack(args))
  812.     billy.load()
  813.  
  814.     local pause = 0.05
  815.     local on = true
  816.     local timer = os.startTimer(pause)
  817.     while on do
  818.         local event,p1,p2,p3 = os.pullEvent()
  819.         if event == "timer" and p1 == timer then
  820.             billy.time = billy.time + 1
  821.             billy.update()
  822.             if billy.draw() then
  823.                 billy.buffer:render()
  824.             end
  825.         elseif event == "key" and billy.keys.used[p1] == true then
  826.             if billy.keys.quit == p1 then
  827.                 billy.quit()
  828.                 on = false
  829.             end
  830.         else
  831.             billy.event(event, p1, p2, p3)
  832.         end
  833.         if on then
  834.             timer = os.startTimer(pause)
  835.         end
  836.     end
  837. else
  838.     print("Error: No program entered")
  839. end
Advertisement
Add Comment
Please, Sign In to add comment