View Single Post
  #1  
Old 04-20-2019, 02:05 AM
t0neg0d
Fire Beetle
 
Join Date: Oct 2014
Posts: 22
Default Heading (LookAt x/y) and a bit more...

Couple functions and their usage...

The first (GetRandomPointAround) will get a random location around the destination coords and at the given min/max radius.

The second (LookAt) will return the correct heading to face any $mob towards a given x/y loc.

SomePlugin.pl
Code:
# PARAMS: x coord, y coord, min radius, max radius
sub GetRandomPointAround {
	my $x = shift;
	my $y = shift;
	my $minradius = shift;
	my $maxradius = shift;
	
	my $angle = rand(360);
	my $radius = $minradius+rand($maxradius-$minradius);
	
	my $ax = ($radius * sin($angle)) + $x;
	my $ay = ($radius * cos($angle)) + $y;
	
	my @ret = ($ax,$ay);
	return @ret;
}

# PARAMS: origin mob, lookat x coord, lookat y coord
sub LookAt {
	my $origin = shift;
	my $x2 = shift;
	my $y2 = shift;
	
	my $laa = atan2(($origin->GetX()-$x2),($origin->GetY()-$y2));
	$laa *= 180;
	$laa /= 3.14;
	$laa /= 360;
	$laa *= 512;
	$laa -= 256;
	
	return $laa;

	# Or comment out above from 'my $laa = ' and use:
#	return ((((atan2(($origin->GetX()-$x2),($origin->GetY()-$y2))*180)/3.14)/360)*512)-256;
}
Test case: SomeNPC.pl
Code:
my @baseloc = (1221.84, -530.34, 69.41, 172);
my @nextloc;

sub EVENT_SAY {
	if ($text=~/Hail/i) {
		@nextloc = ( plugin::GetRandomPointAround($baseloc[0],$baseloc[1],10,14) );
		
		quest::moveto(
			$nextloc[0],
			$nextloc[1],
			$baseloc[2]
		);
	}
}

sub EVENT_WAYPOINT_ARRIVE {
	$npc->SaveGuardSpot(
		$nextloc[0],
		$nextloc[1],
		$baseloc[2],
		plugin::LookAt($npc,$baseloc[0],$baseloc[1])
	);
}
Reply With Quote