Adding Computer Enemies for Single Player Mission.
by Rutger Van Dijk · in RTS Starter Kit · 08/08/2005 (6:20 am) · 27 replies
Hi,
another question. where and how should i add computer controlled enemies for a single player mission? i tried to search the forums; but couldn't find any clear help. should be out there somewhere right? =)
cheers,
Rutger
another question. where and how should i add computer controlled enemies for a single player mission? i tried to search the forums; but couldn't find any clear help. should be out there somewhere right? =)
cheers,
Rutger
#2
08/08/2005 (11:26 am)
I want to go for a RTSAiConnection, but how do i have them actually join the game? where do you describe that there will be lets say 1, 2 or 3 computer opponents in a mission? =)
#3
08/08/2005 (3:50 pm)
You would call the usual on-game-join code, just passing it the RTSAIConnection instead of a normal connection?
#4
Now i tried adding a bot, by adding the following code at the bottom of the function:
now - the mission starts loading - but just never appears. it is indicated that one of the players is [Super] and the other is [Bot] - so that's something already. as mentioned; i think my reasoning about the creation of the connection and who owns them and controls them is clouded. would you guys minding shedding a little light? all i want is actually a few units that belong to the enemy and just stand around (and auto attack back when attacked; but got that code already)...
it's just that i got a deadline by the end of the month and doing a million things at a time; just getting rather paranoid. haha! thanks in advance guys :)
rutger
08/11/2005 (6:28 am)
I'm sorry to bother you guys, but i just can't get it to run. i guess i'm confused on the ways multiple connections are created on one machine. i tried editing the "start demo" function.function SM_StartDemo()
{
%id = SM_missionList.getSelectedId();
%mission = "starter.rts/data/missions/frenzy.mis";
createServer("SinglePlayer", %mission);
%conn = new RTSConnection(ServerConnection);
RootGroup.add(ServerConnection);
%conn.setConnectArgs($pref::Player::Name);
%conn.setJoinPassword($Client::Password);
%conn.connectLocal();
// Just auto join
commandToServer('PlayerReady', true);
}Now i tried adding a bot, by adding the following code at the bottom of the function:
%conn = new AIConnection(Bot1Connection);
RootGroup.add(Bot1Connection);
%conn.setConnectArgs("Freud");
%conn.setJoinPassword($Client::Password);
%conn.connectLocal();
// Just auto join
commandToServer('PlayerReady', true);now - the mission starts loading - but just never appears. it is indicated that one of the players is [Super] and the other is [Bot] - so that's something already. as mentioned; i think my reasoning about the creation of the connection and who owns them and controls them is clouded. would you guys minding shedding a little light? all i want is actually a few units that belong to the enemy and just stand around (and auto attack back when attacked; but got that code already)...
it's just that i got a deadline by the end of the month and doing a million things at a time; just getting rather paranoid. haha! thanks in advance guys :)
rutger
#5
08/15/2005 (9:19 am)
Anyone? :(
#6
Just add it t o the group and go to town.
08/15/2005 (10:55 am)
You don't need to do any connection stuff. That's all to... you know... establish a connection over the network. Which won't work. Because it's an AI. There's nothing there.Just add it t o the group and go to town.
#7
08/15/2005 (10:57 am)
Add it to what group? lol - i'm sorry man, but it's not as easy for people newer to the engine to have a clear overview of all these structures...
#8
08/15/2005 (3:00 pm)
Just set the connection to the same as the player, and have it so you cant control the AI's units.. because essentially it is the same connection.
#9
connectLocal is blowing up for you.
08/15/2005 (6:37 pm)
Do this:%conn = new AIConnection(Bot1Connection); RootGroup.add(Bot1Connection);
connectLocal is blowing up for you.
#10
I tried doing this; i add a few units with the %player.setControllingConnection(%this); set to the client - and if i had 1 unit with no controllingconnection, it crashes. It's either all controlled by 1 connection or none controlled by a connection. Any mixed combination crashes the engine.
Ben:
I removed the connectLocal code; and tried fiddeling around with it. I mean the code works, but as soon as i try to spawn a player for that connection, the thing crashes (i re-assigned: RTSConnection.createPlayer(...) to GameConnection.createPlayer(...) - so they both inherit the function. Guess it still doesn't like it much though.
i thought it wouldn't have to be as hard adding a few units that aren't controlled by the user and whom just fire back when encoutered. but i'm going banana's, my deadline is in 3 weeks and i really need to get this to work! :( please, any more suggestions or code would help me out... cheers.
08/16/2005 (3:16 am)
Robert: "Just set the connection to the same as the player, and have it so you cant control the AI's units.. because essentially it is the same connection."I tried doing this; i add a few units with the %player.setControllingConnection(%this); set to the client - and if i had 1 unit with no controllingconnection, it crashes. It's either all controlled by 1 connection or none controlled by a connection. Any mixed combination crashes the engine.
Ben:
I removed the connectLocal code; and tried fiddeling around with it. I mean the code works, but as soon as i try to spawn a player for that connection, the thing crashes (i re-assigned: RTSConnection.createPlayer(...) to GameConnection.createPlayer(...) - so they both inherit the function. Guess it still doesn't like it much though.
i thought it wouldn't have to be as hard adding a few units that aren't controlled by the user and whom just fire back when encoutered. but i'm going banana's, my deadline is in 3 weeks and i really need to get this to work! :( please, any more suggestions or code would help me out... cheers.
#11
Then in side this code in commands.cs
change it to this
Acually couldent you skip all that and when you create on same connection just put on different team?
I know the way I do it is that all units have a .owner tag and so only the proper owner can control it. simmiliar to this.
08/16/2005 (9:56 am)
No spawn the AI exactly like you spawn the players. Same controlling connection. Add something to the unit when you create him like %player.isai = "1";Then in side this code in commands.cs
function serverCmdAddToSelection(%client, %objID)
{
%obj = %client.resolveObjectFromGhostIndex(%objID);
%client.selection.add(%obj);
sendAllStatsToClient(%client, %obj);
} change it to this
function serverCmdAddToSelection(%client, %objID)
{
%obj = %client.resolveObjectFromGhostIndex(%objID);
if(%obj.isai != "1")
{
%client.selection.add(%obj);
sendAllStatsToClient(%client, %obj);
}
}That should work, although it should be easier than that. Let me acually see how I do this in my game.Acually couldent you skip all that and when you create on same connection just put on different team?
I know the way I do it is that all units have a .owner tag and so only the proper owner can control it. simmiliar to this.
#12
08/16/2005 (11:10 am)
Have you considered just having "orphan" AI units that aren't tied to a connection at all?
#13
08/23/2005 (7:30 pm)
Did anybody get this to work?
#14
08/24/2005 (2:42 am)
Did anybody get this to work?
#15
08/24/2005 (3:41 am)
Reposts are bad. :)
#16
having set the units auto-attack any unit that is closeby and is not on their team; they started attacking each other. which is fine. this method is not very neat; but it gets the job done if you want it something simple like this =)
08/24/2005 (4:42 am)
I just followed Robert's last post as a guideline; when creating the unit you set a 'newplayer.isai' variable indicating that it isn't controlled by the user. after this i set this enemy opponent's team to a different one from the user. having set the units auto-attack any unit that is closeby and is not on their team; they started attacking each other. which is fine. this method is not very neat; but it gets the job done if you want it something simple like this =)
#17
11/06/2005 (2:44 am)
@Ben, Ok my curiosity is piqued, how would one go about creating an Orphaned AI unit? What would the code look like. For my purposes, I basically just want a couple of units standing still or walking to random places, but I don't see how to spawn a computer controlled unit at all.
#18
11/07/2005 (2:32 am)
Here is what I'm trying but it seems to segfault, and since I can't get RTS to compile, I'm stuck without the debug symbols to run a back trace, so at this point I'm a little clueless.function addAIPlayer(%index,%spawnpoint,%name){
//%index removed for testing, for now just spawn the botBlock
echo("Entering addAIPlayer");
%player = new RTSUnit()
{
scale = "1 1 1";
dataBlock = botBlock;
shapeFile = "~/data/shapes/player/player.dts";
path = "";
};
echo("AI Player was spawned");
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint);
%player.setMoveDestination(GetRandomLocation());
return(%player);
}
#19
If RTSConnection::CreatePlayer is never called there is no segfault, however the player created seems to have no body either.
There are no errors even with trace(1); invoked.
I'm stumped.
*update*
Taking cues from starter.fps I wrapped things up into a ScriptObject and am trying to use an aiPlayer.cs type of setup. Now I have no more segfaults, however even though the aiPlayer spawns, I cannot see him at all. If I go to the WorldEditor I do however see a bounding box.
I'm getting closer I feel it. Here is the code for my AIPlayer.cs for those interested. Also I just copied the code from game.cs that makes Kork spawn, it's in an identical location.
11/07/2005 (3:19 am)
Ok, oddly enough I have found that this only segFaults if it's called before or after RTSConnection::createPlayerIf RTSConnection::CreatePlayer is never called there is no segfault, however the player created seems to have no body either.
There are no errors even with trace(1); invoked.
I'm stumped.
*update*
Taking cues from starter.fps I wrapped things up into a ScriptObject and am trying to use an aiPlayer.cs type of setup. Now I have no more segfaults, however even though the aiPlayer spawns, I cannot see him at all. If I go to the WorldEditor I do however see a bounding box.
I'm getting closer I feel it. Here is the code for my AIPlayer.cs for those interested. Also I just copied the code from game.cs that makes Kork spawn, it's in an identical location.
//-----------------------------------------------------------------------------
// AIPlayer static functions
//-----------------------------------------------------------------------------
function AIPlayer::spawn(%name,%spawnPoint)
{
// Create the demo player object
%player = new RTSUnit()
{
scale = "1 1 1";
dataBlock = botBlock;
shapeFile = "~/data/shapes/player/player.dts";
path = "";
};
MissionCleanup.add(%player);
%player.setShapeName(%name);
%player.setTransform(%spawnPoint);
return %player;
}
function AIManager::think(%this)
{
// We could hook into the player's onDestroyed state instead of
// having to "think", but thinking allows us to consider other
// things...
if (!isObject(%this.player))
%this.player = %this.spawn();
%this.schedule(500,think);
}
function AIManager::spawn(%this)
{
%player = AIPlayer::spawn("Kork","0 0 400");
return %player;
}
#20
if so please leave a detailed step by step process with source code :)
03/13/2007 (9:03 pm)
Does anyone having a working Computer AIif so please leave a detailed step by step process with source code :)
Torque 3D Owner Stephen Zepp
There are a couple of strategies worth exploring:
A) Create an RTSAIConnection similar to the AIConnection from TGE and assign your computer controlled teams to that connection.
B) Hack through and intercept all communications to/from a client that are implied expected by the use of RTSConnection. In other words, find a way to short circuit what is sent to and expected from the player's RTSConnection, and set a boolean that allows your code to handle each type--an RTSConnection with a real player connected, and an RTSConnection without a real client attached to it. This shouldn't be too difficult, but will be very time consuming as you will need a large amount of test cases to make sure you tighten up -all- of the code associated with the modification.