problems with ai - problems with ai. torque 3d alpha
by StephenBar · in Torque 3D Beginner · 08/28/2010 (9:41 pm) · 2 replies
I am student at glwyndr unversity in wales i have followed my lectures notes to implement ai. - and have followed.
http://www.torquepowered.com/community/forums/viewthread/103503
chris gs zombie thing.
and this.
Firstly, you will need to download the two accompanying files “aiPlayer.cs” and “aiPlayerDatablocks.cs”. You also need to have an appropriate code editor application such as Notepad++, Crimson Editor or Torsion (the standard Torque Script Editor application).
Open “server\scripts\game.cs”.
Look at the function called “startGame()”. There you should several lines of code designed to activate the built in Torque AI Manager system:
// Start the AIManager
new ScriptObject(AIManager) {};
MissionCleanup.add(AIManager);
AIManager.think();
In order to implement a more complex solution, we will need to deactivate it and replace it with our own functionality. Comment out the above codeusing either line comments “//” or block comments “/*” and “*/”. Replace the code with the following:
AIPlayer::LoadEntities();
Instead of invoking the standard AI Manager (which has a set way of dealing with AI objects), we will be loading a series of independent entities into our mission map that will act in accordance with our desired settings.
Save and close “game.cs”. Open “server\scripts\player.cs”. Next, we need to amend the Armor object damage function to ensure that it includes our AI characters.
The following code is designed to return information about which player-client, player object, type of damage inflicted and in what location of the map it occurred whenever the player character is killed:
if (%obj.getState() $= "Dead")
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
The Torque suite (by default) is not set up to support multiple player objects in map at any one time, so we need to amend the Armor function to ensure that each time a player death occurs on the map, we check to see if it is an AI bot, or the player character.Ideally, we can then either re-spawn the enemy character if it is killed, or re-spawn the player character after a designated amount of time. Comment out the above lines of code, and replace them with the following:
if (%obj.isbot == true)
{
%obj.attentionlevel=1;
%obj.enhancefov(%obj);
}
if (%obj.getState() $= "Dead")
{
if (%obj.isbot == true)
{
if (%obj.respawn == true)
{
%obj.delaybeforerespawn(%obj.botname, %obj.marker);
%this.player=0;
}
}
else
{
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}
}
At this point, we can now make use of the files we downloaded earlier.
Add the “aiPlayerDatablocks.cs” file to your “server\scripts” folder. In addition, you need to we need to replace the “aiPlayer.cs” file with the new one we downloaded (be sure to save a copy of the original aiPlayer file before you overwrite it!).
Open “server\scripts\game.cs”.
Inside the function called “onServerCreated()”, we need to ensure that the execute command for “aiPlayer.cs” is at the bottom of the list (or at least AFTER all of the game character script execution commands in the list). This is due to the fact that we want the AI player Datablocks that we create to override existing character models, without this all AI characters will end up with the Torque default character model.
A word about “aiPlayer.cs”… If you take a moment to review our new version of the aiPlayer file, you’ll notice a large number of changes. This is now the central store of all functionality for our new AI characters, and also provides the set-up definitions for our AIMarker objects that will appear in the Torque Mission Editor when we next start our game.
Open the “aiPlayerDatablocks.cs” file. Here, we can now create a new DataBlock definition for our AI character. Add the following code to the bottom of the file:
datablock PlayerData(TestAIPlayer : PlayerBody)
{
shapeFile = "~/data/shapes/player/player.dts";
maxDamage = 100;
maxForwardSpeed = 7;
with the Dynamic Variables As mentioned earlier, dynamic variable can be easily overridden to allow more varied content. If you don't want to override a variable's default value, just don't add that variable to the marker when you deploy it. In addition, you should not assign a value which is already the default value to a variable. All of the variables that can be set on the spawn marker itself and their values (more details below):
pathname Set this to the name of the path that this bot is to follow. You have to place a path with nodes in your mission and give it a corresponding name to have the mob go on that path. If you wish for the bot to be unpathed, don't add this variable and the bot will default to unpathed. As an example, to make the bot pathed when using the Stronghold mission, create a new dynamic variable called 'pathname' and set its value to 'path1'. respawn If you want to override the default respawn value, create a dynamic variable called 'respawn' and set it's value to either 'true' or 'false'. range = "ranged"; This variable can be used to differentiate between bots with different weapon ranges. For example melee only bots and bots with ranged weapons; or ones with sniper rifles and rocket launcher versus
maxBackwardSpeed = 5;
maxSideSpeed = 5;
shootingDelay = 20;
};
In addition, you’ll notice the execute command for our “aiPlayerDatablocks.cs” file, and a large selection of “AI_GUARD” variable types… these will be of critical importance when we come to customize the behavior of our different AI player characters.
doesReturn = "guard"; This sets whether the bot returns to its spawn point or stays near the location where it last saw the player, after killing or losing sight of the player. This can be set differently for each bot. For unpathed bots the default is for the bot to go to the player's last location. When "doesRetun" is set to "guard" for unpathed bots, that particular bot will return to its spawn point (much like in the original AI Guard Unit resource).
For pathed bots the default is for the bot to go back to its path. When set to "guard", after the bot has lost sight of or killed the player, the bot will then operate like an unpathed bot that is not set to "guard"
Using DataBlocks to change AI Character Models
A few things need to be set up before you can use different datablocks. Firstly, the model has to exist under data/shapes with all the necessary .dsq and .dts files. Secondly, the new character needs to have a definition script in /server/scripts/ (eg. player.cs, adam.cs, kork.cs or skeleton.cs).
Next that definition file must be called in onServerCreated() in game.cs. Then you have to copy, paste and change the names of the first 2 functions in aiPlayer.cs which are DefaultPlayer::onReachDestination() and DefaultPlayer::OnDamage(). Changing "DefaultPlayer" in the function's name to whatever the name of your datablock is. After that, add the datablock to aiPlayerDatablocks.cs making sure that the name of the datablock is unique, and the body type is valid (eg. datablock PlayerData(UniqueName : ValidBody)). If all that is set up correctly, just add a block = "UniqueName" parameter to your AIPlayerMarker and you should be ready to go.
Changing the Default Weapons
Setting up different weapons is very similar to using different datablocks: * The model has to exist in data/shapes * A Script has to be made to handle the weapon (eg. crossbow.cs) * That script must be called in onServerCreated() in game.cs Where it differs is in adding the datablock. For weapons, you don't have to add the datablock into aiPlayer.cs or aiPlayerDatablocks.cs. You should just be able to add a Weapon = "myNewWeapon" parameter to your aiPlayerMarker.
i have tried changing the data, types altering ai marker (which don;t appear, i have tried for months to get the ai to work, there is no really tuturial to show the implementation of ai. i have pathed my game with markers, found the various functions if not in correct space. i understand all the coding so why doesn;t the ai work? i have edited the code in so many different ways to try and create ai can some one help, note i want to it with scripting more than by just recommending an add on. i have also used the torque 3d resorce for rts (under enemy but that also don;t work.
http://www.torquepowered.com/community/forums/viewthread/103503
chris gs zombie thing.
and this.
Firstly, you will need to download the two accompanying files “aiPlayer.cs” and “aiPlayerDatablocks.cs”. You also need to have an appropriate code editor application such as Notepad++, Crimson Editor or Torsion (the standard Torque Script Editor application).
Open “server\scripts\game.cs”.
Look at the function called “startGame()”. There you should several lines of code designed to activate the built in Torque AI Manager system:
// Start the AIManager
new ScriptObject(AIManager) {};
MissionCleanup.add(AIManager);
AIManager.think();
In order to implement a more complex solution, we will need to deactivate it and replace it with our own functionality. Comment out the above codeusing either line comments “//” or block comments “/*” and “*/”. Replace the code with the following:
AIPlayer::LoadEntities();
Instead of invoking the standard AI Manager (which has a set way of dealing with AI objects), we will be loading a series of independent entities into our mission map that will act in accordance with our desired settings.
Save and close “game.cs”. Open “server\scripts\player.cs”. Next, we need to amend the Armor object damage function to ensure that it includes our AI characters.
The following code is designed to return information about which player-client, player object, type of damage inflicted and in what location of the map it occurred whenever the player character is killed:
if (%obj.getState() $= "Dead")
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
The Torque suite (by default) is not set up to support multiple player objects in map at any one time, so we need to amend the Armor function to ensure that each time a player death occurs on the map, we check to see if it is an AI bot, or the player character.Ideally, we can then either re-spawn the enemy character if it is killed, or re-spawn the player character after a designated amount of time. Comment out the above lines of code, and replace them with the following:
if (%obj.isbot == true)
{
%obj.attentionlevel=1;
%obj.enhancefov(%obj);
}
if (%obj.getState() $= "Dead")
{
if (%obj.isbot == true)
{
if (%obj.respawn == true)
{
%obj.delaybeforerespawn(%obj.botname, %obj.marker);
%this.player=0;
}
}
else
{
%client.onDeath(%sourceObject, %sourceClient, %damageType, %location);
}
}
At this point, we can now make use of the files we downloaded earlier.
Add the “aiPlayerDatablocks.cs” file to your “server\scripts” folder. In addition, you need to we need to replace the “aiPlayer.cs” file with the new one we downloaded (be sure to save a copy of the original aiPlayer file before you overwrite it!).
Open “server\scripts\game.cs”.
Inside the function called “onServerCreated()”, we need to ensure that the execute command for “aiPlayer.cs” is at the bottom of the list (or at least AFTER all of the game character script execution commands in the list). This is due to the fact that we want the AI player Datablocks that we create to override existing character models, without this all AI characters will end up with the Torque default character model.
A word about “aiPlayer.cs”… If you take a moment to review our new version of the aiPlayer file, you’ll notice a large number of changes. This is now the central store of all functionality for our new AI characters, and also provides the set-up definitions for our AIMarker objects that will appear in the Torque Mission Editor when we next start our game.
Open the “aiPlayerDatablocks.cs” file. Here, we can now create a new DataBlock definition for our AI character. Add the following code to the bottom of the file:
datablock PlayerData(TestAIPlayer : PlayerBody)
{
shapeFile = "~/data/shapes/player/player.dts";
maxDamage = 100;
maxForwardSpeed = 7;
with the Dynamic Variables As mentioned earlier, dynamic variable can be easily overridden to allow more varied content. If you don't want to override a variable's default value, just don't add that variable to the marker when you deploy it. In addition, you should not assign a value which is already the default value to a variable. All of the variables that can be set on the spawn marker itself and their values (more details below):
pathname Set this to the name of the path that this bot is to follow. You have to place a path with nodes in your mission and give it a corresponding name to have the mob go on that path. If you wish for the bot to be unpathed, don't add this variable and the bot will default to unpathed. As an example, to make the bot pathed when using the Stronghold mission, create a new dynamic variable called 'pathname' and set its value to 'path1'. respawn If you want to override the default respawn value, create a dynamic variable called 'respawn' and set it's value to either 'true' or 'false'. range = "ranged"; This variable can be used to differentiate between bots with different weapon ranges. For example melee only bots and bots with ranged weapons; or ones with sniper rifles and rocket launcher versus
maxBackwardSpeed = 5;
maxSideSpeed = 5;
shootingDelay = 20;
};
In addition, you’ll notice the execute command for our “aiPlayerDatablocks.cs” file, and a large selection of “AI_GUARD” variable types… these will be of critical importance when we come to customize the behavior of our different AI player characters.
doesReturn = "guard"; This sets whether the bot returns to its spawn point or stays near the location where it last saw the player, after killing or losing sight of the player. This can be set differently for each bot. For unpathed bots the default is for the bot to go to the player's last location. When "doesRetun" is set to "guard" for unpathed bots, that particular bot will return to its spawn point (much like in the original AI Guard Unit resource).
For pathed bots the default is for the bot to go back to its path. When set to "guard", after the bot has lost sight of or killed the player, the bot will then operate like an unpathed bot that is not set to "guard"
Using DataBlocks to change AI Character Models
A few things need to be set up before you can use different datablocks. Firstly, the model has to exist under data/shapes with all the necessary .dsq and .dts files. Secondly, the new character needs to have a definition script in /server/scripts/ (eg. player.cs, adam.cs, kork.cs or skeleton.cs).
Next that definition file must be called in onServerCreated() in game.cs. Then you have to copy, paste and change the names of the first 2 functions in aiPlayer.cs which are DefaultPlayer::onReachDestination() and DefaultPlayer::OnDamage(). Changing "DefaultPlayer" in the function's name to whatever the name of your datablock is. After that, add the datablock to aiPlayerDatablocks.cs making sure that the name of the datablock is unique, and the body type is valid (eg. datablock PlayerData(UniqueName : ValidBody)). If all that is set up correctly, just add a block = "UniqueName" parameter to your AIPlayerMarker and you should be ready to go.
Changing the Default Weapons
Setting up different weapons is very similar to using different datablocks: * The model has to exist in data/shapes * A Script has to be made to handle the weapon (eg. crossbow.cs) * That script must be called in onServerCreated() in game.cs Where it differs is in adding the datablock. For weapons, you don't have to add the datablock into aiPlayer.cs or aiPlayerDatablocks.cs. You should just be able to add a Weapon = "myNewWeapon" parameter to your aiPlayerMarker.
i have tried changing the data, types altering ai marker (which don;t appear, i have tried for months to get the ai to work, there is no really tuturial to show the implementation of ai. i have pathed my game with markers, found the various functions if not in correct space. i understand all the coding so why doesn;t the ai work? i have edited the code in so many different ways to try and create ai can some one help, note i want to it with scripting more than by just recommending an add on. i have also used the torque 3d resorce for rts (under enemy but that also don;t work.
About the author
#2
11/02/2010 (6:01 pm)
sorry for long responce but thanks. twisted posted up on that forum link. that you gave in thier i found the problem. The ai manger was put in the wrong place. it does not go in with the other ai information but inside the start game fuction. when the game is intilised.
Justin P Greer
First you must learn that new File structure for T3D. Yes it is different if you have not noticed. One main thing you need to know when working with the AI in the T3D is that it is disabled by default. This may be why you are doing everything correctly and nothing is happening. Now I may be wrong but I think the function AIManger is needed for the AI to work unless you have made a custom .cs file that holds the Function that calls the AIManger to start. But I doubt thats the smart way to do it.
Ok, from the resource you used there is a couple of things you have to look at.
1. The resource was for TGE or TGEA and there for it will not work with T3D. but alls you have to do is port the scripts to work with T3D by pretty much changing a few things in the file ( Simple ) I will post the a link to a T3d resource I just posted that has already been ported to T3D. The porting consist of Changing the Default Player Datablock of TGE and TGEA to the Defualt player Datablock of T3D. They are different. This will make the AI system not work but you will see it in the console when the game play has started with a error.
Make sure that you change DefaultPlayerData i believe to DemoPlayerData in the aiplayer.cs ( Now dont quote me on this, I am going off of memory which is not good lol )
2. The Game.cs file is not where the AImanger function is with T3D. it is located in GameCore.cs and is commented by default. This will again not make it work with everything else right. Simply make sure it is un commented and that should work.. Now for the resource you read, I am pretty sure you did not want to delete the AIManger function you simply just wanted to place this line
after the AIManger function.
3. The line in AIPlayerDatablocks.cs
is supost to be changed to your own .dts model it can be anything you want. I used a small griminess thing as a marker..
4. The Paths that are in the Resource may be in correct due to the change in file structure with T3D. Easy just change them to T3D paths.
Pretty much that is a quick thing I can think of right now.
I used a similar resource when doing mine from Twisted. The guy that created the AI package for Torque.
Here is my resource for T3D and AI using the same setup as you..
www.torquepowered.com/community/resources/view/20211
If you need help I can help and I am sure twisted could get you on the right path as well.. He knows what he is doing in the AI Field. I also am going to be doing a video from beginning to end of implanting AI into Torque T3D along with other Old resources for TGE and TGEA that have been ported to work with T3D