the most basic way to know who is the caster and who is the target is using a. or b. in the damage formulas (but that you probally know)
-----------------------------------
commons events execute after damage calculation, but you can use yanfly skillcore and action sequence pack to make a custon executions.
-----------------------------------
use yanfly damage control. Then in the plugin damage step formula config you place something like this: (try to place deep down to not get errors):
if (b.isActor()) {v[11] = target.actorId();} else {v[11] = 5;} // obs: it will become part of the global formulain this case i used v[11] to store the actor ID in case he is a target (that is because the normal attack in my game executes a common event everytime someone attacks and the common event have a conditional on it, it will execute everytime v[11] is different from 5, in other words, everytime a actor is attacked), other than that you have to select the the target of your modifications in the common event as the variable you stored the ID instead of selecting a actor, unless you want to do a lot of conditionals... (for example: before you would do a event that would give Haroldo a ability, but now you will create a event that will give a actor with a ID = v[11] a hability)
------------------------
To get the index of a especific monster you use the function
index(); and to target that monster later you use it on script calls:
https://docs.google.com/spreadsheets/d/1-Oa0cRGpjC8L5JO8vdMwOaYMKO75dtfKDOetnvh7OHs/edit?pref=2&pli=1#gid=0
example: this will transform who you attack to the monster with ID 3 id you store the last attacked monster index on variable 14
var inde = $gameVariables.value(14);$gameTroop.members()[inde].transform(3);to get the enemy Id you use: (it will tell you what the enemy is not the id for targeting it)
a.enemyId() and b.enemyId() or(for yanfly) target.enemyId() and user.enemyId()------------------------
as for spells it's only once, but the "target" will be the entire group,
i think that is === null, not sure, but if that is the case you can make a conditional for it.
i tested some things and couldn't figure out how to detect it :/ (it uses some random actorId as value)