AutoFindDropItemCommand.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. local AutoFindDropItemCommand = class('AutoFindDropItemCommand', framework.SimpleCommand)
  2. local proxyUtils = requireProxy( "proxyUtils" )
  3. local function diffLen( x, y )
  4. return math.max( math.abs( x ), math.abs( y ) )
  5. end
  6. function AutoFindDropItemCommand:ctor()
  7. end
  8. function AutoFindDropItemCommand:execute(notification)
  9. local player = global.gamePlayerController:GetMainPlayer()
  10. if not player then
  11. return nil
  12. end
  13. local facade = global.Facade
  14. local autoProxy = facade:retrieveProxy( global.ProxyTable.Auto )
  15. local target = nil
  16. local pMapX = player:GetMapX()
  17. local pMapY = player:GetMapY()
  18. -- only tips
  19. local bagProxy = facade:retrieveProxy( global.ProxyTable.Bag )
  20. local bagfull = bagProxy:isToBeFull()
  21. -- find target
  22. local itemVec = global.dropItemManager:FindDropItemInCurrViewField( SL:GetMetaValue("GAME_DATA","FindDropItemRange"), true )
  23. local mainPlayerID = player:GetID()
  24. local pickLen = nil
  25. local currPickLen = nil
  26. local targets = {}
  27. local targetCount = 0
  28. for _, dropItem in ipairs( itemVec ) do
  29. if self:isPickable( dropItem, mainPlayerID, bagfull ) then
  30. -- for random pick nearest item
  31. currPickLen = diffLen( dropItem:GetMapX() - pMapX, dropItem:GetMapY() - pMapY )
  32. if pickLen then
  33. if currPickLen > pickLen then
  34. break
  35. end
  36. end
  37. pickLen = currPickLen
  38. targetCount = targetCount + 1
  39. targets[targetCount] = dropItem
  40. end
  41. end
  42. -- random drop item
  43. if targetCount > 0 then
  44. local targetIndex = Random( targetCount )
  45. target = targets[targetIndex]
  46. end
  47. if target then
  48. local targetID = target:GetID()
  49. local targetX = target:GetMapX()
  50. local targetY = target:GetMapY()
  51. -- record pick item ID
  52. autoProxy:SetPickItemID( targetID )
  53. if not (targetX == pMapX and targetY == pMapY) then
  54. -- move to range pos
  55. local movePos = {}
  56. movePos.x = targetX
  57. movePos.y = targetY
  58. movePos.type = global.MMO.INPUT_MOVE_TYPE_FINDITEM
  59. facade:sendNotification( global.NoticeTable.InputMove, movePos )
  60. end
  61. else
  62. -- oh, can't find drop
  63. autoProxy:ClearAutoPick()
  64. end
  65. end
  66. function AutoFindDropItemCommand:isPickable( dropItem, mainPlayerID, bagfull )
  67. if not dropItem then
  68. return false
  69. end
  70. if dropItem:IsIngored() then
  71. return false
  72. end
  73. if bagfull and dropItem:GetTypeIndex() ~= 0 then -- 背包满可以捡金币
  74. return false
  75. end
  76. -- check pick state
  77. if dropItem:GetPickState() >= 1 or dropItem:IsPickTimeout() then
  78. return false
  79. end
  80. -- check owner
  81. if not proxyUtils:AutoPickItemEnable( dropItem, mainPlayerID ) then
  82. return false
  83. end
  84. return true
  85. end
  86. return AutoFindDropItemCommand