Manual:DIL Manual/integer

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:27, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

integer

Type. A whole number, signed, 32 bits.

var
   n : integer;

Integers count, measure and flag things, and they are DIL's truth values too: a condition is true when it is not 0. TRUE (1) and FALSE (0) are defined in vme.h; a comparison gives 1 or 0.

Numbers are written in decimal (42) or hexadecimal (0x2A). A negative number is a minus sign in front of one (-5). Most constants in vme.h and values.h (UNIT_ST_PC, PULSE_SEC, SFB_CMD...) are integers.

Operators

  • Arithmetic: + - * / %. Division cuts towards 0.
  • Bits: & (and), | (or). isset(), set() and unset() do the same for flag fields.
  • Logic: and, or, not. Both sides are always worked out.
  • Comparison: == != < > <= >=.

Notes

  • Comparisons bind more loosely than and and or: if (a == 1 and b == 2) compiles, but means (a == (1 and b)) == 2. Put each comparison in brackets: if ((a == 1) and (b == 2)).
  • Dividing by 0 (/ or %) gives no number but a failed value. A failed value is false in every comparison, and assigning it changes nothing: the variable keeps its old value.
  • A field of a null unit fails the same way: u.level > 5 and u.level <= 5 are both false when u is null.
  • Many integer fields are stored in fewer bits (8 or 16); a value assigned to them that doesn't fit wraps around. Each field's page says.
  • Integer variables keep their value through waits and are saved with the unit.
  • itoa() makes a string of an integer, atoi() an integer of a string; "a" + 5 is a compile error.

Example

dilbegin integer percent(part : integer, whole : integer);
code
{
   if (whole == 0)
      return (0);
   return ((part * 100) / whole);
}
dilend

dilbegin do_health(arg : string);
external
   integer percent(part : integer, whole : integer);
var
   pct : integer;
code
{
   pct := percent(self.hp, self.max_hp);
   if (pct < 25)
      sendtext("You are badly hurt (" + itoa(pct) + "%).<br/>", self);
   else
      sendtext("You are at " + itoa(pct) + "% of your hit points.<br/>", self);
   quit;
}
dilend

A function that works out a percentage without dividing by 0, and a command that uses it.

See also