def stack(self, z):
        dirIn = rotd('r', self.rotate)

        rt = ("remove", z)
        if self.state["state"] == "keep":
            self.state["state"] = "main"
            cfrom = self.getOption("from")
            cto = self.getOption("to")
            h = dirDef[dirIn]
            cval = self.state["colorCaptive"][cfrom]
            newcol = photons.emptyColor()
            newcol[cto] = cval

            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
            rt = [("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirIn,
                             color=newcol,
                             pos=newPos,
                             active=1),
            ]), ("remove", z)]

        if z != []:
            beam = z[0]
            if beam.direction == dirIn:
                self.state["colorCaptive"] = beam.color
                self.state["state"] = "keep"

        return rt
Пример #2
0
class main(photons.Block):
    defaultState = {
        "active": True,
        "state": "main",
        "colorCaptive": photons.emptyColor()
    }
    """
    states:
        main    -- empty cell, nothing inside
        keep   -- a beam had entered the cell

    """

    blockName = "Negator"
    opts_polygon = {'width': 2, 'outline': 'black', 'fill': ''}
    opts_line = {'width': 2}

    boxGraphics = [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                   ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                   ("line", (0, 0.5, 0.2, 0.5), opts_line),
                   ("polygon", (0.2, 0.2, 0.2, 0.8, 0.5, 0.5), opts_polygon),
                   ("oval", (0.5, 0.35, 0.8, 0.65)),
                   ("line", (0.8, 0.5, 1, 0.5), opts_line)]

    def getGraphicsList(self):
        return main.boxGraphics

    def stack(self, z):
        dirIn = rotd('r', self.rotate)

        rt = ("remove", z)
        if self.state["state"] == "keep":
            self.state["state"] = "main"
            h = dirDef[dirIn]
            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
            rt = [("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirIn,
                             color=ltod([
                                 15 - self.state["colorCaptive"]['r'],
                                 15 - self.state["colorCaptive"]['g'],
                                 15 - self.state["colorCaptive"]['b']
                             ]),
                             pos=newPos,
                             active=2),
            ]), ("remove", z)]

        if z != []:
            beam = z[0]
            if beam.direction == dirIn:
                self.state["colorCaptive"] = beam.color
                self.state["state"] = "keep"

        return rt
Пример #3
0
    def stack(self, z):
        dirIn = rotd('r', self.rotate)

        dirA = rotd('d', self.rotate)
        dirB = rotd('u', self.rotate)

        rt = [("remove", z)]

        if self.state["state"] == "keep":
            self.state["state"] = "main"
            h = dirDef[dirIn]
            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])

            c1 = self.state["colorA"]
            c2 = self.state["colorB"]
            if self.getOption("absdif") in ("1", "true", "True", "aye"):
                d = [abs(c1[x] - c2[x]) for x in "rgb"]
            else:
                d = [max(0, c1[x] - c2[x]) for x in "rgb"]
            clr = ltod(d)

            rt.append(("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirIn,
                             color=clr,
                             pos=newPos,
                             active=2)
            ]))
            self.state["colorA"] = photons.emptyColor()
            self.state["colorB"] = photons.emptyColor()

        if z != []:
            for beam in z:
                if beam.direction in (dirA, dirIn):
                    self.state["colorA"] = beam.color
                    self.state["state"] = "keep"
                elif beam.direction == dirB:
                    self.state["colorB"] = beam.color
                    self.state["state"] = "keep"

        return rt
Пример #4
0
class main(photons.Block):
    defaultOptions = {"color":{"name":"Color","type":"color","value":photons.emptyColor()}}
    """
    states:
        main
        on
        
    ╔═══╗
    ║ L═╢ 1
    ╚═══╝
      
    """
    blockName = "Button"
    boxGraphics = [
            ("line", (0,0,1,0)),
            ("line", (1,0,1,1)),
            ("line", (1,1,0,1)),
            ("line", (0,1,0,0)),
            ]
    
    def getGraphicsList(self):
        a = main.boxGraphics.copy()
        z = self.getOption("color")
        cl3 = [(z['r']+1)*16-1,(z['g']+1)*16-1,(z['b']+1)*16-1]
        clr = photons.listToTkColor(cl3)
        opt_oval = {'fill': clr}
        opt_line = {'fill': clr, 'width': 2}
        a += [
                ("oval", (0.2,0.2,0.8,0.8)),
                ("oval", (0,0.4,0.4,0.6), opt_oval),
                ("line", (0.1,0.5,1,0.5), opt_line)
                
            ]
        return a
    
    def clickMouse(self):
        if self.state["state"] == "on":
            self.state["state"] = "main"
        else:
            self.state["state"] = "on"
    
    def stack(self, z):
        if self.state["state"] == "on":
            dirOut = rotd('r', self.rotate)
            h = dirDef[dirOut]
            newPos = (self.pos[0]+h[0], self.pos[1]+h[1])
            #self.state["state"] = "main"
            return [
                    
                    (
                     "add",
                     [photons.Beam(   
                        cell = self.cell.field[newPos],
                        field = self.cell.field,
                        direction = dirOut,
                        color = photons.fixColor(self.getOption("color")),
                        pos = newPos,
                        active = 1
                      )
                    ]
                   ),
                     
                   (
                    "remove",
                    z
                   )
                    ]
        else:
            return ("remove",z)
                 
    pictures = {
            0: ("╔═══╗","║ L─╢","╚═══╝"),
            1: ("╔═══╗","║ L ║","╚═╧═╝"),
            2: ("╔═══╗","╟─L ║","╚═══╝"),
            3: ("╔═╤═╗","║ L ║","╚═══╝")
            }
    
    def pretty(self):
        x = main.pictures[self.rotate]
        return x
class main(photons.Block):
    """
    states:
        off
        offKeep -- off, has a beam inside
        on
        onKeep  -- on,  has a beam inside
     
       B
     ╔═*═╗
   2 ║ ? → A
     ╚═══╝
       1
    """
    blockName = "Lens"
    defaultState = {
        "active": True,
        "state": "off",
        "ticks": 1,
        "colorCaptive": photons.emptyColor()
    }

    defaultOptions = {
        "cap": {
            "name": "Gate capacitance",
            "type": "integer",
            "value": 0
        },
        "invert": {
            "name": "Invert",
            "type": "bool",
            "value": False
        }
    }

    boxGraphics = {
        ("off", False): [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                         ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                         ("line", (0.5, 0, 0.5, 0.5), {
                             'arrow': 'last',
                             'width': 2
                         }), ("line", (0.5, 0.5, 1, 0.5), {
                             'width': 3
                         }), ("line", (0.5, 0.5, 0.5, 1), {
                             'width': 3
                         })],
        ("on", False): [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                        ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                        ("line", (0.5, 0, 0.5, 0.5), {
                            'arrow': 'last',
                            'width': 2
                        }), ("line", (0.5, 0.5, 0, 0.5), {
                            'width': 3
                        }), ("line", (0.5, 0.5, 0.5, 1), {
                            'width': 3
                        })],
        ("on", True): [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                       ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                       ("oval", (0.4, 0.25, 0.6, 0.5)),
                       ("line", (0.5, 0, 0.5, 0.25), {
                           'width': 2
                       }), ("line", (0.5, 0.5, 1, 0.5), {
                           'width': 3
                       }), ("line", (0.5, 0.5, 0.5, 1), {
                           'width': 3
                       })],
        ("off", True): [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                        ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                        ("oval", (0.4, 0.25, 0.6, 0.5)),
                        ("line", (0.5, 0, 0.5, 0.25), {
                            'width': 2
                        }), ("line", (0.5, 0.5, 0, 0.5), {
                            'width': 3
                        }), ("line", (0.5, 0.5, 0.5, 1), {
                            'width': 3
                        })],
    }

    def getGraphicsList(self):
        return main.boxGraphics[(self.state["state"].replace("Keep", ""),
                                 self.getOption("invert"))]

    def __repr__(self):
        if "off" in self.state["state"]:
            return "[  LS off]"
        else:
            return "[  LS on ]"

    pictures = {
        0: ("╔═*═╗", "← ? →", "╚═══╝"),
        1: ("╔═↑═╗", "║ ? *", "╚═↓═╝"),
        2: ("╔═══╗", "← ? →", "╚═*═╝"),
        3: ("╔═↑═╗", "* ? ║", "╚═↓═╝")
    }

    def stack(self, z):
        dirIn1 = rotd('u', self.rotate)
        if ("off" in self.state["state"]) != self.getOption("invert"):
            dirIn2 = rotd('r', self.rotate)
        else:
            dirIn2 = rotd('l', self.rotate)
        dirInB = rotd('d', self.rotate)

        on = False
        keep = None

        for i in z:
            if i.direction == dirInB:
                on = True
            if i.direction in (dirIn1, dirIn2):
                keep = i.color

        if not on:
            if ("on" in self.state["state"]):
                self.state["ticks"] -= 1
                if self.state["ticks"] <= 0:
                    self.state["state"] = self.state["state"].replace(
                        "on", "off")
        else:
            self.state["ticks"] = self.getOption("cap")
            self.state["state"] = self.state["state"].replace("off", "on")

        if "Keep" in self.state["state"]:
            self.state["state"] = self.state["state"].replace("Keep", "")
            h = dirDef[dirIn2]
            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
            u = [("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirIn2,
                             color=self.state["colorCaptive"],
                             pos=newPos,
                             active=1)
            ])]
        else:
            u = []

        if not (keep is None):
            self.state["colorCaptive"] = keep
            self.state["state"] = self.state["state"] + "Keep"

        u.append(("remove", z))

        return u

    def pretty(self):
        r = list(main.pictures[self.rotate])
        right = (("on" in self.state["state"]) == self.getOption("invert"))
        left = not right
        if (right and self.rotate == 0) or (left and self.rotate == 2):
            r[1] = r[1].replace("←", "║")
        if (left and self.rotate == 0) or (right and self.rotate == 2):
            r[1] = r[1].replace("→", "║")
        if (right and self.rotate == 1) or (left and self.rotate == 3):
            r[0] = r[0].replace("↑", "═")
        if (left and self.rotate == 1) or (right and self.rotate == 3):
            r[2] = r[2].replace("↓", "═")

        a = self.getOption("cap")
        if a > 9:
            a = "C"
        else:
            a = str(a)

        if self.getOption("invert"):
            r = [i.replace("*", "o") for i in r]

        r[1] = r[1].replace("?", a)
        return r
class main(photons.Block):
    defaultState = {
        "active": True,
        "state": "main",
        "colorCaptive": photons.emptyColor()
    }
    """
    states:
        main    -- empty cell, nothing inside
        keep   -- a beam had entered the cell

    """
    blockName = "Remapper"
    defaultOptions = {
        "from": {
            "name": "Map from",
            "type": "colorLetter",
            "value": "r"
        },
        "to": {
            "name": "Map onto",
            "type": "colorLetter",
            "value": "g"
        }
    }

    boxGraphics = [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                   ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                   ("line", (0, 0.5, 0.2, 0.5)),
                   ("line", (0.5, 0.3, 0.5, 0.7)),
                   ("line", (0.4, 0.5, 0.5, 0.5)),
                   ("line", (0.8, 0.5, 1, 0.5))]

    def getGraphicsList(self):
        a = main.boxGraphics.copy()

        opts_polygon1 = {
            'fill': cofl(self.getOption("from")),
            'outline': 'black'
        }
        opts_polygon2 = {
            'fill': cofl(self.getOption("to")),
            'outline': 'black'
        }

        a.append(("polygon", (0.2, 0.3, 0.2, 0.7, 0.4, 0.5), opts_polygon1))
        a.append(("polygon", (0.6, 0.3, 0.6, 0.7, 0.8, 0.5), opts_polygon2))

        return a

    def stack(self, z):
        dirIn = rotd('r', self.rotate)

        rt = ("remove", z)
        if self.state["state"] == "keep":
            self.state["state"] = "main"
            cfrom = self.getOption("from")
            cto = self.getOption("to")
            h = dirDef[dirIn]
            cval = self.state["colorCaptive"][cfrom]
            newcol = photons.emptyColor()
            newcol[cto] = cval

            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
            rt = [("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirIn,
                             color=newcol,
                             pos=newPos,
                             active=1),
            ]), ("remove", z)]

        if z != []:
            beam = z[0]
            if beam.direction == dirIn:
                self.state["colorCaptive"] = beam.color
                self.state["state"] = "keep"

        return rt
Пример #7
0
class main(photons.Block):
    defaultState = {
        "active": True,
        "state": "main",
        "colorCaptive": photons.emptyColor()
    }
    """
    states:
        main    -- empty cell, nothing inside
        keep   -- a beam had entered the cell

    """

    boxGraphics = [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                   ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                   ("line", (0.5, 0.5, 0.5, 0), {
                       'fill': '#FF0000',
                       'width': 2
                   }),
                   ("line", (0.5, 0.5, 1, 0.5), {
                       'fill': '#00FF00',
                       'width': 2
                   }),
                   ("line", (0.5, 0.5, 0.5, 1), {
                       'fill': '#0000FF',
                       'width': 2
                   }), ("line", (0, 0.5, 0.4, 0.5)),
                   ("line", (0.4, 0.5, 0.2, 0.3)),
                   ("line", (0.4, 0.5, 0.2, 0.7))]

    blockName = "Decoupler"

    def getGraphicsList(self):
        return main.boxGraphics

    def stack(self, z):
        dirIn = rotd('r', self.rotate)

        dirR = rotd('u', self.rotate)
        dirG = rotd('r', self.rotate)
        dirB = rotd('d', self.rotate)

        rt = ("remove", z)
        if self.state["state"] == "keep":
            self.state["state"] = "main"
            hr = dirDef[dirR]
            hg = dirDef[dirG]
            hb = dirDef[dirB]
            newPosR = (self.pos[0] + hr[0], self.pos[1] + hr[1])
            newPosG = (self.pos[0] + hg[0], self.pos[1] + hg[1])
            newPosB = (self.pos[0] + hb[0], self.pos[1] + hb[1])
            rt = [("add", [
                photons.Beam(cell=self.cell.field[newPosR],
                             field=self.cell.field,
                             direction=dirR,
                             color=ltod(
                                 [self.state["colorCaptive"]['r'], 0, 0]),
                             pos=newPosR,
                             active=2),
                photons.Beam(cell=self.cell.field[newPosG],
                             field=self.cell.field,
                             direction=dirG,
                             color=ltod(
                                 [0, self.state["colorCaptive"]['g'], 0]),
                             pos=newPosR,
                             active=2),
                photons.Beam(cell=self.cell.field[newPosB],
                             field=self.cell.field,
                             direction=dirB,
                             color=ltod(
                                 [0, 0, self.state["colorCaptive"]['b']]),
                             pos=newPosB,
                             active=2),
            ]), ("remove", z)]

        if z != []:
            beam = z[0]
            if beam.direction == dirIn:
                self.state["colorCaptive"] = beam.color
                self.state["state"] = "keep"

        return rt
class main(photons.Block):
    defaultState = {"active":True, "state":"main", "ticks":0}
    defaultOptions = {
            "color":{
                    "name":"Color",
                    "type":"color",
                    "value":photons.emptyColor()
                    },
            "lowtime":{
                    "name":"Low time",
                    "type":"integer",
                    "value": 7
                },
            "hightime":{
                    "name":"High time",
                    "type":"integer",
                    "value": 3
                }
            }
    """
    states:
        main
        on
        
    ╔═══╗
    ║ G═╢ 1
    ╚═══╝
      
    """
    
    blockName = "Generator"
    boxGraphics = [
        ("line", (0,0,1,0)),
        ("line", (1,0,1,1)),
        ("line", (1,1,0,1)),
        ("line", (0,1,0,0)),
        ]
    
    def getGraphicsList(self):
        a = main.boxGraphics.copy()
        z = self.getOption("color")
        cl3 = [(z['r']+1)*16-1,(z['g']+1)*16-1,(z['b']+1)*16-1]
        if self.state["state"] == "main":
            cl3 = [x//2 for x in cl3]
        clr = photons.listToTkColor(cl3)
        opt_line = {'fill': clr, 'width': 2}
        a += [
                ("line", (0,0.5,0.2,0.5), opt_line),
                ("line", (0.2,0.5,0.2,0.3), opt_line),
                ("line", (0.2,0.3,0.4,0.3), opt_line),
                ("line", (0.4,0.3,0.4,0.7), opt_line),
                ("line", (0.4,0.7,0.6,0.7), opt_line),
                ("line", (0.6,0.7,0.6,0.5), opt_line),
                ("line", (0.6,0.5,1,0.5), opt_line),
                
                ("line", (1,0.5,0.8,0.3), opt_line),
                ("line", (1,0.5,0.8,0.7), opt_line)
                
            ]
        return a
    
    def stack(self, z):
        self.state["ticks"] += 1
        if self.state["state"] == "on":
            if self.state["ticks"] >= self.getOption("hightime"):
                self.state["state"] = "main"
                self.state["ticks"] = 0
            dirOut = rotd('r', self.rotate)
            h = dirDef[dirOut]
            newPos = (self.pos[0]+h[0], self.pos[1]+h[1])
            #self.state["state"] = "main"
            return [
                    
                    (
                     "add",
                     [photons.Beam(   
                        cell = self.cell.field[newPos],
                        field = self.cell.field,
                        direction = dirOut,
                        color = photons.fixColor(self.options["color"]["value"]),
                        pos = newPos,
                        active = 2
                      )
                    ]
                   ),
                     
                   (
                    "remove",
                    z
                   )
                    ]
        else:
            if self.state["ticks"] >= self.getOption("lowtime"):
                self.state["state"] = "on"
                self.state["ticks"] = 0
            return [("remove", z)]
                 
    pictures = {
            0: ("╔═══╗","║ G─╢","╚═══╝"),
            1: ("╔═══╗","║ G ║","╚═╧═╝"),
            2: ("╔═══╗","╟─G ║","╚═══╝"),
            3: ("╔═╤═╗","║ G ║","╚═══╝")
            }
    
    def pretty(self):
        x = main.pictures[self.rotate]
        return x
Пример #9
0
class main(photons.Block):
    defaultState = {
        "active": True,
        "state": "main",
        "colorA": photons.emptyColor(),
        "colorB": photons.emptyColor()
    }
    """
    states:
        main    -- empty cell, nothing inside
        keep   -- a beam had entered the cell

    """
    defaultOptions = {
        "absdif": {
            "name": "Absolute difference",
            "type": "bool",
            "value": True
        }
    }

    blockName = "Comparator"
    opts_r = {'fill': '#FF0000', 'width': 2}
    opts_b = {'fill': '#0000FF', 'width': 2}

    boxGraphics = [("line", (0, 0, 1, 0)), ("line", (1, 0, 1, 1)),
                   ("line", (1, 1, 0, 1)), ("line", (0, 1, 0, 0)),
                   ("line", (0.5, 0.5, 0.5, 0), opts_r),
                   ("line", (0.5, 0.5, 0, 0.5), opts_r),
                   ("line", (0.5, 0.5, 0.5, 1), opts_b),
                   ("line", (0.7, 0.5, 0.9, 0.5)),
                   ("line", (0.9, 0.5, 0.7, 0.3)),
                   ("line", (0.9, 0.5, 0.7, 0.7))]

    def getGraphicsList(self):
        a = main.boxGraphics.copy()
        if self.getOption("absdif") in ("1", True, 1, "yes", "Ja-ja"):
            a.append(("oval", (0.3, 0.3, 0.7, 0.7)))
        else:
            a.append(("rectangle", (0.3, 0.3, 0.7, 0.7)))
        return a

    def stack(self, z):
        dirIn = rotd('r', self.rotate)

        dirA = rotd('d', self.rotate)
        dirB = rotd('u', self.rotate)

        rt = [("remove", z)]

        if self.state["state"] == "keep":
            self.state["state"] = "main"
            h = dirDef[dirIn]
            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])

            c1 = self.state["colorA"]
            c2 = self.state["colorB"]
            if self.getOption("absdif") in ("1", "true", "True", "aye"):
                d = [abs(c1[x] - c2[x]) for x in "rgb"]
            else:
                d = [max(0, c1[x] - c2[x]) for x in "rgb"]
            clr = ltod(d)

            rt.append(("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirIn,
                             color=clr,
                             pos=newPos,
                             active=2)
            ]))
            self.state["colorA"] = photons.emptyColor()
            self.state["colorB"] = photons.emptyColor()

        if z != []:
            for beam in z:
                if beam.direction in (dirA, dirIn):
                    self.state["colorA"] = beam.color
                    self.state["state"] = "keep"
                elif beam.direction == dirB:
                    self.state["colorB"] = beam.color
                    self.state["state"] = "keep"

        return rt
Пример #10
0
class main(photons.Block):
    defaultState = {
        "active": True,
        "state": "main",
        "colorCaptive": photons.emptyColor()
    }
    """
    states:
        main    -- empty cell, nothing inside
        from1   -- a beam had entered the cell in port 1
        from2   -- a beam had entered the cell in port 1
        
    ╔═══╗
    ║ ┌─╢ 1
    ╚═╧═╝
      2
    """
    blockName = "Mirror"
    boxGraphics = [("line", (0, 0, 1, 0)), ("line", (1, 0, 0, 1)),
                   ("line", (0.9, 0, 0, 0.9), {
                       'fill': "#bfbfbf"
                   }), ("line", (0, 1, 0, 0))]

    def getGraphicsList(self):
        a = main.boxGraphics.copy()
        z = self.state["colorCaptive"]
        cl3 = [(z['r'] + 1) * 16 - 1, (z['g'] + 1) * 16 - 1,
               (z['b'] + 1) * 16 - 1]
        clr = photons.listToTkColor(cl3)
        opt = {'fill': clr, 'width': 4}
        if "from" in self.state["state"]:
            a += [
                ("line", (0.5, 0.5, 1, 0.5), opt),
                ("line", (0.5, 0.5, 0.5, 1), opt),
            ]
        return a

    def stack(self, z):
        dirIn1 = rotd('l', self.rotate)
        dirIn2 = rotd('u', self.rotate)
        dirOut1 = rotd('r', self.rotate)
        dirOut2 = rotd('d', self.rotate)
        rt = ("remove", z)
        if self.state["state"] != "main":
            if self.state["state"] == "from2":
                self.state["state"] = "main"
                h = dirDef[dirOut1]
                newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
                rt = [("add", [
                    photons.Beam(cell=self.cell.field[newPos],
                                 field=self.cell.field,
                                 direction=dirOut1,
                                 color=self.state["colorCaptive"],
                                 pos=newPos,
                                 active=2)
                ]), ("remove", z)]
            elif self.state["state"] == "from1":
                self.state["state"] = "main"
                h = dirDef[dirOut2]
                newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
                rt = [("add", [
                    photons.Beam(cell=self.cell.field[newPos],
                                 field=self.cell.field,
                                 direction=dirOut2,
                                 color=self.state["colorCaptive"],
                                 pos=newPos,
                                 active=2)
                ]), ("remove", z)]

        if z != []:
            for beam in z:
                if (beam.color != {'r': 0, 'g': 0, 'b': 0}):
                    if beam.direction == dirIn1:
                        self.state["colorCaptive"] = beam.color
                        self.state["state"] = "from1"
                    elif beam.direction == dirIn2:
                        self.state["colorCaptive"] = beam.color
                        self.state["state"] = "from2"

        return rt

    def __repr__(self):
        return "[  SD  ]"

    pictures = {
        0: ("╔═══╗", "║ ┌─╢", "╚═╧═╝"),
        1: ("╔═══╗", "╟─┐ ║", "╚═╧═╝"),
        2: ("╔═╤═╗", "╟─┘ ║", "╚═══╝"),
        3: ("╔═╤═╗", "║ └─╢", "╚═══╝")
    }

    def pretty(self):
        x = main.pictures[self.rotate]
        if self.state["state"] != "main":
            x = [i.replace(" ", "X") for i in x]
        #x = [i.replace(" ",str(len(self.cell.stack))) for i in x]
        return x
Пример #11
0
class main(photons.Block):
    defaultState = {
        "active": True,
        "state": "main",
        "colorCaptive": photons.emptyColor()
    }
    """
    states:
        main    -- empty cell, nothing inside
        from1   -- a beam had entered the cell in port 1 or 3
        from2   -- a beam had entered the cell in port 1
        
    ╔═══╗
  3 ╟─┬─╢ 1
    ╚═╧═╝
      2
    """
    blockName = "Scatterer"
    boxGraphics = [("line", (0, 0, 1, 0)), ("line", (1, 0, 0.5, 1)),
                   ("line", (0.5, 1, 0, 0))]

    def getGraphicsList(self):
        a = main.boxGraphics.copy()
        z = self.state["colorCaptive"]
        cl3 = [(z['r'] + 1) * 16 - 1, (z['g'] + 1) * 16 - 1,
               (z['b'] + 1) * 16 - 1]
        clr = photons.listToTkColor(cl3)
        opt_oval = {'fill': clr, 'outline': clr}
        opt_line = {'fill': clr, 'width': 4}
        if photons.colorIsNull(self.state["colorCaptive"]) is False:
            if self.state["state"] == "from1":
                a += [
                    ("line", (0.5, 0.5, 0.5, 1), opt_line),
                    ("oval", (0.35, 0.35, 0.65, 0.65), opt_oval),
                ]
            elif self.state["state"] == "from2":
                a += [
                    ("line", (0, 0.5, 0.25, 0.5), opt_line),
                    ("line", (1, 0.5, 0.75, 0.5), opt_line),
                ]

        return a

    def stack(self, z):
        dirIn1 = rotd('l', self.rotate)
        dirIn2 = rotd('u', self.rotate)
        dirIn3 = rotd('r', self.rotate)

        dirOut1 = rotd('r', self.rotate)
        dirOut2 = rotd('d', self.rotate)
        dirOut3 = rotd('l', self.rotate)

        rt = ("remove", z)
        if self.state["state"] == "from2":
            self.state["state"] = "main"
            h1 = dirDef[dirOut1]
            h2 = dirDef[dirOut3]
            newPos1 = (self.pos[0] + h1[0], self.pos[1] + h1[1])
            newPos2 = (self.pos[0] + h2[0], self.pos[1] + h2[1])
            rt = [("add", [
                photons.Beam(cell=self.cell.field[newPos2],
                             field=self.cell.field,
                             direction=dirOut3,
                             color=self.state["colorCaptive"],
                             pos=newPos2,
                             active=2),
                photons.Beam(cell=self.cell.field[newPos1],
                             field=self.cell.field,
                             direction=dirOut1,
                             color=self.state["colorCaptive"],
                             pos=newPos1,
                             active=2)
            ]), ("remove", z)]
        elif self.state["state"] == "from1":
            self.state["state"] = "main"
            h = dirDef[dirOut2]
            newPos = (self.pos[0] + h[0], self.pos[1] + h[1])
            rt = [("add", [
                photons.Beam(cell=self.cell.field[newPos],
                             field=self.cell.field,
                             direction=dirOut2,
                             color=self.state["colorCaptive"],
                             pos=newPos,
                             active=2)
            ]), ("remove", z)]

        if z != []:
            for beam in z:
                if beam.direction in (dirIn1, dirIn3):
                    self.state["colorCaptive"] = beam.color
                    self.state["state"] = "from1"
                elif beam.direction == dirIn2:
                    self.state["colorCaptive"] = beam.color
                    self.state["state"] = "from2"

        return rt

    def __repr__(self):
        return "[  SD  ]"

    pictures = {
        0: ("╔═══╗", "╟─┬─╢", "╚═╧═╝"),
        1: ("╔═╤═╗", "╟─┤ ║", "╚═╧═╝"),
        2: ("╔═╤═╗", "╟─┴─╢", "╚═══╝"),
        3: ("╔═╤═╗", "║ ├─╢", "╚═╧═╝")
    }

    def pretty(self):
        x = main.pictures[self.rotate]
        if self.state["state"] != "main":
            x = [i.replace(" ", "X") for i in x]
        #x = [i.replace(" ",str(len(self.cell.stack))) for i in x]
        return x