Army AI: Creating System for Changing Alliances (identifying enemy vs ally)


Working on an FPS war simulator type game where you fight along side one faction that fights multiple armies that will eventually change sides (ie enemy group now becomes your ally). Eventually I would like to have 20+ mini factions all with the ability to become allies/enemies with everyone else, changing during the game. Thus I would need some sort of ‘system’ for the AI to recognize who is friend of foe.

I’m guessing I would need some kind grid-matrix type deal that maybe holds some sort of binary data. So in script there would be line stated if the gameobjects’ ‘allegiance’ value is 407 (binary value 110010111) then it would identify X number of factions as enemy, Y number as neutral, Z number as ally, etc. With that said should alliances be tag driven?

I really would love to get this working but not even sure where to start or what the system would look like, any help is greatly appreciated! Thanks!

Here is how i would approach if i had to implement :-

To achieve this kind of feature you will need to have a jagged array and each faction needs to have an array where you will set to which faction they are friends and to which they are enemies.

I will explain in detail below :-(pls dont sleep while reading the long answer)

for example take 5 factions A,B,C,D,E

each faction is a friend of its own by default

0 - enemy
1- friend

Below is the faction matrix initialization.Say every one is everyone enemy .

A 1 0 0 0 0
B 0 1 0 0 0
C 0 0 1 0 0
D 0 0 0 1 0
E 0 0 0 0 1

Similarly if A is friends with B set the faction matrix as below

A 1 0 0 0 0
B 1 1 0 0 0
C 0 0 1 0 0
D 0 0 0 1 0
E 0 0 0 0 1

Notice BA position is set to 1 to denote friendship

similarly you need to set for each enemy or friend case.

Now the programming part :-

suppose you have a script for each faction -

Each faction you can have an array where you will be update the array to either friends or enemies with other factions


you will update this based on the function you write and call it when an event happens (ex:if both factions are <50 health and are next to each other make them friends)

Say A and B are enemies and when their health is <50 we update F1[1]=‘1’.

Now for the jagged array we need this for reference for use in multiplayer etc
jagged array is an array of arrays.

we store the arrays of factions F1,F2,F3,F4,F5 in FG as below

int [,] FG = new int[,] {{1,0,0,0,0},{1,1,0,0,0},{0,0,1,0,0},{0,0,0,1,0},{0,0,0,0,1}};

i suggest using this type of array as we don’t restrict the array size so if a faction left the game we can just drop the updation of that and reduce the load on each player CPU.

Here is a nice link for example of diffrent arrays.

Hope you will achieve what you wanted.

I like the “matrix” idea, but see no need to deal with bitmasks; it’s not going use up very much memory.

In the below (uncompiled/untested) example Factions are identified by an interger value and the total number of factions is defined by numFactions.

bool enemyMatrix[][] = new bool[numFactions-1][numFactions-1];  //this 2d array variable would be defined once in the scene, possibly in some-kind of "BattleManager" class, rather than in the players, or units.


bool AreEnemies(int Faction1, int Faction2)
   return enemyMatrix[Faction1][Faction2] ;

void SetAsEnemies(int Faction1,int Faction2)
   enemyMatrix[Faction2][Faction1]=true;  //should faction2 ALSO now consider faction 1 an enemy?  I'll assume so.
void SetAsAllies(int Faction1,int Faction2)

It’s all above my head. My enemy script simply has a public variable where I drag and drop “Player” so the enemy attacks me. Just to try it out I had a trigger zone that when I walked into it I simply changed that variable to someone else

using UnityEngine;
using System.Collections;

public class changeBadGuy : MonoBehaviour {
	public GameObject enemy;// the dude I want to be friends with
	public Transform newTarget;//the person my new friend attacks
	public MonoBehaviour attackScript;//my new friends attack script
	void Start () {
		attackScript = enemy.GetComponent<Attack>();//Attack is the name of his attack script

	void OnTriggerEnter(Collider other)
		if(other.tag == "Player")
		{ = newTarget;// didn't work sbut this did