Adding an agent to the recruitment pool (Adv)

--[[
	Script by Aexrael Dex
	adding a unique agent to the recruitment pool, plus ancillary listener for rank up + event popup for event feed
	Use the other simpler script as a first timer
]]

function unique_agent()
	-- Checking whether the script has already run for saved games and if it has then the script doesn't need to run again
	if cm:get_saved_value("unique_agent_enabled") == nil then

		-- Starting agent setup
		unique_agent_setup();
	end;

	-- Listening for CharacterRankUp to apply Unique Agent's ancillary when they reach a specific level, example use is lvl 9
	if cm:get_saved_value("unique_agent_has_ancillary") == nil then
		local agent_subtype = "unique_agent_subtype_key"; -- agent subtype_key from agent_subtypes

		core:add_listener(
			"unique_agent_anc",
			"CharacterRankUp",
			function(context)
				return context:character():character_subtype(agent_subtype) and context:character():rank() >= 9; -- change level unlock to match ancillary unlock level
			end,
			function(context)
				local char_str = context:character();
				local agent_ancillary = "unique_agent_ancillary_key"; -- ancillary key from ancillary table

				-- Grants Helman the ancillary
				cm:force_add_ancillary(char_str, agent_ancillary, true, false);
				out("Adding " .. agent_ancillary .. " to " .. agent_subtype);

				-- Setting saved value so the listener doesn't run again on Load Saved Game
				cm:set_saved_value("unique_agent_has_ancillary", true);
				out("Setting saved value unique_agent_has_ancillary");
			end,
			false
		);
	end;
end;

function unique_agent_setup()
	local agent_faction_str = "";			-- faction_key from factions
	local agent_forename_key = "";			-- forename_key from local_en
	local agent_family_name_key = "";		-- family_name_key from local_en
	local agent_gender_bool = true;			-- boolean for male true/false
	local agent_type_key = "";				-- agent type champion, colonel, dignitary, engineer, general, minister, runesmith, spy, wizard
	local agent_subtype_key = "";			-- agent subtype_key from agent_subtypes
	local agent_art_set_key = "";			-- agent art_set_id from campaign_character_arts
	local agent_event_title = ""; 			-- Keys from event_feed_strings_loc
	local agent_event_primary_detail = "";
	local agent_event_secondary_detail = "";
	local agent_event_id = 12345;			-- From campaign_group_member_criteria_values_tables

	-- Listening for FactionTurnStart for the faction the agent is to belong to
	core:add_listener(
		"unique agent_setup",
		"FactionTurnStart",
		function(context)
			return context:faction():name() == agent_faction_str;
		end,
		function(context)
			-- Unique agent is added to the recruitment menu
			cm:spawn_character_to_pool(agent_faction_str, agent_forename_key, agent_family_name_key, "", "", 18, agent_gender_bool, agent_type_key, agent_subtype_key, true, agent_art_set_key);
			out("Adding " .. agent_forename_key .. " " .. agent_family_name_key .. " to " .. agent_type_key .. " recruitment pool for " .. agent_faction_str);

			-- Displays an Event Message for the availability of the unique agent in the faction event feed
			cm:show_message_event(agent_faction_str, agent_event_title, agent_event_primary_detail, agent_event_secondary_detail, true, agent_event_id);
			out("Displaying event message for " .. agent_forename_key .. " " .. agent_family_name_key .. " in event feed");
      
      		local agent_saved_value = "unique_agent_enabled";

			-- Setting saved value, so that the hero script doesn't run again when reloaded from a saved game
			cm:set_saved_value(agent_saved_value, true);
			out("Setting saved value " .. agent_saved_value);
		end,
		false
	);
end;