Mission event file for Pavonis special mission
Gidsen
/
Mission event file for Pavonis special mission
Geüpdatet 4 years ago door Merric

-- Scavenger Hunt Win Condtition - Used for Pavonis Province in Dark Crusade

-- Find all "Scientists" (using IG civilians as placeholder scientists until final item/unit/artifact is decided on)

-- Author: James Mouat

-- (c) 2006 Relic Entertainment Inc.

import("ScarUtil.scar") import("WXPScarUtil.scar")

function OnGameSetup()

g_ticker = 0 g_IE_Delay = 0 g_Capture_Delay = 0

g_count_scientist1 = 0 g_count_scientist2 = 0 g_count_scientist3 = 0 g_count_scientist4 = 0 g_count_scientist5 = 0 g_count_scientist6 = 0 g_count_scientist7 = 0 g_count_scientist8 = 0 g_count_scientist9 = 0 g_count_scientist10 = 0

g_sci_pos1 = 0 g_sci_pos2 = 0 g_sci_pos3 = 0 g_sci_pos4 = 0 g_sci_pos5 = 0 g_sci_pos6 = 0 g_sci_pos7 = 0 g_sci_pos8 = 0 g_sci_pos9 = 0 g_sci_pos10 = 0

g_P1_squad_count = 0

g_Win = false

g_Scientists_Captured = 0

g_P1_Scientist_Count = 0 g_P2_Scientist_Count = 0 g_P3_Scientist_Count = 0 g_P4_Scientist_Count = 0

g_P1_Win_Count = 0 g_P2_Win_Count = 0 g_P3_Win_Count = 0 g_P4_Win_Count = 0

g_Scientist_Math_firstrun = false

g_total_enemy_score = 0

g_enemy_near_win_delay = 0

g_scientist_arrived_delay = 0

g_territory_index = MetaMap_GetDefendingTerritoryIndex()

g_total_enemy_count = MetaMap_GetTerritoryOpponentCount(g_territory_index)

------------------------------------------------------------------------------
--[[Start Darwin's attacker/defender algorhythm]]

g_attacker_race = MetaMap_GetAttackerRaceName()
g_defender_race = MetaMap_GetDefenderRaceName()
g_player_race = MetaMap_GetPlayerRaceName() 

--print("attacker race = "..g_attacker_race)
--print("defender race = "..g_defender_race)


--check for player vs CPU -- used in the enemy creation functions!
if g_player_race == g_attacker_race then
	
	g_cpu_player_race = g_defender_race
	
	g_defender_strength = MetaMap_GetTerritoryMilitaryStrength(MetaMap_GetDefendingTerritoryIndex())
	
	--print("defender military strength = "..g_defender_strength)
	
	g_cpu_strength = g_defender_strength
	
elseif g_player_race == g_defender_race then
	
	g_cpu_player_race = g_attacker_race
	
	g_attacker_strength = MetaMap_GetTerritoryMilitaryStrength(MetaMap_GetAttackingTerritoryIndex())
	
	--print("attacker military strength = "..g_attacker_strength)
	
	g_cpu_strength = g_attacker_strength
	
end

g_enemy_players_on_map = MetaMap_GetTerritoryOpponentCount(7)

--define a global varible that tells how many total players are on the map.
g_total_players_on_map = g_enemy_players_on_map + 1


--[[defining the name of the player's faction +Engineer BP's +IE Event ID's]]
if g_player_race == "chaos_marine_race" then
	
	factionname = "$690014"
	g_P1_Engineer = "chaos_squad_slave"
	g_P1_IE = 1
	g_player_color = "default_1"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)
	Misc_PlayerTeamColor(g_Player1,g_player_color)
	
elseif g_player_race == "eldar_race" then
	
	factionname = "$690012"
	g_P1_Engineer = "eldar_squad_bonesinger"
	g_P1_IE = 2
	g_player_color = "default_5"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)
	Misc_PlayerTeamColor(g_Player1,g_player_color)
	
elseif g_player_race == "guard_race" then
	
	factionname = "$690011"
	g_P1_Engineer = "guard_squad_enginseer"
	g_P1_IE = 3
	g_player_color = "liberators"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)

	
elseif g_player_race == "ork_race" then
	
	factionname = "$690013"
	g_P1_Engineer = "ork_squad_grot"
	g_P1_IE = 4
	g_player_color = "default_4"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)
	Misc_PlayerTeamColor(g_Player1,g_player_color)
	
elseif g_player_race == "space_marine_race" then
	
	factionname = "$690010"
	g_P1_Engineer = "space_marine_squad_servitor"
	g_P1_IE = 5
	g_player_color = "default_0"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)

	
elseif g_player_race == "tau_race" then
	
	factionname = "$690015"
	g_P1_Engineer = "tau_builder_squad"
	g_P1_IE = 6
	g_player_color = "default_0"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)

	
elseif g_player_race == "necron_race" then
	
	factionname = "$690016"
	g_P1_Engineer = "necron_builder_scarab_squad"
	g_P1_IE = 7
	g_player_color = "default_0"
	
	g_Player1 = Setup_Player(0, factionname, MetaMap_GetPlayerRaceName(), 1)

	
end


	--[[defining the name of the enemy's faction +HQ BP's +Engineer BP's +IE Event ID's +Team color]]  
if g_cpu_player_race == "chaos_marine_race" then
	
	g_enemy_factionname = "$690014"
	g_CPU_HQ = "chaos_hq"
	g_CPU_Engineer = "chaos_squad_slave"
	g_CPU_IE = 1
	g_CPU_color = "default_1"
	
elseif g_cpu_player_race == "eldar_race" then

	g_enemy_factionname = "$690012"
	g_CPU_HQ = "eldar_hq"
	g_CPU_Engineer = "eldar_squad_bonesinger"
	g_CPU_IE = 2
	g_CPU_color = "default_5"
	
elseif g_cpu_player_race == "guard_race" then

	g_enemy_factionname = "$690011"
	g_CPU_HQ = "guard_hq"
	g_CPU_Engineer = "guard_squad_enginseer"
	g_CPU_IE = 3
	g_CPU_color = "liberators"

elseif g_cpu_player_race == "ork_race" then

	g_enemy_factionname = "$690013"
	g_CPU_HQ = "ork_hq"
	g_CPU_Engineer = "ork_squad_grot"
	g_CPU_IE = 4
	g_CPU_color = "default_4"
	
elseif g_cpu_player_race == "space_marine_race" then

	g_enemy_factionname = "$690010"
	g_CPU_HQ = "space_marine_hq"
	g_CPU_Engineer = "space_marine_squad_servitor"
	g_CPU_IE = 5
	g_CPU_color = "default_0"
	
elseif g_cpu_player_race == "tau_race" then
	
	g_enemy_factionname = "$690015"
	g_CPU_HQ = "tau_hq"
	g_CPU_Engineer = "tau_builder_squad"
	g_CPU_IE = 6
	g_CPU_color = "default_0"
	
elseif g_cpu_player_race == "necron_race" then

	g_enemy_factionname = "$690016"
	g_CPU_HQ = "monolith"
	g_CPU_Engineer = "necron_builder_scarab_squad"
	g_CPU_IE = 7
	g_CPU_color = "default_0"

end

--[[End Darwin's attacker/defender algorhythm]]
----------------------------------------------------------------------------



g_Player2 = Setup_Player(1, g_enemy_factionname, g_cpu_player_race, 2)
Misc_PlayerTeamColor(g_Player2, g_CPU_color)

g_Player3 = Setup_Player(2, g_enemy_factionname, g_cpu_player_race, 2)
Misc_PlayerTeamColor(g_Player3, g_CPU_color)

g_Player4 = Setup_Player(3, g_enemy_factionname, g_cpu_player_race, 2)
Misc_PlayerTeamColor(g_Player4, g_CPU_color)

-- neutral team used for scientists
g_Player6 = Setup_Player(5,"$3100100", "npc_race", TEAM_NEUTRAL)
Misc_PlayerTeamColor(g_Player6,"default_7")


--[[ define the "enemy" who will win if the player 'loses' ]]
g_enemy = g_Player2

end

function OnInit() --comment this back in to view the beginning of the SM campaign outro. -- Util_StartNIS( EVENTS.NIS_SM_Campaign_Outro1 ) Rule_Add(Scavenger_Hunt)

end

--[[ the Scar_AddInit(OnInit) function is mandatory! This registers your init function with scar. ]] Scar_AddInit(OnInit)

function Scavenger_Hunt()

Rule_AddInterval(Rule_Timer, 1)

Rule_Add(Rule_Create_Player_Buildings)
Rule_AddOneShot(Rule_Create_Player_Units,1)
Rule_AddOneShot(Rule_Create_NPC_Buildings,1)
Rule_AddOneShot(Rule_Create_Enemy_Bonus_Squads,1)

Rule_AddOneShot(Rule_PresetAI, 1)

Rule_AddOneShot(Rule_Create_Engineers,1)

-- add scientists to the map
Rule_AddOneShot(Rule_Create_Scientists, 2)


Rule_AddOneShot(Rule_Add_Objective, 5)

-- show counter of captured scientists
UI_ShowCount("Scientist_Counter_P2", g_Player2, 3100151, g_P2_Scientist_Count)
UI_ShowCount("Scientist_Counter_P1", g_Player1, 3100150, g_P1_Scientist_Count)

--UI_ShowCount("Scientist_Counter_P3", g_Player3, 3100152, g_P3_Scientist_Count)
--UI_ShowCount("Scientist_Counter_P4", g_Player4, 3100153, g_P4_Scientist_Count)


--make the scientists move around near their spawn point
--Rule_AddInterval(Rule_Scientist_Mover, 45)


Rule_AddIntervalDelay(Rule_EndGame_Win,5,10)
Rule_AddIntervalDelay(Rule_EndGame_Lose,5,10)
Rule_AddIntervalDelay(Rule_EndGame_Lose_ALT,5,10)


--[[ Music ]]
Rule_AddOneShot(Rule_Start_Music, 0)


Rule_Remove(Scavenger_Hunt)

end

function Rule_Add_Objective()

--establish objective
Objective_Scavenger_Hunt = { title_id = 3100001, short_desc_id = 3100002, help_tip_id = 3100001 }

Util_WXP_ObjectiveCreate(Objective_Scavenger_Hunt, true, g_Player1)

Objective_ShowScreen()
Flash_OtherButton("dlg_objectives", 0, false, true)

end

function Rule_Start_Music()

--set the music table based on the players race
if MetaMap_GetPlayerRaceName()  == "chaos_marine_race" then
	
	t_music = {	"MU_IG_STR_Chaos_perc_brass", "MU_IG_STR_Chaos", "MU_IG_STR_Chaos_perc", 
						"MU_IG_STR_Chaos_perc_str", "MU_IG_STR_Chaos_perc_voice"}
	
elseif MetaMap_GetPlayerRaceName()  == "eldar_race" then
	
	t_music = {	"MU_IG_STR_Eldar_perc_brass", "MU_IG_STR_Eldar", "MU_IG_STR_Eldar_perc", 
						"MU_IG_STR_Eldar_perc_str"}
	
elseif MetaMap_GetPlayerRaceName()  == "guard_race" then
	
	t_music = {	"MU_IG_STR_ImpGd_perc_brass", "MU_IG_STR_ImpGd", "MU_IG_STR_ImpGd_perc", 
						"MU_IG_STR_ImpGd_perc_str", "MU_IG_STR_ImpGd_perc_voice"}
	
elseif MetaMap_GetPlayerRaceName()  == "ork_race" then
	
	t_music = {	"MU_IG_STR_Ork_perc_brass", "MU_IG_STR_Ork", "MU_IG_STR_Ork_perc", 
						"MU_IG_STR_Ork_perc_str"}
	
elseif MetaMap_GetPlayerRaceName()  == "space_marine_race" then
	
	t_music = {	"MU_IG_STR_SpaceMarine_perc_brass", "MU_IG_STR_SpaceMarine", "MU_IG_STR_SpaceMarine_perc", 
						"MU_IG_STR_SpaceMarine_perc_str"}
	
elseif MetaMap_GetPlayerRaceName()  == "tau_race" then
	
	t_music = {	"MU_IG_STR_Tau_perc_brass", "MU_IG_STR_Tau", "MU_IG_STR_Tau_perc", 
						"MU_IG_STR_Tau_perc_str"}
	
elseif MetaMap_GetPlayerRaceName()  == "necron_race" then
	
	t_music = {	"MU_IG_STR_Necron_perc_brass", "MU_IG_STR_Necron", "MU_IG_STR_Necron_brass", 
						"MU_IG_STR_Necron_perc"}
	
end

Playlist_Manager( PC_Music, t_music, true, true , {2, 4})


--set ambient sounds
t_ambient = {"ambient_city"}

Playlist_Manager( PC_Ambient, t_ambient, true, true , {0, 15})

end

function Rule_Timer()

g_ticker =  g_ticker + 1

--delay on scientist captured IE
if g_IE_Delay > 0 then
	
	g_IE_Delay = g_IE_Delay - 1
	
end

if g_Capture_Delay > 0 then
	
	g_Capture_Delay = g_Capture_Delay - 1
	
end

--delay on enemy about to win IE
if g_enemy_near_win_delay > 0 then
	
	g_enemy_near_win_delay = g_enemy_near_win_delay - 1
	
end

--delay on new scientist arrival IE
if g_scientist_arrived_delay > 0 then
	
	g_scientist_arrived_delay = g_scientist_arrived_delay - 1
	
end

end

function Rule_Create_Player_Buildings()

t_Player_Buildings ={

chaos_name = {"eg_Player_HQ"},
chaos_building_blueprint = {"chaos_hq"},
chaos_marker_spawn = {"mkr_Player_HQ"},

eldar_name = {"eg_Player_HQ"},
eldar_building_blueprint = {"eldar_hq"},
eldar_marker_spawn ={"mkr_Player_HQ"}, 

guard_name = {"eg_Player_HQ"},
guard_building_blueprint = {"guard_hq"}, 
guard_marker_spawn ={"mkr_Player_HQ"},

ork_name = {"eg_Player_HQ"},
ork_building_blueprint = {"ork_hq"},
ork_marker_spawn ={"mkr_Player_HQ"},

marines_name = {"eg_Player_HQ"},
marines_building_blueprint = {"space_marine_hq"},
marines_marker_spawn ={"mkr_Player_HQ"},

tau_name = {"eg_Player_HQ"},
tau_building_blueprint = {"tau_hq"},
tau_marker_spawn= {"mkr_Player_HQ"},

necron_name ={"eg_Player_HQ"},
necron_building_blueprint = {"monolith"},
necron_marker_spawn = {"mkr_Player_HQ"},

}

if MetaMap_GetPlayerRaceName() == "chaos_marine_race" then
	
	for i = 1, table.getn(t_Player_Buildings.chaos_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.chaos_name[i], t_Player_Buildings.chaos_building_blueprint[i], t_Player_Buildings.chaos_marker_spawn[i], 1)
	end
	
elseif MetaMap_GetPlayerRaceName() == "eldar_race" then
	
	for i = 1, table.getn(t_Player_Buildings.eldar_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.eldar_name[i], t_Player_Buildings.eldar_building_blueprint[i], t_Player_Buildings.eldar_marker_spawn[i], 1)
	end
	
elseif MetaMap_GetPlayerRaceName() == "guard_race" then
	
	for i = 1, table.getn(t_Player_Buildings.guard_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.guard_name[i], t_Player_Buildings.guard_building_blueprint[i], t_Player_Buildings.guard_marker_spawn[i], 1)
	end
	
elseif MetaMap_GetPlayerRaceName() == "ork_race" then
	
	for i = 1, table.getn(t_Player_Buildings.ork_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.ork_name[i], t_Player_Buildings.ork_building_blueprint[i], t_Player_Buildings.ork_marker_spawn[i], 1)
	end
	
elseif MetaMap_GetPlayerRaceName() == "space_marine_race" then
	
	for i = 1, table.getn(t_Player_Buildings.marines_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.marines_name[i], t_Player_Buildings.marines_building_blueprint[i], t_Player_Buildings.marines_marker_spawn[i], 1)
	end
	
elseif MetaMap_GetPlayerRaceName() == "tau_race" then
	
	for i = 1, table.getn(t_Player_Buildings.tau_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.tau_name[i], t_Player_Buildings.tau_building_blueprint[i], t_Player_Buildings.tau_marker_spawn[i], 1)
	end
	
elseif MetaMap_GetPlayerRaceName() == "necron_race" then
	
	for i = 1, table.getn(t_Player_Buildings.necron_name) do
		Entity_CreateBuildingMarker(g_Player1, t_Player_Buildings.tau_name[i], t_Player_Buildings.necron_building_blueprint[i], t_Player_Buildings.necron_marker_spawn[i], 1)
	end
	
end


--establish variables to be used to incrementally create turrets and generators by concantenating the variables onto the markers as needed
-- generator markers start at 0
g_bonus_gen_counter = 0
-- turrer markers start at 6
g_bonus_turret_counter = 6

--determine race specific blueprints to be looked for when creating forward base
if MetaMap_GetPlayerRaceName() == "chaos_marine_race" then
	
	g_bonus_generator = "chaos_plasma_generator"
	g_bonus_turret = "chaos_turret_bolter"
	g_bonus_barracks = "chaos_temple"
	g_bonus_barracks2 = 0 --value on used for tau kroot nest, dummy value for all other races, like the Tau turret.
	g_bonus_research = "chaos_armoury"
	
elseif MetaMap_GetPlayerRaceName() == "eldar_race" then
	
	g_bonus_generator = "eldar_warp_generator"
	g_bonus_turret = "eldar_support_platform_scatterlaser"
	g_bonus_barracks = "eldar_aspect_portal"
	g_bonus_barracks2 = 0 --value on used for tau kroot nest, dummy value for all other races, like the Tau turret.
	g_bonus_research = "eldar_soul_shrine"
	
elseif MetaMap_GetPlayerRaceName() == "guard_race" then
	
	g_bonus_generator = "guard_plasma_generator"
	g_bonus_turret = "guard_turret_heavy_bolter"
	g_bonus_barracks = "guard_infantry"
	g_bonus_barracks2 = 0 --value on used for tau kroot nest, dummy value for all other races, like the Tau turret.
	g_bonus_research = "guard_tactica"
	
elseif MetaMap_GetPlayerRaceName() == "ork_race" then
	
	g_bonus_generator = "ork_generator"
	g_bonus_turret = "ork_waagh_banner"
	g_bonus_barracks = "ork_boy_hut"
	g_bonus_barracks2 = 0 --value on used for tau kroot nest, dummy value for all other races, like the Tau turret.
	g_bonus_research = "ork_pile_o_guns"
	
elseif MetaMap_GetPlayerRaceName() == "space_marine_race" then
	
	g_bonus_generator = "space_marine_generator"
	g_bonus_turret = "space_marine_turret_bolter"
	g_bonus_barracks = "space_marine_barracks"
	g_bonus_barracks2 = 0 --value on used for tau kroot nest, dummy value for all other races, like the Tau turret.
	g_bonus_research = "space_marine_armoury"
	
elseif MetaMap_GetPlayerRaceName() == "tau_race" then
	
	g_bonus_generator = "tau_plasma_generator"
	g_bonus_turret = 0  --tau have no turrets, so zero is simply a filler value to establish the variable instead of writting cutom checks for tau later.  0 will never be a value fed in by code, so it simply allows the algorithym to function in a standadrd manner for all races.
	g_bonus_barracks = "tau_barracks"
	g_bonus_barracks2 = "tau_kroot_nest"
	g_bonus_research = "tau_research_building"
	
elseif MetaMap_GetPlayerRaceName() == "necron_race" then
	
	g_bonus_generator = "necron_plasma_generator"
	g_bonus_turret = "necron_turret"
	g_bonus_barracks = "necron_summoning_core"
	g_bonus_barracks2 = 0 --value on used for tau kroot nest, dummy value for all other races, like the Tau turret.
	g_bonus_research = "necron_forbidden_archive"
	
end

--Add in any bonus buildings made availible if the player has purchased them via owning Hyperion peaks province


t_blueprintEntitybonus = {}
MetaMap_GetAttackingRaceStartingEntitiesList(t_blueprintEntitybonus)

for j = 1,table.getn(t_blueprintEntitybonus) do
	
	if t_blueprintEntitybonus[j] == g_bonus_generator then
		
		Entity_CreateBuildingMarker(g_Player1, "eg_Bonus"..j, t_blueprintEntitybonus[j], "MM_Reinforcement"..g_bonus_gen_counter, 1)
		
		--increase by one, the next generator will then spawn at the correct marker after this one
		g_bonus_gen_counter = g_bonus_gen_counter + 1
		
	elseif t_blueprintEntitybonus[j] == g_bonus_turret then
		
		Entity_CreateBuildingMarker(g_Player1, "eg_Bonus"..j, t_blueprintEntitybonus[j], "MM_Reinforcement"..g_bonus_turret_counter, 1)
		
		--increase by one, the next turret will then spawn at the correct marker after this one
		g_bonus_turret_counter = g_bonus_turret_counter + 1
		
	elseif t_blueprintEntitybonus[j] == g_bonus_barracks then
		
		--fixed marker spawn based on Phil's table.  Only one barracks is spawned, always at marker 10
		--Necron intentionally spawn a summoning core in place of a barracks due to the monolith doubling as their barracks.
		Entity_CreateBuildingMarker(g_Player1, "eg_Bonus"..j, t_blueprintEntitybonus[j], "MM_Reinforcement10", 1)
		
	elseif t_blueprintEntitybonus[j] == g_bonus_research then
		
		--fixed marker spawn based on Phil's table.  Only one research building is spawned, always at marker 11
		Entity_CreateBuildingMarker(g_Player1, "eg_Bonus"..j, t_blueprintEntitybonus[j], "MM_Reinforcement11", 1)
		
	elseif t_blueprintEntitybonus[j] == g_bonus_barracks2 then
		
		--This will only be used to spawn the kroot nest when the player is Tau, otherwise it will be passed over due to g_bonus_barracks2 being a dummy value.
		--fixed marker spawn based on Phil's table.  Only one kroot nest is spawned, always at marker 13
		Entity_CreateBuildingMarker(g_Player1, "eg_Bonus"..j, t_blueprintEntitybonus[j], "MM_Reinforcement13", 1)
		
	end
	
end


Rule_Remove(Rule_Create_Player_Buildings)

end

function Rule_Create_Player_Units()

--setting up the bonus units for the player
t_blueprintbonus = {}
MetaMap_GetRaceStartingSquadsList(MetaMap_GetPlayerRaceName(), t_blueprintbonus)

for j = 1,  table.getn(t_blueprintbonus) do
		
	if j <= 7 then
			
			k = j
	else
	
		k = j - 7
	
	end
	
	Util_CreateSquadsAtMarker(g_Player1, "sg_Bonus"..j, t_blueprintbonus[j], "mkr_Bonus"..k, 1)
	
end

--setting up the upgrades for the commander
MetaMap_UpdatePlayerWargear(g_Player1)

end

function Rule_Create_Enemy_Bonus_Squads()

--setting up the bonus units for the player
t_cpu_blueprintbonus = {}
MetaMap_GetRaceStartingSquadsList(g_cpu_player_race, t_cpu_blueprintbonus)

for t = 1,  table.getn(t_cpu_blueprintbonus) do
		
	u = math.mod(t - 1, 6) + 1
	
	Util_CreateSquadsAtMarker(g_Player2, "sg_cpu_bonus"..t, t_cpu_blueprintbonus[t], "mkr_cpu_bonus"..u, 1)
	
	
end

end

function Rule_Create_NPC_Buildings()

Entity_CreateBuildingMarker(g_Player2, "eg_Player2_HQ", g_CPU_HQ, "mkr_NPC_player2", 1)
Entity_CreateBuildingMarker(g_Player3, "eg_Player3_HQ", g_CPU_HQ, "mkr_NPC_player3", 1)
Entity_CreateBuildingMarker(g_Player4, "eg_Player4_HQ", g_CPU_HQ, "mkr_NPC_player4", 1)

end

function Rule_Create_Engineers()

t_players = {--[[1]] g_Player1, --[[2]] g_Player2, --[[3]] g_Player3, --[[4]] g_Player4}

for i = 1,g_total_players_on_map do
	
	if i == 1 then
		
		Util_CreateSquadsAtMarker(t_players[i],"sg_engineer_P"..i, g_P1_Engineer, "mkr_engineer"..i, 1)
		
	else
		
		Util_CreateSquadsAtMarker(t_players[i],"sg_engineer_P"..i, g_CPU_Engineer, "mkr_engineer"..i, 1)
		
	end
	
end

end

function Rule_PresetAI()

if g_total_enemy_count > 0 then
	
	--[[CPU player2]]
	Cpu_Enable(g_Player2, true)
	Cpu_EnableComponent(g_Player2, true, CT_Resourcing)
	Cpu_EnableComponent(g_Player2, true, CT_BuildBuildings)
	Cpu_EnableComponent(g_Player2, true, CT_BuildUnits)
	Cpu_EnableComponent(g_Player2, true, CT_BuildResearch)
	Cpu_EnableComponent(g_Player2, true, CT_BuildAddOns)
	Cpu_EnableComponent(g_Player2, true, CT_Tactics)
	
	--set difficulty based on kronus.map tabled values.
	Cpu_SetDifficulty(g_Player2, MetaMap_GetTerritoryOpponentDifficulty(g_territory_index,0))
	--print(MetaMap_GetTerritoryOpponentDifficulty(g_territory_index,0))
	
	
	if g_total_enemy_count > 1 then
		--[[CPU player3]]
		Cpu_Enable(g_Player3, true)	
		Cpu_EnableComponent(g_Player3, true, CT_Resourcing)
		Cpu_EnableComponent(g_Player3, true, CT_BuildBuildings)
		Cpu_EnableComponent(g_Player3, true, CT_BuildUnits)
		Cpu_EnableComponent(g_Player3, true, CT_BuildResearch)
		Cpu_EnableComponent(g_Player3, true, CT_BuildAddOns)
		Cpu_EnableComponent(g_Player3, true, CT_Tactics)
		
		--set difficulty based on kronus.map tabled values.
		Cpu_SetDifficulty(g_Player3, MetaMap_GetTerritoryOpponentDifficulty(g_territory_index,1))
		--print(MetaMap_GetTerritoryOpponentDifficulty(g_territory_index,1))
		
		
		if g_total_enemy_count > 2 then
			--[[CPU player4]]
			Cpu_Enable(g_Player4, true)
			Cpu_EnableComponent(g_Player4, true, CT_Resourcing)
			Cpu_EnableComponent(g_Player4, true, CT_BuildBuildings)
			Cpu_EnableComponent(g_Player4, true, CT_BuildUnits)
			Cpu_EnableComponent(g_Player4, true, CT_BuildResearch)
			Cpu_EnableComponent(g_Player4, true, CT_BuildAddOns)
			Cpu_EnableComponent(g_Player4, true, CT_Tactics)
			
			--set difficulty based on kronus.map tabled values.
			Cpu_SetDifficulty(g_Player4, MetaMap_GetTerritoryOpponentDifficulty(g_territory_index,2))
			--print(MetaMap_GetTerritoryOpponentDifficulty(g_territory_index,2))
			
		end
		
	end
	
end

--disable the scientist team AI to prevent unexpected shenanigans!
Cpu_Enable(g_Player6, false)

end

function Rule_Get_All_Units()

--[[This will add all units belonging to eachplayer into one sgroup (per player).  these sgroups will be used for detecting scientists for each player.]]
for i = 1,g_total_players_on_map do
	
	SGroup_AddGroup(SGroup_CreateIfNotFound("sg_All_Units_P"..i), Player_GetSquads(t_players[i]))
	
end

--[[old

SGroup_CreateIfNotFound("sg_All_P1_Units")

SGroup_Clear("sg_All_P1_Units")

SGroup_AddGroup("sg_All_P1_Units", Player_GetSquads(g_Player1))

g_P1_squad_count = SGroup_Count(Player_GetSquads(g_Player1))

--this routine will analyze all the players units and add all non-scientists to an Sgroup.  
--This Sgroup is used to determine player unit proximity to the scientists for acquiring and relinquishing control of the scientists.
if g_P1_squad_count > 0 then
	
	local countsquad = SGroup_CountSpawned("sg_All_P1_Units")
	
	for i = 1, countsquad do
		
		SGroup_Clear(SGroup_CreateIfNotFound("sg_Potential_Add"))
		SGroup_Add(SGroup_FromName("sg_Potential_Add"), SGroup_GetSpawnedSquadAt("sg_All_P1_Units", i))
		
		local blueprinttable = Util_MakeBlueprintTable("guard_squad_civilian_male_a")
		
		if SGroup_ContainsBlueprints(SGroup_FromName("sg_Potential_Add"), blueprinttable, true) == false then
			
			SGroup_AddGroup(SGroup_CreateIfNotFound("sg_Player_Units_no_Scientists"), "sg_Potential_Add")
			
		end
		
	end
	
end

]]

end

function Rule_Create_Scientists()

for i = 1,10 do
	
	Util_CreateSquadsAtMarker(g_Player6, "sg_Scavenger"..i, "space_marine_squad_servitor_sp", "Scavenger_Spawn"..i, 1)
	
	SGroup_AddGroup(SGroup_CreateIfNotFound("sg_Scavenger_Parent"), "sg_Scavenger"..i)
	
	SGroup_SetHealthInvulnerable("sg_Scavenger"..i, true)
	Cpu_LockSGroupAcrossPlayers("sg_Scavenger"..i)
	
	Cmd_SetStance("sg_Scavenger"..i, STANCE_StandGround)
	
end

Rule_AddInterval(Rule_Get_All_Units, 1)
Rule_AddIntervalDelay(Rule_Check_Scientist_Prox, 1,5)

Rule_AddIntervalDelay(Rule_Scientist_Math, 1, 10)

end

function Rule_Scientist_Mover()

for i = 1,10 do
	
	if Prox_AllSquadsNearMarker("sg_Scavenger"..i, "Scavenger_Spawn"..i) then
		
		Util_AttackMoveMarkerRandomRadius("sg_Scavenger"..i, "Scavenger_Spawn"..i)
		
	end
	
end

end

t_scientist_totals = { g_count_scientist1, g_count_scientist2, g_count_scientist3, g_count_scientist4, g_count_scientist5, g_count_scientist6, g_count_scientist7, g_count_scientist8, g_count_scientist9, g_count_scientist10}

-- if scientists are in range of any of the players squads, their status flag will toggle to true, otherwise it will toggle to false function Rule_Check_Scientist_Prox()

--positions of each scientist, updated each pass of the rule.
t_scientist = {	pos = {g_sci_pos1,
									g_sci_pos2,
									g_sci_pos3,
									g_sci_pos4,
									g_sci_pos5,
									g_sci_pos6,
									g_sci_pos7,
									g_sci_pos8,
									g_sci_pos9,
									g_sci_pos10},
						
						mkr = { 	"mkr_Player_HQ",
										"mkr_NPC_player2",
										"mkr_NPC_player3",
										"mkr_NPC_player4"}
}

--apply current positions of scientists.
for a = 1,10 do
	
	t_scientist.pos[a] = SGroup_GetPosition("sg_Scavenger"..a)
	
end

-- iterate for each player
for i = 1,g_total_players_on_map do
	
	--iterate for each scientist
	for k = 1,10 do
		
		SGroup_CreateIfNotFound("sg_sci_cap_P"..i)
		
		SGroup_Clear("sg_sci_cap_P"..i)
		
		--if the player doesn't own the scientist being checked, then proceed with the detection
		if not (t_scientist_totals[k] == i) then
			
			Player_GetAllSquadsNearPos(t_players[i], "sg_sci_cap_P"..i, t_scientist.pos[k], 15)
			
			if SGroup_Count("sg_sci_cap_P"..i) > 0 then
				
				if g_Capture_Delay == 0 then
					
					--move to player ones marker (player has different marker name than NPCs)
					Cmd_MoveToMarker("sg_Scavenger"..k, t_scientist.mkr[i])
					
					t_scientist_totals[k] = i
					
					if i == 1 then	
						
						g_temp_race = g_P1_IE
						
						if Rule_Exists(Rule_IE_Scientist_Found_By_Player) == false then
							
							Rule_AddOneShot(Rule_IE_Scientist_Found_By_Player, 0.5)
							
						end
						
					else
						
						g_temp_race = g_CPU_IE
						
						if Rule_Exists(Rule_IE_Scientist_Found_By_Enemy) == false then
							
							Rule_AddOneShot(Rule_IE_Scientist_Found_By_Enemy, 0.5)
							
						end
						
					end
					
					g_temp_pos = t_scientist.pos[k]
					
					g_Capture_Delay = 3	
					
				end
				
			end
			
		--re-issue move order to HQ if the scientist has somehow been taken out of range of the HQ (i.e. - area effect blast), or has gotten path blocked and stopped in the field.
		elseif t_scientist_totals[k] == i then
			
			if Prox_MarkerSGroup(t_scientist.mkr[i], "sg_Scavenger"..k, PROX_SHORTEST) > 15 then
				
				Cmd_MoveToMarker("sg_Scavenger"..k, t_scientist.mkr[i])
				
			end
			
		end
		
	end
	
end

end

t_player_totals = { score = { g_P1_Scientist_Count, g_P2_Scientist_Count, g_P3_Scientist_Count, g_P4_Scientist_Count},

							win = { 	g_P1_Win_Count,
											g_P2_Win_Count,
											g_P3_Win_Count,
											g_P4_Win_Count},
											
						position = {	g_pos_P1,
											g_pos_P2,
											g_pos_P3,
											g_pos_P4},
							
					UI_range = {	3100150,
											3100151,
											3100152,
											3100153}

}

function Rule_Scientist_Math()

--ensure the tabled values are initialized
if g_Scientist_Math_firstrun == false then
	
	t_player_totals.score[1] = 0
	t_player_totals.score[2] = 0
	t_player_totals.score[3] = 0
	t_player_totals.score[4] = 0
	
	g_Scientist_Math_firstrun = true
	
end

if t_player_totals.position[1] == nil then
	
	t_player_totals.position[1] = Marker_GetPosition(Marker_FromName("mkr_Player_HQ", "basic_marker"))
	t_player_totals.position[2] = Marker_GetPosition(Marker_FromName("mkr_NPC_player2", "basic_marker"))
	t_player_totals.position[3] = Marker_GetPosition(Marker_FromName("mkr_NPC_player3", "basic_marker"))
	t_player_totals.position[4] = Marker_GetPosition(Marker_FromName("mkr_NPC_player4", "basic_marker"))
	
end

-- iterate for each player
for i = 1,g_total_players_on_map do
	
	--clear out the temp values for the next iterations use
	--g_temp_score = 0
	g_temp_win = 0
	
	--clear out SG to make sure if the player has lost all scientists, the count is reflective
	SGroup_Clear(SGroup_CreateIfNotFound("sg_Scientists_P"..i))
	
	--create SG's for all scientists near each player HQ.  These SG's will be used for a count value to determine win condition eligibilty.
	Player_GetAllSquadsNearPos(g_Player6, "sg_Scientists_P"..i, t_player_totals.position[i], 15)
	
	g_temp_win = SGroup_Count("sg_Scientists_P"..i)		
	
	--play the scientist arrived at player HQ IE if the new score is greater than the old score (thus indicating that a new scientist had arrived at the player HQ)
	if i == 1 and g_temp_win > t_player_totals.score[i] and g_scientist_arrived_delay == 0 and g_temp_win < 6 then
		
		if g_player_race == "chaos_marine_race" then
			
			Util_StartIntel(EVENTS.IE_Chaos_Servitor_Arrives_At_Players_Base)
			
		elseif g_player_race == "eldar_race" then
			
			Util_StartIntel(EVENTS.IE_Eldar_Servitor_Arrives_At_Players_Base)
			
		elseif g_player_race == "guard_race" then
			
			Util_StartIntel(EVENTS.IE_IG_Servitor_Arrives_At_Players_Base)
			
		elseif g_player_race == "ork_race" then
			
			Util_StartIntel(EVENTS.IE_Ork_Servitor_Arrives_At_Players_Base)
			
		elseif g_player_race == "space_marine_race" then
			
			Util_StartIntel(EVENTS.IE_SM_Servitor_Arrives_At_Players_Base)
			
		elseif g_player_race == "tau_race" then
			
			Util_StartIntel(EVENTS.IE_Tau_Servitor_Arrives_At_Players_Base)
			
		elseif g_player_race == "necron_race" then
			
			Util_StartIntel(EVENTS.IE_Necron_Servitor_Arrives_At_Players_Base)
			
		end
		
		g_scientist_arrived_delay = 10
		
	end
	
	-- re-assign the new player totals.
	--t_player_totals.score[i] = g_temp_score
	t_player_totals.score[i] = g_temp_win
	t_player_totals.win[i] = g_temp_win
	
	if i == 1 then
		
		-- update counter for player 1
		UI_ShowCountUpdateDxp("Scientist_Counter_P"..i, t_player_totals.score[i], t_player_totals.UI_range[i])
		
	else
		
		g_total_enemy_score = t_player_totals.score[2] + t_player_totals.score[3] + t_player_totals.score[4]
		
		UI_ShowCountUpdateDxp("Scientist_Counter_P2", g_total_enemy_score, t_player_totals.UI_range[2])
		
		--run Enemy about to win IE if the AI is ine away from winning.
		if g_total_enemy_score == 5 and g_enemy_near_win_delay == 0 then
			
			if g_player_race == "chaos_marine_race" then
				
				Util_StartIntel(EVENTS.IE_Chaos_Enemy_About_To_Win)
				
			elseif g_player_race == "eldar_race" then
				
				Util_StartIntel(EVENTS.IE_Eldar_Enemy_About_To_Win)
				
			elseif g_player_race == "guard_race" then
				
				Util_StartIntel(EVENTS.IE_IG_Enemy_About_To_Win)
				
			elseif g_player_race == "ork_race" then
				
				Util_StartIntel(EVENTS.IE_Ork_Enemy_About_To_Win)
				
			elseif g_player_race == "space_marine_race" then
				
				Util_StartIntel(EVENTS.IE_SM_Enemy_About_To_Win)
				
			elseif g_player_race == "tau_race" then
				
				Util_StartIntel(EVENTS.IE_Tau_Enemy_About_To_Win)
				
			elseif g_player_race == "necron_race" then
				
				Util_StartIntel(EVENTS.IE_Necron_Enemy_About_To_Win)
				
			end
			
			--add 5 minute delay before the IE will repeat.
			g_enemy_near_win_delay = 300
			
		end
		
	end
	
end

end

function Rule_IE_Scientist_Found_By_Player()

if g_IE_Delay == 0 then
	
	if g_player_race == "chaos_marine_race" then
		
		Util_StartIntel(EVENTS.IE_Chaos_Found_By_Player)
		
	elseif g_player_race == "eldar_race" then
		
		Util_StartIntel(EVENTS.IE_Eldar_Found_By_Player)
		
	elseif g_player_race == "guard_race" then
		
		Util_StartIntel(EVENTS.IE_IG_Found_By_Player)
		
	elseif g_player_race == "ork_race" then
		
		Util_StartIntel(EVENTS.IE_Ork_Found_By_Player)
		
	elseif g_player_race == "space_marine_race" then
		
		Util_StartIntel(EVENTS.IE_SM_Found_By_Player)
		
	elseif g_player_race == "tau_race" then
		
		Util_StartIntel(EVENTS.IE_Tau_Found_By_Player)
		
	elseif g_player_race == "necron_race" then
		
		Util_StartIntel(EVENTS.IE_Necron_Found_By_Player)
		
	end
	
	g_IE_Delay = 30
	
end

Ping_Position(g_temp_pos, false, default)

end

function Rule_IE_Scientist_Found_By_Enemy()

if g_IE_Delay == 0 then
	
	if g_player_race == "chaos_marine_race" then
		
		Util_StartIntel(EVENTS.IE_Chaos_Found_By_Enemy)
		
	elseif g_player_race == "eldar_race" then
		
		Util_StartIntel(EVENTS.IE_Eldar_Found_By_Enemy)
		
	elseif g_player_race == "guard_race" then
		
		Util_StartIntel(EVENTS.IE_IG_Found_By_Enemy)
		
	elseif g_player_race == "ork_race" then
		
		Util_StartIntel(EVENTS.IE_Ork_Found_By_Enemy)
		
	elseif g_player_race == "space_marine_race" then
		
		Util_StartIntel(EVENTS.IE_SM_Found_By_Enemy)
		
	elseif g_player_race == "tau_race" then
		
		Util_StartIntel(EVENTS.IE_Tau_Found_By_Enemy)
		
	elseif g_player_race == "necron_race" then
		
		Util_StartIntel(EVENTS.IE_Necron_Found_By_Enemy)
		
	end
	
	g_IE_Delay = 30
	
end

Ping_Position(g_temp_pos, false, default)

end

function Rule_IE_Win()

if g_player_race == "chaos_marine_race" then
	
	Util_StartIntel(EVENTS.IE_Chaos_Mission_Success)
	
elseif g_player_race == "eldar_race" then
	
	Util_StartIntel(EVENTS.IE_Eldar_Mission_Success)
	
elseif g_player_race == "guard_race" then
	
	Util_StartIntel(EVENTS.IE_IG_Mission_Success)
	
elseif g_player_race == "ork_race" then
	
	Util_StartIntel(EVENTS.IE_Ork_Mission_Success)
	
elseif g_player_race == "space_marine_race" then
	
	Util_StartIntel(EVENTS.IE_SM_Mission_Success)
	
elseif g_player_race == "tau_race" then
	
	Util_StartIntel(EVENTS.IE_Tau_Mission_Success)
	
elseif g_player_race == "necron_race" then
	
	Util_StartIntel(EVENTS.IE_Necron_Mission_Success)
	
end

Rule_Remove(Rule_IE_Win)

end

function Rule_IE_Lose()

if g_cpu_player_race == "chaos_marine_race" then
	
	Util_StartIntel(EVENTS.IE_Chaos_Mission_Success)
	
elseif g_cpu_player_race == "eldar_race" then
	
	Util_StartIntel(EVENTS.IE_Eldar_Mission_Success)
	
elseif g_cpu_player_race == "guard_race" then
	
	Util_StartIntel(EVENTS.IE_IG_Mission_Success)
	
elseif g_cpu_player_race == "ork_race" then
	
	Util_StartIntel(EVENTS.IE_Ork_Mission_Success)
	
elseif g_cpu_player_race == "space_marine_race" then
	
	Util_StartIntel(EVENTS.IE_SM_Mission_Success)
	
elseif g_cpu_player_race == "tau_race" then
	
	Util_StartIntel(EVENTS.IE_Tau_Mission_Success)
	
elseif g_cpu_player_race == "necron_race" then
	
	Util_StartIntel(EVENTS.IE_Necron_Mission_Success)
	
end

end

function Rule_EndGame_Win()

if t_player_totals.win[1] >= 6 then
	
	Util_ObjectiveComplete(Objective_Scavenger_Hunt.title_id)
	
	g_temp_race = g_P1_IE
	
	Rule_AddOneShot(Rule_IE_Win, 0.5)
	
	World_SetTeamWin( g_Player1, "" )
	Rule_AddIntervalEx( Rule_GameOver,10,1)
	
	Rule_Remove(Rule_EndGame_Win)
	Rule_Remove(Rule_EndGame_Lose)
	Rule_Remove(Rule_EndGame_Lose_ALT)
	
end

end

function Rule_EndGame_Lose()

if g_total_enemy_score >= 6 then
	
	g_temp_race = g_CPU_IE
	
	Rule_AddOneShot(Rule_IE_Lose, 0.5)
	
	Fade_Start(4, false)
	World_SetTeamWin( g_enemy, "" )
	Rule_AddIntervalEx( Rule_GameOver,10,1 )
	
	Rule_Remove(Rule_EndGame_Win)
	Rule_Remove(Rule_EndGame_Lose)
	Rule_Remove(Rule_EndGame_Lose_ALT)
	
end

end

function Rule_EndGame_Lose_ALT()

print("ALT_Lose_GO!")

g_building_exceptions = 
{
	--guard buildings
	"guard_thermo_plasma",
	"guard_plasma_generator",
	"guard_turret_heavy_bolter",
	"guard_mines",
	
	--chaos buildings
	"chaos_thermo_plasma_generator",
	"chaos_plasma_generator",
	"chaos_turret_bolter",
	"chaos_mine_field",
	
	--eldar buildings
	"eldar_advanced_warp_generator",
	"eldar_warp_generator",
	"eldar_support_platform_scatterlaser",
	"eldar_mine_field",
	
	--necron buildings
	"necron_thermoplasma_generator",
	"necron_plasma_generator",
	"necron_turret",
	
	--ork buildings
	"ork_bigger_generator",
	"ork_generator",
	"ork_waagh_banner",
	"ork_mine_field",
	
	--space marines
	"space_marine_thermo_generator",
	"space_marine_generator",
	"space_marine_turret_bolter",
	"space_marine_mine_field",
	
	--tau buildings
	"tau_thermoplasma_generator",
	"tau_plasma_generator"
}

g_unit_exceptions = 
{

}

--[[ Lose because of Annihilation ]]	
if Player_HasBuildingsExcept(g_Player1, g_building_exceptions) == false then
	
	Rule_RemoveAll()
	Fade_Start(4, false)
	World_SetTeamWin( g_enemy, "" )
	Rule_AddIntervalEx( Rule_GameOver,5,1 )
	
end

end

--[[ call this with an interval to ensure the mission ends ]] function Rule_GameOver() World_SetGameOver() end