· 7 years ago · Nov 21, 2018, 03:20 AM
1--[[
2All weapons looted have a 25% chance to have a random enchant visual
3This is purely visual fun and the visual will be replaced when the weapon is enchanted.
4]]
5
6local chance = 0.25
7
8local charactersSQL = [[
9CREATE TABLE IF NOT EXISTS `custom_item_enchant_visuals` (
10 `iguid` INT(10) UNSIGNED NOT NULL COMMENT 'item DB guid',
11 `display` INT(10) UNSIGNED NOT NULL COMMENT 'enchantID',
12 PRIMARY KEY (`iguid`)
13)
14COMMENT='stores the enchant IDs for the visuals'
15COLLATE='latin1_swedish_ci'
16ENGINE=InnoDB;
17]]
18CharDBQuery(charactersSQL)
19
20-- script variables:
21local EQUIPMENT_SLOT_MAINHAND = 15
22local EQUIPMENT_SLOT_OFFHAND = 16
23local PLAYER_VISIBLE_ITEM_1_ENCHANTMENT = 284
24local PERM_ENCHANTMENT_SLOT = 0
25local DD
26
27-- functions
28local LoadDB, setVisual, applyVisuals, LOGIN
29
30function LoadDB()
31 DD = {}
32 CharDBQuery("DELETE FROM custom_item_enchant_visuals WHERE NOT EXISTS(SELECT 1 FROM item_instance WHERE custom_item_enchant_visuals.iguid = item_instance.guid)")
33 local Q = CharDBQuery("SELECT iguid, display FROM custom_item_enchant_visuals")
34 if (Q) then
35 repeat
36 local iguid, display = Q:GetUInt32(0), Q:GetUInt32(1)
37 DD[iguid] = display
38 until not Q:NextRow()
39 end
40end
41LoadDB()
42
43function setVisual(player, item, display)
44 if (not player or not item) then return
45 false
46 end
47 local iguid = item:GetGUIDLow()
48 local enID = item:GetEnchantmentId(PERM_ENCHANTMENT_SLOT) or 0
49 if (enID ~= 0) then
50 CharDBExecute("DELETE FROM custom_item_enchant_visuals WHERE iguid = "..iguid)
51 DD[iguid] = nil
52 display = enID
53 elseif (not display) then
54 if (not DD[iguid]) then
55 return false
56 end
57 display = DD[iguid]
58 else
59 CharDBExecute("REPLACE INTO custom_item_enchant_visuals (iguid, display) VALUES ("..iguid..", "..display..")")
60 DD[iguid] = display
61 end
62 if (item:IsEquipped()) then
63 player:SetUInt16Value(PLAYER_VISIBLE_ITEM_1_ENCHANTMENT + (item:GetSlot() * 2), 0, display)
64 end
65 return true
66end
67
68function applyVisuals(player)
69 if (not player) then
70 return
71 end
72 for i = EQUIPMENT_SLOT_MAINHAND, EQUIPMENT_SLOT_OFFHAND do
73 setVisual(player, player:GetItemByPos(255, i))
74 end
75end
76
77function LOGIN(event, player)
78 applyVisuals(player)
79end
80
81RegisterPlayerEvent(3, LOGIN)
82RegisterPlayerEvent(29, function(e,p,i,b,s) setVisual(p, i) end)
83
84-- Enchant IDs
85local E = {3789, 3854, 3273, 3225, 3870, 1899, 2674, 2675, 2671, 2672, 3365, 2673, 2343, 425, 3855, 1894, 1103, 1898, 3345, 1743, 3093, 1900, 3846, 1606, 283, 1, 3265, 2, 3, 3266, 1903, 13, 26, 7, 803, 1896, 2666, 25}
86local SubClasses = {
87 [0] = true,
88 [1] = true,
89 [4] = true,
90 [5] = true,
91 [6] = true,
92 [7] = true,
93 [8] = true,
94 [10] = true,
95 [11] = true,
96 [12] = true,
97 [14] = true,
98 [15] = true,
99}
100
101math.randomseed(os.time())
102local function ONITEMLOOT(event, player, item, count, guid)
103 if (item:GetClass() == 2 and SubClasses[item:GetSubClass()]) then
104 if (math.random() < chance) then -- 25% of looted weapons get the visuals
105 setVisual(player, item, E[math.random(#E)])
106 end
107 end
108end
109
110RegisterPlayerEvent(32, ONITEMLOOT)