Oskar1121

Untitled

Oct 8th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. --[[
  2. @Authors: Ben Dol (BeniS)
  3. @Details: Auto targeting event logic
  4. ]]
  5.  
  6. TargetsModule.AutoTarget = {}
  7. AutoTarget = TargetsModule.AutoTarget
  8.  
  9. -- Variables
  10.  
  11. AutoTarget.creatureData = {}
  12.  
  13. -- Methods
  14.  
  15. function AutoTarget.init()
  16. connect(Creature, { onAppear = AutoTarget.addCreature })
  17. connect(TargetsModule, { onAddTarget = AutoTarget.scan })
  18. end
  19.  
  20. function AutoTarget.terminate()
  21. disconnect(Creature, { onAppear = AutoTarget.addCreature })
  22. disconnect(TargetsModule, { onAddTarget = AutoTarget.scan })
  23. end
  24.  
  25. function AutoTarget.hasTargets()
  26. return AutoTarget.creatureData ~= nil and #AutoTarget.creatureData > 0
  27. end
  28.  
  29. function AutoTarget.getCreatureData()
  30. return AutoTarget.creatureData
  31. end
  32.  
  33. function AutoTarget.scan()
  34. print("scanning")
  35. local targetList = {}
  36. for k,v in pairs(TargetsModule.getTargets()) do
  37. table.insert(targetList, v:getName())
  38. end
  39.  
  40. local player = g_game.getLocalPlayer()
  41. local targets = player:getTargetsInArea(targetList, true)
  42.  
  43. for k,target in pairs(targets) do
  44. if not target:isDead() and not target:isRemoved() then
  45. if not AutoTarget.isAlreadyStored(target) then
  46. AutoTarget.addCreature(target)
  47. end
  48. else
  49. AutoTarget.removeCreature(target)
  50. end
  51. end
  52. end
  53.  
  54. function AutoTarget.isAlreadyStored(creature)
  55. return AutoTarget.creatureData[creature:getId()] ~= nil
  56. end
  57.  
  58. function AutoTarget.addCreature(creature)
  59. -- Avoid adding new targets when attacking
  60. if creature and creature:isMonster() then
  61. --connect(creature, { onHealthPercentChange = AutoTarget.onTargetHealthChange })
  62. connect(creature, { onDeath = AutoLoot.onTargetDeath })
  63. connect(creature, { onDisappear = AutoTarget.removeCreature })
  64.  
  65. AutoTarget.creatureData[creature:getId()] = creature
  66. end
  67. end
  68.  
  69. function AutoTarget.removeCreature(creature)
  70. if creature then
  71. --disconnect(creature, { onHealthPercentChange = AutoTarget.onTargetHealthChange })
  72. disconnect(creature, { onDeath = AutoLoot.onTargetDeath })
  73. disconnect(creature, { onDisappear = AutoTarget.removeCreature })
  74.  
  75. AutoTarget.creatureData[creature:getId()] = nil
  76. end
  77. end
  78.  
  79. function AutoTarget.checkChaseMode(target)
  80. if not target then return end
  81. local t = TargetsModule.getTarget(target:getName())
  82. if t then
  83. local setting = t:getSetting(1)
  84. if setting:getFollow() then
  85. g_game.setChaseMode(ChaseOpponent)
  86. else
  87. g_game.setChaseMode(DontChase)
  88. end
  89. end
  90. end
  91.  
  92. function AutoTarget.checkStance(target)
  93. if not target then return end
  94. local t = TargetsModule.getTarget(target:getName())
  95. if t then
  96. local setting = t:getSetting(1)
  97. if setting then
  98. g_game.setFightMode(setting:getStance())
  99. end
  100. end
  101. end
  102.  
  103. function AutoTarget.onTargetHealthChange(creature)
  104.  
  105. end
  106.  
  107. function AutoTarget.isValidTarget(creature)
  108. local player = g_game.getLocalPlayer()
  109. return TargetsModule.hasTarget(creature:getName()) and player:hasSight(creature:getPosition())
  110. end
  111.  
  112. function AutoTarget.getBestTarget()
  113. local player = g_game.getLocalPlayer()
  114. local playerPos = player:getPosition()
  115. local target, distance = nil, nil
  116.  
  117. for id,t in pairs(AutoTarget.creatureData) do
  118. if t and AutoTarget.isValidTarget(t) then
  119. local toPos = t:getPosition()
  120. if math.abs(playerPos.x - toPos.x) < 13 and math.abs(playerPos.y - toPos.y) < 8 then
  121. local d = Position.distance(playerPos, toPos)
  122. if not target or d < distance then
  123. -- print("Found closest target")
  124. target = t
  125. distance = d
  126. end
  127. end
  128. end
  129. end
  130. return target
  131. end
  132.  
  133. function AutoTarget.onStopped()
  134. --
  135. end
  136.  
  137. function AutoTarget.Event(event)
  138. -- TODO: There seems to be a rare bug when changing
  139. -- attacker too fast the client gets confused thinking
  140. -- its attacking when on the server its not. To resolve
  141. -- this we will need to find out what is causing it and
  142. -- also add a fail safe timeout mechanism.
  143. -- See: https://github.com/BenDol/otclient-candybot/issues/20
  144.  
  145. -- Cannot continue if still attacking or is in pz
  146. local player = g_game.getLocalPlayer()
  147. if player:hasState(PlayerStates.Pz) then
  148. return Helper.safeDelay(600, 2000)
  149. elseif g_game.isAttacking() then
  150. local target = g_game.getAttackingCreature()
  151. if not target or not AutoTarget.isValidTarget(target) then
  152. g_game.cancelAttackAndFollow()
  153. else
  154. return Helper.safeDelay(600, 2000)
  155. end
  156. end
  157.  
  158. -- Find a valid target to attack
  159. local target = AutoTarget.getBestTarget()
  160. if target then
  161. -- If looting pause to prioritize targeting
  162. if AutoLoot.isLooting() then
  163. AutoLoot.pauseLooting()
  164. end
  165.  
  166. AutoTarget.checkChaseMode(target)
  167. AutoTarget.checkStance(target)
  168.  
  169. g_game.attack(target)
  170. end
  171.  
  172. -- Keep the event live
  173. return Helper.safeDelay(600, 1400)
  174. end
Advertisement
Add Comment
Please, Sign In to add comment