index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. const Command = require('command'),
  2. config = require('./config.json'),
  3. blacklist = config.blacklist.concat(config.motes),
  4. trash = config.trash.concat(config.crystals, config.strongboxes)
  5. module.exports = function Loot(dispatch) {
  6. const command = Command(dispatch)
  7. let auto = config.modes.auto || false,
  8. autotrash = config.modes.trash || false,
  9. enabled = config.modes.easy || true
  10. let gameId = null,
  11. playerId = -1,
  12. myLoc = null,
  13. mounted = false,
  14. inven = null,
  15. loot = new Map(),
  16. lootTimeout = null
  17. let commands = {
  18. auto: {
  19. alias: ['auto', 'autoloot', 'toggle'],
  20. run: function() {
  21. auto = !auto
  22. command.message(`Autoloot mode toggled: ${auto}`)
  23. if(auto && !lootTimeout) tryLoot()
  24. else {
  25. clearTimeout(lootTimeout)
  26. lootTimeout = null
  27. }
  28. }
  29. },
  30. enable: {
  31. alias: ['enable', 'on'],
  32. run: function() {
  33. enabled = true
  34. command.message('Easy looting is enabled.')
  35. }
  36. },
  37. disable: {
  38. alias: ['disable', 'off'],
  39. run: function() {
  40. enabled = false
  41. command.message('Easy looting is disabled.')
  42. }
  43. },
  44. autotrash: {
  45. alias: ['autotrash', 'trash'],
  46. run: function() {
  47. autotrash = !autotrash
  48. command.message('Autotrash toggled: ' + (autotrash ? 'on' : 'off'))
  49. }
  50. }
  51. }
  52. dispatch.hook('S_LOGIN', 9, event => { ({gameId, playerId} = event) })
  53. command.add('loot', c => {
  54. if(c)
  55. for(let cmd in commands)
  56. if(commands[cmd].alias.includes(c))
  57. commands[cmd].run()
  58. })
  59. dispatch.hook('S_LOAD_TOPO', 3, event => {
  60. myLoc = event.loc
  61. mounted = false
  62. loot.clear()
  63. })
  64. dispatch.hook('C_PLAYER_LOCATION', 3, event => { myLoc = event.loc })
  65. dispatch.hook('S_RETURN_TO_LOBBY', 'raw', () => { loot.clear() })
  66. dispatch.hook('S_MOUNT_VEHICLE', 2, event => { if(event.gameId.equals(gameId)) mounted = true })
  67. dispatch.hook('S_UNMOUNT_VEHICLE', 2, event => { if(event.gameId.equals(gameId)) mounted = false })
  68. dispatch.hook('S_SPAWN_DROPITEM', 6, event => {
  69. if(event.owners.some(owner => owner.playerId === playerId) && !blacklist.includes(event.item)) {
  70. loot.set(event.gameId.toString(), Object.assign(event, {priority: 0}))
  71. if(auto && !lootTimeout) tryLoot()
  72. }
  73. })
  74. dispatch.hook('C_TRY_LOOT_DROPITEM', 4, event => {
  75. if(enabled && !lootTimeout) lootTimeout = setTimeout(tryLoot, config.lootInterval)
  76. })
  77. dispatch.hook('S_DESPAWN_DROPITEM', 4, event => { loot.delete(event.gameId.toString()) })
  78. dispatch.hook('S_INVEN', 12, event => {
  79. inven = event.first ? event.items : inven.concat(event.items)
  80. if(!event.more) {
  81. if(autotrash)
  82. for(let item of inven)
  83. if(item.slot < 40) continue // First 40 slots are reserved for equipment, etc.
  84. else if(trash.includes(item.id)) deleteItem(item.slot, item.amount)
  85. inven = null
  86. }
  87. })
  88. function deleteItem(slot, amount) {
  89. dispatch.toServer('C_DEL_ITEM', 2, {
  90. gameId,
  91. slot: slot - 40,
  92. amount
  93. })
  94. }
  95. function tryLoot() {
  96. clearTimeout(lootTimeout)
  97. lootTimeout = null
  98. if(!loot.size) return
  99. if(!mounted)
  100. for(let l of [...loot.values()].sort((a, b) => a.priority - b.priority))
  101. if(myLoc.dist3D(l.loc) <= config.lootRadius) {
  102. dispatch.toServer('C_TRY_LOOT_DROPITEM', 4, l)
  103. lootTimeout = setTimeout(tryLoot, Math.min(config.lootInterval * ++l.priority, config.lootThrottleMax))
  104. return
  105. }
  106. if(auto) setTimeout(tryLoot, config.lootScanInterval)
  107. }
  108. }