sm-blueprint-lib 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ from dataclasses import asdict
2
+ from json import load, dump, loads, dumps
3
+ from math import ceil, log2
4
+
5
+ from numpy import ndarray
6
+
7
+ from sm_blueprint_lib.bases.parts.baseinteractablepart import BaseInteractablePart
8
+ from sm_blueprint_lib.blueprint import Blueprint
9
+ from sm_blueprint_lib.parts.barrierblock import BarrierBlock
10
+ from sm_blueprint_lib.parts.logicgate import LogicGate
11
+ from sm_blueprint_lib.parts.timer import Timer
12
+ from sm_blueprint_lib.pos import Pos
13
+
14
+
15
+ def load_blueprint(path):
16
+ """Load a blueprint from a path file (normally a blueprint.json).
17
+
18
+ Args:
19
+ path (str): The path to the json file.
20
+
21
+ Returns:
22
+ Blueprint: The loaded blueprint.
23
+ """
24
+ with open(path) as fp:
25
+ return Blueprint(**load(fp))
26
+
27
+
28
+ def save_blueprint(path, bp: Blueprint):
29
+ """Save a blueprint to a file (normally a blueprint.json).
30
+
31
+ Args:
32
+ path (str): The path to save the json file.
33
+ bp (Blueprint): The blueprint to be saved.
34
+ """
35
+ with open(path, mode="w") as fp:
36
+ return dump(asdict(bp), fp, sort_keys=True, separators=(',', ':'))
37
+
38
+
39
+ def load_string(str):
40
+ """Load a blueprint from a json string.
41
+
42
+ Args:
43
+ str (str): The string to be loaded.
44
+
45
+ Returns:
46
+ Blueprint: The loaded blueprint.
47
+ """
48
+ return Blueprint(**loads(str))
49
+
50
+
51
+ def dump_string(bp: Blueprint):
52
+ """Dump a blueprint into a json-formatted string.
53
+
54
+ Args:
55
+ bp (Blueprint): The blueprint to be dumped.
56
+
57
+ Returns:
58
+ str: The json-formatted string.
59
+ """
60
+ return dumps(asdict(bp), sort_keys=True, separators=(',', ':'))
61
+
62
+
63
+ def connect(_from, _to, *, parallel=True):
64
+ """Connect interactable parts together, recursively.
65
+
66
+ Args:
67
+ _from (Any): Must be an instance of BaseInteractablePart or a subclass.
68
+ Also it can be any nested iterable of instances (list of parts, list of lists of parts, etc).
69
+ _to (Any): Must be an instance of BaseInteractablePart or a subclass.
70
+ Also it can be any nested iterable of instances (list of parts, list of lists of parts, etc).
71
+ parallel (bool, optional): Defines the behaviour of the connections in the following way:
72
+
73
+ With parallel=False, everything connects to everything:
74
+ from1 🔀 to1
75
+
76
+ from2 🔀 to2
77
+
78
+ With parallel=True, every row is connected respectively:
79
+ from1 → to1
80
+
81
+ from2 → to2
82
+
83
+ Also, if the dimensions does not match it tries to adapt (many to one, one to many, etc)
84
+
85
+ Defaults to True.
86
+ """
87
+ if isinstance(_from, BaseInteractablePart) and isinstance(_to, BaseInteractablePart):
88
+ _from.connect(_to)
89
+ return
90
+ if parallel: # Try connect things row-by-row if possible (one to one, one to many, many to many)
91
+ if not isinstance(_from, BaseInteractablePart) and not isinstance(_to, BaseInteractablePart): # Assume both are sequence of parts
92
+ for subfrom, subto in zip(_from, _to):
93
+ connect(subfrom, subto, parallel=parallel)
94
+ elif not isinstance(_from, BaseInteractablePart): # Assume _from is a sequence of parts
95
+ for subfrom in _from:
96
+ connect(subfrom, _to, parallel=parallel)
97
+ else: # Assume _to is a sequence of parts
98
+ for subto in _to:
99
+ connect(_from, subto, parallel=parallel)
100
+ else: # Just connect everything to everything lol
101
+ if not isinstance(_from, BaseInteractablePart) and not isinstance(_to, BaseInteractablePart): # Assume both are sequence of parts
102
+ for subfrom in _from:
103
+ for subto in _to:
104
+ connect(subfrom, subto, parallel=parallel)
105
+ elif not isinstance(_from, BaseInteractablePart): # Assume _from is a sequence of parts
106
+ for subfrom in _from:
107
+ connect(subfrom, _to, parallel=parallel)
108
+ else: # Assume _to is a sequence of parts
109
+ for subto in _to:
110
+ connect(_from, subto, parallel=parallel)
111
+
112
+
113
+ def check_pos(pos):
114
+ if not isinstance(pos, Pos):
115
+ pos = Pos(*pos)
116
+ return pos
117
+
118
+
119
+ def get_bits_required(number: int | float):
120
+ """Calculates how many bits are required to store this number.
121
+
122
+ Args:
123
+ number (int | float): The target number.
124
+ """
125
+ return ceil(log2(number))
126
+
127
+
128
+ def num_to_bit_list(number: int, bit_length: int):
129
+ """Converts a number to a numpy array of its bits.
130
+
131
+ Args:
132
+ number (int): The number to convert.
133
+ bit_length (int): The number of bits the list will have.
134
+ """
135
+ output = ndarray(bit_length, dtype=bool)
136
+ for b in range(bit_length):
137
+ output[b] = bool((number >> b) & 1)
138
+ return output
@@ -0,0 +1,20 @@
1
+ from dataclasses import dataclass, field
2
+ from typing import Any, Optional
3
+
4
+ from sm_blueprint_lib.constants import get_new_id
5
+ from sm_blueprint_lib.id import ID
6
+
7
+
8
+ @dataclass
9
+ class BaseController:
10
+ """Base class for controller objects (used in interactable parts and so)
11
+ """
12
+ controllers: Optional[list[ID]] = field(kw_only=True, default=None)
13
+ id: int = field(kw_only=True, default_factory=get_new_id)
14
+ joints: Optional[list[ID]] = field(kw_only=True, default=None)
15
+
16
+ def __post_init__(self):
17
+ try:
18
+ self.controllers = [ID(**c) for c in self.controllers]
19
+ except TypeError:
20
+ pass
@@ -0,0 +1,10 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from sm_blueprint_lib.bases.controllers.basecontroller import BaseController
4
+
5
+
6
+ @dataclass
7
+ class BaseLogicController(BaseController):
8
+ """Base class for Logic parts' Controllers (mostly Logic Gate and Timer)
9
+ """
10
+ active: bool = field(kw_only=True, default=False)
@@ -0,0 +1,10 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from sm_blueprint_lib.bases.controllers.baselogiccontroller import BaseLogicController
4
+
5
+
6
+ @dataclass
7
+ class LogicGateController(BaseLogicController):
8
+ """Logic Gate's Controller
9
+ """
10
+ mode: int = 0
@@ -0,0 +1,24 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from sm_blueprint_lib.bases.controllers.basecontroller import BaseController
4
+
5
+
6
+ @dataclass
7
+ class SensorController(BaseController):
8
+ """Sensor's Controller
9
+ """
10
+ audioEnable: bool
11
+ buttonMode: bool
12
+ color: str
13
+ colorMode: bool
14
+ range: int
15
+
16
+ def __post_init__(self):
17
+ super().__post_init__()
18
+ # if color given as (r, g, b) then convert to hex string
19
+ if not isinstance(self.color, str):
20
+ self.color = "%02X%02X%02X" % (
21
+ self.color[0], self.color[1], self.color[2])
22
+ self.audioEnable = bool(self.audioEnable)
23
+ self.buttonMode = bool(self.buttonMode)
24
+ self.colorMode = bool(self.colorMode)
@@ -0,0 +1,11 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from sm_blueprint_lib.bases.controllers.baselogiccontroller import BaseLogicController
4
+
5
+
6
+ @dataclass
7
+ class TimerController(BaseLogicController):
8
+ """Timer's Controller
9
+ """
10
+ seconds: int
11
+ ticks: int
@@ -0,0 +1,31 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from sm_blueprint_lib.bases.parts.basepart import BasePart
4
+ from sm_blueprint_lib.body import Body
5
+ from sm_blueprint_lib.constants import VERSION
6
+
7
+
8
+ @dataclass
9
+ class Blueprint:
10
+ bodies: list[Body] = field(default_factory=lambda: [Body()])
11
+ version: int = VERSION.BLUEPRINT_VERSION
12
+
13
+ def __post_init__(self):
14
+ try:
15
+ self.bodies = [Body(**body) for body in self.bodies]
16
+ except TypeError:
17
+ pass
18
+
19
+ def add(self, *obj, body=0):
20
+ """Adds the object(s) to the blueprint.
21
+
22
+ Args:
23
+ obj (Any): Can be a instance of BasePart or a subclass. It also can be any nested iterable of instances (list of parts, list of lists of parts, etc).
24
+ body (int, optional): Specify in which blueprint's body the object will be placed. Defaults to 0.
25
+ """
26
+ for subobj in obj:
27
+ if isinstance(subobj, BasePart):
28
+ self.bodies[body].childs.append(subobj)
29
+ else:
30
+ for subsubobj in subobj:
31
+ self.add(subsubobj, body=body)
@@ -0,0 +1,13 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from sm_blueprint_lib.bases.parts.basepart import BasePart
4
+ from sm_blueprint_lib.constants import SHAPEID
5
+
6
+
7
+ @dataclass
8
+ class Body:
9
+ childs: list[BasePart] = field(default_factory=list)
10
+
11
+ def __post_init__(self):
12
+ self.childs = [SHAPEID.SHAPEID_TO_CLASS[child["shapeId"]](**child)
13
+ for child in self.childs]
@@ -0,0 +1,10 @@
1
+ from dataclasses import dataclass
2
+
3
+
4
+ @dataclass
5
+ class Bounds:
6
+ """Class that represents the bounds of a boundable block (x, y, z)
7
+ """
8
+ x: int
9
+ y: int
10
+ z: int
@@ -0,0 +1,29 @@
1
+ class SHAPEID:
2
+ BARRIER_BLOCK = "09ca2713-28ee-4119-9622-e85490034758"
3
+ LOGIC_GATE = "9f0f56e8-2c31-4d83-996c-d00a9b296c3f"
4
+ TIMER = "8f7fd0e7-c46e-4944-a414-7ce2437bb30f"
5
+ SENSOR5 = "20dcd41c-0a11-4668-9b00-97f278ce21af"
6
+ SWITCH = "7cf717d7-d167-4f2d-a6e7-6b2c70aa3986"
7
+ SHAPEID_TO_CLASS = {}
8
+
9
+
10
+ class AXIS:
11
+ DEFAULT_XAXIS = 1
12
+ DEFAULT_ZAXIS = 3
13
+
14
+
15
+ class VERSION:
16
+ BLUEPRINT_VERSION = 4
17
+
18
+
19
+ TICKS_PER_SECOND = 40
20
+
21
+ __global_id_counter = 0
22
+ """Atempting to modify this global variable may cause to break your blueprints lol.
23
+ """
24
+
25
+
26
+ def get_new_id():
27
+ global __global_id_counter
28
+ __global_id_counter = (new_id := __global_id_counter) + 1
29
+ return new_id
sm_blueprint_lib/id.py ADDED
@@ -0,0 +1,5 @@
1
+ from dataclasses import dataclass
2
+
3
+ @dataclass
4
+ class ID:
5
+ id: int
@@ -0,0 +1,16 @@
1
+ from dataclasses import dataclass
2
+ from typing import Sequence
3
+
4
+
5
+ @dataclass
6
+ class Pos:
7
+ """Class that represents the position of a block (x, y, z)
8
+ """
9
+ x: int
10
+ y: int
11
+ z: int
12
+
13
+ def __add__(self, o: "Pos" | Sequence):
14
+ if isinstance(o, Pos):
15
+ return Pos(self.x + o.x, self.y + o.y, self.z + o.z)
16
+ return Pos(self.x + o[0], self.y + o[1], self.z + o[2])
@@ -0,0 +1,44 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+
8
+
9
+ def simple_adder_subtractor(bp: Blueprint,
10
+ bit_length: int,
11
+ pos: Pos | Sequence = (0, 0, 0)):
12
+ pos = check_pos(pos)
13
+ input_a = [LogicGate(pos+(x, 0, 2), "FF0000", 1)
14
+ for x in range(bit_length)]
15
+ input_b = [LogicGate(pos+(x, 0, 0), "FF0000", 2)
16
+ for x in range(bit_length)]
17
+ and_0 = [LogicGate(pos+(x, -1, 2), "000000", 0)
18
+ for x in range(bit_length)]
19
+ xor_0 = [LogicGate(pos+(x, -1, 0), "000000", 2)
20
+ for x in range(bit_length)]
21
+ or_0 = [LogicGate(pos+((x, -2, 0) if x + 1 != bit_length else (x+1, -2, 2)), "000000" if x + 1 != bit_length else "0000FF", 1)
22
+ for x in range(bit_length)]
23
+ and_1 = [LogicGate(pos+(x, -2, 1), "000000", 0)
24
+ for x in range(bit_length)]
25
+ xor_1 = [LogicGate(pos+(x, -2, 2), "0000FF", 2)
26
+ for x in range(bit_length)]
27
+ carry_in = LogicGate(pos+(-1, -2, 0), "FF0000", 1)
28
+
29
+ connect(input_a, and_0)
30
+ connect(input_a, xor_0)
31
+ connect(input_b, and_0)
32
+ connect(input_b, xor_0)
33
+ connect(and_0, or_0)
34
+ connect(xor_0, and_1)
35
+ connect(xor_0, xor_1)
36
+ connect(and_1, or_0)
37
+ connect(or_0, and_1[1:])
38
+ connect(or_0, xor_1[1:])
39
+ connect(carry_in, and_1[0])
40
+ connect(carry_in, xor_1[0])
41
+ connect(carry_in, input_b)
42
+
43
+ bp.add(input_a, input_b, and_0, xor_0, or_0, and_1, xor_1, carry_in)
44
+ return input_a, input_b, and_0, xor_0, or_0, and_1, xor_1, carry_in
@@ -0,0 +1,86 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+
8
+
9
+ def barrel_shifter(bp: Blueprint, bit_length: int, num_bit_shift: int, pos: Pos | Sequence = (0, 0, 0)):
10
+ pos = check_pos(pos)
11
+ inputs = [LogicGate(pos + (x, 0, 0), "FF0000", 1)
12
+ for x in range(bit_length)]
13
+ inputs_shift_binary = ndarray((num_bit_shift, 2), dtype=LogicGate)
14
+ # always_off = LogicGate(pos + (-1, -1, 0), "000000")
15
+
16
+ for x in range(num_bit_shift):
17
+ inputs_shift_binary[x, :] = [
18
+ LogicGate(pos + (x-num_bit_shift, -1, 0), "FF0000", 4),
19
+ LogicGate(pos + (x-num_bit_shift, 0, 0), "FF0000", 1),
20
+ ]
21
+
22
+ arr = [
23
+ [
24
+ [LogicGate(pos+(x, (-y-1), 0), "000000")
25
+ for x in range(bit_length + (2**y - 1 if y != num_bit_shift - 1 else 0))]
26
+ for y in range(num_bit_shift)
27
+ ],
28
+ [
29
+ [LogicGate(pos+((x - (2**y if y == num_bit_shift-1 else 0)), (-y-1), 1), "000000")
30
+ for x in range(2**y, bit_length + 2**(y+1) - 1 - (2**y if y == num_bit_shift-1 else 0))]
31
+ for y in range(num_bit_shift)
32
+ ],
33
+ [
34
+ [LogicGate(pos+(x, (-y-1), 2), "000000" if y != num_bit_shift - 1 else "0000FF", 1)
35
+ for x in range(bit_length - 1 + (2**(y+1) if y != num_bit_shift-1 else 1))]
36
+ for y in range(num_bit_shift)
37
+ ],
38
+ ]
39
+
40
+ connect(inputs, arr[0][0])
41
+ connect(inputs, arr[1][0])
42
+ for y in range(num_bit_shift):
43
+ connect(arr[0][y], arr[2][y])
44
+ for y in range(num_bit_shift-1):
45
+ connect(arr[2][y], arr[0][y+1])
46
+ connect(arr[1][y], arr[2][y][2**y:])
47
+ for y in range(num_bit_shift-2):
48
+ connect(arr[2][y], arr[1][y+1])
49
+ connect(arr[2][-2][2**(num_bit_shift-1):], arr[1][-1])
50
+ connect(arr[1][-1], arr[2][-1])
51
+ for y in range(num_bit_shift):
52
+ connect(inputs_shift_binary[y, 0], arr[0][y])
53
+ connect(inputs_shift_binary[y, 1], arr[1][y])
54
+
55
+ bp.add(inputs, inputs_shift_binary, arr)
56
+ # arr = ndarray((bit_length, num_bit_shift, 3), dtype=LogicGate)
57
+ # for x in range(num_bit_shift):
58
+ # inputs_shift_binary[x, :] = [
59
+ # LogicGate(pos + (x+bit_length+1, -1, 0), "FF0000", 4),
60
+ # LogicGate(pos + (x+bit_length+1, 0, 0), "FF0000", 1),
61
+ # ]
62
+ # for x in range(bit_length):
63
+ # for y in range(num_bit_shift):
64
+ # arr[x, y, :] = [
65
+ # LogicGate(pos + (x, -y-1, 0), "000000"),
66
+ # LogicGate(pos + (x, -y-1, 1), "000000"),
67
+ # LogicGate(pos + (x, -y-1, 2), "000000"
68
+ # if y + 1 < num_bit_shift else "0000FF", 1),
69
+ # ]
70
+ # always_off.connect(always_off)
71
+
72
+ # connect(inputs, arr[:, 0, 0])
73
+ # connect(inputs, arr[1:, 0, 1])
74
+ # connect(arr[:, :, 0], arr[:, :, 2])
75
+ # connect(arr[:, :, 1], arr[:, :, 2])
76
+ # connect(arr[:, :, 2], arr[:, 1:, 0])
77
+ # for y in range(num_bit_shift):
78
+ # if y + 1 < num_bit_shift:
79
+ # connect(arr[:, y, 2], arr[2**(y+1):, y+1, 1])
80
+ # connect(always_off, arr[:2**y, y, 1])
81
+ # connect(inputs_shift_binary[y, 0], arr[:, y, 0])
82
+ # connect(inputs_shift_binary[y, 1], arr[:, y, 1])
83
+
84
+ # bp.add(inputs, inputs_shift_binary, arr, always_off)
85
+
86
+ # return inputs, inputs_shift_binary, arr, always_off
@@ -0,0 +1,28 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+
8
+
9
+ def clock40hz(bp: Blueprint, bit_length: int, pos: Pos | Sequence = (0, 0, 0)):
10
+ pos = check_pos(pos)
11
+
12
+ arr = ndarray((bit_length, 2), dtype=LogicGate)
13
+ for x in range(bit_length):
14
+ arr[x, :] = [
15
+ LogicGate(pos + (x, 0, 0), "0000FF", 2 if x else 4),
16
+ LogicGate(pos + (x, 1, 0), "000000", 0),
17
+ ]
18
+
19
+ connect(arr[:, 0], arr[:, 0])
20
+ connect(arr[1:, 1], arr[1:, 0])
21
+ connect(arr[0, 0], arr[0, 1])
22
+ connect(arr[0, 1], arr[1:, 1])
23
+ for x in range(1, bit_length):
24
+ connect(arr[x, 0], arr[x+1:, 1])
25
+
26
+ bp.add(arr)
27
+
28
+ return arr
@@ -0,0 +1,70 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import check_pos, connect
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+
8
+
9
+ def comparator(bp: Blueprint,
10
+ bit_length: int,
11
+ pos: Pos | Sequence = (0, 0, 0)):
12
+ pos = check_pos(pos)
13
+ inputs = ndarray((bit_length, 4), dtype=LogicGate)
14
+ ands_2 = ndarray((bit_length, 2), dtype=LogicGate)
15
+ nors_3 = ndarray(bit_length, dtype=LogicGate)
16
+ ands_4 = ndarray(2 * (bit_length - 1) + 1, dtype=LogicGate)
17
+ ors_5 = ndarray(2, dtype=LogicGate)
18
+ inputs[:, 0] = [
19
+ LogicGate(pos+(x, -1, 0), "FF0000", 4)
20
+ for x in range(bit_length)
21
+ ]
22
+ inputs[:, 1] = [
23
+ LogicGate(pos+(x, 0, 0), "FF0000", 1)
24
+ for x in range(bit_length)
25
+ ]
26
+ inputs[:, 2] = [
27
+ LogicGate(pos+(x, -1, 2), "FF0000", 4)
28
+ for x in range(bit_length)
29
+ ]
30
+ inputs[:, 3] = [
31
+ LogicGate(pos+(x, 0, 2), "FF0000", 1)
32
+ for x in range(bit_length)
33
+ ]
34
+ for x in range(bit_length):
35
+ ands_2[x, :] = [
36
+ LogicGate(pos+(x, -2, 0), "000000"),
37
+ LogicGate(pos+(x, -2, 1), "000000"),
38
+ ]
39
+ nors_3[x] = LogicGate(pos+(x, -2, 2), "000000", 4)
40
+
41
+ for n in range(2 * (bit_length - 1) + 1):
42
+ if n == 0:
43
+ ands_4[n] = LogicGate(pos+(1, -4, 0), "0000FF")
44
+ else:
45
+ ands_4[n] = LogicGate(pos+((n-1)//2, -3, (n-1) % 2), "000000")
46
+
47
+ ors_5[:] = [
48
+ LogicGate(pos+(0, -4, 0), "0000FF", 1),
49
+ LogicGate(pos+(2, -4, 0), "0000FF", 1),
50
+ ]
51
+
52
+ connect(inputs[:, 0], ands_2[:, 1])
53
+ connect(inputs[:, 1], ands_2[:, 0])
54
+ connect(inputs[:, 2], ands_2[:, 0])
55
+ connect(inputs[:, 3], ands_2[:, 1])
56
+
57
+ connect(ands_2, nors_3)
58
+
59
+ connect(ands_2.flat, ands_4[1:])
60
+ for x in range(bit_length):
61
+ connect(nors_3[x], ands_4[:2*x+1])
62
+
63
+ connect(ands_4[1::2], ors_5[0])
64
+ connect(ands_4[2::2], ors_5[1])
65
+ connect(ands_2[-1, 0], ors_5[0])
66
+ connect(ands_2[-1, 1], ors_5[1])
67
+
68
+ bp.add(inputs, ands_2, nors_3, ands_4, ors_5)
69
+
70
+ return inputs, ands_2, nors_3, ands_4, ors_5
@@ -0,0 +1,68 @@
1
+ from typing import Sequence
2
+ from numpy import ndarray
3
+ from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from sm_blueprint_lib.blueprint import Blueprint
5
+ from sm_blueprint_lib.parts.logicgate import LogicGate
6
+ from sm_blueprint_lib.pos import Pos
7
+
8
+
9
+ def counter(bp: Blueprint,
10
+ bit_length: int,
11
+ pos: Pos | Sequence = (0, 0, 0),
12
+ precreated_swxors=None, precreated_ands=None, precreated_count=None):
13
+ pos = check_pos(pos)
14
+ arr = ndarray((bit_length, 2), dtype=LogicGate)
15
+ count = precreated_count if precreated_count is not None else LogicGate(
16
+ pos + (-1, 1, 0), "FF0000", 1)
17
+ for x in range(bit_length):
18
+ arr[x, :] = [
19
+ precreated_swxors[x] if precreated_swxors is not None else LogicGate(
20
+ pos + (x, 0, 0), "0000FF", 2),
21
+ precreated_ands[x] if precreated_ands is not None else LogicGate(
22
+ pos + (x, 1, 0), "000000"),
23
+ ]
24
+ if precreated_swxors is None:
25
+ connect(arr[:, 0], arr[:, 0])
26
+ connect(arr[:, 1], arr[:, 0])
27
+ for x in range(bit_length):
28
+ connect(arr[x, 0], arr[x+1:, 1])
29
+ connect(count, arr[:, 1])
30
+
31
+ if precreated_swxors is None:
32
+ bp.add(arr[:, 0])
33
+ if precreated_ands is None:
34
+ bp.add(arr[:, 1])
35
+ if precreated_count is None:
36
+ bp.add(count)
37
+ return arr, count
38
+
39
+
40
+ def counter_decrement(bp: Blueprint,
41
+ bit_length: int,
42
+ pos: Pos | Sequence = (0, 0, 0),
43
+ precreated_swxors=None, precreated_nors=None, precreated_count_nor=None):
44
+ pos = check_pos(pos)
45
+ arr = ndarray((bit_length, 2), dtype=LogicGate)
46
+ count = precreated_count_nor if precreated_count_nor is not None else LogicGate(
47
+ pos + (-1, 1, 0), "FF0000", 4)
48
+ for x in range(bit_length):
49
+ arr[x, :] = [
50
+ precreated_swxors[x] if precreated_swxors is not None else LogicGate(
51
+ pos + (x, 0, 0), "0000FF", 2),
52
+ precreated_nors[x] if precreated_nors is not None else LogicGate(
53
+ pos + (x, 1, 0), "000000", 4),
54
+ ]
55
+ if precreated_swxors is None:
56
+ connect(arr[:, 0], arr[:, 0])
57
+ connect(arr[:, 1], arr[:, 0])
58
+ for x in range(bit_length):
59
+ connect(arr[x, 0], arr[x+1:, 1])
60
+ connect(count, arr[:, 1])
61
+
62
+ if precreated_swxors is None:
63
+ bp.add(arr[:, 0])
64
+ if precreated_nors is None:
65
+ bp.add(arr[:, 1])
66
+ if precreated_count_nor is None:
67
+ bp.add(count)
68
+ return arr, count