Page 1 of 2

New LUA AI Functions

Posted: Tue Aug 27, 2013 11:05 pm
by Jabantiz
A lot of AI related functions have now been exposed thanks mostly to thefoof, here is the list of all new lua functions added over the past 2 nights


SetTarget(Spawn, Spawn)
IsPet(Spawn)
GetOwner(Spawn)
SetInCombat(Spawn, bool)
CompareSpawns(Spawn, Spawn)
Runback(Spawn)
GetRunbackDistance(Spawn)
IsCasting(Spawn)
IsMezzed(Spawn)
IsStunned(Spawn)
IsMezzedOrStunned(Spawn)
ClearEncounter(Spawn)
ClearHate(Spawn, [Spawn])
GetMostHated(Spawn)
GetEncounterSize(Spawn)
HasRecovered(Spawn)
ProcessMelee(Spawn, Spawn, float)
ProcessSpell(Spawn, Spawn, float)
GetEncounter(Spawn)
GetHateList(Spawn)
HasGroup(Spawn)


The following spawn script will use lua AI that will duplicate what we have in the server core.

Code: Select all

local MAX_CHASE_DISTANCE = 80

function Think(Spawn)
	-- Make sure the NPC is not mezzed or stunned
	if not IsMezzedOrStunned(Spawn) then
		-- Get the most hated spanw
		local Target = GetMostHated(Spawn)
		
		-- Get runback distance
		local runback_distance = GetRunbackDistance(Spawn)
		-- Check to see if we got a target
		if Target ~= nil then
			
			if GetTarget(Spawn) == nil or not CompareSpawns(Target, GetTarget(Spawn)) then
				SetTarget(Spawn, Target)
				FaceTarget(Spawn, Target)
			end
			
			if not IsInCombat(Spawn) then
				SetInCombat(Spawn, true)
			end
			
			-- Check distance to run back location
			if runback_distance > MAX_CHASE_DISTANCE then
				-- Clear the hate list
				ClearHate(Spawn)
				-- Clear the encounter list
				ClearEncounter(Spawn)
			else
				local distance = GetDistance(Spawn, GetTarget(Spawn), 1)
				
				if not IsCasting(Spawn) and (not HasRecovered(Spawn) or not ProcessSpell(Spawn, Target, distance)) then
					FaceTarget(Spawn, Target)
					ProcessMelee(Spawn, Target, distance)
				end
			end
		else
			-- nothing in hate list
			
			-- check to see if the NPC is still flagged in combat
			if IsInCombat(Spawn) then
				SetInCombat(Spawn, false)
				if not IsPet(Spawn) or not IsPlayer(GetOwner(Spawn)) then
					SetHP(Spawn, GetMaxHP(Spawn))
				end
			end
			
			if runback_distance > 0 then
				Runback(Spawn)
			end
			
			if GetEncounterSize(Spawn) > 0 then
				ClearEncounter(Spawn)
			end
			
		end
	end		
		
end

function spawn(NPC)
	SetLuaBrain(NPC)
	SetBrainTick(NPC, 200)
end

function respawn(NPC)
	spawn(NPC)
end

function hailed(NPC, Player)
	
	local target = GetTarget(Player)
	if CompareSpawns(NPC, target) then
		Say(NPC, "Same")
	else
		Say(NPC, "Not the same")
	end
end
wiki pages will come over the next few days

Re: New LUA AI Functions

Posted: Wed Aug 28, 2013 8:45 am
by John Adams
Holy crap! ~screams like a Beatles girl~

Impressive list. I suppose Alfa just got an early Christmas present ;)

Re: New LUA AI Functions

Posted: Wed Aug 28, 2013 10:38 am
by alfa
John Adams wrote:Holy crap! ~screams like a Beatles girl~

Impressive list. I suppose Alfa just got an early Christmas present ;)
Jab PM me this yesterday, start some cool things like basic Warrior, Priest, Scout, Mage script... Before start some boss scrit :)
More to come soon ^^

Re: New LUA AI Functions

Posted: Wed Aug 28, 2013 9:44 pm
by Jabantiz
All lua functions in the original post have been updated to link to their wiki page.

A lot of copy and paste was used so there might be a few errors...

Re: New LUA AI Functions

Posted: Thu Aug 29, 2013 7:01 am
by John Adams
That's a ton of Wiki-work. Thank you for doing that.

Re: New LUA AI Functions

Posted: Thu Aug 29, 2013 4:16 pm
by alfa
Thanks Jab ;) Admit you just do Wiki Page for stopping me spam you for explain about it :p
And quick question, is GetHateList() return Player in order or randomly ? I know there is GetMostHated() but only return #1 in hate list and if I need the #2 :p

Re: New LUA AI Functions

Posted: Thu Aug 29, 2013 10:06 pm
by Jabantiz
Updated the AI wiki page and added the lua page.

I accidently clicked a link while editing the lua page and lost a lot of my work so the last portion may seem half assed and that is probably because it is from trying to remember and retype it all out. Hopefully the page will be useful to some of you though.

Re: New LUA AI Functions

Posted: Sun Sep 01, 2013 12:52 pm
by John Adams
Jabantiz wrote:I accidently clicked a link while editing the lua page and lost a lot of my work
I cannot tell you how many times I've done that same thing. That sucks, man.

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 5:36 pm
by John Adams
Btw, all 3 linux services fail to compile because of Brain.

Code: Select all

In file included from ../common/../common/../WorldServer/Combat.h:24,
                 from ../common/../common/../WorldServer/zoneserver.h:34,
                 from ../common/../common/../WorldServer/client.h:26,
                 from ../common/../common/Log.h:24,
                 from ../common/Mutex.cpp:22:
../common/../common/../WorldServer/NPC_AI.h:75: error: extra qualification ‘Brain::’ on member ‘GetHateList’
In file included from ../common/../common/../WorldServer/Combat.h:24,
                 from ../common/../common/../WorldServer/zoneserver.h:34,
                 from ../common/../common/../WorldServer/client.h:26,
                 from ../common/../common/Log.h:24,
                 from ../common/MiscFunctions.cpp:21:
../common/../common/../WorldServer/NPC_AI.h:75: error: extra qualification ‘Brain::’ on member ‘GetHateList’
No idea why NPC_AI is affecting Login and Patcher.

Commit 2217 is where it breaks. The addition of vector<Entity*>* Brain::GetHateList(); in NPC_AI.h

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 5:53 pm
by John Adams
Oh, duh?

Code: Select all

vector<Entity*>* Brain::GetHateList();
should be

Code: Select all

vector<Entity*>* GetHateList();
?

Still boggles me why this is touching common code.

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 5:53 pm
by Jabantiz
My bad

Code: Select all

vector<Entity*>* Brain::GetHateList();
NPC_AI.h, forgot to take out the brain:: from a copy and paste....

Will commit to SVN in a sec

EDIT: And john found my issue, still will commit
EDIT2: Fix on svn

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 6:02 pm
by John Adams
haha JABANTIZ!!

Code: Select all

Entity.cpp:979: error: no matching function for call to ‘max(float, double)’
lol you min/max'd me again :mrgreen:

You really need a linux box :)

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 6:09 pm
by thefoof
John Adams wrote:haha JABANTIZ!!

Code: Select all

Entity.cpp:979: error: no matching function for call to ‘max(float, double)’
lol you min/max'd me again :mrgreen:

You really need a linux box :)
Lol that one was probably me.

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 6:10 pm
by Jabantiz

Code: Select all

float Entity::CalculateCastingSpeedMod() {
	float cast_speed = info_struct.casting_speed;
	
	if(cast_speed > 0)
		return 100 * max((float) 0.5, 1 + (1 - (1 / (1 + (cast_speed * .01)))));
	else if (cast_speed < 0)
		return 100 * min((float) 1.5, 1 + (1 - (1 / (1 + (cast_speed * -.01)))));
	return 0;
}
WOOT not me this time. I will add the float to the second portion of it though and commit

EDIT: Fix on dev svn

Re: New LUA AI Functions

Posted: Tue Sep 03, 2013 6:11 pm
by John Adams
lol you guys are aging me faster than normal.