Difference between revisions of "Manual:DIL Manual/spellindex()"
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| Line 1: | Line 1: | ||
| − | + | = spellindex() = | |
| − | + | '''Function''' (integer). Finds a spell's number from its name or an abbreviation of it. | |
| − | |||
| − | + | spellindex(s : string) | |
| − | |||
| − | + | Looks for <code>s</code> among the spell names from <code>spells.def</code> and returns that spell's | |
| − | + | number, the same number as its <code>SPL_*</code> constant (<code>values.h</code>, <code>vme.h</code>). | |
| − | + | Every word may be abbreviated: <code>"cu li wo"</code> finds ''cure light wounds''. Case doesn't | |
| + | matter, and spaces in front are skipped. | ||
| − | + | The spell spheres are in the same list: numbers 0 to 11 (<code>SPL_ALL</code> to | |
| − | < | + | <code>SPL_EXTERNAL</code>) are ''theory'', ''divine sphere'' and so on to ''acid sphere''. |
| − | |||
| − | |||
| − | + | == Parameters == | |
| − | + | {| class="wikitable" | |
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | s || string || The spell's name, whole or with its words abbreviated. | ||
| + | |} | ||
| − | + | == Returns == | |
| − | + | Integer: the spell number, or -1 when no spell matches (also for <code>""</code>). | |
| − | + | It fails when <code>s</code> is a string variable that was never set: <code>i := spellindex(s)</code> | |
| + | then leaves <code>i</code> as it was, so set <code>i</code> or test <code>s</code> first. | ||
| − | + | == Notes == | |
| + | * The words of <code>s</code> are matched one by one against the words of each name. The name | ||
| + | that matches the most words wins; on a tie the lowest number wins. So <code>"fire"</code> finds | ||
| + | the ''fire sphere'' (7), not ''fireball'', and <code>"cure"</code> finds the first spell whose | ||
| + | name begins with ''cure''. Words after a full match are ignored: | ||
| + | <code>"cure light wounds now"</code> still finds ''cure light wounds''. | ||
| + | * Spells only; skills and weapons are other lists. | ||
| + | * Spell numbers run from 0 to <code>SPL_TREE_MAX</code> - 1 (499), with gaps. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | dilbegin spell_num(arg : string); | ||
| + | var | ||
| + | si : integer; | ||
| + | code | ||
| + | { | ||
| + | si := spellindex(arg); | ||
| + | if (si == -1) | ||
| + | sendtext("There is no spell called '" + arg + "'.<br/>", self); | ||
| + | else | ||
| + | sendtext("'" + arg + "' is spell number " + itoa(si) + ".<br/>", self); | ||
| + | quit; | ||
| + | } | ||
| + | dilend | ||
| + | </pre> | ||
| + | A player command (<code>func =</code> in <code>commands.def</code>) that tells the number of the | ||
| + | spell named, as the god command in commands.zon that sets a unit's spell skill looks it up. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/spellinfo()|spellinfo()]] | ||
| + | * [[Manual:DIL Manual/cast spell()|cast spell()]] | ||
| + | * [[Manual:DIL Manual/attack spell()|attack spell()]] | ||
| + | * [[Manual:DIL Manual/spells|spells]] | ||
Latest revision as of 23:03, 14 November 2026
spellindex()
Function (integer). Finds a spell's number from its name or an abbreviation of it.
spellindex(s : string)
Looks for s among the spell names from spells.def and returns that spell's
number, the same number as its SPL_* constant (values.h, vme.h).
Every word may be abbreviated: "cu li wo" finds cure light wounds. Case doesn't
matter, and spaces in front are skipped.
The spell spheres are in the same list: numbers 0 to 11 (SPL_ALL to
SPL_EXTERNAL) are theory, divine sphere and so on to acid sphere.
Parameters
| Name | Type | Meaning |
|---|---|---|
| s | string | The spell's name, whole or with its words abbreviated. |
Returns
Integer: the spell number, or -1 when no spell matches (also for "").
It fails when s is a string variable that was never set: i := spellindex(s)
then leaves i as it was, so set i or test s first.
Notes
- The words of
sare matched one by one against the words of each name. The name
that matches the most words wins; on a tie the lowest number wins. So"fire"finds the fire sphere (7), not fireball, and"cure"finds the first spell whose name begins with cure. Words after a full match are ignored:"cure light wounds now"still finds cure light wounds.
- Spells only; skills and weapons are other lists.
- Spell numbers run from 0 to
SPL_TREE_MAX- 1 (499), with gaps.
Example
dilbegin spell_num(arg : string);
var
si : integer;
code
{
si := spellindex(arg);
if (si == -1)
sendtext("There is no spell called '" + arg + "'.<br/>", self);
else
sendtext("'" + arg + "' is spell number " + itoa(si) + ".<br/>", self);
quit;
}
dilend
A player command (func = in commands.def) that tells the number of the
spell named, as the god command in commands.zon that sets a unit's spell skill looks it up.