Adding a unique agent to the recruitment pool

--[[
	Script by Aexrael Dex
]]

local function unique_agent_init()
	-- 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

		local agent_saved_value = "unique_agent_enabled";

		-- Starting agent setup
		unique_agent_setup();

		-- 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;

	-- Starting unique agent's ancillary listener
	unique_Agent_anc_monitor();
end;

function unique_agent_setup()
	local agent_faction_key = "";			-- 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 = false;		-- 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 = 90301;			-- From campaign_group_member_criteria_values_tables

	-- Monitor activated, listening for FactionTurnStart for the faction the agent is to belong to
	core:add_listener(
		"unique agent_setup_monitor",
		"FactionTurnStart",
		function(context)
			return context:faction():name() == agent_faction_key;
		end,
		function(context)
			-- Unique agent is added to the recruitment menu
			cm:spawn_character_to_pool(agent_faction_key, 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_key);

			-- Displays an Event Message for the availability of the unique agent in the faction event feed
			cm:show_message_event(agent_faction_key, 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");
		end,
		false
	);
end;

function unique_agent_anc_monitor()
	-- Monitor activated, listening for CharacterRankUp to apply unique agent's ancillary when they reach a specific level, example of lvl 9 used
	core:add_listener(
		"unique agent_anc_monitor",
		"CharacterRankUp",
		function(context)
			return context:character():character_subtype("unique agent") and context:character():rank() >= 9;
		end,
		function(context)
			-- Checks if the unique agent already has the ancillary
			if context:character():has_ancillary("unique_agent_anc_string") then
				-- If they do, the listener is removed
				core:remove_listener("unique agent_anc_monitor");
			else
				-- Grants the unique agent the ancillary
				cm:force_add_ancillary(context:character(), "unique_agent_anc_string", true, false);
				out("Adding ancillary to unique agent");
			end;
		end,
		false
	);
end;

cm:add_first_tick_callback(function() unique_agent_init() end);
Close Menu