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
|
if SERVER then
inv = nrequire("sv_invtracker.lua")
end
local log = nrequire("log.lua")
local quests = {}
local comp = {}
comp.Name = "Quest Component Gather"
--[[
Update this quest, this is called every time a player
picks up an item. This method removes the items from the player, then
adds them back in (so dropping an item and picking it up x number of times
will not work.
]]
function comp:Update()
if SERVER then
local items = {}
self.items = 0
log.debug("in comp:Update, self.ItemNumber is" .. tostring(self.ItemNumber))
for i = 1, self.ItemNumber do
local loc = self.Owner:HasItem(self.ItemName)
if loc then
items[loc] = self.Owner:RemoveItem(loc)
self.items = i
else
break
end
end
log.debug("Update called, self.items is now" .. tostring(self.items))
for k,v in pairs(items) do
self.Owner:PutItem(k,v)
end
end
self.Quest:UpdateCompleted()
end
function comp:Complete()
if self.items >= self.ItemNumber then
log.debug("Completed a gather arc of a quest!")
end
return self.items >= self.ItemNumber
end
function comp:Init(ply,itemname,itemnumber)
if not ply or not itemname or not itemnumber then return end
self.ItemName = itemname
self.ItemNumber = itemnumber
self.Items = 0
if CLIENT then return end
self.Owner = ply
-- self:Update()
quests[#quests + 1] = self
log.debug("After initalizing quest, found " .. tostring(self.items) .. " items")
end
--[[
Detour the player's GiveItem(), to check if the quest is
complete after giving the player the item
]]
if SERVER then
local plymeta = FindMetaTable("Player")
local det = plymeta.GiveItem
function plymeta:GiveItem(tbl)
det(self,tbl)
log.debug("Calling component_gather's GiveItem()")
for k,v in pairs(quests) do
if v.Name == comp.Name and v.ItemName == tbl.Name then
v:Update()
end
end
end
end
function comp:GetText()
return string.format("Gather %s %s",self.ItemNumber,self.ItemName)
end
function comp:Serialize()
return util.TableToJSON({self.ItemName,self.ItemNumber})
end
function comp:DeSerialize(data)
local tbl = util.JSONToTable(data)
self.ItemName = tbl[1]
self.ItemNumber = tbl[2]
end
nrequire("core/quests/arcs.lua").RegisterArc(comp)
|