This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
/
item.lua
559 lines (493 loc) · 14.5 KB
/
item.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
function Item.getType(self)
return ItemType(self:getId())
end
function Item.isContainer(self)
return false
end
function Item.isCreature(self)
return false
end
function Item.isMonster(self)
return false
end
function Item.isNpc(self)
return false
end
function Item.isPlayer(self)
return false
end
function Item.isTeleport(self)
return false
end
function Item.isTile(self)
return false
end
-- Helper class to make string formatting prettier
StringStream = {}
setmetatable(StringStream, {
__call = function(self)
local obj = {}
return setmetatable(obj, {__index = StringStream})
end
})
function StringStream.append(self, str, ...)
self[#self+1] = string.format(str, ...)
end
function StringStream.concat(self, sep)
return table.concat(self, sep)
end
local aux = {
['Defense'] = {key = ITEM_ATTRIBUTE_DEFENSE},
['ExtraDefense'] = {key = ITEM_ATTRIBUTE_EXTRADEFENSE},
['Attack'] = {key = ITEM_ATTRIBUTE_ATTACK},
['AttackSpeed'] = {key = ITEM_ATTRIBUTE_ATTACK_SPEED},
['HitChance'] = {key = ITEM_ATTRIBUTE_HITCHANCE},
['ShootRange'] = {key = ITEM_ATTRIBUTE_SHOOTRANGE},
['Armor'] = {key = ITEM_ATTRIBUTE_ARMOR},
['Duration'] = {key = ITEM_ATTRIBUTE_DURATION, cmp = function(v) return v > 0 end},
['Text'] = {key = ITEM_ATTRIBUTE_TEXT, cmp = function(v) return v ~= '' end},
['Date'] = {key = ITEM_ATTRIBUTE_DATE},
['Writer'] = {key = ITEM_ATTRIBUTE_WRITER, cmp = function(v) return v ~= '' end}
}
function setAuxFunctions()
for name, def in pairs(aux) do
Item['get'.. name] = function(self)
local attr = self:getAttribute(def.key)
if def.cmp and def.cmp(attr) then
return attr
elseif not def.cmp and attr and attr ~= 0 then
return attr
end
local default = ItemType['get'.. name]
return default and default(self:getType()) or nil
end
end
end
setAuxFunctions()
do
local function internalItemGetNameDescription(it, item, subType, addArticle)
local subType = subType or (item and item:getSubType() or -1)
local ss = StringStream()
local obj = item or it
local name = obj:getName()
if name ~= '' then
if it:isStackable() and subType > 1 then
if it:hasShowCount() then
ss:append('%d ', subType)
end
ss:append('%s', obj:getPluralName())
else
if addArticle and obj:getArticle() ~= '' then
ss:append('%s ', obj:getArticle())
end
ss:append('%s', obj:getName())
end
else
ss:append('an item of type %d', obj:getId())
end
return ss:concat()
end
function Item.getNameDescription(self, subType, addArticle)
return internalItemGetNameDescription(self:getType(), self, subType, addArticle)
end
function ItemType.getNameDescription(self, subType, addArticle)
return internalItemGetNameDescription(self, nil, subType, addArticle)
end
end
do
local function addSeparator(ss, begin)
if begin then
begin = false
ss:append(' (')
else
ss:append(', ')
end
return begin
end
local function addGenerics(item, it, abilities, ss, begin)
local obj = item or it
if it:getWeaponType() == WEAPON_DISTANCE and it:getAmmoType() ~= 0 then
ss:append(' (Range:%d', obj:getShootRange())
local attack = obj:getAttack()
local hitChance = obj:getHitChance()
if attack ~= 0 then
ss:append(', Atk%s%d', showpos(attack), math.abs(attack))
end
if hitChance ~= 0 then
ss:append(', Hit%%%s%d', showpos(hitChance), math.abs(hitChance))
end
begin = false
elseif it:getWeaponType() ~= WEAPON_AMMO then
local attack = obj:getAttack()
local defense = obj:getDefense()
local extraDefense = obj:getExtraDefense()
if attack ~= 0 then
begin = false
ss:append(' (Atk:%d', attack)
if abilities.elementType ~= COMBAT_NONE and abilities.elementDamage ~= 0 then
ss:append(' physical + %d %s', abilities.elementDamage, getCombatName(abilities.elementType))
end
end
if defense ~= 0 or extraDefense ~= 0 then
begin = addSeparator(ss, begin)
ss:append('Def:%d', defense)
if extraDefense ~= 0 then
ss:append(' %s%d', showpos(extraDefense), math.abs(extraDefense))
end
end
end
-- Skills
for skill, value in ipairs(abilities.skills) do
if value ~= 0 then
begin = addSeparator(ss, begin)
ss:append('%s %s%d', getSkillName(skill - 1), showpos(value), math.abs(value))
end
end
-- Special Skills
for specialSkill, value in ipairs(abilities.specialSkills) do
if value ~= 0 then
begin = addSeparator(ss, begin)
ss:append('%s %s%d%%', getSpecialSkillName(specialSkill - 1), showpos(value), math.abs(value))
end
end
local magicPoints = abilities.stats[4]
if magicPoints ~= 0 then
begin = addSeparator(ss, begin)
ss:append('magic level %s%d', showpos(magicPoints), math.abs(magicPoints))
end
-- Absorb
local show = abilities.absorbPercent[1]
if show ~= 0 then
for _, value in ipairs(abilities.absorbPercent) do
if value ~= show then
show = 0
end
end
end
if show == 0 then
local tmp = true
for i, value in ipairs(abilities.absorbPercent) do
if value ~= 0 then
if tmp then
tmp = false
begin = addSeparator(ss, begin)
ss:append('protection ')
else
ss:append(', ')
end
ss:append('%s %s%d%%', getCombatName(indexToCombatType(i - 1)), showpos(value), math.abs(value))
end
end
else
begin = addSeparator(ss, begin)
ss:append('protection all %s%d%%', showpos(show), math.abs(show))
end
-- Field absorb
local show = abilities.fieldAbsorbPercent[1]
if show ~= 0 then
for _, value in ipairs(abilities.fieldAbsorbPercent) do
if value ~= show then
show = 0
end
end
end
if show == 0 then
local tmp = true
for i, value in ipairs(abilities.fieldAbsorbPercent) do
if value ~= 0 then
if tmp then
tmp = false
begin = addSeparator(ss, begin)
ss:append('protection ')
else
ss:append(', ')
end
ss:append('%s field %s%d%%', getCombatName(indexToCombatType(i - 1)), showpos(value), math.abs(value))
end
end
else
begin = addSeparator(ss, begin)
ss:append('protection all fields %s%d%%', showpos(show), math.abs(show))
end
if abilities.speed ~= 0 then
begin = addSeparator(ss, begin)
ss:append('speed %s%d', showpos(abilities.speed), math.abs(abilities.speed / 2))
end
return begin
end
local function internalItemGetDescription(it, lookDistance, item, subType, addArticle)
local abilities = it:getAbilities()
local ss = StringStream()
local subType = subType or (item and item:getSubType() or -1)
local text = nil
local begin = true
local obj = item or it
if item then
ss:append(item:getNameDescription(subType, addArticle or true))
else
ss:append(it:getNameDescription(subType, addArticle or true))
end
if it:isRune() then
local rune = Spell(it:getId())
if rune then
if rune:runeLevel() and rune:runeLevel() > 0 or rune:runeMagicLevel() and rune:runeMagicLevel() > 0 then
local tmpVocMap = rune:vocation()
local vocMap = {}
for k, vocName in ipairs(tmpVocMap) do
local vocation = Vocation(vocName)
if vocation and vocation:getPromotion() then
vocMap[#vocMap + 1] = vocName
end
end
ss:append('. %s can only be used by', it:isStackable() and subType > 1 and 'They' or 'It')
-- Only show base vocations in description; promotions should be a given
if #vocMap == 0 then
ss:append(' players')
else
for i = 1, #vocMap - 1 do
local vocName = vocMap[i]
local vocation = Vocation(vocName)
ss:append(' %ss', vocName:lower())
if i + 1 == #vocMap then
ss:append(' and')
else
ss:append(',')
end
end
local vocName = vocMap[#vocMap]
ss:append(' %ss', vocName:lower())
end
ss:append(' with')
if rune:runeLevel() > 0 then
ss:append(' level %d', rune:runeLevel())
end
if rune:runeMagicLevel() > 0 then
if rune:runeLevel() > 0 then
ss:append(' and ')
end
ss:append('magic level %d', rune:runeMagicLevel())
end
ss:append(' or higher')
end
if not begin then
ss:append(')')
end
end
elseif it:getWeaponType() ~= WEAPON_NONE then
begin = addGenerics(item, it, abilities, ss, begin)
if not begin then
ss:append(')')
end
elseif obj:getArmor() ~= 0 or it:hasShowAttributes() then
if obj:getArmor() ~= 0 then
ss:append(' (Arm:%d', obj:getArmor())
begin = false
end
begin = addGenerics(item, it, abilities, ss, begin)
if not begin then
ss:append(')')
end
elseif it:isContainer() or item and item:isContainer() then
local volume = 0
if not item or not item:hasAttribute(ITEM_ATTRIBUTE_UNIQUEID) then
if it:isContainer() then
volume = it:getCapacity()
else
volume = item:getCapacity()
end
end
if volume ~= 0 then
ss:append(' (Vol:%d)', volume)
end
else
local found = true
if abilities.speed > 0 then
ss:append(' (speed %s%d)', showpos(abilities.speed), math.abs(abilities.speed / 2))
elseif bit.band(abilities.conditionSuppressions, CONDITION_DRUNK) == 1 then
ss:append(' (hard drinking)')
elseif abilities.invisible then
ss:append(' (invisibility)')
elseif abilities.regeneration then
ss:append(' (faster regeneration)')
elseif abilities.manashield then
ss:append(' (mana shield)')
else
found = false
end
if not found then
if it:getType() == ITEM_TYPE_KEY then
local aid = item and item:getActionId() or 0
ss:append(' (Key:%s)', ('0'):rep(4 - #tostring(aid)) .. aid)
elseif it:getGroup() == ITEM_GROUP_FLUID then
if subType > 0 then
local name = getSubTypeName(subType)
ss:append(' of %s', name ~= '' and name or 'unknown')
else
ss:append('. It is empty')
end
elseif it:getGroup() == ITEM_GROUP_SPLASH then
local name = getSubTypeName(subType)
ss:append(' of ')
if subType > 0 and name ~= '' then
ss:append(name)
else
ss:append('unknown')
end
elseif it:hasAllowDistRead() and (it:getId() < 7369 or it:getId() > 7371) then
ss:append('.\n')
if lookDistance <= 4 then
if item then
if not text then
text = item:getText()
end
if text then
local writer = item:getWriter()
if writer then
local date = item:getDate()
ss:append('%s wrote', writer)
if date then
ss:append(' on %s', os.date('%d %b %Y'))
end
ss:append(': ')
else
ss:append('You read: ')
end
ss:append(text)
else
ss:append('Nothing is written on it')
end
else
ss:append('Nothing is written on it')
end
else
ss:append('You are too far away to read it.')
end
elseif it:getLevelDoor() ~= 0 and item then
local aid = item:getActionId()
if aid >= it:getLevelDoor() then
ss:append(' for level %d', aid - it:getLevelDoor())
end
end
end
end
if it:hasShowCharges() then
ss:append(' that has %d charge%s left', subType, subType ~= -1 and 's' or '')
end
-- show duration
if it:hasShowDuration() then
if item and item:hasAttribute(ITEM_ATTRIBUTE_DURATION) then
local duration = item:getDuration() / 1000
if duration > 0 then
ss:append(' that will expire in ')
if duration >= 86400 then
local days = math.floor(duration / 86400)
local hours = math.floor((duration % 86400) / 3600)
ss:append('%d day%s', days, days ~= 1 and 's' or '')
if hours > 0 then
ss:append(' and %d hour%s', hours, hours ~= 1 and 's' or '')
end
elseif duration >= 3600 then
local hours = math.floor(duration / 3600)
local minutes = math.floor((duration % 3600) / 60)
ss:append('%d hour%s', hours, hours ~= 1 and 's' or '')
if minutes > 0 then
ss:append(' and %d minute%s', minutes, minutes ~= 1 and 's' or '')
end
elseif duration >= 60 then
local minutes = math.floor(duration / 60)
local seconds = duration % 60
ss:append('%d minute%s', minutes, minutes ~= 1 and 's' or '')
if seconds > 0 then
ss:append(' and %d second%s', seconds, seconds ~= 1 and 's' or '')
end
else
ss:append('%d second%s', duration, duration ~= 1 and 's' or '')
end
end
else
ss:append(' that is brand-new')
end
end
if not it:hasAllowDistRead() or (it:getId() >= 7369 and it:getId() <= 7371) then
ss:append('.')
else
if not text and item then
text = item:getText()
end
if not text or text == '' then
ss:append('.')
end
end
local wieldInfo = it:getWieldInfo()
if wieldInfo ~= 0 then
ss:append('\nIt can only be wielded properly by ')
if bit.band(wieldInfo, WIELDINFO_PREMIUM) ~= 0 then
ss:append('premium ')
end
local vocStr = it:getVocationString()
if vocStr ~= '' then
ss:append(vocStr)
else
ss:append('players')
end
if bit.band(wieldInfo, WIELDINFO_LEVEL) ~= 0 then
ss:append(' of level %d or higher', it:getMinReqLevel())
end
if bit.band(wieldInfo, WIELDINFO_MAGLV) ~= 0 then
if bit.band(wieldInfo, WIELDINFO_LEVEL) ~= 0 then
ss:append(' and')
else
ss:append(' of')
end
ss:append(' magic level %d or higher', it:getMinReqMagicLevel())
end
ss:append('.')
end
if lookDistance <= 1 then
local weight = obj:getWeight()
local count = item and item:getCount() or 1
if weight ~= 0 and it:isPickupable() then
ss:append('\n')
if it:isStackable() and count > 1 and it:hasShowCount() then
ss:append('They weigh ')
else
ss:append('It weighs ')
end
ss:append('%.2f oz.', weight / 100)
end
end
local desc = it:getDescription()
if item then
local specialDesc = item:getSpecialDescription()
if specialDesc ~= '' then
ss:append('\n%s', specialDesc)
elseif lookDistance <= 1 and desc ~= '' then
ss:append('\n%s', desc)
end
elseif lookDistance <= 1 and desc ~= '' then
ss:append('\n%s', it:getDescription())
end
if it:hasAllowDistRead() or (it:getId() >= 7369 and it:getId() <= 7371) then
if not text and item then
text = item:getText()
end
if text and text ~= '' then
ss:append('\n%s', text)
end
end
return ss:concat()
end
if not oldItemDesc then
oldItemDesc = Item.getDescription
end
if configManager.getBoolean(configKeys.LUA_ITEM_DESC) then
function Item.getDescription(self, lookDistance, subType)
return internalItemGetDescription(self:getType(), lookDistance, self, subType)
end
function ItemType.getItemDescription(self, lookDistance, subType)
return internalItemGetDescription(self, lookDistance, nil, subType)
end
else
Item.getDescription = oldItemDesc
end
end