kts.Tile

Name

kts.Tile – create a new Tile

Synopsis

tile =
    kts.Tile { 
        type = <"", "home", "door", "chest" or "barrel">,

        -- For all tile types:
        access = <access table (see below)>,
        connectivity_check = <integer>,
        control = <control or function>,
        depth = <integer>,
        graphic = <graphic>,
        hit_points = <function or integer>,
        items = <see description below>,
        on_activate = <function>,
        on_approach = <function>,
        on_destroy = <function>,
        on_hit = <function>,
        on_walk_over = <function>,
        on_withdraw = <function>,
        stairs_down = <"special", "north", "south", "east", "west">,
        tutorial = <integer>,
        map_as = <"wall", "floor">,

        -- For type = "home":
        facing = <"north", "south", "east", "west">,
        special_exit = <boolean>,
        unsecured_colour = <integer>,

        -- For type = "door" or "chest":
        open = <boolean>,
        open_graphic = <graphic>,
        lock_chance = <number between 0.0 and 1.0>,
        lock_pick_only_chance = <number between 0.0 and 1.0>,
        keymax = <integer>,
        special_lock = <boolean>,
        on_open_or_close = <function>,
        on_unlock_fail = <function>,

        -- Additionally for type = "chest":
        facing = <"north", "south", "east", "west">,
        trap_chance = <number between 0.0 and 1.0>,
        traps = <function>
    }

where:

<access table> = {
    "flying" = <access type>,
    "missiles" = <access type>,
    "walking" = <access type>
}

<access type> = nil, "approach", "partial", "blocked", "clear"

All fields listed above also accept nil as a valid value.

Description

A "Tile" is an object representing the "terrain" within a particular square of the dungeon. A Tile might therefore represent a wall, a piece of floor, a treasure chest, etc. A given dungeon square can contain more than one Tile, for example a treasure chest tile might be overlaid on top of a floor tile.

The kts.Tile function creates a new Tile. The function takes one parameter, a table, which contains various properties that the new Tile should have. These are as follows:

Return Value

The new Tile (a Lua userdata object) will be returned.

Errors

There are several different errors that can be generated if any of the input parameters are incorrect.

Notes

Shared Tile instances

For most tiles, the Lua object representing that tile is "shared" across all instances of the tile in the dungeon. So, for example, if there is a tile named t_wall_normal, then all "normal wall" tiles in the dungeon will actually be a reference to that same userdata object, and, for example, they will compare equal using the Lua == operator. This can be useful at times; for example, if you want to detect whether a certain tile is a "crystal ball", then you can compare it to t_crystal_ball using ==. (This technique was used in the "telewand" mod to create a special effect when a crystal ball was struck with a certain wand.)

However, for home, door, chest or barrel tiles, each copy of the tile in the dungeon is actually a separate userdata instance. This is because these tiles have their own "state" that needs to be stored separately for each instance of the tile (for example, a door needs to know whether it is open or closed, a home needs to know whether it is secured, and a chest or barrel needs to know the stored item). The upshot of this is that, for example, you cannot detect whether a tile is a barrel simply by comparing it to t_barrel using ==, because each individual barrel tile is considered "not equal" to t_barrel, and they are also all "not equal" to each other.

Custom properties

Note also that, once created, Tiles can store user-defined properties. For example, if t is a Tile, then we can write to a field such as t.my_custom_field, and read back from it again later. This might be useful for some special purposes.

Examples

A simple floor tile:

floor = kts.Tile {
    access = {
        walking = "clear",
        flying = "clear",
        missiles = "clear"
    },
    items = "floor",
    graphic = my_floor_graphic
}

A treasure chest:

chest = kts.Tile {
    type = "chest",

    graphic = my_graphic,   -- previously created by calling kts.Graphic
    open_graphic = my_open_graphic,
    facing = "north",
    depth = -4,

    items = "chest",
    access = {
        walking = "approach",
        flying = "clear",
        missiles = "clear"
    },
    trap_chance = 0.5,
    traps = function(pos, dir)
        if kts.RandomChance(0.5) then
            kts.SetPoisonTrap(pos, poison_trap_item)
        else
            kts.SetBladeTrap(pos, blade_trap_item, bolt_item, dir)
        end
    end,
    lock_chance = 0.5,
    lock_pick_only_chance = 0.16667,
    keymax = 3,
    hit_points = function() return kts.RandomRange(1, 20) end,
    control = my_control,  -- previously created by calling kts.Control

    -- Not shown here:
    -- functions on_open_or_close, on_unlock_fail, on_hit,
    -- which would typically play sound effects and/or show messages.
}

See Also

kts.Activate

kts.AddItem

kts.AddStuff

kts.CloseDoor

kts.ConnectivityCheck

kts.Control

kts.Graphic

kts.IsDoorOpen

kts.ItemType

kts.LoadSegments

kts.LockDoor

kts.OpenDoor

kts.OpenOrCloseDoor

kts.SetBladeTrap

kts.SetPoisonTrap