--[[ Generic item for creating quests ]] local a = nrequire("core/quests/arcs.lua") local log = nrequire("log.lua") local item = {} local strikethoughmap = { a = "a̶", b = "b̶", c = "c̶", d = "d̶", e = "e̶", f = "f̶", g = "g̶", h = "h̶", i = "i̶", j = "j̶", k = "k̶", l = "l̶", m = "m̶", n = "n̶", o = "o̶", p = "p̶", q = "q̶", r = "r̶", s = "s̶", t = "t̶", u = "u̶", v = "v̶", w = "w̶", x = "x̶", y = "y̶", z = "z̶" } item.Name = "Quest" item.Tooltip = "The quest item" item.Arcs = {} item.ArcsCompleted = 0 function item:GetText() log.debug("Calling quest item's GetText()") local text = {} for i=1,self.ArcsCompleted do local thisarctxt = self.Arcs[i]:GetText() local tas = {} for j = 0,#thisarctxt+1 do tas[#tas + 1] = strikethoughmap[thisarctxt[j]] end text[#text + 1] = table.concat(tas) end if self.Arcs[self.ArcsCompleted + 1] then text[#text + 1] = self.Arcs[self.ArcsCompleted + 1]:GetText() end return table.concat(text,"\n") end function item:Serialize() local s = { completed = self.ArcsCompleted, questname = self.QuestName, rewards = self.Rewards, } assert(type(self.Arcs) == "table", "self.Arcs was not a table, it was a" .. type(self.Arcs)) for k,v in pairs(self.Arcs) do s[#s + 1] = {v.Name, v:Serialize()} end return util.TableToJSON(s) end function item:UpdateCompleted() --Remove ourselves local qstloc = self.Owner:HasItem(function(tbl) return tbl == self end) self.Owner:RemoveItem(qstloc) log.debug("Quest item's UpdateCompleted() called") log.debug(string.format("Looking at %s arc:", self.Arcs[self.ArcsCompleted + 1].Name)) if self.Arcs[self.ArcsCompleted + 1]:Complete() then log.debug("Completed!") self.ArcsCompleted = self.ArcsCompleted + 1 end if #self.Arcs == self.ArcsCompleted then log.debug("Completed Quest! : " .. self.QuestName) end --Re-add ourselves self.Owner:GiveItem(self) end function item:Complete() if #self.Arcs == self.ArcsCompleted then return true elseif #self.Arcs < self.ArcsCompleted then log.error(string.format("Finished %d arcs of %d arcs in quest %s",self.ArcsCompleted, #self.Arcs, self.QuestName)) else return false end end function item:DeSerialize(data) local t = util.JSONToTable(data) self.ArcsCompleted = t.completed self.QuestName = t.questname self.Rewards = t.rewards self.Arcs = {} for k,v in ipairs(t) do local arcname,arcdata = unpack(v) local arc = a.MakeArcWithData(arcname,arcdata) arc.Quest = self self.Arcs[k] = arc end end local itm = nrequire("item.lua") itm.RegisterItem(item)