AI relation, using arrays...

Hello

So I started trying to make a pretty smart ai that you can open diplomatic relations with, such as trade, marry your daughter and more. This would increase your relation with that faction. It all worked well but when i tried to add more factions and make them have relations between eachother and still keep track of it. Ye i failed. I tried using arrays and index for each faction and an array of intagers for the relations (0 is neutral, 100 is very likable, -100 is enemy). But it was very difficult to keep track of it all and at the same time focus on the relation to the player.

How is the ideal way to deal with this? Reducing the amount of factions would maybe work but i only tried with 9 and that shouldn’t be so hard right?

Any help is welcome.
~Flux

So I think you can do exactly what you’re talking about using an array and an index for each faction. Here’s an example implementation:

// the RelationshipInfo class can store properties of the relationship
public class RelationshipInfo 
{
    public int RelationshipScore;

    public bool HasMarriage; // just an example of a relationship property

    public bool IsAlly 
    {
        get { return this.RelationshipScore > 50; } // another example of a relationship property
    }
}

public class Faction
{
    // just an example helper method when you want to work with relationships more easily.
    public RelationshipInfo GetRelationshipWith(Faction otherFaction)
    {
        return RelationshipManager.GetRelationshipInfo(this, otherFaction);
    }

    // ... other faction code
}

public static class RelationshipManager
{
    // here, we're storing relationships in a 2D array. 
    // with tons of factions, this may blow up in memory usage, but it should easily handle the 9 you mentioned.
    private static RelationshipInfo[,] relationships;

    // each faction will get an index in the array
    private static Dictionary<Faction, int> factionIndexes;

    // this method must be called at the start of your game to initialize the faction manager
    public static void Initialize(ICollection<Faction> factions)
    {
        relationships = new RelationshipInfo[factions.Count, factions.Count];
        factionIndexes = new Dictionary<Faction, int>(factions.Count);

        int i = 0;
        foreach(var faction in factions)
        {
            factionIndexes[faction] = i;
            i++;
        }
    }

    public static RelationshipInfo GetRelationshipInfo(Faction faction1, Faction faction2) 
    {
        var faction1Index = factionIndexes[faction1];
        var faction2Index = factionIndexes[faction2];

        var relationshipInfo = relationships[faction1Index, faction2Index];
        if(relationshipInfo == null)
        {
            relationshipInfo = new RelationshipInfo();
            relationships[faction1Index, faction2Index] = relationshipInfo;
        }

        return relationshipInfo;
    }
}