[GWA2] Get angle of agent relative to player

06/13/2018 06:12 3vangelist#1
Hi all,

Wanted to create a set of functions to figure out (eventually) if an agent is in front, behind, left or right of the player based on rotation - wanted to speed up runs a bit by using Heart of Shadow if we're running away from an enemy towards a location.

I know that the player's rotation is from 0 = East to 3 = West, and that some mathematical witchcraft needs to be done based on the target Agent's position relative to the player's position to figure this out. May want to open it up to use MoveX and MoveY instead (player may be running somewhere backwards just after casting)

Has anyone already written code to figure this out?
06/13/2018 10:49 DerMoench14#2
Well ... i wrote this some years ago for my private uwsc bot.
Can also be used in vaetir bot ...

Code:
;~ Description: Returns best Target to hos from for get best Direction to next Coordinates
Func GetGoodAngle($lDestX, $lDestY)
	SendChat("stuck", "/")
	RndSleep(100)
	Local $lTargetX, $lTargetY, $lBestHosTarget = -1, $lBestHosAngle = 360, $lPI = 3.14159
	Local $lMeX = MemoryRead($MePtr + 116, 'float')
	Local $lMeY = MemoryRead($MePtr + 120, 'float')
	If ComputeDistance($lDestX, $lDestY, $lMeX, $lMeY) < 200 Then Return -3										; We are too close to our Destination Coords, so take next Coords
	Local $lWantedAngle = _ATan2(($lDestY - $lMeY), ($lDestX - $lMeX)) * 180 / $lPI
	;Out("WANTED:   " & Int($lWantedAngle))
	Local $aAgentArray = GetAgentPtrArray2(0xDB, 3, True)
	For $i = 1 to $aAgentArray[0]
		If GetDistance($MePtr, $aAgentArray[$i]) > 1150 Then ContinueLoop
		$lTargetX = MemoryRead($aAgentArray[$i] + 116, 'float')
		$lTargetY = MemoryRead($aAgentArray[$i] + 120, 'float')
		$lTargetAngle = _ATan2(($lMeY - $lTargetY), ($lMeX - $lTargetX)) * 180 / $lPI
		;Out("NOW:   " & int($lTargetAngle))
		If Abs($lWantedAngle - $lTargetAngle) < $lBestHosAngle Then
			$lBestHosTarget = $aAgentArray[$i]
			$lBestHosAngle = Abs($lWantedAngle - $lTargetAngle)
			;Out("BestTempAngle:   " & Int($lTargetAngle))
		EndIf
	Next
	;Out("BESTDIFF:   " & Int($lBestHosAngle))
	Return $lBestHosTarget
EndFunc

Func _ATan2($y, $x)
	$lPI = 3.14159
	Select
		Case $x > 0
			Return ATan($y / $x)
		Case $x < 0 And $y >= 0
			Return ATan($y / $x) + $lPI
		Case $x < 0 And $y < 0
			Return ATan($y / $x) - $lPI
		Case $x = 0 And $y > 0
			Return $lPI / 2
		Case $x = 0 And $y < 0
			Return -$lPI / 2
		Case $x = 0 And $y = 0
			Return  @ Error
	EndSelect
EndFunc
You'll need to edit this to your needs.

Edit: Delete the space between "@" and "Error" in last Return Case.
How the fu** can i disable the shit as this Board interprets it as an internal link without the space?
06/13/2018 11:03 3vangelist#3
Quote:
Originally Posted by DerMoench14 View Post
Well ... i wrote this some years ago for my private uwsc bot.
Can also be used in vaetir bot ...

Code:
;~ Description: Returns best Target to hos from for get best Direction to next Coordinates
Func GetGoodAngle($lDestX, $lDestY)
	SendChat("stuck", "/")
	RndSleep(100)
	Local $lTargetX, $lTargetY, $lBestHosTarget = -1, $lBestHosAngle = 360, $lPI = 3.14159
	Local $lMeX = MemoryRead($MePtr + 116, 'float')
	Local $lMeY = MemoryRead($MePtr + 120, 'float')
	If ComputeDistance($lDestX, $lDestY, $lMeX, $lMeY) < 200 Then Return -3										; We are too close to our Destination Coords, so take next Coords
	Local $lWantedAngle = _ATan2(($lDestY - $lMeY), ($lDestX - $lMeX)) * 180 / $lPI
	;Out("WANTED:   " & Int($lWantedAngle))
	Local $aAgentArray = GetAgentPtrArray2(0xDB, 3, True)
	For $i = 1 to $aAgentArray[0]
		If GetDistance($MePtr, $aAgentArray[$i]) > 1150 Then ContinueLoop
		$lTargetX = MemoryRead($aAgentArray[$i] + 116, 'float')
		$lTargetY = MemoryRead($aAgentArray[$i] + 120, 'float')
		$lTargetAngle = _ATan2(($lMeY - $lTargetY), ($lMeX - $lTargetX)) * 180 / $lPI
		;Out("NOW:   " & int($lTargetAngle))
		If Abs($lWantedAngle - $lTargetAngle) < $lBestHosAngle Then
			$lBestHosTarget = $aAgentArray[$i]
			$lBestHosAngle = Abs($lWantedAngle - $lTargetAngle)
			;Out("BestTempAngle:   " & Int($lTargetAngle))
		EndIf
	Next
	;Out("BESTDIFF:   " & Int($lBestHosAngle))
	Return $lBestHosTarget
EndFunc

Func _ATan2($y, $x)
	$lPI = 3.14159
	Select
		Case $x > 0
			Return ATan($y / $x)
		Case $x < 0 And $y >= 0
			Return ATan($y / $x) + $lPI
		Case $x < 0 And $y < 0
			Return ATan($y / $x) - $lPI
		Case $x = 0 And $y > 0
			Return $lPI / 2
		Case $x = 0 And $y < 0
			Return -$lPI / 2
		Case $x = 0 And $y = 0
			Return  @ Error
	EndSelect
EndFunc
You'll need to edit this to your needs.

Edit: Delete the space between "@" and "Error" in last Return Case.
How the fu** can i disable the shit as this Board interprets it as an internal link without the space?
This is great, thankyou - looks like I can also pull out a good candidate for a GetDirection() function from this, too :)