Here's the script, in case anyone's interested. Doesn't seem to crash the game for me at least.
-- show and sever economic links, by xzaxza
local help = [====[
economiclinks
======================
Displays the economic links of a site, or optionally severs them.
Usage:
economiclinks [sever] [site_id]
Both options are optional. If absent, site_id defaults to the current site.
Examples:
Running `economiclinks` without arguments displays the economic links of the current site.
Running `economiclinks 7` displays the economic links of the site with id 7.
Running `economiclinks sever` severs the economic links of the current site.
Running `economiclinks sever 7` severs the economic links of the site with id 7.
]====]
function fullname(item) --lifted from modtools/extra-gamelog
return dfhack.TranslateName(item.name)..' ('..dfhack.TranslateName(item.name ,true)..')'
end
function CountLinks(site_id)
if df.world_site.find(site_id) == nil then
print "Error: site not found."
return nil
end
local site = df.world_site.find(site_id)
local local_markets = {}
for k,v in pairs(site.entity_links) do
if v.flags.local_market then
local_markets[#local_markets+1] = v.entity_id
end
end
return #local_markets
end
function ShowLinks(site_id)
if df.world_site.find(site_id) == nil then
print "Error: site not found."
return
end
local site = df.world_site.find(site_id)
local local_markets = {}
print(dfhack.df2console('Site '..site_id..': '..fullname(site)..' at coordinates ('..site.pos.x..','..site.pos.y..')'))
for k,v in pairs(site.entity_links) do
if v.flags.local_market then
local_markets[#local_markets+1] = v.entity_id
end
end
if #local_markets > 0 then
print(dfhack.df2console('The site has the following economic links:'))
for k,v in pairs(local_markets) do
tmp_ent = df.historical_entity.find(v)
if tmp_ent ~= nil then
print(dfhack.df2console(' Entity '..v..', '..fullname(tmp_ent)))
else
print(' Entity '..v..', <unknown entity>')
end
end
else
print(dfhack.df2console('The site has no economic links.'))
end
end
function SeverLinks(site_id)
if df.world_site.find(site_id) == nil then
print "Error: site not found."
return
end
local site = df.world_site.find(site_id)
local tmp_ent
local tmp_ent_id
for k,v in pairs(site.entity_links) do
tmp_size1 = #site.entity_links
if v.flags.local_market then
tmp_ent_id = v.entity_id
tmp_ent = df.historical_entity.find(tmp_ent_id)
for k2,v2 in pairs(tmp_ent.site_links) do
if v2.target == site_id and v2.flags.local_market then
tmp_ent.site_links:erase(k2)
break
end
end
site.entity_links:erase(k)
return
end
end
end
-- main script
local opt = ...
local args = {...}
local site_id = df.global.ui.site_id
local link_count
if opt and opt ~= "" then
if opt=="sever" then
if tonumber(args[2]) then
site_id = math.floor(tonumber(args[2]))
end
link_count = CountLinks(site_id)
for i = 1,link_count,1 do
SeverLinks(site_id)
end
return
elseif tonumber(opt) then
site_id = math.floor(tonumber(opt))
ShowLinks(site_id)
return
else
print(help)
end
else
ShowLinks(site_id)
end