RuleEngine

ldtk.rules.RuleEngine (Class)

The auto-layer rule engine: matches rule patterns against a RuleSource IntGrid and produces tiles for a RuleTarget layer. One engine per target layer instance, reusable. Hot paths never allocate.

Two output paths share the same matching and tile production code:

  • the editor path works on an AutoTileCache map (per rule, per cell), supports partial recomputes (applyRulesInRect) and applies break-on-match as a final pass;
  • the runtime path writes into a reusable buffer and applies break-on-match on the fly: computeAll fills a FlatTileBuffer, computeFull and computeRect fill a CellTileBuffer which supports partial recomputes. All produce the same tiles.

Static Members

clearRect(cache: AutoTileCache, ruleUid: Int, w: Int, cx: Int, cy: Int, wid: Int, hei: Int): Void
Name Type
cache AutoTileCache
ruleUid Int
w Int
cx Int
cy Int
wid Int
hei Int

clearRule(cache: AutoTileCache, ruleUid: Int): Void
Name Type
cache AutoTileCache
ruleUid Int

flatten(cache: AutoTileCache, rulesDisplayOrder: Array<RuleDef>): Array<FlatAutoTile>

Tiles of the cache in a deterministic order: rules in display order, cells by ascending coordId

Name Type
cache AutoTileCache
rulesDisplayOrder Array<RuleDef>
Returns
Array<FlatAutoTile>

toJsonTiles(flat: Array<FlatAutoTile>): Array<ldtk.Tile>

Convert flattened tiles to the autoLayerTiles JSON format

Name Type
flat Array<FlatAutoTile>
Returns
Array<ldtk.Tile>

Instance Members

source: RuleSource

target: RuleTarget

tileset: RuleTileset

matches(r: RuleDef, cx: Int, cy: Int, ?dirX: Int = 1, ?dirY: Int = 1): Bool

Does the rule pattern match at given coord? dirX/dirY = -1 test the mirrored pattern.

Name Type Default
r RuleDef
cx Int
cy Int
dirX Int 1
dirY Int 1
Returns
Bool

applyRuleAt(sink: AutoTileSink, r: RuleDef, cx: Int, cy: Int): Bool

Check and apply a rule at given coord (all flip variants). Returns TRUE if the rule matched. The sink must already be positioned on this rule (beginRule).

Name Type
sink AutoTileSink
r RuleDef
cx Int
cy Int
Returns
Bool

addRuleTilesAt(sink: AutoTileSink, r: RuleDef, cx: Int, cy: Int, flips: Int): Void

Produce the tiles of a matched rule at given coord, for given matching flips (bit0=X, bit1=Y)

Name Type
sink AutoTileSink
r RuleDef
cx Int
cy Int
flips Int

computeAll(buffer: FlatTileBuffer, rulesEvalOrder: Array<RuleDef>): Void

Compute every tile of the layer into buffer (reset first), with break-on-match applied on the fly. Tiles are stored rule by rule in evaluation order, cells row by row; iterate the segments backwards to get the display order.

Name Type
buffer FlatTileBuffer
rulesEvalOrder Array<RuleDef>

computeFull(store: CellTileBuffer, rulesEvalOrder: Array<RuleDef>): Int

Compute every tile of the layer into a cell indexed store (emptied first). Returns the number of computed cells.

Name Type
store CellTileBuffer
rulesEvalOrder Array<RuleDef>
Returns
Int

computeRect(store: CellTileBuffer, rulesEvalOrder: Array<RuleDef>, cx: Int, cy: Int, wid: Int, hei: Int): Int

Recompute the tiles of a rectangle of cells in a store previously filled by computeFull (or computeRect), after the source IntGrid changed in that rectangle. The area is expanded by MAX_RADIUS so that every pattern reading a modified cell is re-evaluated; break-on-match locks are reset and recomputed for these cells only. The result is identical to a full recompute. Falls back to computeFull if the store does not match the layer or if the rule list changed. Returns the number of recomputed cells.

Name Type
store CellTileBuffer
rulesEvalOrder Array<RuleDef>
cx Int
cy Int
wid Int
hei Int
Returns
Int

applyRulesInRect(cache: AutoTileCache, rulesEvalOrder: Array<RuleDef>, cx: Int, cy: Int, wid: Int, hei: Int): Void

Recompute all rules in a rectangle (expanded to cover neighbouring patterns), then apply break-on-match on it

Name Type
cache AutoTileCache
rulesEvalOrder Array<RuleDef>
cx Int
cy Int
wid Int
hei Int

applyAll(cache: AutoTileCache, rulesEvalOrder: Array<RuleDef>): Void

Recompute everything into an emptied cache

Name Type
cache AutoTileCache
rulesEvalOrder Array<RuleDef>

applyRuleEverywhere(cache: AutoTileCache, r: RuleDef): Void

Recompute a single rule on the whole layer (break-on-match is NOT applied, the caller decides)

Name Type
cache AutoTileCache
r RuleDef

applyBreakOnMatchesArea(cache: AutoTileCache, rulesEvalOrder: Array<RuleDef>, cx: Int, cy: Int, wid: Int, hei: Int): Void

Discard the tiles hidden by break-on-match rules (or opaque tiles) in given area

Name Type
cache AutoTileCache
rulesEvalOrder Array<RuleDef>
cx Int
cy Int
wid Int
hei Int

new(source: RuleSource, target: RuleTarget, tileset: RuleTileset): Void
Name Type
source RuleSource
target RuleTarget
tileset RuleTileset

Private Members



addedTiles: Int

addedOpaque: Bool

trackOpaque: Bool

imin(a: Int, b: Int): Int
Name Type
a Int
b Int
Returns
Int

imax(a: Int, b: Int): Int
Name Type
a Int
b Int
Returns
Int

ensureLocks(): Void

termMatches(term: Int, value: Int): Bool

Check a single condition against an IntGrid value: +v = value required, -v = value forbidden, with the "anything" and group special values

Name Type
term Int
value Int
Returns
Bool

emit(sink: AutoTileSink, coordId: Int, x: Int, y: Int, tid: Int, flips: Int, alpha: Float): Void
Name Type
sink AutoTileSink
coordId Int
x Int
y Int
tid Int
flips Int
alpha Float

locksCell(r: RuleDef): Bool

TRUE if a rule that produced tiles in a cell prevents the following rules from rendering there

Name Type
r RuleDef
Returns
Bool

computeCells(sink: AutoTileSink, rulesEvalOrder: Array<RuleDef>, left: Int, top: Int, right: Int, bottom: Int): Void

Shared runtime loop: rules in evaluation order, cells of the rectangle (inclusive bounds) row by row, locks applied on the fly

Name Type
sink AutoTileSink
rulesEvalOrder Array<RuleDef>
left Int
top Int
right Int
bottom Int