Advertisement
Guest User

lgame

a guest
Oct 15th, 2023
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.60 KB | None | 0 0
  1. --v2.1
  2. local function update()
  3.   shell.run("delete","lg")
  4.   shell.run("pastebin","get","sSF7NPYC","lg")
  5.   os.reboot()
  6. end
  7. local args = {...}
  8. if (args[1] == "update") then
  9.   update()
  10. end
  11. function setup(name)
  12.   sx,sy = nil
  13.   buttons = {}
  14.   pixels = {}
  15.   local bg = colors.black
  16.   sprites = {}
  17.   inputs = {}
  18.   if (name == "term") then
  19.     sx,sy = term.getSize()
  20.   else
  21.     m = peripheral.wrap(name)
  22.     term.redirect(m)
  23.     sx,sy = m.getSize()
  24.   end
  25. end
  26. function exit()
  27.   term.native()
  28. end
  29. function drawText(x,y,color,colorb,text)
  30.   term.setCursorPos(x,y)
  31.   term.setTextColor(color)
  32.   term.setBackgroundColor(colorb)
  33.   term.write(text)
  34. end
  35. function clearScreen()
  36.   term.clear()
  37. end
  38. function setBackgroundColor(color)
  39.   term.setBackgroundColor(color)
  40. end
  41. function setTextColor(color)
  42.   term.setTextColor(color)
  43. end
  44. function drawBT(name,x,xe,y,ye,color,colorb,text)
  45.   term.setBackgroundColor(colorb)
  46.   term.setTextColor(color)
  47.   for i=x,xe do
  48.     for g=y,ye do
  49.       term.setCursorPos(i,g)
  50.       term.write(" ")
  51.     end
  52.   end
  53.   textline = (y + ye) / 2
  54.   textline = string.format("%.1f", textline)
  55.   textline = tonumber(textline)
  56.   lines = {}
  57.   for word in string.gmatch(text,"[^\n]+") do
  58.      table.insert(lines,word)
  59.   end
  60.   if (#lines > 1) then
  61.     for w=1,#lines do
  62.       term.setCursorPos(x,y+w)
  63.       term.write(lines[w])
  64.     end
  65.   else
  66.     term.setCursorPos(x,textline)
  67.     term.write(text)
  68.   end
  69.   table.insert(buttons,name..";"..x..";"..xe..";"..y..";"..ye)  
  70. end
  71. function isButton(x,y)
  72.   for i=1,#buttons do
  73.     local parts = {}
  74.     for word in string.gmatch(buttons[i],"[^;]+") do
  75.       table.insert(parts,word)
  76.     end
  77.     name,bx,bxe,by,bye = parts[1],tonumber(parts[2]),tonumber(parts[3]),tonumber(parts[4]),tonumber(parts[5])
  78.     if (bx-1 < x and x < bxe + 1 ) then
  79.       if (by-1 < y and bye+1 > y) then
  80.         return name
  81.       end
  82.     end
  83.   end
  84.   return false
  85. end
  86. function fill(x,x2,y,y2,color)
  87.   term.setBackgroundColor(color)
  88.   for i=x,x2 do
  89.     for g=y,y2 do
  90.       term.setCursorPos(i,g)
  91.       pixels[x..y] = color
  92.       term.write(" ")
  93.     end
  94.   end
  95. end
  96. function fillScreen(color)
  97.   term.setBackgroundColor(color)
  98.   term.clear()
  99.   pixels = {}
  100. end
  101. function drawImage(x,y,name)
  102.   image = paintutils.loadImage(name)
  103.   paintutils.drawImage(image,x,y)
  104. end
  105. function getSize()
  106.   local sizet = {}
  107.   sizet.x = sx
  108.   sizet.y = sy
  109.   return sizet
  110. end
  111. function drawInput(name,x,x2,y,y2,limit)
  112.   term.setBackgroundColor(colors.gray)
  113.   term.setTextColor(colors.white)
  114.   for i=x,x2 do
  115.     for g=y,y2 do
  116.       term.setCursorPos(i,g)
  117.       term.write(" ")
  118.     end
  119.   end
  120.   table.insert(inputs,name..";"..x..";"..x2..";"..y..";"..y2..";"..limit)
  121. end
  122. function isInput(x,y)
  123.   for i=1,#inputs do
  124.     local parts = {}
  125.     for word in string.gmatch(inputs[i],"[^;]+") do
  126.       table.insert(parts,word)
  127.     end
  128.     name,bx,bxe,by,bye = parts[1],tonumber(parts[2]),tonumber(parts[3]),tonumber(parts[4]),tonumber(parts[5])
  129.     if (bx-1 < x and x < bxe + 1 ) then
  130.       if (by-1 < y and bye+1 > y) then
  131.         return name
  132.       end
  133.     end
  134.   end
  135.   return false
  136. end
  137. function getInput(x,y)
  138.   for i=1,#inputs do
  139.     local parts = {}
  140.     for word in string.gmatch(inputs[i],"[^;]+") do
  141.       table.insert(parts,word)
  142.     end
  143.     name,bx,bxe,by,bye,limit = parts[1],tonumber(parts[2]),tonumber(parts[3]),tonumber(parts[4]),tonumber(parts[5]),tonumber(parts[6])
  144.     if (bx-1 < x and x < bxe + 1 ) then
  145.       if (by-1 < y and bye+1 > y) then
  146.         term.setCursorPos(bx,by)
  147.         local text = limitRead(limit)
  148.         return text
  149.       end
  150.     end
  151.   end
  152.   return false
  153. end
  154. function lggSetDefBG(color)
  155.   bg = color
  156. end
  157. function drawSprite(name,x,y,iname)
  158.   image = paintutils.loadImage(iname)
  159.   paintutils.drawImage(image,x,y)
  160.   table.insert(sprites,name..";"..x..";"..y..";"..iname)
  161. end
  162. function moveSprite(name,x,y)
  163.   for h=1,#sprites do
  164.     local parts = {}
  165.     term.setBackgroundColor(colors.black)
  166.     for word in string.gmatch(sprites[h],"[^;]+") do
  167.       table.insert(parts,word)
  168.     end
  169.     if (parts[1] == name) then
  170.     local iname = nil
  171.     local name,ox,oy,iname = parts[1],tonumber(parts[2]),tonumber(parts[3]),parts[4]
  172.     image = paintutils.loadImage(iname)
  173.     term.setCursorPos(10,1)
  174.     term.setBackgroundColor(colors.black)
  175.     for i=ox,ox+getWidth(image) do
  176.       for g=oy,oy+getHeight(image) do
  177.         if (pixels[i..g] ~= nil) then
  178.           term.setCursorPos(i,g)
  179.           term.setBackgroundColor(pixels[i..g])
  180.           term.write(" ")
  181.         else
  182.           term.setCursorPos(i,g)
  183.           term.setBackgroundColor(bg)
  184.           term.write(" ")
  185.         end
  186.       end
  187.     end
  188.     paintutils.drawImage(image,x,y)
  189.     table.remove(sprites,h)
  190.     table.insert(sprites,name..";"..x..";"..y..";"..iname)    
  191.     else
  192.     end
  193.   end
  194.   return false
  195. end
  196. function getSpriteColider(name)
  197.   for h=1,#sprites do
  198.     local parts = {}
  199.     term.setBackgroundColor(colors.black)
  200.     for word in string.gmatch(sprites[h],"[^;]+") do
  201.       table.insert(parts,word)
  202.     end
  203.     if (parts[1] == name) then
  204.     local iname = nil
  205.     local name,ox,oy,iname = parts[1],tonumber(parts[2]),tonumber(parts[3]),parts[4]
  206.     image = paintutils.loadImage(iname)
  207.     local sizet = {}
  208.     sizet.x = ox
  209.     sizet.y = oy
  210.     sizet.x2 = ox + getWidth(image)
  211.     sizet.y2 = oy + getHeight(image)
  212.     return sizet
  213.     else
  214.     end
  215.   end
  216.   return false
  217. end
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235. -- FUNC
  236. function limitRead(limX, rChar,color,colorb)
  237.         term.setBackgroundColor(colors.gray)
  238.         term.setTextColor(colors.white)
  239.         term.setCursorBlink(true)
  240.         local origX, origY = term.getCursorPos()
  241.         local returnString = ""
  242.         while true do
  243.                 local xPos, yPos = term.getCursorPos()
  244.                 local event, p1, p2 = os.pullEvent()
  245.                 if event == "char" then
  246.                         returnString = returnString..p1
  247.                         if not rChar then
  248.                                 if not limX then
  249.                                         write(p1)
  250.                                 else
  251.                                         if string.len(returnString) >= limX then
  252.                                                 term.setCursorPos(origX, origY)
  253.                                                 write(string.sub(returnString, (string.len(returnString)-limX)+1))
  254.                                         elseif string.len(returnString) < limX then
  255.                                                 write(p1)
  256.                                         end
  257.                                 end
  258.                         else
  259.                                 if not limX then
  260.                                         write(rChar)
  261.                                 else
  262.                                         if string.len(returnString) >= limX then
  263.                                                 term.setCursorPos(origX, origY)
  264.                                                 write(string.rep(rChar, limX))
  265.                                         elseif string.len(returnString) < limX then
  266.                                                 write(rChar)
  267.                                         end
  268.                                 end
  269.                         end
  270.                 elseif event == "key" and p1 == 259 then --backspace
  271.                         returnString = string.sub(returnString, 1, (string.len(returnString))-1)
  272.                         term.setCursorPos(xPos-1,yPos)
  273.                         write(" ")
  274.                         term.setCursorPos(origX, origY)
  275.                         if string.len(returnString) >= limX then
  276.                                 if not rChar then
  277.                                         write(string.sub(returnString, (string.len(returnString)-limX)+1))
  278.                                 else
  279.                                         write(string.rep(rChar,limX))
  280.                                 end
  281.                         else
  282.                                 if not rChar then
  283.                                         write(returnString)
  284.                                 else
  285.                                         write(string.rep(rChar, string.len(returnString)))
  286.                                 end
  287.                         end
  288.                 elseif event == "key" and p1 == 257 then --enter
  289.                         break
  290.                 end
  291.         end
  292.         term.setCursorBlink(false)
  293.         return returnString
  294. end
  295. function getHeight( image )
  296.   --# init the image start and end variables, these are what track where the start and end are
  297.   local imgStart = #image --# we need to start at the end so we can apply math.min
  298.   local imgEnd = 0 --# we need to start at the start so we can apply math.max
  299.   --# loop through the table
  300.   for index,imageLine in ipairs(image) do
  301.         --# create a variable to hold if the line has a pixel
  302.         local isLine = false
  303.         --# loop through the pixels in the line
  304.         for i,pixel in ipairs(imageLine) do
  305.           --# if the pixel is drawable
  306.           if pixel ~= 0 then
  307.                 --# record that there is a pixel, and exit the loop, no need to check anymore, we know there is at least 1
  308.                 isLine = true
  309.                 break
  310.           end
  311.         end
  312.         --# if the line has pixels, check if its the start or end
  313.         if isLine then
  314.           imgStart = math.min(imgStart,index)
  315.           imgEnd = math.max(imgEnd,index)
  316.         end
  317.   end
  318.   --# return the height of the image, which is the end plus the start, plus 1 because tables start at index 1 not 0
  319.   return imgEnd - imgStart + 1
  320. end
  321. function getWidth(img)
  322.   --# the max width
  323.   local maxWidth = 0
  324.   --# loop through the image
  325.   for i,line in pairs(img) do
  326.         --# the variable to hold where the line starts
  327.         local lineStart = #line --# start it at the end so we can apply math.min
  328.         --# the variable to hold where the line ends
  329.         local lineEnd = 0 --# start at the start so we can apply math.max
  330.         --# loop the line
  331.         for index,v in pairs(line) do
  332.           --# if its a pixel
  333.           if v ~= 0 then
  334.                 --# check if its the start or the end of the line
  335.                 lineStart = math.min(lineStart,index)
  336.                 lineEnd = math.max(lineEnd,index)
  337.           end
  338.         end
  339.         --# the length of the line is the end minus the start positions, +1 because tables start at index 1 not 0
  340.         local length = lineEnd - lineStart + 1
  341.         --# check if its the max length
  342.         maxWidth = math.max(maxWidth, length)
  343.   end
  344.   --# return the max length
  345.   return maxWidth
  346. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement