Hello, this little script does 2 things: sorts all sites or regions in adventurer's log by name, distance or type.
I made it since I was really annoyed by how long it takes to find desired site in the list when I played adventure mode, now its much shorter. I dont know much about gui scripting, so there's no filter string for now (although i wish to add it).
Usage: type script name in dfhack console while viewing site/region list, arguments "a" or "t" are optional
The script itself:
advsort.lua
-- sorts sites and regions in adventure mode log view. type "?" for help
local helpstring = [[
advsort script help
usage: advsort [a|t|d] in adventure mode log view
a: sorts list of sites or regions in alphabetical order
t: sorts list of sites by type
d: sorts list of sites by distance
]]
--By Raidau
local args = {...}
if args[1] == "?" then
print(helpstring)
return
end
if df.global.gamemode ~=1 then return end
local parent = df.global.gview.view.child.child
local screenl
local function getdist(x1,y1,x2,y2)
return math.floor(math.sqrt((x1-x2)^2+(y1-y2)^2))
end
if #parent.sites>0 then
screenl = parent.sites
end
if #parent.regions>0 then
screenl = parent.regions
end
local function compSiteName (site1,site2)
return dfhack.TranslateName(site1.name,true) > dfhack.TranslateName(site2.name,true)
end
local function compSiteType (site1,site2)
return site1.type > site2.type
end
local function compSiteDist (site1,site2)
return getdist(parent.player_region_x,parent.player_region_y,site1.pos.x,site1.pos.y) > getdist(parent.player_region_x,parent.player_region_y,site2.pos.x,site2.pos.y)
end
local function swap (tab,ind1,ind2)
local temp = tab[ind1]
tab[ind1] = tab[ind2]
tab[ind2] = temp
end
local function bsort (list,compf)
for i=0, #list-1 do
for j=0, (#list-2)-i do
if compf(list[j],list[j+1]) then swap(list,j,j+1) end
end
end
end
if not screenl then return end
if args[1] == "s" or args[1] == "a" or not args[1] then
bsort(screenl,compSiteName)
end
if args[1] == "t" then
bsort(screenl,compSiteType)
end
if args[1] == "d" and #parent.sites>0 then
bsort(screenl,compSiteDist)
end
Installation:
1. create advsort.lua file in DF/hack/script folder and paste the script code there (use any basic text editor such as notepad).
2. (recommended) add following lines to your dfhack.init file:
keybinding add F@adventure_log advsort
keybinding add D@adventure_log "advsort d"