;---------------
; Easy Auto-Fight
; Auto-Generated by AutoRune
; Created by Kaitnieks
; For fighting NPCs type 65
; With fight mode 1
; Don't attack if HP below 5


SetOnPlaceTime(600)
OnLogin(@Init)
@Init:
EnableEvents()
; Set up some events
OnStatsChanged(@StatsChange,%Stat)
OnServerMessage(@ServMes)
OnItemAppeared(@NewItem,%ItemID,%X,%Y)

; Set it to train strength or whatever
FightMode(1)
; Ignore those NPCs that are in fight
CheckFighters(1)
; Let AR know, that we are not in fight
SetVarNum(%FightOver,1)


@MainLoop:
; Don't attack if we are already fighting
GoToIfVarEqualNum(@DontAttack,%FightOver,0)
; Attack if any enemies visible
GoToIfNPCNear(@Attack,65)
; Otherwise don;t attack
GoTo(@DontAttack)


@Attack:
; Attack only if my HP is above certain number
; or unknown
SetVarMyHP(%HP)
GoToIfVarEqualNum(@Attack2,%HP,0)
GoToIfVarBelowNum(@DontAttack,%HP,5)


@Attack2:
; get coords and ID of nearest enemy
SetVarsNearestNPC(%x,%y,%ID,65)
; attack the enemy
ActionVarVar(%x,%y)
AttackNPCVar(%ID)
; let AR know that we are in fight now
SetVarNum(%FightOver,0)
; reset idle counter
SetVarNum(%IdleC,0)


@DontAttack:
Wait(1)

; count how many loops we are idling
AddVarNum(%IdleC,1)
; if more than 20 loops - attack next enemy
GoToIfVarAboveNum(@FightNext,%IdleC,20)
; if more than 4 loops - burry all bones we have
GoToIfVarAboveNum(@IHaveBones,%IdleC,4)
GoTo(@MainLoop)


@FightNext:
; tell AR that we are not fighting
SetVarNum(%FightOver,1)
; reset idle counter
SetVarNum(%IdleC,0)
GoTo(@MainLoop)


@IHaveBones:
; don't dig bones if we are fighting
GoToIfVarEqualNum(@NotWorthToDig,%FightOver,0)
; dig only if I do have bones
GoToIfInInventory(@IReallyHaveBones,20)
GoTo(@NotWorthToDig)
@IReallyHaveBones:
GoSub(@DigBones)
@NotWorthToDig:
GoTo(@MainLoop)


;--- OnStatChaged
@StatsChange:
@CanMove:
;If my HitPoints skill changed, it means
;fight is over
GoToIfVarEqualNum(@SetCanMove,%Stat,3)
ERet()
@SetCanMove:
SetVarNum(%FightOver,1)
SetVarNum(%IdleC,0)
ERet()


;--- OnServerMessage
@ServMes:
;-- If someone stole your enemy
GoToIfLastServerMessageIs(@ResetGo,"I can't get close enough")
;-- or your victim runs away
GoToIfLastServerMessageIs(@ResetGo,"Your opponent is retreating")
ERet()
;-- then let AR know that fight is over
@ResetGo:
SetVarNum(%FightOver,1)
ERet()


;--- New item appeared
@NewItem:
;-- If we're not fighting or burying bones
; then take the item
GoToIfVarEqualNum(@TakeItems,%FightOver,1)
ERet()
@TakeItems:
SetVarMyX(%MX)
SetVarMyY(%MY)
;Only take the item if it's right under my feet
GoToIfVarNotEqual(@DontGrabAnything,%X,%MX)
GoToIfVarNotEqual(@DontGrabAnything,%Y,%MY)
;-- Only take certain items
GoToIfVarNotEqualNum(@DontGrabBones,%ItemID,20)
-GoTo(@GrabBones)
@DontGrabBones:
GoToIfVarNotEqualNum(@DontGrabCoins,%ItemID,10)
-GoTo(@GrabCoins)
@DontGrabCoins:
@DontGrabAnything:
ERet()



;---Pick up bones
@GrabBones:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,20)
Wait(10)
GoTo(@DontGrabBones)

;---Pick up coins
@GrabCoins:
ActionVarVar(%X,%Y)
TakeItemVarVar(%X,%Y,10)
Wait(10)
GoTo(@DontGrabCoins)



;--- Dig all bones
@DigBones:
; Don't dig if we are in fight
GoToIfVarEqualNum(@DontDig,%FightOver,0)
@DigBonesLoop:
UseItemByID(20)
Wait(20)
; if we still have bones, keep digging
GoToIfInInventory(@DigBonesLoop,20)
@DontDig:
Ret()
-

