So I’m trying to make each object assigned a clan tag and have a relationship between each tag and the others. So for example object 1 is assigned to clan 1 and object 2 is assigned to clan 2. If they are in distance from each other they check the relationship between their clans (friendly,neutral, or hostile) then act upon the result of the relationship.
Now my question is how can I map this relationship efficiently;
The first idea that comes to mind is just manually set the relationship for every clan with the rest. The problem with this idea that it’s not maintainable if the number of clans increases.
The second idea is to automatically loop over the list and match every clan with the rest. (default neutral, and adjusted later on) this seems the most likely outcome. But this still has some issues that I’ll have to maintain two values each time I have to adjust the relationship of two clans since they are set as independent list. This is susceptible to errors.
So is there any better way to create a relationship table between all the element of a list (clans) where I don’t have to create duplicates. other ideas are welcome to discussion as well
I haven’t done anything similiar so far and I obviously don’t know the scope of your project so I don’t know if this fits your needs but shouldn’t something along these lines do what you need:
List<Clan> allClans = GetAllClans (); // could be implemented in many different ways
float[,] clanRelations = new float[allClans.Count, allClans.Count];
for (int i = 0; i<allClans.Count-1; i++) {
for (int j = i+1; j<allClans.Count; j++) {
clanRelations[i,j] = CalculateRelationValue (allClans [i], allClans [j]);
clanRelations[j,i] = CalculateRelationValue (allClans [i], allClans [j]);
}
}
return clanRelations;
Yes, this does store the relation between Clans twice but that maybe that’s a good thing. Two Clans don’t need to have the exact same impression of their relationship with each other. For example, if a float value of -1 stands for “as bad as possible” and a value for 1 for “as good as possible”, and let’s assume Clan A buys something they need from Clan B on a regular basis. Then the reation value for (A; B) could be 0.7 because Clan A is really thankful that Clan B provides them with the desired product. But the value for (B; A) could be around 0.5, because although Clan B appreciates the trade with Clan A they could also sell their goods to an other Clan if necessary.
If you want both Clans to have the same impression of their relationship, you might want to create a new data type and do something like this:
List<Clan> allClans = GetAllClans (); // could be implemented in many different ways
List<ClanRelation> clanRelations = new List<ClanRelation> ();
for (int i = 0; i<allClans.Count-1; i++) {
for (int j = i+1; j<allClans.Count; j++) {
clanRelations.Add (new ClanRelation (allClans [i], allClans [j]));
}
}
return clanRelations;
As mentioned before, though, that#s nothing I’ve used myself. It’s just what popped into my mind when reading your question so use it with care.
Edit: Both approaches are meant to keep track of all relationship at all time. If you don’t need that you could of course also just write an algorithm to evaluate the relationship and call it like
public class Clan {
public float EvaluateRelationshipWith (Clan other) {
// think of a way to evaluate the relationship
}
}
Thank you for your answer,
I decided to go with a 2d matrix approach similar to yours, since most of repeated operations are going to be checking rather than setting values.
Still thinking about making different relation between two clans, since if combat is included, if one clan is hostile while the other is neutral I’ll have to add a case of stay neutral until provoked/attacked.