예제 #1
0
def initialize_graphics(instance):
    from PySide import QtGui, QtCore

    #Calculates points to be visualized, stores as "ambushes" which are visualized.
    def calculatePOIS(points=[]):
        results = points
        if points == None:
            for x in range(2):
                p = instance.level.findRandomFreePositionInBox(
                    instance.level.area)
                o = Vector2(random.uniform(-0.5, 0.5),
                            random.uniform(-0.5, 0.5)).normalized()
                results.append(p)
        instance.ambushes = []
        instance.ambushes = results

    calculatePOIS()

    def drawPreWorld(visualizer):
        brightest = max([
            instance.visibilities.pixel(i, j)
            for i, j in itertools.product(range(88), range(50))
        ])

        for i, j in itertools.product(range(88), range(50)):
            n = instance.terrain[j][i]
            if n:
                if instance.mode == instance.MODE_VISIBILITY:
                    d = instance.visibilities.pixel(i,
                                                    j) * 255 / (brightest + 1)
            else:
                d = 32
            visualizer.drawPixel((i, j), QtGui.qRgb(d, d, d))

    def drawPreBots(visualizer):
        for p in instance.ambushes:
            visualizer.drawCircle(p, QtGui.qRgb(255, 255, 0), 5)

    def keyPressed(e):
        if e.key() == QtCore.Qt.Key_Space:
            instance.mode = 1 - instance.mode

    #Main logic #!!!! REQUIRES INSTANCE TO HAVE GRAPH
    instance.mode = instance.MODE_VISIBILITY
    instance.visualizer = VisualizerApplication(instance)

    instance.visualizer.setDrawHookPreWorld(drawPreWorld)
    instance.visualizer.setDrawHookPreBots(drawPreBots)
    instance.visualizer.setKeyboardHook(keyPressed)

    #Coloration based on score in an 88 by 50 pixel grid?
    #Grid squares are set with setPixel(xcoord, ycoord, intRepresentingDarkness)
    instance.visibilities = QtGui.QImage(88, 50, QtGui.QImage.Format_ARGB32)
    instance.visibilities.fill(0)

    def evaluate(position, orientation, callback):
        return 1
예제 #2
0
파일: sneak.py 프로젝트: wildertm/jytwai
    def initialize(self):
        self.makeGraph()

        self.graph.add_node("enemy_base")
        self.positions["enemy_base"] = None
        start, finish = self.level.botSpawnAreas[self.game.enemyTeam.name]
        for i, j in itertools.product(range(int(start.x), int(finish.x)),
                                      range(int(start.y), int(finish.y))):
            self.graph.add_edge("enemy_base", self.terrain[j][i], weight=1.0)

        self.graph.add_node("base")
        self.positions["base"] = None
        start, finish = self.level.botSpawnAreas[self.game.team.name]
        for i, j in itertools.product(range(int(start.x), int(finish.x)),
                                      range(int(start.y), int(finish.y))):
            self.graph.add_edge("base", self.terrain[j][i], weight=1.0)

        self.node_EnemyFlagIndex = self.getNodeIndex(
            self.game.team.flag.position)
        self.node_EnemyScoreIndex = self.getNodeIndex(
            self.game.enemyTeam.flagScoreLocation)

        # self.node_Bases = self.graph.add_vertex()
        # e = self.graph.add_edge(self.node_Bases, self.node_MyBase)
        # e = self.graph.add_edge(self.node_Bases, self.node_EnemyBase)

        vb2f = nx.shortest_path(self.graph,
                                source="enemy_base",
                                target=self.node_EnemyFlagIndex)
        vf2s = nx.shortest_path(self.graph,
                                source=self.node_EnemyFlagIndex,
                                target=self.node_EnemyScoreIndex)
        #vb2s = nx.shortest_path(self.graph, source="enemy_base", target=self.node_EnemyScoreIndex)

        self.node_EnemyBaseToFlagIndex = "enemy_base_to_flag"
        self.graph.add_node(self.node_EnemyBaseToFlagIndex)
        self.positions["enemy_base_to_flag"] = None
        for vertex in vb2f:
            self.graph.add_edge(self.node_EnemyBaseToFlagIndex,
                                vertex,
                                weight=1.0)

        self.node_EnemyFlagToScoreIndex = "enemy_flag_to_score"
        self.graph.add_node(self.node_EnemyFlagToScoreIndex)
        self.positions["enemy_flag_to_score"] = None
        for vertex in vf2s:
            self.graph.add_edge(self.node_EnemyFlagToScoreIndex,
                                vertex,
                                weight=1.0)

        self.node_EnemyBaseToScoreIndex = "enemy_base_to_score"
        self.graph.add_node(self.node_EnemyBaseToScoreIndex)
        self.positions["enemy_base_to_score"] = None
        # for vertex in vb2s:
        #     self.graph.add_edge(self.node_EnemyBaseToScoreIndex, vertex, weight = 1.0)

        ## node = self.makeNode(self.game.enemyTeam.flag.position)
        self.distances = nx.single_source_shortest_path_length(
            self.graph, self.node_EnemyFlagToScoreIndex)

        self.graph.remove_node("base")
        self.graph.remove_node("enemy_base")
        self.graph.remove_node(self.node_EnemyBaseToFlagIndex)
        self.graph.remove_node(self.node_EnemyFlagToScoreIndex)
        self.graph.remove_node(self.node_EnemyBaseToScoreIndex)

        self.updateEdgeWeights()

        self.paths = {b: None for b in self.game.team.members}

        self.visualizer = VisualizerApplication(self)
        self.visualizer.setDrawHookPreWorld(self.drawPreWorld)
        self.visualizer.setDrawHookPreBots(self.drawPreBots)
        self.visualizer.setDrawHookEnd(self.drawEnd)
예제 #3
0
    def initialize(self):
        self.mode = self.MODE_VISIBILITY
        self.visualizer = VisualizerApplication(self)

        self.visualizer.setDrawHookPreWorld(self.drawPreWorld)
        self.visualizer.setDrawHookPreBots(self.drawPreBots)
        self.visualizer.setKeyboardHook(self.keyPressed)

        self.makeGraph()

        self.graph.add_node("enemy_base")
        start, finish = self.level.botSpawnAreas[self.game.enemyTeam.name]
        for i, j in itertools.product(range(int(start.x), int(finish.x)),
                                      range(int(start.y), int(finish.y))):
            self.graph.add_edge("enemy_base", self.terrain[j][i], weight=1.0)

        self.graph.add_node("base")
        start, finish = self.level.botSpawnAreas[self.game.team.name]
        for i, j in itertools.product(range(int(start.x), int(finish.x)),
                                      range(int(start.y), int(finish.y))):
            self.graph.add_edge("base", self.terrain[j][i], weight=1.0)

        self.node_EnemyFlagIndex = self.getNodeIndex(
            self.game.team.flag.position)
        self.node_EnemyScoreIndex = self.getNodeIndex(
            self.game.enemyTeam.flagScoreLocation)

        # self.node_Bases = self.graph.add_vertex()
        # e = self.graph.add_edge(self.node_Bases, self.node_MyBase)
        # e = self.graph.add_edge(self.node_Bases, self.node_EnemyBase)

        vb2f = nx.shortest_path(self.graph,
                                source="enemy_base",
                                target=self.node_EnemyFlagIndex)
        vf2s = nx.shortest_path(self.graph,
                                source=self.node_EnemyFlagIndex,
                                target=self.node_EnemyScoreIndex)
        #vb2s = nx.shortest_path(self.graph, source="enemy_base", target=self.node_EnemyScoreIndex)

        self.visibilities = QtGui.QImage(88, 50, QtGui.QImage.Format_ARGB32)
        self.visibilities.fill(0)
        path = vb2f + vf2s
        edgesinpath = zip(path[0:], path[1:])
        for vt, vf in edgesinpath[:-1]:
            if "position" not in self.graph.node[vf]:
                continue
            position = Vector2(*self.graph.node[vf]["position"])
            if "position" not in self.graph.node[vt]:
                continue
            next_position = Vector2(*self.graph.node[vt]["position"])
            if position == next_position:
                continue
            orientation = (next_position - position).normalized()

            def visible(p):
                delta = (p - position)
                l = delta.length()
                if l > 20.0:
                    return False
                if l < 2.5:
                    return True
                delta /= l
                return orientation.dotProduct(delta) >= 0.5

            cells = []
            w = Wave((88, 50), lambda x, y: self.level.blockHeights[x][y] > 1,
                     lambda x, y: cells.append((x, y)))
            w.compute(position)

            for x, y in [
                    c for c in cells
                    if visible(Vector2(c[0] + 0.5, c[1] + 0.5))
            ]:
                self.visibilities.setPixel(x, y,
                                           self.visibilities.pixel(x, y) + 1)

        starte, finishe = self.level.botSpawnAreas[self.game.enemyTeam.name]
        startf, finishf = self.level.botSpawnAreas[self.game.team.name]
        points = [
            self.game.team.flag.position, self.game.enemyTeam.flag.position,
            self.game.team.flagScoreLocation,
            self.game.enemyTeam.flagScoreLocation
        ] * 4
        for i, j in list(itertools.product(range(int(starte.x), int(finishe.x)), range(int(starte.y), int(finishe.y)))) \
                    + list(itertools.product(range(int(startf.x), int(finishf.x)), range(int(startf.y), int(finishf.y)))) \
                    + [(int(p.x), int(p.y)) for p in points]:
            n = self.terrain[j][i]
            if not n: continue

            position = Vector2(*self.graph.node[n]["position"])

            def visible(p):
                delta = (p - position)
                l = delta.length()
                return l <= 15.0

            cells = []
            w = Wave((88, 50), lambda x, y: self.level.blockHeights[x][y] > 1,
                     lambda x, y: cells.append((x, y)))
            w.compute(position)

            for x, y in [
                    c for c in cells
                    if visible(Vector2(c[0] + 0.5, c[1] + 0.5))
            ]:
                self.visibilities.setPixel(x, y,
                                           self.visibilities.pixel(x, y) + 1)

        self.node_EnemyBaseToFlagIndex = "enemy_base_to_flag"
        self.graph.add_node(self.node_EnemyBaseToFlagIndex)
        for vertex in vb2f:
            self.graph.add_edge(self.node_EnemyBaseToFlagIndex,
                                vertex,
                                weight=1.0)

        self.node_EnemyFlagToScoreIndex = "enemy_flag_to_score"
        self.graph.add_node(self.node_EnemyFlagToScoreIndex)
        for vertex in vf2s:
            self.graph.add_edge(self.node_EnemyFlagToScoreIndex,
                                vertex,
                                weight=1.0)

        self.node_EnemyBaseToScoreIndex = "enemy_base_to_score"
        self.graph.add_node(self.node_EnemyBaseToScoreIndex)
        # for vertex in vb2s:
        #     self.graph.add_edge(self.node_EnemyBaseToScoreIndex, vertex, weight = 1.0)

        ## node = self.makeNode(self.game.enemyTeam.flag.position)
        self.distances = nx.single_source_shortest_path_length(
            self.graph, self.node_EnemyFlagToScoreIndex)
        self.queue = {}
        self.index = 0
        print "Done with init. Calculating ambush spots"
        self.calculateAmbushes()
예제 #4
0
파일: ambush.py 프로젝트: wildertm/jytwai
    def initialize(self):
        self.mode = self.MODE_VISIBILITY
        self.visualizer = VisualizerApplication(self)

        self.visualizer.setDrawHookPreWorld(self.drawPreWorld)
        self.visualizer.setDrawHookPreBots(self.drawPreBots)
        self.visualizer.setKeyboardHook(self.keyPressed)

        self.makeGraph()

        self.graph.add_node("enemy_base")
        start, finish = self.level.botSpawnAreas[self.game.enemyTeam.name]
        for i, j in itertools.product(range(int(start.x), int(finish.x)),
                                      range(int(start.y), int(finish.y))):
            self.graph.add_edge("enemy_base", self.terrain[j][i], weight=1.0)

        self.graph.add_node("base")
        start, finish = self.level.botSpawnAreas[self.game.team.name]
        for i, j in itertools.product(range(int(start.x), int(finish.x)),
                                      range(int(start.y), int(finish.y))):
            self.graph.add_edge("base", self.terrain[j][i], weight=1.0)

        self.node_EnemyFlagIndex = self.getNodeIndex(
            self.game.team.flag.position)
        self.node_EnemyScoreIndex = self.getNodeIndex(
            self.game.enemyTeam.flagScoreLocation)

        # self.node_Bases = self.graph.add_vertex()
        # e = self.graph.add_edge(self.node_Bases, self.node_MyBase)
        # e = self.graph.add_edge(self.node_Bases, self.node_EnemyBase)

        vb2f = nx.shortest_path(self.graph,
                                source="enemy_base",
                                target=self.node_EnemyFlagIndex)
        vf2s = nx.shortest_path(self.graph,
                                source=self.node_EnemyFlagIndex,
                                target=self.node_EnemyScoreIndex)
        #vb2s = nx.shortest_path(self.graph, source="enemy_base", target=self.node_EnemyScoreIndex)

        #Coloration based on score in an 88 by 50 pixel grid?
        #Grid squares are set with setPixel(xcoord, ycoord, intRepresentingDarkness)
        self.visibilities = QtGui.QImage(88, 50, QtGui.QImage.Format_ARGB32)
        self.visibilities.fill(0)
        #Path from enemy base to flag to scoring position.
        path = vb2f + vf2s
        #Zip returns a list of 0-nth tuples using ith items from each iterable.
        #Each represents two graph node indexes, or an edge along the path.
        edgesinpath = zip(path[0:], path[1:])
        #Vt is the start node index, vf is the end note index.
        for vt, vf in edgesinpath[:-1]:
            if "position" not in self.graph.node[vf]:
                continue
            position = Vector2(*self.graph.node[vf]["position"])
            if "position" not in self.graph.node[vt]:
                continue
            next_position = Vector2(*self.graph.node[vt]["position"])
            if position == next_position:
                continue
            #Facing vector of the move on the graph.
            orientation = (next_position - position).normalized()

            #TODO swap to actual vector/sight range visibility calc.
            def visible(p):
                delta = (p - position)
                l = delta.length()
                if l > 20.0:
                    return False
                if l < 2.5:
                    return True
                delta /= l
                return orientation.dotProduct(delta) >= 0.5

            #List of points that are visible from a designated point.
            cells = []
            w = Wave((88, 50), lambda x, y: self.level.blockHeights[x][y] > 1,
                     lambda x, y: cells.append((x, y)))
            w.compute(position)

            #Increment the visibility int of each point that it has visibility to and sees by one.
            for x, y in [
                    c for c in cells
                    if visible(Vector2(c[0] + 0.5, c[1] + 0.5))
            ]:
                self.visibilities.setPixel(x, y,
                                           self.visibilities.pixel(x, y) + 1)

        #A bunch of points along the paths are added into a big list of positions.
        #This piece of code makes squares very close to the path even higher, and create a bit of spread around the path.
        #Otherwise it is the same as the previous big chunk save a simpler visibility calculus.
        starte, finishe = self.level.botSpawnAreas[self.game.enemyTeam.name]
        startf, finishf = self.level.botSpawnAreas[self.game.team.name]
        points = [
            self.game.team.flag.position, self.game.enemyTeam.flag.position,
            self.game.team.flagScoreLocation,
            self.game.enemyTeam.flagScoreLocation
        ] * 4
        for i, j in list(itertools.product(range(int(starte.x), int(finishe.x)), range(int(starte.y), int(finishe.y)))) \
                    + list(itertools.product(range(int(startf.x), int(finishf.x)), range(int(startf.y), int(finishf.y)))) \
                    + [(int(p.x), int(p.y)) for p in points]:
            #Yields node index for each list coordinate pair.
            n = self.terrain[j][i]
            if not n: continue

            position = Vector2(*self.graph.node[n]["position"])

            def visible(p):
                delta = (p - position)
                l = delta.length()
                return l <= 15.0

            cells = []
            w = Wave((88, 50), lambda x, y: self.level.blockHeights[x][y] > 1,
                     lambda x, y: cells.append((x, y)))
            w.compute(position)

            for x, y in [
                    c for c in cells
                    if visible(Vector2(c[0] + 0.5, c[1] + 0.5))
            ]:
                self.visibilities.setPixel(x, y,
                                           self.visibilities.pixel(x, y) + 1)

        #Add weighted edges to graph
        self.node_EnemyBaseToFlagIndex = "enemy_base_to_flag"
        self.graph.add_node(self.node_EnemyBaseToFlagIndex)
        for vertex in vb2f:
            self.graph.add_edge(self.node_EnemyBaseToFlagIndex,
                                vertex,
                                weight=1.0)

        self.node_EnemyFlagToScoreIndex = "enemy_flag_to_score"
        self.graph.add_node(self.node_EnemyFlagToScoreIndex)
        for vertex in vf2s:
            self.graph.add_edge(self.node_EnemyFlagToScoreIndex,
                                vertex,
                                weight=1.0)

        self.node_EnemyBaseToScoreIndex = "enemy_base_to_score"
        self.graph.add_node(self.node_EnemyBaseToScoreIndex)

        #Calculate shortest path to enemy flag from each point.
        self.distances = nx.single_source_shortest_path_length(
            self.graph, self.node_EnemyFlagToScoreIndex)

        self.queue = {}
        self.index = 0
        print "Done with init. Calculating ambush spots"
        self.calculateAmbushes()