Custom spell ptoblem

Howdy all - a 1.69 user here.

I’ve been trying to fashion a ranged cure/buff spell for a hostile-faction Githyanki healer, in the hope that the NPC will heal one of his buddies who has taken damage in combat with PCs.

As with all my attempts at scripting, it’s probably a complete mess, but the main issue as things stand is that the healer fires the spell at the PC instead of his wounded pal.

Below is the code (if you can call it that!) in its current form:

//::////////////////////////////////////////////////////////////////////////////
//:: Redactive Hand - Githyanki Redactor Ranged Healing Spell
//:: fof_psiheal.nss
//::////////////////////////////////////////////////////////////////////////////
//::
//:: Most damaged Githyanki Faction member gains 5 points of healing and gains
//:: a +3 increase to A.C. for 3 rounds.
//::
//::////////////////////////////////////////////////////////////////////////////

#include "nw_i0_spells"
#include "x2_inc_spellhook"

void main()
{
//  Spellcast Hook Code
    if (!X2PreSpellCastCode())
    {
    // If code within the PreSpellCastHook reports FALSE, do not run this spell
        return;
    }
// End of Spell Cast Hook

    //Declare major variables
    int nDuration = 3;
    int nToHeal = 10;
    effect eHeal;
    // Get most damaged member of own faction
    object oMostDamaged = GetFactionMostDamagedMember(OBJECT_SELF, TRUE);

    if(GetIsObjectValid(oMostDamaged))
    {

//  object oTarget = GetSpellTargetObject();

      if(GetIsReactionTypeFriendly(oMostDamaged))
      {
        //Fire cast spell at event for the specified target
        SignalEvent(oMostDamaged, EventSpellCastAt(OBJECT_SELF, 458, FALSE));

        if (!GetHasSpellEffect(GetSpellId(),oMostDamaged))
        {

        effect eAC1 = EffectACIncrease(3, AC_DODGE_BONUS);
        effect eVis = EffectVisualEffect(VFX_DUR_BIGBYS_INTERPOSING_HAND);
        effect eLink = EffectLinkEffects(eAC1, eVis);
        eHeal = EffectHeal(nToHeal);
        effect eCure = EffectVisualEffect(VFX_IMP_HEALING_S);

        //Apply the TO HIT PENALTIES bonuses and the VFX impact
        ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eLink, oMostDamaged, RoundsToSeconds(nDuration));
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oMostDamaged);
        ApplyEffectToObject(DURATION_TYPE_INSTANT, eCure, oMostDamaged);

        }
      }
    }
}

And here is the relevant line from spells.2da:

 Label                             Name     IconResRef      School Range VS   MetaMagic TargetType ImpactScript     Bard Cleric Druid Paladin Ranger Wiz_Sorc Innate ConjTime ConjAnim ConjHeadVisual  ConjHandVisual  ConjGrndVisual  ConjSoundVFX    ConjSoundMale    ConjSoundFemale  CastAnim CastTime CastHeadVisual CastHandVisual CastGrndVisual CastSound         Proj ProjModel        ProjType     ProjSpwnPoint ProjSound        ProjOrientation ImmunityType   ItemImmunity SubRadSpell1 SubRadSpell2 SubRadSpell3 SubRadSpell4 SubRadSpell5 Category Master UserType SpellDesc UseConcentration SpontaneouslyCast AltMessage HostileSetting FeatID    Counter1 Counter2 HasProjectile

458 Redactive_Hand 61530 is_Bless E S s 0x3a 0x2B fof_psiheal **** **** **** **** **** **** 5 1500 hand **** **** **** **** **** **** touch 1000 **** **** **** **** 1 vpr_bigby_o homing hand spr_aroacid path **** 1 **** **** **** **** **** 2 **** 1 61531 1 0 61532 0 **** **** **** 1

BTW I took the TargetType hex code from Lesser Mind Blank - a Ranged buffing spell.

So, if anyone would be kind enough to look at the above and point out what I’ve done wrong, I will be massively grateful.
Cheers,
PT

I’m probably the wrong person to try to answer any of this, but…

Where is this spell put?
What I’m thinking is: Maybe make it clear for the script which faction oMostDamaged belongs to?
Could that be the reason it fails?

Something like:

object oHealer = GetObjectByTag("githyankidudetag"); //Change to the tag of the healer
// Get most damaged member of own faction
    object oMostDamaged = GetFactionMostDamagedMember(oHealer, TRUE);

There are a lot of dependencies form the concrete situation. What if the MostDamagedFactionMember isn’t nearby? What Faction, hopefully not Standard_Faction_Hostile. Etc. The script has much stuff I don’t understand.

Following andgalfs approach this could be something like this

//#include "nw_i0_spells"
//#include "x2_inc_spellhook"

void main()
{
//  if (!X2PreSpellCastCode()) return;

  int nToHeal = 10, n = 0, max = 0, d = 0;
  object oMostDamaged;
  object o = GetObjectByTag("githyankidudetag", 0);
  while (GetIsObjectValid(o))
    {
      if (o != OBJECT_SELF && GetDistanceBetween (o, OBJECT_SELF) < 20.0)
        {
          d = GetMaxHitPoints(o) - GetCurrentHitPoints (o);
          if (d > max) { max=d; oMostDamaged=o; }
        }
      o = GetObjectByTag("githyankidudetag", ++n);
    }

  if (!GetIsObjectValid(oMostDamaged)) return;

  effect eHeal = EffectHeal(nToHeal);
  effect eCure = EffectVisualEffect(VFX_IMP_HEALING_S);
  ApplyEffectToObject(DURATION_TYPE_INSTANT, eHeal, oMostDamaged);
  ApplyEffectToObject(DURATION_TYPE_INSTANT, eCure, oMostDamaged);
}

caution: this is very quick and dirty and not tested.