Advertisement
Griffi

Falling Sand

Sep 19th, 2020
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.18 KB | None | 0 0
  1. print('loading')
  2. local ver=1.2
  3. local computer = require'computer'
  4. local event = require("event")
  5. local term = require'term'
  6. if not term.isAvailable() then
  7.   return
  8. end
  9. local gpu = term.gpu()
  10. local w,h = gpu.getResolution()
  11. --term.clear()
  12.  
  13. local sim={w=w/2,h=h}
  14. for i=1,sim.w do
  15.   sim[i]={}
  16. end
  17. setmetatable(sim,{__index=function() return{} end})
  18.  
  19. for i=1,sim.w do --add floor
  20.   for j=1,2 do
  21.     sim[i][sim.h-4+j]=1
  22.   end
  23. end
  24.  
  25. local function tryMove(x,y,nx,ny,f)
  26.   local sim=sim
  27.   if sim[nx][ny] and not(f)then
  28.     return true
  29.   end
  30.   local val=sim[x][y]
  31.   sim[x][y]=nil
  32.   sim[nx][ny]=val
  33.   return false
  34. end
  35.  
  36. local elem={
  37.   [0]={
  38.     name='nil',
  39.     color=0
  40.   },
  41.   {
  42.     name='wall',
  43.     color=0xaaaaaa,
  44.     --update=function(x,y) end
  45.   },
  46.   {
  47.     name='sand',
  48.     update=function(x,y)
  49.       if tryMove(x,y,x,y+1) then
  50.         if tryMove(x,y,x+1,y+1) then
  51.           return tryMove(x,y,x-1,y+1)
  52.         end
  53.       end
  54.     end,
  55.     color=0xf0f00c
  56.   }
  57. }
  58.  
  59.  
  60. local function touchHandler(a,b,x,y)
  61.   sim[math.ceil(x/2)][y+1]=2
  62. end
  63. local brk=false
  64. local function exit()
  65.   term.clear()
  66.   on,nb,sim=nil,nil,nil
  67.   brk=true
  68.   print('sAND v.'..ver)
  69. end
  70.  
  71. event.listen("touch", touchHandler)
  72. event.listen("interrupted", exit)
  73.  
  74. local function buf()
  75.   return setmetatable({},{__index=function()return 0 end})
  76. end
  77.  
  78. local ob=buf()
  79. local nb=buf()
  80. local bufl=sim.w*sim.h
  81.  
  82. local function to1d(x,y,w)
  83.   return x+(y-1)*w
  84. end
  85. local function to2d(i,w)
  86.   local f=math.floor((i-1)/w)+1
  87.   return i-(f-1)*w,f
  88. end
  89.  
  90. term.clear()
  91. while true do os.sleep(0)
  92.   if brk then break end
  93.  
  94.   local sim=sim
  95.   for x=1,sim.w do
  96.     local nu,nils=0,0
  97.     local sy,sm,c=1,'',1
  98.     for y=sim.h,1,-1 do
  99.       local v=sim[x][y]
  100.       if v then
  101.         local e=elem[v]
  102.         if e.update then
  103.           e.update(x,y)
  104.         end
  105.         nb[to1d(x,y,sim.w)]=e.color
  106.       end
  107.     end
  108.   end
  109.  
  110.   for i=1,bufl do
  111.     if ob[i]~=nb[i] then
  112.       gpu.setBackground(nb[i])
  113.       local x,y=to2d(i,sim.w)
  114.       gpu.set(x*2-1,y-1,'  ')
  115.     end
  116.     if rawget(ob,i)==0 then rawset(ob,i,nil) end
  117.     if rawget(nb,i)==0 then rawset(nb,i,nil) end
  118.   end
  119.  
  120.   ob=nb
  121.   nb=buf()
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement