Create an item
SCRIPT_CREATE_ITEM · opcode 0xea · 7 to 10 bytes
Script virtual machine · Opcode list · Scripted item creation
Creates a new item as a copy of an existing one, optionally changes its value, flags or icon, and puts it in the player's hand or in a block.
Encoding
Offsets count from the opcode byte. Words are little-endian. Up to three optional bytes follow, one for each override bit set, in the order below.
| Offset | Type | Field | Meaning |
|---|---|---|---|
| 0 | u8 | opcode | 0xea |
| 1 | u16le | template_item | Index of the item to copy. |
| 3 | u16le | destination | Block index, or a special ScriptItemDestination value. |
| 5 | s8 | position | For an ordinary block destination, stored as the item's position_raw: 0–3 a floor quarter, 8 a niche. |
| 6 | u8 | overrides | ScriptItemOverrideFlags: which optional bytes follow. |
| 7 | u8 | value | Present if SCRIPT_ITEM_OVERRIDE_VALUE is set. |
| … | u8 | item_flags | Present if SCRIPT_ITEM_OVERRIDE_FLAGS is set. |
| … | u8 | icon | Present if SCRIPT_ITEM_OVERRIDE_ICON is set. |
The size is 7 bytes plus one per override bit set. Other bits of overrides
add no bytes; preserve them as found.
Example (constructed, not from a level): ea 05 00 fe ff 00 01 07 copies item
5 into the current block, with its value set to 7.
Behavior
- The template item is duplicated.
- The selected overrides are written to the new item's value, flags and
icon fields (
ItemRuntime), in that order. - The item is placed:1
destination | Placement |
|---|---|
SCRIPT_ITEM_DESTINATION_HELD_OR_CURRENT (0xffff) | On the mouse pointer if it is empty, otherwise in the party's block, in one of its two front quarters chosen at random. |
SCRIPT_ITEM_DESTINATION_CURRENT_BLOCK (0xfffe) | In the party's block, in one of its two front quarters chosen at random; position is ignored. |
| Any other value | In that block of the current level, at position: 0 to 3 the north-west, north-east, south-west and south-east quarters, 8 a wall niche, 4 to 7 a flight position with no flight. |
The copy is made by the game's ordinary item creation: the template must not be an unused entry, and nothing is created when the 600 item entries are all in use. An item placed on the pointer prints its name and "taken." in the message area. Placing an item in a block sends no event and requests no scene update.1
Edge cases and unknowns
The overrides are written before the handler checks whether the copy succeeded. When the item table is full, the copy's index is 0 and the value, flags and icon overrides are written into item entry 0, the "no item" entry that item conditions read when nothing is held. A faithful reimplementation writes them there too.
SCRIPT_ITEM_DESTINATION_HELD_OR_CURRENT tests the mouse pointer directly,
whereas the other item commands use the held-item rule that also covers
keyboard control. In keyboard control the pointer stays empty (inferred),
so the new item goes into the selected slot through the placement rule. An
item already there is replaced, is no longer referenced, and its entry is
never freed. When that slot is a hand holding a cursed item, the game prints
the refusal messages and the new item falls on the party's block.
- Any template index is accepted: the 14 bytes at that index are copied
unless their location word is −1 (see
values passed on); the original scripts
use only the first hundred entries of
ITEM.DAT, which are templates.
Observed use
The reference scripts contain 43 instructions of this kind. They use the
override masks 0, 1, 2, 3 and 5, and the destinations
SCRIPT_ITEM_DESTINATION_HELD_OR_CURRENT (9 times),
SCRIPT_ITEM_DESTINATION_CURRENT_BLOCK (once) and ordinary blocks (33
times, at positions 0 (13), 1 (6), 2 (7), 3 (3) and 8 (4)).