Game Development Community

Improved AI Guard Unit

by Twisted Jenius · 08/25/2008 (7:20 am) · 167 comments

Code File Downloads: TGE 1.5.2 Version and T3D 1.1 Version. If you are using TGEA, download the TGE files and then see comments #8 and #9 below for the necessary changes you need to make.

Update: The new T3D port of this resource already includes the "final update" posted in the comments as well as updated instructions. However it does not include any additional features, such as those found in The Universal AI Starter Kit.



Improved AI Guard Unit


First off I want to give credit to the 3 resources that I based most of this resource's code on.

AI Guard Unit
A-Star Guard
Artificial Intelligence Beginning

I'd recommend checking those resources (especially AI Guard Unit) if you have any questions or problems before posting here, because they already have a lot of good feedback and answers in their comments.



Now down to business. What's different about this resource from those? In addition to having all the great features of the AI Guard Unit resource, it also has the following new features and changed behaviors when compared to that resource (some of which are taken from the other 2 resources listed):

-Unpathed bots pace while in an idle guarding state.
-After killing or losing track of the player, the bots can either return to their spawn point or stay near the location where they last saw the player alive.
-Individual bots can be either pathed or unpathed; multiples of both kinds can be used at the same time from this one resource.
-Pathed bots are still able to move away from their paths while attacking and then return to their path once they are done (returning to the closest node).
-The bots are able to detect players better after the bot has been sniped.
-The bots won't keep bumping into you while they're attacking (with 2 different range settings for that).
-Vastly improved ability to follow the player around corners and other obstacles.
-You are able to use different datablocks and weapons for different bots.
-Trigger based spawning in addition to loading them all at the start.
-No engine modifications are needed.
-Line of sight tests and correct facing while sidestepping obstacles and dodging attacks.
-Various bug fixes and other improvements.
-Lots of easy to change variables (conveniently located at the top of aiPlayer.cs) with well commented code. Most variables can be set to 0 to turn off features that you may not be using.
-Compared to any of the resources whose code this was based on; this AI is a lot more life like and challenging to battle with, while being even more customizable.



INSTALLATION

These instructions are for TGE 1.5.2. For TGEA instructions please check the comments.

1. In the file game.cs function startGame() replace this-

// Start the AIManager
   new ScriptObject(AIManager) {};
   MissionCleanup.add(AIManager);
   AIManager.think();

With this-

AIPlayer::LoadEntities();

2. In the file player.cs function Armor::damage() replace this-

// Deal with client callbacks here because we don't have this
   // information in the onDamage or onDisable methods
   %client = %obj.client;
   %sourceClient = %sourceObject ? %sourceObject.client : 0;

   if (%obj.getState() $= "Dead")
      %client.onDeath(%sourceObject, %sourceClient, %damageType, %location);

With this-

// Deal with client callbacks here because we don't have this
   // information in the onDamage or onDisable methods
   %client = %obj.client;
   %sourceClient = %sourceObject ? %sourceObject.client : 0;
   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);
      }
   }

3. Replace your old aiPlayer.cs with the new one and just place aiPlayerDatablocks.cs in the same folder.
4. Load your map - Stronghold as an example.
5. Go into the map editor (F11). Then go into the Editor Creator (F4).
6. Under Shapes there should be a new drop down called AIMarker, under that a new item called AIPlayerMarker.
7. Create a new AIPlayerMarker.
8. Select your marker, position it where you like and hit (F3) to modify the marker.
9. If you want to override any of the marker's default values, create a dynamic variable with the appropriate name and value (see below for details on what those are).
10. Update your item by clicking 'APPLY' - very important and easy to miss step.
11. Save your mission and reload it.



ADD-ONS

If the dynamic variables listed below sound too complicated or like too much work, check out the new AI Marker Editor resource instead of messing with all of that. The AI Marker Editor resource is a GUI based editor to help simplify creating, editing and managing your spawn markers. If you install this resource, you can forgo steps 6-9 in the above instructions by pressing "alt a" before step 5 and using the editor.

Also check here for a bug fix update (these fixes are already in the T3D version download).



DYNAMIC VARIABLES

NOTE: If you don't want to override a variable's default value, just don't add that variable to the marker. 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	path name
respawn		"true" or "false"
range		"ranged"
doesReturn	"guard"
block		datablock name
Weapon		weapon name
spawnGroup	number


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 ones with assault rifles and other small arms. If you don't wish for a mob to be considered as ranged, don't add this variable to the marker.


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".


block
A few things need to be set up before you can use different datablocks (characters for your AI). First, the model has to exist under data/shapes with all the necessary .dsq and .dts files. Second 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.


Weapon
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.



SPAWNING BY TRIGGER

spawnGroup
A bit of setup is needed to spawn by a trigger. First, you have to place a triggerObject down. You can name it whatever you want, but you have to specify a few things:
* dataBlock = "guardTrigger";
* spawnGroup = "number";

You have to specify a spawnGroup for each trigger, and then add a matching spawnGroup parameter on each AIPlayerMarker you want to spawn in that group. Once that's done, comment out AIPlayer::LoadEntities(); in game.cs and you should be good to go.



CHANGE LOG
* August 27, 2008: Added the doesReturn dynamic variable and another line of sight improvement.
* October 9, 2008: Further improved the bots ability to turn corners.
* October 14, 2008: Fixed the premature firing bug and changed a few of the default values.
* October 15, 2008: Modified the item retrieval functions a little.
* November 3, 2008: Commented out the premature firing bug fix I made on October 14. While it did fix that bug, it could cause another (worse) firing bug.
* November 4, 2008: Re-fixed the premature firing bug in a slightly different manner so that it should no longer cause other problems.
* November 12, 2008: Fixed a different kind of premature firing bug.
* November 20, 2008: Added the changes posted in the comments by Rex (originally written by Dale Harper).
* November 21, 2008: Fixed a rare pacing bug.
* November 28, 2008: Minor change to one of the movement functions.
* January 16, 2009: Fixed an error in the usage instructions. The dynamic variable that I previously had listed as 'weapRange' should have just been 'range' (this is now corrected).
* September 23, 2009: Posted the final update I will be making for this resource.
* November 29, 2009: I am no longer giving any support for this resource at all.
* August 28, 2010: Added the T3D port of this resource.



If you enjoy using this resource, you should consider purchasing The Universal AI Starter Kit. It has been updated with many new features, bugs fixes and performance enhancements, along with better documentation, an easier installation process, and the code has been more modular and customizable. Check The UAISK's product page or my blogs for more information.

About the author

Developer of The Universal AI Starter Kit, which is available in the Add-ons store on this site. Also working on the game Twisty's Asylum Escapades.

Page«First 3 4 5 6 7 8 9 Next»
#161
03/19/2010 (12:35 am)
Thank you kindly, for your response.

Unfortunately the advice what you provide for me, doesn’t work. Putting variables of the AImarker, from PathMarker, did not help, also I did put whole new StaticShape() code outside of “Path 1”, still bot act like he is not aware of the path at all. Is it anywhere in the code, of “AIplayer.cs”, that I should mention something like “Path1”? I mean where is this variable which is deciding about when Bot, is on the Path and when not? I just give here example of code what I tried did to make it work in AIplayer.cs:

function AIPlayer::followPath(%this, %path, %node, %obj)
{
   //3575.followpath("MissionGroup/Paths/Path1", -1)
   
   //Start the bot following a path
   %this.stopThread(0);
   //Check if the bot is pathed
   if (!isObject(%path))
   {
      %this.path = "MissionGroup/Paths/Path1";
      return;
   }

   %dist = 0;
   %tempdist = 0;
   %index = -1;
   %botpos = %this.getposition();
   %count = %path.getCount();
   //Cycle through all nodes on this path and set the closest node as the bot's current location
   while ((%node = %count) != 0)
   {
     	%nodepos = %this.path.getObject(%count - 1).getposition();
		%tempdist = vectorDist(%nodepos, %botpos);

		if(%tempdist < %dist || %dist == 0)
		{
			%dist = %tempdist;
			%index = %node;
		}
     	%count--;
   }
   %index = %index - 1;
   %this.moveToNode(%index);

   if (%index > %path.getCount() - 1)
   {
      %this.targetNode = %path.getCount() - 1;
   }
   else
   {
      %this.targetNode = %index;
   }
}

#162
04/20/2010 (11:36 pm)
Hi Romek,

You might need to check if your AI's PlayerData datablock name is the same with the namespace of the functions in AIPlayer.cs (onReachDestination & onDamage).
#163
05/16/2010 (11:26 pm)
Regarding to this resource, does anybody have idea where is the all code, for FOV? I mean I want use my own code for AI, but I would like to steal a part, which is telling to bot where to look, and making him see only 90 degrees (simply so can sneak behind the bot and "knife him"), Ive look in forums, and I havent found anything yet. So if somebody can pull from this resource and past in here, I would appriciate a lot.
#164
12/30/2011 (6:21 pm)
Apologies - wrong thread. /facepalm
#165
03/21/2012 (12:58 pm)
So far, this has definitely been one of the best resources I've seen for AI in Torque with no engine changes. I've done a lot in TGE, and I am very new to T3d, so I do need a bit of help.

For some reason, I can't seem to give my bots the Lurker or the Ryder. I can give them the rocketlauncher, proxmine and even turrets, but when I try to give them the Lurker or the Ryder, the gun doesn't even appear in their hands. It's like I'm naming them incorrectly or something.

Could it be that these weapons have clips and function differently than the other weapons? Any help would be greatly appreciate, as I am at a loss!
#166
03/22/2012 (2:26 pm)
Link to download is dead :/
Edit: the 1.5.2 download that is ;)
Edit edit: No link anyways i was mistaken.
#167
03/23/2012 (5:13 am)
I used the T3D resource and it seems to be working well in TGE with just minor changes :) Anything i should be aware of that might be causing trouble later?
Page«First 3 4 5 6 7 8 9 Next»