In the game I am trying to make, there will be a number of different types of objects, some harmless, some friendly, others hostile, etc. But what I want to do is use some kind of arrangement that allows targeting of objects based upon their allegiances. So for example, if you become a certain characters friend, it may make all of that characters enemies your enemies. Likewise, these enemies will automatically be detected as hostile.
So I would basically need a group of variables that declare each units friendliness of hostility to other objects in the scene, and also a variable control that will declare each units government allegiances. A script should run and find all these links and then set up a list of viable targets and ignore units that are determined to be friendly. These units should then be capable of searching out and destroying any and all hostile targets.
In the case of AI’s much of this capability would be included into the AI scripts, and with human players, these targets are loaded into a list of available targets that the player can cycle through. The same methods would be also employed with determining what kind of targets the unit should prioritize on, e.g. units of a certain class, etc.
So I guess my question is how exactly would I go about this. I’m not asking anyone to write these scripts, but some pointers on how to go about this. Methods I am thinking of include maybe tags, or perhaps attaching scripts that contain variables that can be set to either true or false, or perhaps numerical values (in which case a value less that a certain amount is ignored such that some groups are more easily provoked than others)
The system would have to be set up in such a way that if a new object is to enter the scene, its information is broadcast, and the targeting system is reevaluated.
The best thing to do would have a variable in your “Character” (whatever you decide to call it) script, which would probably be nothing more complicated than an integer. For neutral “critters” you could use some flag value like -1, and for each team you assign a value.
When it comes time to compile lists of friendlies/enemies you can use FindObjectsOfType(Character) to get all of them, and loop through them.
function GetEnemies() : Array {
var allCharacters : Array = FindObjectsOfType(Character);
var enemies : Array = new Array();
for (c=0;c<allCharacters.length;c++) {
if (allCharacters[c].groupID != groupID) enemies.Add(allCharacters[c]);
}
return enemies;
}
Well, I guess what I am wanting is a bit more complicated than that. Basically, each team has a set group of enemies, plus characters, such as the player that can change allegiance through he course of the game. So basically, every time you attack one groups enemies, you become more loved by the one team and more hated by the other. This effect can be undone by attacking former allies and eventually, the previous enemies will view you as a friend (though you would be stuck in a period with no friends for a little while).
So I was thinking something more alone the lines of a friendliness value, where say positive values are friendly toward you and negative values are hostile toward you. So you have a group of AI enemies where their hatred, or lack thereof is hard coded, by your friendliness or lack thereof can change. The script you showed above might work, but the criteria just need to be more complex. Also, should this script be attached to each unit, or should it be attached to a global object that sets the targeting information, or a combination of both?
Just looking at that script, I think it will work, but I may need to make the types of targets that can be selected a dynamic list, and make some other script that controls what kind of targets meet these criteria.
This sounds like a job for a Bayesian network, but the topic is too big to describe on the forum. There is a brief introduction to it in Rabin et al’s book “AI Game Programming Wisdom” (vol I) and other books cited on Wikipedia and elsewhere.
Basically, it’s a technique where the degree of confidence in a property (such as “friendliness”) is modelled as a probability based on available evidence. In your game, the evidence might be the main character’s recent actions plus the current friendliness of other non-player characters. The network allows the evidence to change over time and can accommodate situations like new characters joining the game, etc.