̬̬̬̈̬̬̬̬̈̬̬̬̬̬̪쬪̬̬̬̪쬪 >?>>>3>>~~ww~~3>w~w~w>~~~ww7~p~p>pwpwppwppwpwpppppwppp p        ə   ̙ p       p  """ p""""""" """"""""""  """""""""""" """""""""""""" w"""""""""""""""""" """"""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""""""""""p """"""""""""""""""""""""""" """""""""""""""""""""""wy""""""""""""""""""""""y'"""""""""""""""""""'"""""""""""""""""w""""""""""""" """""""""""""""""""""""""""""""""""'"pw pw pp p```` `f`UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU"""""""RUUUUUUUDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""R333333SDDDDDDDTUUUUUUUfffffffvwwwwwww"""""""RUUUUUUUDDDDDDDTUUUUUUUfffffffvwwwwwww-- title: Powder sim -- author: Sava -- desc: Simple powder sim i made in a day. Has exactly 400 lines of code -- license: MIT License -- version: 0.2 -- script: lua -- input: mouse hotbarS = 20 w=240 h=136-hotbarS names = {"air", "brick", "weak brick", "fire", "sand", "plant", "acid", "toxic gas", "steam", "water", "copy", "clone", "snow", "stone", "smoke", "void"} selSize = w/#names upd = { function(x, y) end, -- air function(x, y) end, -- brick function(x, y) end, -- weak brick function(x, y) -- fire if math.random() < 0.05 then pix(x, y, 14) else if math.random() < 0.6 and isPass(x, y-1) then swap(x, y, 0, -1) else if math.random() < 0.5 then if isPass(x+1, y-1) then swap(x, y, 1, -1) end elseif isPass(x-1, y-1) then swap(x, y, -1, -1) end end end n = getNeig(x, y) for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) == 9 then pix(j.x, j.y, 8) end if boundC(j.x, j.y) and (rpix(j.x, j.y) == 7 or rpix(j.x, j.y) == 5) then pix(j.x, j.y, 3) end end end, function(x, y) -- sand if isPass(x, y+1) and math.random() < 0.8 then swap(x, y, 0, 1) else if math.random() < 0.5 then if isPass(x+1, y+1) then swap(x, y, 1, 1) end elseif isPass(x-1, y+1) then swap(x, y, -1, 1) end end end, function(x, y) -- plant n = getNeig(x, y) for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) == 9 then pix(j.x, j.y, 5) end end end, function(x, y) -- acid if isPass(x, y+1) and math.random() < 0.8 then swap(x, y, 0, 1) else if math.random() < 0.5 then if isPass(x+1, y) then swap(x, y, 1, 0) end elseif isPass(x-1, y) then swap(x, y, -1, 0) end end n = getNeig(x, y) for i, j in ipairs(n) do if isWeak(j.x, j.y) and rpix(j.x, j.y) ~= 6 and rpix(j.x, j.y) ~= 7 then pix(j.x, j.y, 7) pix(x, y, 7) end end end, function(x, y) -- toxic gas if math.random() < 0.001 then pix(x, y, 0) elseif math.random() < 0.5 then if isGasPass(x, y-1) and math.random() < 0.8 then swap(x, y, 0, -1) else if math.random() < 0.5 then if isGasPass(x+1, y-1) then swap(x, y, 1, -1) end elseif isGasPass(x-1, y-1) then swap(x, y, -1, -1) end end end end, function(x, y) -- steam if math.random() < 0.01 then if math.random() < 0.5 then pix(x, y, 9) end elseif math.random() < 0.5 then if isGasPass(x, y-1) and math.random() < 0.8 then swap(x, y, 0, -1) else if math.random() < 0.5 then if isGasPass(x+1, y-1) then swap(x, y, 1, -1) end elseif isGasPass(x-1, y-1) then swap(x, y, -1, -1) end end end end, function(x, y) -- water if isPass(x, y+1) and math.random() < 0.8 then swap(x, y, 0, 1) else if math.random() < 0.5 then if isPass(x+1, y) then swap(x, y, 1, 0) end elseif isPass(x-1, y) then swap(x, y, -1, 0) end end end, function(x, y) -- copy n = getNeig(x, y) for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) ~= 0 and rpix(j.x, j.y) ~= 10 then pix(x, y, rpix(j.x, j.y)) break end end end, function(x, y) -- clone local curP = 0 n = { {x=x-1, y=y-1}, {x=x-1, y=y}, {x=x-1, y=y+1}, {x=x, y=y-1}, {x=x, y=y+1}, {x=x+1, y=y-1}, {x=x+1, y=y}, {x=x+1, y=y+1}, } local empty = false for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) ~= 0 and rpix(j.x, j.y) ~= 1 and rpix(j.x, j.y) ~= 11 then curP = rpix(j.x, j.y) break elseif boundC(j.x, j.y) and rpix(j.x, j.y) == 0 then empty = true end end if curP ~= 0 then for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) ~= 11 then pix(j.x, j.y, curP) end end elseif not empty then pix(x, y, 1) end end, function(x, y) -- snow local chance = 0.001 n = getNeig(x, y) for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) == 9 then chance = chance * 1.5 end if boundC(j.x, j.y) and rpix(j.x, j.y) == 3 then chance = chance * 20 end end if math.random() < chance then pix(x, y, 9) elseif math.random() < 0.5 then if isPass(x, y+1) and math.random() < 0.8 then swap(x, y, 0, 1) else if math.random() < 0.5 then if isPass(x+1, y+1) then swap(x, y, 1, 1) end elseif isPass(x-1, y+1) then swap(x, y, -1, 1) end end end end, function(x, y) -- stone if isPass(x, y+1) and math.random() < 0.8 then swap(x, y, 0, 1) end end, function(x, y) -- smoke if math.random() < 0.005 then pix(x, y, 0) elseif math.random() < 0.5 then if isGasPass(x, y-1) and math.random() < 0.8 then swap(x, y, 0, -1) else if math.random() < 0.5 then if isGasPass(x+1, y-1) then swap(x, y, 1, -1) end elseif isGasPass(x-1, y-1) then swap(x, y, -1, -1) end end end end, function(x, y) -- void n = getNeig(x, y) for i, j in ipairs(n) do if boundC(j.x, j.y) and rpix(j.x, j.y) ~= 15 then pix(j.x, j.y, 0) end end end } function boundC(x, y) return x >= 0 and x < w and y >= 0 and y < h end function isEmpty(x, y) return boundC(x, y) and rpix(x, y) == 0 end function isPass(x, y) return boundC(x, y) and (rpix(x, y) == 0 or rpix(x, y) == 8 or rpix(x, y) == 14 or (rpix(x, y) == 9 and math.random() < 0.3)) end function isGasPass(x, y) return boundC(x, y) and rpix(x, y) ~= 1 and rpix(x, y) ~= 5 and rpix(x, y) ~= 2 and rpix(x, y) ~= 15 and rpix(x, y) ~= 10 and rpix(x, y) ~= 11 end function isWeak(x, y) return boundC(x, y) and rpix(x, y) ~= 0 and rpix(x, y) ~= 1 and rpix(x, y) ~= 15 and rpix(x, y) ~= 10 and rpix(x, y) ~= 11 end function swap(x, y, dx, dy) local temp = pix(x, y) pix(x, y, pix(x+dx, y+dy)) pix(x+dx, y+dy, temp) end function rpix(x, y) return bkp[x+1][y+1] -- scary! end function getNeig(x, y) tab = { {x=x-1, y=y}, {x=x, y=y-1}, {x=x, y=y+1}, {x=x+1, y=y} } return tab end function BOOT() for i=1, w do for j=1, h do pix(i-1, j-1, 0) end end end sel = 1 thick = 5 pmx, pmy = 0, 0 pleft = false pright = false rem = {} function TIC() if mx ~= nil then for i=mx-thick-1, mx+thick+1 do for j=my-thick-1, my+thick+1 do pix(i, j, rem[i][j]) end end end rem = {} pmx, pmy = mx, my pleft = left pright = right mx, my, left, middle, right, scrX, scrY = mouse() if left then if my < h then if pleft then tline(mx, my, pmx, pmy, thick, sel) else circ(mx, my, thick, sel) end end end if right then if my < h then if pright then tline(mx, my, pmx, pmy, thick, 0) else circ(mx, my, thick, 0) end end end if middle then for i=1, w do for j=1, h do pix(i-1, j-1, 0) end end end thick = math.min(math.max(thick + scrY, 0), 50) update() for i=mx-thick-1, mx+thick+1 do rem[i]={} for j=my-thick-1, my+thick+1 do rem[i][j]=pix(i, j) end end circb(mx, my, thick, 12) draw() end function update() bkp = {} local pos = {} for i=1, w do bkp[i] = {} for j=1, h do p=pix(i-1, j-1) bkp[i][j] = p pos[j+(i-1)*h] = {i, j} end end for po=1, #pos do local po1 = math.random(1, #pos) local temp = pos[po1] pos[po1] = pos[po] pos[po] = temp end for po=1, #pos do local i, j = pos[po][1], pos[po][2] p=bkp[i][j] upd[p+1](i-1, j-1) end end function draw() for i=1, #names do rect((i-1)*selSize, h+1, selSize, hotbarS-1, i-1) if sel == i-1 then rectb((i-1)*selSize, h+1, selSize, hotbarS-1, i+1) end end line(0, h, w, h, 5) for i=1, #names do if (mx >= (i-1)*selSize and mx < i*selSize and my > h) then rectb((i-1)*selSize, h+1, selSize, hotbarS-1, i) local posS = i > 10 and -30 or 10 print(names[i], mx+posS, my) if left then sel=i-1 end end end end function tline(x, y, x1, y1, rad, col) for i=0, 1, 0.05 do local px, py = lerp(x, x1, i), lerp(y, y1, i) circ(px, py, rad, col) end end function lerp(a, b, t) return a*(1-t)+b*t end