Advertisement
john9912

frame v3

Jun 18th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.50 KB | None | 0 0
  1. t = {}
  2. c = {}
  3. b = {}
  4. ct = {}
  5. cc = {}
  6. cb = {}
  7. local function tT(x,y)
  8.    x = tonumber(x)
  9.    y = tonumber(y)
  10.    local a = ""
  11.    if setting.format == "large" then
  12.       if x < 10 then
  13.          a = "00"..x
  14.       elseif x < 100 then
  15.          a = "0"..x
  16.       else
  17.          a = x
  18.       end
  19.       if y < 10 then
  20.          a = a.."00"..y
  21.       elseif y < 100 then
  22.          a = a.."0"..y
  23.       else
  24.          a = a..y
  25.       end
  26.    elseif setting.format == "normal" then
  27.       if x < 10 then
  28.          a =  "0"..x
  29.       else
  30.          a = x
  31.       end
  32.       if y < 10 then
  33.          a = a.."0"..y
  34.       else
  35.          a = a..y
  36.       end
  37.    else
  38.       errorLog:write(os.time().." Incorrect format at x:"..x.." y:"..y.."\n")
  39.    end
  40.    return a
  41. end
  42. -- user settings!
  43. setting = {}
  44. setting.useColor = true -- For advanced computers. who uses normal anymore?
  45. setting.format = "normal" --"normal" = xxyy  "large"  = xxxyyy
  46. setting.useGetSize = true -- automate MaxX and MaxY
  47. setting.drawEmpty = true -- So that you can draw to the terminal if false
  48. setting.initialized = false -- Don't touch.
  49. setting.logLocation = "log/"..os.day().."/"..os.time()
  50. setting.defaultBkg = "0"
  51. setting.defaultClr = "f"
  52. MaxX = 51
  53. MaxY = 19
  54. errorLog = fs.open(tostring(setting.logLocation), "w")
  55. errorLog.write("Log started\n")
  56. function sett(x,y,char)
  57.    t[tT(x,y)] = char
  58. end
  59. function setc(x,y,color)
  60.    c[tT(x,y)] = color
  61. end
  62. function setb(x,y,color)
  63.    b[tT(x,y)] = color
  64. end
  65. function setAll(x,y,char,color,bkgclr)
  66.    local a = tT(x,y)
  67.    t[a] = char
  68.    c[a] = color
  69.    b[a] = bkgclr
  70. end
  71. function gett(x,y)
  72.    local a = t[tT(x,y)]
  73.    return a
  74. end
  75. function getc(x,y)
  76.    local a = c[tT(x,y)]
  77.    return a
  78. end
  79. function getb(x,y)
  80.    local a = b[tT(x,y)]
  81.    return a
  82. end
  83. function init()
  84.    if setting.useGetSize == true then
  85.       MaxX, MaxY = term.getSize()
  86.    end
  87.    local Y = 1
  88.    while Y < MaxY+1 do
  89.       local X = 1
  90.       while X < MaxX+1 do
  91.          sett(X,Y," ")
  92.          if setting.useColor == true then
  93.             setc(X,Y,setting.defaultClr)
  94.             setb(X,Y,setting.defaultBkg)
  95.          end
  96.          X = X + 1
  97.       end
  98.       Y = Y + 1
  99.    end
  100.    setting.initialized = true
  101.    return true
  102. end
  103. function exit()
  104.    errorLog.write("Properly closed at "..os.time())
  105.    errorLog:close()
  106.    c = nil
  107.    b = nil
  108.    t = nil
  109.    ct = nil
  110.    cc = nil
  111.    cb = nil
  112. end
  113. function setText(x,y,text)
  114.    text = tostring(text)
  115.    if y > 0 and y < MaxY+1 and x > 0 and x < MaxX+1 and string.len(text) + x < MaxX+1 then
  116.       local a = 0
  117.       repeat
  118.          a = a + 1
  119.          sett(x,y,text:sub(a,a))
  120.          x = x + 1
  121.       until a == string.len(text)
  122.    else
  123.       errorLog.write(tostring(os.time().." setText incorrect input at x:"..x.." y:"..y.."\n"))
  124.    end
  125. end
  126. function setBkg(color)
  127.    if setting.useColor then
  128.       local y = 1
  129.       while y < MaxY + 1 do
  130.          local x = 1
  131.          while x < MaxX + 1 do
  132.             setb(x,y,color)
  133.             x = x + 1
  134.          end
  135.          y = y + 1
  136.       end
  137.    else
  138.       errorLog.write(os.time().." setBkg called with useColor false at x:"..x.." y:"..y.."\n")
  139.    end
  140. end
  141. function setCol(color)
  142.    if setting.useColor then
  143.       local y = 1
  144.       while y < MaxY + 1 do
  145.          local x = 1
  146.          while x < MaxX + 1 do
  147.             setc(x,y,color)
  148.             x = x + 1
  149.          end
  150.          y = y + 1
  151.       end
  152.    end
  153. end
  154. local function tt(x,y)
  155.    local a = ""
  156.    if x < 10 then
  157.       a = "00"..x
  158.    elseif x < 100 then
  159.       a = "0"..x
  160.    else
  161.       a = x
  162.    end
  163.    if y < 10 then
  164.       a = a.."00"..x
  165.    elseif y < 100 then
  166.       a = a.."0"..x
  167.    else
  168.       a = a..x
  169.    end
  170.    return a
  171. end
  172. function saveTo(filename,x1,y1,x2,y2) -- since we dont want to save everything (99x99 canvas)
  173.    if x1 > 0 and x1 < x2 and y1 > 0 and y1 < y2 and x2 < MaxX+1 and y2 < MaxY+1 then
  174.       local file = fs.open(filename,"w")
  175.       local y = y1
  176.       while y < y2 + 1 do
  177.          local x = x1
  178.          while x < x2 do
  179.             local a = tt(x,y)..gett(x,y)..getc(x,y)..getb(x,y).."\n"
  180.             file.write(tostring(a))
  181.             x = x + 1
  182.          end
  183.          y = y + 1
  184.       end
  185.       file:close()
  186.    else
  187.       errorLog.write(os.time().." saveTo incorrect input at x:"..x1.." y:"..y1.."\n")
  188.    end
  189. end
  190. function importFromRaw(filename)
  191.    file = fs.open(filename, "r")
  192.    while true do
  193.       local a = file.readLine()
  194.       if a == nil then
  195.          break
  196.       else
  197.          local x = 1+tonumber(a:sub(1,3))
  198.          local y = 1+tonumber(a:sub(4,6))
  199.          frame.sett(x,y,a:sub(7,7))
  200.          frame.setc(x,y,a:sub(8,8))
  201.          frame.setb(x,y,a:sub(9,9))
  202.       end
  203.    end
  204.    file:close()
  205. end
  206. --end user functions
  207. function draw(x,y)
  208.    local A = tT(x,y)
  209.    if setting.drawEmpty == false and t[A] == " " then
  210.       return false
  211.    else
  212.       if setting.useColor then
  213.          term.setCursorPos(x,y)
  214.          term.blit(t[A],c[A],b[A])
  215.       else
  216.          term.setCursorPos(x,y)
  217.          write(t[A])
  218.       end
  219.    end
  220. end
  221. function drawPart(x1,y1,x2,y2)
  222.    if x1 > 0 and x1 < x2+1 and y1 > 0 and y1 < y2+1 and x2 < MaxX+1 and y2 < MaxY+1 then
  223.       local y = y1
  224.       while y < y2+1 do
  225.          local x = x1
  226.          while x < x2+1 do
  227.             draw(x,y)
  228.             x = x + 1
  229.          end
  230.          y = y + 1
  231.       end
  232.    else
  233.       errorLog.write(os.time().." drawPart incorrect input at x:"..x1.." y:"..y1.."\n")
  234.    end
  235. end
  236. function drawAll()
  237.    drawPart(1,1,MaxX,MaxY)
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement