Skip to content

Commit c713c4d

Browse files
authored
Merge pull request #3977 from ToxicBananaParty/placesort
Add search to Zones page
2 parents ce55f8f + 924c780 commit c713c4d

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

plugins/lua/sort.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,7 @@ OVERLAY_WIDGETS = {
12981298
burrow_assignment=require('plugins.sort.unitselector').BurrowAssignmentOverlay,
12991299
slab=require('plugins.sort.slab').SlabOverlay,
13001300
world=require('plugins.sort.world').WorldOverlay,
1301+
places=require('plugins.sort.places').PlacesOverlay,
13011302
}
13021303

13031304
dfhack.onStateChange[GLOBAL_KEY] = function(sc)

plugins/lua/sort/places.lua

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
local _ENV = mkmodule('plugins.sort.places')
2+
3+
local sortoverlay = require('plugins.sort.sortoverlay')
4+
local widgets = require('gui.widgets')
5+
local utils = require('utils')
6+
7+
local info = df.global.game.main_interface.info
8+
local buildings = info.buildings
9+
10+
local zone_names = {
11+
[df.civzone_type.MeetingHall] = 'Meeting Area',
12+
[df.civzone_type.Bedroom] = 'Bedroom',
13+
[df.civzone_type.DiningHall] = 'Dining Hall',
14+
[df.civzone_type.Pen] = 'Pen Pasture',
15+
[df.civzone_type.Pond] = 'Pit Pond',
16+
[df.civzone_type.WaterSource] = 'Water Source',
17+
[df.civzone_type.Dungeon] = 'Dungeon',
18+
[df.civzone_type.FishingArea] = 'Fishing',
19+
[df.civzone_type.SandCollection] = 'Sand',
20+
[df.civzone_type.Office] = 'Office',
21+
[df.civzone_type.Dormitory] = 'Dormitory',
22+
[df.civzone_type.Barracks] = 'Barrachs',
23+
[df.civzone_type.ArcheryRange] = 'Archery Range',
24+
[df.civzone_type.Dump] = 'Garbage Dump',
25+
[df.civzone_type.AnimalTraining] = 'Animal Training',
26+
[df.civzone_type.Tomb] = 'Tomb',
27+
[df.civzone_type.PlantGathering] = 'Gather Fruit',
28+
[df.civzone_type.ClayCollection] = 'Clay'
29+
}
30+
31+
-- I used strings rather than df.civzone_type because nobody is going to search "MeadHall" they're going to search "Tavern"
32+
local language_name_types = {
33+
[df.language_name_type.SymbolFood] = 'Inn Tavern',
34+
[df.language_name_type.Temple] = 'Temple',
35+
[df.language_name_type.Hospital] = 'Hospital',
36+
[df.language_name_type.Guildhall] = 'Guildhall'
37+
}
38+
39+
local function get_default_zone_name(zone_type)
40+
return zone_names[zone_type] or ''
41+
end
42+
43+
local function get_zone_search_key(zone)
44+
local site = df.global.world.world_data.active_site[0]
45+
local result = {}
46+
47+
-- allow zones to be searchable by their name
48+
if #zone.name == 0 then
49+
table.insert(result, get_default_zone_name(zone.type))
50+
else
51+
table.insert(result, zone.name)
52+
end
53+
54+
-- allow zones w/ assignments to be searchable by their assigned unit
55+
if zone.assigned_unit ~= nil then
56+
table.insert(result, sortoverlay.get_unit_search_key(zone.assigned_unit))
57+
end
58+
59+
-- allow zones to be searchable by type
60+
if zone.location_id == -1 then -- zone is NOT a special location and we don't need to do anything special for type searching
61+
table.insert(result, df.civzone_type[zone.type]);
62+
else -- zone is a special location and we need to get its type from world data
63+
local building, success, _ = utils.binsearch(site.buildings, zone.location_id, 'id')
64+
if success and building.name then
65+
table.insert(result, language_name_types[building.name.type] or '')
66+
if building.name.has_name then
67+
table.insert(result, dfhack.TranslateName(building.name, true))
68+
end
69+
end
70+
end
71+
72+
-- allow barracks to be searchable by assigned squad
73+
for _, squad in ipairs(zone.squad_room_info) do
74+
table.insert(result, dfhack.military.getSquadName(squad.squad_id))
75+
end
76+
77+
return table.concat(result, ' ')
78+
end
79+
80+
-- ----------------------
81+
-- PlacesOverlay
82+
--
83+
84+
PlacesOverlay = defclass(PlacesOverlay, sortoverlay.SortOverlay)
85+
PlacesOverlay.ATTRS{
86+
default_pos={x=71, y=9},
87+
viewscreens='dwarfmode/Info',
88+
frame={w=40, h=6}
89+
}
90+
91+
function PlacesOverlay:init()
92+
self:addviews{
93+
widgets.BannerPanel{
94+
frame={l=0, t=0, r=0, h=1},
95+
visible=self:callback('get_key'),
96+
subviews={
97+
widgets.EditField{
98+
view_id='search',
99+
frame={l=1, t=0, r=1},
100+
label_text="Search: ",
101+
key='CUSTOM_ALT_S',
102+
on_change=function(text) self:do_search(text) end,
103+
},
104+
},
105+
},
106+
}
107+
108+
self:register_handler('ZONES', buildings.list[df.buildings_mode_type.ZONES], curry(sortoverlay.single_vector_search, {get_search_key_fn=get_zone_search_key}))
109+
end
110+
111+
function PlacesOverlay:get_key()
112+
if info.current_mode == df.info_interface_mode_type.BUILDINGS then
113+
-- TODO: Replace nested if with 'return df.buildings_mode_type[buildings.mode]' once other handlers are written
114+
-- Not there right now so it doesn't render a search bar on unsupported Places subpages
115+
if buildings.mode == df.buildings_mode_type.ZONES then
116+
return 'ZONES'
117+
end
118+
end
119+
end
120+
121+
return _ENV

0 commit comments

Comments
 (0)