------------------------------------------------------------------------
--Details
------------------------------------------------------------------------
--[[	Map; CommunityMap (8)
	Mapversion; 1.0
	CodeVersion; 1.1
]]
------------------------------------------------------------------------
--Credits
------------------------------------------------------------------------
--[[	Thankyou Relic for the awesome game and easily accessible script and mod possibilities
	Map made by Wargrim, Grabnutz, Ragnarok, Wildcat, Incarnate, Croaxleigh, SickSick6, Argonaut and Akranadas
	Scar coding for Gates by Incarnate 2007 (Jan White)
	Scar coding for Eruptions by Argonaut 2007 (Martin Snape)
	Removed Blood Rain and Snow, causing to many problems
	Last edited 15/6/07 by Argonaut
	edited 1/6/07 by Incarnate
	edited 12/11/07 Akranadas
]]
-------------------------------------------------
--[[ TODO ]]
-------------------------------------------------
--[[
	*fix Animations*
	*sound FX for eruption*
	*flame FX for eruption*
]]
-------------------------------------------------
--[[ IMPORTS ]]
-------------------------------------------------
import("ScarUtil.scar")
import("WXPScarUtil.scar")

-------------------------------------------------
--[[GLOBAL VARIABLES, TABLES]]
-------------------------------------------------
----------------
--[[Variables]]
----------------
--false means raised, true is lowered
g_moving = false
g_gatestatus = false
-------------
--[[tables]]
-------------
-------------------------------------------------
--[[ ON INIT ]]
-------------------------------------------------
function OnInit()
	--incys
	Rule_AddInterval(Rule_Gate, 1)
	--argos
	Rule_AddInterval(Rule_ShakeThatCameraLikeACrazyMan,340)
	Rule_AddInterval(Rule_Erupt,23)
	--Akras
	g_Player1 = World_GetPlayerAt(0)
	Taldeer()
end
Scar_AddInit(OnInit)
-------------------------------------------------
--[[ ON LOAD ]]
-------------------------------------------------
function OnLoad()
	g_Player1 = World_GetPlayerAt(0)
end

Scar_AddLoad(OnLoad)
------------------------------------------------------------------------
--[[MAIN CODE]]
------------------------------------------------------------------------
--raises and lowers the gate for player 1 only
function Rule_Gate()
	if Player_IsAlive(g_Player1) == true then
		if g_moving == false then
			if g_gatestatus == false then
				if Player_AreSquadsNearMarker( g_Player1, "mkr_Main_Gate" ) == true then
					--lower gate
					Anim_PlayEGroupAnim(EGroup_FromName("eg_gate1"), "die")
					Rule_AddOneShot(DeSpawnGate, 7)
					g_moving = true
				end
			elseif g_gatestatus == true then
				if Player_AreSquadsNearMarker( g_Player1, "mkr_Main_Gate" ) == false then
					EGroup_ReSpawn("eg_gate1")
					Anim_PlayEGroupAnim(EGroup_FromName("eg_gate1"), "door_raise")
					g_gatestatus = false
					g_moving = true
					Rule_AddOneShot(EnableAnim, 7)
				end
			end
		end
	else
		if g_gatestatus == true then
			EGroup_ReSpawn("eg_gate1")
			Anim_PlayEGroupAnim(EGroup_FromName("eg_gate1"), "door_raise")
			Rule_Remove(Rule_Gate)
			print("--PRINT: Rule_GateRemoved")
		else
			Rule_Remove(Rule_Gate)
			print("--PRINT: Rule_GateRemoved")
		end
	end
end
function DeSpawnGate()
	EGroup_DeSpawn("eg_gate1")
	g_gatestatus = true
	g_moving = false
end
function EnableAnim()
	g_moving = false
end

-------------------------------------------------------------------------------------------------
--ERUPTION AND CAMERA SHAKE CODE
-------------------------------------------------------------------------------------------------

--Define a great big camera shake :)

function CameraShake_Eruption( epicentre, duration )
	Camera_Shake( 
		epicentre, 
		duration, 
		1.85, 		-- intesity
		260.0, 		-- radius
		0.8			-- falloff
	)
end

function Rule_Erupt()
	Rule_Check_AddInterval(Rule_VolcanicExplosions_1,0.8)
	Rule_Check_AddInterval(Rule_VolcanicExplosions_2,0.8)
	Rule_AddOneShot(Rule_RemoveTheExplosions,18)	
end

function Rule_ShakeThatCameraLikeACrazyMan()
	
	camera_shakepos=Marker_GetPosition(Marker_FromName("mk_epicenter","basic_marker"))
	CameraShake_Eruption(camera_shakepos,20)
	local sound_eruption=Sound_Play("races/imperial_guard/baneblade/big_gun/fire_2")
	print"sound played"
end

function Rule_VolcanicExplosions_1()

	local markername = "mk_epicenter"
	local eventfile = "data:Art/Events/guard/weapons/baneblade_cannon_hit_dirtsand"
	local radius = 2
	local frequency = 3
	
	if 1 < World_GetRand(1, frequency) then
		Deg_RandomAreaFX(markername, eventfile, radius)
	end

end

function Rule_VolcanicExplosions_2()

	local markername = "mk_epicenter"
	local eventfile = "data:Art/Events/guard/weapons/inferno_cannon_area_flame"
	local radius = 2
	local frequency = 3
	
	if 1 < World_GetRand(1, frequency) then
		Deg_RandomAreaFX(markername, eventfile, radius)
	end

end


function Deg_RandomAreaFX (markername, eventfile, radius)

	local t_pos = Marker_GetPosition ( Marker_FromName( markername, "basic_marker" ) );
--	World_GetRandseed(t_pos.x)
	local t_posfinal = t_pos
	t_posfinal.z = t_posfinal.z + World_GetRand ((-1*radius), radius);
	t_posfinal.x = t_posfinal.x + World_GetRand ((-1*radius), radius);
	
	World_FXEvent (eventfile, t_posfinal)
	
	
end

function Rule_RemoveTheExplosions()

	Rule_Remove(Rule_VolcanicExplosions_1)
	Rule_Remove(Rule_VolcanicExplosions_2)
	
end

----------------------------------------------------------------------------------------------------------
--teleports squads code for 666's Island
----------------------------------------------------------------------------------------------------------
function Rule_Teleporters()
	local count = World_GetPlayerCount()
	for i = 0, count -1 do
		local j = (i+1);
		t_Player[j] = World_GetPlayerAt(i)
	end
	for j = 1, count do
		if Player_IsAlive(t_Player[j]) == true then
			for i = 1, 11 do
				if i < 10 then
					Player_GetAllSquadsNearMarker( t_Player[j], "sg_tele", "teleport 0"..i)
					if SGroup_Count("sg_tele") > 0 then
						World_FXEventSquad ("data:art/events/unit_ability_fx/sabotage_caster", "sg_tele")
						World_FXEventSquad ("data:art/events/unit_upgrade_morale_fx/reinforce_chaos_trooper", "sg_tele")
						SGroup_DeSpawn("sg_tele")
						SGroup_ReSpawnMarker("sg_tele", "destination 0"..i)
						World_FXEventSquad ("data:art/events/unit_ability_fx/sabotage_caster", "sg_tele")
						World_FXEventSquad ("data:art/events/unit_upgrade_morale_fx/reinforce_chaos_trooper", "sg_tele")
						SGroup_Clear("sg_tele")
						LocalNotifications(j,i)
					end
				else	
					Player_GetAllSquadsNearMarker( t_Player[j], "sg_tele", "teleport "..i)
					if SGroup_Count("sg_tele") > 0 then
						World_FXEventSquad ("data:art/events/unit_ability_fx/sabotage_caster", "sg_tele")
						World_FXEventSquad ("data:art/events/unit_upgrade_morale_fx/reinforce_chaos_trooper", "sg_tele")
						SGroup_DeSpawn("sg_tele")
						SGroup_ReSpawnMarker("sg_tele", "destination "..i)
						World_FXEventSquad ("data:art/events/unit_ability_fx/sabotage_caster", "sg_tele")
						World_FXEventSquad ("data:art/events/unit_upgrade_morale_fx/reinforce_chaos_trooper", "sg_tele")
						SGroup_Clear("sg_tele")
						LocalNotifications(j,i)
					end
				end
			end
		end
	end
end

--pings and displays eventcue local player only
function LocalNotifications(j,i)
	if World_GetPlayerAt(j) == Misc_GetLocalPlayerID()-1000 then
		EventCue_DoEvent("warning", "event_cue_notifications/general_alert", "Unit Teleported", "The dark forces have teleported your troops through the warp")
		if i < 10 then
			local MarkerID = Marker_FromName("destination 0"..i, "basic_marker")
			Ping_Position(Marker_GetPosition(MarkerID), false)
		else
			local MarkerID = Marker_FromName("destination "..i, "basic_marker")
			Ping_Position(Marker_GetPosition(MarkerID), false)
		end
	end
end
----------------------------------------------------------------------------------------------------------
--Taldeer worship code for Akranadas Island
----------------------------------------------------------------------------------------------------------
function Taldeer()
	SGroup_FaceSGroup("Followers", "Taldeer")
	Anim_PlaySGroupAnim(SGroup_FromName("Followers"), "capture_strategic_point")
end