Skip to content

Instantly share code, notes, and snippets.

@bukowa
Last active February 19, 2025 15:47
Show Gist options
  • Save bukowa/22502096368f1395e49ce62acdbe69a7 to your computer and use it in GitHub Desktop.
Save bukowa/22502096368f1395e49ce62acdbe69a7 to your computer and use it in GitHub Desktop.
tried
-- Helper function to get the faction list from the game model
function getFactionList()
return scripting.game_interface:model():world():faction_list()
end
-- Helper function to get the region list from a faction
function getRegionList(faction)
return faction:region_list()
end
-- Helper function to iterate over a faction list
function iterateFactionList(factionList, callback)
for i = 0, factionList:num_items() - 1 do
local faction = factionList:item_at(i)
callback(faction)
end
end
-- Helper function to iterate over a region list of a faction
function iterateRegionList(regionList, callback)
for i = 0, regionList:num_items() - 1 do
local region = regionList:item_at(i)
callback(region)
end
end
-- Helper function to trigger a rebellion in a region
function triggerRebellionInRegion(region, unitList)
scripting.game_interface:force_rebellion_in_region(region, 10, unitList, 0, 0, true)
end
-- Helper function to transfer region ownership to another faction
function transferRegionToFaction(region, factionName)
scripting.game_interface:transfer_region_to_faction(region, factionName)
end
-- Helper function to check if a faction has any regions
function hasRegions(faction)
return faction:region_list():num_items() ~= 0
end
-- Main function that handles the settlement selection logic
scripting.AddEventCallBack('OnSettlementSelected', function(context)
-- Get the list of factions
local factionList = getFactionList()
-- Iterate over all factions
iterateFactionList(factionList, function(faction)
local factionName = faction:name()
local regionList = getRegionList(faction)
-- Iterate over all regions of the current faction
iterateRegionList(regionList, function(region)
local regionName = region:name()
-- Trigger a rebellion in the region
triggerRebellionInRegion(regionName, "Rom_Equites_Late_Allied")
-- Get the list of factions again to transfer regions
local factionList2 = getFactionList()
-- Iterate through the factions again to find one without regions
iterateFactionList(factionList2, function(faction2)
-- Skip factions that already own regions
if hasRegions(faction2) then return end
local faction2Name = faction2:name()
-- Transfer the region to the new faction
transferRegionToFaction(regionName, faction2Name)
-- Check if the transfer was successful
if region:owning_faction():name() == faction2Name then
end
end)
end)
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment