Difference between revisions of "Manual:DIL Manual/opponent()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="bfop"></span>
+
= opponent() =
−
integer opponent(u1 : unitptr, u2 : unitptr)
+
'''Function''' (integer). Tells whether two characters are fighting each other.
−
    u1, u2: Two characters
 
−
    return: TRUE if u1 is in combat with u2, FALSE otherwise.
 
−
    Example: if (opponent(self, victim)) ...
 
  
−
    When in a combat, you are usually only melee-attacking one opponent,
+
opponent(ch : unitptr, vict : unitptr)
−
    although you may have many other opponents. This function lets you
 
−
    check if you are directly / indirectly an opponent to another unit.
 
  
−
---~---~---~---~---~---~---~---~---
+
A character in a fight has a list of opponents: everyone fighting it and everyone it fights. It melee-attacks only one of them, the one in its [[Manual:DIL Manual/fighting|fighting]] field. <code>opponent()</code> looks in the whole list, so it is also true for an opponent that is not the melee target.
−
<span id="bfop"></span>
 
−
integer opponent(u1 : unitptr, u2 : unitptr)
 
−
    u1, u2: Two characters
 
−
    return: TRUE if u1 is in combat with u2, FALSE otherwise.
 
−
    Example: if (opponent(self, victim)) ...
 
  
−
    When in a combat, you are usually only melee-attacking one opponent,
+
== Parameters ==
−
    although you may have many other opponents. This function lets you
+
{| class="wikitable"
−
    check if you are directly / indirectly an opponent to another unit.
+
! Name !! Type !! Meaning
 +
|-
 +
| ch || unitptr || The character whose opponents are checked.
 +
|-
 +
| vict || unitptr || The character looked for among them.
 +
|}
  
−
---~---~---~---~---~---~---~---~---
+
== Returns ==
 +
TRUE when <code>vict</code> is among <code>ch</code>'s opponents, FALSE when it isn't or <code>ch</code> is not fighting. It fails when either unit is null or not a character (false in an <code>if</code>).
 +
 
 +
== Notes ==
 +
* Opponents always come in pairs: when one character gets the other as an opponent, the other gets it too. So <code>opponent(a, b)</code> and <code>opponent(b, a)</code> give the same answer.
 +
* The number of opponents is [[Manual:DIL Manual/opponentcount|opponentcount]]; [[Manual:DIL Manual/getopponent()|getopponent()]] gets each of them.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin hit_back(victim : unitptr, ch : unitptr);
 +
code
 +
{
 +
  if (victim.position <= POSITION_SLEEPING)
 +
      return;
 +
  if (opponent(victim, ch))
 +
      return;
 +
  set_fighting(victim, ch);
 +
}
 +
dilend
 +
</pre>
 +
Makes a victim who is awake fight back, unless the two are already fighting, as <code>provoked_attack</code> does in skills.zon.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/getopponent()|getopponent()]]
 +
* [[Manual:DIL Manual/opponentcount|opponentcount]]
 +
* [[Manual:DIL Manual/fighting|fighting]]
 +
* [[Manual:DIL Manual/set fighting()|set fighting()]]
 +
* [[Manual:DIL Manual/stop fighting()|stop fighting()]]

Latest revision as of 23:08, 14 November 2026

opponent()

Function (integer). Tells whether two characters are fighting each other.

opponent(ch : unitptr, vict : unitptr)

A character in a fight has a list of opponents: everyone fighting it and everyone it fights. It melee-attacks only one of them, the one in its fighting field. opponent() looks in the whole list, so it is also true for an opponent that is not the melee target.

Parameters

Name Type Meaning
ch unitptr The character whose opponents are checked.
vict unitptr The character looked for among them.

Returns

TRUE when vict is among ch's opponents, FALSE when it isn't or ch is not fighting. It fails when either unit is null or not a character (false in an if).

Notes

  • Opponents always come in pairs: when one character gets the other as an opponent, the other gets it too. So opponent(a, b) and opponent(b, a) give the same answer.
  • The number of opponents is opponentcount; getopponent() gets each of them.

Example

dilbegin hit_back(victim : unitptr, ch : unitptr);
code
{
   if (victim.position <= POSITION_SLEEPING)
      return;
   if (opponent(victim, ch))
      return;
   set_fighting(victim, ch);
}
dilend

Makes a victim who is awake fight back, unless the two are already fighting, as provoked_attack does in skills.zon.

See also