This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathnpc_util.lua
585 lines (508 loc) · 20.3 KB
/
npc_util.lua
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
--[[
Helper functions for common NPC tasks.
npcUtil.popFromQM(player, qm, mobId, params)
npcUtil.pickNewPosition(npc, positionTable, allowCurrentPosition)
npcUtil.giveItem(player, items)
npcUtil.giveKeyItem(player, keyitems)
npcUtil.completeQuest(player, area, quest, params)
npcUtil.tradeHas(trade, items)
npcUtil.queueMove(npc, point, delay)
npcUtil.UpdateNPCSpawnPoint(id, minTime, maxTime, posTable, serverVar)
npcUtil.fishingAnimation(npc, phaseDuration, func)
--]]
require("scripts/globals/settings")
require("scripts/globals/status")
npcUtil = {}
--[[ *******************************************************************************
Pop mob(s) from question mark NPC.
If any mob is already spawned, return false.
Params (table) can contain the following parameters:
radius (number)
if set, spawn mobs randomly within radius of NPC
claim (boolean, default true)
do spawned mobs automatically aggro the player
hide (number, default FORCE_SPAWN_QM_RESET_TIME)
how long to hide the QM for after mobs die
******************************************************************************* --]]
function npcUtil.popFromQM(player, qm, mobId, params)
local qmId = qm:getID()
-- default params
if not params then
params = {}
end
if params.claim == nil or type(params.claim) ~= "boolean" then
params.claim = true
end
if params.hide == nil or type(params.hide) ~= "number" then
params.hide = FORCE_SPAWN_QM_RESET_TIME
end
-- get list of mobs to pop
local mobs = {}
if type(mobId) == "number" then
table.insert(mobs, mobId)
elseif type(mobId) == "table" then
for _, v in pairs(mobId) do
if type(v) == "number" then
table.insert(mobs, v)
end
end
end
-- make sure none are spawned
for k, v in pairs(mobs) do
local mob = GetMobByID(v)
if mob == nil or mob:isSpawned() then
return false
else
mobs[k] = mob
end
end
-- hide qm
if params.hide > 0 then
qm:setStatus(tpz.status.DISAPPEAR)
end
-- spawn mobs and give each a listener that will show QM after they are all dead
for _, mob in pairs(mobs) do
-- choose random position uniformly from within radius
if params.radius and type(params.radius) == "number" then
local r = params.radius * math.sqrt(math.random())
local theta = math.random() * 2 * math.pi
local x = r * math.cos(theta)
local z = r * math.sin(theta)
mob:setSpawn(qm:getXPos() + x, qm:getYPos(), qm:getZPos() + z)
end
-- spawn
mob:spawn()
-- claim
if params.claim then
mob:updateClaim(player)
end
-- look
if params.look then
mob:lookAt(player:getPos())
end
-- reappear the QM when all spawned mobs are dead, plus params.hide seconds
if params.hide > 0 then
local myId = mob:getID()
mob:setLocalVar("qm", qmId)
mob:addListener("DESPAWN", "QM_"..myId, function(m)
m:removeListener("QM_"..myId)
for _, v in pairs(mobs) do
if v:isAlive() then
return false
end
end
GetNPCByID(m:getLocalVar("qm")):updateNPCHideTime(params.hide)
end)
end
end
return true
end
--[[ *******************************************************************************
Queue a position change for an NPC. We do this because if you setPos() an NPC
immediately after you setStatus(tpz.status.DISAPPEAR) it, the QM does not hide
on the players' screens.
point may be any of the following formats:
{x, y, z}
{x, y, z, rot}
{x = x, y = y, z = z}
{x = x, y = y, z = z, rot = r}
******************************************************************************* --]]
local function doMove(x, y, z, r)
if not r then
r = 0
end
return function(entity)
entity:setPos(x, y, z, r)
end
end
function npcUtil.queueMove(npc, point, delay)
if not delay then
delay = 3000
end
if point.rot then
point = {point.x, point.y, point.z, point.rot}
elseif point.x then
point = {point.x, point.y, point.z}
end
npc:queue(delay, doMove(unpack(point)))
end
-- Picks a new position for an NPC and excluding the current position.
-- INPUT: npc = npcID, position = 2D table with coords: index, {x, y, z}
-- RETURN: table index
function npcUtil.pickNewPosition(npc, positionTable, allowCurrentPosition)
local npc = GetNPCByID(npc)
local positionIndex = 1 -- Default to position one in the table if it can't be found.
local tableSize = 0
local newPosition = 0
allowCurrentPosition = allowCurrentPosition or false
for i, v in ipairs(positionTable) do -- Looking for the current position
if not allowCurrentPosition then
-- Finding by comparing the NPC's coords
if math.floor(v[1]) == math.floor(npc:getXPos()) and math.floor(v[2]) == math.floor(npc:getYPos()) and math.floor(v[3]) == math.floor(npc:getZPos()) then
positionIndex = i -- Found where the NPC is!
end
end
tableSize = tableSize + 1 -- Counting the array size
end
if not allowCurrentPosition then
-- Pick a new pos that isn't the current
repeat
newPosition = math.random(1, tableSize)
until (newPosition ~= positionIndex)
else
newPosition = math.random(1, tableSize)
end
return {["x"] = positionTable[newPosition][1], ["y"] = positionTable[newPosition][2], ["z"] = positionTable[newPosition][3]}
end
--[[ *******************************************************************************
Give item(s) to player.
If player has inventory space, give items, display message, and return true.
If not, do not give items, display a message to indicate this, and return false.
Examples of valid items parameter:
640 -- copper ore x1
{ 640, 641 } -- copper ore x1, tin ore x1
{ {640,2} } -- copper ore x2
{ {640,2}, 641 } -- copper ore x2, tin ore x1
******************************************************************************* --]]
function npcUtil.giveItem(player, items)
local ID = zones[player:getZoneID()]
-- create table of items, with key/val of itemId/itemQty
local givenItems = {}
local itemId
local itemQty
if type(items) == "number" then
table.insert(givenItems, {items,1})
elseif type(items) == "table" then
for _, v in pairs(items) do
if type(v) == "number" then
table.insert(givenItems, {v,1})
elseif type(v) == "table" and #v == 2 and type(v[1]) == "number" and type(v[2]) == "number" then
table.insert(givenItems, {v[1],v[2]})
else
print(string.format("ERROR: invalid items parameter given to npcUtil.giveItem in zone %s.", player:getZoneName()))
return false
end
end
end
-- does player have enough inventory space?
if player:getFreeSlotsCount() < #givenItems then
player:messageSpecial(ID.text.ITEM_CANNOT_BE_OBTAINED, givenItems[1][1])
return false
end
-- give items to player
for _, v in pairs(givenItems) do
if player:addItem(v[1], v[2], true) then
player:messageSpecial(ID.text.ITEM_OBTAINED, v[1])
elseif #givenItems == 1 then
player:messageSpecial(ID.text.ITEM_CANNOT_BE_OBTAINED, givenItems[1][1])
return false
end
end
return true
end
--[[ *******************************************************************************
Give currency to a player.
Message is displayed showing currency obtained.
Examples of valid parameters:
gil, 500
bayld, 1000
******************************************************************************* --]]
function npcUtil.giveCurrency(player, currency, amount)
local ID = zones[player:getZoneID()]
if (not type(currency) == "string") or (not type(amount) == "number") then
print(string.format("ERROR: invalid parameter given to npcUtil.giveCurrency in zone %s.", player:getZoneName()))
return false
end
currency = string.lower(currency)
local currency_types =
{
["gil"] = {"GIL_OBTAINED", GIL_RATE},
["bayld"] = {"BAYLD_OBTAINED", BAYLD_RATE}
}
local currency_type = currency_types[currency]
if not currency_type then
print(string.format("ERROR: invalid currency '%s' given to npcUtil.giveCurrency in zone %s.", currency, player:getZoneName()))
return false
end
local message_id = ID.text[currency_type[1]]
if not message_id then
print(string.format("ERROR: no message ID defined for currency '%s' given to npcUtil.giveCurrency in zone %s.", currency, player:getZoneName()))
return false
end
amount = amount * currency_type[2]
if currency == "gil" then
player:addGil(amount)
else
player:addCurrency(currency, amount)
end
player:messageSpecial(message_id, amount)
return true
end
--[[ *******************************************************************************
Give key item(s) to player.
Message is displayed showing key items obtained.
Examples of valid keyitems parameter:
tpz.ki.ZERUHN_REPORT
{tpz.ki.PALBOROUGH_MINES_LOGS}
{tpz.ki.BLUE_ACIDITY_TESTER, tpz.ki.RED_ACIDITY_TESTER}
******************************************************************************* --]]
function npcUtil.giveKeyItem(player, keyitems)
local ID = zones[player:getZoneID()]
-- create table of keyitems
local givenKeyItems = {}
if type(keyitems) == "number" then
givenKeyItems = {keyitems}
elseif type(keyitems) == "table" then
givenKeyItems = keyitems
else
print(string.format("ERROR: invalid keyitems parameter given to npcUtil.giveKeyItem in zone %s.", player:getZoneName()))
return false
end
-- give key items to player, with message
for _, v in pairs(givenKeyItems) do
if not player:hasKeyItem(v) then
player:addKeyItem(v)
player:messageSpecial(ID.text.KEYITEM_OBTAINED,v)
end
end
return true
end
--[[ *******************************************************************************
Complete a quest.
If quest rewards items, and the player cannot carry them, return false.
Otherwise, return true.
Example of usage with params (all params are optional):
npcUtil.completeQuest(player, SANDORIA, ROSEL_THE_ARMORER, {
item = { {640,2}, 641 }, -- see npcUtil.giveItem for formats
keyItem = tpz.ki.ZERUHN_REPORT, -- see npcUtil.giveKeyItem for formats
fame = 120, -- fame defaults to 30 if not set
bayld = 500,
gil = 200,
xp = 1000,
title = tpz.title.ENTRANCE_DENIED,
var = {"foo1", "foo2"} -- variable(s) to set to 0. string or table
})
******************************************************************************* --]]
function npcUtil.completeQuest(player, area, quest, params)
params = params or {}
-- load text ids
local ID = zones[player:getZoneID()]
-- item(s) plus message. return false if player lacks inventory space.
if params["item"] ~= nil then
if not npcUtil.giveItem(player, params["item"]) then
return false
end
end
-- key item(s), fame, gil, bayld, xp, and title
if params["keyItem"] ~= nil then
npcUtil.giveKeyItem(player, params["keyItem"])
end
if params["fame"] == nil then
params["fame"] = 30
end
if area["fame_area"] ~= nil and type(params["fame"]) == "number" then
player:addFame(area, params["fame"])
elseif params["fameArea"] ~= nil and params["fameArea"]["fame_area"] ~= nil and type(params["fame"]) == "number" then
player:addFame(params["fameArea"], params["fame"])
end
if params["gil"] ~= nil and type(params["gil"]) == "number" then
player:addGil(params["gil"] * GIL_RATE)
player:messageSpecial(ID.text.GIL_OBTAINED, params["gil"] * GIL_RATE)
end
if params["bayld"] ~= nil and type(params["bayld"]) == "number" then
player:addCurrency('bayld', params["bayld"] * BAYLD_RATE)
player:messageSpecial(ID.text.BAYLD_OBTAINED, params["bayld"] * BAYLD_RATE)
end
if params["xp"] ~= nil and type(params["xp"]) == "number" then
player:addExp(params["xp"] * EXP_RATE)
end
if params["title"] ~= nil then
player:addTitle(params["title"])
end
if params["var"] ~= nil then
local playerVarsToZero = {}
if type(params["var"]) == "table" then
playerVarsToZero = params["var"]
elseif type(params["var"]) == "string" then
table.insert(playerVarsToZero, params["var"])
end
for _, v in pairs(playerVarsToZero) do
player:setCharVar(v, 0)
end
end
-- successfully complete the quest
player:completeQuest(area,quest)
return true
end
--[[ *******************************************************************************
check whether trade has all required items
if yes, confirm all the items and return true
if no, return false
valid examples of items:
640 -- copper ore x1
{ 640, 641 } -- copper ore x1, tin ore x1
{ 640, 640 } -- copper ore x2
{ {640,2} } -- copper ore x2
{ {640,2}, 641 } -- copper ore x2, tin ore x1
{ 640, {"gil", 200} } -- copper ore x1, gil x200
******************************************************************************* --]]
function npcUtil.tradeHas(trade, items, exact)
if type(exact) ~= "boolean" then exact = false end
-- create table of traded items, with key/val of itemId/itemQty
local tradedItems = {}
local itemId
local itemQty
for i = 0, trade:getSlotCount()-1 do
itemId = trade:getItemId(i)
itemQty = trade:getItemQty(itemId)
tradedItems[itemId] = itemQty
end
-- create table of needed items, with key/val of itemId/itemQty
local neededItems = {}
if type(items) == "number" then
neededItems[items] = 1
elseif type(items) == "table" then
local itemId
local itemQty
for _, v in pairs(items) do
if type(v) == "number" then
itemId = v
itemQty = 1
elseif type(v) == "table" and #v == 2 and type(v[1]) == "number" and type(v[2]) == "number" then
itemId = v[1]
itemQty = v[2]
elseif type(v) == "table" and #v == 2 and type(v[1]) == "string" and type(v[2]) == "number" and string.lower(v[1]) == "gil" then
itemId = 65535
itemQty = v[2]
else
print("ERROR: invalid value contained within items parameter given to npcUtil.tradeHas.")
itemId = nil
end
if itemId ~= nil then
neededItems[itemId] = (neededItems[itemId] == nil) and itemQty or neededItems[itemId] + itemQty
end
end
else
print("ERROR: invalid items parameter given to npcUtil.tradeHas.")
return false
end
-- determine whether all needed items have been traded. return false if not.
for k, v in pairs(neededItems) do
local tradedQty = (tradedItems[k] == nil) and 0 or tradedItems[k]
if v > tradedQty then
return false
else
tradedItems[k] = tradedQty - v
end
end
-- if an exact trade was requested, check if any excess items were traded. if so, return false.
if exact then
for k, v in pairs(tradedItems) do
if v > 0 then
return false
end
end
end
-- confirm items
for k, v in pairs(neededItems) do
trade:confirmItem(k,v)
end
return true
end
--[[ *******************************************************************************
check whether trade has exactly required items
if yes, confirm all the items and return true
if no, return false
valid examples of items:
640 -- copper ore x1
{ 640, 641 } -- copper ore x1, tin ore x1
{ 640, 640 } -- copper ore x2
{ {640,2} } -- copper ore x2
{ {640,2}, 641 } -- copper ore x2, tin ore x1
{ 640, {"gil", 200} } -- copper ore x1, gil x200
******************************************************************************* --]]
function npcUtil.tradeHasExactly(trade, items)
return npcUtil.tradeHas(trade, items, true)
end
-----------------------------------
-- UpdateNPCSpawnPoint
----------------------------------
function npcUtil.UpdateNPCSpawnPoint(id, minTime, maxTime, posTable, serverVar)
local npc = GetNPCByID(id)
local respawnTime = math.random(minTime, maxTime)
local newPosition = npcUtil.pickNewPosition(npc:getID(), posTable, true)
serverVar = serverVar or nil -- serverVar is optional
if serverVar then
if GetServerVariable(serverVar) <= os.time(t) then
npc:hideNPC(1) -- hide so the NPC is not "moving" through the zone
npc:setPos(newPosition.x, newPosition.y, newPosition.z)
end
end
npc:timer(respawnTime * 1000, function(npc)
npcUtil.UpdateNPCSpawnPoint(id, minTime, maxTime, posTable, serverVar)
end)
end
function npcUtil.fishingAnimation(npc, phaseDuration, func)
func = func or function(npc)
-- return true to not loop again
return false
end
if func(npc) then
return
end
npc:timer(phaseDuration * 1000, function(npc)
local anims =
{
[tpz.anim.FISHING_NPC] = { duration = 5, nextAnim = { tpz.anim.FISHING_START } },
[tpz.anim.FISHING_START] = { duration = 10, nextAnim = { tpz.anim.FISHING_FISH } },
[tpz.anim.FISHING_FISH] = { duration = 10,
nextAnim =
{
tpz.anim.FISHING_CAUGHT,
tpz.anim.FISHING_ROD_BREAK,
tpz.anim.FISHING_LINE_BREAK,
}
},
[tpz.anim.FISHING_ROD_BREAK] = { duration = 3, nextAnim = { tpz.anim.FISHING_NPC } },
[tpz.anim.FISHING_LINE_BREAK] = { duration = 3, nextAnim = { tpz.anim.FISHING_NPC } },
[tpz.anim.FISHING_CAUGHT] = { duration = 5, nextAnim = { tpz.anim.FISHING_NPC } },
[tpz.anim.FISHING_STOP] = { duration = 3, nextAnim = { tpz.anim.FISHING_NPC } },
}
local anim = anims[npc:getAnimation()]
local nextAnimationId = tpz.anim.FISHING_NPC
local nextAnimationDuration = 10
local nextAnim = nil
if anim then
nextAnim = anim.nextAnim[math.random(1, #anim.nextAnim)]
end
if nextAnim then
nextAnimationId = nextAnim
if anims[nextAnimationId] then
nextAnimationDuration = anims[nextAnimationId].duration
end
end
npc:setAnimation(nextAnimationId)
npcUtil.fishingAnimation(npc, nextAnimationDuration, func)
end)
end
function npcUtil.castingAnimation(npc, magicType, phaseDuration, func)
func = func or function(npc)
-- return true to not loop again
return false
end
if func(npc) then
return
end
npc:timer(phaseDuration * 1000, function(npc)
local anims =
{
[tpz.magic.spellGroup.BLACK] = { start = "cabk", duration = 2000, stop = "shbk" },
[tpz.magic.spellGroup.WHITE] = { start = "cawh", duration = 1800, stop = "shwh" },
}
npc:entityAnimationPacket(anims[magicType].start)
npc:timer(anims[magicType].duration, function(npc)
npc:entityAnimationPacket(anims[magicType].stop)
end)
npcUtil.castingAnimation(npc, magicType, phaseDuration, func)
end)
end