   AI and understanding novel scripted spells
  I'm debating how to handle the coding of spell effects in a game that I'm designing.The initial concept was for all spells and abilities to be defined in a data file containing each spell's basic properties, as well as simple scripts for their actual effects.Now these would indeed be very simple scripts, since all of the real code is done inside the engine itself, which exposes a pretty complete API for every basic kind of spell effect. Scripts basically just string together simple actions, perhaps using some simple logic.For example:doDamage(target, FIRE, 10);doDamage(target, COLD, 15);addEffect(target, EFFECT_CHILL, 10);if (target.type == UNDEAD)  doDamage(target, HOLY, 50);if (target.hasEffect(POISON)){  doAoEDamage(target, POISON, 3, target.getEffect(POISON).power * 5);  removeEffect(target, POISON, 0);  }  However, I've come to realize that having the combinations of effects for all spells be contained in scripts would make it hard for the AI to actually figure out what each of the spells does, and thus, when to use them.Even for the simplest of attack magic, the type of elemental damage is does is specified in the script. Even a relatively primitive AI should be able to choose appropriate damage types to exploit enemy weaknesses (or at the very least, not continuously cast things a target is immune or highly resistant to), if they have the spells available to do so.And for more complex spells, like the poison explosion example above, they don't even do anything useful unless certain conditions are met. But how could an AI understand this if they only have the script to look at? Even in a very restricted case like this, I imagine writing an algorithm to determine the function of arbitrary code is hugely complicated (and seriously impractical). Or is there some more simple way to parse the basic information out of a script that I may be overlooking?Considering that most spells have fairly simple combinations of base effects, I considered implementing spell behavior as a list of Effect classes, which would contain members for effect type, and arguments for the basic fields of this effect type. Certainly, for the most basic cases, this makes things more straightforward. The AI can see that a spell has an Effect with type = doDamage, and element = fire attached to it, and understand how to use the spell. For spells with more than one effect, it can examine each of them in turn, just as easily. However, specifying even simple logic in a system like this could get really painful to implement, I think. Basic restrictions, based on target type (ie: friendly/enemy, undead/humanoid/etc.) or buffs/debuffs are probably simple enough, but more complex ones, such as "target has less health than caster" might need to be hardcoded on a case-by-case basis. Or, heck, even allowing basic arithmetic in spell parameters. Basically, I can see this easily ballooning into a monster to write and use.Another way of handling this, I suppose, is to specify information in each spell's definition that suggests when and how an AI should use it. This is probably the 'easiest' way to handle this problem, although I'm not certain exactly what form these AI hints would take. I suppose it could vary in complexity from simple category suggestions (ie: "This is an attack spell" vs. "This is a buff spell"), to a bunch of weights based on certain conditions (ie: "Target is at low health: +5", "Target is weak against fire: +10", "Target is within melee range of caster: +2"). This latter method would allow some interaction between both a spell itself, and the AI of the caster, since not all creatures which possess the same abilities will favor the same tactics. The caster AI could have parameters that are multiplied against certain classes of weights specified in the spell.I've never actually sat down to write any AI before, in case it isn't abundantly obvious by this point. I wonder if anyone has any thoughts on the general issue? Have any of you dealt with similar problems in games that you've worked on?   
  Making learning or adaptive AI for complex games like RPG isn't really possible. Esp when it invokes something like spells (which have so many non-local affects), what people usually do is create a "play book" of strategies for the AI and it just picks from those which apply. These playbooks are human solved strategies for enemies using spells which the designer has come up with overtime. That's the same technique people use for RTS and other complex games.Good Luck!-ddn;  The best way I can think of to test it out would be to give the AI the ability to test a script before using it. Say the AI has 5 different spells they can choose from, run through those 5 scripts and work effect they would have on their target, then choose the script that returns with the highest intended effect.For an example, your mage is attacking an undead creature and has a choice between fireball and iceball. Ice works poorly against undead enemies, so the test-script returns a damage of 5. Fire, however,  is very effective against undead creatures, and returns 30. Since the fireball got the best intended damage amount, the AI chooses to execute the fireball script.   ;  You can add some meta data about the spell so the AI easier understand the effects.For example:set_property("damage_type", FIRE);set_property("mana_drain", LOW);set_property("damage", LOW);doDamage(target, FIRE, 10);set_property("damage_type", COLD);set_property("mana_drain", LOW);set_property("damage", MEDIUM);doDamage(target, COLD, 15);addEffect(target, EFFECT_CHILL, 10);set_property("damage_type", HOLY);set_property("mana_drain", LOW);set_property("damage", HIGH);if (target.type == UNDEAD)doDamage(target, HOLY, 50);set_property("damage_type", POSION);set_property("mana_drain", HIGH);set_property("damage", HIGH);set_property("area_of_effect", LARGE);if (target.hasEffect(POISON)){doAoEDamage(target, POISON, 3, target.getEffect(POISON).power * 5);removeEffect(target, POISON, 0);}Then in theory, if the AI has little mana left it casts low mana spells or  many targets are nearby picks an spell with area of effect. It could also query for the targets greatness weakness and choose an appropriate spell.