-- gen_npc.lua: NPC field sprites (16x24). One column per variant.
-- Layout: 1 row, many columns of 16x24.
-- Order:
--  0 afu-smile, 1 afu-flat, 2 afu-off
--  3 yuanyuan-smile,4 flat,5 off
--  6 dabao-smile,7 flat,8 off
--  9 mimi-smile,10 flat,11 off
-- 12 dr_fu-smile,13 flat,14 off  (dr_fu flat/off identical to smile per PRD "does not corrupt")
-- 15 birdA (mouth closed), 16 birdB (mouth open)  -- twin birds, 2-frame
-- 17 grandpa-smile,18 flat,19 off
-- 20..24 extras (squirrel/hedgehog loop frame1, frame2)
-- Produces assets/sprites/npc.png
local FW = 16
local FH = 24
local N = 25
local W = FW*N
local H = FH
app.command.NewFile{ width=W, height=H, colorMode=ColorMode.RGB }
local spr = app.activeSprite
local img = app.activeImage
for y=0,H-1 do for x=0,W-1 do img:drawPixel(x,y, app.pixelColor.rgba(0,0,0,0)) end end
local function R(ox,oy,w,h,c) for y=oy,oy+h-1 do for x=ox,ox+w-1 do img:drawPixel(x,y,c) end end end
local function px(x,y,c) img:drawPixel(x,y,c) end

local eye  = app.pixelColor.rgba(56,40,40,255)
local mouth= app.pixelColor.rgba(200,110,110,255)
local cheek= app.pixelColor.rgba(255,170,170,255)

-- draw a standing NPC at ox,oy with body color, species ears, expression
local function npc(ox, oy, body, ear, expr)
  -- head
  R(ox+5, oy+1, 6, 6, body)
  -- ears (simple)
  if ear == "rabbit" then R(ox+6,oy-2,1,4,body); R(ox+9,oy-2,1,4,body)
  elseif ear == "dog" then R(ox+4,oy+1,1,3,body); R(ox+11,oy+1,1,3,body); R(ox+3,oy+3,1,4,body); R(ox+12,oy+3,1,4,body)
  elseif ear == "bear" then R(ox+4,oy+0,2,2,body); R(ox+10,oy+0,2,2,body)
  elseif ear == "cat" then px(ox+5,oy+1,body);px(ox+4,oy+0,body); px(ox+10,oy+1,body);px(ox+11,oy+0,body)
  elseif ear == "deer" then px(ox+6,oy-1,body);px(ox+6,oy+0,body); px(ox+9,oy-1,body);px(ox+9,oy+0,body)
  elseif ear == "turtle" then R(ox+5,oy+0,6,2,body) -- shell cap on head
  end
  -- eyes
  if expr == "smile" then
    px(ox+6, oy+3, eye); px(ox+9, oy+3, eye)
    px(ox+7, oy+5, mouth); px(ox+8, oy+5, mouth)
    px(ox+5, oy+4, cheek); px(ox+10, oy+4, cheek)
  elseif expr == "flat" then
    px(ox+6, oy+3, eye); px(ox+9, oy+3, eye)
    px(ox+7, oy+5, mouth)
  elseif expr == "off" then
    px(ox+6, oy+3, eye); px(ox+9, oy+4, eye) -- one eye 1px lower
    px(ox+7, oy+5, mouth); px(ox+8, oy+5, mouth)
  end
  -- torso
  R(ox+4, oy+8, 8, 8, body)
  -- arms
  R(ox+3, oy+9, 1, 5, body); R(ox+12, oy+9, 1, 5, body)
  -- legs
  R(ox+5, oy+16, 2, 6, body); R(ox+9, oy+16, 2, 6, body)
  px(ox+5, oy+22, eye); px(ox+6, oy+22, eye); px(ox+9, oy+22, eye); px(ox+10, oy+22, eye)
end

local dogC  = app.pixelColor.rgba(200,150,90,255)
local rabC  = app.pixelColor.rgba(245,235,230,255)
local bearC = app.pixelColor.rgba(150,100,60,255)
local catC  = app.pixelColor.rgba(230,220,210,255)
local deerC = app.pixelColor.rgba(210,160,110,255)
local turC  = app.pixelColor.rgba(120,160,90,255)
local sqC   = app.pixelColor.rgba(170,110,70,255)
local hedC  = app.pixelColor.rgba(150,110,70,255)

-- afu dog
npc(0*FW, 0, dogC, "dog", "smile")
npc(1*FW, 0, dogC, "dog", "flat")
npc(2*FW, 0, dogC, "dog", "off")
-- yuanyuan rabbit
npc(3*FW, 0, rabC, "rabbit", "smile")
npc(4*FW, 0, rabC, "rabbit", "flat")
npc(5*FW, 0, rabC, "rabbit", "off")
-- dabao bear (with postman bag: add a small brown satchel)
npc(6*FW, 0, bearC, "bear", "smile"); R(6*FW+10, 10, 3, 4, app.pixelColor.rgba(120,80,40,255))
npc(7*FW, 0, bearC, "bear", "flat"); R(7*FW+10, 10, 3, 4, app.pixelColor.rgba(120,80,40,255))
npc(8*FW, 0, bearC, "bear", "off"); R(8*FW+10, 10, 3, 4, app.pixelColor.rgba(120,80,40,255))
-- mimi cat
npc(9*FW, 0, catC, "cat", "smile")
npc(10*FW, 0, catC, "cat", "flat")
npc(11*FW, 0, catC, "cat", "off")
-- dr_fu deer (smile only effectively)
npc(12*FW, 0, deerC, "deer", "smile")
npc(13*FW, 0, deerC, "deer", "smile")
npc(14*FW, 0, deerC, "deer", "smile")
-- twin birds (2 frames): bird A mouth closed, bird B mouth open
R(15*FW+6, 6, 4, 4, app.pixelColor.rgba(120,200,230,255)); px(15*FW+7,7,eye); px(15*FW+10,7,eye) -- closed
R(16*FW+6, 6, 4, 4, app.pixelColor.rgba(120,200,230,255)); px(16*FW+7,7,eye); px(16*FW+10,7,eye); px(16*FW+8,9,mouth); px(16*FW+9,9,mouth) -- open beak
-- grandpa turtle
npc(17*FW, 0, turC, "turtle", "smile")
npc(18*FW, 0, turC, "turtle", "flat")
npc(19*FW, 0, turC, "turtle", "off")
-- extras (5): squirrels/hedgehogs with 2 walk frames each would be ideal; provide standing + step
npc(20*FW, 0, sqC, "dog", "smile")
npc(21*FW, 0, hedC, "dog", "smile")
npc(22*FW, 0, sqC, "dog", "smile")
npc(23*FW, 0, hedC, "dog", "smile")
npc(24*FW, 0, sqC, "dog", "smile")

local out = "assets/sprites/npc.png"
spr:saveCopyAs(out)
print("Saved " .. out)
app.exit()
