Page 1 of 1

Detecting a specific AI in the game

Posted: 13 Sep 2016, 02:13
by Forboding Angel
Is there a nice simple way to detect if a specific AI is in the game?

Re: Detecting a specific AI in the game

Posted: 14 Sep 2016, 02:18
by Forboding Angel
So I decided on Spring.GetAIInfo (https://springrts.com/wiki/Lua_SyncedRead (thanks hoko!))

In each gadget for chickens I set up this craptastic loop...

Code: Select all

for i = 0,64 do
	local _, _, _, chicken = Spring.GetAIInfo(i)
	Spring.Echo(chicken)
	if chicken == "Chicken: Very Easy" or 
	chicken == "Chicken: Easy" or 
	chicken == "Chicken: Normal" or 
	chicken == "Chicken: Hard" or 
	chicken == "Chicken: Very Hard" or 
	chicken == "Chicken: Epic!" or 
	chicken == "Chicken: Custom" or 
	chicken == "Chicken: Survival" then
		chickensEnabled = true
	end
end

if chickensEnabled == true then
	Spring.Echo("[Chickens] Activated!")
else
	Spring.Echo("[Chickens] Deactivated!")
	return false
end
"But Forb... Wtf are you checking for the full names of the AIs???"

Well that's a great question, I'm glad you asked. Because I can't find a SINGLE REFERENCE in the wiki on how to set luaAI shortnames.
This was about as close as I got: https://springrts.com/wiki/AI:Development:Lang:Lua

Of course I tried this:

Code: Select all

  {
    name = 'Chicken: Very Easy',
    shortName = 'chickens',
    desc = 'Trivial Games'
  },
And ofc it doesn't do jack.

"But forb, why does any of this matter?"

Another even more excellent question... Because for reasons, this loop appears to run twice. If I echo "chicken" in infolog, I get 2 sets of results:

First set:
[f=-000001] nil
[f=-000001] SYNCED_NOSHORTNAME
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] [Chickens] Deactivated!
And the second Set:
[f=-000001] nil
[f=-000001] Chicken: Very Easy
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] nil
[f=-000001] [Chickens] Activated!
Because of the first set of results, the gadget disables itself and that's GG for the second set. Obviously the first set reports that a shortname isn't set, so logic leads me to assume that if I can set a shortname, the first set of results shows the actual shortname. HA, spoiler alert, there doesn't seem to be any way to set a shortname because the engine apparently likes to use name for shortname. This is a dumb problem to have.

Can someone please help me with this?

Re: Detecting a specific AI in the game

Posted: 14 Sep 2016, 02:36
by Forboding Angel
For anyone that cares, it occurred to me that I was already using "name" even if it is the engine overriding shortname, so I switched up my loop to use name instead and it works because the first set actually returns the proper name.

Imo this qualifies as an engine bug. Having the engine automatically assign the shortname as name is stupid.

Code: Select all

for i = 0,64 do
	local _, chicken = Spring.GetAIInfo(i)
	--Spring.Echo(chicken)
	if chicken == "AI: Chicken: Very Easy" or 
	chicken == "AI: Chicken: Easy" or 
	chicken == "AI: Chicken: Normal" or 
	chicken == "AI: Chicken: Hard" or 
	chicken == "AI: Chicken: Very Hard" or 
	chicken == "AI: Chicken: Epic!" or 
	chicken == "AI: Chicken: Custom" or 
	chicken == "AI: Chicken: Survival" then
		chickensEnabled = true
	end
end

if chickensEnabled == true then
	Spring.Echo("[Chickens] Activated!")
else
	Spring.Echo("[Chickens] Deactivated!")
	return false
end

Re: Detecting a specific AI in the game

Posted: 14 Sep 2016, 02:40
by sprunk
Two sets:

Code: Select all

if (gadgetHandler:IsSyncedCode()) then return end
Therere is no "short" name, what you're getting is the shortest you can get (the other available name is just the bot's nick). LuaAIs have to be packed inside the mod anyway so you always know your entire full set of AIs -- just parse for "chicken" manually.

Re: Detecting a specific AI in the game

Posted: 14 Sep 2016, 03:16
by Forboding Angel
Imo, "name" shoudl be changed to botName to signify the bots player name, and name shoutl be the name of the ai and shortname should be a shortname of the AI.

This is solved btw. Big thanks to sprunk/sprung for helping me with a better loop:

Code: Select all

local teams = Spring.GetTeamList()
for i =1, #teams do
	local luaAI = Spring.GetTeamLuaAI(teams[i])
	if luaAI ~= "" then
		if luaAI == "Chicken: Very Easy" or 
		luaAI == "Chicken: Easy" or 
		luaAI == "Chicken: Normal" or 
		luaAI == "Chicken: Hard" or 
		luaAI == "Chicken: Very Hard" or 
		luaAI == "Chicken: Epic!" or 
		luaAI == "Chicken: Custom" or 
		luaAI == "Chicken: Survival" then
			chickensEnabled = true
		end
	end
end

if chickensEnabled == true then
	Spring.Echo("[ChickenDefense: Chicken Defense Spawner] Activated!")
else
	Spring.Echo("[ChickenDefense: Chicken Defense Spawner] Deactivated!")
	return false
end

Chickens march onwards!