Mob looking/heading angle relative to me

A forum for macro code snippets to be used in writing other macros. Post routines or .inc files here only, completed macros go to the Macro Depot. MQ2Data format only!

Moderator: MacroQuest Developers

Vayeco Dynn
a ghoul
a ghoul
Posts: 113
Joined: Wed Aug 25, 2004 3:00 am

Mob looking/heading angle relative to me

Post by Vayeco Dynn » Thu Jul 03, 2014 8:26 pm

Want to know whether or not a mob is looking at you? Headed in your direction? Headed away from you? This collection of subs will tell you what angle the mob is looking at relative to you, with 0.00 being directly at you and 180.00 being directly away from you. There's a full 0-360 degree version and a normalized 0-180 degree version (a little more practical for most applications). Hint: 90 degrees means they're looking perpendicular to you, not quite at you or away from you.

Code: Select all

#turbo

Sub Main

   /call MobLookNormalized ${Target.ID}

/return



|--------------------------------------------------------------------------------
|SUB: Angle from 2 points
|--------------------------------------------------------------------------------
Sub SlopeToAngle(float P1X, float P1Y, float P2X, float P2Y)

	/declare Denominator float
	
	/varcalc Denominator ${P2X} - ${P1X}
	
	/if ( ${Math.Abs[${Denominator}]} < 0.01 ) {
	
		/return 0
	
	}
	
	/return ${Math.Atan[${Math.Calc[(${P2Y}-${P1Y})/(${P2X}-${P1X})]}]}

/return

|--------------------------------------------------------------------------------
|SUB: Angle a mob is looking relative to Me
|--------------------------------------------------------------------------------
Sub MobLook(float SpawnID)

	/call SlopeToAngle ${Me.Y} ${Me.X} ${Spawn[${SpawnID}].Y} ${Spawn[${SpawnID}].X} 
	/declare MobAngle float ${Macro.Return}
	
	/if ( ${Math.Calc[${Me.Y} - ${Spawn[${SpawnID}].Y}]} < 0 ) /varcalc MobAngle ${MobAngle} + 180
	
	/if ( ${MobAngle} < 0 ) /varcalc MobAngle ${MobAngle} + 360
	
	/declare Result float ${Math.Abs[${Math.Calc[${MobAngle} - ${Spawn[${SpawnID}].Heading.DegreesCCW}]}]}
	
	/echo Mob is looking: ${Result} relative to Me (${MobAngle} vs ${Spawn[${SpawnID}].Heading.DegreesCCW})
	/return ${Result}

/return

|--------------------------------------------------------------------------------
|SUB: Angle a mob is looking relative to Me (normalized from 0-180)
|--------------------------------------------------------------------------------
Sub MobLookNormalized(float SpawnID)

	/call MobLook ${SpawnID}
	/declare Result float ${Macro.Return}
	/if ( ${Result} > 180 ) {
	
		/varcalc Result 360 - ${Result}
	
	}
	
	/echo Mob is looking: ${Result} relative to Me (normalized)
	/return ${Result}
	
/return