Combat, damage, and death

Combat has its own family of events and a few action commands for dealing, healing, and rewarding. The events here are phase-restricted, so getting the phase right matters even more than usual.

Reacting when combat starts — fight

The fight event fires when combat is initiated involving the owner mob, with $actor the other combatant. It is after-only (deferred to the next tick), so use it to react, not to prevent:

after fight {
  do "say So, [name $actor], you want a fight!"
  do "emote bares its fangs."
}

Every pulse of a fight — combat

The combat event is the in-combat counterpart of idle: it fires on every activity pulse while the owner mob is fighting. It is handle-only and binds no actor. Use it for ongoing battle behavior — special attacks, taunts, calls for help:

handle combat {
  if [random 20] {
    do "emote presses the attack with a flurry of blows."
  }
}

Knowing who you are fighting

isfighting tests whether a creature has a living opponent, and fighting produces the iterable of its combatants. position tells you a creature's stance, which combat changes:

handle combat {
  let foes [fighting $self]
  unless [empty $foes]
  do "say I face [count $foes] of you at once!"
  let target [first $foes]
  if [eq [position $target] "sitting"] {
    do "emote looms over its faltering foe."
  }
}

Dealing and healing — damage

damage <creature> <int> <spell-name> applies hit-point damage of a named type. A negative amount heals. When the owner is a mob it is recorded as the attacker:

handle combat {
  if [random 10] {
    damage [first [fighting $self]] 15 fireball
    do "emote hurls a gout of flame!"
  }
}

One quirk worth knowing: if the owner is an unapproved mob, the attacker attribution reduces the damage to 0 — the call runs but does no harm. Approved mobs and room/object owners deal the full amount.

Casting — spell

spell <creature> <level> <spell-name> casts a spell at a given caster level (capped at the system maximum):

handle command (heal) {
  require [isplayer $actor]
  spell $actor 30 "cure light"
  do "say Be whole again, [name $actor]."
}

There is also a spell event — fired when a spell is cast on the owner mob or in its room — that binds $spell, the spell number. Filter it to react to particular spells:

after spell (42) {
  do "say I felt that magic, [name $actor]!"
}

Rewarding a kill — giveexp and award

Grant raw experience with giveexp, or credit a tracked challenge with award (players only; the amount defaults to 1):

after command (claim) {
  require [isplayer $actor]
  giveexp $actor 5000
  award $actor "dragon-slayer" 1
  do "say The realm honours your deed, [name $actor]."
}

Intercepting death — death

The death event fires on the dying creature, the room, and every witnessing mob, so one handler covers both "I am dying" and "someone died near me." Tell them apart with [eq $actor $self]. The bound names are $actor (the deceased) and $killer (which may be null).

A witness reacts:

after death {
  unless [eq $actor $self]
  do "emote averts its eyes from the fallen [name $actor]."
}

The dier's own handler can abort its death — but a handle death must heal the dier itself, because intercepting does not restore HP:

handle death {
  require [eq $actor $self]
  require [isnull $killer]          # only cheat death from non-combat causes
  damage $self -100 heal            # negative damage heals
  do "say I... am not finished yet."
}

If $killer is non-null, you might instead just have the dying mob speak a last line in the before phase, which never suppresses:

before death {
  if [eq $actor $self] {
    do "say Avenge me!"
  }
}

See also