forked from JasXSL/ExiWoW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGateway.lua
193 lines (155 loc) · 6.04 KB
/
Gateway.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
local appName, internal = ...
local require = internal.require;
function internal.Gateway()
local Event = require("Event");
local Character = require("Character");
local RPText = require("RPText");
local Spell = require("Spell");
local Loot = require("Loot");
local Action = require("Action");
local Database = require("Database");
local Timer = require("Timer");
local Index = require("Index");
local Condition = require("Condition");
-- Swing
local function onSwing(unit, sender, crit)
--print("Checking hardlimits on swing, sender", sender, Index.checkHardLimits(sender, "player", false));
if not Index.checkHardLimits(sender, "player", true) then return end
local chance = globalStorage.swing_text_freq;
if crit ~= "" then
chance = chance*4;
end -- Crits have 3x chance for swing text
local rand = math.random();
--print("Swing. Crit: "..crit.." chance "..chance.." rand "..rand);
if not RPText.getTakehitCD() and rand < chance and unit and not UnitIsPlayer(unit) and not UnitInVehicle("player") then
-- id, senderUnit, receiverUnit, senderChar, receiverChar, eventData, event, action
local npc = Character.buildNPC(unit, sender);
local rp = RPText.get(Event.Types.SWING..crit, unit, "player", npc, ExiWoW.ME, nil, Event.Types.SWING..crit);
if rp then
RPText.setTakehitTimer();
rp:convertAndReceive(npc, ExiWoW.ME)
end
end
end
Event.on(Event.Types.SWING, function(data)
onSwing(data.unit, data.name, "");
end);
Event.on(Event.Types.SWING_CRIT, function(data)
onSwing(data.unit, data.name, "_CRIT");
end);
-- See buildSpellTrigger in Event for aura
-- name is the name of the unit
local function onSpell(event, aura, unit, name)
if not Index.checkHardLimits(unit, "player", true) then return end
local chance = 1;
if event == Event.SPELL_TICK then
chance = 0.05;
end
chance = chance*globalStorage.spell_text_freq;
local npc = Character.buildNPC(unit, name);
local eventData = RPText.buildSpellData(aura.spellId, aura.name, aura.harmful, npc.name, aura.count, aura.crit);
-- See if this spell was bound at all
local spells = Spell.filter(aura.name, unit, "player", npc, ExiWoW.ME, eventData, event);
local all = Database.getIDs("Spell", aura.name);
for _,sp in pairs(all) do
if type(sp.onTrigger) == "function" then
sp:onTrigger(event, unit, "player", npc, ExiWoW.ME);
end
end
for _,sp in pairs(spells) do
if type(sp.onAccepted) == "function" then
sp:onAccepted(event, unit, "player", npc, ExiWoW.ME);
end
end
-- Trigger random RP texts
local spell = spells[1];
if spell and not RPText.getTakehitCD() and math.random() < chance*spell.chanceMod and not UnitInVehicle("player") then
eventData.tags = spell:exportTags();
local name = aura.name;
if spell.alias then
name = spell.alias;
end
local rp = RPText.get(name, unit, "player", npc, ExiWoW.ME, eventData, event);
if rp then
RPText.setTakehitTimer();
rp:convertAndReceive(npc, ExiWoW.ME, false, eventData);
end
end
end
Event.on(Event.Types.SPELL_ADD, function(data) onSpell(Event.Types.SPELL_ADD, data.aura, data.unit, data.name); end);
Event.on(Event.Types.SPELL_REM, function(data) onSpell(Event.Types.SPELL_REM, data.aura, data.unit, data.name); end);
Event.on(Event.Types.SPELL_TICK, function(data) onSpell(Event.Types.SPELL_TICK, data.aura, data.unit, data.name); end);
Event.on(Event.Types.SPELL_RAN, function(data) onSpell(Event.Types.SPELL_RAN, data.aura, data.unit, data.name); end);
local function rollLoot(event, npcName, eventData)
local npc = Character.buildNPC("none", npcName);
---- senderUnit, receiverUnit, senderChar, receiverChar, eventData, event, action
local available = Loot.filter("none", "player", npc, ExiWoW.ME, eventData, event, Action.get("FORAGE"));
--print("Rolling loot with", event, npc.name, "found", #available);
local out = {};
for _,loot in pairs(available) do
for _,item in pairs(loot.items) do
local chance = 1
if item.chance then chance = item.chance end
if math.random() < chance then
local quant = item.quant;
if not quant or quant < 1 then quant = 1 end
if type(item.quantRand) == "number" and item.quantRand > 0 then
quant = quant+math.random(item.quantRand+1)-1;
end
local added = ExiWoW.ME:addItem(item.type, item.id, quant);
if added then
-- RP text
if item.text then
item.text.item = added.name;
item.text:convertAndReceive(npc, ExiWoW.ME, false, nil, function(text)
text = string.gsub(text, "%%Qs", quant ~= 1 and "s" or "")
text = string.gsub(text, "%%Q", quant)
return text
end);
end
if item.sound then PlaySound(item.sound, "Dialog") end
table.insert(out, item);
if event == Event.Types.FORAGE then
return out;
end
end
end
end
end
if #out > 0 then return out end
return false
end
Event.on(Event.Types.MONSTER_KILL, function(data)
if not Index.checkHardLimits("player", "player", true) then return end
rollLoot(Event.Types.MONSTER_KILL, data.name);
RPText.trigger(nil, data.name, "player", Character.buildNPC("none", data.name), ExiWoW.ME, {}, Event.Types.MONSTER_KILL);
end);
Event.on(Event.Types.FORAGE, function(data)
if not rollLoot(Event.Types.FORAGE, data.name) then
PlaySound(1142, "Dialog")
RPText.print("You found nothing");
end
end);
-- World containers
local scanned_containers = {};
Event.on(Event.Types.CONTAINER_OPENED, function(data)
if not Index.checkHardLimits("player", "player", true) then return end
--print("Container opened", ExiWoW.json.encode(data));
for i=1, GetNumLootItems() do
local items = {GetLootSourceInfo(i)};
--print("Item", i, GetLootSlotInfo(i))
--print("Sources", GetLootSourceInfo(i));
for k,v in ipairs(items) do
if k%2 == 1 then
if not scanned_containers[v] then
scanned_containers[v] = true;
Timer.set(function()
scanned_containers[v] = nil;
end, 300);
rollLoot(Event.Types.CONTAINER_OPENED, data.container, data);
end
end
end
end
end);
end