sm-blueprint-lib 0.0.4__py3-none-any.whl → 0.0.6__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.
Files changed (35) hide show
  1. sm_blueprint_lib/__init__.py +10 -138
  2. sm_blueprint_lib/bases/controllers/basecontroller.py +2 -2
  3. sm_blueprint_lib/bases/controllers/baselogiccontroller.py +1 -1
  4. sm_blueprint_lib/bases/controllers/logicgatecontroller.py +1 -1
  5. sm_blueprint_lib/bases/controllers/sensorcontroller.py +1 -1
  6. sm_blueprint_lib/bases/controllers/timercontroller.py +1 -1
  7. sm_blueprint_lib/bases/parts/baseboundablepart.py +23 -0
  8. sm_blueprint_lib/bases/parts/baseinteractablepart.py +19 -0
  9. sm_blueprint_lib/bases/parts/baselogicpart.py +13 -0
  10. sm_blueprint_lib/bases/parts/basepart.py +34 -0
  11. sm_blueprint_lib/blueprint.py +3 -3
  12. sm_blueprint_lib/body.py +2 -2
  13. sm_blueprint_lib/parts/barrierblock.py +9 -0
  14. sm_blueprint_lib/parts/logicgate.py +24 -0
  15. sm_blueprint_lib/parts/sensor.py +24 -0
  16. sm_blueprint_lib/parts/switch.py +9 -0
  17. sm_blueprint_lib/parts/timer.py +20 -0
  18. sm_blueprint_lib/prebuilds/adder.py +4 -4
  19. sm_blueprint_lib/prebuilds/barrel_shifter.py +4 -4
  20. sm_blueprint_lib/prebuilds/clock40hz.py +4 -4
  21. sm_blueprint_lib/prebuilds/comparator.py +4 -4
  22. sm_blueprint_lib/prebuilds/counter.py +4 -4
  23. sm_blueprint_lib/prebuilds/decoder.py +4 -4
  24. sm_blueprint_lib/prebuilds/distance_sensor.py +6 -6
  25. sm_blueprint_lib/prebuilds/ram.py +5 -5
  26. sm_blueprint_lib/prebuilds/register.py +5 -5
  27. sm_blueprint_lib/prebuilds/rom.py +6 -6
  28. sm_blueprint_lib/prebuilds/timer_ram_cached.py +13 -13
  29. sm_blueprint_lib/prebuilds/timer_ram_multiclient.py +9 -9
  30. sm_blueprint_lib/utils.py +138 -0
  31. {sm_blueprint_lib-0.0.4.dist-info → sm_blueprint_lib-0.0.6.dist-info}/METADATA +1 -1
  32. sm_blueprint_lib-0.0.6.dist-info/RECORD +38 -0
  33. sm_blueprint_lib-0.0.4.dist-info/RECORD +0 -28
  34. {sm_blueprint_lib-0.0.4.dist-info → sm_blueprint_lib-0.0.6.dist-info}/WHEEL +0 -0
  35. {sm_blueprint_lib-0.0.4.dist-info → sm_blueprint_lib-0.0.6.dist-info}/licenses/LICENSE +0 -0
@@ -1,138 +1,10 @@
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
1
+ from .blueprint import Blueprint
2
+ from .body import *
3
+ from .bounds import *
4
+ from .constants import *
5
+ from .id import *
6
+ from .pos import *
7
+ from .utils import *
8
+
9
+ from .parts import *
10
+ from .prebuilds import *
@@ -1,8 +1,8 @@
1
1
  from dataclasses import dataclass, field
2
2
  from typing import Any, Optional
3
3
 
4
- from sm_blueprint_lib.constants import get_new_id
5
- from sm_blueprint_lib.id import ID
4
+ from ...constants import get_new_id
5
+ from ...id import ID
6
6
 
7
7
 
8
8
  @dataclass
@@ -1,6 +1,6 @@
1
1
  from dataclasses import dataclass, field
2
2
 
3
- from sm_blueprint_lib.bases.controllers.basecontroller import BaseController
3
+ from .basecontroller import BaseController
4
4
 
5
5
 
6
6
  @dataclass
@@ -1,6 +1,6 @@
1
1
  from dataclasses import dataclass, field
2
2
 
3
- from sm_blueprint_lib.bases.controllers.baselogiccontroller import BaseLogicController
3
+ from .baselogiccontroller import BaseLogicController
4
4
 
5
5
 
6
6
  @dataclass
@@ -1,6 +1,6 @@
1
1
  from dataclasses import dataclass, field
2
2
 
3
- from sm_blueprint_lib.bases.controllers.basecontroller import BaseController
3
+ from .basecontroller import BaseController
4
4
 
5
5
 
6
6
  @dataclass
@@ -1,6 +1,6 @@
1
1
  from dataclasses import dataclass, field
2
2
 
3
- from sm_blueprint_lib.bases.controllers.baselogiccontroller import BaseLogicController
3
+ from .baselogiccontroller import BaseLogicController
4
4
 
5
5
 
6
6
  @dataclass
@@ -0,0 +1,23 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from .basepart import BasePart
4
+ from ...bounds import Bounds
5
+ from ...constants import AXIS
6
+
7
+
8
+ @dataclass
9
+ class BaseBoundablePart(BasePart):
10
+ """Base class for all Boundable parts (those that are draggable)
11
+ """
12
+ bounds: Bounds
13
+ xaxis: int = field(kw_only=True, default=AXIS.DEFAULT_XAXIS)
14
+ zaxis: int = field(kw_only=True, default=AXIS.DEFAULT_ZAXIS)
15
+
16
+ def __post_init__(self):
17
+ super().__post_init__()
18
+ if not isinstance(self.bounds, Bounds):
19
+ try:
20
+ self.bounds = Bounds(**self.bounds)
21
+ except TypeError:
22
+ self.bounds = Bounds(
23
+ self.bounds[0], self.bounds[1], self.bounds[2])
@@ -0,0 +1,19 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ..controllers.basecontroller import BaseController
4
+ from .basepart import BasePart
5
+ from ...id import ID
6
+
7
+
8
+ @dataclass
9
+ class BaseInteractablePart(BasePart):
10
+ """Base class for Interactable parts
11
+ """
12
+ controller: BaseController = field(default_factory=BaseController)
13
+
14
+ def connect(self, o: "BaseInteractablePart"):
15
+ if not self.controller.controllers:
16
+ self.controller.controllers = []
17
+
18
+ self.controller.controllers.append(ID(o.controller.id))
19
+ return o
@@ -0,0 +1,13 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from .baseinteractablepart import BaseInteractablePart
4
+ from ..controllers.baselogiccontroller import BaseLogicController
5
+
6
+
7
+ @dataclass
8
+ class BaseLogicPart(BaseInteractablePart):
9
+ """Base class for Logic parts with active state (mostly Logic Gate and Timer)
10
+ """
11
+ controller: BaseLogicController = field(default_factory=BaseLogicController)
12
+ xaxis: int = field(kw_only=True, default=1)
13
+ zaxis: int = field(kw_only=True, default=-2)
@@ -0,0 +1,34 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ...constants import AXIS, SHAPEID
4
+ from ...pos import Pos
5
+
6
+
7
+ @dataclass
8
+ class BasePart:
9
+ """Base class for all in-game parts
10
+ """
11
+ shapeId: str
12
+ pos: Pos
13
+ color: str
14
+ xaxis: int = field(kw_only=True, default=AXIS.DEFAULT_XAXIS)
15
+ zaxis: int = field(kw_only=True, default=AXIS.DEFAULT_ZAXIS)
16
+
17
+ def __post_init__(self):
18
+ # if pos given as {"x": ..., "y": ..., "z": ...} or (x, y, z) then convert to Pos class
19
+ if not isinstance(self.pos, Pos):
20
+ try:
21
+ self.pos = Pos(**self.pos)
22
+ except TypeError:
23
+ self.pos = Pos(self.pos[0], self.pos[1], self.pos[2])
24
+ # if color given as (r, g, b) then convert to hex string
25
+ if not isinstance(self.color, str):
26
+ self.color = "%02X%02X%02X" % (
27
+ self.color[0], self.color[1], self.color[2])
28
+
29
+ def __init_subclass__(cls):
30
+ super().__init_subclass__()
31
+ try:
32
+ SHAPEID.SHAPEID_TO_CLASS[cls.shapeId.default] = cls
33
+ except AttributeError:
34
+ pass
@@ -1,8 +1,8 @@
1
1
  from dataclasses import dataclass, field
2
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
3
+ from .bases.parts.basepart import BasePart
4
+ from .body import Body
5
+ from .constants import VERSION
6
6
 
7
7
 
8
8
  @dataclass
sm_blueprint_lib/body.py CHANGED
@@ -1,7 +1,7 @@
1
1
  from dataclasses import dataclass, field
2
2
 
3
- from sm_blueprint_lib.bases.parts.basepart import BasePart
4
- from sm_blueprint_lib.constants import SHAPEID
3
+ from .bases.parts.basepart import BasePart
4
+ from .constants import SHAPEID
5
5
 
6
6
 
7
7
  @dataclass
@@ -0,0 +1,9 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ..bases.parts.baseboundablepart import BaseBoundablePart
4
+ from ..constants import SHAPEID
5
+
6
+
7
+ @dataclass
8
+ class BarrierBlock(BaseBoundablePart):
9
+ shapeId: str = field(kw_only=True, default=SHAPEID.BARRIER_BLOCK)
@@ -0,0 +1,24 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ..bases.parts.baselogicpart import BaseLogicPart
4
+ from ..bases.controllers.logicgatecontroller import LogicGateController
5
+ from ..constants import SHAPEID
6
+
7
+
8
+ @dataclass
9
+ class LogicGate(BaseLogicPart):
10
+ controller: LogicGateController = field(
11
+ default_factory=LogicGateController)
12
+ shapeId: str = field(kw_only=True, default=SHAPEID.LOGIC_GATE)
13
+
14
+ def __post_init__(self):
15
+ super().__post_init__()
16
+ # Can specify mode as a dict, a tuple (mode,) or just the parameter mode
17
+ if not isinstance(self.controller, LogicGateController):
18
+ try:
19
+ self.controller = LogicGateController(**self.controller)
20
+ except TypeError:
21
+ try:
22
+ self.controller = LogicGateController(*self.controller)
23
+ except TypeError:
24
+ self.controller = LogicGateController(self.controller)
@@ -0,0 +1,24 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ..bases.controllers.sensorcontroller import SensorController
4
+ from ..bases.parts.baseinteractablepart import BaseInteractablePart
5
+ from ..constants import SHAPEID
6
+
7
+
8
+ @dataclass
9
+ class Sensor5(BaseInteractablePart):
10
+ controller: SensorController = field(
11
+ default_factory=SensorController)
12
+ shapeId: str = field(kw_only=True, default=SHAPEID.SENSOR5)
13
+
14
+ def __post_init__(self):
15
+ super().__post_init__()
16
+ # Can specify sensor controller as a dict, a tuple (audioEnable, buttonMode, color, colorMode, range) or just the parameter mode
17
+ if not isinstance(self.controller, SensorController):
18
+ try:
19
+ self.controller = SensorController(**self.controller)
20
+ except TypeError:
21
+ try:
22
+ self.controller = SensorController(*self.controller)
23
+ except TypeError:
24
+ self.controller = SensorController(self.controller)
@@ -0,0 +1,9 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ..bases.parts.baseinteractablepart import BaseInteractablePart
4
+ from ..constants import SHAPEID
5
+
6
+
7
+ @dataclass
8
+ class Switch(BaseInteractablePart):
9
+ raise NotImplemented
@@ -0,0 +1,20 @@
1
+ from dataclasses import dataclass, field
2
+
3
+ from ..bases.controllers.timercontroller import TimerController
4
+ from ..bases.parts.baselogicpart import BaseLogicPart
5
+ from ..constants import SHAPEID
6
+
7
+
8
+ @dataclass
9
+ class Timer(BaseLogicPart):
10
+ controller: TimerController = field(default_factory=TimerController)
11
+ shapeId: str = field(kw_only=True, default=SHAPEID.TIMER)
12
+
13
+ def __post_init__(self):
14
+ super().__post_init__()
15
+ # Can provide seconds and ticks as a the TimerController class itself, a dict or a tuple (seconds, tick)
16
+ if not isinstance(self.controller, TimerController):
17
+ try:
18
+ self.controller = TimerController(**self.controller)
19
+ except TypeError:
20
+ self.controller = TimerController(*self.controller)
@@ -1,9 +1,9 @@
1
1
  from typing import Sequence
2
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
3
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
7
 
8
8
 
9
9
  def simple_adder_subtractor(bp: Blueprint,
@@ -1,9 +1,9 @@
1
1
  from typing import Sequence
2
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
3
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
7
 
8
8
 
9
9
  def barrel_shifter(bp: Blueprint, bit_length: int, num_bit_shift: int, pos: Pos | Sequence = (0, 0, 0)):
@@ -1,9 +1,9 @@
1
1
  from typing import Sequence
2
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
3
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
7
 
8
8
 
9
9
  def clock40hz(bp: Blueprint, bit_length: int, pos: Pos | Sequence = (0, 0, 0)):
@@ -1,9 +1,9 @@
1
1
  from typing import Sequence
2
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
3
+ from ..utils import check_pos, connect
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
7
 
8
8
 
9
9
  def comparator(bp: Blueprint,
@@ -1,9 +1,9 @@
1
1
  from typing import Sequence
2
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
3
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
7
 
8
8
 
9
9
  def counter(bp: Blueprint,
@@ -1,9 +1,9 @@
1
1
  from typing import Sequence
2
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
3
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
7
 
8
8
 
9
9
  def decoder(bp: Blueprint, num_address: int, pos: Pos | Sequence = (0, 0, 0),
@@ -1,11 +1,11 @@
1
1
  from typing import Sequence
2
2
  from numpy import array, 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.parts.sensor import Sensor5
7
- from sm_blueprint_lib.parts.timer import Timer
8
- from sm_blueprint_lib.pos import Pos
3
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..parts.sensor import Sensor5
7
+ from ..parts.timer import Timer
8
+ from ..pos import Pos
9
9
 
10
10
 
11
11
  def distance_sensor(bp: Blueprint,
@@ -1,11 +1,11 @@
1
1
  from itertools import cycle
2
2
  from typing import Sequence
3
3
  from numpy import ndarray
4
- from sm_blueprint_lib import get_bits_required, check_pos, connect
5
- from sm_blueprint_lib.blueprint import Blueprint
6
- from sm_blueprint_lib.parts.logicgate import LogicGate
7
- from sm_blueprint_lib.pos import Pos
8
- from sm_blueprint_lib.prebuilds.decoder import decoder
4
+ from ..utils import get_bits_required, check_pos, connect
5
+ from ..blueprint import Blueprint
6
+ from ..parts.logicgate import LogicGate
7
+ from ..pos import Pos
8
+ from ..prebuilds.decoder import decoder
9
9
 
10
10
 
11
11
  def ram(bp: Blueprint, bit_length: int, num_address: int, pos: Pos | Sequence = (0, 0, 0),
@@ -1,10 +1,10 @@
1
1
  from typing import Sequence
2
2
  from numpy import ndarray
3
- from sm_blueprint_lib import check_pos
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
- from sm_blueprint_lib.prebuilds.counter import counter, counter_decrement
3
+ from ..utils import check_pos
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..pos import Pos
7
+ from ..prebuilds.counter import counter, counter_decrement
8
8
 
9
9
 
10
10
  def register(bp: Blueprint,
@@ -2,12 +2,12 @@ from itertools import batched, cycle
2
2
  from math import ceil
3
3
  from typing import Sequence
4
4
  from numpy import ndarray
5
- from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
6
- from sm_blueprint_lib.blueprint import Blueprint
7
- from sm_blueprint_lib.parts.logicgate import LogicGate
8
- from sm_blueprint_lib.prebuilds.decoder import decoder
9
- from sm_blueprint_lib.parts.timer import Timer
10
- from sm_blueprint_lib.pos import Pos
5
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
6
+ from ..blueprint import Blueprint
7
+ from ..parts.logicgate import LogicGate
8
+ from ..prebuilds.decoder import decoder
9
+ from ..parts.timer import Timer
10
+ from ..pos import Pos
11
11
 
12
12
 
13
13
  def rom(
@@ -1,19 +1,19 @@
1
1
  from itertools import cycle
2
2
  from typing import Sequence
3
3
  from numpy import array, clip, ndarray
4
- from sm_blueprint_lib import get_bits_required, check_pos, connect, num_to_bit_list
5
- from sm_blueprint_lib.bases.parts.baseinteractablepart import BaseInteractablePart
6
- from sm_blueprint_lib.bases.parts.baselogicpart import BaseLogicPart
7
- from sm_blueprint_lib.blueprint import Blueprint
8
- from sm_blueprint_lib.constants import TICKS_PER_SECOND
9
- from sm_blueprint_lib.parts.logicgate import LogicGate
10
- from sm_blueprint_lib.parts.timer import Timer
11
- from sm_blueprint_lib.pos import Pos
12
- from sm_blueprint_lib.prebuilds.clock40hz import clock40hz
13
- from sm_blueprint_lib.prebuilds.counter import counter
14
- from sm_blueprint_lib.prebuilds.decoder import decoder
15
- from sm_blueprint_lib.prebuilds.ram import ram
16
- from sm_blueprint_lib.prebuilds.register import register
4
+ from ..utils import get_bits_required, check_pos, connect, num_to_bit_list
5
+ from ..bases.parts.baseinteractablepart import BaseInteractablePart
6
+ from ..bases.parts.baselogicpart import BaseLogicPart
7
+ from ..blueprint import Blueprint
8
+ from ..constants import TICKS_PER_SECOND
9
+ from ..parts.logicgate import LogicGate
10
+ from ..parts.timer import Timer
11
+ from ..pos import Pos
12
+ from ..prebuilds.clock40hz import clock40hz
13
+ from ..prebuilds.counter import counter
14
+ from ..prebuilds.decoder import decoder
15
+ from ..prebuilds.ram import ram
16
+ from ..prebuilds.register import register
17
17
 
18
18
 
19
19
  def timer_ram_cached(
@@ -1,14 +1,14 @@
1
1
  from typing import Sequence
2
2
  from numpy import ndarray
3
- from sm_blueprint_lib import get_bits_required, 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.parts.timer import Timer
7
- from sm_blueprint_lib.pos import Pos
8
- from sm_blueprint_lib.prebuilds.clock40hz import clock40hz
9
- from sm_blueprint_lib.prebuilds.decoder import decoder
10
- from sm_blueprint_lib.prebuilds.ram import ram
11
- from sm_blueprint_lib.prebuilds.register import register
3
+ from ..utils import get_bits_required, check_pos, connect
4
+ from ..blueprint import Blueprint
5
+ from ..parts.logicgate import LogicGate
6
+ from ..parts.timer import Timer
7
+ from ..pos import Pos
8
+ from ..prebuilds.clock40hz import clock40hz
9
+ from ..prebuilds.decoder import decoder
10
+ from ..prebuilds.ram import ram
11
+ from ..prebuilds.register import register
12
12
 
13
13
 
14
14
  def timer_ram_multiclient(bp: Blueprint, bit_length: int, num_address: int, num_clients=1, pos: Pos | Sequence = (0, 0, 0)):
@@ -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 .bases.parts.baseinteractablepart import BaseInteractablePart
8
+ from .blueprint import Blueprint
9
+ from .parts.barrierblock import BarrierBlock
10
+ from .parts.logicgate import LogicGate
11
+ from .parts.timer import Timer
12
+ from .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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sm_blueprint_lib
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: Scrap Mechanic Library for Blueprint manipulation.
5
5
  Project-URL: Homepage, https://github.com/MauriceTZ/sm_blueprint_lib
6
6
  Project-URL: Issues, https://github.com/MauriceTZ/sm_blueprint_lib/issues
@@ -0,0 +1,38 @@
1
+ sm_blueprint_lib/__init__.py,sha256=yTdxFiDVUYCQoW_vakHBbtoQQfHhYkyGlziEO_BIRWE,215
2
+ sm_blueprint_lib/blueprint.py,sha256=fmdB4XKXF-f-iSoq5ckcLdQAX90ftkfvDuOSzHTB_44,1080
3
+ sm_blueprint_lib/body.py,sha256=sjnS0e1V0bZEhODA6XOxPH2oCt3xrFLQZ47PGhKjD_w,365
4
+ sm_blueprint_lib/bounds.py,sha256=fH-i8PdlZVg8qlEd3FmIf5mbx4RKhdBOFRAe5cSWWiI,183
5
+ sm_blueprint_lib/constants.py,sha256=mll05HrWLGCETzvQlkDPRMBe5HJLAGn5rRkxs5gJ71E,710
6
+ sm_blueprint_lib/id.py,sha256=5sBHC8qZJnCaizSBs4KGS1dnZHOSdoq5wkuIpuYNRnw,71
7
+ sm_blueprint_lib/pos.py,sha256=W0yaIg_nYgRqFwSkHlpd7Tfr69XSVwjTY1DfICHXoDo,411
8
+ sm_blueprint_lib/utils.py,sha256=g3psyklJ6D1PvyL-MQPDN1TqW7Q1JVr1eSuoIdJy0Ro,4819
9
+ sm_blueprint_lib/bases/controllers/basecontroller.py,sha256=VFS5J7kGQ8vfWBFjiTPVfXXjyMwJ1VqA3mYuh7Z_8ak,624
10
+ sm_blueprint_lib/bases/controllers/baselogiccontroller.py,sha256=lDoDt9NKFu01CqyJE09_bq9oDeYK7T-fERO54kd-sFA,290
11
+ sm_blueprint_lib/bases/controllers/logicgatecontroller.py,sha256=wzw8kdt65Zg6ucNNy47V3-okYod_ea2NH8erfjRu7uw,223
12
+ sm_blueprint_lib/bases/controllers/sensorcontroller.py,sha256=Do8TJbnm7EhAMqGPprO9lTxAuNQMOFsSo-FslZZWnqk,710
13
+ sm_blueprint_lib/bases/controllers/timercontroller.py,sha256=GcGzSicm0oGMP3gsuOA5t6PqEhRAsUVHWaCjhClbfMI,229
14
+ sm_blueprint_lib/bases/parts/baseboundablepart.py,sha256=jNx_btmkCTksT6FcpHQxEVJXzN1a3DwwHS-2GdTy2CE,744
15
+ sm_blueprint_lib/bases/parts/baseinteractablepart.py,sha256=VXINrObIr6Y2tFIOI59cB_-625rPwtKZdGDmaa3EgQ8,574
16
+ sm_blueprint_lib/bases/parts/baselogicpart.py,sha256=al9nLCg8YiLKWd_c_ZQmq9f6sazjF6RNbuGGbYROOps,498
17
+ sm_blueprint_lib/bases/parts/basepart.py,sha256=hzJBcB4uol65THzEZEGq4d2N9SbK1S4XiMghEuB6cuc,1126
18
+ sm_blueprint_lib/parts/barrierblock.py,sha256=GPI8ArKKvzO6wnHfBfgmU4Ezcrw9SmtKR0p4mWczYiw,267
19
+ sm_blueprint_lib/parts/logicgate.py,sha256=In7-d-lCx1zJIT-7S0Mh7s32lj7xZ6wxObJMnKJKQQ4,960
20
+ sm_blueprint_lib/parts/sensor.py,sha256=ZJY5xo0NB6wAwgzS7EO-xgsukXYbwV7ptbGOqLfeBKQ,1008
21
+ sm_blueprint_lib/parts/switch.py,sha256=3Yo1xMKV5kWnptgnAA1XR_VU2ft-10EuKR62nzvZ0gI,225
22
+ sm_blueprint_lib/parts/timer.py,sha256=gw9XaWvP2yW3egT72BGowMu-CZFUY-Hke-Dwg0lsxjM,806
23
+ sm_blueprint_lib/prebuilds/adder.py,sha256=Rq-006N_oD5ExIwQelgfrBKd7g8zFVxk4tj2lZEp82w,1750
24
+ sm_blueprint_lib/prebuilds/barrel_shifter.py,sha256=BOvsUg9o6YzVrclmGNzhSbt9eQL23GJHHVyNxkXbcaE,3556
25
+ sm_blueprint_lib/prebuilds/clock40hz.py,sha256=ygG8ieqpFzGMnuY5CBTs60liAYyAsBx-YIlb0gl3SXM,844
26
+ sm_blueprint_lib/prebuilds/comparator.py,sha256=qUvW1MisQMODLz0BdPp1dQbDQQjSYQ0ro6Rsy6ELDjQ,2205
27
+ sm_blueprint_lib/prebuilds/counter.py,sha256=-gQQWvFsNHrCe0SGwNuU3VWkKiXZNx0ippnaz5bfegw,2548
28
+ sm_blueprint_lib/prebuilds/decoder.py,sha256=cvs2JomBfSLc3mwkrHk3z-qRUAgVwrLjgIYV6tdmQ1g,2077
29
+ sm_blueprint_lib/prebuilds/distance_sensor.py,sha256=ooohDPSXtsPO2Hj5YL-kbeWJ_HgqGULqjR5WNOErIPo,1737
30
+ sm_blueprint_lib/prebuilds/ram.py,sha256=YVhswLxG06Ypp8M_PY9PIQdJSbM9I13ktKN6yIgdZ5U,4212
31
+ sm_blueprint_lib/prebuilds/register.py,sha256=MAiplmiA2L_VJaxCX2E8QuancnoqvyLY-vXuaP5bppw,2675
32
+ sm_blueprint_lib/prebuilds/rom.py,sha256=4dxAaNxRX60kDW17NKew-TJkPB7EMfHuK_zVIm6N-jo,3700
33
+ sm_blueprint_lib/prebuilds/timer_ram_cached.py,sha256=PG53hDKYS00fur5vazh7iOs2Gm4G_URJLX7d9xWJOe4,19057
34
+ sm_blueprint_lib/prebuilds/timer_ram_multiclient.py,sha256=NlQeJB6ilEGJDrIOfkFmPWd7afDR_0CUZH7RQpjyA4s,4200
35
+ sm_blueprint_lib-0.0.6.dist-info/METADATA,sha256=PuNO-nrz_OB8Iq0BGapU0yrouBhfC5d0bbkxfkv8yXM,611
36
+ sm_blueprint_lib-0.0.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
37
+ sm_blueprint_lib-0.0.6.dist-info/licenses/LICENSE,sha256=DWhaodryTSEv07KdWbanCMaEujOXYxnj-IEhmgTkOq0,1084
38
+ sm_blueprint_lib-0.0.6.dist-info/RECORD,,
@@ -1,28 +0,0 @@
1
- sm_blueprint_lib/__init__.py,sha256=9ktxwUvthBopqAAg6vPnLAwiOP97nnLpPcH2wY-weG0,4915
2
- sm_blueprint_lib/blueprint.py,sha256=foTUnSeMMcpfTKL-NnhkYE9vUX8QqG5qndTDVnaJ8Ps,1128
3
- sm_blueprint_lib/body.py,sha256=6Daa4mjUwrBYQjowTbDozEWsESVTtkhqW4441TwZJDM,397
4
- sm_blueprint_lib/bounds.py,sha256=fH-i8PdlZVg8qlEd3FmIf5mbx4RKhdBOFRAe5cSWWiI,183
5
- sm_blueprint_lib/constants.py,sha256=mll05HrWLGCETzvQlkDPRMBe5HJLAGn5rRkxs5gJ71E,710
6
- sm_blueprint_lib/id.py,sha256=5sBHC8qZJnCaizSBs4KGS1dnZHOSdoq5wkuIpuYNRnw,71
7
- sm_blueprint_lib/pos.py,sha256=W0yaIg_nYgRqFwSkHlpd7Tfr69XSVwjTY1DfICHXoDo,411
8
- sm_blueprint_lib/bases/controllers/basecontroller.py,sha256=wWz2dMvBzBG9Zn1l_sZf5L-L5lwNRMhNTynFSLC4KPs,652
9
- sm_blueprint_lib/bases/controllers/baselogiccontroller.py,sha256=PNQhukaP8jceHs7O3b33xbKalS1Bym8x5T4ZN7XNGyQ,324
10
- sm_blueprint_lib/bases/controllers/logicgatecontroller.py,sha256=g-Cla8kvFA77KQkAMbRxfyS_tvjFaIf6GJjOfsQ1FsY,257
11
- sm_blueprint_lib/bases/controllers/sensorcontroller.py,sha256=K_nE8RmQSCxXAEiwpWeMSpeZtxXePbn_BDbgkkgMtEM,744
12
- sm_blueprint_lib/bases/controllers/timercontroller.py,sha256=q0eusjpJ2EQ82mfxMHJP6ok5QH29eqIUpkzNfGLGsAQ,263
13
- sm_blueprint_lib/prebuilds/adder.py,sha256=eWABQFvpuB2hHosMzD2LgJkdn_rLh00Wptc75UR8eT4,1804
14
- sm_blueprint_lib/prebuilds/barrel_shifter.py,sha256=soDQWvOCfSNo3G3HXjglIfjte-QCil72SpwQDDDS7UE,3610
15
- sm_blueprint_lib/prebuilds/clock40hz.py,sha256=XOEe1pthSGnVznU64itpzUMYCNsAJS-nrNxftgY-vJ0,898
16
- sm_blueprint_lib/prebuilds/comparator.py,sha256=FuEz72Bh7G5yKHiuApnaF_mjuW-fwikxgjOUXK2H4UE,2259
17
- sm_blueprint_lib/prebuilds/counter.py,sha256=zsfC6UKDH9PPrb9dDcM8G80_-QIPk9wAFYlu4US6eao,2602
18
- sm_blueprint_lib/prebuilds/decoder.py,sha256=Ofso078Llj5Wu__9k0E_Q9-rREZu294piifdpA4mqGM,2131
19
- sm_blueprint_lib/prebuilds/distance_sensor.py,sha256=RWvaoPYJySkjcrD6gfugho26QxQGc1Ub-Wmox8gUtdI,1821
20
- sm_blueprint_lib/prebuilds/ram.py,sha256=YGzalt72H5vYKxcVqNkXLm_VbcoyebGlmRRp8oiTfZM,4281
21
- sm_blueprint_lib/prebuilds/register.py,sha256=Y66NazPvjhajMQqBA4dwtdoFAxiwpeHBu8WMqbymUAA,2744
22
- sm_blueprint_lib/prebuilds/rom.py,sha256=74rNXxVUCqvdsPhsUW7gLI-f2-jQa4-CWT04uyUAQAE,3784
23
- sm_blueprint_lib/prebuilds/timer_ram_cached.py,sha256=EFEQP-fOt9M5U_tM3WdwwPFjr70BzbD_U1Ay4jfCTzo,19246
24
- sm_blueprint_lib/prebuilds/timer_ram_multiclient.py,sha256=76domx9N9WelMIxIxTX4i-S66B6wTItOPIlXj1KIYdE,4329
25
- sm_blueprint_lib-0.0.4.dist-info/METADATA,sha256=0rNXUr3hq67wWgIsIaPu1UQgnFsvtnhboAwuUSbCpuo,611
26
- sm_blueprint_lib-0.0.4.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
27
- sm_blueprint_lib-0.0.4.dist-info/licenses/LICENSE,sha256=DWhaodryTSEv07KdWbanCMaEujOXYxnj-IEhmgTkOq0,1084
28
- sm_blueprint_lib-0.0.4.dist-info/RECORD,,