Repository navigation
Expand file tree
/
Copy pathlistunits.lua
More file actions
184 lines (166 loc) · 7.18 KB
/
Copy pathlistunits.lua
File metadata and controls
184 lines (166 loc) · 7.18 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
-- List active units or find a unit by searching
local help = [====[
listunits
=========
Lists active units and their positions, optionally by filter. Filtering is done against unit's full name, translated name, race, caste and profession. Regex is supported - if you're unfamiliar, search for lua regex or lua pattern matching for more info on that.
Accented characters are matched by their base ASCII versions in most cases.
Example usage:
`listunits` lists all active units
`listunits urist` lists all active units that match "urist"
`listunits goat` lists all active units that match "goat"
Fine-tuning with letter case:
`listunits iss` matches Issha Mudungarli, and also any units whose profession is fish dissector
`listunits Iss` matches Issha Mudungarli, but not the fish dissector profession
Fine-tuning with regular expressions:
`listunits man` matches both Cave Swallow Man and Cave Swallow Woman
`listunits [%s^]man` matches only Cave Swallow Man
`listunits ^urist` matches only units whose first name starts exactly with urist, ie., "urist" appearing in last name doesn't match, and also, eg., urîst doesn't match because of the accent
`listunits gen` matches both Or Ágengencesh and Bosa Gengutes
`listunits %sgen` matches Bosa Gengutes but not Or Ágengencesh
]====]
---------
-- convert accented characters to their ASCII versions, for more convenient searching
-- copied from the interwebs
-- knowing these conversions might be useful with searching in some cases, but probably not necessary so I won't include that in help
---------
function stripChars(str)
local tableAccents = {}
tableAccents["À"] = "A"
tableAccents["Á"] = "A"
tableAccents["Â"] = "A"
tableAccents["Ã"] = "A"
tableAccents["Ä"] = "A"
tableAccents["Å"] = "A"
tableAccents["Æ"] = "AE"
tableAccents["Ç"] = "C"
tableAccents["È"] = "E"
tableAccents["É"] = "E"
tableAccents["Ê"] = "E"
tableAccents["Ë"] = "E"
tableAccents["Ì"] = "I"
tableAccents["Í"] = "I"
tableAccents["Î"] = "I"
tableAccents["Ï"] = "I"
tableAccents["Ð"] = "D"
tableAccents["Ñ"] = "N"
tableAccents["Ò"] = "O"
tableAccents["Ó"] = "O"
tableAccents["Ô"] = "O"
tableAccents["Õ"] = "O"
tableAccents["Ö"] = "O"
tableAccents["Ø"] = "O"
tableAccents["Ù"] = "U"
tableAccents["Ú"] = "U"
tableAccents["Û"] = "U"
tableAccents["Ü"] = "U"
tableAccents["Ý"] = "Y"
tableAccents["Þ"] = "P"
tableAccents["ß"] = "s"
tableAccents["à"] = "a"
tableAccents["á"] = "a"
tableAccents["â"] = "a"
tableAccents["ã"] = "a"
tableAccents["ä"] = "a"
tableAccents["å"] = "a"
tableAccents["æ"] = "ae"
tableAccents["ç"] = "c"
tableAccents["è"] = "e"
tableAccents["é"] = "e"
tableAccents["ê"] = "e"
tableAccents["ë"] = "e"
tableAccents["ì"] = "i"
tableAccents["í"] = "i"
tableAccents["î"] = "i"
tableAccents["ï"] = "i"
tableAccents["ð"] = "eth"
tableAccents["ñ"] = "n"
tableAccents["ò"] = "o"
tableAccents["ó"] = "o"
tableAccents["ô"] = "o"
tableAccents["õ"] = "o"
tableAccents["ö"] = "o"
tableAccents["ø"] = "o"
tableAccents["ù"] = "u"
tableAccents["ú"] = "u"
tableAccents["û"] = "u"
tableAccents["ü"] = "u"
tableAccents["ý"] = "y"
tableAccents["þ"] = "p"
tableAccents["ÿ"] = "y"
local normalisedString = str:gsub("[%z\1-\127\194-\244][\128-\191]*", tableAccents)
return normalisedString
end
---------
-- access the list of active units, and print according to searchstring
---------
function ListUnits(searchstring)
-- variables
local tmp_name -- temporary variable for the initial name string
local unit_name -- the name for printing
local search_name -- the name for searching, containing
local id -- unit's id
local index1 -- string.find's return value
local index2 -- string.find's return value
local do_search -- operation mode, list all or search
local matches = 0 -- counter for matches
local unit_total = #(df.global.world.units.active) -- total number of active units
-- check whether a search string was provided, and set the search mode accordingly
if searchstring and searchstring ~= nil and searchstring ~= "" then
do_search = true
end
for key,unit in ipairs(df.global.world.units.active) do
tmp_name = dfhack.TranslateName(dfhack.units.getVisibleName(unit))
if not tmp_name or tmp_name == nil or tmp_name == "" then
-- if the unit doesn't have a name, set caste as its name
unit_name = dfhack.units.getCasteProfessionName(unit.race, unit.caste, unit.profession)
-- if we're in search mode, ensure we're working in UTF
if do_search then
search_name = dfhack.df2utf(unit_name)
end
else
-- if the unit has a name, we'll add translated name too, and the unit's profession data
-- this seems to be incomplete - for example, prisoners aren't shown as prisoners
unit_name = tmp_name.." ("..dfhack.TranslateName(dfhack.units.getVisibleName(unit), true).."), "..dfhack.units.getCasteProfessionName(unit.race, unit.caste, unit.profession)
-- if we're in search mode, include a non-accented version of the name + use UTF
if do_search then
search_name = dfhack.df2utf(tmp_name).." "..stripChars(dfhack.df2utf(tmp_name)).." "..dfhack.df2utf(dfhack.TranslateName(dfhack.units.getVisibleName(unit), true, true)).." "..dfhack.df2utf(dfhack.units.getCasteProfessionName(unit.race, unit.caste, unit.profession))
end
end
id = unit.id
if do_search then
-- lua's string.find() is case sensitive, so for a reasonably functional search we'll use lower case only...
-- ...unless the search string contains capital letters, in which case we don't transform letter case to make use of the extra matching power
if string.match(searchstring, "[A-Z]") ~= nil then
index1, index2 = string.find(search_name, searchstring)
else
index1, index2 = string.find(string.lower(search_name), string.lower(searchstring))
end
if index1 ~= nil then
matches = matches + 1
print(dfhack.df2console("The unit "..unit_name.." (id "..id..") matches: at coords: "..unit.pos.x..","..unit.pos.y..","..unit.pos.z))
end
else
print(dfhack.df2console("Found "..unit_name.." (id "..id..") at coords: "..unit.pos.x..","..unit.pos.y..","..unit.pos.z..""))
end
end
-- print totals regarding the search/listing operation
if do_search then
if matches == 1 then
print("Found "..matches.." match out of "..unit_total.." total active units.")
else
print("Found "..matches.." matches out of "..unit_total.." total active units.")
end
else
print("Total active units: "..#(df.global.world.units.active))
end
end
---------
-- if a search string was provided, use it
-- otherwise we'll just list everything
---------
local opt = {...}
if opt ~= nil then
ListUnits(table.concat(opt, ' '))
else
ListUnits(false)
end