forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiplomacy.lua
More file actions
168 lines (145 loc) · 5.29 KB
/
diplomacy.lua
File metadata and controls
168 lines (145 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
local _ENV = mkmodule('plugins.sort.diplomacy')
local overlay = require('plugins.overlay')
local sortoverlay = require('plugins.sort.sortoverlay')
local widgets = require('gui.widgets')
local diplomacy = df.global.game.main_interface.diplomacy
-- ----------------------
-- DiplomacyOverlay
--
DiplomacyOverlay = defclass(DiplomacyOverlay, sortoverlay.SortOverlay)
DiplomacyOverlay.ATTRS{
desc='Adds search and sort functionality to the elevate unit to barony screen.',
default_pos={x=25, y=7},
viewscreens='dwarfmode/Diplomacy',
frame={w=57, h=1},
}
function DiplomacyOverlay:init()
self:addviews{
widgets.BannerPanel{
view_id='panel',
frame={l=0, t=0, r=0, h=1},
visible=self:callback('get_key'),
subviews={
widgets.EditField{
view_id='search',
frame={l=1, t=0, r=1},
label_text="Search: ",
key='CUSTOM_ALT_S',
text='',
on_change=function(text) self:do_search(text) end,
},
},
},
}
self:register_handler('ELEVATE', diplomacy.land_holder_avail_hfid,
curry(sortoverlay.single_vector_search,
{
get_search_key_fn=self:callback('get_search_key'),
get_sort_fn=self:callback('get_sort'),
}))
end
function DiplomacyOverlay:get_key()
if diplomacy.selecting_land_holder_position then
return 'ELEVATE'
end
end
local function to_item_type_str(item_type)
return string.lower(df.item_type[item_type]):gsub('_', ' ')
end
local function make_item_description(item_type, subtype)
local itemdef = dfhack.items.getSubtypeDef(item_type, subtype)
return itemdef and string.lower(itemdef.name) or to_item_type_str(item_type)
end
local function get_preferences(unit)
if not unit then return {} end
local preferences = {}
for _, pref in ipairs(unit.status.current_soul.preferences) do
if pref.type == df.unit_preference.T_type.LikeItem and pref.active then
table.insert(preferences, make_item_description(pref.item_type, pref.item_subtype))
end
end
return preferences
end
local function get_unit(hfid)
local hf = df.historical_figure.find(hfid)
if not hf then return end
return df.unit.find(hf.unit_id)
end
function DiplomacyOverlay:get_search_key(hfid)
local unit = get_unit(hfid)
if not unit then return end
return ('%s %s'):format(sortoverlay.get_unit_search_key(unit),
table.concat(get_preferences(unit), ' '))
end
function DiplomacyOverlay:get_sort()
local _, sh = dfhack.screen.getWindowSize()
local list_height = sh - 17
local num_elems = list_height // 3
diplomacy.scroll_position_land_holder_hf = math.max(0,
math.min(diplomacy.scroll_position_land_holder_hf,
#diplomacy.land_holder_avail_hfid - num_elems))
return function(a, b)
local unita = get_unit(a)
local unitb = get_unit(b)
return #get_preferences(unita) < #get_preferences(unitb)
end
end
function DiplomacyOverlay:preUpdateLayout(parent_rect)
local margin = (parent_rect.width - 114) // 3
self.frame.w = 57 + margin
self.subviews.panel.frame.l = margin
end
-- ----------------------
-- PreferenceOverlay
--
PreferenceOverlay = defclass(PreferenceOverlay, overlay.OverlayWidget)
PreferenceOverlay.ATTRS{
desc='Adds information about unit preferences to the elevate unit to barony screen.',
default_pos={x=-34, y=9},
viewscreens='dwarfmode/Diplomacy/ElevateLandHolder',
default_enabled=true,
frame={w=29, h=1},
}
function PreferenceOverlay:init()
self:addviews{
widgets.Label{
view_id='annotations',
frame={t=0, l=0},
text='',
text_pen=COLOR_GRAY,
}
}
end
function PreferenceOverlay:preUpdateLayout(parent_rect)
local list_height = parent_rect.height - 17
self.frame.w = parent_rect.width - 85
self.frame.h = list_height
local margin = (parent_rect.width - 114) // 3
self.subviews.annotations.frame.l = margin
end
function PreferenceOverlay:onRenderFrame(dc, rect)
local margin = self.subviews.annotations.frame.l
local num_elems = self.frame.h // 3
local max_elem = math.min(#diplomacy.land_holder_avail_hfid-1,
diplomacy.scroll_position_land_holder_hf+num_elems-1)
local annotations = {}
for idx=diplomacy.scroll_position_land_holder_hf,max_elem do
table.insert(annotations, NEWLINE)
table.insert(annotations, NEWLINE)
local prefs = get_preferences(get_unit(diplomacy.land_holder_avail_hfid[idx]))
table.insert(annotations, {text='[', pen=COLOR_RED})
if #prefs == 0 then
table.insert(annotations, 'no item preferences')
else
local pref_str = ('%d preference%s: %s'):format(#prefs,
#prefs > 1 and 's' or '', table.concat(prefs, ', '))
table.insert(annotations, pref_str:sub(1, self.frame.w-(margin+2)))
end
table.insert(annotations, {text=']', pen=COLOR_RED})
table.insert(annotations, NEWLINE)
end
self.subviews.annotations:setText(annotations)
self.subviews.annotations:updateLayout()
PreferenceOverlay.super.onRenderFrame(self, dc, rect)
end
return _ENV