Custom Start with Dilemma

Skarsnik dilemma
Skarsnik, true ruler of Drakenhof
--[[
	Script by Aexrael Dex
    Based in part on the vanilla script wh2_campaign_custom_starts
    Skarsnik's Whacky Adventure Tour to Drakenhof
]]

local skarsnik_faction_str = "wh_main_grn_crooked_moon";

function skarsnik_custom_start()
	if cm:is_new_game() then

		local skarsnik_faction_obj = cm:get_faction("wh_main_grn_crooked_moon");

		-- Checking if Skarsnik is under Human control
		if skarsnik_faction_obj:is_null_interface() == false and skarsnik_faction_obj:is_human() == true then
            -- Initiating the custom_start_dilemma listener function, we're sending through the faction string to be used in the next function
			skarsnik_custom_start_dilemma(skarsnik_faction_str);
			out("Starting dilemma listener for Skarsnik");

		-- Nothing happens
        else
            -- If Skarsnik is not under human control, there is no need to show the Dilemma, we'll leave the AI alone
			out("Listed faction are not under Human control");
		end;
	end;
end;

function skarsnik_custom_start_dilemma(faction_str)
-- Dilemma trigger listener
	core:add_listener(
		"SkarsnikCustomStartTriggerDilemma",
		"CharacterSelected",
		function(context)
			return context:character():faction():name() == faction_str;
		end,
		function(context)

			-- Checking if the faction string which we sent through matches skarsniks faction string, in which case we trigger our dilemma to show for the skarsnik player
			if faction_str == skarsnik_faction_str then
				cm:trigger_dilemma(skarsnik_faction_str, "skarsnik_start_dilemma");
				out("Dilemma triggered for Skarsnik");
			end;
		end,
		false
	);

-- Dilemma choice listener
	core:add_listener(
		"SkarsnikDilemmaChoiceMadeEvent",
		"DilemmaChoiceMadeEvent",
		function(context)
			return context:dilemma() == "skarsnik_start_dilemma"; -- we're listening for the specific Dilemma choice in the dilemma we showed to the Skarsnik player
		end,
		function(context)
			local choice = context:choice();

			-- skarsnik, checking if the dilemma choice made was the first one, note it starts at 0
			if context:dilemma() == "skarsnik_start_dilemma" then
				if choice == 0 then

					-- Setting saved value for Skarsnik Custom Start
					cm:set_saved_value("skarsnik_custom_start", true);
					out("Setting saved value skarsnik_custom_start");

					-- Initiating Custom Start for skarsnik
					skarsnik_custom_start_run();
					out("Skarsnik whacky adventure tour executed its changes");

					-- Moving the camera to Castle Drakenhof
					cm:scroll_camera_from_current(true, 1, {459.2, 326.1, 10, 0.0, 10});

                -- skarsnik, checking if the dilemma choice made was the second one, note it is labled 1
				elseif choice == 1 then

					-- Nothing happens
					out("Nothing happens");
				end;
			end;
		end,
		false
	);
end;

-- This table contains the things we want to change, to see the possible options view the vanilla game script wh2_campaign_custom_starts.lua
local skarsnik_custom_start_changes = {
    {faction = "wh_main_grn_crooked_moon",
		custom_start = {
			{
				exceptions = nil,
				changes = {
					-- skarsnik changes
                    {"region_change", "wh_main_eastern_sylvania_castle_drakenhof", "wh_main_grn_crooked_moon"}, -- Grants Drakenhof to Skarsnik
					{"teleport_character", "wh_main_grn_crooked_moon", 474,	407, 686, 419, true}, -- Teleports Skarsnik to Drakenhof
					{"force_diplomacy", "wh_main_vmp_rival_sylvanian_vamps", "wh_main_grn_crooked_moon", "war"}, -- Forces war between Templehof and Skarsnik
					{"block_diplomacy","faction:wh_main_vmp_rival_sylvanian_vamps","faction:wh_main_grn_crooked_moon","peace", false, false}, -- Prevents Peace between Templehof and Skarsnik
					{"block_diplomacy","faction:wh_main_grn_crooked_moon", "faction:wh_main_vmp_rival_sylvanian_vamps","peace", false, false} -- Prevents Peace between Skarsnik and Templehof
				}
            }
        }
	}
};

-- Executes the changes from the above table, piggybacks off CAs functions in wh2_campaign_custom_starts
function skarsnik_custom_start_run()

	-- Setting starting table undefined
	local human_factions = cm:get_human_factions();

	if human_factions[1] ~= nil then
		local custom_table = {};

		-- Checking which saved value was set
		if cm:get_saved_value("skarsnik_custom_start") == true then
			custom_table = skarsnik_custom_start_changes;
		end;

		for i = 1, #human_factions do 
			for j = 1, #custom_table do
				if human_factions[i] == custom_table[j].faction or custom_table[j].faction == "all" then
					local custom_faction = custom_table[j];
					for k = 1, #custom_faction.custom_start do
						local current_custom_start = custom_faction.custom_start;
						local exception_list = current_custom_start[k].exceptions;

						if not exception_list or not cm:are_any_factions_human(exception_list) then
							local custom_changes = current_custom_start[k].changes;
							
							for l = 1, #custom_changes do
								local changes = custom_changes[l];
								if cm:is_new_game() == true then
									cm:disable_event_feed_events(true,"all","","")
									if changes[1] == "region_change" then
										region_change(changes[2], changes[3]);
									elseif changes[1] == "primary_slot_change" then 
										primary_slot_change(changes[2], changes[3]);
									elseif changes[1] == "port_slot_change" then 
										port_slot_change(changes[2], changes[3]);
									elseif changes[1] == "secondary_slot_change" then 
										secondary_slot_change(changes[2], changes[3], changes[4]);
									elseif changes[1] == "create_army" then 
										create_army(changes[2], changes[3], changes[4], changes[5], changes[6], changes[7], changes[8], changes[9], changes[10]);
									elseif changes[1] == "create_army_for_leader" then
										create_army_for_faction_leader(changes[2], changes[3], changes[4], changes[5], changes[6]);
									elseif changes[1] == "teleport_character" then 
										teleport_character(changes[2], changes[3], changes[4], changes[5], changes[6], changes[7]);
									elseif changes[1] == "teleport_character_faction_leader" then
										teleport_character_faction_leader(changes[2], changes[3], changes[4]);
									elseif changes[1] == "hide_faction_leader" then
										hide_faction_leader(changes[2], changes[3], changes[4]);
									elseif changes[1] == "add_units" then 
										add_units_to_army(changes[2], changes[3], changes[4], changes[5]);
									elseif changes[1] == "force_diplomacy" then 
										force_diplomacy_change(changes[2], changes[3], changes[4]);
									elseif changes[1] == "abandon_region" then 
										abandon_region(changes[2]);
									elseif changes[1] == "kill_faction" then 
										kill_faction(changes[2]);											
									elseif changes[1] == "char_effect_bundle" then 
										apply_effect_bundle_character(changes[2], changes[3], changes[4], changes[5], changes[6]);
									elseif changes[1] == "add_development_points" then 
										cm:add_development_points_to_region(changes[2],changes[3])
									end
									cm:callback(function() 
										cm:disable_event_feed_events(false, "all", "", "");
									end, 0.5);
								end;
								if changes[1] == "block_diplomacy" then 
									block_diplomacy(changes[2], changes[3], changes[4], changes[5], changes[6]);
								end;
							end;
						end;
					end;
				end;
			end;
		end;
	end;
end;