Autoadding to group in groupAi

Autoadding to group in groupAi

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

Moderators: hoijui, Moderators

Post Reply
alik83
Posts: 82
Joined: 08 Sep 2004, 15:32

Autoadding to group in groupAi

Post by alik83 »

AF, SJ or anybody, do you know how to make a simple groupAI automatically add new created units to it?
I tried to modify SimpleFormationAI to automatically add newly created mobile units to it but it didn't work.
Have anyone managed to get autoadding working?

It would seem to work by going through all your known friendly units, marking them as selected, then copy the code that adds units to a group from which at least one unit is selected, but I couldn't make it work yet...

P.S.
Is SJ taking the work with globalAI interface from now on?
SJ
Posts: 618
Joined: 13 Aug 2004, 17:13

Post by SJ »

Only way to autoadd units to an ai is to have the factory building assigned to that ai.

Yes I will probably take over development of the globalai interface although I will fix the new map format first.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

I wonder if you could do something like this

Code: Select all

for(int i=1; i<10000; i++)(
     this->add(i);
)
Aside from that something with the GlobalAI interface.
alik83
Posts: 82
Joined: 08 Sep 2004, 15:32

Still doesn't work

Post by alik83 »

I spent quite a bit of time trying to code this autoadding but it still doesn't work,
but if a player can select all units which include a groupAi and give a "Add to group command" it shouldn't be difficult to make the computer do this operation automatically every Update() cycle.

I tried imitating this player operation by adding following method to GroupAiCallback.cpp:

Code: Select all

void CGroupAiCallback::AddAllUnits() 
{
list<CUnit*> selection;
	set<CUnit*>* tu=&gs->teams[gu->myTeam]->units;
		for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
			selection.push_back(*ui);
		}
		selectedUnits.ClearSelected();
		for(list<CUnit*>::iterator ui=selection.begin();ui!=selection.end();++ui) {
			selectedUnits.AddUnit(*ui);
		}
		Command c;
		c.id=CMD_GROUPADD;
		selectedUnits.GiveCommand(c, false);


/*
		CGroup* group=0;
		for(list<CUnit*>::iterator ui=selection.begin();ui!=selection.end();++ui){
			if((*ui)->group){
				group=(*ui)->group;
				break;
			}
		}
		if(group){
			for(list<CUnit*>::iterator ui=selection.begin();ui!=selection.end();++ui){
				if(!(*ui)->group)
					(*ui)->SetGroup(group);
			}	
			selectedUnits.SelectGroup(group->id);
		}

*/
}
Commented is my alternative try, both way it compiles fine but has no effect.

Then I added this line to SimpleformationAi code in the update method:

Code: Select all

 callback->AddAllUnits();
The dll loads fine but no effect, I wonder why? (the logic seems fine to me...)
User avatar
Min3mat
Posts: 3455
Joined: 17 Nov 2004, 20:19

Post by Min3mat »

why would u want autoadding AI? except for the PC i suppose >.<
SJ
Posts: 618
Joined: 13 Aug 2004, 17:13

Post by SJ »

The dlls are completly separate from the rest of the application. The only way they can affect it is through the callback function they are given. selectedUnits etc isnt pointing to anything when called in a dll and im really surprised that that code would compile let alone run.
alik83
Posts: 82
Joined: 08 Sep 2004, 15:32

It Works!

Post by alik83 »

I made autoadding to group work!
For now it only adds all new units to the last group (groupAI) that a player assigned but it's easy to change to make for ex. mobile units go in one group, builders to other automatically and so on.

As I said I created a method AddAllUnits() in GroupAiCallback.cpp(and in IGroupAICallback) (updated):

Code: Select all

void CGroupAiCallback::AddAllUnits() 
{
	set<CUnit*>* tu=&gs->teams[gu->myTeam]->units;
		CGroup* group=0;
		bool newUn = false;
		for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
			if((*ui)->group && !group){
				group=(*ui)->group;
				//break;
			}
			else if(!(*ui)->group){
				if(!group){ newUn=true; }
				if(group){ (*ui)->SetGroup(group); }
			}
		}
		if(group && newUn){
			for(set<CUnit*>::iterator ui=tu->begin();ui!=tu->end();++ui){
				if(!(*ui)->group)
					(*ui)->SetGroup(group);
			}	
		}



}
And SJ gave me a hint that I should just recompile the engine to make it work;) (Stupid me:))

Then in the groupAi.cpp of SimpleFormationAI I inserted in Update() :

Code: Select all

callback->AddAllUnits();
Maybe there is more optimal way to autoadd units (because I noticed the time and CPU percent in debug are slowly but constantly increasing with new groupAI)

Now as I see I can code smth., I'll try for practice to code some more additions to groupAI like the canBuildAt() function and so on to make a simpleAi possible (It's almost possible now)

Ofcourse SJ can code that stuff better then me, but I'm anxious to do something real with AI :twisted:

BTW a question: how do you compile a project without debug info to make executables the same size as in Spring0.51 distribution?
Why does my compiled executable run quite a lot slower then original, do I need to set any special flags for the project?
Thanks
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Firstly, isnt there already a canbuildat sort of function available? Secondly, SJ or Jouninkomiko will be doing all fo this stuff for the new GlobalAI.

However you have joined the select club of people who have actually written AI code publicly.
SJ
Posts: 618
Joined: 13 Aug 2004, 17:13

Post by SJ »

You need to compile it as release and not debug to achive better speed etc. No official release will contain an AddAllUnits command in the groupai, thats the realm of the globalai but i guess you and others can use it as a hack until that is done.

CanBuildAt is rather simple just call the corresponding function in unithandler.
Post Reply

Return to “AI”