Made purge command reset the locale of the saved data, and added files for 0.44 release.
[QuestHelper.git] / quest.lua
blob88b052657f33e6418f6bda40316c2bbd93d11203
1 local function QuestKnown(self)
2 if not self.target and not self.destination then
3 if self.o.finish then
4 self.target = self.qh:GetObjective("monster", self.o.finish)
5 elseif self.fb.finish then
6 self.target = self.qh:GetObjective("monster", self.fb.finish)
7 elseif self.o.pos then
8 self.destination = self.o.pos
9 elseif self.fb.pos then
10 self.destination = self.fb.pos
11 end
12 end
13 return (self.target or self.destination) and self:DefaultKnown() and (self.destination or self.target:Known())
14 end
16 local function QuestAppendPositions(self, objective, weight, why)
17 why2 = why and why.."\n" or ""
19 if self.target then
20 self.target:AppendPositions(self, 1, why2..QHFormat("OBJECTIVE_TALK", self.o.finish or self.fb.finish))
21 elseif self.destination then
22 for i, p in ipairs(self.destination) do
23 self:AddLoc(p[1], p[2], p[3], p[4], p[5])
24 end
25 end
26 end
28 function QuestHelper:GetQuest(name, level, hash)
29 if not level then
30 if QuestHelper_Quests[self.faction] then
31 for l, quest_list in ipairs(QuestHelper_Quests[self.faction]) do
32 if quest_list[name] then
33 level = l
34 break
35 end
36 end
37 end
39 if not level and
40 QuestHelper_StaticData[self.locale] and
41 QuestHelper_StaticData[self.locale].quest[self.faction] then
42 for l, quest_list in ipairs(QuestHelper_StaticData[self.locale].quest[self.faction]) do
43 if quest_list[name] then
44 level = l
45 break
46 end
47 end
48 end
50 if not level then
51 error("Don't know of a quest named '"..name.."' for your faction.")
52 end
54 self:TextOut("Determined the quest level of '"..name.."' to be "..level..".")
55 end
57 local bracket = self.quest_objects[level]
59 if not bracket then
60 bracket = {}
61 self.quest_objects[level] = bracket
62 end
64 local bracket2 = bracket[name]
66 if not bracket2 then
67 bracket2 = {}
68 bracket[name] = bracket2
69 end
71 local quest_object = bracket2[hash or -1]
73 if not quest_object then
74 quest_object = self:NewObjectiveObject()
75 quest_object.icon_id = 7
76 quest_object.icon_bg = 15
77 quest_object.AppendPositions = QuestAppendPositions
78 quest_object.Known = QuestKnown
79 quest_object.cat = "quest"
80 quest_object.obj = level.."/"..(hash or "").."/"..name
82 bracket2[hash or -1] = quest_object
84 local fbracket = QuestHelper_Quests[self.faction]
86 if not fbracket then
87 fbracket = {}
88 QuestHelper_Quests[self.faction] = fbracket
89 end
91 bracket = fbracket[level]
93 if not bracket then
94 bracket = {}
95 fbracket[level] = bracket
96 end
98 quest_object.o = bracket[name]
100 if not quest_object.o then
101 if hash then
102 quest_object.o = {hash=hash}
103 bracket[name] = quest_object.o
104 else -- Not going to actually save the quest without a hash.
105 quest_object.o = {}
109 local l = QuestHelper_StaticData[self.locale]
111 if l then
112 fbracket = l.quest[self.faction]
113 if fbracket then
114 bracket = fbracket[level]
115 if bracket then
116 quest_object.fb = bracket[name]
120 if not quest_object.fb then
121 quest_object.fb = {}
124 if quest_object.o.hash and quest_object.o.hash ~= hash then
125 if not quest_object.o.alt then quest_object.o.alt = {} end
126 local real_quest_data = quest_object.o.alt[hash]
127 if not real_quest_data then
128 real_quest_data = {hash=hash}
129 quest_object.o.alt[hash] = real_quest_data
131 quest_object.o = real_quest_data
132 elseif not quest_object.o.hash then
133 -- Not setting the hash now, as we might not actually have the correct quest loaded.
134 -- When we can verify our data is correct, we'll assign a value.
135 quest_object.need_hash = true
138 if hash and quest_object.fb.hash and quest_object.fb.hash ~= hash then
139 quest_object.fb = quest_object.fb.alt and quest_object.fb.alt[hash]
140 if not quest_object.fb then
141 quest_object.fb = {}
145 -- TODO: If we have some other source of information (like LightHeaded) add its data to quest_object.fb
149 return quest_object
152 function QuestHelper:PurgeItemFromQuest(quest, item_name, item_object)
153 if quest.alt then
154 for hash, alt_quest in pairs(quest.alt) do
155 self:PurgeItemFromQuest(alt_quest, item_name, item_object)
159 if quest.item then
160 local item_data = quest.item[item_name]
161 if item_data then
162 quest.item[item_name] = nil
163 if not next(quest.item, nil) then quest.item = nil end
165 if item_data.pos then
166 for i, pos in ipairs(item_data.pos) do
167 self:AppendObjectivePosition(item_object, unpack(pos))
169 else
170 if item_data.drop then
171 for monster, count in pairs(item_data.drop) do
172 self:AppendObjectiveDrop(item_object, monster, count)
176 if item_data.contained then
177 for item, count in pairs(item_data.contained) do
178 self:AppendItemObjectiveContained(item_object, item, count)
183 if item_object.bad_pos then
184 for i, pos in ipairs(item_object.bad_pos) do
185 self:AppendObjectivePosition(item_object, unpack(pos))
187 item_object.bad_pos = nil
188 elseif item_object.bad_drop then
189 for monster, count in pairs(item_object.bad_drop) do
190 self:AppendObjectiveDrop(item_object, monster, count)
192 item_object.bad_drop = nil
198 function QuestHelper:PurgeQuestItem(item_name, item_object)
199 for faction, level_list in pairs(QuestHelper_Quests) do
200 for level, quest_list in pairs(level_list) do
201 for quest_name, quest in pairs(quest_list) do
202 self:PurgeItemFromQuest(quest, item_name, item_object)
208 function QuestHelper:AppendQuestPosition(quest, item_name, i, x, y, w)
209 local item_list = quest.o.item
211 if not item_list then
212 item_list = {}
213 quest.o.item = item_list
216 local item = item_list[item_name]
218 if not item then
219 item = {}
220 item_list[item_name] = item
223 local pos = item.pos
224 if not pos then
225 if item.drop then
226 return -- If it's dropped by a monster, don't record the position we got the item at.
228 item.pos = self:AppendPosition({}, i, x, y, w)
229 else
230 self:AppendPosition(pos, i, x, y, w)
234 function QuestHelper:AppendQuestDrop(quest, item_name, monster_name, count)
235 local item_list = quest.o.item
237 if not item_list then
238 item_list = {}
239 quest.o.item = item_list
242 local item = item_list[item_name]
244 if not item then
245 item = {}
246 item_list[item_name] = item
249 local drop = item.drop
251 if drop then
252 drop[monster_name] = (drop[monster_name] or 0) + (count or 1)
253 else
254 item.drop = {[monster_name] = count or 1}
255 item.pos = nil -- If we know monsters drop the item, don't record the position we got the item at.