Code:
-------------------------------------------------------
-- startup diplomacy
-------------------------------------------------------
-- sets up alliances and war between factions on the campaign
function setup_diplomacy()
-- prevent diplomacy event messages from being generated
cm:disable_event_feed_events(false, "wh_event_category_diplomacy", "", "");
local human_factions = cm:get_human_factions();
local num_human_factions = #human_factions;
out("");
if num_human_factions == 1 then
out(" *** setup_diplomacy() called, there is 1 human faction: " .. human_factions[1]);
out("");
setup_all_factions_at_war();
else
out(" *** setup_diplomacy() called, there are " .. tostring(num_human_factions) .. " human factions:");
for i = 1, #human_factions do
out("\t" .. human_factions[i]);
end;
out("");
if num_human_factions == 2 then
setup_random_alliances_player_competitive_1v1();
elseif num_human_factions == 4 then
setup_predetermined_alliances_player_competitive_2v2();
end;
end;
-- allow diplomacy event messages to be generated again
cm:disable_event_feed_events(true, "wh_event_category_diplomacy", "", "");
end;
-- every faction at war with every other faction
function setup_all_factions_at_war()
out(" *** Setting every faction at war with every other faction");
local faction_list = cm:model():world():faction_list();
for i = 0, faction_list:num_items() - 1 do
if faction_list:item_at(i):name() ~= "wh3_prologue_ksl_kislev_survivors" then
set_faction_at_war_with_all_other_factions(faction_list:item_at(i):name());
end
end;
end;
-- ally each player with one non-player faction at random
function setup_random_alliances_player_competitive_1v1()
local faction_list = cm:model():world():faction_list();
-- get a list of non-human factions
local non_human_factions = {};
for i = 0, faction_list:num_items() - 1 do
local current_faction = faction_list:item_at(i);
if not current_faction:is_human() and not current_faction:is_dead() and current_faction:region_list():num_items() > 0 then
table.insert(non_human_factions, current_faction:name());
end
end;
-- randomly sort human faction list
local human_factions = cm:get_human_factions();
human_factions = cm:random_sort(human_factions);
-- don't proceed if we have less human factions than non-human
if #non_human_factions < #human_factions then
script_error("WARNING: setup_random_alliances_player_competitive_1v1() could find " .. tostring(#human_factions) .. " but only " .. tostring(#non_human_factions) .. " non-human factions, not allying anyone");
return;
end;
-- randomly sort non-human faction list
non_human_factions = cm:random_sort(non_human_factions);
out(" *** Players are playing 1v1 competitive, setting random alliances");
-- set each human faction to be allied to one non-human faction and at war with the rest
for i = 1, #human_factions do
out("\t* ALLYING " .. human_factions[i] .. " to " .. non_human_factions[i]);
cm:force_alliance(human_factions[i], non_human_factions[i], true);
set_faction_at_war_with_all_other_factions(human_factions[i], non_human_factions[i])
end;
-- set each non-human faction to be at war with every other non-human faction
for i = 1, #non_human_factions do
set_faction_at_war_with_all_other_factions(non_human_factions[i], human_factions);
end;
out("");
end;
function setup_predetermined_alliances_player_competitive_2v2()
-- set up specific alliances
local human_factions = cm:get_human_factions();
out(" *** Players are playing 2v2 competitive, setting specified alliances");
cm:force_alliance(human_factions[1], human_factions[2], true);
out("\t* ALLYING " .. human_factions[1] .. " to " .. human_factions[2]);
set_faction_at_war_with_all_other_factions(human_factions[1], human_factions[2])
set_faction_at_war_with_all_other_factions(human_factions[2], human_factions[1])
cm:force_alliance(human_factions[3], human_factions[4], true);
out("\t* ALLYING " .. human_factions[3] .. " to " .. human_factions[4]);
set_faction_at_war_with_all_other_factions(human_factions[3], human_factions[4])
set_faction_at_war_with_all_other_factions(human_factions[4], human_factions[3])
end;
function setup_random_alliances_player_competitive_2v2()
-- TBD
end;
-- set the specified faction at war with all other factions in the campaign, with the exception of those in the supplied list (list can also be a single string)
function set_faction_at_war_with_all_other_factions(faction_name, exception_list)
local faction = cm:get_faction(faction_name);
if not (faction and not faction:is_dead()) then
return;
end;
local faction_list = cm:model():world():faction_list();
if not is_table(exception_list) then
exception_list = {exception_list};
end;
for i = 0, faction_list:num_items() - 1 do
local current_faction = faction_list:item_at(i);
if not current_faction:is_dead() then
local current_faction_name = current_faction:name();
local exclude_current_faction = false;
if current_faction_name == faction_name then
exclude_current_faction = true;
else
for j = 1, #exception_list do
if exception_list[j] == current_faction_name then
exclude_current_faction = true;
break;
end;
end;
end;
if not exclude_current_faction then
out("\t * " .. faction_name .. " is now at war with " .. current_faction_name);
if current_faction_name ~= "wh3_prologue_ksl_kislev_survivors" then
cm:force_declare_war(faction_name, current_faction_name, false, false);
end
end;
end;
end;
end;