update changes
[QuestHelper.git] / quest.lua
blob4b1e143aaf5fa367fc9f5f3506a96365e70dcbd4
1 QuestHelper_File["quest.lua"] = "Development Version"
3 local function QuestKnown(self)
4 if not self.target and not self.destination then
5 if self.o.finish then
6 self.target = self.qh:GetObjective("monster", self.o.finish)
7 elseif self.fb.finish then
8 self.target = self.qh:GetObjective("monster", self.fb.finish)
9 elseif self.o.pos then
10 self.destination = self.o.pos
11 elseif self.fb.pos then
12 self.destination = self.fb.pos
13 end
14 end
15 return (self.target or self.destination) and self:DefaultKnown() and (self.destination or self.target:Known())
16 end
18 local function QuestAppendPositions(self, objective, weight, why, restrict)
19 why2 = why and why.."\n" or ""
21 if self.target then
22 self.target:AppendPositions(self, 1, why2..QHFormat("OBJECTIVE_TALK", self.o.finish or self.fb.finish), restrict)
23 elseif self.destination then
24 for i, p in ipairs(self.destination) do
25 if not restrict or not self.qh:Disallowed(p[1]) then
26 self:AddLoc(p[1], p[2], p[3], p[4], p[5])
27 end
28 end
29 end
30 end
32 QuestHelper.default_objective_quest_param =
34 Known = QuestKnown,
35 AppendPositions = QuestAppendPositions
38 for key, value in pairs(QuestHelper.default_objective_param) do
39 if not QuestHelper.default_objective_quest_param[key] then
40 QuestHelper.default_objective_quest_param[key] = value
41 end
42 end
44 QuestHelper.default_objective_quest_meta = { __index = QuestHelper.default_objective_quest_param }
46 function QuestHelper:GetQuest(name, level, hash)
47 if not level then
48 if QuestHelper_Quests_Local[self.faction] then
49 for l, quest_list in ipairs(QuestHelper_Quests_Local[self.faction]) do
50 if quest_list[name] then
51 level = l
52 break
53 end
54 end
55 end
57 if not level and
58 QuestHelper_StaticData[self.locale] and
59 QuestHelper_StaticData[self.locale].quest[self.faction] then
60 for l, quest_list in ipairs(QuestHelper_StaticData[self.locale].quest[self.faction]) do
61 if quest_list[name] then
62 level = l
63 break
64 end
65 end
66 end
68 if not level then
69 error("Don't know of a quest named '"..name.."' for your faction.")
70 end
72 self:TextOut("Determined the quest level of '"..name.."' to be "..level..".")
73 end
75 local bracket = self.quest_objects[level]
77 if not bracket then
78 bracket = {}
79 self.quest_objects[level] = bracket
80 end
82 local bracket2 = bracket[name]
84 if not bracket2 then
85 bracket2 = {}
86 bracket[name] = bracket2
87 end
89 local quest_object = bracket2[hash or -1]
91 if not quest_object then
92 quest_object = self:NewObjectiveObject()
93 quest_object.icon_id = 7
94 quest_object.icon_bg = 15
95 setmetatable(quest_object, QuestHelper.default_objective_quest_meta)
97 quest_object.cat = "quest"
98 quest_object.obj = level.."/"..(hash or "").."/"..name
100 bracket2[hash or -1] = quest_object
102 local fbracket = QuestHelper_Quests_Local[self.faction]
104 if not fbracket then
105 fbracket = {}
106 QuestHelper_Quests_Local[self.faction] = fbracket
109 bracket = fbracket[level]
111 if not bracket then
112 bracket = {}
113 fbracket[level] = bracket
116 quest_object.o = bracket[name]
118 if not quest_object.o then
119 if hash then
120 quest_object.o = {hash=hash}
121 bracket[name] = quest_object.o
122 else -- Not going to actually save the quest without a hash.
123 quest_object.o = {}
127 local l = QuestHelper_StaticData[self.locale]
129 if l then
130 fbracket = l.quest[self.faction]
131 if fbracket then
132 bracket = fbracket[level]
133 if bracket then
134 quest_object.fb = bracket[name]
138 if not quest_object.fb then
139 quest_object.fb = {}
142 if quest_object.o.hash and quest_object.o.hash ~= hash then
143 if not quest_object.o.alt then quest_object.o.alt = {} end
144 local real_quest_data = quest_object.o.alt[hash]
145 if not real_quest_data then
146 real_quest_data = {hash=hash}
147 quest_object.o.alt[hash] = real_quest_data
149 quest_object.o = real_quest_data
150 elseif not quest_object.o.hash then
151 -- Not setting the hash now, as we might not actually have the correct quest loaded.
152 -- When we can verify our data is correct, we'll assign a value.
153 quest_object.need_hash = true
156 if hash and quest_object.fb.hash and quest_object.fb.hash ~= hash then
157 quest_object.fb = quest_object.fb.alt and quest_object.fb.alt[hash]
158 if not quest_object.fb then
159 quest_object.fb = {}
163 -- TODO: If we have some other source of information (like LightHeaded) add its data to quest_object.fb
167 return quest_object
170 function QuestHelper:PurgeItemFromQuest(quest, item_name, item_object)
171 if quest.alt then
172 for hash, alt_quest in pairs(quest.alt) do
173 self:PurgeItemFromQuest(alt_quest, item_name, item_object)
177 if quest.item then
178 local item_data = quest.item[item_name]
179 if item_data then
180 quest.item[item_name] = nil
181 if not next(quest.item, nil) then quest.item = nil end
183 if item_data.pos then
184 for i, pos in ipairs(item_data.pos) do
185 self:AppendObjectivePosition(item_object, unpack(pos))
187 else
188 if item_data.drop then
189 for monster, count in pairs(item_data.drop) do
190 self:AppendObjectiveDrop(item_object, monster, count)
194 if item_data.contained then
195 for item, count in pairs(item_data.contained) do
196 self:AppendItemObjectiveContained(item_object, item, count)
201 if item_object.bad_pos then
202 for i, pos in ipairs(item_object.bad_pos) do
203 self:AppendObjectivePosition(item_object, unpack(pos))
205 item_object.bad_pos = nil
206 elseif item_object.bad_drop then
207 for monster, count in pairs(item_object.bad_drop) do
208 self:AppendObjectiveDrop(item_object, monster, count)
210 item_object.bad_drop = nil
216 function QuestHelper:PurgeQuestItem(item_name, item_object)
217 for faction, level_list in pairs(QuestHelper_Quests_Local) do
218 for level, quest_list in pairs(level_list) do
219 for quest_name, quest in pairs(quest_list) do
220 self:PurgeItemFromQuest(quest, item_name, item_object)
226 function QuestHelper:AppendQuestPosition(quest, item_name, i, x, y, w)
227 local item_list = quest.o.item
229 if not item_list then
230 item_list = {}
231 quest.o.item = item_list
234 local item = item_list[item_name]
236 if not item then
237 item = {}
238 item_list[item_name] = item
241 local pos = item.pos
242 if not pos then
243 if item.drop then
244 return -- If it's dropped by a monster, don't record the position we got the item at.
246 item.pos = self:AppendPosition({}, i, x, y, w)
247 else
248 self:AppendPosition(pos, i, x, y, w)
252 function QuestHelper:AppendQuestDrop(quest, item_name, monster_name, count)
253 local item_list = quest.o.item
255 if not item_list then
256 item_list = {}
257 quest.o.item = item_list
260 local item = item_list[item_name]
262 if not item then
263 item = {}
264 item_list[item_name] = item
267 local drop = item.drop
269 if drop then
270 drop[monster_name] = (drop[monster_name] or 0) + (count or 1)
271 else
272 item.drop = {[monster_name] = count or 1}
273 item.pos = nil -- If we know monsters drop the item, don't record the position we got the item at.