Exemple #1
0
    def drawLevel(self):
        visible = QtGui.QImage(88, 50, QtGui.QImage.Format_Mono)
        visible.fill(0)
        if self.mouse:
            w = Wave((88, 50),
                     lambda x, y: self.level.blocks.pixel(x, y) != 4294967295,
                     lambda x, y: visible.setPixel(x, y, 1))
            w.compute(
                Vector2(
                    float(self.mouse.x()) / 10.0,
                    float(self.mouse.y()) / 10.0))

        for i, j in itertools.product(range(88), range(50)):
            color = self.level.blocks.pixel(i, j)
            if visible.pixel(i, j) != 4278190080:
                self.drawPixel((i, j), QtGui.qRgb(192, 0, 192))
            else:
                self.drawPixel((i, j), color)

        colors = [QtGui.QColor(255, 0, 0), QtGui.QColor(32, 32, 255)]
        for i, (x, y) in enumerate(self.level.bases):
            self.drawBox((x - 2, y - 2), colors[i], 5, 'B')

        for i, (x, y) in enumerate(self.level.goals):
            self.drawBox((x - 1, y - 1), colors[i].darker(125), 3, 'G')

        for i, (x, y) in enumerate(self.level.flags):
            self.drawBox((x - 1, y - 1), colors[i].lighter(125), 3, 'F')
Exemple #2
0
    def evaluate(self, position, orientation, callback):
        cells = []
        w = Wave((88, 50), lambda x, y: self.level.blockHeights[x][y] > 1, lambda x, y: cells.append((x,y)))
        w.compute(position)

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

        total = 0.0
        for x, y in [c for c in cells if visible(Vector2(c[0]+0.5, c[1]+0.5))]:
            total += callback(x,y)
        return total
Exemple #3
0
    def evaluate(self, position, orientation, callback):
        cells = []
        w = Wave((88, 50), lambda x, y: self.level.blockHeights[x][y] > 1,
                 lambda x, y: cells.append((x, y)))
        w.compute(position)

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

        total = 0.0
        for x, y in [
                c for c in cells if visible(Vector2(c[0] + 0.5, c[1] + 0.5))
        ]:
            total += callback(x, y)
        return total
Exemple #4
0
    def drawLevel(self):
        visible = QtGui.QImage(88, 50, QtGui.QImage.Format_Mono)
        visible.fill(0)
        if self.mouse:
            w = Wave((88, 50), lambda x, y: self.level.blocks.pixel(x, y) != 4294967295, lambda x, y: visible.setPixel(x, y, 1))
            w.compute(Vector2(float(self.mouse.x())/10.0, float(self.mouse.y())/10.0))

        for i, j in itertools.product(range(88), range(50)):
            color = self.level.blocks.pixel(i, j)
            if visible.pixel(i, j) != 4278190080:
                self.drawPixel((i, j), QtGui.qRgb(192, 0, 192))
            else:
                self.drawPixel((i, j), color)

        colors = [QtGui.QColor(255,0,0), QtGui.QColor(32,32,255)]
        for i, (x, y) in enumerate(self.level.bases):
            self.drawBox((x-2, y-2), colors[i], 5, 'B')
            
        for i, (x, y) in enumerate(self.level.goals):
            self.drawBox((x-1, y-1), colors[i].darker(125), 3, 'G')

        for i, (x, y) in enumerate(self.level.flags):
            self.drawBox((x-1, y-1), colors[i].lighter(125), 3, 'F')
Exemple #5
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()
Exemple #6
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()
Exemple #7
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)

        #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()
Exemple #8
0
    def initialize(self):
        self.midEnemySpawn = self.game.enemyTeam.botSpawnArea[0].midPoint(self.game.enemyTeam.botSpawnArea[1])
        visited, self.islandEdges, islandOuter = [], [], []
        for x in range(0, len(self.level.blockHeights)):
            for y in range(0, len(self.level.blockHeights[x])):
                _, edges, island = self.recurseNeighbours(x, y, visited)
                if edges:
                    self.islandEdges.append(edges)
                    islandOuter.append(island)
        blocked = [item for sublist in islandOuter for item in sublist]
        #blockedOrSpawn = blocked[:]
        spawn = []
        for x in range(int(self.game.enemyTeam.botSpawnArea[0].x), int(self.game.enemyTeam.botSpawnArea[1].x)):
            for y in range(int(self.game.enemyTeam.botSpawnArea[0].y), int(self.game.enemyTeam.botSpawnArea[1].y)):
                spawn.append(Vector2(x, y))
        
        self.campLines = []
        for island in self.islandEdges:
            for coord, orientation in island:
                if orientation is self.TOPLEFT:
                    self.campLines.append((coord, Vector2(-self.level.firingDistance / 1.0283968, 0.24 * self.level.firingDistance / 1.0283968)))
                elif orientation is self.BOTTOMLEFT:
                    self.campLines.append((coord, Vector2(-self.level.firingDistance / -1.0283968, -0.24 * self.level.firingDistance / 1.0283968)))
                elif orientation is self.LEFTUP:
                    self.campLines.append((coord, Vector2(0.24 * self.level.firingDistance / 1.0283968, -self.level.firingDistance / 1.0283968)))
                elif orientation is self.RIGHTUP:
                    self.campLines.append((coord, Vector2(-0.24 * self.level.firingDistance / 1.0283968, -self.level.firingDistance / 1.0283968)))
                elif orientation is self.TOPRIGHT:
                    self.campLines.append((coord, Vector2(self.level.firingDistance / 1.0283968, 0.24 * self.level.firingDistance / 1.0283968)))
                elif orientation is self.BOTTOMRIGHT:
                    self.campLines.append((coord, Vector2(self.level.firingDistance / 1.0283968, -0.24 * self.level.firingDistance / 1.0283968)))
                elif orientation is self.LEFTDOWN:
                    self.campLines.append((coord, Vector2(0.24 * self.level.firingDistance / 1.0283968, self.level.firingDistance / 1.0283968)))
                elif orientation is self.RIGHTDOWN:
                    self.campLines.append((coord, Vector2(-0.24 * self.level.firingDistance / 1.0283968, self.level.firingDistance / 1.0283968)))
        sys.stdout.write(str(self.campLines) + '\n')
        self.deadlines = dict()
        for i in range(len(self.islandEdges)):
            for coord, orientation in self.islandEdges[i]:
                if orientation is self.TOPLEFT:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x - self.level.firingDistance / 1.0283968, coord.y + 0.24 * self.level.firingDistance / 1.0283968))
                elif orientation is self.BOTTOMLEFT:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x - self.level.firingDistance / -1.0283968, coord.y - 0.24 * self.level.firingDistance / 1.0283968))
                elif orientation is self.LEFTUP:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x + 0.24 * self.level.firingDistance / 1.0283968, coord.y - self.level.firingDistance / 1.0283968))
                elif orientation is self.RIGHTUP:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x - 0.24 * self.level.firingDistance / 1.0283968, coord.y - self.level.firingDistance / 1.0283968))
                elif orientation is self.TOPRIGHT:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x + self.level.firingDistance / 1.0283968, coord.y + 0.24 * self.level.firingDistance / 1.0283968))
                elif orientation is self.BOTTOMRIGHT:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x + self.level.firingDistance / 1.0283968, coord.y - 0.24 * self.level.firingDistance / 1.0283968))
                elif orientation is self.LEFTDOWN:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x + 0.24 * self.level.firingDistance / 1.0283968, coord.y + self.level.firingDistance / 1.0283968))
                elif orientation is self.RIGHTDOWN:
                    self.deadlineFromLine(blocked, spawn, coord, Vector2(coord.x - 0.24 * self.level.firingDistance / 1.0283968, coord.y + self.level.firingDistance / 1.0283968))
                
        sys.stdout.write(str(self.deadlines) + '\n')
        pointsAndLinesByEdge = dict()
        try:
            self.recursePaths(self.midEnemySpawn, blocked, self.deadlines, [], pointsAndLinesByEdge)
        except RuntimeError as e:
            sys.stdout.write(str(e) + '\n')
        self.spawnCamplines = set()
        for edge, pls in pointsAndLinesByEdge.iteritems():
            for _, contact in pls:
                self.spawnCamplines.add((self.level.findNearestFreePosition(edge), contact))
        sys.stdout.write('\n' + str(self.spawnCamplines))
        
        self.spawnCampers = []
        for cl in self.spawnCamplines:
            self.spawnCampers.append([[], cl])
        
        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
        #path = vb2f = nx.shortest_path(self.graph, source="enemy_base", target=self.node_EnemyFlagIndex)
        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.distances = nx.single_source_shortest_path_length(self.graph, self.node_EnemyBaseToFlagIndex)
        self.queue = {}
        self.index = 0
        print "Done with init. Calculating ambush spots"
        self.calculateAmbushes(self.campLines)
        self.aliveEnemies = 0
Exemple #9
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)

        #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()