Advertisement
Guest User

Untitled

a guest
May 2nd, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | None | 0 0
  1. -- Alien Text Generator
  2. -- Save as main.lua, run with Love2D
  3. -- First it show you the alphabet
  4. -- Then press any key to get a page of filled-up random alien text
  5. -- Then press any key twice more to get a page of random alien text with spacing
  6.  
  7. local CharParams={Width=3,Height=4}
  8. local CharSheets={}
  9. local Space={}
  10.  
  11. local State=0
  12. local Matrix={}
  13.  
  14. --
  15. --  ^
  16. --  |        ^
  17. --  |  j,v,y |
  18. --  |
  19. --  |    #
  20. --  |   # #
  21. --  |   ###
  22. --  |   # #
  23. --  |          i,u,x ->
  24. --  +-------------------->
  25. --
  26.  
  27. local xor = function(a,b) return (a and not b) or (b and not a) end
  28.  
  29. local function Shuffle(list)
  30.     for k=#list,1,-1 do
  31.         list[#list]=table.remove(list,math.random(k))
  32.     end
  33. end
  34.  
  35. local function isCharCorrect(c,u,v)
  36.  
  37.     -- Forbids 2x2 blobs of pixels
  38.     for i=2,u do
  39.         for j=2,v do
  40.             if c[i][j] and c[i][j-1] and c[i-1][j] and c[i-1][j-1] then
  41.                 return false
  42.             end
  43.         end
  44.     end
  45.  
  46.     -- Must have a pixel on the bottom
  47.     local hasPixel=false
  48.     for i=1,u do
  49.         if c[i][1] then
  50.             hasPixel=true
  51.         end
  52.     end
  53.     if not hasPixel then
  54.         return false
  55.     end
  56.  
  57.     -- Must be centered
  58.     for i=1,math.floor(u/2) do
  59.         local hasPixel=false
  60.         for j=1,v do
  61.             if c[i][j] then
  62.                 hasPixel=true
  63.             end
  64.         end
  65.         if hasPixel then
  66.             hasPixel=false
  67.             for j=1,v do
  68.                 if c[u-i][j] or c[u-i+1][j] then
  69.                     hasPixel=true
  70.                 end
  71.             end
  72.             if not hasPixel then
  73.                 return false
  74.             end
  75.         else
  76.             for j=1,v do
  77.                 if c[u-i+1][j] then
  78.                     return false
  79.                 end
  80.             end
  81.         end
  82.     end
  83.  
  84.     -- Must not have disjoint pixels
  85.     local d={}
  86.     local k=1
  87.     for i=1,u do
  88.         d[i]={}
  89.         for j=1,v do
  90.             if c[i][j] then
  91.                 d[i][j]=k
  92.                 k=k+1
  93.             end
  94.         end
  95.     end
  96.     local retry=true
  97.     while retry do
  98.         retry=false
  99.         local merges={}
  100.         for i=1,u do
  101.             for j=1,v do
  102.                 if d[i][j] then
  103.                     for x=math.max(1,i-1),math.min(u,i+1) do
  104.                         for y=math.max(1,j-1),math.min(v,j+1) do
  105.                             if d[x][y] and not (d[x][y]==d[i][j]) then
  106.                                 d[x][y]=math.min(d[x][y],d[i][j])
  107.                                 d[i][j]=d[x][y]
  108.                                 retry=true
  109.                             end
  110.                         end
  111.                     end
  112.                 end
  113.             end
  114.         end
  115.     end
  116.     local n=0
  117.     for i=1,u do
  118.         for j=1,v do
  119.             n=math.max(n,d[i][j] or n)
  120.         end
  121.     end
  122.     if not (n==1) then
  123.         return false
  124.     end
  125.  
  126.     return true
  127. end
  128.  
  129. local function MakeCharSheets(u,v)
  130.     Space={}
  131.     for i=1,u do
  132.         Space[i]={}
  133.     end
  134.     local s={}
  135.     local b={}
  136.     for n=1,2^(u*v) do
  137.         local r=false
  138.         for k=1,u*v do
  139.             if b[k] then
  140.                 b[k]=not b[k]
  141.             else
  142.                 b[k]=true
  143.                 break
  144.             end
  145.         end
  146.         local c={}
  147.         for i=1,u do
  148.             c[i]={}
  149.             for j=1,v do
  150.                 c[i][j]=b[i+u*v-u*j]
  151.             end
  152.         end
  153.         if isCharCorrect(c,u,v) then
  154.             table.insert(s,c)
  155.         end
  156.     end
  157.     return s
  158. end
  159.  
  160. function love.load()
  161.     love.graphics.setCaption("Alien Letters")
  162.     love.graphics.setMode(512,768)
  163.     love.graphics.setBlendMode("alpha")
  164.     love.graphics.setBackgroundColor(0,0,0)
  165.     love.graphics.setColor(32,255,32,255)
  166.     Font12 = love.graphics.newFont(12)
  167.     Font24 = love.graphics.newFont(24)
  168.     Font48 = love.graphics.newFont(48)
  169.     love.graphics.setFont(Font12)
  170.     CharSheets=MakeCharSheets(CharParams.Width,CharParams.Height)
  171.     --love.mouse.setVisible(false) -- make default mouse invisible
  172.     --love.mouse.setGrab(true)
  173.     State=0
  174. end
  175.  
  176.  
  177. local function DrawText(text,x,y)
  178.     love.graphics.print(text,x,love.graphics.getHeight()-y-12)
  179. end
  180.  
  181. local function DrawPoint(x,y)
  182.     love.graphics.point(0.5+x,0.5+love.graphics.getHeight()-y)
  183. end
  184.  
  185. local function DrawCharOverRec(c,x,y)
  186.     for i=1,CharParams.Width do
  187.         for j=1,CharParams.Height do
  188.             if c[i][j] then
  189.                 love.graphics.setColor(32,255,32,255)
  190.             else
  191.                 love.graphics.setColor(96,32,32,255)
  192.             end
  193.             DrawPoint(x+2*i-1,y+2*j-1)
  194.             DrawPoint(x+2*i-1,y+2*j-2)
  195.             DrawPoint(x+2*i-2,y+2*j-1)
  196.             DrawPoint(x+2*i-2,y+2*j-2)
  197.         end
  198.     end
  199. end
  200.  
  201. local function DrawChar(c,x,y)
  202.     for i=1,CharParams.Width do
  203.         for j=1,CharParams.Height do
  204.             if c[i][j] then
  205.                 DrawPoint(x+2*i-1,y+2*j-1)
  206.                 DrawPoint(x+2*i-1,y+2*j-2)
  207.                 DrawPoint(x+2*i-2,y+2*j-1)
  208.                 DrawPoint(x+2*i-2,y+2*j-2)
  209.             end
  210.         end
  211.     end
  212. end
  213.  
  214. function love.draw()
  215.     love.graphics.setColor(32,255,32,255)
  216.     ;(({[0]=function()
  217.         local k=1
  218.         for y=love.graphics.getHeight()-20,16,-2*(CharParams.Height+1) do
  219.             for x=8,love.graphics.getWidth()-16,2*(CharParams.Width+1) do
  220.                 if CharSheets[k] then
  221.                     DrawCharOverRec(CharSheets[k],x,y)
  222.                 end
  223.                 k=k+1
  224.             end
  225.         end
  226.     end,
  227.     [2]=function()
  228.         local k=0
  229.         for y=love.graphics.getHeight()-20,16,-2*(CharParams.Height+1) do
  230.             for x=8,love.graphics.getWidth()-16,2*(CharParams.Width+1) do
  231.                 k=k+1
  232.                 if not Matrix[k] then
  233.                     Matrix[k]=CharSheets[math.random(1,#CharSheets)]
  234.                 end
  235.                 if Matrix[k]=="\n" then
  236.                     break
  237.                 end
  238.                 DrawChar(Matrix[k],x,y)
  239.             end
  240.         end
  241.     end
  242.     })[State]or(function() end))()
  243. end
  244.  
  245. function love.keypressed(key,unicode)
  246.     if key=="escape" then
  247.         if love.event.quit then
  248.             love.event.quit() -- 0.8.0
  249.         else
  250.             love.event.push('q') -- 0.7.2
  251.         end
  252.     end
  253.     State=State+1
  254.     if State>=1 then
  255.         if Matrix["2"]==2 then
  256.             for k=1,#CharSheets*12/100 do
  257.                 table.insert(CharSheets,Space)
  258.             end
  259.             for k=1,#CharSheets*12/1000 do
  260.                 table.insert(CharSheets,"\n")
  261.             end
  262.         end
  263.         Shuffle(CharSheets)
  264.         Matrix={["2"]=(Matrix["2"] or 0)+1}
  265.         State=2
  266.     end
  267. end
  268.  
  269.  
  270.  
  271. function love.update(dt)
  272.     if love.event.quit then
  273.         love.timer.sleep(0.030) -- 0.8.0
  274.     else
  275.         love.timer.sleep(30) -- 0.7.2
  276.     end
  277.     if #(Matrix or {})>1 then
  278.         Matrix[math.random(1,#Matrix)]=CharSheets[math.random(1,#CharSheets)]
  279.     end
  280. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement