Code:
persian_faction_str = "pel_persia";
persian_home_region = "pel_karia_kolossai";
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.
]]
function GetTurnNum()
return scripting.game_interface:model():turn_number();
end
local function ScriptPersian(context)
turn_num = GetTurnNum();
if context:faction():name() == "pel_persia"
and context:faction():is_human() == false
and turn_num == 1
then scripting.game_interface:treasury_mod("pel_persia", 10000), function() persian_escalation_message() end)
end
function persian_escalation_message()
cm:show_message_event("custom_event_1", 349,238), function() begin_spawn_sequence() end)
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 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