Instantly remove units

Instantly remove units

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Instantly remove units

Post by allanmc »

Hi,

To do numerous battle simulations in a row, we use the command COMMAND_UNIT_SELF_DESTROY to destroy surviving units from the previous battle, in order to be ready to simulate the next battle.

To initiate the next battle, we can spawn new units instantly using the command COMMAND_CHEATS_GIVE_ME_NEW_UNIT. However, since there is a looong wait before the COMMAND_UNIT_SELF_DESTROY command actually makes the units suicide, we have to wait a long time before we can initiate the new battle.

Is there any possibility to instantly remove/suicide/destroy/kill/vanish a unit?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Instantly remove units

Post by AF »

This is lua gadget land

It would be faster to run headless spring at uber high gamespeed, and simply exit the game and restart. This would have the benefit of avoiding problems such as changed terrain, or not being able to 'reset' the state of lua game logic
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: Instantly remove units

Post by hoijui »

it should be possible, but will take some work to implement.

please tell us about your AI please!
its it open source, can we see it? are you writing a paper?

as they are already doing it this way, i guess they sorted out the other problems already, eg. using undeformable map and using BA (which will may have little Lua state problems. and of course insta-killing all units and reading commanders is much faster then restarting spring.
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

Well, our very-much work-in-progress AI is actually public, but it is not at all a usable AI, since we are just using it to do research in som specific areas, and hence it actually does not play the game.

We are writing both papers and reports, but just as the current AI, they are not about a complete AI. The report from our last semester can be found here, but our paper from the last semester is currently not public, since it is under review for ADMI-10.

The battle simulation i'm referring to in this thread is still in a private repository, but will be made public when completed. We are simulating every single relevant battle between unit group compilations in BA, in order to use this as critical information for implementing RL for battle decisions.

Regarding the instant-kill we have currently just "hacked" it in our version of spring, by forcing selfDCountdown to be 1 in rts\Sim\Units\Unit.cpp. Though we still have some issues where it seems like the units are still not suiciding in some rare cases (<%1 of the times).

And yes, hoijui, you are right that we are using undeformable map, no-wrecks, and BA :-)
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Re: Instantly remove units

Post by trepan »

Spring.RevertHeightMap() can be used to revert
the terrain's height map to its original state.

Spring.KillUnit() can be used to instantly remove units.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: Instantly remove units

Post by hoijui »

yeah.. there are basically two options.
1. allowing to kill units instantly with a chat command, the ones that users can use too. that would work somehow like:

Code: Select all

/cheat
/destroy 456 prune
2. writing a gadget (Lua), and putting it into the mod. the gadget would use the functions that trepan showed. you could then communicate with that gadget using chat commands, eg. like in 1., but you had to use others, not already used by the engine.
Writing a gadget is simple. just extract the contents of the mod, go to right folder (something like LuaUI/Gadgets or Rules), then copy paste one file and edit it using the functions trepan mentioned and this reference:
http://springrts.com/wiki/Lua_Scripting
http://springrts.com/wiki/LuaCallinReturn

if you always use the same mod (plus version), and i guess you, then 2. seems like a viable option.

... and thanks for the info about your project :-)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Instantly remove units

Post by zwzsg »

Code: Select all

function gadget:GetInfo()
	return {
		name = "Wipe Units Cmd",
		desc = "Implement the new command /luarules wipeunits",
		author = "zwzsg",
		date = "18 february 2010",
		version = "1.0",
		license = "Free",
		layer = 0,
		enabled = true
	}
end

if (gadgetHandler:IsSyncedCode()) then
--SYNCED

	local function WipeUnits(Cmd,Line,WordList,Player)
		for _,u in ipairs(Spring.GetAllUnits()) do
			Spring.DestroyUnit(u,false,true)
		end
	end

	local function SetupWipeUnitsCmd()
		local cmd,func,help
		cmd  = "wipeunits"
		func = WipeUnits
		help = " instantly remove all units"
		gadgetHandler:AddChatAction(cmd,func,help)
		Script.AddActionFallback(cmd..' ',help)
	end


	function gadget:Initialize()
		SetupWipeUnitsCmd()
	end

end
In case you are unfamiliar with opening mods and placing gadgets, I attached below a BA mutator with the preceding gadget. With that gadget, a human can instantly remove all units by typing in the chat console /luarules wipeunits
Attachments
BA-WipeUnits.sd7
Must be placed along side BA71.sd7

Is a new mod, "Balanced Annihilation V7.1-WU" that is just like BA, but with the addition of a new command, /luarules wipeunits
(912 Bytes) Downloaded 16 times
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

Thank you very much for your Lua inputs. We will try to see if we get better results with that approach, instead of hacking in the engine :-)
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

I now tested it, and it looks very good. Great that we don't have to wait for the explosions!

However, one problem is remaining. Is there any way to activate such a lua command from the AI? I tried sending the command by using "COMMAND_SEND_TEXT_MESSAGE", but that just makes the AI write the "/luarules wipeunits" command in the chat, without activating the command.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Instantly remove units

Post by imbaczek »

you can make a widget that listens for chats from AI and invokes this command, but i don't know if it'll work in spring headless.
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: Instantly remove units

Post by hoijui »

i think it should.. with latest springheadless. hugh removed the engine internal setting enforcements (which included Lua widgets disabling).
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Instantly remove units

Post by zwzsg »

I hoped you would know how an AI can send commands, for I know nothing about the C++ AI interface.


If all else fails, I could make the gadget analyse every chat line and look for a keyword.
Attachments
cnslcmd.lua
Here is my console command widgets. It is a file that must be saved into the \LuaUI\Widgets\ folder, either inside a mod, or outside. I originally made that widget to remove the hassle to append "luarules" to every gadget command. Since it parses chat lines instead of registering commands, hopefully it will parse the AI text message too. Make your AI say the TEXT /wipeunits, and that widget will translate it into the COMMAND /luarules wipeunits
(523 Bytes) Downloaded 17 times
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

Thank you very much, yet again zwzsg! Your code showed me how to let Lua parse AI chat commands.

And no, as far as i know, there isn't any direct way to access Lua commands from the C++ AI interface, but i might have missed something.

Nevertheless, based on your code, i made this simple code, which takes every "/luarules x" command from the chat, and executes it:

Code: Select all

function widget:AddConsoleLine(line,priority)
	if string.match(line,"/luarules (.+)") then
		Spring.SendCommands(string.match(line,"/(luarules .+)"))
	end
end
Without that code, the "/luarules xxx" chat-commands from the AI, would just be shown in the chat-history without being executed.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Instantly remove units

Post by zwzsg »

>_< That's practically what my widget does, except with mine the AI wouldn't even need to write "luarules" between the "/" and the command.

I also had some worries issuing the same chat command that was read could cause infinite loop, hence why I discarded chat messages already starting by /luarules.

FYI, Spring.SendCommands is not limited to sending Lua commands. It's a lua command to execute any Spring command, including hard coded local or remote commands, such as for instance Spring.SendCommands{"cheat","advshading 1"} I am actually surprised that the C++ AI interface has no equivalent to this SendCommands fonction.

Symmetrically, the commands registered by a Lua gadget can be called in all the same ways as hardcoded commands. If you were to tell your AI to run any of the commands listed in /docs/cmds.txt, how would you?

Anyway, since your main goal is to work on your learning AI thesis, not to learn Lua, as long as what you have now gives satisfaction, we could leave it at that.
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

zwzsg wrote:>_< That's practically what my widget does, except with mine the AI wouldn't even need to write "luarules" between the "/" and the command.
Well just almost - the problem with using your function as-is, is that the AI's chat lines didn't match your regex:

Code: Select all

<SkirmishAI: BattleSim 666 (team 0)>: /luarules wipencunits
Is there any way to discard matched console-lines, so that they are not printed in the console or written to infolog.txt? Discarding default behavior. I thought of using "return false", but that didn't do anything.
zwzsg wrote: If you were to tell your AI to run any of the commands listed in /docs/cmds.txt, how would you?
Well we can't directly. There are "wrapper" functions for some common-used commands, but far from all of them.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Instantly remove units

Post by zwzsg »

allanmc wrote:Well just almost - the problem with using your function as-is, is that the AI's chat lines didn't match your regex:

Code: Select all

<SkirmishAI: BattleSim 666 (team 0)>: /luarules wipencunits
Oh. Then tell you can probably workaround by making the AI say "\n/wipeunits"

allanmc wrote:Is there any way to discard matched console-lines, so that they are not printed in the console or written to infolog.txt? Discarding default behavior. I thought of using "return false", but that didn't do anything.
IDK

allanmc wrote:
zwzsg wrote:If you were to tell your AI to run any of the commands listed in /docs/cmds.txt, how would you?
Well we can't directly. There are "wrapper" functions for some common-used commands, but far from all of them.
Well, if the need ever arise, you now know how to plumb that with Lua.
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

If anyone's interested, i solved the problem by allowing the AI to send console commands using the chat (just as the user can). The small change to rts/ExternalAI/AICallback.cpp can be seen here:
http://github.com/initram/springheadles ... 5ac#diff-0
Attachments
0001-AI-can-do-console-commands.txt
0001-AI-can-do-console-commands
(1.76 KiB) Downloaded 95 times
Last edited by allanmc on 23 Feb 2010, 16:06, edited 1 time in total.
User avatar
allanmc
Posts: 13
Joined: 07 Sep 2009, 14:29

Re: Instantly remove units

Post by allanmc »

And, if anyone is interested, here is the battle-simulator AI:
http://github.com/allanmc/BattleSim/

It requires the above mentioned patch, which is included in this Spring fork:
http://github.com/initram/springheadless/

If two instances of the AI is added to a game, they will simply fight 225625 battles between various group compilations, and in a binary file store a value [-100;100] for each battle indicating which team won, and how "well" it won.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Instantly remove units

Post by imbaczek »

nice work.
Post Reply

Return to “AI”