Hit Point Check Script

Can someone show me how to check the current amount of hitpoints on a monster, and when its down to 25% I want to spawn in his buddies.

There are GetMaxHitPoints and GetCurrentHitPoints.
Because int must be a whole number, we multiply the ratio by 100 to get the comparison.
You can call the script in your OnDamaged script for the NPC

void main()
{
int nMax = GetMaxHitPoints();
int nHP = GetCurrentHitPoints();
int nRatio = (100*nHP)/nMax;
if (nRatio <= 25)
    {
    //whatever you are doing
    }
else return;
}

Thank you very much. I really appreciate it.

int nRatio = 100*(nHP/nMax);

consider integer truncation… anything less than full HP results in nRatio=0.

try

int nRatio = (nHP * 100) / nMax;

that also rounds very low HP down to nRatio=0 but should be okay here.

1 Like

I think you are right - forgot my parenthesis! Corrected my post above

1 Like

Apologies. Im a bit lost and my script is cluttered.
I cant get the hit points to fire and my monsters spawn every time i hit it. I end up with 12 spiders in about 10 seconds…
again I apologize.

//:://////////////////////////////////////////////////
//:: NW_C2_DEFAULT6
//:: Default OnDamaged handler
/*
If already fighting then ignore, else determine
combat round
*/
//:://////////////////////////////////////////////////
//:: Copyright © 2002 Floodgate Entertainment
//:: Created By: Naomi Novik
//:: Created On: 12/22/2002
//:://////////////////////////////////////////////////
//:://////////////////////////////////////////////////
//:: Modified By: Deva Winblood
//:: Modified On: Jan 17th, 2008
//:: Added Support for Mounted Combat Feat Support
//:://////////////////////////////////////////////////

#include “nw_i0_generic”
#include “x3_inc_horse”

void main()
{
object oDamager = GetLastDamager();
object oMe=OBJECT_SELF;
int nHPBefore;
if (!GetLocalInt(GetModule(),“X3_NO_MOUNTED_COMBAT_FEAT”))
if (GetHasFeat(FEAT_MOUNTED_COMBAT)&&HorseGetIsMounted(OBJECT_SELF))
{ // see if can negate some damage
if (GetLocalInt(OBJECT_SELF,“bX3_LAST_ATTACK_PHYSICAL”))
{ // last attack was physical
nHPBefore=GetLocalInt(OBJECT_SELF,“nX3_HP_BEFORE”);
if (!GetLocalInt(OBJECT_SELF,“bX3_ALREADY_MOUNTED_COMBAT”))
{ // haven’t already had a chance to use this for the round
SetLocalInt(OBJECT_SELF,“bX3_ALREADY_MOUNTED_COMBAT”,TRUE);
int nAttackRoll=GetBaseAttackBonus(oDamager)+d20();
int nRideCheck=GetSkillRank(SKILL_RIDE,OBJECT_SELF)+d20();
if (nRideCheck>=nAttackRoll&&!GetIsDead(OBJECT_SELF))
{ // averted attack
if (GetIsPC(oDamager)) SendMessageToPC(oDamager,GetName(OBJECT_SELF)+GetStringByStrRef(111991));
//if (GetIsPC(OBJECT_SELF)) SendMessageToPCByStrRef(OBJECT_SELF,111992");
if (GetCurrentHitPoints(OBJECT_SELF)<nHPBefore)
{ // heal
effect eHeal=EffectHeal(nHPBefore-GetCurrentHitPoints(OBJECT_SELF));
AssignCommand(GetModule(),ApplyEffectToObject(DURATION_TYPE_INSTANT,eHeal,oMe));
} // heal
} // averted attack
} // haven’t already had a chance to use this for the round
} // last attack was physical
} // see if can negate some damage
if(GetFleeToExit()) {
// We’re supposed to run away, do nothing
} else if (GetSpawnInCondition(NW_FLAG_SET_WARNINGS)) {
// don’t do anything?
} else {
if (!GetIsObjectValid(oDamager)) {
// don’t do anything, we don’t have a valid damager
} else if (!GetIsFighting(OBJECT_SELF)) {
// If we’re not fighting, determine combat round
if(GetBehaviorState(NW_FLAG_BEHAVIOR_SPECIAL)) {
DetermineSpecialBehavior(oDamager);
} else {
if(!GetObjectSeen(oDamager)
&& GetArea(OBJECT_SELF) == GetArea(oDamager)) {
// We don’t see our attacker, go find them
ActionMoveToLocation(GetLocation(oDamager), TRUE);
ActionDoCommand(DetermineCombatRound());
} else {
DetermineCombatRound();
}
}
} else {
// We are fighting already – consider switching if we’ve been
// attacked by a more powerful enemy
object oTarget = GetAttackTarget();
if (!GetIsObjectValid(oTarget))
oTarget = GetAttemptedAttackTarget();
if (!GetIsObjectValid(oTarget))
oTarget = GetAttemptedSpellTarget();

        // If our target isn't valid
        // or our damager has just dealt us 25% or more
        //    of our hp in damager
        // or our damager is more than 2HD more powerful than our target
        // switch to attack the damager.
        if (!GetIsObjectValid(oTarget)
            || (
                oTarget != oDamager
                &&  (
                     GetTotalDamageDealt() > (GetMaxHitPoints(OBJECT_SELF) / 4)
                     || (GetHitDice(oDamager) - 2) > GetHitDice(oTarget)
                     )
                )
            )
        {
            // Switch targets
            DetermineCombatRound(oDamager);
        }
    }
}

// Send the user-defined event signal
if(GetSpawnInCondition(NW_FLAG_DAMAGED_EVENT))
{
    SignalEvent(OBJECT_SELF, EventUserDefined(EVENT_DAMAGED));
}

int nMax = GetMaxHitPoints(OBJECT_SELF);
int nHP = GetCurrentHitPoints(OBJECT_SELF);
int nRatio = 100*(nHP/nMax);
if (nRatio <= 25)
{

object oTarget;
object oSpawn;
location lTarget;
oTarget = oDamager;

lTarget = GetLocation(oDamager);

oSpawn = CreateObject(OBJECT_TYPE_CREATURE, “spiddire001”, lTarget);

oTarget = oSpawn;

SetIsTemporaryEnemy(oDamager, oTarget);

AssignCommand(oTarget, ActionAttack(oDamager));

AssignCommand(oTarget, DetermineCombatRound(oDamager));

oTarget = oDamager;

lTarget = GetLocation(oTarget);

oSpawn = CreateObject(OBJECT_TYPE_CREATURE, “spiddire001”, lTarget);

oTarget = oSpawn;

SetIsTemporaryEnemy(oDamager, oTarget);

AssignCommand(oTarget, ActionAttack(oDamager));

AssignCommand(oTarget, DetermineCombatRound(oDamager));

oTarget = oDamager;

lTarget = GetLocation(oTarget);

oSpawn = CreateObject(OBJECT_TYPE_CREATURE, “spiddire001”, lTarget);

oTarget = oSpawn;

SetIsTemporaryEnemy(oDamager, oTarget);

AssignCommand(oTarget, ActionAttack(oDamager));

AssignCommand(oTarget, DetermineCombatRound(oDamager));
}
else return;

}

ok. the hit point part of the script works as advertised. I just cant keep it from spawning 3 dire spiders. Once it dips below 25% and I hit the owner of the script, 3 more spiders spawn, over and over with every subsequent hit???
how do i make it fire only once?

You need to use a variable to mark the critters spawned and check for it.


int nMax = GetMaxHitPoints();
int nHP = GetCurrentHitPoints();
int nRatio = (100*nHP)/nMax;
if (nRatio <= 25)
    {
    //whatever you are doing
   int nSpawn = GetLocalInt(OBJECT_SELF, "Spawned");
    if(nSpawn!=1)
           {
           //Spawn your critters here
           //mark them spawned
           SetLocalInt(OBJECT_SELF, "Spawned", 1);
           }
           return;
    }
else return;

I bow to your Code Fu! Lol
Thank you again Mannast

1 Like