
addHook("MapLoad", do
    for player in players.iterate
		if player.mo.skin ~= "nasya" then
			continue
		end
			if not multiplayer then //>Implying anyone's ever going to host Nasya in coop.
				if (gamemap == 3 or gamemap == 6 or gamemap == 9 or gamemap == 12 or gamemap == 16 or gamemap == 27) then //Special cutscenes play after these maps
					G_SetCustomExitVars(580) //Set the nextlevel to the cutscene map
					player.lastmap = gamemap //Remember where we came from so we no which cutscene to play when we get there.
				end
				if (gamemap == 580) then
					local spt = 1
					if (player.lastmap == 3) then spt = 2 //Does Lua have switch statements? Oh well
					elseif (player.lastmap == 6) then spt = 3
					elseif (player.lastmap == 9) then spt = 4
					elseif (player.lastmap == 12) then spt = 5
					elseif (player.lastmap == 16) then spt = 6
					elseif (player.lastmap == 27) then 
						spt = 7
						if (All7Emeralds(emeralds)) then
								G_SetCustomExitVars(581, 1)
						else
								G_SetCustomExitVars(1102, 1)
						end
					end
					
					for mt in mapthings.iterate do //Search through the player spawns until we find the correct one for the map we came from
						if mt.type == spt then //In retrospect, I probably should have just done a different map for every cutscene.
							P_TeleportMove(player.mo, mt.x*FRACUNIT, mt.y*FRACUNIT, mt.z*FRACUNIT) 
						end
					end
					
				end
				
			end
			if (gamemap == 581) then
				if not (All7Emeralds(emeralds)) then
					print ("It seems you don't have the power to do that...")
					print ("Perhaps if you completed the game with the Chaos Emeralds...?")
					G_SetCustomExitVars(1100, 2)
					P_DoPlayerExit(player)
				end
			end
    end
end)

addHook("ThinkFrame", function()
	for player in players.iterate do
		
		if player.mo.skin ~= "nasya" then
			continue
		end
		

		if (player.exiting) then
			if (gamemap == 27) then //BCZ3's ending sequence doesn't respect G_SetCustomExit(), so I have to take drastic measures to play Nasya's custom ending
				G_SetCustomExitVars(580)
				G_ExitLevel()
			end
		end
	end
end)
		
function A_RadialFireShot(actor, var1, var2) //Special shooting action for Skymetal's cool jetpack cannon thing
	if not (actor.target) then
		return
	end
	A_FaceTarget(actor, var1, var2)
	
		local flydist = 32
		
		local spawnx = actor.x + FixedMul(flydist*sin(var1), cos(actor.angle+ANGLE_90) )
		local spawny = actor.y + FixedMul(flydist*sin(var1), sin(actor.angle+ANGLE_90) )
		local spawnz = actor.z + flydist*cos(actor.angle) +(32*FRACUNIT) 
	  	
		local starshot = P_SpawnMobj(spawnx, spawny, spawnz, var2)
		starshot.angle = actor.angle
		starshot.target = actor
		P_InstaThrust(starshot, starshot.angle, mobjinfo[starshot.type].speed)

end


function A_ShotsFromHell(actor, var1, var2) //Special shooting action for Starchaser's rising star barrage
	if not (actor.target) then
		return
	end
		local xoffset = actor.target.x + (P_RandomRange(var1*-1, var1)*FRACUNIT)
		local yoffset = actor.target.y + (P_RandomRange(var1*-1, var1)*FRACUNIT)
		if (R_PointToDist2(actor.x, actor.y, xoffset, yoffset)  > 256*FRACUNIT) then
			local starshot = P_SpawnMobj(xoffset, yoffset, FRACUNIT, var2)
			starshot.target = actor
		end

end

function A_ScriptPain(actor, var1, var2) //Calls a linedef executer based on remaining HP, and then A_Pain
	P_LinedefExecute(29+(28-actor.health),actor.target)
	
	if (actor.health > 20) then
		actor.tics = 240
	end
	A_Pain(actor, var1, var2)
end


addHook("LinedefExecute", function(line, mobj, sector)
    for player in players.iterate
		if player.mo.skin ~= "nasya" then
			continue
		end
		player.mo.state = S_PLAY_NASYA_TRANSFORM
		player.transtime = 1
		player.twirling = 0
		player.rings = 50
	end


end, "NATRAN")