-- Voidy's Upgrades --

rawset(_G, "VoidyUpgrades", {})

freeslot("SPR_VUPG", "S_EUDIALYTE", "MT_EUDIALYTE", "S_SELENITE", 
"MT_SELENITE", "S_CARNELIAN", "MT_CARNELIAN", "S_TOURMALINE", "MT_TOURMALINE", 
"S_TOPAZ", "MT_TOPAZ", "S_GARNET", "MT_GARNET", "S_MOONSTONE", "MT_MOONSTONE")

states[S_EUDIALYTE] = {SPR_VUPG, A|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}
states[S_SELENITE] = {SPR_VUPG, B|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}
states[S_CARNELIAN] = {SPR_VUPG, C|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}
states[S_TOURMALINE] = {SPR_VUPG, D|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}
states[S_TOPAZ] = {SPR_VUPG, E|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}
states[S_GARNET] = {SPR_VUPG, F|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}
states[S_MOONSTONE] = {SPR_VUPG, G|FF_FULLBRIGHT, -1, nil, 0, 0, S_NULL}

mobjinfo[MT_EUDIALYTE] = {
	--$Name Eudialyte Shard
	--$Sprite VUPGA0
	--$Category Voidy Upgrades
	doomednum = 323,
	spawnstate = S_EUDIALYTE,
	spawnhealth = 1,
	height = 16*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}
mobjinfo[MT_SELENITE] = {
	--$Name Selenite of Balance
	--$Sprite VUPGB0
	--$Category Voidy Upgrades
	doomednum = 324,
	spawnstate = S_SELENITE,
	spawnhealth = 1,
	height = 48*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}
mobjinfo[MT_CARNELIAN] = {
	--$Name Carnelian of Strength
	--$Sprite VUPGC0
	--$Category Voidy Upgrades
	doomednum = 325,
	spawnstate = S_CARNELIAN,
	spawnhealth = 1,
	height = 48*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}
mobjinfo[MT_TOURMALINE] = {
	--$Name Tourmaline of Protection
	--$Sprite VUPGD0
	--$Category Voidy Upgrades
	doomednum = 326,
	spawnstate = S_TOURMALINE,
	spawnhealth = 1,
	height = 48*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}
mobjinfo[MT_TOPAZ] = {
	--$Name Topaz of Prosperity
	--$Sprite VUPGE0
	--$Category Voidy Upgrades
	doomednum = 327,
	spawnstate = S_TOPAZ,
	spawnhealth = 1,
	height = 48*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}
mobjinfo[MT_GARNET] = {
	--$Name Garnet of Passion
	--$Sprite VUPGF0
	--$Category Voidy Upgrades
	doomednum = 328,
	spawnstate = S_GARNET,
	spawnhealth = 1,
	height = 48*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}
mobjinfo[MT_MOONSTONE] = {
	--$Name Moonstone of Harmony
	--$Sprite VUPGG0
	--$Category Voidy Upgrades
	doomednum = 329,
	spawnstate = S_MOONSTONE,
	spawnhealth = 1,
	height = 48*FRACUNIT,
	radius = 16*FRACUNIT,
	speed = 0*FRACUNIT,
	deathstate = S_NULL,
	flags = MF_NOGRAVITY|MF_NOCLIPHEIGHT|MF_SPECIAL
}

local TB = CFTextBoxes

local eudialyte1_info = {
	[1] = {
        text = "Obtained a Eudialyte Shard. Extends the Life Force guage by 5%. Frenzy Install can be activated when the gauge is at least 80% full.",
        next = 0
    },
}

local eudialyte2_info = {
	[1] = {
        text = "Obtained all Eudialyte Shards. Life Force guage is extended to maximum value. Frenzy Install can be activated when the gauge is at least 80% full.",
        next = 0
    },
}

local selenite_info = {
	[1] = {
        text = "Obtained the Selenite of Balance. Reduces the consumption rate of Life Force during Frenzy Install by half.",
        next = 0
    },
}
local auto_selenite_info = {
	[1] = {
		text = "Obtained all Combat Enhancement upgrades. Earned the Selenite of Balance as a bonus.",
		next = 2
	},
	[2] = {
        text = "Reduces the consumption rate of Life Force during Frenzy Install by half.",
		script = function(player)
            player.weird = true
        end,
        next = 0
    },
}

local carnelian_info = {
	[1] = {
        text = "Obtained the Carnelian of Strength. Press Spin a fourth time to perform an additional combo finisher.",
        next = 0
    },
}
local auto_carnelian_info = {
	[1] = {
		text = "Obtained all Combat Enhancement upgrades. Earned the Carnelian of Strength as a bonus.",
		next = 2
	},
	[2] = {
        text = "Press Spin a fourth time to perform an additional combo finisher.",
		script = function(player)
            player.weird = true
        end,
        next = 0
    },
}

local tourmaline_info = {
	[1] = {
        text = "Obtained the Tourmaline of Protection. Press Custom 2 on the ground to nullify incoming damage. Also prevents knockback in Basic state, and prevents staggering during Frenzy Install.",
        next = 0
    },
}
local auto_tourmaline_info = {
	[1] = {
		text = "Obtained all Combat Enhancement upgrades. Earned the Tourmaline of Protection as a bonus.",
		next = 2
	},
	[2] = {
        text = "Press Custom 2 on the ground to nullify incoming damage. Also prevents knockback in Basic state, and prevents staggering during Frenzy Install.",
		script = function(player)
            player.weird = true
        end,
        next = 0
    },
}

local topaz_info = {
	[1] = {
        text = "Obtained the Topaz of Prosperity. Doubles the amount of Life Force gained from non-passive sources.",
        next = 0
    },
}
local auto_topaz_info = {
	[1] = {
		text = "Obtained all Combat Enhancement upgrades. Earned the Topaz of Prosperity as a bonus.",
		next = 2
	},
	[2] = {
        text = "Doubles the amount of Life Force gained from non-passive sources.",
		script = function(player)
            player.weird = true
        end,
        next = 0
    },
}

local garnet_info = {
	[1] = {
        text = "Obtained the Garnet of Passion. Releases an explosion of dark energy when transforming into Frenzy Install.",
        next = 0
    },
}
local auto_garnet_info = {
	[1] = {
		text = "Obtained all Combat Enhancement upgrades. Earned the Garnet of Passion as a bonus.",
		next = 2
	},
	[2] = {
        text = "Releases an explosion of dark energy when transforming into Frenzy Install.",
		script = function(player)
            player.weird = true
        end,
        next = 0
    },
}

local moonstone_info = {
	[1] = {
        text = "Obtained the Moonstone of Harmony. Release spin mid-charge to extend attack range with a shadow clone. The clone will perform its own finisher when Voidy performs hers.",
        next = 0
    },
}
local auto_moonstone_info = {
	[1] = {
		text = "Obtained all Combat Enhancement upgrades. Earned the Moonstone of Harmony as a bonus.",
		next = 2
	},
	[2] = {
        text = "Release spin mid-charge to extend attack range with a shadow clone. The clone will perform its own finisher when Voidy performs hers.",
		script = function(player)
            player.weird = true
        end,
        next = 0
    },
}

local already_upgraded = {
	[1] = {
        text = "You already have collected this upgrade.",
		delay = 2*TICRATE,
        next = 0
    },
}

local not_voidy = {
	[1] = {
        text = "This gem is radiating a strange energy. It's probably best that you leave it alone.",
		delay = 2*TICRATE,
        next = 0
    },
}

if not AutoUpgrade
	rawset(_G, "AutoUpgrade", 6)
end

local function CheckAutoUpgrade(player)
	if AutoUpgrade > 0
		if AutoUpgrade == 6 and player.moonstonecollected == 0
			if player.selenitecollected == 1 
			and player.carneliancollected == 1 
			and player.tourmalinecollected == 1 
			and player.topazcollected == 1 
			and player.garnetcollected == 1
				player.moonstonecollected = 1
				player.allupgradesnotif = 1
			end
		elseif AutoUpgrade == 5 and player.garnetcollected == 0
			if player.selenitecollected == 1 
			and player.carneliancollected == 1 
			and player.tourmalinecollected == 1 
			and player.topazcollected == 1 
			and player.moonstonecollected == 1
				player.garnetcollected = 1
				player.allupgradesnotif = 1
			end
		elseif AutoUpgrade == 4 and player.topazcollected == 0
			if player.selenitecollected == 1 
			and player.carneliancollected == 1 
			and player.tourmalinecollected == 1 
			and player.garnetcollected == 1 
			and player.moonstonecollected == 1
				player.topazcollected = 1
				player.allupgradesnotif = 1
			end
		elseif AutoUpgrade == 3 and player.tourmalinecollected == 0
			if player.selenitecollected == 1 
			and player.carneliancollected == 1 
			and player.topazcollected == 1 
			and player.garnetcollected == 1 
			and player.moonstonecollected == 1
				player.tourmalinecollected = 1
				player.allupgradesnotif = 1
			end
		elseif AutoUpgrade == 2 and player.carneliancollected == 0
			if player.selenitecollected == 1 
			and player.tourmalinecollected == 1 
			and player.topazcollected == 1 
			and player.garnetcollected == 1 
			and player.moonstonecollected == 1
				player.carneliancollected = 1
				player.allupgradesnotif = 1
			end
		elseif AutoUpgrade == 1 and player.selenitecollected == 0
			if player.carneliancollected == 1 
			and player.tourmalinecollected == 1 
			and player.topazcollected == 1 
			and player.garnetcollected == 1 
			and player.moonstonecollected == 1
				player.selenitecollected = 1
				player.allupgradesnotif = 1
			end
		end
	end
end

addHook("PlayerThink", function(player)
	if player.mo and player.mo.skin == "voidy"
		if not player.textBox.tree and not player.weird and player.allupgradesnotif == 1
			if AutoUpgrade == 6
				TB:DisplayBox(player, auto_moonstone_info)
			elseif AutoUpgrade == 5
				TB:DisplayBox(player, auto_garnet_info)
			elseif AutoUpgrade == 4
				TB:DisplayBox(player, auto_topaz_info)
			elseif AutoUpgrade == 3
				TB:DisplayBox(player, auto_tourmaline_info)
			elseif AutoUpgrade == 2
				TB:DisplayBox(player, auto_carnelian_info)
			elseif AutoUpgrade == 1
				TB:DisplayBox(player, auto_selenite_info)
			end
			S_StartSound(player.mo, sfx_kc5c)
			player.allupgradesnotif = 0
			saveVoidyData(player)
		end
	end
end)

local function UpgradeCollector(special, toucher)
	if toucher and toucher.skin == "voidy"
		if special.type == MT_EUDIALYTE
			if toucher.player.maxlifeforce < 100
				toucher.player.maxlifeforce = $ + 5
				if toucher.player.maxlifeforce < 100
					TB:DisplayBox(toucher.player, eudialyte1_info)
				end
				if toucher.player.maxlifeforce == 100
					TB:DisplayBox(toucher.player, eudialyte2_info)
				end
				if gamemap == 4 and toucher.player.shard1collected == 0
					toucher.player.shard1collected = 1
				end
				if gamemap == 7 and toucher.player.shard2collected == 0
					toucher.player.shard2collected = 1
				end
				if gamemap == 10 and toucher.player.shard3collected == 0
					toucher.player.shard3collected = 1
				end
				if gamemap == 13 and toucher.player.shard4collected == 0
					toucher.player.shard4collected = 1
				end
				S_StartSound(toucher, sfx_vupitm)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
		if special.type == MT_SELENITE
			if toucher.player.selenitecollected == 0
				toucher.player.selenitecollected = 1
				TB:DisplayBox(toucher.player, selenite_info)
				S_StartSound(toucher, sfx_vupitm)
				CheckAutoUpgrade(toucher.player)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
		if special.type == MT_CARNELIAN
			if toucher.player.carneliancollected == 0
				toucher.player.carneliancollected = 1
				TB:DisplayBox(toucher.player, carnelian_info)
				S_StartSound(toucher, sfx_vupitm)
				CheckAutoUpgrade(toucher.player)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
		if special.type == MT_TOURMALINE
			if toucher.player.tourmalinecollected == 0
				toucher.player.tourmalinecollected = 1
				TB:DisplayBox(toucher.player, tourmaline_info)
				S_StartSound(toucher, sfx_vupitm)
				CheckAutoUpgrade(toucher.player)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
		if special.type == MT_TOPAZ
			if toucher.player.topazcollected == 0
				toucher.player.topazcollected = 1
				TB:DisplayBox(toucher.player, topaz_info)
				S_StartSound(toucher, sfx_vupitm)
				CheckAutoUpgrade(toucher.player)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
		if special.type == MT_GARNET
			if toucher.player.garnetcollected == 0
				toucher.player.garnetcollected = 1
				TB:DisplayBox(toucher.player, garnet_info)
				S_StartSound(toucher, sfx_vupitm)
				CheckAutoUpgrade(toucher.player)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
		if special.type == MT_MOONSTONE
			if toucher.player.moonstonecollected == 0
				toucher.player.moonstonecollected = 1
				TB:DisplayBox(toucher.player, moonstone_info)
				S_StartSound(toucher, sfx_vupitm)
				CheckAutoUpgrade(toucher.player)
				saveVoidyData(toucher.player)
			else
				if not toucher.player.textBox.tree
					TB:DisplayBox(toucher.player, already_upgraded, true)
				end
				return true
			end
		end
	else
		if not toucher.player.textBox.tree
			TB:DisplayBox(toucher.player, not_voidy, true)
		end
		return true
	end
end
addHook("TouchSpecial", UpgradeCollector, MT_EUDIALYTE)
addHook("TouchSpecial", UpgradeCollector, MT_SELENITE)
addHook("TouchSpecial", UpgradeCollector, MT_CARNELIAN)
addHook("TouchSpecial", UpgradeCollector, MT_TOURMALINE)
addHook("TouchSpecial", UpgradeCollector, MT_TOPAZ)
addHook("TouchSpecial", UpgradeCollector, MT_GARNET)
addHook("TouchSpecial", UpgradeCollector, MT_MOONSTONE)

local function UpgradeRemover(mo)
	local p = players[0].mo
	if mo and mo.valid and p and p.valid and p.skin == "voidy"
		if mo.type == MT_EUDIALYTE
			if (gamemap == 4 and players[0].shard1collected == 1) 
			or (gamemap == 7 and players[0].shard2collected == 1) 
			or (gamemap == 10 and players[0].shard3collected == 1) 
			or (gamemap == 13 and players[0].shard4collected == 1)
				mo.state = S_NULL
			end
		end
		if (mo.valid and mo.type == MT_SELENITE and players[0].selenitecollected == 1) 
		or (mo.valid and mo.type == MT_CARNELIAN and players[0].carneliancollected == 1) 
		or (mo.valid and mo.type == MT_TOURMALINE and players[0].tourmalinecollected == 1) 
		or (mo.valid and mo.type == MT_TOPAZ and players[0].topazcollected == 1) 
		or (mo.valid and mo.type == MT_GARNET and players[0].garnetcollected == 1) 
		or (mo.valid and mo.type == MT_MOONSTONE and players[0].moonstonecollected == 1)
			mo.state = S_NULL
		end
	end
end
addHook("MobjThinker", UpgradeRemover, MT_EUDIALYTE)
addHook("MobjThinker", UpgradeRemover, MT_SELENITE)
addHook("MobjThinker", UpgradeRemover, MT_CARNELIAN)
addHook("MobjThinker", UpgradeRemover, MT_TOURMALINE)
addHook("MobjThinker", UpgradeRemover, MT_TOPAZ)
addHook("MobjThinker", UpgradeRemover, MT_GARNET)
addHook("MobjThinker", UpgradeRemover, MT_MOONSTONE)