forked from teralove/auto-heal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
283 lines (242 loc) · 11.3 KB
/
index.js
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
'use strict'
module.exports = function AutoHeal(mod) {
let hooks = [],
debug = false,
playerLocation = {},
partyMembers = [],
job = -1,
glyphs = null;
mod.command.add('autoheal', (p1)=> {
if (p1) p1 = p1.toLowerCase();
if (p1 == null) {
mod.settings.autoHeal = !mod.settings.autoHeal;
} else if (p1 === 'off') {
mod.settings.autoHeal = false;
} else if (p1 === 'on') {
mod.settings.autoHeal = true;
} else if (p1 === 'debug') {
debug = !debug;
mod.command.message('Debug ' + (debug ? 'enabled' : 'disabled'));
return;
} else if (p1 === 'test') {
outputDebug(0);
return;
} else if (!isNaN(p1)) {
mod.settings.autoHeal = true;
mod.settings.hpCutoff = (p1 < 0 ? 0 : p1 > 100 ? 100 : p1);
} else {
mod.command.message(p1 +' is an invalid argument');
return;
}
mod.command.message('Healing ' + (mod.settings.autoHeal ? 'enabled (' + mod.settings.hpCutoff + '%)' : 'disabled'));
});
mod.command.add('autocleanse', (p1) => {
if (p1) p1 = p1.toLowerCase();
if (p1 == null) {
mod.settings.autoCleanse = !mod.settings.autoCleanse;
} else if (p1 === 'off') {
mod.settings.autoCleanse = false;
} else if (p1 === 'on') {
mod.settings.autoCleanse = true;
} else {
mod.command.message(p1 +' is an invalid argument for cleanse command');
return;
}
mod.command.message('Cleansing ' + (mod.settings.autoCleanse ? 'enabled' : 'disabled'));
});
mod.command.add('autocast', (p1)=> {
if (p1) p1 = p1.toLowerCase();
if (p1 == null) {
mod.settings.autoCast = !mod.settings.autoCast;
} else if (p1 === 'off') {
mod.settings.autoCast = false;
} else if (p1 === 'on') {
mod.settings.autoCast = true;
} else {
mod.command.message(p1 +' is an invalid argument for cast command');
return;
}
mod.command.message('Casting ' + (mod.settings.autoCast ? 'enabled' : 'disabled'));
});
mod.game.on('enter_game', () => {
job = (mod.game.me.templateId - 10101) % 100;
(mod.settings.skills[job]) ? load() : unload();
})
function hook() {
hooks.push(mod.hook(...arguments));
}
function unload() {
if (hooks.length) {
for (let h of hooks)
mod.unhook(h);
hooks = [];
}
}
function load() {
if (!hooks.length) {
hook('S_PARTY_MEMBER_LIST', 9, (event) => {
const copy = partyMembers;
partyMembers = event.members.filter(m => m.playerId != mod.game.me.playerId); // remove self from targets
// restore missing gameIds. sometimes gameIds are 0 since 64-bit patch
if (copy) {
for(let i = 0; i < partyMembers.length; i++) {
const copyMember = copy.find(m => m.playerId == partyMembers[i].playerId);
if (copyMember) {
partyMembers[i].gameId = copyMember.gameId;
if (copyMember.loc) partyMembers[i].loc = copyMember.loc;
}
}
}
})
hook('S_LEAVE_PARTY', 1, (event) => {
partyMembers = [];
})
hook('C_PLAYER_LOCATION', 5, (event) => {
playerLocation = event;
})
hook('S_SPAWN_ME', 3, (event) => {
playerLocation.loc = event.loc;
playerLocation.w = event.w;
})
hook('S_SPAWN_USER', 17, (event) => {
if (partyMembers.length != 0) {
let member = partyMembers.find(m => m.playerId === event.playerId);
if (member) {
member.gameId = event.gameId;
member.loc = event.loc;
member.alive = event.alive;
member.hpP = (event.alive ? 100 : 0);
}
}
})
hook('S_USER_LOCATION', 6, (event) => {
let member = partyMembers.find(m => m.gameId === event.gameId);
if (member) member.loc = event.loc;
})
hook('S_USER_LOCATION_IN_ACTION', 2, (event) => {
let member = partyMembers.find(m => m.gameId === event.gameId);
if (member) member.loc = event.loc;
})
hook('S_INSTANT_DASH', 3, (event) => {
let member = partyMembers.find(m => m.gameId === event.gameId);
if (member) member.loc = event.loc;
})
hook('S_PARTY_MEMBER_CHANGE_HP', 4, (event) => {
if (mod.game.me.playerId == event.playerId) return;
let member = partyMembers.find(m => m.playerId === event.playerId);
if (member) {
member.hpP = (Number(event.currentHp) / Number(event.maxHp)) * 100;
}
})
hook('S_PARTY_MEMBER_STAT_UPDATE', 4, (event) => {
if (mod.game.me.playerId == event.playerId) return;
let member = partyMembers.find(m => m.playerId === event.playerId);
if (member) {
member.hpP = (Number(event.hp) / Number(event.maxHp)) * 100;
member.alive = event.alive;
}
})
hook('S_DEAD_LOCATION', 2, (event) => {
let member = partyMembers.find(m => (m.gameId === event.gameId));
if (member) {
member.loc = event.loc;
member.hpP = 0;
member.alive = false;
}
})
hook('S_LEAVE_PARTY_MEMBER', 2, (event) => {
partyMembers = partyMembers.filter(m => m.playerId != event.playerId);
});
hook('S_LOGOUT_PARTY_MEMBER', 1, (event) => {
let member = partyMembers.find(m => m.playerId === event.playerId);
if (member) member.online = false;
});
hook('S_BAN_PARTY_MEMBER', 1, (event) => {
partyMembers = partyMembers.filter(m => m.playerId != event.playerId);
});
hook('C_START_SKILL', 7, (event) => {
if (partyMembers.length == 0) return; // be in a party
if (event.skill.id / 10 & 1 != 0) { // is casting (opposed to locking on)
playerLocation.w = event.w;
}
let skill = Math.floor(event.skill.id / 10000);
if(mod.settings.skills[job] && mod.settings.skills[job].includes(skill)) {
if (skill != 9 && !mod.settings.autoHeal) return; // skip heal if disabled
if (skill == 9 && !mod.settings.autoCleanse) return; // skip cleanse if disabled
if (skill == 9 && partyMembers.length > 4) return; // skip cleanse if in a raid
let targetMembers = [];
let maxTargetCount = getMaxTargets(skill);
if (skill != 9) sortHp();
for (let i = 0, n = partyMembers.length; i < n; i++) {
if (partyMembers[i].online &&
partyMembers[i].alive &&
partyMembers[i].hpP != undefined &&
partyMembers[i].hpP != 0 &&
((skill == 9) ? true : partyMembers[i].hpP <= mod.settings.hpCutoff) && // (cleanse) ignore max hp
partyMembers[i].loc != undefined &&
(partyMembers[i].loc.dist3D(playerLocation.loc) / 25) <= mod.settings.maxDistance) {
targetMembers.push(partyMembers[i]);
if (targetMembers.length == maxTargetCount) break;
}
}
if (targetMembers.length > 0) {
if (debug) outputDebug(event.skill);
for (let i = 0, n = targetMembers.length; i < n; i++) {
setTimeout(() => {
mod.send('C_CAN_LOCKON_TARGET', 3, {target: targetMembers[i].gameId, skill: event.skill.id});
}, mod.settings.lockSpeed);
}
if (mod.settings.autoCast) {
setTimeout(() => {
mod.send('C_START_SKILL', 7, Object.assign({}, event, {w: playerLocation.w, skill: (event.skill.id + 10)}));
}, mod.settings.castSpeed);
}
}
}
})
hook('S_CREST_INFO', 2, (event) => {
glyphs = event.crests;
})
hook('S_CREST_APPLY', 2, (event) => {
let glyph = glyphs.find(g => g.id == event.id);
if (glyph) glyph.enable = event.enable;
})
}
}
function getMaxTargets (skill) {
switch(skill) {
case 19: return isGlyphEnabled(28003) ? 4 : 2;
case 37: return 1;
case 5: return isGlyphEnabled(27000) ? 4 : 2;
case 9: return (isGlyphEnabled(27063) || isGlyphEnabled(27003)) ? 5 : 3;
}
return 1;
}
function isGlyphEnabled(glyphId) {
let glyph = glyphs.find(g => g.id == glyphId && g.enable);
if (glyph) return true;
else return false;
}
function sortHp() {
partyMembers.sort(function (a, b) {
return parseFloat(a.hpP) - parseFloat(b.hpP);
});
}
function outputDebug(skill) {
let out = '\nAutoheal Debug... Skill: ' + skill.id + '\tpartyMemebers.length: ' + partyMembers.length;
for (let i = 0; i < partyMembers.length; i++) {
out += '\n' + i + '\t';
let name = partyMembers[i].name;
name += ' '.repeat(21-name.length);
let hp = '\tHP: ' + parseFloat(partyMembers[i].hpP).toFixed(2);
let dist = '\tundefined';
if (partyMembers[i].loc) dist = '\tDist: ' + (partyMembers[i].loc.dist3D(playerLocation.loc) / 25).toFixed(2);
// let vert = '\tVert: ' + (Math.abs(partyMembers[i].loc.z - playerLocation.loc.z) / 25).toFixed(2);
let online = '\tOnline: ' + partyMembers[i].online;
let alive = '\tAlive: ' + partyMembers[i].alive;
let pid = '\tpid: ' + partyMembers[i].playerId + ' gid: ' + partyMembers[i].gameId ;
out += name + hp + dist + online + alive + pid;
}
console.log(out)
}
}