index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. const config = require('./config.json');
  2. module.exports = function Loot(dispatch) {
  3. let auto = config.modes.auto || false,
  4. autotrash = config.modes.trash || false,
  5. enabled = config.modes.easy || true,
  6. lootInterval = auto ? setInterval(tryLootAll, 250) : null,
  7. location;
  8. let blacklist = config.blacklist.concat(config.motes);
  9. let trash = config.trash.concat(config.crystals.concat(config.strongboxes));
  10. let cid = null;
  11. let loot = {};
  12. let inventory = null;
  13. let commands = {
  14. auto: {
  15. alias: ['auto', 'autoloot', 'toggle'],
  16. run: function() {
  17. auto = !auto;
  18. message(`Autoloot mode toggled: ${auto}`);
  19. if(auto){
  20. lootInterval = setInterval(tryLootAll, 250);
  21. }
  22. else
  23. clearInterval(lootInterval);
  24. }
  25. },
  26. enable: {
  27. alias: ['enable', 'on'],
  28. run: function() {
  29. enabled = true;
  30. message('Easy looting is enabled.');
  31. }
  32. },
  33. disable: {
  34. alias: ['disable', 'off'],
  35. run: function() {
  36. enabled = false;
  37. message('Easy looting is disabled.');
  38. }
  39. },
  40. autotrash: {
  41. alias: ['autotrash', 'trash'],
  42. run: function() {
  43. autotrash = !autotrash;
  44. message('Autotrash toggled: ' + (autotrash ? 'on' : 'off'));
  45. garbageCollect();
  46. }
  47. }
  48. }
  49. dispatch.hook('S_LOGIN', 1, event => { ({cid} = event) })
  50. dispatch.hook('C_CHAT', 1, (event) => {
  51. if(!event.message.includes('!loot'))
  52. return;
  53. let command = event.message.replace(/<\/?[^<>]*>/gi, '').split(' ');
  54. if(command.length > 1) {
  55. for(let cmd in commands) {
  56. if(commands[cmd].alias.indexOf(command[1].toString()) > -1)
  57. commands[cmd].run();
  58. }
  59. }
  60. return false;
  61. });
  62. dispatch.hook('S_LOAD_TOPO', 1, (event) => {
  63. loot = {};
  64. });
  65. dispatch.hook('C_PLAYER_LOCATION', 1, (event) => {
  66. location = event;
  67. });
  68. dispatch.hook('S_SPAWN_DROPITEM', 1, (event) => {
  69. if(!(blacklist.indexOf(event.item) > -1)) loot[event.id.toString()] = event;
  70. });
  71. dispatch.hook('C_TRY_LOOT_DROPITEM', 1, (event) => {
  72. if(enabled) tryLootAll();
  73. });
  74. dispatch.hook('S_DESPAWN_DROPITEM', 1, (event) => {
  75. if(event.id.toString() in loot) delete loot[event.id.toString()];
  76. });
  77. dispatch.hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  78. if(trash.includes(event.item)) {
  79. garbageCollect();
  80. }
  81. });
  82. dispatch.hook('S_SYSTEM_MESSAGE', 1, (event) => {
  83. if(event.message === '@41') return false; // Block "That isn't yours." system message.
  84. });
  85. function garbageCollect(){
  86. dispatch.hook('S_INVEN', 5, event => {
  87. if(autotrash) {
  88. if(event.first) inventory = []
  89. else if(!inventory) return
  90. for(let item of event.items) inventory.push(item)
  91. if(!event.more) {
  92. for(let item of inventory) {
  93. if(item.slot < 40) continue // First 40 slots are reserved for equipment, etc.
  94. else if(trash.includes(item.item)) deleteItem(item.slot, item.amount)
  95. }
  96. inventory = null
  97. }
  98. }
  99. })
  100. }
  101. function deleteItem(slot, amount) {
  102. dispatch.toServer('C_DEL_ITEM', 1, {
  103. cid: cid,
  104. slot: slot - 40,
  105. amount
  106. })
  107. }
  108. function tryLootAll() {
  109. for(let item in loot) {
  110. if(location)
  111. if(Math.abs(loot[item].x - location.x1) < 120 && Math.abs(loot[item].y - location.y1) < 120)
  112. dispatch.toServer('C_TRY_LOOT_DROPITEM', 1, {
  113. id: loot[item].id
  114. });
  115. }
  116. }
  117. function message(msg) {
  118. dispatch.toClient('S_CHAT', 1, {
  119. channel: 24,
  120. authorID: 0,
  121. unk1: 0,
  122. gm: 0,
  123. unk2: 0,
  124. authorName: '',
  125. message: ' (Autoloot) ' + msg
  126. });
  127. }
  128. }