TourGuide
[WoW-TourGuide.git] / TourGuide.lua
blob74acd8687eb6842850d93e8365609ab88e9cfc08
2 local OptionHouse = DongleStub("OptionHouse-1.0")
5 local myfaction = UnitFactionGroup("player")
7 TourGuide = DongleStub("Dongle-1.0"):New("TourGuide")
8 if tekDebug then TourGuide:EnableDebug(1, tekDebug:GetFrame("TourGuide")) end
9 TourGuide.guides = {}
10 TourGuide.guidelist = {}
11 TourGuide.nextzones = {}
14 TourGuide.icons = setmetatable({
15 ACCEPT = "Interface\\GossipFrame\\AvailableQuestIcon",
16 COMPLETE = "Interface\\Icons\\Ability_DualWield",
17 TURNIN = "Interface\\GossipFrame\\ActiveQuestIcon",
18 RUN = "Interface\\Icons\\Ability_Tracking",
19 MAP = "Interface\\Icons\\Ability_Spy",
20 FLY = "Interface\\Icons\\Ability_Druid_FlightForm",
21 TRAIN = "Interface\\GossipFrame\\trainerGossipIcon",
22 SETHEARTH = "Interface\\Icons\\Spell_Holy_ElunesGrace",
23 HEARTH = "Interface\\Icons\\INV_Misc_Rune_01",
24 NOTE = "Interface\\Icons\\INV_Misc_Note_01",
25 GRIND = "Interface\\Icons\\INV_Stone_GrindingStone_05",
26 USE = "Interface\\Icons\\INV_Misc_Bag_08",
27 BUY = "Interface\\Icons\\INV_Misc_Coin_01",
28 BOAT = "Interface\\Icons\\Spell_Frost_SummonWaterElemental",
29 GETFLIGHTPOINT = "Interface\\Icons\\Spell_Nature_GiftoftheWaterSpirit",
30 }, {__index = function() return "Interface\\Icons\\INV_Misc_QuestionMark" end})
33 local actiontypes = {
34 A = "ACCEPT",
35 C = "COMPLETE",
36 T = "TURNIN",
37 R = "RUN",
38 t = "TRAIN",
39 H = "HEARTH",
40 h = "SETHEARTH",
41 G = "GRIND",
42 F = "FLY",
43 f = "GETFLIGHTPOINT",
44 N = "NOTE",
45 B = "BUY",
46 b = "BOAT",
47 U = "USE",
51 function TourGuide:Initialize()
52 self.db = self:InitializeDB("TourGuideAlphaDB", {
53 char = {
54 turnedin = {},
55 cachedturnins = {},
58 self.turnedin = self.db.char.turnedin
59 self.cachedturnins = self.db.char.cachedturnins
61 self.db.char.currentguide = self.db.char.currentguide or self.guidelist[1]
62 self:LoadGuide(self.db.char.currentguide)
63 end
66 function TourGuide:Enable()
67 local _, title = GetAddOnInfo("TourGuide")
68 local author, version = GetAddOnMetadata("TourGuide", "Author"), GetAddOnMetadata("TourGuide", "Version")
69 local oh = OptionHouse:RegisterAddOn("Tour Guide", title, author, version)
70 oh:RegisterCategory("Guides", TourGuide, "CreateGuidesPanel")
71 oh:RegisterCategory("Objectives", TourGuide, "CreateObjectivePanel")
73 for _,event in pairs(self.TrackEvents) do self:RegisterEvent(event) end
74 self.TrackEvents = nil
75 self:UpdateStatusFrame()
76 end
79 function TourGuide:RegisterGuide(name, nextzone, faction, sequencefunc)
80 if faction ~= myfaction then return end
81 self.guides[name] = sequencefunc
82 self.nextzones[name] = nextzone
83 table.insert(self.guidelist, name)
84 end
87 function TourGuide:LoadGuide(name)
88 if not name then return end
90 self.db.char.currentguide = name
91 if not self.guides[name] then self.db.char.currentguide = self.guidelist[1] end
92 self:DebugF(1, "Loading guide: %s", name)
93 self:ParseObjectives(self.guides[self.db.char.currentguide]())
94 end
97 function TourGuide:LoadNextGuide()
98 local name = self.nextzones[self.db.char.currentguide]
99 if not name then return end
101 for i,quest in ipairs(self.quests) do self.turnedin[quest] = nil end -- Clean out old completed objectives, to avoid conflicts
103 self:LoadGuide(name)
104 self:UpdateGuidesPanel()
105 return true
109 function TourGuide:GetQuestLogIndexByName(name)
110 name = name or self.quests[self.current]
111 name = name:gsub("%s%(Part %d+%)", "")
112 for i=1,GetNumQuestLogEntries() do
113 if GetQuestLogTitle(i) == name then return i end
117 function TourGuide:GetQuestDetails(name)
118 if not name then return end
120 local i = self:GetQuestLogIndexByName(name)
121 local complete = i and select(7, GetQuestLogTitle(i)) == 1
122 return i, complete
126 local function FindBagSlot(itemid)
127 for bag=0,4 do
128 for slot=1,GetContainerNumSlots(bag) do
129 local item = GetContainerItemLink(bag, slot)
130 if item and string.find(item, "item:"..itemid) then return bag, slot end
136 function TourGuide:GetCurrentObjectiveInfo()
137 return self:GetObjectiveInfo(self.current)
141 function TourGuide:GetObjectiveInfo(i)
142 local action, quest, note = self.actions[i], self.quests[i], self.notes[i]
143 if not action then return end
145 local logi, complete = self:GetQuestDetails(quest)
146 local hasitem = action == "ITEM" and self.questitems[i] and FindBagSlot(self.questitems[i])
148 return action, quest:gsub("@.*@", ""), note, logi, complete, hasitem, self.turnedin[quest], quest, self.useitems[i], self.optional[i]
152 local myclass = UnitClass("player")
153 local titlematches = {"For", "A", "The", "Or", "In", "Then", "From", "To"}
154 local function ParseQuests(...)
155 local accepts, turnins, completes = {}, {}, {}
156 local uniqueid = 1
157 local actions, notes, quests, items, useitems, optionals = {}, {}, {}, {}, {}, {}
158 local i = 1
160 for j=1,select("#", ...) do
161 local text = select(j, ...)
162 local _, _, class = text:find("|C|([^|]+)|")
164 if text ~= "" and (not class or class == myclass) then
165 local _, _, action, quest = text:find("^(%a) ([^|]*)")
166 assert(actiontypes[action], "Unknown action: "..text)
167 quest = quest:trim()
168 if not (action == "I" or action == "A" or action =="C" or action =="T") then
169 quest = quest.."@"..uniqueid.."@"
170 uniqueid = uniqueid + 1
173 actions[i], quests[i] = actiontypes[action], quest
175 local _, _, note = string.find(text, "|N|([^|]+)|")
176 if note then notes[i] = note end
178 local _, _, item = string.find(text, "|I|(%d+)|")
179 if item then items[i] = item end
181 local _, _, useitem = string.find(text, "|U|(%d+)|")
182 if useitem then useitems[i] = useitem end
184 if string.find(text, "|O|") then optionals[i] = true end
186 i = i + 1
188 -- Debuggery
189 if action == "A" then accepts[quest] = true
190 elseif action == "T" then turnins[quest] = true
191 elseif action == "C" then completes[quest] = true end
193 if action == "A" or action == "C" or action == "T" then
194 -- Catch bad Title Case
195 for _,word in pairs(titlematches) do
196 if quest:find("[^:]%s"..word.."%s") or quest:find("[^:]%s"..word.."$") or quest:find("[^:]%s"..word.."@") then
197 TourGuide:DebugF(1, "%s %s -- Contains bad title case", action, quest)
201 local _, _, comment = string.find(text, "(|.|[^|]+)$")
202 if comment then TourGuide:Debug(1, "Unclosed comment: ".. comment) end
206 -- More debug
207 for quest in pairs(accepts) do if not turnins[quest] then TourGuide:DebugF(1, "Quest has no 'turnin' objective: %s", quest) end end
208 for quest in pairs(turnins) do if not accepts[quest] then TourGuide:DebugF(1, "Quest has no 'accept' objective: %s", quest) end end
209 for quest in pairs(completes) do if not accepts[quest] and not turnins[quest] then TourGuide:DebugF(1, "Quest has no 'accept' and 'turnin' objectives: %s", quest) end end
211 return actions, notes, quests, items, useitems, optionals
215 function TourGuide:ParseObjectives(text)
216 self.actions, self.notes, self.quests, self.questitems, self.useitems, self.optional = ParseQuests(string.split("\n", text))
220 function TourGuide:SetTurnedIn(i, value)
221 if not i then
222 i = self.current
223 value = true
226 if value then value = true else value = nil end -- Cleanup to minimize savedvar data
228 self.turnedin[self.quests[i]] = value
229 self:DebugF(1, "Set turned in %q = %s", self.quests[i], tostring(value))
230 self:UpdateStatusFrame()
234 function TourGuide:CompleteQuest(name)
235 local i = self.current + 1
236 repeat
237 action, quest, note, logi, complete, hasitem, turnedin, fullquestname = self:GetObjectiveInfo(i)
238 if action == "TURNIN" and not turnedin and name == quest:gsub("%s%(Part %d+%)", "") then
239 self:DebugF(1, "Saving early quest turnin %q", quest)
240 return self:SetTurnedIn(i, true)
242 i = i + 1
243 until not action
244 self:DebugF(1, "Quest %q not found!", name)