Advertisement
billysback

billy

Aug 21st, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.47 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. --[[
  467.     gets all of the lines out of a file
  468.         dir - the directory of the file
  469.     returns:
  470.         a table containing all the lines, in format:
  471.             table[i] = line
  472.         where:
  473.             i = index of the table, a number
  474.             line = the line of the file
  475. ]]
  476. IO.getLines = function(dir)
  477.     local lines = {}
  478.     if fs.exists(dir) then
  479.         local file = fs.open(dir, "r")
  480.         local line = file.readLine()
  481.         while line ~= nil do
  482.             lines[#lines + 1] = line
  483.             line = file.readLine()
  484.         end
  485.         file.close()
  486.     end
  487.     return lines
  488. end
  489.  
  490. --[[
  491.     re-writes a file (or creates a new one) with the lines in it
  492.         dir - the directory of the file
  493.         lines - a table containing all the lines to write in the format:
  494.             table[i] = line
  495.         where:
  496.             i = index of the table, a number
  497.             line = the line of the file
  498.     no returns.
  499. ]]
  500. IO.writeLines = function(dir, lines)
  501.     local file = fs.open("w")
  502.     for i=1,#lines do
  503.         local line = lines[i]
  504.         file.writeLine(line)
  505.     end
  506.     file.close()
  507. end
  508.  
  509. --[[
  510.     adds lines to a file at certain index, adds them before the line at that index
  511.         dir - the directory of the file
  512.         lines - a table containing all of the lines and their indexes, in the format:
  513.             table[i] = {index, line}
  514.         where:
  515.             i = index of the table, a number
  516.             index = index of the file to put the line in
  517.             line = line to add to the file
  518.     no returns.
  519. ]]
  520. IO.addLines = function(dir, lines)
  521.     local lns = billy.IO.getLines(dir)
  522.     local nlines = {}
  523.     for i=1,#lns do
  524.         local line = lines[i]
  525.         for i=1,#lines do
  526.             if lines[i][1] == i then nlines[#nlines + 1] = lines[i][2] end
  527.         end
  528.         nlines[#nlines + 1] = line
  529.     end
  530.     billy.IO.writeLines(nlines)
  531. end
  532.  
  533. billy.IO = IO
  534.  
  535. local image = {}
  536.  
  537. --[[
  538.     creates a table in image-table format from a file
  539.         dir - the directory of the file
  540.     returns:
  541.         a table containing all of the characters in the file in the following format:
  542.             table[x][y] = character
  543.         where:
  544.             x = the x position of the character
  545.             y = the y position of the character
  546.             character = the character
  547.         the table also contains another table called "data", this contains significant data
  548.         about the images and formating of the image-table.
  549. ]]
  550. image.createImageTable = function(dir)
  551.     local lines = billy.IO.getLines(file)
  552.     local imgtab = {}
  553.     for y=1,#lines do
  554.         local line = lines[y]
  555.         if line:sub(1, 5) ~= "DAT::" then
  556.             for x=1,string.len(line) do
  557.                 local char = line:sub(x, y)
  558.                 if imgtab[x] == nil then imgtab[x] = {} end
  559.                 imgtab[x][y] = char
  560.             end
  561.         else
  562.             imgtab.data[#imgtab.data] = billy.misc.split(string.lower(line:sub(6, -1)), "::")
  563.         end
  564.     end
  565.     return imgtab
  566. end
  567.  
  568. --[[
  569.     creates an image table out of a imgtab table using the appropriate name
  570.         imgtab - the image-table
  571.         name - the name of the image in the image-table
  572.     returns:
  573.         a table containing all of the characters in the image in the following format:
  574.             table[x][y] = character
  575.         where:
  576.             x = the x position of the character
  577.             y = the y position of the character
  578.             character = the character
  579.         the table also contains the following values:
  580.             table.x = the x coordinate of the image
  581.             table.y = the y coordinate of the image
  582.             table.width = the width of the image
  583.             table.height = the height of the image
  584.     NOTE:
  585.         if you want to get the true coordinate of one of the image coordinates it can be done via:
  586.             c+image.c-1
  587.         where:
  588.             c = the x or y coordinate inside the image from 1 to width
  589.             image = the image table
  590.             image.c = image.x/y depending on what c is
  591.         the -1 must be there as the inner-image co-ords start at 1, not 0.
  592. ]]
  593. image.getImage = function(imgtab, name)
  594.     local data = imgtab.data
  595.     for i=1,#data do
  596.         local tab = data[i]
  597.         if tab[1] == "image" then
  598.             if tab[2] == name then
  599.                 local x1 = tonumber(tab[3])
  600.                 local y1 = tonumber(tab[4])
  601.                 local x2 = tonumber(tab[5])
  602.                 local y2 = tonumber(tab[6])
  603.                 local img = {}
  604.                 img.x = x1
  605.                 img.y = y1
  606.                 img.width = x2-x1+1
  607.                 img.height = y2-y1+1
  608.                 for x=x1,x2 do
  609.                     for y=y1,y2 do
  610.                         if imgtab[x] ~= nil then
  611.                             local char = imgtab[x][y]
  612.                             if char ~= nil then
  613.                                 img[x-x1+1][y-y1+1] = char
  614.                             end
  615.                         end
  616.                     end
  617.                 end
  618.                 return img
  619.             end
  620.         end
  621.     end
  622.     return nil
  623. end
  624.  
  625. billy.image = image
  626.  
  627. local bufferAPI = {}
  628. bufferAPI.curCols = {} --almost useless, saves presents of "colour" combinations when they are used - character, background and text colour.
  629.  
  630. bufferAPI.buffer_details = {1, 1, colors.black, colors.white, nil}
  631. local sw,sh = term.getSize()
  632.  
  633. bufferAPI.getColorTab = function(char, background, text)
  634.         if billy.bufferAPI.curCols[char] ~= nil then
  635.                 if billy.bufferAPI.curCols[char][background] ~= nil then
  636.                         if billy.bufferAPI.curCols[char][background][text] ~= nil then
  637.                                 return billy.bufferAPI.curCols[char][background][text]
  638.                         end
  639.                 end
  640.         end
  641.         return nil
  642. end
  643.  
  644. bufferAPI.addColorTab = function(col)
  645.         local char = col.char
  646.         local background = col.background
  647.         local text = col.text
  648.         if billy.bufferAPI.getColorTab(char, background, text) == nil then
  649.                 if billy.bufferAPI.curCols[char] == nil then billy.bufferAPI.curCols[char] = {} end
  650.                 if billy.bufferAPI.curCols[char][background] == nil then billy.bufferAPI.curCols[char][background] = {} end
  651.                 billy.bufferAPI.curCols[char][background][text] = col
  652.                 return true
  653.         end
  654.         return false
  655. end
  656.  
  657. bufferAPI.createColor = function(char, background, text)
  658.         --sleep(0)
  659.         local col = billy.bufferAPI.getColorTab(char, background, text)
  660.         if col == nil then
  661.                 col = {}
  662.                 col.char = char
  663.                 col.background = background
  664.                 col.text = text
  665.                 billy.bufferAPI.addColorTab(col)
  666.         end
  667.         return col
  668. end
  669.  
  670. bufferAPI.createPixel = function(x, y, col)
  671.         local pixel = {}
  672.         pixel.x = x
  673.         pixel.y = y
  674.         pixel.col = col
  675.         pixel.compare = function(self, col)
  676.                 return self.col == col
  677.         end
  678.         return pixel
  679. end
  680.  
  681.  
  682. bufferAPI.createBuffer = function()
  683.         local buffer = {}
  684.         buffer.width = sw
  685.         buffer.height = sh
  686.         buffer.changes = {}
  687.         buffer.changed = {}
  688.         buffer.term = term
  689.         buffer.ps = 0
  690.        
  691.         billy.bufferAPI.buffer_details[5] = buffer
  692.         for x=1,sw do
  693.                 buffer[x] = {}
  694.                 buffer.changes[x] = {}
  695.                 for y=1,sh do
  696.                         --print("x="..x..", y="..y)
  697.                         buffer[x][y] = billy.bufferAPI.createPixel(x, y, billy.bufferAPI.createColor(" ", colors.black, colors.white))
  698.                         buffer.changes[x][y] = false
  699.                 end
  700.         end
  701.        
  702.         buffer.get = function(self, x, y)
  703.                 if self[x] ~= nil then
  704.                         if self[x][y] ~= nil then
  705.                                 return self[x][y]
  706.                         end
  707.                 end
  708.                 return nil
  709.         end
  710.        
  711.         buffer.check = function(self, x, y)
  712.                 if self.changes[x] ~= nil then
  713.                         if self.changes[x][y] ~= nil then
  714.                                 return self.changes[x][y]
  715.                         end
  716.                 end
  717.                 return false
  718.         end
  719.        
  720.         buffer.set = function(self, x, y, col)
  721.                 local pixel = self:get(x,y)
  722.                 if pixel ~= nil then
  723.                         if not pixel:compare(col) then
  724.                                 pixel.col = col
  725.                                 if not self:check(x,y) then
  726.                                         self.changed[#self.changed + 1] = {x,y}
  727.                                         if self.changes[x] == nil then self.changes[x] = {} end
  728.                                         self.changes[x][y] = true
  729.                                 end
  730.                         end
  731.                 end
  732.         end
  733.        
  734.         buffer.render = function(self)
  735.                 for i=1,#self.changed do
  736.                         local pos = self.changed[i]
  737.                         self.changes[pos[1]][pos[2]] = false
  738.                         local pixel = self[pos[1]][pos[2]]
  739.                         self.term.setCursorPos(pixel.x, pixel.y)
  740.                         self.term.setBackgroundColor(pixel.col.background)
  741.                         self.term.setTextColor(pixel.col.text)
  742.                         self.term.write(pixel.col.char)
  743.                 end
  744.                 --term.setCursorPos(1, 13)
  745.                 --term.setBackgroundColor(colors.black)
  746.                 --term.setTextColor(colors.white)
  747.                 --print(#self.changed.." pixels changed")
  748.                 self.ps = #self.changed
  749.                 self.changed = {}
  750.         end
  751.        
  752.         buffer.setBuffer = function(self)
  753.                 buffer_detials[5] = self
  754.         end
  755.        
  756.         buffer.setCursorPos = function(x, y)
  757.                 billy.bufferAPI.buffer_details[1] = x
  758.                 billy.bufferAPI.buffer_details[2] = y
  759.         end    
  760.        
  761.         buffer.setBackgroundColor = function(col)
  762.                 billy.bufferAPI.buffer_details[3] = col
  763.         end
  764.        
  765.         buffer.setTextColor = function(col)
  766.                 billy.bufferAPI.buffer_details[4] = col
  767.         end
  768.        
  769.         buffer.getSize = function()
  770.                 local buffer = billy.bufferAPI.buffer_details[5]
  771.                 if buffer == nil then print("NIL_BUFFER") return 1,1 else
  772.                     return billy.bufferAPI.buffer_details[5].width, billy.bufferAPI.buffer_details[5].height
  773.                 end
  774.         end
  775.        
  776.         buffer.writeChar = function(char, ox, oy)
  777.                 if ox == nil then ox = 0 end
  778.                 if oy == nil then oy = 0 end
  779.                 char = char:sub(1,1)
  780.                 local col = billy.bufferAPI.createColor(char, billy.bufferAPI.buffer_details[3], billy.bufferAPI.buffer_details[4])
  781.                 local buffer = billy.bufferAPI.buffer_details[5]
  782.                 if buffer ~= nil then
  783.                         buffer:set(billy.bufferAPI.buffer_details[1]+ox, billy.bufferAPI.buffer_details[2]+oy, col)
  784.                 end
  785.         end
  786.        
  787.         buffer.write = function(str)
  788.             for i=1,string.len(str) do
  789.                 billy.buffer.writeChar(str:sub(i,i), i-1, 0)
  790.             end
  791.         end
  792.        
  793.         buffer.clear = function()
  794.                 billy.bufferAPI.buffer_details[5]:reset()
  795.         end
  796.        
  797.         buffer.reset = function(self)
  798.                 local buffer = self
  799.                 for x=1,self.width do
  800.                         buffer[x] = {}
  801.                         buffer.changes[x] = {}
  802.                         for y=1,sh do
  803.                                 --print("x="..x..", y="..y)
  804.                                 local pixel = billy.bufferAPI.createPixel(x, y, billy.bufferAPI.createColor(" ", billy.bufferAPI.buffer_details[3], colors.white))
  805.                                 self.term.setCursorPos(x, y)
  806.                                 self.term.setBackgroundColor(pixel.col.background)
  807.                                 self.term.setTextColor(pixel.col.text)
  808.                                 self.term.write(pixel.col.char)
  809.                                 buffer[x][y] = pixel
  810.                                
  811.                                 buffer.changes[x][y] = false
  812.                         end
  813.                 end
  814.                 buffer.changes = {}
  815.                 buffer.changed = {}
  816.         end
  817.        
  818.         return buffer
  819. end
  820.  
  821. billy.bufferAPI = bufferAPI
  822. billy.buffer = billy.bufferAPI.createBuffer()
  823.  
  824. local args = {...}
  825. if #args > 0 then
  826.    
  827.     shell.run(unpack(args))
  828.     print(billy.load())
  829.  
  830.     local pause = 0.05
  831.     local on = true
  832.     local timer = os.startTimer(pause)
  833.     while on do
  834.         local event,p1,p2,p3 = os.pullEvent()
  835.         if event == "timer" and p1 == timer then
  836.             billy.time = billy.time + 1
  837.             billy.update()
  838.             if billy.draw() then
  839.                 billy.buffer:render()
  840.             end
  841.         elseif event == "key" and billy.keys.used[p1] == true then
  842.             if billy.keys.quit == p1 then
  843.                 billy.quit()
  844.                 on = false
  845.             end
  846.         else
  847.             billy.event(event, p1, p2, p3)
  848.         end
  849.         if on then
  850.             timer = os.startTimer(pause)
  851.         end
  852.     end
  853. else
  854.     print("Error: No program entered")
  855. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement