Idle SCV to gather minerals

Anything related on how to build / improve your AI using BWAPI, BWTA, BWSAL, etc.
abhisheknage
Posts: 5
Joined: Wed Jan 12, 2011 5:52 am

Idle SCV to gather minerals

Postby abhisheknage » Wed Jan 12, 2011 5:57 am

Hey Guys,

Firstly this is my first post here, and I am an ultra noob in coding.

My problem is I am unable to have idle/just spawned SCV's to gather minerals.

This is my code here:

for(std::set<Unit*>::const_iterator i= Broodwar->self()->getUnits().begin();i!=Broodwar->self()->getUnits().end();i++){
//Broodwar->sendText("collecting all units information");
if ((*i)->getType().isWorker())
{
if ((*i)->isGatheringMinerals()){
Broodwar->sendText("is gathering minerals");
}
else
{


Unit* closestMineral=NULL;
Broodwar->sendText("getting free workers1");
for(std::set<Unit*>::iterator m=mineralFieldsLeft.begin();m!=mineralFieldsLeft.end();m++)
{

if (closestMineral==NULL || (*i)->getDistance(*m)<(*i)->getDistance(closestMineral))
closestMineral=*m;
}

if (closestMineral!=NULL){
(*i)->rightClick(closestMineral);
mineralFieldsLeft.erase(closestMineral);
}
}
}
}

mineralFieldsList is already defined, but it seems that when it's playing starcraft, the message getting free workers does show up, but I don't know now what to do, because the free worker won't mine minerals.

Any help would be greatly appreciated.

Thanks,
darklord
krasi0
Posts: 254
Joined: Thu Dec 18, 2008 12:42 pm

Postby krasi0 » Wed Jan 12, 2011 7:11 am

Hi,
When do you use this code? in onStart or onFrame? If it's onFrame, know that it takes a few frames for the SCV to head to the minerals so (*i)->isGatheringMinerals() may return false for a while.
abhisheknage
Posts: 5
Joined: Wed Jan 12, 2011 5:52 am

Postby abhisheknage » Wed Jan 12, 2011 9:39 am

hi krasi0,

Well i am using it on onFrame, and i found out where i made the mistake though. Else if((*i)->isIdle && (*i)->isCompleted)
I guess i shud have added tht. But now it works.

Return to “AI development (BWAPI Bots)”

Who is online

Users browsing this forum: No registered users and 1 guest