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?
Autoadding to group in groupAi
Moderators: hoijui, Moderators
I wonder if you could do something like this
Aside from that something with the GlobalAI interface.
Code: Select all
for(int i=1; i<10000; i++)(
this->add(i);
)
Still doesn't work
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:
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:
The dll loads fine but no effect, I wonder why? (the logic seems fine to me...)
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);
}
*/
}
Then I added this line to SimpleformationAi code in the update method:
Code: Select all
callback->AddAllUnits();
It Works!
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):
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() :
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
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
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);
}
}
}
Then in the groupAi.cpp of SimpleFormationAI I inserted in Update() :
Code: Select all
callback->AddAllUnits();
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

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
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.
CanBuildAt is rather simple just call the corresponding function in unithandler.