Jump to content

Welcome to Gaming On Steroids Forums
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account
Photo

My IsFacing() implementation, if anyone wants to use it


  • Please log in to reply
1 reply to this topic

#1
Bananaraka

Bananaraka

    Member

  • Members
  • 18 posts

Writing a Cassiopeia script, and am including an option to 'only ult enemies facing you.'

 

Since the API doesn't seem to have such a function, here's an implementation:

function MyScript:IsFacing(target)
	-- make sure directions are facing opposite:
	local dotProduct = myHero.dir.x*target.dir.x + myHero.dir.z*target.dir.z
	if (dotProduct < 0) then
		-- also make sure you dont have your backs to each other:
		if (myHero.dir.x > 0 and myHero.dir.z > 0) then
			return ((target.pos.x - myHero.pos.x > 0) and (target.pos.z - myHero.pos.z > 0))
		elseif (myHero.dir.x < 0 and myHero.dir.z < 0) then
			return ((target.pos.x - myHero.pos.x < 0) and (target.pos.z - myHero.pos.z < 0))
		elseif (myHero.dir.x > 0 and myHero.dir.z < 0) then
			return ((target.pos.x - myHero.pos.x > 0) and (target.pos.z - myHero.pos.z < 0))
		elseif (myHero.dir.x < 0 and myHero.dir.z > 0) then
			return ((target.pos.x - myHero.pos.x < 0) and (target.pos.z - myHero.pos.z > 0))
		end
	end
	return false
end

  • 1

#2
Feretorix

Feretorix

    Administrator

  • Administrators
  • 3,248 posts

Hello, to improve the function i would suggest storing the dir and pos of the target and yourself in a local variable at the beginning of the function, the rest should be the same.

 

The thing is that each time you call a member from the class (like myHero.dir.x), you call a function in the core which takes extra processing time (even if it's 0.001 it's still something).

 

Thank you for the contribution.


  • 0




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users