Arrgh
[QuestHelper.git] / quest.lua
bloba0f92f6a2de78492aabb367de31cf1f518980422
1 QuestHelper_File["quest.lua"] = "Development Version"
2 QuestHelper_Loadtime["quest.lua"] = GetTime()
4 local function QuestKnown(self)
5 if not self.target and not self.destination then
6 if self.o.finish then
7 self.target = self.qh:GetObjective("monster", self.o.finish)
8 elseif self.fb.finish then
9 self.target = self.qh:GetObjective("monster", self.fb.finish)
10 elseif self.o.pos then
11 self.destination = self.o.pos
12 elseif self.fb.pos then
13 self.destination = self.fb.pos
14 end
15 end
16 return (self.target or self.destination) and self:DefaultKnown() and (self.destination or self.target:Known())
17 end
19 local function QuestAppendPositions(self, objective, weight, why, restrict)
20 why2 = why and why.."\n" or ""
22 if self.target then
23 self.target:AppendPositions(self, 1, why2..QHFormat("OBJECTIVE_TALK", self.o.finish or self.fb.finish), restrict)
24 elseif self.destination then
25 for i, p in ipairs(self.destination) do
26 if not restrict or not self.qh:Disallowed(p[1]) then
27 self:AddLoc(p[1], p[2], p[3], p[4], p[5])
28 end
29 end
30 end
31 end
33 QuestHelper.default_objective_quest_param =
35 Known = QuestKnown,
36 AppendPositions = QuestAppendPositions
39 for key, value in pairs(QuestHelper.default_objective_param) do
40 if not QuestHelper.default_objective_quest_param[key] then
41 QuestHelper.default_objective_quest_param[key] = value
42 end
43 end
45 QuestHelper.default_objective_quest_meta = { __index = QuestHelper.default_objective_quest_param }
47 function QuestHelper:GetQuest(name, level, hash)
48 if not level then
49 if QuestHelper_Quests_Local[self.faction] then
50 for l, quest_list in ipairs(QuestHelper_Quests_Local[self.faction]) do
51 if quest_list[name] then
52 level = l
53 break
54 end
55 end
56 end
58 if not level and
59 QuestHelper_StaticData[self.locale] and
60 QuestHelper_StaticData[self.locale].quest[self.faction] then
61 for l, quest_list in ipairs(QuestHelper_StaticData[self.locale].quest[self.faction]) do
62 if quest_list[name] then
63 level = l
64 break
65 end
66 end
67 end
69 if not level then
70 error("Don't know of a quest named '"..name.."' for your faction.")
71 end
73 self:TextOut("Determined the quest level of '"..name.."' to be "..level..".")
74 end
76 local bracket = self.quest_objects[level]
78 if not bracket then
79 bracket = {}
80 self.quest_objects[level] = bracket
81 end
83 local bracket2 = bracket[name]
85 if not bracket2 then
86 bracket2 = {}
87 bracket[name] = bracket2
88 end
90 local quest_object = bracket2[hash or -1]
92 if not quest_object then
93 quest_object = self:NewObjectiveObject()
94 quest_object.icon_id = 7
95 quest_object.icon_bg = 15
96 setmetatable(quest_object, QuestHelper.default_objective_quest_meta)
98 quest_object.cat = "quest"
99 quest_object.obj = level.."/"..(hash or "").."/"..name
101 bracket2[hash or -1] = quest_object
103 local fbracket = QuestHelper_Quests_Local[self.faction]
105 if not fbracket then
106 fbracket = {}
107 QuestHelper_Quests_Local[self.faction] = fbracket
110 bracket = fbracket[level]
112 if not bracket then
113 bracket = {}
114 fbracket[level] = bracket
117 quest_object.o = bracket[name]
119 if not quest_object.o then
120 if hash then
121 quest_object.o = {hash=hash}
122 bracket[name] = quest_object.o
123 else -- Not going to actually save the quest without a hash.
124 quest_object.o = {}
128 local l = QuestHelper_StaticData[self.locale]
130 if l then
131 fbracket = l.quest[self.faction]
132 if fbracket then
133 bracket = fbracket[level]
134 if bracket then
135 quest_object.fb = bracket[name]
139 if not quest_object.fb then
140 quest_object.fb = {}
143 if quest_object.o.hash and quest_object.o.hash ~= hash then
144 if not quest_object.o.alt then quest_object.o.alt = {} end
145 local real_quest_data = quest_object.o.alt[hash]
146 if not real_quest_data then
147 real_quest_data = {hash=hash}
148 quest_object.o.alt[hash] = real_quest_data
150 quest_object.o = real_quest_data
151 elseif not quest_object.o.hash then
152 -- Not setting the hash now, as we might not actually have the correct quest loaded.
153 -- When we can verify our data is correct, we'll assign a value.
154 quest_object.need_hash = true
157 if hash and quest_object.fb.hash and quest_object.fb.hash ~= hash then
158 quest_object.fb = quest_object.fb.alt and quest_object.fb.alt[hash]
159 if not quest_object.fb then
160 quest_object.fb = {}
164 -- TODO: If we have some other source of information (like LightHeaded) add its data to quest_object.fb
168 return quest_object
171 function QuestHelper:PurgeItemFromQuest(quest, item_name, item_object)
172 if quest.alt then
173 for hash, alt_quest in pairs(quest.alt) do
174 self:PurgeItemFromQuest(alt_quest, item_name, item_object)
178 if quest.item then
179 local item_data = quest.item[item_name]
180 if item_data then
181 quest.item[item_name] = nil
182 if not next(quest.item, nil) then quest.item = nil end
184 if item_data.pos then
185 for i, pos in ipairs(item_data.pos) do
186 self:AppendObjectivePosition(item_object, unpack(pos))
188 else
189 if item_data.drop then
190 for monster, count in pairs(item_data.drop) do
191 self:AppendObjectiveDrop(item_object, monster, count)
195 if item_data.contained then
196 for item, count in pairs(item_data.contained) do
197 self:AppendItemObjectiveContained(item_object, item, count)
202 if item_object.bad_pos then
203 for i, pos in ipairs(item_object.bad_pos) do
204 self:AppendObjectivePosition(item_object, unpack(pos))
206 item_object.bad_pos = nil
207 elseif item_object.bad_drop then
208 for monster, count in pairs(item_object.bad_drop) do
209 self:AppendObjectiveDrop(item_object, monster, count)
211 item_object.bad_drop = nil
217 function QuestHelper:PurgeQuestItem(item_name, item_object)
218 for faction, level_list in pairs(QuestHelper_Quests_Local) do
219 for level, quest_list in pairs(level_list) do
220 for quest_name, quest in pairs(quest_list) do
221 self:PurgeItemFromQuest(quest, item_name, item_object)
227 function QuestHelper:AppendQuestPosition(quest, item_name, i, x, y, w)
228 local item_list = quest.o.item
230 if not item_list then
231 item_list = {}
232 quest.o.item = item_list
235 local item = item_list[item_name]
237 if not item then
238 item = {}
239 item_list[item_name] = item
242 local pos = item.pos
243 if not pos then
244 if item.drop then
245 return -- If it's dropped by a monster, don't record the position we got the item at.
247 item.pos = self:AppendPosition({}, i, x, y, w)
248 else
249 self:AppendPosition(pos, i, x, y, w)
253 function QuestHelper:AppendQuestDrop(quest, item_name, monster_name, count)
254 local item_list = quest.o.item
256 if not item_list then
257 item_list = {}
258 quest.o.item = item_list
261 local item = item_list[item_name]
263 if not item then
264 item = {}
265 item_list[item_name] = item
268 local drop = item.drop
270 if drop then
271 drop[monster_name] = (drop[monster_name] or 0) + (count or 1)
272 else
273 item.drop = {[monster_name] = count or 1}
274 item.pos = nil -- If we know monsters drop the item, don't record the position we got the item at.