add early exit
[QuestHelper.git] / director_find.lua
blobc75a95a16525015a6a740fc3da9e076936c6d302
1 QuestHelper_File["director_find.lua"] = "Development Version"
2 QuestHelper_Loadtime["director_find.lua"] = GetTime()
4 local function getitall(name)
5 local segments = {}
6 for i = 1, #name - 1 do
7 local sub = name:sub(i, i + 1):lower()
8 if not sub:find(" ") then
9 local dbi = DB_GetItem("find", sub:byte(1) * 256 + sub:byte(2), true)
10 --print("inserting", sub)
11 if not dbi then return {} end
12 segments[dbi] = true
13 end
14 end
16 local segwork = {}
17 for k in pairs(segments) do
18 table.insert(segwork, {k, 1, k[1]})
19 end
21 local found = {}
23 while true do
24 local lowest = segwork[1][3]
25 local highest = segwork[1][3]
26 local lid = 1
27 for i = 2, #segwork do
28 local v = segwork[i][3]
29 --print("loop", i, lowest, highest, v, segwork[i][2])
30 if v < lowest then
31 --print("lower")
32 lowest, lid = v, i
33 end
34 if v > highest then
35 --print("higher")
36 highest = v
37 end
38 end
40 if lowest == highest then
41 table.insert(found, lowest)
42 segwork[1][2] = segwork[1][2] + 1
43 if not segwork[1][1][segwork[1][2]] then break end
44 segwork[1][3] = segwork[1][3] + segwork[1][1][segwork[1][2]]
45 else
46 while segwork[lid][3] < highest do
47 segwork[lid][2] = segwork[lid][2] + 1
48 if not segwork[lid][1][segwork[lid][2]] then break end
49 segwork[lid][3] = segwork[lid][3] + segwork[lid][1][segwork[lid][2]]
50 end
51 end
53 if not segwork[lid][1][segwork[lid][2]] then break end
54 end
56 return found
57 end
59 local function generate_objective(dbi)
60 local clooster = {}
62 local why = {desc = dbi.name, tracker_desc = dbi.name}
63 for _, v in ipairs(dbi.loc) do
64 QuestHelper: Assert(QuestHelper_ParentLookup)
65 QuestHelper: Assert(QuestHelper_ParentLookup[v.p], v.p)
66 table.insert(clooster, {loc = {x = v.x, y = v.y, c = QuestHelper_ParentLookup[v.p], p = v.p}, cluster = clooster, tracker_hidden = true, why = why, map_desc = {QuestHelper:HighlightText(dbi.name)}, tracker_desc = dbi.name, map_suppress_ignore = true, map_custom_menu = function (menu) QuestHelper:CreateMenuItem(menu, QHText("FIND_REMOVE")):SetFunction(function () QH_Route_ClusterRemove(clooster) end) end})
67 end
69 QH_Route_ClusterAdd(clooster)
70 end
72 local msfires = {desc = QHText("FIND_CUSTOM_LOCATION"), tracker_desc = QHText("FIND_CUSTOM_LOCATION")}
74 function QH_FindName(name)
75 local locd = name:match("^loc (.+)")
76 if locd then
77 -- I just know some guy's gonna get pissed off because he was trying to find "loc crocolisk" or something, but screw him
78 local locx, locy, locz
80 locx, locy = locd:match("^(%d+) (%d+)$")
81 if locx and locy then
82 locx, locy = tonumber(locx), tonumber(locy)
83 locz = QuestHelper_NameLookup[QuestHelper_IndexLookup[QuestHelper.routing_c][QuestHelper.routing_z]]
84 end
86 if not locx then
87 locz, locx, locy = locd:match("^(.+) (%d+) (%d+)$")
88 locx, locy = tonumber(locx), tonumber(locy)
89 end
91 if locz then
92 for z, nam in pairs(QuestHelper_NameLookup) do
93 if nam:lower():find(locz:lower()) then
94 locz = z
95 break
96 end
97 end
99 if type(locz) == "number" then
100 local ec, ez = unpack(QuestHelper_ZoneLookup[locz])
101 local c, x, y = QuestHelper.Astrolabe:GetAbsoluteContinentPosition(ec, ez, locx / 100, locy / 100)
102 local node = {loc = {x = x, y = y, p = locz, c = QuestHelper_ParentLookup[locz]}, why = msfires, map_desc = {QHText("FIND_CUSTOM_LOCATION")}, tracker_desc = QHText("FIND_CUSTOM_LOCATION"), tracker_hidden = true}
103 local cluster = {node}
104 node.cluster = cluster
106 node.map_suppress_ignore = true
107 node.map_custom_menu = function (menu) QuestHelper:CreateMenuItem(menu, QHText("FIND_REMOVE")):SetFunction(function () QH_Route_ClusterRemove(cluster) end) end
109 QH_Route_ClusterAdd(cluster)
112 else
113 if not DB_Ready() then
114 print(QHText("FIND_NOT_READY"))
115 return
118 local found = getitall(name)
120 local mennix = QuestHelper:CreateMenu()
121 QuestHelper:CreateMenuTitle(mennix, QHText("RESULTS_TITLE"))
123 local made_item = false
125 local found_db = {}
126 local has_name = {}
127 local needs_postfix = {}
129 for _, v in ipairs(found) do
130 local dbi = DB_GetItem("monster", v)
131 assert(dbi)
133 if dbi.loc then
134 table.insert(found_db, dbi)
136 if has_name[dbi.name] then needs_postfix[dbi.name] = true end
137 has_name[dbi.name] = true
141 table.sort(found_db, function (a, b) return a.name < b.name end)
143 for _, v in ipairs(found_db) do
144 made_item = true
146 local name = v.name
148 if needs_postfix[name] then name = name .. " (" .. QuestHelper_NameLookup[v.loc[1].p] .. ")" end
150 local opt = QuestHelper:CreateMenuItem(mennix, name)
151 opt:SetFunction(generate_objective, v)
154 if not made_item then
155 QuestHelper:CreateMenuItem(mennix, QHText("NO_RESULTS"))
158 mennix:ShowAtCursor()