update changes
[QuestHelper.git] / quest.lua
blob37efb8faddbce0f39d0dabc68e02564b27ed7474
1 QuestHelper_File["quest.lua"] = "Development Version"
2 QuestHelper_Loadtime["quest.lua"] = GetTime()
4 do return end
6 local function QuestKnown(self)
7 if not self.target and not self.destination then
8 if self.o.finish then
9 self.target = self.qh:GetObjective("monster", self.o.finish)
10 elseif self.fb.finish then
11 self.target = self.qh:GetObjective("monster", self.fb.finish)
12 elseif self.o.pos then
13 self.destination = self.o.pos
14 elseif self.fb.pos then
15 self.destination = self.fb.pos
16 end
17 end
18 return (self.target or self.destination) and self:DefaultKnown() and (self.destination or self.target:Known())
19 end
21 local function QuestAppendPositions(self, objective, weight, why, restrict)
22 why2 = why and why.."\n" or ""
24 if self.target then
25 self.target:AppendPositions(self, 1, why2..QHFormat("OBJECTIVE_TALK", self.o.finish or self.fb.finish), restrict)
26 elseif self.destination then
27 for i, p in ipairs(self.destination) do
28 if not restrict or not self.qh:Disallowed(p[1]) then
29 self:AddLoc(p[1], p[2], p[3], p[4], p[5])
30 end
31 end
32 end
33 end
35 QuestHelper.default_objective_quest_param =
37 Known = QuestKnown,
38 AppendPositions = QuestAppendPositions
41 for key, value in pairs(QuestHelper.default_objective_param) do
42 if not QuestHelper.default_objective_quest_param[key] then
43 QuestHelper.default_objective_quest_param[key] = value
44 end
45 end
47 QuestHelper.default_objective_quest_meta = { __index = QuestHelper.default_objective_quest_param }
49 function QuestHelper:GetQuest(name, level, hash)
50 if not level then
51 if QuestHelper_Quests_Local[self.faction] then
52 for l, quest_list in ipairs(QuestHelper_Quests_Local[self.faction]) do
53 if quest_list[name] then
54 level = l
55 break
56 end
57 end
58 end
60 if not level and
61 QuestHelper_StaticData[self.locale] and
62 QuestHelper_StaticData[self.locale].quest[self.faction] then
63 for l, quest_list in ipairs(QuestHelper_StaticData[self.locale].quest[self.faction]) do
64 if quest_list[name] then
65 level = l
66 break
67 end
68 end
69 end
71 if not level then
72 error("Don't know of a quest named '"..name.."' for your faction.")
73 end
75 self:TextOut("Determined the quest level of '"..name.."' to be "..level..".")
76 end
78 local bracket = self.quest_objects[level]
80 if not bracket then
81 bracket = {}
82 self.quest_objects[level] = bracket
83 end
85 local bracket2 = bracket[name]
87 if not bracket2 then
88 bracket2 = {}
89 bracket[name] = bracket2
90 end
92 local quest_object = bracket2[hash or -1]
94 if not quest_object then
95 quest_object = self:NewObjectiveObject()
96 quest_object.icon_id = 7
97 quest_object.icon_bg = 15
98 setmetatable(quest_object, QuestHelper.default_objective_quest_meta)
100 quest_object.cat = "quest"
101 quest_object.obj = level.."/"..(hash or "").."/"..name
103 bracket2[hash or -1] = quest_object
105 local fbracket = QuestHelper_Quests_Local[self.faction]
107 if not fbracket then
108 fbracket = {}
109 QuestHelper_Quests_Local[self.faction] = fbracket
112 bracket = fbracket[level]
114 if not bracket then
115 bracket = {}
116 fbracket[level] = bracket
119 quest_object.o = bracket[name]
121 if not quest_object.o then
122 if hash then
123 quest_object.o = {hash=hash}
124 bracket[name] = quest_object.o
125 else -- Not going to actually save the quest without a hash.
126 quest_object.o = {}
130 local l = QuestHelper_StaticData[self.locale]
132 if l then
133 fbracket = l.quest[self.faction]
134 if fbracket then
135 bracket = fbracket[level]
136 if bracket then
137 quest_object.fb = bracket[name]
141 if not quest_object.fb then
142 quest_object.fb = {}
145 if quest_object.o.hash and quest_object.o.hash ~= hash then
146 if not quest_object.o.alt then quest_object.o.alt = {} end
147 local real_quest_data = quest_object.o.alt[hash]
148 if not real_quest_data then
149 real_quest_data = {hash=hash}
150 quest_object.o.alt[hash] = real_quest_data
152 quest_object.o = real_quest_data
153 elseif not quest_object.o.hash then
154 -- Not setting the hash now, as we might not actually have the correct quest loaded.
155 -- When we can verify our data is correct, we'll assign a value.
156 quest_object.need_hash = true
159 if hash and quest_object.fb.hash and quest_object.fb.hash ~= hash then
160 quest_object.fb = quest_object.fb.alt and quest_object.fb.alt[hash]
161 if not quest_object.fb then
162 quest_object.fb = {}
166 -- TODO: If we have some other source of information (like LightHeaded) add its data to quest_object.fb
170 return quest_object
173 function QuestHelper:PurgeItemFromQuest(quest, item_name, item_object)
174 if quest.alt then
175 for hash, alt_quest in pairs(quest.alt) do
176 self:PurgeItemFromQuest(alt_quest, item_name, item_object)
180 if quest.item then
181 local item_data = quest.item[item_name]
182 if item_data then
183 quest.item[item_name] = nil
184 if not next(quest.item, nil) then quest.item = nil end
186 if item_data.pos then
187 for i, pos in ipairs(item_data.pos) do
188 self:AppendObjectivePosition(item_object, unpack(pos))
190 else
191 if item_data.drop then
192 for monster, count in pairs(item_data.drop) do
193 self:AppendObjectiveDrop(item_object, monster, count)
197 if item_data.contained then
198 for item, count in pairs(item_data.contained) do
199 self:AppendItemObjectiveContained(item_object, item, count)
204 if item_object.bad_pos then
205 for i, pos in ipairs(item_object.bad_pos) do
206 self:AppendObjectivePosition(item_object, unpack(pos))
208 item_object.bad_pos = nil
209 elseif item_object.bad_drop then
210 for monster, count in pairs(item_object.bad_drop) do
211 self:AppendObjectiveDrop(item_object, monster, count)
213 item_object.bad_drop = nil
219 function QuestHelper:PurgeQuestItem(item_name, item_object)
220 for faction, level_list in pairs(QuestHelper_Quests_Local) do
221 for level, quest_list in pairs(level_list) do
222 for quest_name, quest in pairs(quest_list) do
223 self:PurgeItemFromQuest(quest, item_name, item_object)
229 function QuestHelper:AppendQuestPosition(quest, item_name, i, x, y, w)
230 local item_list = quest.o.item
232 if not item_list then
233 item_list = {}
234 quest.o.item = item_list
237 local item = item_list[item_name]
239 if not item then
240 item = {}
241 item_list[item_name] = item
244 local pos = item.pos
245 if not pos then
246 if item.drop then
247 return -- If it's dropped by a monster, don't record the position we got the item at.
249 item.pos = self:AppendPosition({}, i, x, y, w)
250 else
251 self:AppendPosition(pos, i, x, y, w)
255 function QuestHelper:AppendQuestDrop(quest, item_name, monster_name, count)
256 local item_list = quest.o.item
258 if not item_list then
259 item_list = {}
260 quest.o.item = item_list
263 local item = item_list[item_name]
265 if not item then
266 item = {}
267 item_list[item_name] = item
270 local drop = item.drop
272 if drop then
273 drop[monster_name] = (drop[monster_name] or 0) + (count or 1)
274 else
275 item.drop = {[monster_name] = count or 1}
276 item.pos = nil -- If we know monsters drop the item, don't record the position we got the item at.