index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 cid = null,
  11. playerId = -1,
  12. location = null,
  13. inventory = null,
  14. loot = {},
  15. lootTimeout = null
  16. let commands = {
  17. auto: {
  18. alias: ['auto', 'autoloot', 'toggle'],
  19. run: function() {
  20. auto = !auto
  21. command.message(`Autoloot mode toggled: ${auto}`)
  22. if(auto && !lootTimeout) tryLoot()
  23. else {
  24. clearTimeout(lootTimeout)
  25. lootTimeout = null
  26. }
  27. }
  28. },
  29. enable: {
  30. alias: ['enable', 'on'],
  31. run: function() {
  32. enabled = true
  33. command.message('Easy looting is enabled.')
  34. }
  35. },
  36. disable: {
  37. alias: ['disable', 'off'],
  38. run: function() {
  39. enabled = false
  40. command.message('Easy looting is disabled.')
  41. }
  42. },
  43. autotrash: {
  44. alias: ['autotrash', 'trash'],
  45. run: function() {
  46. autotrash = !autotrash
  47. command.message('Autotrash toggled: ' + (autotrash ? 'on' : 'off'))
  48. garbageCollect()
  49. }
  50. }
  51. }
  52. dispatch.hook('S_LOGIN', 1, event => { ({cid, 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', 1, event => {
  60. location = event
  61. loot = {}
  62. })
  63. dispatch.hook('C_PLAYER_LOCATION', 2, event => { location = event })
  64. dispatch.hook('S_RETURN_TO_LOBBY', 'raw', () => { loot = {} })
  65. dispatch.hook('S_SPAWN_DROPITEM', 1, event => {
  66. if(event.owners.some(owner => owner.id === playerId) && !blacklist.includes(event.item)) {
  67. loot[event.id.toString()] = Object.assign(event, {priority: 0})
  68. if(auto && !lootTimeout) tryLoot()
  69. }
  70. })
  71. dispatch.hook('C_TRY_LOOT_DROPITEM', 1, event => {
  72. if(enabled && !lootTimeout) lootTimeout = setTimeout(tryLoot, config.lootInterval)
  73. })
  74. dispatch.hook('S_DESPAWN_DROPITEM', 1, event => {
  75. delete loot[event.id.toString()]
  76. })
  77. /*dispatch.hook('S_SYSTEM_MESSAGE_LOOT_ITEM', 1, event => {
  78. if(event.message === '@41') return false // Block "That isn't yours." system message.
  79. })*/
  80. dispatch.hook('S_INVEN', 10, event => {
  81. if(autotrash) {
  82. inventory = inventory ? inventory.concat(event.items) : event.items
  83. if(!event.more) {
  84. for(let item of inventory)
  85. if(item.slot < 40) continue // First 40 slots are reserved for equipment, etc.
  86. else if(trash.includes(item.dbid)) deleteItem(item.slot, item.amount)
  87. inventory = null
  88. }
  89. }
  90. })
  91. function deleteItem(slot, amount) {
  92. dispatch.toServer('C_DEL_ITEM', 1, {
  93. cid: cid,
  94. slot: slot - 40,
  95. amount
  96. })
  97. }
  98. function tryLoot() {
  99. clearTimeout(lootTimeout)
  100. lootTimeout = null
  101. let lootItems = Object.values(loot).sort((a, b) => a.priority - b.priority)
  102. if(!lootItems.length) return
  103. for(let item of lootItems)
  104. if(dist3D(location, item) < config.lootRadius) {
  105. dispatch.toServer('C_TRY_LOOT_DROPITEM', 1, { id: item.id })
  106. item.priority++
  107. lootTimeout = setTimeout(tryLoot, config.lootInterval)
  108. return
  109. }
  110. if(auto) setTimeout(tryLoot, config.lootScanInterval)
  111. }
  112. }
  113. function dist3D(loc1, loc2) {
  114. return Math.sqrt(Math.pow(loc2.x - loc1.x, 2) + Math.pow(loc2.y - loc1.y, 2) + Math.pow(loc2.z - loc1.z, 2))
  115. }