forked from CatAnnaDev/Auto-Pet
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
204 lines (186 loc) · 6.24 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
const path = require("path");
class Servant {
constructor(info) {
this.ID = Number(info.id);
this.UniqueID = Number(info.dbid);
this.Name = info.name;
}
stringify() {
return {
"name": this.Name,
"id": this.ID.toString(),
"dbid": this.UniqueID.toString()
};
}
}
module.exports = function AutoPet(mod) {
mod.dispatch.addDefinition("C_REQUEST_SPAWN_SERVANT", 1, path.join(__dirname, "defs", "C_REQUEST_SPAWN_SERVANT.1.def"));
mod.dispatch.addDefinition("C_REQUEST_SPAWN_SERVANT", 2, path.join(__dirname, "defs", "C_REQUEST_SPAWN_SERVANT.2.def"));
mod.dispatch.addDefinition("C_START_SERVANT_ACTIVE_SKILL", 1, path.join(__dirname, "defs", "C_START_SERVANT_ACTIVE_SKILL.1.def"));
mod.dispatch.addDefinition("C_START_SERVANT_ACTIVE_SKILL", 2, path.join(__dirname, "defs", "C_START_SERVANT_ACTIVE_SKILL.2.def"));
mod.dispatch.addDefinition("S_START_COOLTIME_SERVANT_SKILL", 1, path.join(__dirname, "defs", "S_START_COOLTIME_SERVANT_SKILL.1.def"));
mod.dispatch.addDefinition("S_UPDATE_SERVANT_INFO", 1, path.join(__dirname, "defs", "S_UPDATE_SERVANT_INFO.1.def"));
mod.game.initialize("inventory");
let characterId = null;
let playerLoc = null;
let playerW = null;
let petGameId = null;
let petSummoned = false;
let newServant = null;
let mainServant = null;
let petSkillTimeout = null;
let petSkillCooldown = false;
mod.command.add("pet", {
"save": () => {
if (newServant) {
saveServant();
} else {
mod.command.message("You must summon a pet first before you can save it.");
}
},
"feed": arg => {
const n = Number(arg);
if (isNaN(n) || n >= 100 || n < 0) {
mod.command.message("Pet Stamina % must be set between 1 and 99.");
} else {
mod.settings.feedWhenBelow = n;
mod.command.message(`Auto feed is now set to <font color="#5da8ce">${n}%</font>`);
}
},
"on": () => {
mod.settings.characters[characterId].enabled = true;
mod.command.message(`Module <font color="#00FF00">Enabled</font> for <font color="#00BFFF">${ mod.settings.characters[characterId].name }</font>`);
},
"off": () => {
mod.settings.characters[characterId].enabled = false;
mod.command.message(`Module <font color="#FF0000">Disabled</font> for <font color="#00BFFF">${ mod.settings.characters[characterId].name }</font>`);
},
"$none": () => {
mod.settings.enabled = !mod.settings.enabled;
mod.command.message(`Auto Pet is now ${mod.settings.enabled ? "<font color=\"#5dce6a\">Enabled</font>" : "<font color=\"#dc4141\">Disabled</font>"}.`);
}
});
mod.hook("S_LOGIN", 14, event => {
characterId = `${event.playerId}_${event.serverId}`;
if (mod.settings.characters[characterId] == undefined) {
mod.settings.characters[characterId] = {
"name": event.name,
"enabled": true,
"bondSkill": null
};
}
});
mod.hook("C_PLAYER_LOCATION", 5, event => {
playerLoc = event.loc;
playerW = event.w;
});
mod.hook("S_REQUEST_DESPAWN_SERVANT", 1, event => {
if (event.gameId === petGameId) {
petSummoned = false;
petGameId = null;
newServant = null;
mod.clearTimeout(petSkillTimeout);
}
});
mod.hook("S_REQUEST_SPAWN_SERVANT", 4, (event) => {
if (mod.game.me.is(event.ownerId)) {
newServant = new Servant(event);
petSummoned = true;
petGameId = event.gameId;
if (mainServant == null || newServant.ID != mainServant.ID) {
mod.command.message(`Use 'pet save' to save <font color="#30e785">"${event.name}"</font> as your default pet`);
}
const pet = mod.settings.characters[characterId];
if (mod.settings.enabled && pet && pet.enabled && pet.bondSkill) {
usePetSkill();
}
}
});
mod.hook("C_START_SERVANT_ACTIVE_SKILL", mod.majorPatchVersion >= 100 ? 2 : 1, { "filter": { "fake": null } }, event => {
mod.settings.characters[characterId].bondSkill = event.skill;
});
mod.hook("S_START_COOLTIME_SERVANT_SKILL", 1, (event) => {
petSkillCooldown = true;
mod.clearTimeout(petSkillTimeout);
const pet = mod.settings.characters[characterId];
if (mod.settings.enabled && pet && pet.enabled && pet.bondSkill) {
petSkillTimeout = mod.setTimeout(() => {
petSkillCooldown = false;
usePetSkill();
}, event.cooltime + 100);
}
});
mod.game.me.on("resurrect", () => {
const pet = mod.settings.characters[characterId];
if (mod.settings.enabled && pet && pet.enabled && pet.bondSkill) {
usePetSkill();
}
});
mod.hook("S_UPDATE_SERVANT_INFO", 1, (event) => {
if (mainServant && event.dbid == mainServant.UniqueID) {
const energy = (event.energy / 300) * 100;
if (mod.settings.enabled && petSummoned && !mod.game.me.inCombat && energy <= mod.settings.feedWhenBelow) {
feedPet();
}
}
});
mod.hook("S_VISIT_NEW_SECTION", 1, () => {
const key = `${mod.game.me.serverId}_${mod.game.me.playerId}`;
const playerPet = mod.settings.servantsList[key];
if (playerPet != undefined) {
mainServant = new Servant(playerPet);
}
if (mainServant && !petSummoned && mod.settings.enabled) {
summonPet();
}
});
function summonPet() {
mod.send("C_REQUEST_SPAWN_SERVANT", mod.majorPatchVersion >= 100 ? 2 : 1, {
"servantId": mainServant.ID,
"uniqueId": mainServant.UniqueID,
"unk": 0
});
}
function usePetSkill() {
if (petSummoned && mod.game.me.alive && !petSkillCooldown) {
mod.send("C_START_SERVANT_ACTIVE_SKILL", mod.majorPatchVersion >= 100 ? 2 : 1, {
"gameId": petGameId,
"skill": mod.settings.characters[characterId].bondSkill
});
}
}
function saveServant() {
const key = `${mod.game.me.serverId}_${mod.game.me.playerId}`;
mod.settings.servantsList[key] = newServant.stringify();
mod.command.message(`Saved <font color="#30e785">"${newServant.Name}"</font> as your default pet."`);
mainServant = newServant;
}
function feedPet() {
const foods = mod.settings.petFood;
let foodFound = false;
foods.forEach(item => {
const foodItem = mod.game.inventory.findInBagOrPockets(item.id);
if (foodItem) {
foodFound = true;
mod.send("C_USE_ITEM", 3, {
"gameId": mod.game.me.gameId,
"id": foodItem.id,
"dbid": foodItem.dbid,
"target": 0,
"amount": 1,
"dest": 0,
"loc": playerLoc,
"w": playerW,
"unk1": 0,
"unk2": 0,
"unk3": 0,
"unk4": true
});
return;
}
});
if (!foodFound) {
mod.command.message("You don't have any pet food in inventory!");
}
}
};