/
core.lua
1379 lines (1279 loc) · 56.7 KB
/
core.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local UPGRADE_SYSTEM_VERSION = "3.0.3"
print(">> Loaded Upgrade System v" .. UPGRADE_SYSTEM_VERSION)
US_CONDITIONS = {}
US_BUFFS = {}
local US_SUBID = {}
local TargetCombatEvent = EventCallback
TargetCombatEvent.onTargetCombat = function(creature, target)
target:registerEvent("UpgradeSystemHealth")
target:registerEvent("UpgradeSystemDeath")
return RETURNVALUE_NOERROR
end
TargetCombatEvent:register()
local LoginEvent = CreatureEvent("UpgradeSystemLogin")
function LoginEvent.onLogin(player)
us_onLogin(player)
return true
end
local HealthChangeEvent = CreatureEvent("UpgradeSystemHealth")
local ManaChangeEvent = CreatureEvent("UpgradeSystemMana")
local DeathEvent = CreatureEvent("UpgradeSystemDeath")
local KillEvent = CreatureEvent("UpgradeSystemKill")
local PrepareDeathEvent = CreatureEvent("UpgradeSystemPD")
function us_onEquip(cid, iuid, slot)
local player = Player(cid)
if not player:getSlotItem(slot) then
return
end
iuid = iuid + 1
local slotUid = player:getSlotItem(slot):getUniqueId()
if iuid ~= slotUid then
return
end
local item = Item(iuid)
if player and item then
local maxHP = player:getMaxHealth()
local maxMP = player:getMaxMana()
local newBonuses = item:getBonusAttributes()
if not newBonuses then
return
end
for i = 1, #newBonuses do
local value = newBonuses[i]
local bonusId = value[1]
local bonusValue = value[2]
local attr = US_ENCHANTMENTS[bonusId]
if attr then
if attr.combatType == US_TYPES.CONDITION then
if not US_CONDITIONS[bonusId] then
US_CONDITIONS[bonusId] = {}
end
local itemId = item:getId()
if not US_CONDITIONS[bonusId][bonusValue] then
US_CONDITIONS[bonusId][bonusValue] = {}
end
if not US_CONDITIONS[bonusId][bonusValue][itemId] then
US_CONDITIONS[bonusId][bonusValue][itemId] = Condition(attr.condition)
if attr.condition ~= CONDITION_MANASHIELD then
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_SUBID, 1000 + player:getNextSubId(slot, i))
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(attr.param, attr.percentage == true and 100 + bonusValue or bonusValue)
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_TICKS, -1)
else
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_TICKS, 86400000)
end
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
player:addCondition(US_CONDITIONS[bonusId][bonusValue][itemId])
if attr == BONUS_TYPE_MAXHP then
if player:getHealth() == maxHP then
player:addHealth(player:getMaxHealth())
end
end
if attr == BONUS_TYPE_MAXMP then
if player:getMana() == maxMP then
player:addMana(player:getMaxMana())
end
end
else
player:addCondition(US_CONDITIONS[bonusId][bonusValue][itemId])
if attr.param == CONDITION_PARAM_STAT_MAXHITPOINTS then
if player:getHealth() == maxHP then
player:addHealth(player:getMaxHealth())
end
end
if attr.param == CONDITION_PARAM_STAT_MAXMANAPOINTS then
if player:getMana() == maxMP then
player:addMana(player:getMaxMana())
end
end
end
end
end
end
end
end
local MoveItemEvent = EventCallback
MoveItemEvent.onMoveItem = function(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
if not item:getType():isUpgradable() and not item:getType():canHaveItemLevel() or toPosition.y == CONST_SLOT_AMMO then
return true
end
if not item:getType():usesSlot(toPosition.y) then
return true
end
if item:isUnidentified() then
if toPosition.y <= CONST_SLOT_AMMO and toPosition.y ~= CONST_SLOT_BACKPACK then
player:sendTextMessage(MESSAGE_STATUS_SMALL, "You can't wear unidentified items.")
return false
end
end
if US_CONFIG.REQUIRE_LEVEL == true then
if player:getLevel() < item:getItemLevel() and not item:isLimitless() then
if toPosition.y <= CONST_SLOT_AMMO and toPosition.y ~= CONST_SLOT_BACKPACK then
player:sendTextMessage(MESSAGE_STATUS_SMALL, "You need higher level to equip that item.")
return false
end
end
end
if toPosition.y <= CONST_SLOT_AMMO then
if toPosition.y ~= CONST_SLOT_BACKPACK then
if fromPosition.y >= 64 or fromPosition.x ~= CONTAINER_POSITION then
-- remove old
local oldItem = player:getSlotItem(toPosition.y)
if oldItem then
if oldItem:getType():isUpgradable() then
local oldBonuses = oldItem:getBonusAttributes()
if oldBonuses then
local itemId = oldItem:getId()
for key, value in pairs(oldBonuses) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.combatType == US_TYPES.CONDITION then
if US_CONDITIONS[value[1]] and US_CONDITIONS[value[1]][value[2]] and US_CONDITIONS[value[1]][value[2]][itemId] then
if US_CONDITIONS[value[1]][value[2]][itemId]:getType() ~= CONDITION_MANASHIELD then
player:removeCondition(
US_CONDITIONS[value[1]][value[2]][itemId]:getType(),
CONDITIONID_COMBAT,
US_CONDITIONS[value[1]][value[2]][itemId]:getSubId()
)
else
player:removeCondition(US_CONDITIONS[value[1]][value[2]][itemId]:getType(), CONDITIONID_COMBAT)
end
end
end
end
end
end
end
end
-- apply new
if item:getType():isUpgradable() then
local newBonuses = item:getBonusAttributes()
if newBonuses then
addEvent(us_onEquip, 10, player:getId(), item:getUniqueId(), toPosition.y)
end
end
end
end
end
return true
end
MoveItemEvent:register()
local ItemMovedEvent = EventCallback
ItemMovedEvent.onItemMoved = function(player, item, count, fromPosition, toPosition, fromCylinder, toCylinder)
if not item:getType():isUpgradable() then
return
end
if toPosition.y <= CONST_SLOT_AMMO and toPosition.y ~= CONST_SLOT_BACKPACK then
return
end
if fromPosition.y >= 64 and toPosition.y >= 64 then
return
end
if fromPosition.y >= 64 and toPosition.y == CONST_SLOT_BACKPACK then
return
end
local bonuses = item:getBonusAttributes()
if bonuses then
local itemId = item:getId()
for i = 1, #bonuses do
local value = bonuses[i]
local bonusId = value[1]
local bonusValue = value[2]
local attr = US_ENCHANTMENTS[bonusId]
if attr then
if attr.combatType == US_TYPES.CONDITION then
if US_CONDITIONS[bonusId] and US_CONDITIONS[bonusId][bonusValue] and US_CONDITIONS[bonusId][bonusValue][itemId] then
if US_CONDITIONS[bonusId][bonusValue][itemId]:getType() ~= CONDITION_MANASHIELD then
player:removeCondition(
US_CONDITIONS[bonusId][bonusValue][itemId]:getType(),
CONDITIONID_COMBAT,
US_CONDITIONS[bonusId][bonusValue][itemId]:getSubId()
)
else
player:removeCondition(US_CONDITIONS[bonusId][bonusValue][itemId]:getType(), CONDITIONID_COMBAT)
end
end
end
end
end
end
end
ItemMovedEvent:register()
function us_onLogin(player)
player:registerEvent("UpgradeSystemKill")
player:registerEvent("UpgradeSystemHealth")
player:registerEvent("UpgradeSystemMana")
player:registerEvent("UpgradeSystemPD")
local maxHP = player:getMaxHealth()
local maxMP = player:getMaxMana()
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = player:getSlotItem(slot)
if item then
local newBonuses = item:getBonusAttributes()
if newBonuses then
local itemId = item:getId()
for i = 1, #newBonuses do
local value = newBonuses[i]
local bonusId = value[1]
local bonusValue = value[2]
local attr = US_ENCHANTMENTS[bonusId]
if attr then
if attr.combatType == US_TYPES.CONDITION then
if not US_CONDITIONS[bonusId] then
US_CONDITIONS[bonusId] = {}
end
if not US_CONDITIONS[bonusId][bonusValue] then
US_CONDITIONS[bonusId][bonusValue] = {}
end
if not US_CONDITIONS[bonusId][bonusValue][itemId] then
US_CONDITIONS[bonusId][bonusValue][itemId] = Condition(attr.condition)
if attr.condition ~= CONDITION_MANASHIELD then
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_SUBID, 1000 + player:getNextSubId(slot, i))
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(
attr.param,
attr.percentage == true and 100 + bonusValue or bonusValue
)
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_TICKS, -1)
else
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_TICKS, 86400000)
end
US_CONDITIONS[bonusId][bonusValue][itemId]:setParameter(CONDITION_PARAM_BUFF_SPELL, true)
player:addCondition(US_CONDITIONS[bonusId][bonusValue][itemId])
if attr == BONUS_TYPE_MAXHP then
if player:getHealth() == maxHP then
player:addHealth(player:getMaxHealth())
end
end
if attr == BONUS_TYPE_MAXMP then
if player:getMana() == maxMP then
player:addMana(player:getMaxMana())
end
end
else
player:addCondition(US_CONDITIONS[bonusId][bonusValue][itemId])
if attr.param == CONDITION_PARAM_STAT_MAXHITPOINTS then
if player:getHealth() == maxHP then
player:addHealth(player:getMaxHealth())
end
end
if attr.param == CONDITION_PARAM_STAT_MAXMANAPOINTS then
if player:getMana() == maxMP then
player:addMana(player:getMaxMana())
end
end
end
end
end
end
end
end
end
end
function ManaChangeEvent.onManaChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if not creature or not attacker then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if creature:isPlayer() and creature:getParty() and attacker:isPlayer() and attacker:getParty() then
if creature:getParty() == attacker:getParty() then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
end
if primaryType == COMBAT_LIFEDRAIN or secondaryType == COMBAT_LIFEDRAIN then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if primaryType == COMBAT_MANADRAIN or secondaryType == COMBAT_MANADRAIN then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if creature == attacker and primaryType ~= COMBAT_HEALING then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if origin == ORIGIN_CONDITION then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
return us_onDamaged(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
end
function HealthChangeEvent.onHealthChange(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if not creature or not attacker then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if creature:isPlayer() and creature:getParty() and attacker:isPlayer() and attacker:getParty() then
if creature:getParty() == attacker:getParty() then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
end
if primaryType == COMBAT_LIFEDRAIN or secondaryType == COMBAT_LIFEDRAIN then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if creature == attacker and primaryType ~= COMBAT_HEALING then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if origin == ORIGIN_CONDITION then
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
return us_onDamaged(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
end
function us_onDamaged(creature, attacker, primaryDamage, primaryType, secondaryDamage, secondaryType, origin)
if primaryType == COMBAT_HEALING or secondaryType == COMBAT_HEALING then
if attacker:isPlayer() then
local primaryTotal = 0
local secondaryTotal = 0
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = attacker:getSlotItem(slot)
if item then
if item:getType():usesSlot(slot) then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.name == "Increased Healing" then
if primaryType == COMBAT_HEALING then
primaryTotal = primaryTotal + value[2]
end
if secondaryType == COMBAT_HEALING then
secondaryTotal = secondaryTotal + value[2]
end
end
end
end
end
end
end
end
if primaryType == COMBAT_HEALING then
primaryDamage = math.floor(primaryDamage + (primaryDamage * primaryTotal / 100))
end
if secondaryType == COMBAT_HEALING then
secondaryDamage = math.floor(secondaryDamage + (secondaryDamage * secondaryTotal / 100))
end
end
if creature:isPlayer() then
local primaryTotal = 0
local secondaryTotal = 0
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = creature:getSlotItem(slot)
if item then
if item:getType():usesSlot(slot) then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.name == "Increased Healing" then
if primaryDamage > 0 then
primaryTotal = primaryTotal + value[2]
end
if secondaryDamage > 0 then
secondaryTotal = secondaryTotal + value[2]
end
end
end
end
end
end
end
end
if primaryTotal > 0 then
primaryDamage = math.floor(primaryDamage + (primaryDamage * primaryTotal / 100))
end
if secondaryTotal > 0 then
secondaryDamage = math.floor(secondaryDamage + (secondaryDamage * secondaryTotal / 100))
end
end
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
if attacker:isPlayer() then
local pid = attacker:getId()
if US_BUFFS[pid] then
if US_BUFFS[pid][1] then
if primaryDamage ~= 0 then
primaryDamage = primaryDamage + (primaryDamage * US_BUFFS[pid][1].value / 100)
end
if secondaryDamage ~= 0 then
secondaryDamage = secondaryDamage + (secondaryDamage * US_BUFFS[pid][1].value / 100)
end
end
end
local doubleDamageTotal = 0
local primaryDamageTotal = 0
local secondaryDamageTotal = 0
local lifeStealTotal = 0
local manaStealTotal = 0
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = attacker:getSlotItem(slot)
if item then
if item:getType():usesSlot(slot) then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.combatType and attr.combatType ~= US_TYPES.CONDITION then
if attr.combatType == US_TYPES.TRIGGER then
if attr.triggerType == US_TRIGGERS.ATTACK then
attr.execute(attacker, creature, value[2])
end
elseif attr.name == "Double Damage" then
doubleDamageTotal = doubleDamageTotal + value[2]
else
if attr.combatDamage then
if (attr.combatDamage % (primaryType + primaryType) >= primaryType) == true then
if attr.combatType == US_TYPES.OFFENSIVE then
primaryDamageTotal = primaryDamageTotal + value[2]
end
end
if (attr.combatDamage % (secondaryType + secondaryType) >= secondaryType) == true then
if attr.combatType == US_TYPES.OFFENSIVE then
secondaryDamageTotal = secondaryDamageTotal + value[2]
end
end
end
if attr.name == "Life Steal" then
lifeStealTotal = lifeStealTotal + value[2]
end
if attr.name == "Mana Steal" then
manaStealTotal = manaStealTotal + value[2]
end
end
end
end
end
end
end
end
end
if doubleDamageTotal > 0 then
if math.random(100) < doubleDamageTotal then
primaryDamage = primaryDamage * 2
secondaryDamage = secondaryDamage * 2
end
end
if primaryDamageTotal > 0 then
primaryDamage = math.floor(primaryDamage + (primaryDamage * primaryDamageTotal / 100))
end
if secondaryDamageTotal > 0 then
secondaryDamage = math.floor(secondaryDamage + (secondaryDamage * secondaryDamageTotal / 100))
end
local damage = (primaryDamage + secondaryDamage)
if damage < 0 then
damage = damage * -1
end
if lifeStealTotal > 0 then
local lifeSteal = math.floor((damage * (lifeStealTotal / 100)))
if lifeSteal > 0 then
attacker:addHealth(lifeSteal)
end
end
if manaStealTotal > 0 then
local manaSteal = math.floor((damage * (manaStealTotal / 100)))
if manaSteal > 0 then
attacker:addMana(manaSteal)
end
end
end
if creature:isPlayer() then
local primaryDamageTotal = 0
local secondaryDamageTotal = 0
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = creature:getSlotItem(slot)
if item then
if item:getType():usesSlot(slot) then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.combatType and attr.combatType ~= US_TYPES.CONDITION then
if attr.combatType == US_TYPES.TRIGGER then
if attr.triggerType == US_TRIGGERS.HIT then
attr.execute(creature, attacker, value[2])
end
else
if attr.combatDamage then
if (attr.combatDamage % (primaryType + primaryType) >= primaryType) == true then
if attr.combatType == US_TYPES.DEFENSIVE and creature:isPlayer() then
primaryDamageTotal = primaryDamageTotal + value[2]
end
end
if (attr.combatDamage % (secondaryType + secondaryType) >= secondaryType) == true then
if attr.combatType == US_TYPES.DEFENSIVE and creature:isPlayer() then
secondaryDamageTotal = secondaryDamageTotal + value[2]
end
end
end
end
end
end
end
end
end
end
end
if primaryDamageTotal > 0 then
primaryDamage = math.floor(primaryDamage - (primaryDamage * primaryDamageTotal / 100))
end
if secondaryDamageTotal > 0 then
secondaryDamage = math.floor(secondaryDamage - (secondaryDamage * secondaryDamageTotal / 100))
end
end
return primaryDamage, primaryType, secondaryDamage, secondaryType
end
function DeathEvent.onDeath(creature, corpse, lasthitkiller, mostdamagekiller, lasthitunjustified, mostdamageunjustified)
if not lasthitkiller or not creature:isMonster() or not corpse or corpse.itemid == 0 or not corpse:isContainer() then
return true
end
if not lasthitkiller:isPlayer() and not lasthitkiller:getMaster() then
return true
end
addEvent(
us_CheckCorpse,
10,
creature:getType(),
corpse:getPosition(),
lasthitkiller:getMaster() and lasthitkiller:getMaster():getId() or lasthitkiller:getId()
)
return true
end
function KillEvent.onKill(player, target, lastHit)
if not player or not player:isPlayer() or not target or not target:isMonster() then
return
end
local center = target:getPosition()
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = player:getSlotItem(slot)
if item then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.triggerType == US_TRIGGERS.KILL then
attr.execute(player, value[2], center, target)
end
end
end
end
end
end
end
function PrepareDeathEvent.onPrepareDeath(creature, killer)
if creature:isPlayer() then
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = creature:getSlotItem(slot)
if item then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.name == "Revive on death" then
if math.random(100) < value[2] then
creature:addHealth(creature:getMaxHealth())
creature:addMana(creature:getMaxMana())
creature:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
creature:sendTextMessage(MESSAGE_INFO_DESCR, "You have been revived!")
return false
end
end
end
end
end
end
end
end
return true
end
local GainExperienceEvent = EventCallback
GainExperienceEvent.onGainExperience = function(player, source, exp, rawExp)
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = player:getSlotItem(slot)
if item then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.name == "Experience" then
exp = exp + math.ceil(exp * value[2] / 100)
end
end
end
end
end
end
return exp
end
GainExperienceEvent:register()
function us_CheckCorpse(monsterType, corpsePosition, killerId)
local killer = Player(killerId)
local corpse = Tile(corpsePosition):getTopDownItem()
if killer and killer:isPlayer() and corpse and corpse:isContainer() then
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = killer:getSlotItem(slot)
if item then
local values = item:getBonusAttributes()
if values then
for key, value in pairs(values) do
local attr = US_ENCHANTMENTS[value[1]]
if attr then
if attr.name == "Additonal Gold" then
local cc, plat, gold = 0, 0, 0
for i = 0, corpse:getSize() do
local item = corpse:getItem(i)
if item then
if item.itemid == 2160 then
gold = gold + (item:getCount() * 10000)
elseif item.itemid == 2152 then
gold = gold + (item:getCount() * 100)
elseif item.itemid == 2148 then
gold = gold + item:getCount()
end
end
end
gold = math.floor(gold * value[2] / 100)
while gold >= 10000 do
gold = gold / 10000
cc = cc + 1
end
if cc > 0 then
local crystalCoin = Game.createItem(2160, cc)
corpse:addItemEx(crystalCoin)
end
while gold >= 100 do
gold = gold / 100
plat = plat + 1
end
if plat > 0 then
local platinumCoin = Game.createItem(2152, plat)
corpse:addItemEx(platinumCoin)
end
if gold > 0 then
local goldCoin = Game.createItem(2148, gold)
corpse:addItemEx(goldCoin)
end
end
end
end
end
end
end
local iLvl = monsterType:calculateItemLevel()
if iLvl >= US_CONFIG.CRYSTAL_FOSSIL_DROP_LEVEL then
if math.random(US_CONFIG.CRYSTAL_FOSSIL_DROP_CHANCE) == 1 then
corpse:addItem(US_CONFIG.CRYSTAL_FOSSIL, 1)
local specs = Game.getSpectators(corpsePosition, false, true, 9, 9, 8, 8)
if #specs > 0 then
for i = 1, #specs do
local player = specs[i]
player:say("Crystal Fossil!", TALKTYPE_MONSTER_SAY, false, player, corpsePosition)
end
end
end
end
for i = 0, corpse:getCapacity() do
local item = corpse:getItem(i)
if item then
local itemType = item:getType()
if itemType then
if itemType:canHaveItemLevel() then
item:setItemLevel(math.min(US_CONFIG.MAX_ITEM_LEVEL, math.random(math.max(1, iLvl - 5), iLvl)), true)
end
if itemType:isUpgradable() then
if math.random(US_CONFIG.UNIDENTIFIED_DROP_CHANCE) == 1 then
item:unidentify()
else
item:rollRarity()
end
end
end
end
end
end
end
function us_RemoveBuff(pid, buffId, buffName)
if US_BUFFS[pid] then
US_BUFFS[pid][buffId] = nil
local player = Player(pid)
if player then
player:sendTextMessage(MESSAGE_STATUS_WARNING, buffName .. " ended!")
end
end
end
local LookEvent = EventCallback
LookEvent.onLook = function(player, thing, position, distance, description)
if thing:isItem() and thing.itemid == US_CONFIG.ITEM_MIND_CRYSTAL and thing:hasMemory() then
for i = 4, 1, -1 do
local enchant = thing:getBonusAttribute(i)
if enchant then
local attr = US_ENCHANTMENTS[enchant[1]]
description = description:gsub(thing:getName() .. "%.", "%1\n" .. attr.format(enchant[2]))
end
end
elseif thing:isItem() then
if thing:getType():isUpgradable() then
local upgrade = thing:getUpgradeLevel()
local itemLevel = thing:getItemLevel()
if upgrade > 0 then
description = description:gsub(thing:getName(), "%1 +" .. upgrade)
end
if description:find("(%)%.?)") then
description = description:gsub("(%)%.?)", "%1\nItem Level: " .. itemLevel)
else
if upgrade > 0 then
description = description:gsub("+" .. upgrade .. "%.", "%1\nItem Level: " .. itemLevel)
else
description = description:gsub(thing:getName(), "%1\nItem Level: " .. itemLevel)
end
end
if thing:isUnidentified() then
description = description:gsub(thing:getName(), "unidentified %1")
if thing:getArticle():len() > 0 and thing:getArticle() ~= "an" then
description = description:gsub("You see (" .. thing:getArticle() .. "%S?)", "You see an")
end
else
description = description:gsub(thing:getName(), thing:getRarity().name .. " %1")
if thing:getArticle():len() > 0 and thing:getRarity().name == "epic" and thing:getArticle() ~= "an" then
description = description:gsub("You see (" .. thing:getArticle() .. "%S?)", "You see an")
end
if thing:isUnique() then
description = description:gsub("Item Level: " .. itemLevel, thing:getUniqueName() .. "\n%1")
end
for i = thing:getMaxAttributes(), 1, -1 do
local enchant = thing:getBonusAttribute(i)
if enchant then
local attr = US_ENCHANTMENTS[enchant[1]]
description = description:gsub("Item Level: " .. itemLevel, "%1\n" .. attr.format(enchant[2]))
end
end
end
if US_CONFIG.REQUIRE_LEVEL then
if thing:isLimitless() then
if description:find("It can only be wielded properly by") then
description = description:gsub("It can only be wielded properly by (.-)%.", "Removed required Item Level to wear.")
else
description = description:gsub("It weighs", "Removed required Item Level to wear.\nIt weighs")
end
else
if description:find("of level (%d+) or higher") then
for match in description:gmatch("of level (%d+) or higher") do
if tonumber(match) < itemLevel then
description = description:gsub("of level (%d+) or higher", "of level " .. itemLevel .. " or higher")
end
end
elseif description:find("It can only be wielded properly by") then
description =
description:gsub(
"It can only be wielded properly by (.+).\n",
"It can only be wielded properly by %1 of level " .. itemLevel .. " or higher.\n"
)
else
if description:find("It weighs") then
description =
description:gsub("It weighs", "It can only be wielded properly by players of level " .. itemLevel .. " or higher.\nIt weighs")
else
description = description .. "\nIt can only be wielded properly by players of level " .. itemLevel .. " or higher."
end
end
end
end
if thing:isMirrored() then
if description:find("It weighs") then
description = description:gsub("oz.(.+)", "oz.%1\nMirrored")
else
description = description .. "\nMirrored"
end
end
elseif thing:getType():canHaveItemLevel() then
local itemLevel = thing:getItemLevel()
if description:find("(%)%.?)") then
description = description:gsub("(%)%.?)", "%1\nItem Level: " .. itemLevel)
end
end
elseif thing:isPlayer() then
local iLvl = 0
for slot = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
local item = thing:getSlotItem(slot)
if item then
iLvl = iLvl + item:getItemLevel()
end
end
description = description .. "\nTotal Item LeveL: " .. iLvl
end
return description
end
LookEvent:register(10)
function Item.rollAttribute(self, player, itemType, weaponType, unidentify)
if not itemType:isUpgradable() or self:isUnique() then
return false
end
local attrIds = {}
local item_level = self:getItemLevel()
if unidentify then
if US_CONFIG.IDENTIFY_UPGRADE_LEVEL then
local upgrade_level = 1
for i = US_CONFIG.MAX_UPGRADE_LEVEL, 1, -1 do
if i >= US_CONFIG.UPGRADE_LEVEL_DESTROY then
if math.random(100) <= US_CONFIG.UPGRADE_DESTROY_CHANCE[i] then
upgrade_level = i
break
end
else
if math.random(100) <= US_CONFIG.UPGRADE_SUCCESS_CHANCE[i] then
upgrade_level = i
break
end
end
end
self:setUpgradeLevel(upgrade_level)
end
local slots = math.random(1, self:getMaxAttributes())
local usItemType = self:getItemType()
for i = 1, slots do
local attrId = math.random(1, #US_ENCHANTMENTS)
local attr = US_ENCHANTMENTS[attrId]
while isInArray(attrIds, attrId) or attr.minLevel and item_level < attr.minLevel or bit.band(usItemType, attr.itemType) == 0 or
attr.chance and math.random(100) >= attr.chance do
attrId = math.random(1, #US_ENCHANTMENTS)
attr = US_ENCHANTMENTS[attrId]
end
table.insert(attrIds, attrId)
local value = attr.VALUES_PER_LEVEL and math.random(1, math.ceil(item_level * attr.VALUES_PER_LEVEL)) or 1
self:setCustomAttribute("Slot" .. i, attrId .. "|" .. value)
end
return true
else
local bonuses = self:getBonusAttributes()
if bonuses then
if #bonuses >= self:getMaxAttributes() then
player:sendTextMessage(MESSAGE_STATUS_WARNING, "Max number of bonuses reached!")
return false
end
for v, k in pairs(bonuses) do
table.insert(attrIds, k[1])
end
end
local usItemType = self:getItemType()
local attrId = math.random(1, #US_ENCHANTMENTS)
local attr = US_ENCHANTMENTS[attrId]
while isInArray(attrIds, attrId) or attr.minLevel and item_level < attr.minLevel or bit.band(usItemType, attr.itemType) == 0 or
attr.chance and math.random(100) >= attr.chance do
attrId = math.random(1, #US_ENCHANTMENTS)
attr = US_ENCHANTMENTS[attrId]
end
local value = attr.VALUES_PER_LEVEL and math.random(1, math.ceil(item_level * attr.VALUES_PER_LEVEL)) or 1
self:setCustomAttribute("Slot" .. self:getLastSlot() + 1, attrId .. "|" .. value)
return true
end
return false
end
function Item.addAttribute(self, slot, attr, value)
self:setCustomAttribute("Slot" .. slot, attr .. "|" .. value)
end
function Item.setAttributeValue(self, slot, value)
self:setCustomAttribute("Slot" .. slot, value)
end
function Item.getBonusAttribute(self, slot)
local bonuses = self:getCustomAttribute("Slot" .. slot)
if bonuses then
local data = {}
for bonus in bonuses:gmatch("([^|]+)") do
data[#data + 1] = tonumber(bonus)
end
return data
end
return nil
end
function Item.getBonusAttributes(self)
local data = {}
for i = 1, self:getMaxAttributes() do
local bonuses = self:getCustomAttribute("Slot" .. i)
if bonuses then
local t = {}
for bonus in bonuses:gmatch("([^|]+)") do
t[#t + 1] = tonumber(bonus)
end
data[#data + 1] = t
end
end
return #data > 0 and data or nil
end
function Item.getLastSlot(self)
local last = 0
for i = 1, self:getMaxAttributes() do
if self:getCustomAttribute("Slot" .. i) then
last = i
end
end
return last
end
function Item.setItemLevel(self, level, first)
local oldLevel = self:getItemLevel()
local itemType = ItemType(self.itemid)
local finalValue = 0
local value = 0
if oldLevel < level then
value = (level - oldLevel)
else
value = (oldLevel - level)
end
if itemType:getAttack() > 0 then
if value >= US_CONFIG.ATTACK_PER_ITEM_LEVEL then
finalValue = math.floor((value / US_CONFIG.ATTACK_PER_ITEM_LEVEL) * US_CONFIG.ATTACK_FROM_ITEM_LEVEL)
else
finalValue = 0
end
if oldLevel < level then
self:setAttribute(
ITEM_ATTRIBUTE_ATTACK,
(self:getAttribute(ITEM_ATTRIBUTE_ATTACK) > 0) and (self:getAttribute(ITEM_ATTRIBUTE_ATTACK) + finalValue) or
(itemType:getAttack() + finalValue)
)
else
self:setAttribute(
ITEM_ATTRIBUTE_ATTACK,
(self:getAttribute(ITEM_ATTRIBUTE_ATTACK) > 0) and (self:getAttribute(ITEM_ATTRIBUTE_ATTACK) - finalValue) or
(itemType:getAttack() - finalValue)
)