Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsCog Forum → Friendly AI
Friendly AI
2002-10-23, 12:00 PM #1
OK, I have tried to make friendly AI both with the 06_max.cog / 06_maxstart.cog (as shown in the 'Friendly Baddies and Faithful Companions'tutorial) and also with the one that you can download from the cog section of this site. I have had 2 problems:
1) When there are several of them they attack each other!
2) More importantly, they (or one on its own) will not attack enemies.
Can you help me out?
P.S. This info might help. On my level I am Darth Vader, the AI are stormtroopers and the enemy are a mix of pirates and other aliens. I intend to release the AI from a prison cell and hopefully the will kill the enemies in that sector and although not as important the will hopefully follow me around the level as they do with the cogs I have tried.
2002-10-23, 1:14 PM #2
Here is a cog for the friendlies, but if you want the enimies to attack your friendlies, then I'll need to get another cog made. Also you have to add the rest of the actor cog. Don't change a word of mine, except maybe the killed message. Otherwise, just add. [http://forums.massassi.net/html/wink.gif]
Code:
# Jedi Knight COG Script
#
# ACTOR_FRIENDF.COG
#
# A cog for a friendly actor.
# Kinda like Max, just better.
# Class cog/follow player version.
#
# [DP]
#

symbols

thing		friend				local
thing		player		nolink	local
thing		target				local
int		count=0				local

message	created
message	killed
message	pulse

end

# ========================================================================================

code

Created:
	player = GetLocalPlayerThing();
	SetThingPulse(GetSenderRef(), 1);
	AIClearMode(GetSenderRef(), 0x1206);
	Return;

# ........................................................................................

Killed:
	If(GetThingClassCog(GetSenderRef()) != GetSelfCog()) Return;
	SetThingPulse(GetSenderRef(), 0);
	AIClearMode(friend, 0x1206);
	AISetMode(friend, 0x1);
	Return;

# ........................................................................................

Pulse:
      count = 0;
	friend = GetSenderRef();
	If(AIGetMode(friend) == 0x202) Return;		// So it keeps on the same target
	target = FirstThingInView(friend, 170, 12, 0x4);
	If((GetThingClassCog(target) == GetSelfCog())||(target == player)) target = -1;
	While((target == -1)&&(count < GetThingCount()))
	{
		target = NextThingInView();
		If(count == GetThingCount()) target = -1;
		If((GetThingClassCog(target) == GetSelfCog())||(target == player)) target = -1;
		count = count + 1;		// Incase there isn't anything in view
	}
	If(target != -1)		// If there's a target
	{
		CaptureThing(target);
		AISetFireTarget(friend, target);
		AISetMoveThing(friend, target);
		AISetLookPos(friend, GetThingPos(target));
		AISetMode(friend, 0x202);
		AIClearMode(friend, 0x1000);
	}
	Else		// If not, go to player
	If((VectorDist(GetThingPos(player), GetThingPos(friend))) > .5)
	{
		call followplayer;
		Return;
	}
	Else AIClearMode(friend, 0x1206);
	Return;

# ........................................................................................

FollowPlayer:
	AISetMoveThing(friend, player);
	AISetLookPos(friend, GetThingPos(player));
	AISetMode(friend, 0x1);
	AIClearMode(friend, 0x1206);
	Return;

end
You must credit me for this cog. Both in this cog and in the readme. This is one of my special cogs.

------------------
The Sniper Missions. Current project, The Sniper Missions

The Magician Saber System.

[This message has been edited by Descent_pilot (edited October 23, 2002).]
Major projects working on:
SATNRT, JK Pistol Mod, Aliens TC, Firearms

Completed
Judgement Day (HLP), My level pack
2002-10-24, 12:29 PM #3
Thanks alot. Yes I would like enemies to attack my AI. I was hoping that I would be able to release them from a cell and let them battle it out with some bad guys. Oh yeah and are your initials enough to count as crediting the cog to you. In other words do I have to add anything to what you have already put?
2002-10-24, 1:14 PM #4
OK, I thought I understood what you were on about but it didn't work so I could probably do with more help. I just added copied each section from actor_st_m.cog and pasted into friendlyAI.cog (The code you gave me above) under ther correct sections eg. symbols under symbols. But when I add the cog it gives me loads of boxes to fill in with strange titles like "(unknown)" or part of the code from the cog. Can you help me. I'm still a beginner when it comes to cogs.
2002-10-24, 1:16 PM #5
Here is how it looks:

# Jedi Knight COG Script
#
# ACTOR_FRIENDF.COG
#
# A cog for a friendly actor.
# Kinda like Max, just better.
# Class cog/follow player version.
#
# [DP]
#

symbols

thing friend local
thing player nolink local
thing target local

int count=0 local

message created
message killed
message pulseend

message killed
message damaged
message skill
message timer

int oldAIMode local
int isFrozenNow=0 local
flex extraFreezeTime=0.0 local
template powerup=+DSTrifle local
template limb=+stormlimb local
int newThing local
int victim local
int bin local
int senderref=-1 local
ai flee_ai=noweapon.ai local
int damage local
vector currentpos local

# ========================================================================================

code
Created:
player = GetLocalPlayerThing();
SetThingPulse(GetSenderRef(), 1);
AIClearMode(GetSenderRef(), 0x1206);
Return;
# ........................................................................................
damaged:
damage = GetParam(0);
if(GetParam(1) == 16) // saber damage might cause dismemberment
{
damage = (damage * 2.0);
victim = GetSenderRef();
if(GetThingHealth(victim) <= damage) // but only if damage is sufficient to kill
{
if(rand() < 0.05) // random chance
{
AmputateJoint(victim, 5);
newThing = CreateThingAtPos(limb, GetThingSector(victim), GetThingPos(victim), '0 0 0');
SetThingVel(newThing, '0.0 0.7 1.4');
}
}
}
ReturnEx(damage);
return;

# ........................................................................................

killed:
victim = GetSenderRef();

if(GetActorWeapon(victim, 1) != -1)
{
AmputateJoint(victim, 3);

newThing = CreateThing(powerup, victim);
SetLifeleft(newThing, 200.0);
}

return;

# ........................................................................................

Killed:
If(GetThingClassCog(GetSenderRef()) != GetSelfCog()) Return;
SetThingPulse(GetSenderRef(), 0);
AIClearMode(friend, 0x1206);
AISetMode(friend, 0x1);
Return;

# ........................................................................................

skill:
bin = GetParam(0);
if(bin == 24) // Force Pull
{
// he's a fighter, not a fleer... -srs

senderref = GetSenderRef();

// If he hasn't been pulled yet...
if (GetActorWeapon (senderref, 1) != -1)
{
SetActorWeapon(senderref, 1, -1);

newThing = CreateThing(powerup, GetSenderRef());
SetLifeleft(newThing, 30.0);

AISetClass (senderref, flee_ai);
AISetMode(senderref, 0x4);

ReturnEx(newThing);
}
else
{
// Give the actor a shove towards the player.
currentpos = VectorSub(GetThingPos(GetLocalPlayerThing(), senderref));
currentpos = VectorScale(VectorNorm(currentPos), 250);
ApplyForce(senderref, currentpos);

ReturnEx(-1);
}

//AIFlee(senderref, GetLocalPlayerThing());

Return;
}
else
if(bin == 31) // Force Grip
{
ReturnEx(10); // return base damage that is taken by this actor.
Return;
}
else
if(bin == 34) // Deadly Sight
{
ReturnEx(10); // return base damage that is taken by this actor.
Return;
}
// else
// if(bin == 1035) // Carbonite
// {
// if (!isFrozenNow)
// {
// senderref = GetSenderRef();
// victim = GetParam(2);
//
// isFrozenNow = 1;
// extraFreezeTime = 0.0;
//
// oldAIMode = AIGetMode(victim);
// AISetMode(victim, 0x2000); // Disable the actor's AI.
// AISetMoveThing(victim, victim); // Set the thing as it's own destination so it stops moving.
//
// SetTimerEx(GetParam(1), 1035, victim, 0); // Make an appointment for "Unthawing"
// }
// else
// {
// extraFreezeTime = extraFreezeTime + GetParam(1);
// }
//
// ReturnEx(0);
// return;
// }

ReturnEx(-1);
return;

# ........................................................................................

timer:
senderref = GetSenderRef();

if(GetSenderId() == 1035) // Thaw out
{
victim = GetParam(0);

if (!extraFreezeTime)
{
AIClearMode(victim, 0xFFFFFFFF);
AISetMode(victim, oldAIMode); // Reinstate the actor's AI.
isFrozenNow = 0;
}
else
{
// We've been hit more than once, so we need to extend the freeze time.
SetTimerEx(extraFreezeTime, 1035, victim, 0); // Make an appointment for "Unthawing"
extraFreezeTime = 0.0;
}
}
Return;

# ........................................................................................

Pulse:
count = 0;
friend = GetSenderRef();
If(AIGetMode(friend) == 0x202) Return; // So it keeps on the same target
target = FirstThingInView(friend, 170, 12, 0x4);
If((GetThingClassCog(target) == GetSelfCog())||(target == player)) target = -1;
While((target == -1)&&(count < GetThingCount()))
{
target = NextThingInView();
If(count == GetThingCount()) target = -1;
If((GetThingClassCog(target) == GetSelfCog())||(target == player)) target = -1;
count = count + 1; // Incase there isn't anything in view
}
If(target != -1) // If there's a target
{
CaptureThing(target);
AISetFireTarget(friend, target);
AISetMoveThing(friend, target);
AISetLookPos(friend, GetThingPos(target));
AISetMode(friend, 0x202);
AIClearMode(friend, 0x1000);
}
Else // If not, go to player
If((VectorDist(GetThingPos(player), GetThingPos(friend))) > .5)
{
call followplayer;
Return;
}
Else AIClearMode(friend, 0x1206);
Return;

# ........................................................................................

FollowPlayer:
AISetMoveThing(friend, player);
AISetLookPos(friend, GetThingPos(player));
AISetMode(friend, 0x1);
AIClearMode(friend, 0x1206);
Return;
end
2002-10-24, 3:21 PM #6
Code tag's people! Like this.
Code:
# Jedi Knight COG Script
#
# ACTOR_FRIENDF.COG
#
# A cog for a friendly actor.
# Kinda like Max, just better.
# Class cog/follow player version.
# Slightly modified for IHateC3PO's use.
#
# [DP]
#

symbols

thing		friend				local
thing		player		nolink		local
thing		target				local

int		count=0				local

message		created
message		pulse
message     	killed
message     	damaged
message     	skill
message     	timer

int         	oldAIMode                        local
int         	isFrozenNow=0                    local
flex        	extraFreezeTime=0.0              local
template    	powerup=+DSTrifle                local
template    	limb=+stormlimb                  local
int         	newThing                         local
int         	victim                           local
int         	bin                              local
int         	senderref=-1                     local
ai          	flee_ai=noweapon.ai              local
int         	damage                           local
vector      	currentpos                       local
end
# ========================================================================================

code
Created:	
	player = GetLocalPlayerThing();	
	SetThingPulse(GetSenderRef(), 1);	
	AIClearMode(GetSenderRef(), 0x1206);	
	Return;
# ........................................................................................
damaged:
   damage = GetParam(0);
   if(GetParam(1) == 16)                     // saber damage might cause dismemberment
   {
      damage = (damage * 2.0);
      victim = GetSenderRef();
      if(GetThingHealth(victim) <= damage) // but only if damage is sufficient to kill
      {
         if(rand() < 0.05)                   // random chance
         {
            AmputateJoint(victim, 5);
            newThing = CreateThingAtPos(limb, GetThingSector(victim), GetThingPos(victim), '0 0 0');
            SetThingVel(newThing, '0.0 0.7 1.4');
         }
      }
   }
   ReturnEx(damage);
   return;

# ........................................................................................

Killed:	
	If(GetThingClassCog(GetSenderRef()) != GetSelfCog()) Return;	
	SetThingPulse(GetSenderRef(), 0);

   If(GetActorWeapon(victim, 1) != -1)
   {
      AmputateJoint(victim, 3);

      newThing = CreateThing(powerup, victim);
      SetLifeleft(newThing, 200.0);
   }
	
	AIClearMode(friend, 0x1206);	
	AISetMode(friend, 0x1);	
	Return;

# ........................................................................................

skill:
   bin = GetParam(0);
   if(bin == 24)                             // Force Pull
   {
      // he's a fighter, not a fleer... -srs

      senderref = GetSenderRef();

			// If he hasn't been pulled yet...
		if (GetActorWeapon (senderref, 1) != -1)
		{
			SetActorWeapon(senderref, 1, -1);

			newThing = CreateThing(powerup, GetSenderRef());
			SetLifeleft(newThing, 30.0);

			AISetClass (senderref, flee_ai);
			AISetMode(senderref, 0x4);

			ReturnEx(newThing);
		}
		else
		{
				// Give the actor a shove towards the player.
			currentpos = VectorSub(GetThingPos(GetLocalPlayerThing(), senderref));
			currentpos = VectorScale(VectorNorm(currentPos), 250);
			ApplyForce(senderref, currentpos);

			ReturnEx(-1);
		}

      Return;
   }
   else
   if(bin == 31)                             // Force Grip
   {
      ReturnEx(10);                          // return base damage that is taken by this actor.
      Return;
   }
   else
   if(bin == 34)                             // Deadly Sight
   {
      ReturnEx(10);                          // return base damage that is taken by this actor.
      Return;
   }

   ReturnEx(-1);
   return;

# ........................................................................................

timer:
	senderref = GetSenderRef();

	if(GetSenderId() == 1035)	// Thaw out
	{
		victim =	GetParam(0);

		if (!extraFreezeTime)
		{
			AIClearMode(victim, 0xFFFFFFFF);
			AISetMode(victim, oldAIMode);			// Reinstate the actor's AI.
			isFrozenNow = 0;
		}
		else
		{
				// We've been hit more than once, so we need to extend the freeze time.
			SetTimerEx(extraFreezeTime, 1035, victim, 0);	// Make an appointment for "Unthawing"
			extraFreezeTime = 0.0;
		}
	}
	Return;

# ........................................................................................

Pulse:      
	count = 0;	
		friend = GetSenderRef();	
		If(AIGetMode(friend) == 0x202) Return;		// So it keeps on the same target	
		target = FirstThingInView(friend, 170, 12, 0x4);	
		If((GetThingClassCog(target) == GetSelfCog())||(target == player)) target = -1;	
		While((target == -1)&&(count < GetThingCount()))	
		{		
			target = NextThingInView();
			If(count == GetThingCount()) target = -1;		
			If((GetThingClassCog(target) == GetSelfCog())||(target == player)) target = -1;		
			count = count + 1;		// Incase there isn't anything in view	
		}	
		If(target != -1)		// If there's a target	
		{		
			CaptureThing(target);		
			AISetFireTarget(friend, target);		
			AISetMoveThing(friend, target);		
			AISetLookPos(friend, GetThingPos(target));		
			AISetMode(friend, 0x202);		
			AIClearMode(friend, 0x1000);	
		}	
		Else		// If not, go to player	
		If((VectorDist(GetThingPos(player), GetThingPos(friend))) > .5)	
		{		
			call followplayer;		
			Return;	
		}	
		Else AIClearMode(friend, 0x1206);	
		Return;

# ........................................................................................

FollowPlayer:	
	AISetMoveThing(friend, player);	
	AISetLookPos(friend, GetThingPos(player));	
	AISetMode(friend, 0x1);	
	AIClearMode(friend, 0x1206);	
	Return;
end
1st off, use code tags please, makes it so much easier to read.
2nd - can't have 2 killed messages.
3rd - get Parsec, I use it all the ime, good checker.
4th - watch for little things like pulseend, the ends can bite, hard.
5th - Any symbols that aren't used, delete. Any verbs in the code that are commented ( // or # ), delete. That goes for whole sections of code. Any comments that you don't want, you can delete, but this may or may not make it look cleaner.

Sorry to sound like a teacher, but GBK doesn't do that anymore. [http://forums.massassi.net/html/tongue.gif]

------------------
The Sniper Missions. Current project, The Sniper Missions

The Magician Saber System.
Major projects working on:
SATNRT, JK Pistol Mod, Aliens TC, Firearms

Completed
Judgement Day (HLP), My level pack
2002-10-24, 4:59 PM #7
1 question. JK or MotS?

------------------
The Sniper Missions. Current project, The Sniper Missions

The Magician Saber System.
Major projects working on:
SATNRT, JK Pistol Mod, Aliens TC, Firearms

Completed
Judgement Day (HLP), My level pack
2002-10-25, 10:15 AM #8
Mots.
2002-10-25, 10:59 AM #9
Thanks for all this help your giving me. Got another problem though, with the max cog all I had to do was fill in the boxes to set them to the correct thing (ie. my stormtrooper). The boxes are without titles when I add this cog. Is there some other way that I'm supposed to link it to my actor?
2002-10-25, 11:55 AM #10
This is an actor cog, so it isnt used in JED. Alter the actor's template to refer to this cog. Do this by loading the "master.tpl" file in your favorite text editor, searching for and finding your actor in the list, and modifying the "cog=xxx" entry. DO NOT use JED's "quick and dirty template creator" for this. Edit the file directly.

------------------
I used to believe that we only want what we can't have. I was wrong. The truth is, the wonderful truth is, we only want what we think we can't have.

GBK oNline. Cogs, tuts, and total w00tage.
And when the moment is right, I'm gonna fly a kite.
2002-10-25, 12:07 PM #11
MOTS!!!! AGGGGGG. Don't use this cog! Just change the alignment in the AI files (for each diffuculty) to 1, save it. All fine and dandy. I assumed this is for JK since your asking about friedly AI.

Rule 1 - never make assumtions.
Rule 2 - you should try to follow rule one a best as you can.

------------------
The Sniper Missions. Current project, The Sniper Missions

The Magician Saber System.
Major projects working on:
SATNRT, JK Pistol Mod, Aliens TC, Firearms

Completed
Judgement Day (HLP), My level pack

↑ Up to the top!