This is a response to
mlshuford14, who wrote "Is there a way during encounters to have my enemies top fighting once they reach a certain hit point? Almost like a surrender?".
First, flag the creature as Immortal, so that their HP never falls below 1, regardless of damage taken, and put them in a local faction (one with the Global box unchecked).
There are various ways of managing surrender. I find the following to be robust, for Single Player.
In the OnDamaged event
if (GetCurrentHitPoints() < 10) // or whatever arbitrary value you like
{
bhCeaseFire(GetFirstPC());
}
The function definition is
Spoiler // Cease fire - the caller stops fighting the PC and associates
void bhCeaseFire(object oPC, int bDisarm = FALSE)
{
object oNPC = OBJECT_SELF;
if (oPC == GetFirstPC()) ClearAllActions(TRUE); // Function is recursive for associates so do this only once
// SetIsTemporaryNeutral should be the same as ClearPersonalReputation, in theory, but does not work for associates.
// AdjustReputation is for global factions - this script is for local ones.
AssignCommand(oPC, ClearAllActions(TRUE));
SetIsTemporaryNeutral(oPC, oNPC);
SetIsTemporaryNeutral(oNPC, oPC);
ClearPersonalReputation(oPC, oNPC);
ClearPersonalReputation(oNPC, oPC);
bhCeaseFireAssociates(oPC, ASSOCIATE_TYPE_ANIMALCOMPANION);
bhCeaseFireAssociates(oPC, ASSOCIATE_TYPE_DOMINATED);
bhCeaseFireAssociates(oPC, ASSOCIATE_TYPE_FAMILIAR);
bhCeaseFireAssociates(oPC, ASSOCIATE_TYPE_HENCHMAN);
bhCeaseFireAssociates(oPC, ASSOCIATE_TYPE_SUMMONED);
if (bDisarm)
{
DelayCommand(1.0, bh_disarm());
}
}
// Associate class cease fire.
// Iterative, because henchmen can have horses.
void bhCeaseFireAssociates(object oPC, int nAssociateType)
{
int i = 0;
object oAssociate = GetAssociate(nAssociateType, oPC, ++i);
while (GetIsObjectValid(oAssociate))
{
bhCeaseFire(oAssociate);
oAssociate = GetAssociate(nAssociateType, oPC, ++i);
}
}
// Disarm
void bh_disarm()
{
ClearAllActions();
ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_RIGHTHAND));
ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_LEFTHAND));
ActionUnequipItem(GetItemInSlot(INVENTORY_SLOT_HEAD));
}
You may want to set the plot flag on the creature, to prevent renewed attacks by the player.
If you then want the creature to talk to the player, use ClearAllActions(TRUE) on the player before ActionStartConversation, otherwise the combat state will cause the conversation to fail. Alternatively, a floating string "I surrender" or whatever will do.
Stopping battles with multiple enemies is more tricky. In theory, have each of them call bhCeaseFire. In practice, it may be necessary to repeat after 1 second or so. I suspect this is because there are combat actions and shouts in the pipeline that don't always get cancelled by the first attempt.