-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathquest.lua
More file actions
347 lines (343 loc) · 7.72 KB
/
quest.lua
File metadata and controls
347 lines (343 loc) · 7.72 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
Quest =
{
name = "";
giver = nil;
require = 1;
hint = "None";
target = nil;
status = 0;
repeatable = false;
}
------------------
Quest_mt = {
__index = Quest,
__gc =
function(self)
print("Quest"..self:get_id().." deleted")
end
}
------------------
function Quest:new(name, giver, require, hint, target, number)
local quest
if dokun then
quest = Sprite:new()
else
quest = {}
end
if self == Quest then -- Quest:new
quest.name = name
quest.giver = giver
quest.require = require
quest.hint = hint
else
quest.name = self -- Quest.new
quest.giver = name
quest.require = giver
quest.hint = require
quest.target = hint
end
if Item then
if is_item(target) then
quest.target = target
quest.target.received = 0
quest.target.obtain_limit = num
end
end
if Monster then
if is_monster(target) then
quest.target = target
quest.target.slain = 0
quest.target.kill_limit = num
end
end
if NPC then
if is_npc(target) then
quest.target = target
quest.target.interacted = false
end
end
if not Quest.id then Quest.id = 0 end
Quest.id = Quest.id + 1
quest.id = Quest.id
setmetatable(quest, Quest_mt)
return quest
end
------------------
function Quest:find(seeker) -- updates and finds available global quests
local g = _G
for _, quest in pairs(g) do
-- a valid quest
if is_quest(quest) then
-- player does not have quest yet
if not quest:in_progress() then
-- quest is not completed yet
if not quest:is_complete() or
-- or quest can be repeated
quest:is_repeatable() then
-- quest matches player's level
if seeker:get_level() >= quest:get_require() then
-- set status : Available
quest:set_status(1)
-- return quest
return quest
end
end
end
end
end
return nil
end
------------------
function Quest:obtain(player)
if self:in_log(player) then
if self:in_progress() then
print("Quest in progress")
return false
end
if self:is_complete() and
not self:is_repeatable() then
print("Quest is complete")
return false
end
end
if player:get_level() < self:get_require() then
print("Your level is too low")
return false
end
self:set_status(2)
player:set_quest(self)
print("Obtained Quest '"..self:get_name().."'")
return true
end
------------------
function Quest:delete(player)
for index, quest in pairs(player.quest) do
if self == player:get_quest(index) then
--if self:in_progress() then
-- change status
self:set_status(1)
--end
-- remove from log
player.quest[index] = nil
print("Quest \""..quest:get_name().."\" deleted")
end
end
end
------------------
-- resets quest target data
-- on completion
function Quest:reset()
end
------------------
function Quest:update()
end
------------------
function Quest.show()
for _, quest in pairs(_G) do
if is_quest(quest) then
print(quest:get_name(), quest:get_status())
end
end
end
------------------
-- SETTERS
------------------
function Quest:set_name(name)
self.name = name
end
------------------
function Quest:set_giver(giver)
self.giver = giver
end
------------------
function Quest:set_require(require)
self.require = require
end
------------------
function Quest:set_hint(hint)
self.hint = hint
end
------------------
Quest.set_objective = Quest.set_hint
------------------
function Quest:set_target(target, count) -- num is either number of items to receive or number of monsters to slay
if type(count) ~= "number" then
count = 1
end
if not target then
return
end
-- Item (to be obtained)
if is_item(target) then
self.target = target
self.target.received = 0
self.target.obtain_limit = count
end
-- Monster (to be slain)
if is_monster(target) then
self.target = target
self.target.slain = 0
self.target.kill_limit = count
end
-- NPC (to notify)
if is_npc(target) then
self.target = target
self.target.interacted = false
end
end
------------------
function Quest:set_status(status) -- 0 = not avail, 1 = avail, 2 = in progress, 3 = completed
self.status = status
end
------------------
function Quest:set_repeatable(repeatable)
self.repeatable = repeatable
end
------------------
function Quest:set_reward(reward, amount, type_)
if not amount then amount = 1 end
if not self.reward then self.reward = {} end --quest.reward
if is_item(reward) then
self.reward[#self.reward + 1] = reward:new()
self.reward[#self.reward].amount = amount
end
if type(reward) == "number" then
if string.find(type_, nocase("exp")) then
self.exp_reward = reward
end
if string.find(type_, nocase("gold")) then
self.gold_reward = reward
end
end
end
------------------
-- getters
------------------
function Quest:get_id()
return self.id
end
------------------
function Quest:get_name()
return self.name
end
------------------
function Quest:get_giver()
return self.giver
end
------------------
function Quest:get_hint()
return self.hint
end
------------------
Quest.get_objective = Quest.get_hint
------------------
function Quest:get_require()
return self.require
end
------------------
Quest.get_requirement = Quest.get_require
------------------
function Quest:get_target()
return self.target
end
------------------
function Quest:get_status()
local status_name
if self.status == 0 then
status_name = "Not Available"
end
if self.status == 1 then
status_name = "Available"
end
if self.status == 2 then
status_name = "In Progress"
end
if self.status == 3 then
status_name = "Completed"
end
return self.status, status_name
end
------------------
function Quest:get_reward(index)
return self.reward[index]
end
------------------
function Quest:get_exp_reward()
return self.exp_reward
end
------------------
function Quest:get_gold_reward()
return self.gold_reward
end
------------------
function get_quest_by_name(name)
local g = _G
for _, quest in pairs(g) do
if is_quest(quest) then
if quest:get_name() == name then
return quest
end
end
end
end
------------------
function get_quest_by_id(id)
local g = _G
for _, quest in pairs(g) do
if is_quest(quest) then
if quest:get_id() == id then
return quest
end
end
end
end
------------------
-- BOOLEAN
------------------
function Quest:is_quest()
if getmetatable(self) == Quest_mt then
return true
end
return false
end
------------------
is_quest = Quest.is_quest
------------------
function Quest:is_repeatable()
return self.repeatable
end
------------------
function Quest:is_available()
if self:get_status() == 0 then
return false
end
if self:get_status() == 1 then
return true
end
return false
end
------------------
function Quest:in_progress()
if self:get_status() == 2 then
return true
end
return false
end
------------------
function Quest:is_complete()
if self:get_status() == 3 then
return true
end
return false
end
------------------
function Quest:in_log(player)
if not player.quest then player.quest = {} end
for i = 0, #player.quest do
if player:get_quest(i) == self then
return true
end
end
return false
end
------------------