Turn.inc - Human style turning (Data/DataVars Compatible)

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

GD
a snow griffon
a snow griffon
Posts: 353
Joined: Sat Jun 29, 2002 11:57 pm

Turn.inc - Human style turning (Data/DataVars Compatible)

Post by GD » Sun May 02, 2004 5:51 am

Code: Select all

| Turn.inc -  Created by GD
|
| MQ2Data mod by Icehouse33
|	  26 April 2004
|
| MQ2DataVars mod by GD
|	  2 May 2004
|
| Usage: #Include it into your current macro, then call it as follows:
|
|	  To turn towards Target: /call Turn
|	  To turn towards Location: /call Turn LocY LocX
|		  IE: /call Turn -460 500

#turbo

Sub Turn(THeadingY,THeadingX)
	/declare PHeading outer 0
	/declare THeading outer 0
	/declare Bearing outer 0
	/declare IsTurning outer 0
	/declare TurnKey outer NULL

:TurnLoop
	/delay 0
	/if (${Me.Sitting}) /sit off
	/varset PHeading ${Me.Heading.Degrees}
	/if (${Defined[THeadingX]}) {
		/varset THeading ${Heading[${THeadingY},${THeadingX}].Degrees}
	} else {
		/varset THeading ${Target.HeadingTo.Degrees}
	}

	/varset Bearing ${Math.Calc[(${PHeading}-${THeading}+540)%360-180]}

	/if (${Bearing}>=-7 && ${Bearing}<=7) {
		/if (${String[${TurnKey}].NotEqual[NULL]}) /keypress ${TurnKey}
		/face heading ${Heading[${THeading}].Degrees} nolook
		/return
	} else /if (${Bearing}>=-180 && ${Bearing}>=7) {
		/if (${String[${TurnKey}].NotEqual[LEFT]}) /varset TurnKey LEFT
	} else /if (${Bearing}<=180 && ${Bearing}<=7) {
		/if (${String[${TurnKey}].NotEqual[RIGHT]}) /varset TurnKey RIGHT
	}

	/if (!${IsTurning}) {
		/keypress ${TurnKey} hold
		/varset IsTurning 1
	}
	/goto :TurnLoop

/return

Opinions are like assholes, everyone has one, but most of them stink.

User avatar
blueninja
a grimling bloodguard
a grimling bloodguard
Posts: 541
Joined: Thu Aug 28, 2003 7:03 am
Location: Göteborg, Sweden

Post by blueninja » Sun May 09, 2004 6:26 pm

Shouldn't those variable definitions be like this:

Code: Select all

   /declare PHeading int local 0
   /declare THeading int local 0
   /declare Bearing int local 0
   /declare IsTurning bool local 0
   /declare TurnKey local NULL
Probably don't have to assign null as the default value but I guess it won't hurt :) .

EDIT: Bah typo..
EDIT2: Looks like you do need to assign NULL to TurnKey after all..

mpmq
a lesser mummy
a lesser mummy
Posts: 30
Joined: Thu Mar 18, 2004 8:07 pm

Post by mpmq » Sat May 15, 2004 9:26 am

Been playing with this in one of my macros, and once in a while it will spin me 360 once or twice before turning me to the proper heading. I'm using it with the "turn to target" style call. Last time it messed up, the target in question was nearly standing next to me (not moving). Not sure if that has anything to do with it, since I didn't bother to try and check where the targets were when it happened before.

sumdumgeek
decaying skeleton
decaying skeleton
Posts: 4
Joined: Fri Mar 26, 2004 1:09 am

Post by sumdumgeek » Sun May 23, 2004 8:21 am

I suspect that the reason you sometimes do 360s is that the pressed key never gets released until your target is in the -7..7 range. If you overshoot, it'll press the other arrow, but it doesn't release the first one. What I did to the non-Datavars version was in the else /if sections, where it's setting TurnKey, was to make it /keypress the old key before setting the new one if they were different.

It's probably not a big deal in terms of wasted cycles, but the tests against -180 and 180 in the else /if blocks are redundant. If Bearing is >= 7, it's >-180. (I also set these to be testing for >0 and <0, to make it more obvious that it's a left/right check. It won't get there if the "in front of me" test passes anyway.)