aboutsummaryrefslogtreecommitdiff
path: root/gamemode/itemsystem/quest.lua
blob: bf0ef2bcb0db66d37c9237b0a47018f46574a304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
--[[
	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)