Skip to content
Back to top
View this page

# `tiles`

Module with structure to process Terraria worlds’ tile matrixes.

## `array_dtype`

Submodule containing `TILE_DTYPE`.

lihzahrd.terraria.world.tiles.array_dtype.TILE_DTYPE = dtype([('block_id', '<u2'), ('block_u', '<u2'), ('block_v', '<u2'), ('block_shape', 'u1'), ('block_inactive', '?'), ('block_paint', 'u1'), ('block_illuminant', '?'), ('block_echo', '?'), ('wall_id', '<u2'), ('wall_paint', 'u1'), ('wall_illuminant', '?'), ('wall_echo', '?'), ('liquid_id', 'u1'), ('liquid_volume', 'u1'), ('wire_red', '?'), ('wire_green', '?'), ('wire_blue', '?'), ('wire_yellow', '?'), ('wire_actuator', '?')])

  The `numpy.dtype` with which `WorldTiles` stores tiles.

  The fields `block_id` and `wall_id` are special:

  - if their value is `0`, it means that the tile has no block or wall present

  - if their value is `1` or more, if means that a block or wall is present, and that its ID is the value, minus one.

## `tile`

Submodule containing `Tile`.

class lihzahrd.terraria.world.tiles.tile.Tile(*, block: BlockBase | None = None, wall: WallBase | None = None, liquid: LiquidBase | None = None, wiring: Wiring | None = None)

  Developer-friendly representation of a tile in a Terraria world, with its characteristics.

  block: BlockBase | None

    The foreground block present in the tile, or `None` if there isn’t any.

  wall: WallBase | None

    The background wall present in the tile, or `None` if there isn’t any.

  liquid: LiquidBase | None

    The liquid present in the tile, or `None` if there isn’t any.

  wiring: Wiring

    The wiring present in the tile.

  to_scalar() → ndarray[tuple[Literal[1]], Any]

    Convert a `Tile` into a scalar `numpy.ndarray` of `TILE_DTYPE`.

    Parameters:

      self – The `Tile` to convert.

    Returns:

      The resulting scalar `numpy.ndarray`.

  __array__(dtype=None, copy=None) → ndarray[tuple[Literal[1]], Any]

    Allow `numpy` to treat this as an array.

    See also

    The __array__() method on NumPy docs

  classmethod from_scalar(scalar: ndarray[tuple[Literal[1]], Any], *, frame_important: WorldFrameImportant) → Tile

    Convert a scalar `numpy.ndarray` of `TILE_DTYPE` into a `Tile`.

    Tip

    Useful to get a `Tile` out of `WorldTiles`!

    Parameters:

      - scalar – The scalar `numpy.ndarray` of `TILE_DTYPE` to convert.

      - frame_important – The value of `WorldFrameImportant` to use to determine which blocks should have associated UV values, and which shouldn’t.

    Returns:

      The resulting `Tile`.

## `wiring`

Submodule containing `Tile`.

class lihzahrd.terraria.world.tiles.wiring.Wiring(*, has_red: bool = False, has_blue: bool = False, has_green: bool = False, has_yellow: bool = False, has_actuator: bool = False)

  The wiring of a Terraria tile.

  has_red: bool

    If the tile has Red Wire.

  has_blue: bool

    If the tile has Blue Wire.

  has_green: bool

    If the tile has Green Wire.

  has_yellow: bool

    If the tile has Yellow Wire.

  has_actuator: bool

    If the tile has an Actuator.

  __bool__() → bool
    Returns:

      Whether the tile has any wiring.

## `world_tiles`

Submodule containing `WorldTiles`.

class lihzahrd.terraria.world.tiles.world_tiles.WorldTiles(value: Value)

  Bases: `OpCollection`[`ndarray`[`tuple`[`int`, `int`], `Any`]], `PackPrimitive`[`ndarray`[`tuple`[`int`, `int`], `Any`]]

  `PackPrimitive` of the tiles present in a world.

  Internally, it’s represented by a 2-dimensional `numpy.ndarray` of the `TILE_DTYPE`.

  classmethod _read_batch_to_ndarray(fp: FileProcessor, data: ndarray[tuple[int], Any], *, frame_important: WorldFrameImportant) → int

    Read a batch of tiles to the given 1-dimensional `numpy.ndarray`.

    The tiles are written to the array starting from offset 0 and ending at an unknown offset, determined by the read `repeat` value.

    The `repeat` value, or the number of read tiles, is then returned.

    Parameters:

      - fp – The `FileProcessor` to read tiles from.

      - data – The `numpy.ndarray` to write tiles into.

      - frame_important – The value of `WorldFrameImportant` to use to determine which blocks have associated UV values, and which don’t.

    Returns:

      The number of read tiles (`repeat` value).

  classmethod _read_column_to_ndarray(fp: FileProcessor, data: ndarray[tuple[int], Any], *, frame_important: WorldFrameImportant) → None

    Read a column of tiles to the given 1-dimensional `numpy.ndarray`.

    The tiles are written to the array from start to end, and are read from the `FileProcessor` until the array has no more room.

    Parameters:

      - fp – The `FileProcessor` to read tiles from.

      - data – The `numpy.ndarray` to write tiles into.

      - frame_important – The value of `WorldFrameImportant` to use to determine which blocks have associated UV values, and which don’t.

  classmethod _read_matrix_to_ndarray(fp: FileProcessor, data: ndarray[tuple[int, int], Any], *, frame_important: WorldFrameImportant) → None

    Read the whole tile matrix to the given 2-dimensional `numpy.ndarray`.

    For each column of the matrix, `_read_column_to_ndarray()` is called to fill it.

    Parameters:

      - fp – The `FileProcessor` to read tiles from.

      - data – The `numpy.ndarray` to write tiles into.

      - frame_important – The value of `WorldFrameImportant` to use to determine which blocks have associated UV values, and which don’t.

  classmethod _create_difference_matrix(value: ndarray[tuple[int, int], Any]) → ndarray[tuple[int, int], BoolDType]

    Given a tile matrix, create a new boolean matrix where the true values indicate which tiles mark the start of a new tile batch.

    Parameters:

      value – The `numpy.ndarray` to use for the computation.

    Returns:

      The resulting matrix.

  classmethod _create_tile_batches()

    Given a tile matrix, return a `list` of tile batches.

    The first element of each `tuple` is the tile scalar to write, still in `numpy.ndarray` format, and the second element is how many times the tile is repeated vertically.

    Parameters:

      value – The `numpy.ndarray` to use for the computation.

    Returns:

      The resulting `list` of `tuple`.

  classmethod _write_tile_batch()

    Write a single batch of tiles to the given `FileProcessor`.

    Parameters:

      - fp – The `FileProcessor` to write tiles to.

      - batch – The batch of tiles to write, in the form of a `tuple` returned by `_create_tile_batches()`.

      - frame_important – The value of `WorldFrameImportant` to use to determine which blocks have associated UV values, and which don’t.

  exception InvalidShapeError(value: Value)

    Bases: `ValidationError`

    The shape of the underlying `numpy.ndarray` does not match the given world size.