-- Events


local events = {

SCRZ0100 = [[
cecho "Riddle Jungle...\The most uncanny place on earth.\All native population decimated\by unknown reasons...\This place is haunted by loneliness."
]],

SCRZ0101 = [[
cecho "You must collect the\7 precious stones\to activate the temple."
]],

SCRZ0102 = [[
cecho "CREDITS:\Mod by Hendrix\Special thanks to everyone who\supported this project!\Thanks to:\- LJ Sonic\- Dark-Hunter"
]],

SCRZ0103 = [[
cecho "$redRiddle Jungle\\\\Habitants: loneliness...\\\Rest in peace ancient denizens."
]],

SCRZ0104 = [[
cecho "$redHorus Mansion\The mansion of the masonry!\Where the kings of monarchy\used to live.\Corruption and dihosnesty's home."
]],

SCRZ0129 = [[
cecho "$redStone O' Apocalypse\Ancient denizens believed that this stone\had been given by the gods.\They said this stone gave them\the power to see the future.\
]],

SCRZ0137 = [[
cecho "$blueSacred Garden\This was where missionaries and religious\preached their religion and testimonies.\The children were baptized in this lake\known as Genesis Lake."
]],

SCRZ0135 = [[
color white
echo You are now blessed in the name of god by the Genesis Lake!
]],

SCRZ0200 = [[
cecho "Psalm 147:3\He heals the brokenhearted\and binds up their wounds."
]],

SCRZ0201 = [[
cecho "$redPsalm Church\Masonry manipulated the Catholic church.\All Catholic concepts were stalled\to manipulate the people.\Leading the population to decay.\The money bought the faith..."
]],

SCRZ0116 = [[
cecho "$redThe Pyramids Triangle\Nobody knows about how ancients\constructed the three pyramids\without advanced technology.\Old reports say there is paranormal\activity around here."
]],

SCRZ0191 = [[
cecho "$redBaphomet's Castle\Monarchy was rooted Satanism and Masonry.\This castle is where the\$redevil things\happenned...\Christians were persecuted and\brutally killed here."
]],

SCRZ0192 = [[
cecho "$redKilled Angels Cemetery\Only the nobility was able to\get their bodies buried here.\Commoners were buried into their\own gardens."
]],

SCRZ0193 = [[
cecho "$redHorus Community\Here rituals and sacrifices\were carried out by Masonic priests."
]],

SCRZ0199 = [[
cecho "You have sucessfully activated\$blueTHE TEMPLE\\\$redBaphomet's Castle is now\*UNLOCKED*"
]],

SCRZ0222 = [[
MAP MAPZ1 -FORCE
]],

SCRZ0223 = [[
cecho "You are now entering\$redBaphomet's Castle\The dark side of monarchy lives here..."
]],

SCRZ0800 = [[
cecho "The ritual must be completed..."
]],

SCRZ0801 = [[
cecho "Trigger activated"
]],

SCRZ0802 = [[
cecho "Trigger activated"
]],

SCRZ0803 = [[
cecho "My name is\$redEyelon!\This castle is full of unknown secrets...\I will be watching you!"
]],

SCRZ0804 = [[
cechoduration 5
cecho "I am the king of this room!\My name is Puzzeye!\I can see EVERYTHING...\If you want to go ahead, you must\to solve my puzzle!\$redN S W E"
]],

SCRZ0805 = [[
cecho "I AM NOT SCHIZOPHRENIC!\I AM NOT SCHIZOPHRENIC!"
]],

SCRZ0806 = [[
cecho "PLEASE, I NEED SOME COLLYRIUM!\I DONT BLINK MY EYES SINCE BEFORE CHRIST!"
]],

SCRZ0807 = [[
cecho "Break the crack!\So you will find what is missing..."
]],

SCRZ0810 = [[
cecho "I want to hear some piano music!\Hit 5 notes to me, please."
]],

SCRZ0811 = [[
cecho "Thanks for playing!\Mod by Hendrix\Special thanks to:\- LJ Sonic\- Dark Hunter\-*BreakXable*\-*SonicX*\-Solidus"
]],

SCRZ0815 = [[
MAP MAPZ3 -FORCE
]],

SCRZ0900 = [[
cecho "WORK IN PROGRESS..."
echo "WORK IN PROGRESS! WAIT FOR THE NEXT UPDATE. -HENDRIX"
]],

SCRZ0901 = [[
cecho "Welcome to Las Cruces!\Please respect the nature."
]],

SCRZ0902 = [[
cecho "This is Las Cruces' Church.\The only church that was not corrupted."
]],

SCRZ0903 = [[
cecho "To access the Monk's Cave you\need to find and talk to Shalavar."
echo Find Shalavar, so he will give you the access to the cave.
]],

SCRZ0906 = [[
cecho "The Golden Cave.\It was an old gold mine abandoned due to\$greenradioactivity"
]],

SCRZ0904 = [[
echo "To access the Las Cruces Church's secret room you need to find the trigger."
]],

SCRZ0905 = [[
cecho "$green*EASTER EGG*\The prayer room"
echo "You can now access the Las Cruces' Church secret room"
]],

SCRZ0907 = [[
cecho "This statue is called Angel Of Mercy.\The population believes it keeps them from\$redthe apocalypse"
]],

SCRZ0908 = [[
cecho "Hello! I am Shalavar.\What do you want, foreigner?"
wait 30
echo "<You> I need you to open the Monk's Cave gate, please."
wait 100
cecho "Oh, right! Done.\See you later, foreigner."
echo "You can now access the Monk's Cave!"
]],

SCRZ0909 = [[
echo "To access Old Gold Mine you need to find the trigger to open the gate."
]],

SCRZ0910 = [[
cecho "$green*EASTER EGG*\Gold Digger"
echo "You can now access Old Gold Mine!"
]],

SCRZ0911 = [[
cecho "Hm? Who are you?"
wait 30
echo "<You> I was sent by Shalavar. "
wait 200
cecho "Oh, I remember now!\You are the foreigner..."
wait 30
echo "<You> Hm... yes, he called me like that."
wait 200
cecho "Okay then. I am Monk!\I take care of Las Cruces.\
wait 300
cecho "I have contact with the supernatural...\And I can see the future."
wait 300
cecho "Bad things will happen!\Angel Of Mercy statue is\losing its power..."
wait 300
cecho "The Apocalypse will happen soon..."
wait 200
MAP MAPZ4 -FORCE
]],

SCRZ0915 = [[
cecho "Riddle Jungle is divided into 2 areas:\Riddle Jungle and Las Cruces."
wait 300
cecho "A century ago, a curse was lauched\against Riddle Jungle."
wait 300
cecho "The reason for the curse was\the corruption of the monarchy."
wait 300
cecho "Satanism and freemasonry\manipulated the church\through monarchy. "
wait 300
cecho "During this time\Christian constructed Las Cruces."
wait 300
cecho "But now, after one hundred years\the curse is over."
wait 200
cecho "Riddle Jungle is now free!"
wait 100
MAP MAPZ2 -FORCE
]],

SCRZ0920 = [[
cecho "Go to Riddle Jungle\$redCHAPTERS 1-3"
]],

SCRZ0921 = [[
MAP MAPZ0 -FORCE
]],

SCRZ0940 = [[
cecho "Find and talk to Shalavar!\He will tell you what to do."
echo "Shalavar vanishes, find him!"
]],

SCRZ0941 = [[
cecho "$green*EASTER EGG*\Entering the mothership"
]],

SCRZ0942 = [[
cecho "You found me!\I was here praying all the time."
wait 200
cecho "Then the Monk's prophecy came true!\The apocalypse arrived."
wait 300
cecho "You need to find the way to\El Infierno's Mansion!"
wait 200
cecho "They kidnapped Monk.\Someone must save him!"
wait 300
cecho "He is probably there!"
wait 200
cecho "Good luck on your journey!"
echo "Find the way to El Infierno's Mansion and save Monk!"
]],

SCRZ0943 = [[
cecho "You are now leaving\Las Cruces"
]],

SCRZ0944 = [[
cecho "Do not be welcome to\$redEl Infierno"
]],

SCRZ0945 = [[
cecho "Go to Las Cruces\$redCHAPTERS 4-6"
]],

SCRZ0946 = [[
MAP MAPZ3 -FORCE
]],

SCRZ0947 = [[
cecho "Go to Ethereal Garden\$blue*BONUS MAP*"
]],

SCRZ0948 = [[
MAP MAPZ5 -FORCE
]],

SCRZ0980 = [[
MAP MAPY0 -FORCE
]],

SCRZ0981 = [[
cecho "Welcome to Ethereal Garden!"
]],

SCRZ0982 = [[
cecho "Get back to Starting Zone"
]],

SCRZ0983 = [[
MAP MAP01 -FORCE
]],

SCRZ0984 = [[
cecho "Welcome to Ethereal Garden!\Bringing the peace between\the galaxies since genesis.\Location: Far, far way..."
]],

SCRZ0985 = [[
cecho "Go to Tierra De Dios\$redFINAL CHAPTER"
]],

SCRZ0986 = [[
MAP MAPZ6 -FORCE
]],

SCRZ0990 = [[
cecho "Oh! Cheers foreigner!\I was just chilling here..."
wait 200
cecho "So here we are: Tierra De Dios!\I really love this place but..."
wait 200
cecho "I miss Las Cruces...\and Shalavar."
wait 200
cecho "Well, hope you enjoy your new life in\$blueTierra De Dios\See you later!"
echo "Congratulations! You completed the mod's Last Chapter. Thanks for playing ~Hendrix"
]],

SCRZ0763 = [[
cecho "$redLetter says:\Las Cruces is now under a curse.\We captured Monk and some locals."
wait 200
cecho "We drained Angel Of Mercy's energy\to ensure full lordship\$redEnd of the page."
wait 50
echo "Find Monk!"
]],

SCRZ0770 = [[
cecho "Thanks, foreigner!"
wait 150
cecho "We don't have time to talk!"
wait 150
cecho "I will lead you to a better place\where we can live in peace."
wait 200
cecho "Let's just get out here! Hurry up!"
wait 150
MAP MAPZ6 -FORCE
]],

SCRZ0771 = [[
cecho "$redNOCLIP SUCKS"
]],

}

// Translate strings into tables for ease of use
do
	local function expandColors(text)
		text = $:gsub("$white", "\x80")
		text = $:gsub("$purple", "\x81")
		text = $:gsub("$yellow", "\x82")
		text = $:gsub("$green", "\x83")
		text = $:gsub("$blue", "\x84")
		text = $:gsub("$red", "\x85")
		text = $:gsub("$gray", "\x86")
		text = $:gsub("$orange", "\x87")

		return text
	end

	local function parseEventLine(line, parsedline)
		if line:sub(1, 7) == 'cecho "'
			parsedline.action = 'cecho'
			parsedline.text = expandColors(line:sub(8, -2):gsub("\\", "\n"))

		elseif line:sub(1, 6) == 'cecho '
			parsedline.action = 'cecho'
			parsedline.text = expandColors(line:sub(7):gsub("\\", "\n"))

		elseif line:sub(1, 5) == 'say "'
			parsedline.action = 'say'
			parsedline.text = line:sub(6, -2)

		elseif line:sub(1, 4) == 'say '
			parsedline.action = 'say'
			parsedline.text = line:sub(5):gsub("\\", "\n")

		elseif line:sub(1, 5) == 'wait '
			parsedline.action = 'wait'
			parsedline.duration = tonumber(line:sub(6))

		elseif line:sub(1, 14) == 'cechoduration '
			parsedline.action = 'cechoduration'
			parsedline.duration = tonumber(line:sub(15))

		elseif line:sub(1, 6) == 'tunes '
			parsedline.action = 'tunes'
			parsedline.music = line:sub(7)

		else
			parsedline.action = "command"
			parsedline.text = line

		end
	end

	local function parseEvent(event, parsedevent)
		local i = 1
		for line in event:gmatch("[^\n]+")
			local parsedline = {}
			parseEventLine(line, parsedline)
			parsedevent[i] = parsedline
			i = $ + 1
		end
	end

	local parsedevents = {}
	for name, event in pairs(events)
		local parsedevent = {}
		parseEvent(event, parsedevent)
		parsedevents[name] = parsedevent
	end
	events = parsedevents
end

local eventstates = {}
local messagestate
local messageduration = 3 * TICRATE

// Function prototypes
local nextEventLine
local endEvent

local function showMessage(text)
	messagestate = {}
	messagestate.line = 0
	messagestate.alpha = 10
	messagestate.time = messageduration
	messagestate.text = text
end

local function executeEventLine(eventstateid)
	local eventstate = eventstates[eventstateid]
	local event = events[eventstate.id]
	local line = event[eventstate.line]

	if line.action == "cecho"
		showMessage(line.text)

	elseif line.action == "say"
		for p in players.iterate
			COM_BufInsertText(p, "say "..line.text)
		end

	elseif line.action == "wait"
		eventstate.duration = line.duration
		return

	elseif line.action == "cechoduration"
		messageduration = line.duration * TICRATE

	elseif line.action == "tunes"
		S_ChangeMusic(line.music)

	else
		for p in players.iterate
			COM_BufInsertText(p, line.text)
		end

	end

	nextEventLine(eventstateid)
end

function nextEventLine(eventstateid)
	local eventstate = eventstates[eventstateid]
	local event = events[eventstate.id]

	eventstate.line = $ + 1	

	// End the event if it was the last line
	if eventstate.line > #event
		endEvent(eventstateid)
		return
	end

	executeEventLine(eventstateid)
end

local function startEvent(id)
	local eventstate = {}
	eventstate.id = id
	eventstate.line = 1
	table.insert(eventstates, eventstate)

	executeEventLine(#eventstates)
end

function endEvent(eventstateid)
	table.remove(eventstates, eventstateid)
end

addHook("ThinkFrame", do
	for i, eventstate in ipairs(eventstates)
		local line = events[eventstate.id][eventstate.line]

		if line.action == "wait"
			eventstate.duration = $ - 1
			if not eventstate.duration
				nextEventLine(i)
			end
		end
	end

	if messagestate
		if messagestate.time // Fade in
			if messagestate.alpha and not (leveltime % 6)
				messagestate.alpha = $ - 1
			end
			messagestate.time = $ - 1
		elseif not (leveltime % 4) // Fade out
			if messagestate.alpha == 9
				messagestate = nil
			else
				messagestate.alpha = $ + 1
			end
		end

		if messagestate and not (leveltime % 6) and messagestate.line < 32
			messagestate.line = $ + 1
		end
	end
end)

addHook("MapLoad", do
	eventstates = {}
	messagestate = nil
end)

addHook("LinedefExecute", function(l)
	startEvent("SCRZ0"..(l.frontside.textureoffset / FRACUNIT))
end, "RIDDLEEVENT")

addHook("NetVars", function(n)
	eventstates = n($)
	messagestate = n($)
	messageduration = n($)
end)

local function drawMessage(v, text, alpha, n)
	local x, y = 160, 160

	for line in text:gmatch("[^\n]+")
		y = $ - 12
	end
	for line in text:gmatch("[^\n]+")
		if not n return end
		n = $ - 1
		v.drawString(x, y, line, V_ALLOWLOWERCASE | (alpha << V_ALPHASHIFT), "center")
		y = $ + 12
	end
end

for _, h in ipairs({"time", "lives", "rankings"})
	hud.disable(h)
end

hud.add(function(v)
	if messagestate
		drawMessage(v, messagestate.text, messagestate.alpha, messagestate.line)
	end
end)