Game Development Community

How to add AI players to game at startup [solved!]

by Jeff Yaskus · in RTS Starter Kit · 03/07/2010 (7:02 pm) · 2 replies

I added AI players using this code mod ... from this old post on the forums;
http://www.torquepowered.com/community/forums/viewthread/29771

Just add this to the END of RTSConnection.cc;
ConsoleFunction(addAI,S32,2,3,"addAI('playerName'[,'AIClassType']);")
{
	// create AI player
	AIRTSConnection *aiPlayer = new AIRTSConnection();
	aiPlayer->registerObject();
	aiPlayer->setGhostFrom(false);
	aiPlayer->setGhostTo(false);
	aiPlayer->setSendingEvents(false);
	aiPlayer->setTranslatesStrings(false);
	aiPlayer->setEstablished();

	// add the client to the AI group
	SimGroup *g = Sim::getClientGroup();
	g->addObject(aiPlayer);

	return aiPlayer->getId();
}

and this to the END of RTSConnection.h ;
class AIRTSConnection: public RTSConnection
{
	typedef RTSConnection Parent;
};


This works great, I add the enemy by calling it from script like this;
$TeamB = addAI("bob");
$TeamB.onCliententergame();


However, it has (2) problems ... which ought to be easy to solve;

1) the NAME of the AI player doesnt get set ... so when you hit F2 to show player score, its blank.

I think its set for the player using .cs code like this ... but can't figure out how to do it in C++
%conn.setConnectArgs($pref::Player::Name);

and other problem
2) running onCliententergame() for the AI player ... somehow "overwrites" the player (0) resource store info.
all the players values are set to (0) ... and collecting any resources doesnt add them back to the players store.

As an attempt at a work around, I created another function in gameConnection.cs;
with this code commented out ... stops main player issues, but "breaks" collecting and such for AI;
// For AI - so it dont stomp on players
function RTSConnection::onAIClientEnterGame(%this)
{    
 ...
 //   echo("RTSConnection::onClientEnterGame--setting up resource store");   
 /*
   %this.resourceStore = resourceStore::Ctor();
   commandToClient(%this, 'AcceptSetupStores', "LOCAL"); 
 */   
 ...
}

So any ideas ... how to set the AI players "name" ... and correctly initialize their resources ?


#1
03/14/2010 (2:27 pm)
This code works good enough so far. It takes care of adding the enemy without having to worry about crashes caused by sending the AI messages.

But rather than trying to figure out how to setup stores and such for the AI -- I am working out some simple AI methods for the Enemy AI player(s).

* The barracks will "train" a unit every 8-12 seconds (twice as long as it takes player to train a unit) ... until its destroyed.

* enemy units will "scan" for targets every 5s ... if not already targeting something.

* peons find resources on their own - shout for help and flee if attacked

* guard and patrol unit "modes" ...

who knows if it will ever get finished, but the RTS Kit sure could you a plug-n-play AI engine or such.

#2
03/21/2010 (8:26 pm)
I was able to fix the first problem (players name) with the following code ... easier than I expected.
// spawn enemy "AI"
        $EnemyAI=addAI("goblins1");
        $EnemyAI.name = "goblins1";        
        
        // spawn enemy units and base ...
        $EnemyAI.schedule(30000, "onAIClientEnterGame");

And I got around using the store for the AI units, but "faking" it ... using the above onAICliententergame() function.

So now it works great, F2 shows both players score ... and first one to 8 points wins.

Funny thing, I created (3) AI players ... while testing and twice, the AI players got 8 kills before I could.