;------------------------
; Buckna's Chicken Killer
; Bone's disabled
; Lumbridge
; Remove all friends
; (like you had any)
; And block everything



SetOnPlaceTime(600)
OnLogin(@Init)
@Init:
EnableEvents()
; Set up some events
OnCoordsChanged(@Moving,%NewX,%NewY)
OnPrivateMessage(@Log)
OnChatMessage(@Log)
OnStatsChanged(@StatsChange,%Stat)
OnServerMessage(@ServMes)
OnItemAppeared(@NewItem,%ItemID,%X,%Y)
SetVarMyX(%NewX)
SetVarMyY(%NewY)

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


@MainLoop:
GoToIfCoordsIn(@MoveBack,119,607,122,612)
GoToIfNPCNearIn(@IntoHouse,119,607,122,612,3)
; Don't attack if we are already fighting
GoToIfVarEqualNum(@BlahBlah,%FightOver,0)
GoToIfFighting(@WeAreInFight)
@BlahBlah:
GoToIfVarEqualNum(@DontAttack,%FightOver,0)
; Attack if any enemies visible
GoToIfNPCNear(@Attack,3)
; 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,3)


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


@DontAttack:
Wait(5,6)

; 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")
---------
GoToIfLastServerMessageIs(@Sleep,"@gre@You are too tired to gain experience, get some rest!")
---------
ERet()

@Sleep:
SetVarNum(%nig,100)
UseItem(0)
GoTo(@Rest)

@Rest:
Wait(5)
FightMode(0)
AddVarNum(%nig,-1)
GoToIfVarEqualNum(@Done,%nig,0)
GoToIfLastServerMessageIs(@Done,"You wake up - feeling refreshed")
GoTo(@Rest)

@Done:
FightMode(0)
ResetLastServerMessage()
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,381)
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,381)
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()
-
@MoveBack2:
MoveTo(122,606)
Wait(10)
ERet()

@MoveBack3:
MoveTo(114,608)
Wait(2)
Action(114,608)
AtObject(114,608)
Wait(10)
MoveTo(116,608)
ERet()

;---Move Back
@MoveBack:
GoToIfNPCNearIn(@Attack,119,607,122,612,3)
MoveTo(119,610)
Wait(5)
MoveTo(118,610)
MoveTo(118,606)
GoTo(@MainLoop)

;----Into House
@IntoHouse:
MoveTo(118,606)
MoveTo(118,610)
Wait(5)
MoveTo(119,608)
GoTo(@MainLoop)

@Log:
DebugVar(%NewX,%NewY)
DebugLastChatMessage()
Debug("OMG, had to log - Edited by Buckna")
SaveScreenshot()
Wait(1)
StopAndLogOut()
ERet()

@Moving:
SetVarMyX(%NewX)
SetVarMyY(%NewY)
GoToIfCoordsIn(@MoveBack3,114,604,112,611)
GoToIfCoordsIn(@MoveBack2,122,604,122,603)
GoToIfVarAboveNum(@Log,%NewX,130)
GoToIfVarBelowNum(@Log,%NewX,110)
GoToIfVarAboveNum(@Log,%NewY,615)
GoToIfVarBelowNum(@Log,%NewY,595)
ERet()
