persian_faction_str = "pel_persia";
persian_home_region = "pel_karia_kolossai";
turns_until_persian_escalation = 80;
BOOL_Faction_Fame_Listener_Active = true;
BOOL_Faction_War_Listener_Active = true;
BOOL_Faction_New_Turn_Listener_Active = false;
BOOL_Faction_End_Turn_Listener_Active = false;
BOOL_Time_Listener_Active = true;
SAFE_DISTANCE_FROM_OTHER_FACTIONS = 8;
SAFE_DISTANCE_FROM_PERSIAN = 2;
SAFE_DISTANCE_FROM_PLAYER = 20;
ATTEMPTS = 10;
CHANGE_ZONE = 4;
unit_list = {};
unit_list["1"] = "Pel_Baktrian_Cavalry,"..
"Pel_Baktrian_Cavalry,"..
"Pel_Baktrian_Cavalry,"..
"Pel_Baktrian_Cavalry,"..
"Pel_Immortal_Spearmen,"..
"Pel_Immortal_Spearmen,"..
"Pel_Immortal_Spearmen,"..
"Pel_Immortal_Spearmen,"..
"Pel_Immortal_Spearmen,"..
"Pel_Immortal_Spearmen,"..
"Pel_Immortal_Archers,"..
"Pel_Immortal_Archers,"..
"Pel_Immortal_Archers,"..
"Pel_Immortal_Archers,"..
"Pel_Sakae_Horse_Archers,"..
"Pel_Sakae_Horse_Archers,"..
"Pel_Sakae_Horse_Archers,"..
"Pel_Sakae_Horse_Archers,"..
"Pel_Sakae_Horse_Archers";
zone_1 = { x1 = 623 , x2 = 649 , y1 = 484, y2 = 503 };
zone_2 = { x1 = 557 , x2 = 576 , y1 = 421, y2 = 430 };
zone_3 = { x1 = 535 , x2 = 549 , y1 = 381, y2 = 396 };
zone_4 = { x1 = 532 , x2 = 540 , y1 = 356, y2 = 375 };
zone_5 = { x1 = 540 , x2 = 565 , y1 = 330, y2 = 337 };
zone_6 = { x1 = 560 , x2 = 579 , y1 = 279, y2 = 291 };
zone_7 = { x1 = 635 , x2 = 646 , y1 = 266, y2 = 273 };
zone_8 = { x1 = 667 , x2 = 682 , y1 = 201, y2 = 214 };
zone_9 = { x1 = 670 , x2 = 695 , y1 = 134, y2 = 141 };
zone_list = {zone_1 , zone_2 , zone_3, zone_4, zone_5, zone_6, zone_7, zone_8, zone_9 };
--[[This script summons armies for the persian empire. There are three ways for the "summoning" to be activated.
- The Siege Path: Player sieges certain settlements, that are under control of the persian empire
- The Hate Path: Player's imperium rises too much..
- Time path: Script will activate no matter what on 60 turns
Both these paths activate a new listener , the "new turn listener" , that waits for the player's new turn to show the pesrian escalation
message and to summon the armies.
]]
--[[
**********************************************************************************************************************************************************
*********************************************SIEGE PATH***********************************************
**********************************************************************************************************************************************************
]]
function add_persia_siege_listener()
if not BOOL_Faction_War_Listener_Active then
return
end
output("Attaching siege listener")
cm:add_listener(
"persia_siege_listener", -- a name for the listener, used to cancel it later
"CharacterBesiegesSettlement", -- the event to listen for
function(context)
output("context is " .. context:character():faction():name());
output("context is " .. context:region():owning_faction():name());
if context:region():owning_faction():name() == persian_faction_str then
if context:region():name() == "pel_mysia_pergamon" or context:region():name() == "pel_lydia_sardeis" or context:region():name() == "pel_karia_kibyra" then
return true
end
end
return false
end, -- this is a condition to listen for ??? it means this listener will only fire when the subject faction is local_faction, which is the player (this is set in scripting.lua)
function()
BOOL_Faction_End_Turn_Listener_Active = true;
add_end_of_turn_listener();
end, -- calls the function local_faction_has_levelled_up() when the condition is positive
false -- persistent flag ??? this listener will continue to listen after it???s made a positive match
 

end
--[[
**********************************************************************************************************************************************************
*********************************************HATE PATH***********************************************
**********************************************************************************************************************************************************
]]
function add_imperium_listener()
--If the listener has already performed its duty abort
if not BOOL_Faction_Fame_Listener_Active then
return
end
output("Attaching imperium listener")
cm:add_listener(
"faction_fame_listener", -- a name for the listener, used to cancel it later
"FactionFameLevelUp", -- the event to listen for
function(context) return context.string == local_faction end, -- this is a condition to listen for ??? it means this listener will only fire when the subject faction is local_faction, which is the player (this is set in scripting.lua)
function() local_faction_has_levelled_up() end, -- calls the function local_faction_has_levelled_up() when the condition is positive
true -- persistent flag ??? this listener will continue to listen after it???s made a positive match
 

end
function local_faction_has_levelled_up()
local faction = get_faction(scripting, local_faction);
-- get its imperium level
local imperium_level = faction:imperium_level();
output("imperium_level is ".. tostring(imperium_level));
-- if its sufficiently high (2, in this example), we make the Persians hate us
if imperium_level >= 7 then
output("Making " .. persian_faction_str .. " hate " .. local_faction .. "!");
BOOL_Faction_End_Turn_Listener_Active = true;
add_end_of_turn_listener();
end
end
--[[
**********************************************************************************************************************************************************
*********************************************TIME PATH***********************************************
**********************************************************************************************************************************************************
]]
function add_time_listener()
if not BOOL_Time_Listener_Active then
return
end
cm:add_listener(
"introduction_message_listener", -- a name for the listener, used to cancel it later
"FactionTurnStart", -- the event to listen for
function(context)
output("Turns until persian escalation " .. turns_until_persian_escalation - cm:model():turn_number() );
return cm:model():turn_number() == turns_until_persian_escalation and context:faction():name() == local_faction end, -- this is a condition to listen for ??? it means this listener will only fire when the subject faction is local_faction, which is the player (this is set in scripting.lua)
function()
--output("I made a positive match");
BOOL_Faction_End_Turn_Listener_Active = true;
add_end_of_turn_listener();
end, -- calls the function local_faction_has_levelled_up() when the condition is positive
false -- persistent flag ??? this listener will continue to listen after it???s made a positive match
 
end
--[[
**********************************************************************************************************************************************************
*********************************************END TURN ACTIVATION***********************************************
**********************************************************************************************************************************************************
]]
function add_end_of_turn_listener()
if not BOOL_Faction_End_Turn_Listener_Active then
return
end
cm:add_listener(
"end_turn_listener", -- a name for the listener, used to cancel it later
"FactionAboutToEndTurn", -- the event to listen for
function(context) return context:faction():name() == local_faction end, -- this is a condition to listen for ??? it means this listener will only fire when the subject faction is local_faction, which is the player (this is set in scripting.lua)
function() turn_about_to_end() end, -- calls the function local_faction_has_levelled_up() when the condition is positive
false -- persistent flag ??? this listener will continue to listen after it???s made a positive match
 

-- body
end
function turn_about_to_end()
-- Checking whether the persians are alive
output("I am turn about to end before checking for persians")
if cm:model():world():faction_by_key(persian_faction_str):character_list():is_empty() and cm:model():world():faction_by_key(persian_faction_str) then
output("Persians are dead!");
remove_listeners();
BOOL_Faction_End_Turn_Listener_Active = false;
return;
end
output("I am turn about to end after checking for persians")
remove_listeners();
break_callias_peace();
persians_become_enemies();
begin_spawn_sequence();
BOOL_Faction_End_Turn_Listener_Active = false;
BOOL_Faction_New_Turn_Listener_Active = true;
add_new_turn_listener();
end
--[[
**********************************************************************************************************************************************************
*********************************************NEW TURN ACTIVATION***********************************************
**********************************************************************************************************************************************************
]]
function add_new_turn_listener()
--remove previous listeners <-MAKE THAT BETER LOOKING
if not BOOL_Faction_New_Turn_Listener_Active then
return
end
output("Attaching new turn Listener")
cm:add_listener(
"Time_listener", -- a name for the listener, used to cancel it later
"FactionTurnStart", -- the event to listen for
function(context) return context:faction():name() == local_faction end, -- this is a condition to listen for ??? it means this listener will only fire when the subject faction is local_faction, which is the player (this is set in scripting.lua)
function() new_turn_started() end, -- calls the function local_faction_has_levelled_up() when the condition is positive
false -- persistent flag ??? this listener will continue to listen after it???s made a positive match
 

end
function new_turn_started()
persian_escalation_message();
BOOL_Faction_New_Turn_Listener_Active = false;
end
--[[************************************************************************************************************************************************************************************
**************************************************************************************************************************************************************************************]]
function begin_spawn_sequence()
spawn_army(0, 0,persian_faction_str,unit_list["1"],"1", function() spawn_2_army() end)
end
function spawn_2_army()
spawn_army(0, 0,persian_faction_str,unit_list["1"],"2", function() spawn_3_army() end)
end
function spawn_3_army()
spawn_army(0, 0,persian_faction_str,unit_list["1"],"3", function() spawn_4_army() end)
end;
function spawn_4_army()
spawn_army(0, 0,persian_faction_str,unit_list["1"],"4", function() spawn_5_army() end)
end;
function spawn_5_army()
spawn_army(0, 0,persian_faction_str,unit_list["1"],"5", false )
end;
function persian_escalation_message()
cm:show_message_event("custom_event_1", 349,238);
end
function spawn_army(x,y,faction,unit_list,army_id, callback)
if x == 0 and y == 0 then
x,y = generate_coordinates();
end
output("x is " .. tostring(x) .. "y is " .. tostring(y));
cm:create_force(
faction, -- name of faction
unit_list, -- comma-separated units
persian_home_region, -- home region
x, -- logical x
y, -- logical y
army_id, -- string id for army
true,
function(cqi)
reduce_army_upkeep(cqi) --reduce upkeep is standard issue
if is_function(callback) then
callback(); --this is where we call the next one in the chain
end;
end -- goes via command queue
 

;
end
function generate_coordinates()
--pick a zone
for j = 0 , CHANGE_ZONE , 1 do
local zone = zone_list[math.random(1, #zone_list)];
for i = 0 , ATTEMPTS , 1 do
local x_cord = math.random(zone.x1 , zone.x2)
local y_cord = math.random(zone.y1 , zone.y2) --Pay attention here y2 is actually smaller!!
if is_valid_spawn_point(x_cord,y_cord,true) then
return x_cord,y_cord;
end
end
end
-- do it again no safe-checks
output("SAFETY DEACTIVATED");
for j = 0 , CHANGE_ZONE , 1 do
local zone = zone_list[math.random(1, #zone_list)];
for i = 0 , ATTEMPTS , 1 do
local x_cord = math.random(zone.x1 , zone.x2)
local y_cord = math.random(zone.y1 , zone.y2) --Pay attention here y2 is actually smaller!!
if is_valid_spawn_point(x_cord,y_cord,false) then --run it with no safety checks
return x_cord,y_cord;
end
end
end
end
function is_valid_spawn_point(x,y,safe_check)
local faction_list = cm:model():world():faction_list();
local safe_distance;
for i = 0, faction_list:num_items() - 1 do
local current_faction = faction_list:item_at(i);
if current_faction:name() == persian_faction_str then
safe_distance = SAFE_DISTANCE_FROM_PERSIAN;
end
if current_faction:is_human() then
safe_distance = SAFE_DISTANCE_FROM_PLAYER;
else
safe_distance = SAFE_DISTANCE_FROM_OTHER_FACTIONS;
end
-- if the safe check is false it means that we failed already
if safe_check == false then
safe_distance = 3;
end
local military_force_list = current_faction:military_force_list();
for j = 0, military_force_list:num_items() - 1 do
local current_military_force = military_force_list:item_at(j);
if current_military_force:has_general() then
local char = current_military_force:general_character();
if distance(char:logical_position_x(), char:logical_position_y(), x, y) < safe_distance then
return false;
end
end
end
end
return true;
end
function reduce_army_upkeep( cqi)
output("reducing upkeep, general cqi is " .. tostring(cqi));
cm:apply_effect_bundle_to_characters_force("pel_persian_army_upkeep",cqi,-1);
output("Done");
end
function distance( x1, y1, x2, y2 )
return math.sqrt( (x2-x1)^2 + (y2-y1)^2 )
end
--So the listener is loaded every time the game Load
function remove_listeners()
BOOL_Faction_Fame_Listener_Active = false;
BOOL_Faction_War_Listener_Active = false;
BOOL_Time_Listener_Active = false;
cm:remove_listener("faction_fame_listener");
cm:remove_listener("persia_siege_listener");
cm:remove_listener("Time_listener");
end
cm:register_saving_game_callback(
function(context)
cm:save_value("BOOL_Faction_Fame_Listener_Active", BOOL_Faction_Fame_Listener_Active, context);
end
)
cm:register_loading_game_callback(
function(context)
BOOL_Faction_Fame_Listener_Active = cm:load_value("BOOL_Faction_Fame_Listener_Active", true, context);
end
);
cm:register_saving_game_callback(
function(context)
cm:save_value("BOOL_Faction_War_Listener_Active", BOOL_Faction_War_Listener_Active, context);
end
)
cm:register_loading_game_callback(
function(context)
BOOL_Faction_War_Listener_Active = cm:load_value("BOOL_Faction_War_Listener_Active", true, context);
end
);
cm:register_saving_game_callback(
function(context)
cm:save_value("BOOL_Faction_New_Turn_Listener_Active", BOOL_Faction_New_Turn_Listener_Active, context);
end
)
cm:register_loading_game_callback(
function(context)
BOOL_Faction_New_Turn_Listener_Active = cm:load_value("BOOL_Faction_New_Turn_Listener_Active", false, context);
end
);
cm:register_saving_game_callback(
function(context)
cm:save_value("BOOL_Faction_End_Turn_Listener_Active", BOOL_Faction_End_Turn_Listener_Active, context);
end
)
cm:register_loading_game_callback(
function(context)
BOOL_Faction_End_Turn_Listener_Active = cm:load_value("BOOL_Faction_End_Turn_Listener_Active", false, context);
end
);
cm:register_saving_game_callback(
function(context)
cm:save_value("BOOL_Time_Listener_Active", BOOL_Time_Listener_Active, context);
end
)
cm:register_loading_game_callback(
function(context)
BOOL_Time_Listener_Active = cm:load_value("BOOL_Time_Listener_Active", true, context);
end
);