summary refs log tree commit diff stats
path: root/main.lua
diff options
context:
space:
mode:
authorSoniEx2 <endermoneymod@gmail.com>2019-02-10 23:29:33 -0200
committerSoniEx2 <endermoneymod@gmail.com>2019-02-10 23:31:16 -0200
commit9aeaf7326c0ae52572b055a28f337842e8cd000a (patch)
tree01f1323cd466db7d7c8d6365ab0b793fd677c016 /main.lua
I should've put this on git sooner.
Diffstat (limited to 'main.lua')
-rw-r--r--main.lua332
1 files changed, 332 insertions, 0 deletions
diff --git a/main.lua b/main.lua
new file mode 100644
index 0000000..2a1dd71
--- /dev/null
+++ b/main.lua
@@ -0,0 +1,332 @@
+local kt = {}
+local t = 0.05
+local t0 = 0.5 -- initial cooldown
+local TIMEOUT_FIRST = -32767 -- a very negative number
+
+local saved = 0xFFFFFFFFFFFF
+
+function love.keypressed(k)
+  if k == "escape" then
+    if saved and saved > love.timer.getTime() then
+      love.event.quit()
+    else
+      saved = love.timer.getTime() + 0.5
+    end
+  end
+  kt[k] = TIMEOUT_FIRST
+end
+function love.keyreleased(k)
+  kt[k] = nil
+end
+
+local visual_start
+
+local cp = 1
+local palt = {}
+local imgpalt = {}
+local imgt = {}
+
+for i=1,256 do
+  imgt[i] = 0
+end
+for i=1,256 do
+  imgpalt[i] = 0
+end
+for i=0,255 do
+  palt[i] = 0
+end
+for i=0,15 do
+  palt[i] = i*0x11111100 + 0xFF
+  imgpalt[i*16 + 1] = 0x11*i
+  imgpalt[i*16 + 2] = 0x11*i
+  imgpalt[i*16 + 3] = 0x11*i
+  imgpalt[i*16 + 4] = 0xFF
+end
+
+local batch
+local batchid = {} -- should have 512 entries
+local hexquads = {}
+local palettedata
+local paletting
+local imagedata
+local backimage
+local backimagedata
+local image
+-- PAL_MAX is the max value of a pixel.
+-- this is 255 in paletting mode, 15 otherwise.
+local PAL_MAX = 15
+
+local mul = 36
+local function markupdated(pos, redraw)
+  local x = (pos-1) % 16
+  local y = math.floor((pos-1) / 16)
+
+  if not batchid[pos * 2] then
+    batchid[pos * 2] = batch:add(hexquads[math.floor(imgt[pos]/16)], x * mul, y * mul)
+    batchid[pos * 2 + 1] = batch:add(hexquads[imgt[pos]%16], x * mul + 16, y * mul)
+  else
+    batch:set(batchid[pos * 2], hexquads[math.floor(imgt[pos]/16)], x * mul, y * mul)
+    batch:set(batchid[pos * 2 + 1], hexquads[imgt[pos]%16], x * mul + 16, y * mul)
+  end
+
+  local rgba = palt[imgt[pos]]
+  local r = math.floor(rgba / 16777216) % 256
+  local g = math.floor(rgba / 65536) % 256
+  local b = math.floor(rgba / 256) % 256
+  local a = rgba % 256
+  imagedata:setPixel(x, y, r/255, g/255, b/255, a/255)
+  if paletting then
+    local r, g, b, a = imgt[y*16+1], imgt[y*16+2], imgt[y*16+3], imgt[y*16+4]
+    imagedata:setPixel(3, y, r/255, g/255, b/255, a/255)
+  end
+  if redraw then
+    image:replacePixels(imagedata)
+  end
+end
+
+function love.wheelmoved(x, y)
+  saved = nil
+  imgt[cp] = math.min(math.max(imgt[cp]+y, 0), PAL_MAX)
+  markupdated(cp, true)
+end
+
+function setback(pos, highlight)
+    local x = (pos-1)%16
+    local y = math.floor((pos-1)/16)
+    if highlight then
+      backimagedata:setPixel(x, y, 0.5, 0, 0, 1)
+    else
+      backimagedata:setPixel(x, y, 0, 0, 0, 0)
+    end
+end
+
+function love.update(dt)
+  local function ktup(key, func)
+    if kt[key] then
+      local nt = kt[key] - dt
+      kt[key] = nt
+      if nt <= 0 then
+        if nt <= TIMEOUT_FIRST then
+          kt[key] = t0
+        else
+          kt[key] = t
+        end
+        return func()
+      end
+    end
+  end
+  local function imgup()
+    setback(cp, false)
+    cp = math.max(cp - 16, 1)
+    setback(cp, true)
+    backimage:replacePixels(backimagedata)
+  end
+  local function imgdown()
+    setback(cp, false)
+    cp = math.min(cp + 16, 256)
+    setback(cp, true)
+    backimage:replacePixels(backimagedata)
+  end
+  local function imgleft()
+    setback(cp, false)
+    cp = math.max(cp - 1, 1)
+    setback(cp, true)
+    backimage:replacePixels(backimagedata)
+  end
+  local function imgright()
+    setback(cp, false)
+    cp = math.min(cp + 1, 256)
+    setback(cp, true)
+    backimage:replacePixels(backimagedata)
+  end
+  local function decr()
+    saved = nil
+    local amt = (kt['lshift'] and 16 or 1)
+    for i=visual_start or cp, cp do
+      imgt[i] = math.max(imgt[i] - amt, 0)
+      markupdated(i, true)
+    end
+  end
+  local function incr()
+    saved = nil
+    local amt = (kt['lshift'] and 16 or 1)
+    for i=visual_start or cp, cp do
+      imgt[i] = math.min(imgt[i] + amt, PAL_MAX)
+      markupdated(i, true)
+    end
+  end
+  local function light_propagation_up()
+    if visual_start then
+      print("[NYI] light_propagation_up + visual_start")
+      return
+    end
+    saved = nil
+    local initialValue = imgt[cp]
+    local newValue = math.min(initialValue + 1, 15)
+    if initialValue == newValue then return end
+    local function recurse(pos, dx, dy, targetValue)
+      local xpos = (pos - 1) % 16 + 1
+      local ypos = ((pos - xpos)/16) + 1
+      local npos
+      if dx < 0 and xpos > 1 then
+        npos = pos - 1
+      elseif dx > 0 and xpos < 16 then
+        npos = pos + 1
+      elseif dy > 0 and ypos < 16 then
+        npos = pos + 16
+        flag = print
+      elseif dy < 0 and ypos > 1 then
+        npos = pos - 16
+      end
+      if npos and imgt[npos] < targetValue then
+        imgt[npos] = targetValue
+        markupdated(npos, false)
+        recurse(npos, dx, dy, targetValue - 1)
+        recurse(npos, dy, -dx, targetValue - 1)
+      end
+    end
+    imgt[cp] = newValue
+    markupdated(cp, false)
+    recurse(cp, 0, 1, newValue - 1)
+    recurse(cp, 1, 0, newValue - 1)
+    recurse(cp, 0, -1, newValue - 1)
+    recurse(cp, -1, 0, newValue - 1)
+    image:replacePixels(imagedata)
+  end
+  local function light_propagation_down()
+    if true then
+      print("[NYI] light_propagation_down")
+      return
+    end
+    if visual_start then
+      print("[NYI] light_propagation_down + visual_start")
+      return
+    end
+    saved = nil
+    local initialValue = imgt[cp]
+    local newValue = math.max(initialValue - 1, 0)
+    if initialValue == newValue then return end
+    local function recurse(pos, dx, dy, targetValue)
+      local xpos = (pos - 1) % 16 + 1
+      local ypos = ((pos - xpos)/16) + 1
+      local npos
+      if dx < 0 and xpos > 1 then
+        npos = pos - 1
+      elseif dx > 0 and xpos < 16 then
+        npos = pos + 1
+      elseif dy > 0 and ypos < 16 then
+        npos = pos + 16
+        flag = print
+      elseif dy < 0 and ypos > 1 then
+        npos = pos - 16
+      end
+      if npos and imgt[npos] < targetValue then
+        imgt[npos] = targetValue
+        markupdated(npos, false)
+        recurse(npos, dx, dy, targetValue - 1)
+        recurse(npos, dy, -dx, targetValue - 1)
+      end
+    end
+    imgt[cp] = newValue
+    markupdated(cp, false)
+    recurse(cp, 0, 1, newValue - 1)
+    recurse(cp, 1, 0, newValue - 1)
+    recurse(cp, 0, -1, newValue - 1)
+    recurse(cp, -1, 0, newValue - 1)
+    image:replacePixels(imagedata)
+  end
+  local function save()
+    saved = 0xFFFFFFFFFFFF
+    local data = imagedata:encode("png")
+    local name = love.data.encode("string", "hex", love.data.hash("sha1", data):sub(1, 7)) .. ".png"
+    love.filesystem.write("img-" .. name, data)
+  end
+  local function palette()
+    -- Called to enable/disable palette editing mode
+    paletting = not paletting
+    imagedata, palettedata = palettedata, imagedata
+    imgt, imgpalt = imgpalt, imgt
+    PAL_MAX = paletting and 255 or 15
+    if paletting then
+      for i=0,255 do
+        palt[i] = i*0x01010100 + 0xff
+      end
+    else
+      for i=0,15 do
+        local r = imgpalt[i*16+1]
+        local g = imgpalt[i*16+2]
+        local b = imgpalt[i*16+3]
+        local a = imgpalt[i*16+4]
+        palt[i] = ((r*256+g)*256+b)*256+a
+      end
+    end
+    for i=1,256 do
+      markupdated(i, false)
+    end
+    image:replacePixels(imagedata)
+  end
+  local function visual()
+    visual_start = not visual_start and cp
+  end
+  ktup('i', imgup)
+  ktup('up', imgup)
+  ktup('j', imgleft)
+  ktup('left', imgleft)
+  ktup('k', imgdown)
+  ktup('down', imgdown)
+  ktup('l', imgright)
+  ktup('right', imgright)
+  ktup('z', decr)
+  ktup('x', incr)
+  ktup('a', light_propagation_up)
+  ktup('s', light_propagation_down)
+  ktup('w', save)
+  ktup('p', palette)
+  ktup('v', visual)
+end
+
+local quad
+local backquad
+
+local heximage
+
+function love.load()
+  imagedata = love.image.newImageData(16, 16)
+  backimagedata = love.image.newImageData(16, 16)
+  palettedata = love.image.newImageData(16, 16)
+  image = love.graphics.newImage(imagedata, {mipmaps=false})
+  backimage = love.graphics.newImage(backimagedata, {mipmaps=false})
+  image:setFilter("nearest", "nearest")
+  backimage:setFilter("nearest", "nearest")
+  quad = love.graphics.newQuad(0, 0, 16, 16, 16, 16)
+  backquad = love.graphics.newQuad(0, 0, 16, 16, 16, 16)
+
+  heximage = love.graphics.newImage("hex.png")
+  for i = 0, 15 do
+    hexquads[i] = love.graphics.newQuad(i*16, 0, 16, 32, heximage:getDimensions())
+  end
+  batch = love.graphics.newSpriteBatch(heximage)
+  for i=1,256 do
+    markupdated(i, false)
+  end
+  image:replacePixels(imagedata)
+
+  for x=0, 15 do
+    for y=0, 15 do
+      backimagedata:setPixel(x, y, 0, 0, 0, 0)
+    end
+  end
+  local x = (cp-1)%16
+  local y = math.floor((cp-1)/16)
+  backimagedata:setPixel(x, y, 0.5, 0, 0, 1)
+  backimage:replacePixels(backimagedata)
+end
+
+function love.draw()
+  love.graphics.draw(image, quad, 576, 0, 0, 8)
+  love.graphics.draw(backimage, backquad, 2, 2, 0, mul)
+  love.graphics.print((visual_start and tostring(visual_start) .. ":" or "") .. tostring(cp), 0, 588)
+  love.graphics.draw(batch, 2, 2)
+end
+
+-- vim: set sw=2 sts=2: