CCCPaste
--MobFun V1.01
--written in 10/2025 by Kay Michaelsen
--
--vardef
--
monitor = {}
monitor[1] = peripheral.wrap("monitor_36")

aMoni = #monitor

--gate = peripheral.wrap("advanced_crystal_interface_2")
--trans = peripheral.wrap("transceiver_0")

--

l = 0
m = 0
atext = ""
ax = 0
dPage = 1
dMax = 0

gName = {}
gLine = {}
gStat = {}

aMobs = 0


function MobsDefault(fLesen)

    gName[1] = "Spider"
    gLine[1] = colors.red

    gName[2] = "Skeleton"
    gLine[2] = colors.orange

    gName[3] = "Enderman"
    gLine[3] = colors.magenta

    gName[4] = "Wither Skeleton"
    gLine[4] = colors.blue

    gName[5] = "Zombie"
    gLine[5] = colors.lime

    gName[6] = "Creeper"
    gLine[6] = colors.lightBlue

	gName[7] = "Smasher"
	gLine[7] = colors.cyan


    aMobs = #gName

    MobsSchreiben(true)

end


function MobsLesen(fLesen)

    local fName = "mobsdef.dat"

    local vars = {}
    local line = ""

    datei = fs.open(fName, "r")

    if not (datei == nil) then

        local tableString = datei.read()
        while not(line == nil) do
            line = datei.read()
            if line == nil then
                break
            end
            tableString = tableString..line
        end
        datei.close()

        vars = textutils.unserialise(tableString)

        aMobs = vars.aMobs
        gName = vars.gName
        gLine = vars.gLine

    else
        print("Fehler: Datei nicht lesbar")
    end
end

function MobsSchreiben(fLesen)

    local fName="mobsdef.dat"

    local vars = {}

    vars.aMobs = aMobs
    vars.gName = gName
    vars.gLine = gLine

    local tableString = textutils.serialise(vars)
    datei = fs.open(fName, "w")
    datei.write(tableString)
    datei.close()

end


function Monitore(fLesen)

    for i = 1, aMoni do
        MonAufbau(monitor[i])
    end

end

function UserCommand( xpos, ypos)

    local gNr = 0

    print("UC" ,xpos ,ypos)
    if ypos == 1 then
        if (xpos<5) then
            --prev page
            if dPage > 1 then
                dPage = dPage -1
            end
        elseif (xpos>7) and (xpos<12) then
            if dPage < dMax then
                dPage = dPage +1
            end
        end
    elseif (ypos == 3) or (ypos == 6) or (ypos == 9) or (ypos == 12) or (ypos == 15) or (ypos == 18) or (ypos == 21) then
        if (xpos >= 37) and (xpos <= 45) then
            gNr = (dPage - 1) * 7 + ((ypos) / 3)
			if gStat[gNr] == true then
				gStat[gNr] = false
			else
				gStat[gNr] = true
			end
			setOutput(true)
        end
    end

    Monitore(true)

end

function startMonitors(fLesen)

    local i = 0
    local fmoni = monitor[1]

    for i = 1, (aMoni) do
        if not (monitor[i] == nil) then
            fmoni = monitor[i]
            fmoni.setTextScale(0.5)
            fmoni.setBackgroundColor(colors.black)
            fmoni.clear()
            fmoni.setBackgroundColor(colors.red)
            fmoni.setTextColor(colors.white)
            fmoni.setCursorPos(2,2)
            fmoni.write(" Initialisiere Systeme ... ")
        end
    end
    sleep(2)

end

function MonAufbau(moni)

    local i = 0
    local k = 0
    local pr = 0
    local py = 0

    if not(moni == nil) then
        moni.setTextScale(0.5)
        moni.setBackgroundColor(colors.black)
        moni.clear()
        moni.setBackgroundColor(colors.blue)
        moni.setTextColor(colors.orange)
        moni.setCursorPos(15,1)
        moni.write(" *** Wuffelz Fun mit Mobs *** ")

        moni.setCursorPos(1,1)
        if dPage > 1 then
            moni.setBackgroundColor(colors.orange)
            moni.setTextColor(colors.black)
        else
            moni.setBackgroundColor(colors.black)
            moni.setTextColor(colors.orange)
        end
        moni.write(" << ")

        moni.setCursorPos(6,1)
        moni.setBackgroundColor(colors.black)
        moni.setTextColor(colors.orange)
        moni.write(string.format("%1d", dPage))

        moni.setCursorPos(8,1)
        if dPage < dMax then
            moni.setBackgroundColor(colors.orange)
            moni.setTextColor(colors.black)
        else
            moni.setBackgroundColor(colors.black)
            moni.setTextColor(colors.orange)
        end
        moni.write(" >> ")

        for i = 1,7 do
            k = (i-1) * 3 + 1
            moni.setBackgroundColor(colors.black)
            moni.setTextColor(colors.blue)
            py = k + 2
            pr = ((dPage -1) * 7) + i

            if pr <= aMobs then
                moni.setCursorPos(1,py)
                moni.write(gName[pr])
                moni.setCursorPos(25,(py))
				if gStat[pr] == true then
					moni.setBackgroundColor(colors.black)
					moni.setTextColor(colors.orange)
					moni.write("active  ->")
					moni.setCursorPos(37,(py))
					moni.setBackgroundColor(colors.orange)
					moni.setTextColor(colors.blue)
					moni.write(" stop ")
				else
					moni.setBackgroundColor(colors.black)
					moni.setTextColor(colors.orange)
					moni.write("inactive  ->")
					moni.setCursorPos(39,(py))
					moni.setBackgroundColor(colors.orange)
					moni.setTextColor(colors.blue)
					moni.write(" start ")
				end
			end

		end


    end

end

function setOutput(lesen)

	local lauf = 0
	local bund = 0

	for lauf = 1, aMobs do
		if gStat[lauf] == true then
			bund = bund + gLine[lauf]
		end
	end

	redstone.setBundledOutput("bottom", (bund))

end


--Main

startMonitors(true)
MobsLesen(true)

if (aMobs < 1) then
    MobsDefault(true)
end

aMobs = #gName

dMax = math.floor(aMobs / 7) +1
dPage = 1

print(aMobs, dMax)

for z = 1, aMobs do

	gStat[z] = false

end

redstone.setBundledOutput("bottom", 0)


while true do

    Monitore(true)

    event, button, x, y = os.pullEventRaw()

    if event == "terminate" then
        break
    elseif event == "redstone" then
        --neuer Durchlauf
    elseif (event == "monitor_touch") and (button == "monitor_36") then
        UserCommand( x, y)
    elseif (event == "monitor_touch") and (button == "monitor_63") then
        UserCommand( x, y)
    else
        print(event, button, x, y)
		print(" ")
    end

end