Docstrings

TerrariaWiringHelper.AndGateType
(AndGate|XorGate){T<:Integer, V<:AbstractVector{<:Integer}}(gatestate::Bool, args::T, lampstates::V) <:AbstractGate
CombGate{T, V} = Union{AndGate{T, V}, XorGate{T, V}}

Combinatory gates in Terraria. gatestate tells the default (when all inputs are zero) state of the gate. args tells the number of inputs. lampstates lists how the inputs are wired to each logic lamp.

Example

Consider AndGate(false, 4, [0x12, 0x09, 0x04]). It's an AND gate whose default state is OFF. Since there are 4 args (named a-d), we consider the last 5 bits of each entry of lampstates. The leading bit tells the default state of the lamps. The other 4 bits (from lower to higher) tell which args are wired to the lamp. i.e.,

  • 0x12 = 0b10010 means the default state of the first lamp is ON and only b is connected to the lamp.
  • 0x09 = 0b01001 means the default state of the second lamp is OFF and a and d are connected to the lamp.
  • 0x04 = 0b00100 means the default state of the third lamp is OFF and only c is connected to the lamp.
julia> AndGate(false, 4, [0x12, 0x09, 0x04])
&(~b, ad, c)

The output is visualized via gate2string.

source
TerrariaWiringHelper.CombGateType
(AndGate|XorGate){T<:Integer, V<:AbstractVector{<:Integer}}(gatestate::Bool, args::T, lampstates::V) <:AbstractGate
CombGate{T, V} = Union{AndGate{T, V}, XorGate{T, V}}

Combinatory gates in Terraria. gatestate tells the default (when all inputs are zero) state of the gate. args tells the number of inputs. lampstates lists how the inputs are wired to each logic lamp.

Example

Consider AndGate(false, 4, [0x12, 0x09, 0x04]). It's an AND gate whose default state is OFF. Since there are 4 args (named a-d), we consider the last 5 bits of each entry of lampstates. The leading bit tells the default state of the lamps. The other 4 bits (from lower to higher) tell which args are wired to the lamp. i.e.,

  • 0x12 = 0b10010 means the default state of the first lamp is ON and only b is connected to the lamp.
  • 0x09 = 0b01001 means the default state of the second lamp is OFF and a and d are connected to the lamp.
  • 0x04 = 0b00100 means the default state of the third lamp is OFF and only c is connected to the lamp.
julia> AndGate(false, 4, [0x12, 0x09, 0x04])
&(~b, ad, c)

The output is visualized via gate2string.

source
TerrariaWiringHelper.XorGateType
(AndGate|XorGate){T<:Integer, V<:AbstractVector{<:Integer}}(gatestate::Bool, args::T, lampstates::V) <:AbstractGate
CombGate{T, V} = Union{AndGate{T, V}, XorGate{T, V}}

Combinatory gates in Terraria. gatestate tells the default (when all inputs are zero) state of the gate. args tells the number of inputs. lampstates lists how the inputs are wired to each logic lamp.

Example

Consider AndGate(false, 4, [0x12, 0x09, 0x04]). It's an AND gate whose default state is OFF. Since there are 4 args (named a-d), we consider the last 5 bits of each entry of lampstates. The leading bit tells the default state of the lamps. The other 4 bits (from lower to higher) tell which args are wired to the lamp. i.e.,

  • 0x12 = 0b10010 means the default state of the first lamp is ON and only b is connected to the lamp.
  • 0x09 = 0b01001 means the default state of the second lamp is OFF and a and d are connected to the lamp.
  • 0x04 = 0b00100 means the default state of the third lamp is OFF and only c is connected to the lamp.
julia> AndGate(false, 4, [0x12, 0x09, 0x04])
&(~b, ad, c)

The output is visualized via gate2string.

source
TerrariaWiringHelper.CombLogicMethod
CombLogic(lamps, inputs, outputs::AbstractVector{Bool})

Return the lists of possible combinatory logics for given inputs and outputs.

Arguments

  • lamps::Integer: the number of logic lamps on the gate, from 2 to 7.
  • inputs::AbstractVector{<:Integer}: the list of input values specified by the lower bits of entries, with a 1 in front of the first bit.
  • outputs::AbstractVector{Bool}: the expected return values of the logic. This should have the same length as inputs, although it's not strictly checked.

Example

Imagine we want to check if a 4-bit input can be divided by 3. Further, the input is expected to have only one decimal digit, i.e. in 0:9. We don't care how the logic behaves for inputs 10:15. The truth table would be

inoutinout
0000true0101false
0001false0110true
0010false0111false
0011true1000false
0100false1001true

Now we try to find a configuration with least lamps.

julia> inputs = 16:25; # `0:9` with the 5th last bit set to `1`

julia> outputs = [true, false, false, true, false, false, true, false, false, true];

julia> CombLogic(2, inputs, outputs) # there is no 2-lamp logic
Union{AndGate{Int64, StaticArraysCore.SVector{2, UInt8}}, XorGate{Int64, StaticArraysCore.SVector{2, UInt8}}}[]

julia> CombLogic(3, inputs, outputs) # there are 12 3-lamp logics, all of which use XOR gates
12-element Vector{Union{AndGate{Int64, StaticArraysCore.SVector{3, UInt8}}, XorGate{Int64, StaticArraysCore.SVector{3, UInt8}}}}:
 ^(~b, ad, c)
 ^(~b, abd, bc)
 ^(~ac, ad, ab)
 ^(~ac, abd, a)
 ^(~ac, cd, bc)
 ^(~ac, bcd, c)
 ^(~bd, c, a)
 ^(~bd, bc, ab)
 ^(~bd, cd, ad)
 ^(~bd, bcd, abd)
 ^(~acd, bc, c)
 ^(~acd, abd, ad)

See also CombGate

source
TerrariaWiringHelper.fuseMethod
fuse(setup::Integer, input::Integer) = fuse(setup & input)

Return the state of a tile connected to multiple wires. Each bit of setup represents the state of a wire, and each bit of input represents the connection of a wire to the tile.

source
TerrariaWiringHelper.fuseMethod
fuse(input::Integer)

Return the state of a tile connected to multiple wires. Each bit of the input represents the state of a wire.

Examples

julia> TerrariaWiringHelper.fuse(0b0110)
false

julia> TerrariaWiringHelper.fuse(0b1011)
true
source
TerrariaWiringHelper.hascolorMethod
hascolor(lampstate::Integer, mask::Integer) = lampstate & mask
hascolor(mask::Integer) = Fix2(hascolor, mask)
hascolor(G::AbstractGate, mask::Integer) = any(hascolor(mask), lampstates(G))

Check if some lamp is connected to some wire (combination), or if some gate has a such lamp.

source
TerrariaWiringHelper.lampstate2stringMethod
lampstate2string(lampstate::Integer, args::Integer)

Convert binary coded lampstate into a readable string. args tells the number of distinct inputs.

Examples

julia> lampstate2string(0b1111, 4)
"abcd"

julia> lampstate2string(0b11101, 4)
"~acd"

julia> lampstate2string(0b10000, 4)
"~"
source