Exemple #1
0
def a_star_algorithm(graph, start, end):
    frontier = priorityQueue()
    frontier.enQueue(utils.tup(start), 0)
    path = {}
    cost = {}
    path[utils.tup(start)] = None
    cost[utils.tup(start)] = 0

    while not frontier.isEmpty():
        current = frontier.deQueue()
        if current == end:
            break
        for nb in graph.find_neighbors(vec(current)):
            nb = utils.tup(nb)
            # c(move) = f(x) + g(x) ~> cost = costUpToHere + heuristic
            nextCost = cost[current] + graph.cost(current, nb)
            if nb not in cost or nextCost < cost[nb]:
                cost[nb] = nextCost
                priority = nextCost + heuristic(end, vec(nb))
                frontier.enQueue(nb, priority)
                path[nb] = {
                    "from": vec(current),
                    "direct": vec(current) - vec(nb)
                }
    return path
Exemple #2
0
    def initScene(self):
        # re-create the map, camera, spawners for new scene
        self.map = grid.mapManager(self)
        self.camera = grid.camera(self, self.map.width, self.map.height)
        self.objects.spawners = []
        self.graph = grid.weightedGrid(self, self.map.width, self.map.height)

        # map parsing #
        for rowIndex, tiles in enumerate(self.map.data):
            for collumIndex, tile in enumerate(tiles):
                # cordinates  using row and collum index
                topLeft = vec(collumIndex, rowIndex) * self.state.tileSize
                self.graph.weights[utils.tup(topLeft)] = settings.t_weight
                # wall sprite #
                if tile == 'w':
                    environments.wall(self, topLeft)
                    self.graph.walls.append(utils.tup(topLeft))
                # player sprite #
                elif tile == 'P':
                    self.objects.player = actors.player(self, topLeft)
                # zombie sprite #
                elif tile == 'Z':
                    actors.zombie(self, topLeft)
                # spawner tile #
                elif tile == 's':
                    self.objects.spawners.append(topLeft)

        # initilize text
        self.materialsTxt = "MATERIALS: " + str(
            self.objects.buildMode.buildPoints)
Exemple #3
0
def compute_message_trees(messages):
    from r2.models import Message
    roots = set()
    threads = {}
    mdict = {}
    messages = sorted(messages, key=lambda m: m._date, reverse=True)

    for m in messages:
        if not m._loaded:
            m._load()
        mdict[m._id] = m
        if m.first_message:
            roots.add(m.first_message)
            threads.setdefault(m.first_message, set()).add(m._id)
        else:
            roots.add(m._id)

    # load any top-level messages which are not in the original list
    missing = [m for m in roots if m not in mdict]
    if missing:
        mdict.update(Message._byID(tup(missing), return_dict=True, data=True))

    # sort threads in chrono order
    for k in threads:
        threads[k] = list(sorted(threads[k]))

    tree = [(root, threads.get(root, [])) for root in roots]
    tree.sort(key=tree_sort_fn, reverse=True)

    return tree
Exemple #4
0
def compute_message_trees(messages):
    from r2.models import Message
    roots = set()
    threads = {}
    mdict = {}
    messages = sorted(messages, key = lambda m: m._date, reverse = True)

    for m in messages:
        if not m._loaded:
            m._load()
        mdict[m._id] = m
        if m.first_message:
            roots.add(m.first_message)
            threads.setdefault(m.first_message, set()).add(m._id)
        else:
            roots.add(m._id)

    # load any top-level messages which are not in the original list
    missing = [m for m in roots if m not in mdict]
    if missing:
        mdict.update(Message._byID(tup(missing),
                                   return_dict = True, data = True))

    # sort threads in chrono order
    for k in threads:
        threads[k] = list(sorted(threads[k]))

    tree = [(root, threads.get(root, [])) for root in roots]
    tree.sort(key = tree_sort_fn, reverse = True)

    return tree
Exemple #5
0
def last_modified_multi(things, action):
    from pylons import g
    cache = g.permacache

    things = tup(things)
    keys = dict((last_modified_key(thing, action), thing) for thing in things)

    last_modified = cache.get_multi(keys.keys())
    return dict((keys[k], v) for k, v in last_modified.iteritems())
Exemple #6
0
def last_modified_multi(things, action):
    from pylons import g
    cache = g.permacache

    things = tup(things)
    keys = dict((last_modified_key(thing, action), thing) for thing in things)

    last_modified = cache.get_multi(keys.keys())
    return dict((keys[k], v) for k, v in last_modified.iteritems())
Exemple #7
0
 def navPath(self, path, start, target):
     pos = target
     shortestPath = []
     while pos != start:
         currentNode = path.get(utils.tup(pos),None)
         if currentNode == None: break
         pos, move = currentNode["from"], -currentNode["direct"]
         shortestPath.append(move)
     shortestPath.reverse()
     return shortestPath
Exemple #8
0
def update_comment_votes(comments):
    comments = tup(comments)
    link_map = {}
    for com in comments:
        link_map.setdefault(com.link_id, []).append(com)
    for link_id, coms in link_map.iteritems():
        with g.make_lock(lock_key(link_id)):
            for sort in ("_controversy", "_hot", "_confidence", "_score"):
                key = sort_comments_key(link_id, sort)
                r = g.permacache.get(key)
                # don't bother recomputing a non-existant sort dict, as
                # we'll catch it next time we have to render something
                if r:
                    for comment in coms:
                        r[comment._id] = _get_sort_value(comment, sort)
                    g.permacache.set(key, r)
Exemple #9
0
def update_comment_votes(comments):
    comments = tup(comments)
    link_map = {}
    for com in comments:
        link_map.setdefault(com.link_id, []).append(com)
    for link_id, coms in link_map.iteritems():
        with g.make_lock(lock_key(link_id)):
            for sort in ("_controversy", "_hot", "_confidence", "_score"):
                key = sort_comments_key(link_id, sort)
                r = g.permacache.get(key)
                # don't bother recomputing a non-existant sort dict, as
                # we'll catch it next time we have to render something
                if r:
                    for comment in coms:
                        r[comment._id] = _get_sort_value(comment, sort)
                    g.permacache.set(key, r)
Exemple #10
0
 def thing_attr(self, thing, attr):
     if attr == "pref_clickgadget":
         return bool(thing.pref_clickgadget)
     elif attr == "pref_content_langs":
         return tup(thing.pref_content_langs)
     return ThingJsonTemplate.thing_attr(self, thing, attr)
Exemple #11
0
 def thing_attr(self, thing, attr):
     if attr == "pref_clickgadget":
         return bool(thing.pref_clickgadget)
     elif attr == "pref_content_langs":
         return tup(thing.pref_content_langs)
     return ThingJsonTemplate.thing_attr(self, thing, attr)