Esempio n. 1
0
def main():
    # Variables
    path = lib.getPath(lib.getCell(lib.getMyId()),
                       lib.getCell(lib.getEnemyId()))
    if path != -1:
        lib.mark(path, 'blue')
    start = time.time()
    lib.setWeapon(lib.WEAPON_SWORD)
    grid = Grid(lib.getMyId(), lib.getEnemyId())
    best_move = minimax(grid, 3)
    path = lib.getPath(lib.getCell(lib.getMyId()),
                       [best_move[0], best_move[1]])
    print(best_move, path, file=sys.stderr)
    if path != -1:
        for cell in path:
            lib.moveOn(cell)
    if best_move[2] == '[FLEE]':
        lib.useHeal([best_move[0], best_move[1]])
    elif best_move[2] == '[ATTACK]':
        if best_move[3]['name'] == 'SimpleGun':
            lib.setWeapon(lib.WEAPON_SIMPLE_GUN)
        elif best_move[3]['name'] == 'Sword':
            lib.setWeapon(lib.WEAPON_SWORD)
        lib.attackOn(lib.getCell(lib.getEnemyId()))
        for cell in lib.getPath(lib.getCell(lib.getMyId()),
                                lib.getCell(lib.getEnemyId())):
            lib.moveOn(cell)
    print(time.time() - start, file=sys.stderr)
Esempio n. 2
0
def tile(request, zoom, x, y):
    # Specify that the response will be a png image
    response = HttpResponse(content_type="image/png")

    params =  {
        'ssid': request.GET.get('ssid', None),
        'agg_function': request.GET.get('agg_function', 'median')
    }

    # Check if the ssid is one of the ssids that we prerender
    parentPath = os.path.join(settings.TILE_DIR, params['ssid'])
    if os.path.exists(parentPath):
        # If it is, check to see if we have a tile
        # If the tile exists, open it.  Otherwise return a transparent tile.
        path = getPath(params['ssid'], params['agg_function'], zoom, x, y)
        if os.path.exists(path):
            Image.open(path).save(response, "PNG")
        else:
            Image.new("RGBA", (256, 256)).save(response, "PNG")
    else:
        # If the ssid is not precomputed, render on the fly
        generateTile(
            int(x), int(y), int(zoom), params
        ).save(response, "PNG")

    return response
Esempio n. 3
0
def getpath():
  path = request.query.path
  default_path = getUser()
  if default_path['status'] == 0:
    default_path = default_path['user']['root_path']
    data = getPath(default_path, path)
    return data.getJson()
  return default_path
Esempio n. 4
0
def moveMap(center, mp):
    moveMap = [center]
    for y in range(lib.getMapHeight()):
        for x in range(lib.getMapWidth()):
            if lib.getDistance([x, y], center) < mp + 1 and lib.getCellContent(
                [x, y]) == lib.CELL_EMPTY:
                path = lib.getPath([x, y], center)
                if path != -1 and len(path) <= mp:
                    moveMap.append([x, y])
    return moveMap
Esempio n. 5
0
def main():
    sId = lib.getMyId()
    sPos = lib.getCell(sId)
    sMP = lib.getMp(sId)
    ePos = lib.getCell(lib.getEnemyId())
    lib.setWeapon(lib.WEAPON_SIMPLE_GUN)

    path = lib.getPath(sPos, ePos)
    if path != -1:
        for cell in path:
            lib.moveOn(cell)

    lib.attackOn(ePos)

    pass
Esempio n. 6
0
def tile(request, zoom, x, y):
    # Specify that the response will be a png image
    response = HttpResponse(content_type="image/png")
    params = {
        'ssid': request.GET.get('ssid', None),
        'agg_function': request.GET.get('agg_function', 'median')
    }

    # Check if the ssid is one of the ssids that we prerender
    parentPath = os.path.join(settings.TILE_DIR, params['ssid'])
    if os.path.exists(parentPath):
        # If it is, check to see if we have a tile
        # If the tile exists, open it.  Otherwise return a transparent tile.
        path = getPath(params['ssid'], params['agg_function'], zoom, x, y)
        if os.path.exists(path):
            Image.open(path).save(response, "PNG")
        else:
            Image.new("RGBA", (256, 256)).save(response, "PNG")
    else:
        # If the ssid is not precomputed, render on the fly
        generateTile(int(x), int(y), int(zoom), params).save(response, "PNG")

    return response
Esempio n. 7
0
def main():
    print('---USER 1---')
    lib.setWeapon(lib.WEAPON_SIMPLE_GUN)
    selfId = lib.getMyId()
    selfPos = lib.getCell(selfId)
    selfMp = lib.getMp(selfId)

    enemyId = lib.getEnemyId()
    enemyPos = lib.getCell(enemyId)
    enemyMp = lib.getMp(enemyId)

    tab = []
    OBSTACLES = lib.getObstacles()

    # Movemap
    selfMoveMap = moveMap(selfPos, selfMp)
    #for cell in selfMoveMap:
    #    lib.mark(cell, 'green')

    enemyMoveMap = moveMap(enemyPos, enemyMp)
    print(enemyPos, enemyMp, enemyMoveMap, file=sys.stderr)
    #for cell in enemyMoveMap:
    #    lib.mark(cell, 'red')

    # Find safe cell / attack cell

    canHit = False
    bestMove = [[], -1]
    for simulated in selfMoveMap:
        # Find far cell where we can attack
        if lib.getDistance(simulated, enemyPos) <= 5 and lib.getLineOfSight(
                simulated, enemyPos):
            if lib.getDistance(simulated, enemyPos) >= bestMove[1]:
                bestMove = [
                    simulated.copy(),
                    lib.getDistance(simulated, enemyPos)
                ]
                canHit = True

    if canHit:
        lib.mark(bestMove[0], 'blue')
        path = lib.getPath(selfPos, bestMove[0])
        if path != -1:
            for move in path:
                lib.moveOn(move)
                selfMp -= 1
            lib.attackOn(enemyPos)
            lib.mark(bestMove[0], 'yellow')
            selfPos = bestMove[0]

    # Flee on safe cell
    canFlee = False
    bestFlee = [[], 99999]
    if (selfMp > 0):
        selfMoveMap = moveMap(selfPos, selfMp)
        for selfSimu in selfMoveMap:
            safeCell = True
            for enemySimu in enemyMoveMap:
                print('Simualted (E/S):',
                      enemySimu,
                      '/',
                      selfSimu,
                      file=sys.stderr)
                if lib.getDistance(selfSimu,
                                   enemySimu) <= 5 and lib.getLineOfSight(
                                       selfSimu, enemySimu):
                    # If user can hit us, then cell isn't safe
                    safeCell = False
                    break
            if safeCell:
                lib.mark(selfSimu, 'green')
                canFlee = True
                if (bestFlee[1] >= lib.getDistance(selfSimu, enemyPos)):
                    bestFlee = [[selfSimu.copy()],
                                lib.getDistance(selfSimu, enemyPos)]
                elif (bestFlee[1] == lib.getDistance(selfSimu, enemyPos)):
                    bestFlee[0].append(selfSimu.copy())
            else:
                lib.mark(selfSimu, 'red')

        if canFlee:
            bestFlee[0] = random.choice(bestFlee[0])
            lib.mark(bestFlee[0], 'blue')
            path = lib.getPath(selfPos, bestFlee[0])
            if path != -1:
                for move in path:
                    lib.moveOn(move)
                    selfMp -= 1
                selfPos = bestFlee[0]
Esempio n. 8
0
    def availableMoves(self):
        mp = self.players[self.whoPlay]['mp']
        x, y = self.players[self.whoPlay]['position']
        #print('X/Y', x, y)
        path = lib.getPath(list(self.players[self.whoPlay]['position']),
                           list(self.players[1 - self.whoPlay]['position']))
        if path != -1:
            moves = [path[0]]
            moves[0].append('[FLEE]')
            """if len(path) <= 4:
                print('path', path[2])
                moves.append[path[max(0, len(path) - 2)]]
                moves[1].append('[ATTACK]', self.weapons[lib.WEAPON_SWORD])"""
        else:
            moves = [list(self.players[self.whoPlay]['position'])]
            moves[0].append('[FLEE]')
        if self.whoPlay == self.myId:
            for weapon in self.weapons:
                accessibleCells = [[x, y]]
                enemyPos = lib.getCell(1 - self.whoPlay)
                weaponRange = weapon['maxRange']

                last = [[x, y]]
                while mp >= 0:
                    tmp = []
                    for cell in last:
                        if lib.getLineOfSight(
                                cell, enemyPos) and lib.getDistance(
                                    cell, enemyPos) <= weaponRange:
                            moves.append(
                                tuple(cell) + tuple(['[ATTACK]', weapon]))
                            mp = 0
                        else:
                            for dx, dy in [(0, 1), (0, -1), (-1, 0), (1, 0)]:
                                if lib.getCellContent(
                                    (cell[0] + dx,
                                     cell[1] + dy)) == lib.CELL_EMPTY and not [
                                         cell[0] + dx, cell[1] + dy
                                     ] in accessibleCells:
                                    tmp.append([cell[0] + dx, cell[1] + dy])
                                    accessibleCells.append(
                                        [cell[0] + dx, cell[1] + dy])
                    mp -= 1
                    last = tmp.copy()
        else:
            accessibleCells = [[x, y]]
            enemyPos = lib.getCell(1 - self.whoPlay)
            weaponRange = self.weapons[self.players[self.whoPlay]
                                       ['currentWeapon']]['maxRange']

            last = [[x, y]]
            while mp >= 0:
                tmp = []
                for cell in last:
                    if lib.getLineOfSight(cell, enemyPos) and lib.getDistance(
                            cell, enemyPos) <= weaponRange:
                        moves.append(
                            tuple(cell) + tuple([
                                '[ATTACK]', self.weapons[self.players[
                                    self.whoPlay]['currentWeapon']]
                            ]))
                        mp = 0
                    else:
                        for dx, dy in [(0, 1), (0, -1), (-1, 0), (1, 0)]:
                            if lib.getCellContent(
                                (cell[0] + dx,
                                 cell[1] + dy)) == lib.CELL_EMPTY and not [
                                     cell[0] + dx, cell[1] + dy
                                 ] in accessibleCells:
                                tmp.append([cell[0] + dx, cell[1] + dy])
                                accessibleCells.append(
                                    [cell[0] + dx, cell[1] + dy])
                mp -= 1
                last = tmp.copy()

        # Defensive moves
        mp = self.players[self.whoPlay]['mp']
        last = [[x, y]]
        accessibleCells = [[x, y]]
        defMoves = []
        while mp > 0:
            tmp = []
            h = False
            if defMoves:
                h = True
            for cell in last:
                for dx, dy in [(0, 1), (0, -1), (-1, 0), (1, 0)]:
                    if (lib.getCellContent((cell[0] + dx, cell[1] + dy))
                            == lib.CELL_EMPTY) and not [
                                cell[0] + dx, cell[1] + dy
                            ] in accessibleCells:
                        tmp.append([cell[0] + dx, cell[1] + dy])
                        accessibleCells.append([cell[0] + dx, cell[1] + dy])
                        if not lib.getLineOfSight([cell[0] + dx, cell[1] + dy],
                                                  enemyPos):
                            if h:
                                defMoves = []
                                h = False
                            defMoves.append(
                                (cell[0] + dx, cell[1] + dy, '[FLEE]'))

            mp -= 1
            last = tmp.copy()

        for move in defMoves:
            moves.append(move)

        return moves