Пример #1
0
        def cb(chunk):
            for i, o in ((True, False), (False, True)):
                # Reset the hook.
                self.hook.asic = Asic()

                # The tableau.
                chunk.set_block((2, 1, 1), blocks["sand"].slot)
                chunk.set_block((3, 1, 1), blocks["redstone-torch"].slot)

                # Attach the lever to the sand block, and throw it. For sanity
                # purposes, grab the orientation metadata from the block
                # definition.
                orientation = blocks["lever"].orientation("-x")
                iblock, imetadata = truthify_block(i, blocks["lever"].slot,
                                                   orientation)
                chunk.set_block((1, 1, 1), iblock)
                chunk.set_metadata((1, 1, 1), imetadata)

                # Attach the torch to the sand block too.
                orientation = blocks["redstone-torch"].orientation("+x")
                chunk.set_metadata((3, 1, 1), orientation)

                # Run the circuit, starting at the switch.
                self.hook.feed((1, 1, 1))

                # Lever, torch, sand.
                self.hook.process()
                self.hook.process()
                self.hook.process()

                block = chunk.get_block((3, 1, 1))
                metadata = chunk.get_metadata((3, 1, 1))
                self.assertEqual((block, metadata),
                                 truthify_block(o, block, metadata))
Пример #2
0
    def __init__(self, factory):
        self.factory = factory

        self.asic = Asic()
        self.active_circuits = set()

        self.loop = LoopingCall(self.process)
Пример #3
0
        def cb(chunk):
            for i1, i2, o in (
                (False, False, False),
                (True, False, False),
                (False, True, False),
                (True, True, True),
            ):
                # Reset the hook.
                self.hook.asic = Asic()

                # The tableau.
                chunk.set_block((1, 1, 1), blocks["sand"].slot)
                chunk.set_block((1, 1, 2), blocks["sand"].slot)
                chunk.set_block((1, 1, 3), blocks["sand"].slot)

                chunk.set_block((1, 2, 1), blocks["redstone-torch"].slot)
                chunk.set_metadata((1, 2, 1),
                                   blocks["redstone-torch"].orientation("+y"))
                chunk.set_block((1, 2, 3), blocks["redstone-torch"].slot)
                chunk.set_metadata((1, 2, 3),
                                   blocks["redstone-torch"].orientation("+y"))

                chunk.set_block((1, 2, 2), blocks["redstone-wire"].slot)

                # Output torch.
                chunk.set_block((2, 1, 2), blocks["redstone-torch"].slot)
                chunk.set_metadata((2, 1, 2),
                                   blocks["redstone-torch"].orientation("+x"))

                # Attach the levers to the sand block.
                orientation = blocks["lever"].orientation("-x")
                iblock, imetadata = truthify_block(i1, blocks["lever"].slot,
                                                   orientation)
                chunk.set_block((0, 1, 1), iblock)
                chunk.set_metadata((0, 1, 1), imetadata)
                iblock, imetadata = truthify_block(i2, blocks["lever"].slot,
                                                   orientation)
                chunk.set_block((0, 1, 3), iblock)
                chunk.set_metadata((0, 1, 3), imetadata)

                # Run the circuit, starting at the switches. Six times:
                # Lever (x2), sand (x2), torch (x2), wire, block, torch.
                self.hook.feed((0, 1, 1))
                self.hook.feed((0, 1, 3))
                self.hook.process()
                self.hook.process()
                self.hook.process()
                self.hook.process()
                self.hook.process()
                self.hook.process()

                block = chunk.get_block((2, 1, 2))
                metadata = chunk.get_metadata((2, 1, 2))
                self.assertEqual((block, metadata),
                                 truthify_block(o, block, metadata))
Пример #4
0
    def test_torch_wire(self):
        """
        Wires will connect to torches.
        """

        asic = Asic()
        wire = Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0)
        torch = Torch((0, 0, 1), blocks["redstone-torch"].slot,
                      blocks["redstone-torch"].orientation("-z"))

        wire.connect(asic)
        torch.connect(asic)

        self.assertTrue(wire in torch.outputs)
        self.assertTrue(torch in wire.inputs)
Пример #5
0
    def test_wire_sand_below(self):
        """
        Wire will power the plain block beneath it.
        """

        asic = Asic()
        sand = PlainBlock((0, 0, 0), blocks["sand"].slot, 0x0)
        wire = Wire((0, 1, 0), blocks["redstone-wire"].slot, 0x0)

        sand.connect(asic)
        wire.connect(asic)

        wire.status = True
        sand.update()
        self.assertTrue(wire.status)

        wire.status = False
        sand.update()
        self.assertFalse(wire.status)
Пример #6
0
    def test_lever_sand(self):
        """
        A lever attached to a sand block will cause the sand block to have the
        same value as the lever.
        """

        asic = Asic()
        lever = Lever((0, 0, 0), blocks["lever"].slot,
                      blocks["lever"].orientation("-x"))
        sand = PlainBlock((1, 0, 0), blocks["sand"].slot, 0x0)

        lever.connect(asic)
        sand.connect(asic)

        lever.status = False
        sand.update()
        self.assertFalse(sand.status)

        lever.status = True
        sand.update()
        self.assertTrue(sand.status)
Пример #7
0
    def test_sand_torch_above(self):
        """
        A torch on top of a sand block will turn off when the sand block
        turns on, and vice versa.
        """

        asic = Asic()
        sand = PlainBlock((0, 0, 0), blocks["sand"].slot, 0x0)
        torch = Torch((0, 1, 0), blocks["redstone-torch"].slot,
                      blocks["redstone-torch"].orientation("+y"))

        sand.connect(asic)
        torch.connect(asic)

        sand.status = True
        torch.update()
        self.assertFalse(torch.status)

        sand.status = False
        torch.update()
        self.assertTrue(torch.status)
Пример #8
0
 def setUp(self):
     self.asic = Asic()
Пример #9
0
class TestAsic(TestCase):
    def setUp(self):
        self.asic = Asic()

    def test_trivial(self):
        pass

    def test_find_wires_single(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_plural(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_many(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 1), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_cross(self):
        """
        Finding wires works when the starting point is inside a cluster of
        wires.
        """

        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((-1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((0, 0, 1), blocks["redstone-wire"].slot, 0x0),
            Wire((0, 0, -1), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_inputs_many(self):
        inputs = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 1), blocks["redstone-wire"].slot, 0x0),
        ])
        wires = set([
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        wires.update(inputs)
        torches = set([
            Torch((0, 0, 1), blocks["redstone-torch"].slot,
                  blocks["redstone-torch"].orientation("-z")),
            Torch((3, 0, 1), blocks["redstone-torch"].slot,
                  blocks["redstone-torch"].orientation("-x")),
        ])
        for wire in wires:
            wire.connect(self.asic)
        for torch in torches:
            torch.connect(self.asic)

        self.assertEqual(inputs, set(self.asic.find_wires(0, 0, 0)[0]))

    def test_find_wires_outputs_many(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        outputs = set([
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((3, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        wires.update(outputs)
        plains = set([
            PlainBlock((1, 0, 1), blocks["sand"].slot, 0x0),
            PlainBlock((4, 0, 0), blocks["sand"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)
        for plain in plains:
            plain.connect(self.asic)

        self.assertEqual(outputs, set(self.asic.find_wires(0, 0, 0)[2]))

    def test_update_wires_single(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch-off"].slot,
                      blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        plain.connect(self.asic)

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(plain in outputs)
        self.assertFalse(wire.status)
        self.assertEqual(wire.metadata, 0)

    def test_update_wires_single_powered(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch"].slot,
                      blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        plain.connect(self.asic)

        torch.status = True

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(plain in outputs)
        self.assertTrue(wire.status)
        self.assertEqual(wire.metadata, 15)

    def test_update_wires_multiple(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch-off"].slot,
                      blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        wire2 = Wire((1, 0, 1), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        wire2.connect(self.asic)
        plain.connect(self.asic)

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(wire2 in wires)
        self.assertTrue(plain in outputs)
        self.assertFalse(wire.status)
        self.assertEqual(wire.metadata, 0)
        self.assertFalse(wire2.status)
        self.assertEqual(wire2.metadata, 0)

    def test_update_wires_multiple_powered(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch"].slot,
                      blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        wire2 = Wire((1, 0, 1), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        wire2.connect(self.asic)
        plain.connect(self.asic)

        torch.status = True

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(wire2 in wires)
        self.assertTrue(plain in outputs)
        self.assertTrue(wire.status)
        self.assertEqual(wire.metadata, 15)
        self.assertTrue(wire2.status)
        self.assertEqual(wire2.metadata, 14)
Пример #10
0
 def setUp(self):
     self.asic = Asic()
Пример #11
0
class TestAsic(TestCase):

    def setUp(self):
        self.asic = Asic()

    def test_trivial(self):
        pass

    def test_find_wires_single(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_plural(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_many(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 1), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_cross(self):
        """
        Finding wires works when the starting point is inside a cluster of
        wires.
        """

        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((-1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((0, 0, 1), blocks["redstone-wire"].slot, 0x0),
            Wire((0, 0, -1), blocks["redstone-wire"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)

        self.assertEqual(wires, self.asic.find_wires(0, 0, 0)[1])

    def test_find_wires_inputs_many(self):
        inputs = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 1), blocks["redstone-wire"].slot, 0x0),
        ])
        wires = set([
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        wires.update(inputs)
        torches = set([
            Torch((0, 0, 1), blocks["redstone-torch"].slot,
                blocks["redstone-torch"].orientation("-z")),
            Torch((3, 0, 1), blocks["redstone-torch"].slot,
                blocks["redstone-torch"].orientation("-x")),
        ])
        for wire in wires:
            wire.connect(self.asic)
        for torch in torches:
            torch.connect(self.asic)

        self.assertEqual(inputs, set(self.asic.find_wires(0, 0, 0)[0]))

    def test_find_wires_outputs_many(self):
        wires = set([
            Wire((0, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((2, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        outputs = set([
            Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0),
            Wire((3, 0, 0), blocks["redstone-wire"].slot, 0x0),
        ])
        wires.update(outputs)
        plains = set([
            PlainBlock((1, 0, 1), blocks["sand"].slot, 0x0),
            PlainBlock((4, 0, 0), blocks["sand"].slot, 0x0),
        ])
        for wire in wires:
            wire.connect(self.asic)
        for plain in plains:
            plain.connect(self.asic)

        self.assertEqual(outputs, set(self.asic.find_wires(0, 0, 0)[2]))

    def test_update_wires_single(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch-off"].slot,
            blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        plain.connect(self.asic)

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(plain in outputs)
        self.assertFalse(wire.status)
        self.assertEqual(wire.metadata, 0)

    def test_update_wires_single_powered(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch"].slot,
            blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        plain.connect(self.asic)

        torch.status = True

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(plain in outputs)
        self.assertTrue(wire.status)
        self.assertEqual(wire.metadata, 15)

    def test_update_wires_multiple(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch-off"].slot,
            blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        wire2 = Wire((1, 0, 1), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        wire2.connect(self.asic)
        plain.connect(self.asic)

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(wire2 in wires)
        self.assertTrue(plain in outputs)
        self.assertFalse(wire.status)
        self.assertEqual(wire.metadata, 0)
        self.assertFalse(wire2.status)
        self.assertEqual(wire2.metadata, 0)

    def test_update_wires_multiple_powered(self):
        torch = Torch((0, 0, 0), blocks["redstone-torch"].slot,
            blocks["redstone-torch"].orientation("-x"))
        wire = Wire((1, 0, 0), blocks["redstone-wire"].slot, 0x0)
        wire2 = Wire((1, 0, 1), blocks["redstone-wire"].slot, 0x0)
        plain = PlainBlock((2, 0, 0), blocks["sand"].slot, 0x0)

        torch.connect(self.asic)
        wire.connect(self.asic)
        wire2.connect(self.asic)
        plain.connect(self.asic)

        torch.status = True

        wires, outputs = self.asic.update_wires(1, 0, 0)

        self.assertTrue(wire in wires)
        self.assertTrue(wire2 in wires)
        self.assertTrue(plain in outputs)
        self.assertTrue(wire.status)
        self.assertEqual(wire.metadata, 15)
        self.assertTrue(wire2.status)
        self.assertEqual(wire2.metadata, 14)