0  ,]']±>Sï}WÿÍu§ðp8·d%qy)6o;]ÉA¦ösï÷ôôô”°ÂVl†3<W"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ÌÌ ÀÌ Ì ÌÌ ÀÌ ÌÀ  ÌÌÀÌÌÀÀÀÌ  ÌÌÌÌ ÌÌÀÌÀÌ ÌÌ                                                                       Ì Ì ÌÌ Ì ÌÌ  ÀÀ ÀÀ Ì   Ì  Ì À Ì Ì Ì                                                                       Ì Ì ÌÌÌ Ì ÌÌ  ÀÀÌ Ì Ì   ÌÌ Ì À Ì Ì Ì                                                                       ÌÌ ÌÌÌÀÌ Ì   ÀÀ ÀÀ Ì   Ì  Ì À Ì Ì ÌÌ                                                                       Ì Ì Ì ÌÌ ÀÌ    ÀÀÌÌÀÀ Ì   ÌÌÌÌ ÌÌ Ì ÀÌ Ì                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ÀÌÀÌÀÌÌ ÌÌ ÌÌ   Ì  ÀÌÀÌ   ÌÌ ÀÌ Ì ÌÌ ÀÌ ÌÀÌÌÀÌÌÀÌÌ  ÀÌÌ ÌÌ  ÌÌÀÌÌÀÀÀÌ  ÀÌ ÌÌ  ÀÌÌ ÌÌ  ÀÌ Ì ÌÌ ÀÌÀÌ ÌÌ   ÀÀÀÀÀ ÀÌ ÀÌ   ÀÀ   Ì Ì   Ì Ì ÌÌ Ì ÌÌÀ ÀÀ    Ì Ì Ì    ÀÀ ÀÀ Ì   Ì À   Ì Ì Ì    Ì Ì Ì Ì Ì Ì   ÀÀÀÀÀÌ Ì Ì  ÀÀ   Ì Ì   Ì Ì ÌÌÌ Ì ÌÌÀ Ì ÀÌ   Ì ÌÌÌÌ   ÀÀÌ Ì Ì   Ì À   Ì ÌÌÌÌ   Ì  Ì Ì ÀÌ Ì Ì   ÀÌÀÌÀ  ÀÌ ÀÌ  ÀÌÌ   Ì Ì   ÌÌ ÌÌÌÀÌ Ì ÀÀ À    Ì Ì Ì    ÀÀ ÀÀ Ì   ÌÌÀ   Ì Ì Ì    Ì Ì ÌÌ  ÌÌ ÌÌ   À ÀÀÀÌÌÀÌÀÌ  ÀÀ   Ì ÀÌ   Ì Ì Ì ÌÌ ÀÌ  ÌÌÀÌÌÀÌÌ   Ì Ì ÌÌ  ÀÀÌÌÀÀ Ì   Ì À   Ì Ì ÌÌ  ÀÌ ÀÌ Ì ÌÌ ÀÌ Ì                                                                                                                                           ÌÌ   ÌÌ ÀÌ ÌÌ   ÌÌ Ì  ÀÌ ÌÀÀÀÌÌ  ÀÌÌ ÌÌ  ÀÌ Ì ÌÌ ÀÌÀÌ ÌÌ   ÀÌ ÌÌ ÀÌ Ì Ì ÌÌ                                   Ì   Ì Ì Ì   ÀÀÀ  ÀÌÌÀÀÀÀÀ    Ì Ì Ì    Ì Ì Ì Ì Ì Ì   Ì Ì Ì Ì ÌÌ                                   Ì ÌÌ Ì Ì   ÀÀÀ  ÀÌÌÀÀÀÀÀÌ   Ì ÌÌÌÌ   Ì  Ì Ì ÀÌ Ì Ì   Ì Ì Ì Ì ÌÌÌ                                   Ì   ÌÌ ÌÌÌ   ÀÀÀ  ÀÀÀÀÀ ÌÀ    Ì Ì Ì    Ì Ì ÌÌ  ÌÌ ÌÌ   ÌÌÌÌ Ì Ì ÌÀÌ                                   ÌÌ   Ì  Ì ÌÌ   À Ì  À À Ì À ÀÌÌ   Ì Ì ÌÌ  ÀÌ ÀÌ Ì ÌÌ ÀÌ Ì   Ì Ì ÀÌ ÀÌ Ì ÌÌ                                                                                                                                                               ÀÌÀÌÀÌÌ ÌÌ ÌÌ  ÀÌ  ÀÌÀÌ   ÌÌ Ì ÌÌ ÌÌÀ ÀÌÌ  ÀÌÌ ÌÌ  ÀÌ Ì ÌÌ ÀÌÀÌ ÌÌ                                           ÀÀÀÀÀ ÀÌ ÀÌ   ÀÀ   Ì Ì   ÀÀÀÀ À À À    Ì Ì Ì    Ì Ì Ì Ì Ì Ì                                           ÀÀÀÀÀÌ Ì Ì  ÀÌ   Ì Ì   ÀÀÀÀÌÀÌÀ ÀÌ   Ì ÌÌÌÌ   Ì  Ì Ì ÀÌ Ì Ì                                           ÀÌÀÌÀ  ÀÌ ÀÌ  ÀÀ   Ì Ì   ÀÀÀÀÀÀÀÀ À    Ì Ì Ì    Ì Ì ÌÌ  ÌÌ ÌÌ                                           À ÀÀÀÌÌÀÌÀÌ  ÀÌ   Ì ÀÌ   À Ì ÌÌ ÌÌÀÌÌÀÌÌ   Ì Ì ÌÌ  ÀÌ ÀÌ Ì ÌÌ ÀÌ Ì                                                                                                                                                           ÀÌÀÌÀÌÌ ÌÌ ÌÌ  ÀÀ  ÀÌÀÌ   ÌÌ ÌÌ ÌÌÀÀ ÌÌ  ÀÌÌ ÌÌ ÌÌ  ÀÀÀÌÌÀ ÀÌ  ÀÌ Ì ÌÀÀ  ÀÀÀÌ                           ÀÀÀÀÀ ÀÌ ÀÌ   ÀÀ   Ì Ì   Ì Ì ÀÀÌÀÀ    Ì Ì ÀÀÌ   ÀÀÀ À ÀÀ  ÀÀÀÀÀÀÀ  ÀÀÀÀ                           ÀÀÀÀÀÌ Ì Ì   Ì   Ì Ì   ÌÌ Ì ÀÀÌÌÀÌ   Ì ÌÌÀ Ì  ÀÌÌÀÌÀ ÀÀ  ÀÌÀÀÀ ÀÌ   ÀÀÀÀ                           ÀÌÀÌÀ  ÀÌ ÀÌ  ÀÀ   Ì Ì   Ì ÌÌ ÀÀÌÀÀ   Ì Ì À ÀÌ  ÀÀÀ À ÀÌ  ÀÀÀÌÌÀÀÀ  ÀÀÀÌ                           À ÀÀÀÌÌÀÌÀÌ  ÀÀ   Ì ÀÌ   ÌÌ Ì ÌÌÀÀ ÌÌ   Ì Ì ÌÌÀÌ  ÀÀÀÌÌÀÌÌÀ   ÀÌÀÀ ÌÀÀ   ÌÀ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ÀÌÀÌÀÌÌ ÌÌ ÌÌ   Ì  ÀÌÀÌ   ÌÌÌ ÌÌÀÌ  ÌÌÀÀÀÌÀÌ  ÌÀÌÌÌ Ì                                                     ÀÀÀÀÀ ÀÌ ÀÌ   ÀÀ   Ì Ì   Ì  Ì À Ì   ÀÀÀ Ì Ì   ÌÌÌ  ÌÌ                                                     ÀÀÀÀÀÌ Ì Ì  ÀÀ   Ì Ì   ÌÌ ÀÌ À Ì   ÀÀÌÌ Ì ÀÌ   ÌÌÌÌ ÌÌÌ                                                     ÀÌÀÌÀ  ÀÌ ÀÌ  ÀÌÌ   Ì Ì   Ì  Ì À Ì   ÀÀÀ Ì  Ì  Ì  ÌÀÌ                                                     À ÀÀÀÌÌÀÌÀÌ  ÀÀ   Ì ÀÌ   ÌÌÌ ÌÌ Ì   ÀÀÀÀÌÌÌ    ÌÌÌ ÀÌF -- title:   random text editor
-- author:  khuxkm
-- desc:    a text editor but you can only replace the character at a specific location with a randomly chosen character
-- license: MIT License
-- version: 0.1.0
-- script:  lua
-- saveid: randomtexteditor

-- PMEM enhancement pack (PEP)
-- one liner
(function()local abn5,AvK=string.char,string.byte local function rhVu()local U=""for _u=0,1023 do U=U..abn5(peek(0x14004+_u))end;return U end local function ngzOjWHO(aLgiy) assert(#aLgiy<=1024,"persistent memory buffer overflow!")for mvi=0,#aLgiy-1 do poke(0x14004+mvi,AvK(aLgiy:sub(mvi+1,mvi+1)))end end;local dM={}dM._format=("I4"):rep(256)dM._loadstate=function()dM._state={string.unpack(dM._format,rhVu())}dM._state[#dM._state]=nil end;dM._loadstate()dM._savestate=function()ngzOjWHO(string.pack(dM._format,table.unpack(dM._state)))dM._dirty=nil end;dM.__index=function(g4KV,dT7iYDf4)if type(dT7iYDf4)=="number"and math.type(dT7iYDf4)=="integer"then return g4KV(dT7iYDf4)end;if dT7iYDf4:sub(1,4)=="_fix"then return function(L)return L end end;if dT7iYDf4 =="batch"then return g4KV._batch end;if dT7iYDf4 =="save"then return g4KV._savestate end end;dM.__newindex=function(WRH9,cJoBcud,e)if type(cJoBcud)=="number"and math.type(cJoBcud)=="integer"then dM._state[cJoBcud+1]=e;dM._dirty=true end;if cJoBcud=="batch"then if e then WRH9._batch=true else WRH9._batch=nil WRH9._savestate()end end if cJoBcud=="format"then if not WRH9.ignoresize then _,sz=assert(pcall(string.packsize,e))assert(sz<=1024,"format string too large!")end;if WRH9.batch then WRH9.batch=false end;if WRH9._dirty then WRH9._savestate()end;WRH9._format=e;WRH9._loadstate()end if cJoBcud=="refresh"then if WRH9.batch or WRH9._dirty then error("can't refresh when batched/dirty")end;WRH9._loadstate()end end dM._fixstring=function(B6zKxgVs)while B6zKxgVs:sub(-1,-1)=="\000"do B6zKxgVs=B6zKxgVs:sub(1,-2)end;return B6zKxgVs end dM.__call=function(O3_X,DVs8kf2w,vms5) if dM._dirty and not dM._batch then dM._savestate()end;if(DVs8kf2w+1)>#dM._state or(type(DVs8kf2w)~="number"or math.type(DVs8kf2w)~="integer")then error("invalid persistent tic index")end;lastval=dM._state[DVs8kf2w+1] lastval=dM["_fix"..type(lastval)](lastval) if vms5 ~=nil then dM._state[DVs8kf2w+1]=vms5;dM._dirty=true;if not dM._batch then dM._savestate()end end;return lastval end;_G.pmem=setmetatable(dM,dM)end)()

-- set format (22 rows of 40 characters, 4 uint32s for RNG state, 2 uint8s for position x/y, 1 uint8 for cursor state
pmem.format = ("c40"):rep(22).."I4I4I4I4".."BBB"

local display_help = false

local CONSTANTS = {}
CONSTANTS.ROWS = 22
CONSTANTS.RNG_STATE = CONSTANTS.ROWS
CONSTANTS.CX = CONSTANTS.RNG_STATE + 4
CONSTANTS.CY = CONSTANTS.CX+1
CONSTANTS.CSTATE = CONSTANTS.CY+1

local seed_rng = (pmem(CONSTANTS.RNG_STATE)+pmem(CONSTANTS.RNG_STATE+1)+pmem(CONSTANTS.RNG_STATE+2)+pmem(CONSTANTS.RNG_STATE+3))==0

-- if the length of the first string is 0 (the PMEM enhancement pack truncates null bytes), let's fix that
if #pmem(0)==0 then
    -- fill the character space with spaces
    memset(0x14004,32,CONSTANTS.ROWS*40)
    -- reload the state
    pmem.refresh=true
    -- set the cursor to 0,0 and the cursor state to on
    pmem(CONSTANTS.CX,0)
    pmem(CONSTANTS.CY,0)
    pmem(CONSTANTS.CSTATE,1)
    -- display the help to start
    display_help = true
    seed_rng = true
end

-- set state
local chars = {}
for i=1,CONSTANTS.ROWS do
    chars[i]=pmem(i-1)
end
local cx = pmem(CONSTANTS.CX)
local cy = pmem(CONSTANTS.CY)
local cstate = pmem(CONSTANTS.CSTATE)

-- RNG
local __uint32limit = (1<<32)-1
local __rotl = function(x,b) return ((x<<b)&__uint32limit)|((x>>(32-b))&__uint32limit) end
function __mulberry32(a) -- mulberry32 used to seed the RNG state
    a=(a+0x6D2B79F5)&__uint32limit
    local t = ((a~(a>>15))*(1|a))&__uint32limit
    t = t + ((((t~(t>>7))*(61|t))~t))&__uint32limit
    return a, t
end
function nextr()
    local s = {}
    for i=0,3 do
        s[i]=pmem(CONSTANTS.RNG_STATE+i)
    end
    local result = (__rotl(s[1]*5,7)*9)&__uint32limit
    local t = (s[1]<<9)&__uint32limit
    s[2]=s[2]~s[0]
    s[3]=s[3]~s[1]
    s[1]=s[1]~s[2]
    s[0]=s[0]~s[3]
    s[2]=s[2]~t
    s[3]=__rotl(s[3],11)
    for i=0,3 do
        pmem(CONSTANTS.RNG_STATE+i,s[i])
    end
    return result
end
-- end RNG

function randomize_char(cx,cy)
    local index=cy+1
    local c=cx+1
    -- bitmask with rejection method
    local mask = 127 -- pre-calculated
    local val
    repeat
        val = nextr()&mask
    until val<=95
    local s = chars[index]
    s = s:sub(1,c-1)..string.char(32+val)..s:sub(c+1,-1)
    chars[index]=s
    pmem(cy,s)
end

local t = 0
function _printc(s,y,c)
    local _w = print(s,0,-6)
    print(s,(240//2)-(_w//2),y,c)
end
function TIC()
	cls(0)
    if display_help then
        _printc("RANDOM TEXT EDITOR",26,12)
        _printc("PRESS A TO RANDOMIZE THE TEXT AT THE CURSOR",38,12)
        _printc("D-PAD TO MOVE THE CURSOR AROUND",44,12)
        _printc("PRESS B TO TOGGLE THE CURSOR",50,12)
        _printc("PRESS X TO BRING THIS HELP BACK UP",56,12)
        _printc("PRESS A TO EXIT THIS MENU",68,12)
        if btn(4) then
            display_help = false
            if seed_rng then
                local a, seedval
                a = tstamp()&__uint32limit
                for i=0,3 do
                    a, seedval = __mulberry32(a)
                    seedval = seedval
                    pmem(CONSTANTS.RNG_STATE+i,seedval)
                end
                -- ensure an all-zero state won't happen
                pmem(CONSTANTS.RNG_STATE,pmem(CONSTANTS.RNG_STATE)|1)
                -- discard the first 5 outputs
                for i=1,5 do nextr() end
                seed_rng=false
            end
        end
    else
        local y = (136//2)-((CONSTANTS.ROWS*6)//2)
        local oy = y
        for index=1,CONSTANTS.ROWS do
            if #(chars[index]:gsub("%s+$",""))>0 then
                print(chars[index]:gsub("%s+$",""),x,y,12,true)
            end
            y=y+6
        end
        if (cstate&1)>0 and ((t//30)%2)>0 then
            rect(cx*6,oy+(cy*6),6,6,2)
        end
        if btnp(0,12,6) and (cstate&1)>0 then cy=(cy-1)%CONSTANTS.ROWS end
        if btnp(1,12,6) and (cstate&1)>0 then cy=(cy+1)%CONSTANTS.ROWS end
        if btnp(2,12,6) and (cstate&1)>0 then cx=(cx-1)%40 end
        if btnp(3,12,6) and (cstate&1)>0 then cx=(cx+1)%40 end
        if btnp(4) and (cstate&1)>0 then randomize_char(cx,cy) end
        if btnp(5) then cstate=cstate~1 end
        if btnp(6) then display_help=true end
        pmem(CONSTANTS.CX,cx)
        pmem(CONSTANTS.CY,cy)
        pmem(CONSTANTS.CSTATE,cstate)
        --print(""..cx..";"..cy,1,1,2)
    end
    t=t+1
end
poke(0x3ff8,14)
