Should stop updating the player's position when they're dead, under the assumption...
[QuestHelper.git] / quest.lua
blob32054fd727fc79a944e814f266825ee70d75b606
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)
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))
23 elseif self.destination then
24 for i, p in ipairs(self.destination) do
25 self:AddLoc(p[1], p[2], p[3], p[4], p[5])
26 end
27 end
28 end
30 QuestHelper.default_objective_quest_param =
32 Known = QuestKnown,
33 AppendPositions = QuestAppendPositions
36 for key, value in pairs(QuestHelper.default_objective_param) do
37 if not QuestHelper.default_objective_quest_param[key] then
38 QuestHelper.default_objective_quest_param[key] = value
39 end
40 end
42 QuestHelper.default_objective_quest_meta = { __index = QuestHelper.default_objective_quest_param }
44 function QuestHelper:GetQuest(name, level, hash)
45 if not level then
46 if QuestHelper_Quests[self.faction] then
47 for l, quest_list in ipairs(QuestHelper_Quests[self.faction]) do
48 if quest_list[name] then
49 level = l
50 break
51 end
52 end
53 end
55 if not level and
56 QuestHelper_StaticData[self.locale] and
57 QuestHelper_StaticData[self.locale].quest[self.faction] then
58 for l, quest_list in ipairs(QuestHelper_StaticData[self.locale].quest[self.faction]) do
59 if quest_list[name] then
60 level = l
61 break
62 end
63 end
64 end
66 if not level then
67 error("Don't know of a quest named '"..name.."' for your faction.")
68 end
70 self:TextOut("Determined the quest level of '"..name.."' to be "..level..".")
71 end
73 local bracket = self.quest_objects[level]
75 if not bracket then
76 bracket = {}
77 self.quest_objects[level] = bracket
78 end
80 local bracket2 = bracket[name]
82 if not bracket2 then
83 bracket2 = {}
84 bracket[name] = bracket2
85 end
87 local quest_object = bracket2[hash or -1]
89 if not quest_object then
90 quest_object = self:NewObjectiveObject()
91 quest_object.icon_id = 7
92 quest_object.icon_bg = 15
93 setmetatable(quest_object, QuestHelper.default_objective_quest_meta)
95 quest_object.cat = "quest"
96 quest_object.obj = level.."/"..(hash or "").."/"..name
98 bracket2[hash or -1] = quest_object
100 local fbracket = QuestHelper_Quests[self.faction]
102 if not fbracket then
103 fbracket = {}
104 QuestHelper_Quests[self.faction] = fbracket
107 bracket = fbracket[level]
109 if not bracket then
110 bracket = {}
111 fbracket[level] = bracket
114 quest_object.o = bracket[name]
116 if not quest_object.o then
117 if hash then
118 quest_object.o = {hash=hash}
119 bracket[name] = quest_object.o
120 else -- Not going to actually save the quest without a hash.
121 quest_object.o = {}
125 local l = QuestHelper_StaticData[self.locale]
127 if l then
128 fbracket = l.quest[self.faction]
129 if fbracket then
130 bracket = fbracket[level]
131 if bracket then
132 quest_object.fb = bracket[name]
136 if not quest_object.fb then
137 quest_object.fb = {}
140 if quest_object.o.hash and quest_object.o.hash ~= hash then
141 if not quest_object.o.alt then quest_object.o.alt = {} end
142 local real_quest_data = quest_object.o.alt[hash]
143 if not real_quest_data then
144 real_quest_data = {hash=hash}
145 quest_object.o.alt[hash] = real_quest_data
147 quest_object.o = real_quest_data
148 elseif not quest_object.o.hash then
149 -- Not setting the hash now, as we might not actually have the correct quest loaded.
150 -- When we can verify our data is correct, we'll assign a value.
151 quest_object.need_hash = true
154 if hash and quest_object.fb.hash and quest_object.fb.hash ~= hash then
155 quest_object.fb = quest_object.fb.alt and quest_object.fb.alt[hash]
156 if not quest_object.fb then
157 quest_object.fb = {}
161 -- TODO: If we have some other source of information (like LightHeaded) add its data to quest_object.fb
165 return quest_object
168 function QuestHelper:PurgeItemFromQuest(quest, item_name, item_object)
169 if quest.alt then
170 for hash, alt_quest in pairs(quest.alt) do
171 self:PurgeItemFromQuest(alt_quest, item_name, item_object)
175 if quest.item then
176 local item_data = quest.item[item_name]
177 if item_data then
178 quest.item[item_name] = nil
179 if not next(quest.item, nil) then quest.item = nil end
181 if item_data.pos then
182 for i, pos in ipairs(item_data.pos) do
183 self:AppendObjectivePosition(item_object, unpack(pos))
185 else
186 if item_data.drop then
187 for monster, count in pairs(item_data.drop) do
188 self:AppendObjectiveDrop(item_object, monster, count)
192 if item_data.contained then
193 for item, count in pairs(item_data.contained) do
194 self:AppendItemObjectiveContained(item_object, item, count)
199 if item_object.bad_pos then
200 for i, pos in ipairs(item_object.bad_pos) do
201 self:AppendObjectivePosition(item_object, unpack(pos))
203 item_object.bad_pos = nil
204 elseif item_object.bad_drop then
205 for monster, count in pairs(item_object.bad_drop) do
206 self:AppendObjectiveDrop(item_object, monster, count)
208 item_object.bad_drop = nil
214 function QuestHelper:PurgeQuestItem(item_name, item_object)
215 for faction, level_list in pairs(QuestHelper_Quests) do
216 for level, quest_list in pairs(level_list) do
217 for quest_name, quest in pairs(quest_list) do
218 self:PurgeItemFromQuest(quest, item_name, item_object)
224 function QuestHelper:AppendQuestPosition(quest, item_name, i, x, y, w)
225 local item_list = quest.o.item
227 if not item_list then
228 item_list = {}
229 quest.o.item = item_list
232 local item = item_list[item_name]
234 if not item then
235 item = {}
236 item_list[item_name] = item
239 local pos = item.pos
240 if not pos then
241 if item.drop then
242 return -- If it's dropped by a monster, don't record the position we got the item at.
244 item.pos = self:AppendPosition({}, i, x, y, w)
245 else
246 self:AppendPosition(pos, i, x, y, w)
250 function QuestHelper:AppendQuestDrop(quest, item_name, monster_name, count)
251 local item_list = quest.o.item
253 if not item_list then
254 item_list = {}
255 quest.o.item = item_list
258 local item = item_list[item_name]
260 if not item then
261 item = {}
262 item_list[item_name] = item
265 local drop = item.drop
267 if drop then
268 drop[monster_name] = (drop[monster_name] or 0) + (count or 1)
269 else
270 item.drop = {[monster_name] = count or 1}
271 item.pos = nil -- If we know monsters drop the item, don't record the position we got the item at.