index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. const config = require('./config.js');
  3. module.exports = function AutoLootOld(mod) {
  4. const cmd = mod.command || mod.require.command;
  5. let enable = config.enable,
  6. enableAuto = config.enableAuto,
  7. interval = config.interval,
  8. throttleMax = config.throttleMax,
  9. scanInterval = config.scanInterval,
  10. radius = config.radius;
  11. let location = null,
  12. items = new Map(),
  13. lootTimeout = null;
  14. cmd.add('loot', {
  15. $default() {
  16. enable = !enable;
  17. mod.command.message(`${enable ? 'en' : 'dis'}abled`);
  18. },
  19. auto() {
  20. enableAuto = !enableAuto;
  21. mod.command.message(`auto-loot ${enableAuto ? 'en' : 'dis'}abled`);
  22. }
  23. });
  24. mod.game.me.on('change_zone', () => { items.clear(); });
  25. mod.hook('S_RETURN_TO_LOBBY', 1, () => { items.clear(); });
  26. mod.hook('S_SPAWN_ME', 3, (e) => { location = e.loc; });
  27. mod.hook('C_PLAYER_LOCATION', 5, (e) => { location = e.loc; });
  28. mod.hook('S_SYSTEM_MESSAGE', 1, (e) => { if (e.message === '@41') return false });
  29. mod.hook('C_TRY_LOOT_DROPITEM', 4, () => { if(enable && !lootTimeout) lootTimeout = setTimeout(tryLoot, interval); });
  30. mod.hook('S_DESPAWN_DROPITEM', 4, (e) => { items.delete(e.gameId); });
  31. mod.hook('S_SPAWN_DROPITEM', 8, (e) => {
  32. if(!(config.blacklist.includes(e.item)) && (e.item < 8000 || e.item > 8024) && e.owners.some(owner => owner === mod.game.me.playerId)){
  33. items.set(e.gameId, Object.assign(e, {priority: 0}));
  34. if(enableAuto && !lootTimeout) tryLoot();
  35. }
  36. });
  37. function tryLoot() {
  38. clearTimeout(lootTimeout);
  39. lootTimeout = null;
  40. if(!items.size || mod.game.me.mounted) return;
  41. for(let item of [...items.values()].sort((a, b) => a.priority - b.priority)){
  42. if(location.dist3D(item.loc) <= radius){
  43. mod.send('C_TRY_LOOT_DROPITEM', 4, { gameId: item.gameId });
  44. lootTimeout = setTimeout(tryLoot, Math.min(interval * ++item.priority, throttleMax));
  45. return;
  46. }
  47. }
  48. if(enableAuto) setTimeout(tryLoot, scanInterval);
  49. }
  50. }