forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfishing.ts
More file actions
458 lines (364 loc) · 13 KB
/
Copy pathfishing.ts
File metadata and controls
458 lines (364 loc) · 13 KB
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
import type { EntityId, FishingState, Position, RuntimeCharacter, RuntimeClient } from "./types/runtime";
import type { HandleProtocolApi } from "./handleProtocol";
import type { SocketApi } from "./socket";
import { getCharacterById, getClientById } from "./runtimeRegistry";
export {};
const vars = require("./vars");
const handleProtocol = require("./handleProtocol") as HandleProtocolApi;
const socket = require("./socket") as SocketApi;
const workingLock = require("./workingLock");
type FishingUser = RuntimeCharacter & {
id: EntityId;
nameCharacter: string;
map: number;
pos: Position;
dead?: number | boolean;
navegando?: number | boolean;
level?: number;
inv: Record<string, { idItem: number; cant: number; equipped?: number | boolean }>;
idItemWeapon?: number | string;
fishing?: FishingState;
};
type FishingReward = {
itemId: number;
weight: number;
};
export type FishingApi = {
isFishingRod: (idItem: number) => boolean;
getRodPower: (idItem: number) => number;
handleRodUse: (ws: RuntimeClient, idPos: number | string) => boolean;
handleMapClick: (ws: RuntimeClient, x: number, y: number) => boolean;
processTick: (now: number) => void;
cancelFishing: (idUser: EntityId, reason?: string, keepTargeting?: boolean) => void;
};
function clonePosition(pos: Position): Position {
return { x: pos.x, y: pos.y };
}
function getUser(idUser: EntityId) {
return getCharacterById<FishingUser>(idUser);
}
function withUserClient(idUser: EntityId | null | undefined, callback: (client: RuntimeClient) => void) {
const client = getClientById(idUser);
if (!client) {
return;
}
callback(client);
}
function clearFishingState(user: FishingUser, keepTargeting = false) {
if (!keepTargeting) {
user.fishing = undefined;
return;
}
const state = user.fishing;
if (!state) {
return;
}
user.fishing = {
pendingTarget: true,
slot: state.slot,
itemId: state.itemId,
power: state.power,
};
}
function isWithinMapBounds(x: number, y: number) {
return x >= 1 && x <= 100 && y >= 1 && y <= 100;
}
function isWaterGraphic(graphicLayer1: number) {
return (
(graphicLayer1 >= 1505 && graphicLayer1 <= 1520) ||
(graphicLayer1 >= 5665 && graphicLayer1 <= 5680) ||
(graphicLayer1 >= 13547 && graphicLayer1 <= 13562)
);
}
function isWaterTile(idMap: number, pos: Position) {
if (!isWithinMapBounds(pos.x, pos.y)) {
return false;
}
const tile = vars.mapa[idMap]?.[pos.y]?.[pos.x];
const graphicLayer1 = tile?.graphics?.[1] ?? 0;
const graphicLayer2 = tile?.graphics?.[2] ?? 0;
return isWaterGraphic(graphicLayer1) && !graphicLayer2;
}
function isAdjacentToWater(idMap: number, pos: Position) {
return (
isWaterTile(idMap, { x: pos.x + 1, y: pos.y }) ||
isWaterTile(idMap, { x: pos.x - 1, y: pos.y }) ||
isWaterTile(idMap, { x: pos.x, y: pos.y + 1 }) ||
isWaterTile(idMap, { x: pos.x, y: pos.y - 1 })
);
}
function hasInvalidFishingTrigger(idMap: number, pos: Position) {
if (!isWithinMapBounds(pos.x, pos.y)) {
return true;
}
return vars.mapa[idMap]?.[pos.y]?.[pos.x]?.trigger === vars.fishing.invalidTrigger;
}
function getEquippedFishingRod(user: FishingUser) {
const weaponSlot = Number(user.idItemWeapon ?? 0);
if (!weaponSlot) {
return null;
}
const item = user.inv?.[weaponSlot];
if (!item || !item.equipped || !fishing.isFishingRod(item.idItem)) {
return null;
}
return {
slot: weaponSlot,
itemId: item.idItem,
power: fishing.getRodPower(item.idItem),
};
}
function getRewardsForPower(power: number): FishingReward[] {
const rewardsByPower = vars.fishing.fishByPower?.[power] as FishingReward[] | undefined;
return rewardsByPower ?? vars.fishing.fishByPower?.[1] ?? [];
}
function getSimulatedFishingSkill(user: FishingUser) {
return Math.min(100, Math.max(0, Number(user.level ?? 0) * 3));
}
function getFishingChanceForSkill(skill: number) {
if (skill < 20) {
return 20;
}
if (skill < 40) {
return 35;
}
if (skill < 70) {
return 55;
}
if (skill < 100) {
return 68;
}
return 80;
}
function isFishingNet(itemId: number) {
return /red de pesca/i.test(String(vars.datObj?.[itemId]?.name ?? ""));
}
function getFishingRewardAmount(itemId: number) {
return isFishingNet(itemId) ? Math.floor(Math.random() * 5) + 2 : Math.floor(Math.random() * 3) + 1;
}
function pickWeightedReward(power: number) {
const rewards = getRewardsForPower(power);
if (!rewards.length) {
return null;
}
const totalWeight = rewards.reduce((sum, reward) => sum + reward.weight, 0);
let roll = Math.floor(Math.random() * totalWeight) + 1;
for (const reward of rewards) {
roll -= reward.weight;
if (roll <= 0) {
return reward;
}
}
return rewards[rewards.length - 1];
}
function addRewardToInventory(user: FishingUser, itemId: number, amount: number) {
for (const [slot, item] of Object.entries(user.inv)) {
if (item.idItem === itemId && item.cant + amount <= 10000) {
item.cant += amount;
withUserClient(user.id, (userClient) => {
handleProtocol.agregarUserInvItem(user.id, slot, userClient);
});
return true;
}
}
if (Object.keys(user.inv).length >= 21) {
return false;
}
let nextSlot = 1;
const usedSlots = Object.keys(user.inv)
.map(Number)
.sort((left, right) => left - right);
while (usedSlots[nextSlot - 1] === nextSlot) {
nextSlot++;
}
user.inv[nextSlot] = {
idItem: itemId,
cant: amount,
equipped: 0,
};
withUserClient(user.id, (userClient) => {
handleProtocol.agregarUserInvItem(user.id, nextSlot, userClient);
});
return true;
}
function validateFishingStart(user: FishingUser, target: Position) {
if (!getEquippedFishingRod(user)) {
return "Debes tener equipada una caña de pescar.";
}
if (!isWaterTile(user.map, target)) {
return "Zona de pesca no autorizada. Busca otro lugar para hacerlo.";
}
if (user.navegando) {
if (!isWaterTile(user.map, user.pos)) {
return "Debes estar navegando sobre el agua para pescar desde la barca.";
}
} else if (isWaterTile(user.map, user.pos) || !isAdjacentToWater(user.map, user.pos)) {
return "Acércate a la costa para pescar.";
}
if (hasInvalidFishingTrigger(user.map, user.pos) || hasInvalidFishingTrigger(user.map, target)) {
return "Zona de pesca no autorizada. Busca otro lugar para hacerlo.";
}
if (Math.abs(user.pos.x - target.x) > 1 || Math.abs(user.pos.y - target.y) > 1) {
return "Debes seleccionar una casilla de agua cercana para pescar.";
}
return null;
}
const fishing: FishingApi = {
isFishingRod(idItem) {
return Boolean(vars.fishing.rods?.[idItem]);
},
getRodPower(idItem) {
return vars.fishing.rods?.[idItem]?.power ?? 1;
},
handleRodUse(ws, idPos) {
const user = getUser(ws.id!);
if (!user) {
return false;
}
const item = user.inv?.[idPos];
if (!item || !this.isFishingRod(item.idItem)) {
return false;
}
if (!item.equipped || Number(user.idItemWeapon ?? 0) !== Number(idPos)) {
handleProtocol.console("Debes equiparte la caña de pescar para usarla.", "white", 0, 0, ws);
return true;
}
if (user.fishing?.active) {
this.cancelFishing(user.id, "Has dejado de pescar.");
return true;
}
user.fishing = {
pendingTarget: true,
slot: Number(idPos),
itemId: item.idItem,
power: this.getRodPower(item.idItem),
};
handleProtocol.console("Selecciona una casilla de agua cercana para pescar.", "#fcd34d", 0, 0, ws);
return true;
},
handleMapClick(ws, x, y) {
const user = getUser(ws.id!);
const state = user?.fishing;
if (!user || !state?.pendingTarget) {
return false;
}
const validationError = validateFishingStart(user, { x, y });
if (validationError) {
handleProtocol.console(validationError, "white", 0, 0, ws);
return true;
}
const equippedRod = getEquippedFishingRod(user);
if (!equippedRod) {
clearFishingState(user);
handleProtocol.console("Debes tener equipada una caña de pescar.", "white", 0, 0, ws);
return true;
}
if (workingLock.hasAnotherActiveWorkOnSameIp(user.id)) {
handleProtocol.console("Ya tienes otro personaje trabajando desde esta IP.", "white", 0, 0, ws);
return true;
}
const startedAt = Date.now();
user.fishing = {
active: true,
pendingTarget: false,
slot: equippedRod.slot,
itemId: equippedRod.itemId,
power: equippedRod.power,
target: { x, y },
origin: clonePosition(user.pos),
startedAt,
nextTickAt: startedAt + vars.timing.fishingTickMs,
};
socket.loopArea(user.id, (target) => {
if (!target.isNpc) {
withUserClient(target.id, (targetClient) => {
handleProtocol.playSound(user.id, vars.arSounds.SND_PESCAR, targetClient);
});
}
});
handleProtocol.console("Comienzas a pescar.", "#7dd3fc", 0, 0, ws);
return true;
},
processTick(now) {
for (const idUser in vars.personajes) {
const user = getUser(idUser);
const state = user?.fishing;
if (!user || !state?.active || !state.nextTickAt || now < state.nextTickAt) {
continue;
}
if (workingLock.shouldCancelForSameIpConflict(idUser)) {
this.cancelFishing(
idUser,
"La pesca se canceló porque ya tienes otro personaje trabajando desde esta IP.",
);
continue;
}
if (
user.dead ||
!state.origin ||
user.pos.x !== state.origin.x ||
user.pos.y !== state.origin.y ||
!state.target
) {
this.cancelFishing(idUser, "La pesca se canceló.");
continue;
}
const equippedRod = getEquippedFishingRod(user);
if (!equippedRod || equippedRod.itemId !== state.itemId) {
this.cancelFishing(idUser, "La pesca se canceló porque ya no tienes la caña equipada.");
continue;
}
if (validateFishingStart(user, state.target)) {
this.cancelFishing(idUser, "La pesca se canceló porque ya no estás en una posición válida.");
continue;
}
state.nextTickAt = now + vars.timing.fishingTickMs;
const fishingChance = getFishingChanceForSkill(getSimulatedFishingSkill(user));
if (Math.floor(Math.random() * 100) + 1 > fishingChance) {
continue;
}
const reward = pickWeightedReward(equippedRod.power);
if (!reward) {
continue;
}
const rewardAmount = getFishingRewardAmount(equippedRod.itemId);
if (!addRewardToInventory(user, reward.itemId, rewardAmount)) {
withUserClient(idUser, (userClient) => {
handleProtocol.console("Tienes el inventario lleno.", "white", 0, 0, userClient);
});
this.cancelFishing(idUser, "La pesca se detuvo porque no tienes espacio en el inventario.");
continue;
}
withUserClient(idUser, (userClient) => {
handleProtocol.console(
`Has pescado ${rewardAmount} ${vars.datObj[reward.itemId].name}.`,
"#86efac",
0,
0,
userClient,
);
});
socket.loopArea(idUser, (target) => {
if (!target.isNpc) {
withUserClient(target.id, (targetClient) => {
handleProtocol.playSound(idUser, vars.arSounds.SND_PESCAR, targetClient);
});
}
});
}
},
cancelFishing(idUser, reason, keepTargeting = false) {
const user = getUser(idUser);
const wasActive = Boolean(user?.fishing?.active);
if (!user?.fishing) {
return;
}
clearFishingState(user, keepTargeting);
if (reason && wasActive) {
withUserClient(idUser, (userClient) => {
handleProtocol.console(reason, "white", 0, 0, userClient);
});
}
},
};
module.exports = fishing;