Esempio n. 1
0
    def __init__(self, config, scenario):
        game = vizdoom.DoomGame()
        game.load_config(config)
        game.set_doom_scenario_path(scenario)
        game.init()
        self.game = game
        self.scenario = scenario
        self.wad = WAD(scenario)

        num_buttons = len(game.get_available_buttons())
        self.action_space = gym.spaces.Discrete(num_buttons)
        self.action_map = tuple(
            [action_idx == button_idx for button_idx in range(num_buttons)]
            for action_idx in range(num_buttons))
        self.observation_space = gym.spaces.Tuple(
            (gym.spaces.Box(0, 1, (3, 82, 82), dtype=np.float32),
             gym.spaces.Box(0, 1, (8, 4 * 16), dtype=np.float32),
             gym.spaces.Box(-1, 1, (0, ), dtype=np.float32),
             gym.spaces.Discrete(num_buttons),
             gym.spaces.Box(-1, 1, (3, ), dtype=np.float32)))
        self.current_map = None
        self.episode_reward = 0.0
        self.step_counter = 0
        self.seed()
        self.reset()
Esempio n. 2
0
def create_maze(base_filepath, filename, size, cell_size, keep_prob):
    # load the base file
    BASE_WAD = 'two_color_basefile.wad'
    BASE_CFG = 'two_color_basefile.cfg'

    wad = WAD('scenarios/basefiles/' + BASE_WAD)
    cfg_filename = '{}{}.cfg'.format(base_filepath, filename[:-4])
    shutil.copy('scenarios/basefiles/' + BASE_CFG, cfg_filename)

    if '/' in filename:
        wad_filename = filename.split('/')[-1]
    else:
        wad_filename = filename
    # change the maze name in .cfg file
    # Read in the file
    with open('scenarios/basefiles/' + BASE_CFG, 'r') as file:
        filedata = file.read()

    # Replace the target string
    filedata = filedata.replace(BASE_WAD, wad_filename)

    # Write the file out again
    with open(cfg_filename, 'w') as file:
        file.write(filedata)

    details = {}
    verticies = []
    wall_cons = []
    wall_idx = 0
    map_point_idx = 10
    output_list = ['// Written by generate_mino_maze', 'namespace="zdoom";']

    # create the two map points

    xmin = -size // 2 * cell_size
    ymin = -size // 2 * cell_size

    spawn_spot_i = random.randint(0, size - 1)
    spawn_spot_j = random.randint(0, size - 1)

    while (spawn_spot_i, spawn_spot_j) == (-1, -1):
        spawn_spot_i = random.randint(0, size - 1)
        spawn_spot_j = random.randint(0, size - 1)

    spawn_spot_x = xmin + spawn_spot_i * cell_size + cell_size / 2
    spawn_spot_y = ymin + spawn_spot_j * cell_size + cell_size / 2

    #plt.subplot(1,2,2)
    plt.scatter(-cell_size / 2, -cell_size / 2, c='r')
    plt.scatter(spawn_spot_x, spawn_spot_y, c='k')

    output_list += create_spawn(spawn_spot_x, spawn_spot_y)
    output_list += create_object(-4096.0, -0.0, 30, 11)
    output_list += create_object(-4000., -0.0, 32, 12)
    output_list += create_object(-cell_size / 2, -cell_size / 2, 9001, 10)

    map_point_idx += 1

    exterior, walls = gen_maze(size,
                               cell_size,
                               xmin=xmin,
                               ymin=ymin,
                               keep_prob=keep_prob)
    verticies += exterior[:-1]

    details['start'] = (spawn_spot_x, spawn_spot_y)
    details['end'] = (-cell_size / 2, -cell_size / 2)
    details['exterior'] = exterior[:-1]
    details['walls'] = walls

    with open(base_filepath + filename[:-4] + '.json', 'w') as f:
        json.dump(details, f)

    for k in range(4):
        wall_cons.append((wall_idx + k, wall_idx + ((k + 1) % 4)))
    wall_idx += 4

    pad = 8

    for wall in walls:
        x0, y0, x1, y1 = wall

        if x0 == x1:
            verticies += [(x0 - pad, y0), (x1 + pad, y0), (x1 + pad, y1),
                          (x0 - pad, y1)]
        else:
            verticies += [(x0, y0 - pad), (x1, y0 - pad), (x1, y1 + pad),
                          (x0, y1 + pad)]

        for k in range(4):
            wall_cons.append((wall_idx + k, wall_idx + ((k + 1) % 4)))
        wall_idx += 4

    for vx, vy in verticies:
        output_list += create_vertex(vx, vy)

    for id1, id2 in wall_cons:
        output_list += create_line_def(id1, id2)
        output_list += create_side_def()

    output_list += create_sector_toxic()

    ## iterate through list to create output text file
    output_string = ''
    for output in output_list:
        output_string += output + '\n'

    wad.data['TEXTMAP'].data = output_string.encode()
    wad.to_file(base_filepath + filename)

    plt.savefig(base_filepath + filename[:-4] + '.jpg')
    plt.close()
Esempio n. 3
0
def create_maze(base_filepath, filename, size, cell_size):
    # load the base file
    BASE_WAD = 'find_return_basefile.wad'
    BASE_CFG = 'find_return_basefile.cfg'
    
    wad = WAD('scenarios/basefiles/' + BASE_WAD)
    cfg_filename = '{}{}.cfg'.format(base_filepath,filename[:-4])
    shutil.copy('scenarios/basefiles/' + BASE_CFG, cfg_filename)
    
    if '/' in filename:
        wad_filename = filename.split('/')[-1]
    else:
        wad_filename = filename
    # change the maze name in .cfg file
    # Read in the file
    with open('scenarios/basefiles/' + BASE_CFG, 'r') as file:
      filedata = file.read()
    
    # Replace the target string
    filedata = filedata.replace(BASE_WAD, wad_filename)
    
    # Write the file out again
    with open(cfg_filename, 'w') as file:
      file.write(filedata)    
    
    details = {}
    verticies = []
    wall_cons = []
    wall_idx = 0
    map_point_idx = 10
    output_list = ['// Written by anonymous', 'namespace="zdoom";']
    output_list += create_green_armor(-2048, -2048, invisible=True)
    output_list += create_red_armor(-2048, -2048+64, invisible=True)
    output_list += create_red_pillar(-2048, -2048+64)
    output_list += create_green_pillar(-2048, -2048+64)
    
    
    output_list += create_spawn(64, 64)
    xmin = -size//2 * cell_size
    ymin = -size//2 * cell_size
    
    # red spot
    red_spot_i = random.randint(0, size-1)
    red_spot_j = random.randint(0, size-1)
    
    green_spot_i = random.randint(0, size-1)
    green_spot_j = random.randint(0, size-1)
    
    while((red_spot_i, red_spot_j) == (green_spot_i, green_spot_j)):
        #print('retry')
        green_spot_i = random.randint(0, size-1)
        green_spot_j = random.randint(0, size-1)        
    
    
    red_spot_x = xmin + red_spot_i*cell_size + cell_size/2
    red_spot_y = ymin + red_spot_j*cell_size + cell_size/2
    green_spot_x = xmin + green_spot_i*cell_size + cell_size/2
    green_spot_y = ymin + green_spot_j*cell_size + cell_size/2
    #plt.subplot(1,2,2)
    plt.scatter(red_spot_x, red_spot_y, c='r')
    plt.scatter(green_spot_x, green_spot_y, c='g')
    
    
    output_list += create_map_point(red_spot_x, red_spot_y, 10)
    output_list += create_map_point(green_spot_x, green_spot_y, 11)
    map_point_idx += 1
    
    exterior, walls = gen_maze(size, cell_size, xmin=xmin, ymin=ymin, keep_prob=6)
    details['start'] = (green_spot_x, green_spot_y)
    details['end'] = (red_spot_x, red_spot_y)
    
    details['exterior'] = exterior[:-1]
    details['walls'] = walls   
    
    with open(base_filepath+filename[:-4]+'.json', 'w') as f:
        json.dump(details, f) 
    
    verticies += exterior[:-1]
    
    for k in range(4):
        wall_cons.append((wall_idx + k, wall_idx + ((k +1)%4)))    
    wall_idx += 4    
    
    pad = 8
    
    for wall in walls:
        x0,y0,x1,y1 = wall

        if x0 == x1:
            verticies += [(x0-pad, y0), (x1+pad, y0),
                      (x1+pad, y1), (x0-pad, y1)]
        else:
            verticies += [(x0, y0-pad), (x1, y0-pad),
                      (x1, y1+pad), (x0, y1+pad)]           
        
        for k in range(4):
            wall_cons.append((wall_idx + k, wall_idx + ((k +1)%4)))
        wall_idx += 4          

    
    for vx, vy in verticies:
        output_list += create_vertex(vx, vy)
    
    for id1, id2 in wall_cons:
        output_list += create_line_def(id1,id2)
        output_list += create_side_def() 
    
    output_list += create_sector()
    
    ## iterate through list to create output text file
    output_string = ''
    for output in output_list:
        output_string += output + '\n'
        
    wad.data['TEXTMAP'].data = output_string.encode()
    wad.to_file(base_filepath +filename) 
    
    
    plt.savefig(base_filepath+filename[:-4]+'.jpg')
    plt.close()
def create_maze(base_filepath, filename, size, cell_size):
    # load the base file
    BASE_WAD = 'six_item_basefile.wad'
    BASE_CFG = 'six_item_basefile.cfg'

    wad = WAD('scenarios/basefiles/' + BASE_WAD)
    cfg_filename = '{}{}.cfg'.format(base_filepath, filename[:-4])
    shutil.copy('scenarios/basefiles/' + BASE_CFG, cfg_filename)

    if '/' in filename:
        wad_filename = filename.split('/')[-1]
    else:
        wad_filename = filename
    # change the maze name in .cfg file
    # Read in the file
    with open('scenarios/basefiles/' + BASE_CFG, 'r') as file:
        filedata = file.read()

    # Replace the target string
    filedata = filedata.replace(BASE_WAD, wad_filename)

    # Write the file out again
    with open(cfg_filename, 'w') as file:
        file.write(filedata)

    verticies = []
    wall_cons = []
    wall_idx = 0
    map_point_idx = 10
    output_list = ['// Written by generate_mino_maze', 'namespace="zdoom";']

    # create the two map points

    xmin = -size // 2 * cell_size
    xmax = xmin + size * cell_size
    ymin = -size // 2 * cell_size
    ymax = ymin + size * cell_size

    item_start = 20
    num_items = 6
    locations = []

    item_tids = [2018, 2019, 2012, 2013, 13, 5]
    colors = ['g', 'r', 'b', 'c', 'm', 'y']
    details = {}

    items = []

    for i in range(num_items):

        item_i = random.randint(0, size - 1)
        item_j = random.randint(0, size - 1)

        while (item_i, item_j) in locations:
            item_i = random.randint(0, size - 1)
            item_j = random.randint(0, size - 1)

        locations.append((item_i, item_j))

    for loc, tid, idx, col in zip(locations, item_tids,
                                  range(item_start, item_start + num_items),
                                  colors):
        #print(loc, tid, idx, col)
        item_i, item_j = loc

        item_x = xmin + item_i * cell_size + cell_size / 2
        item_y = ymin + item_j * cell_size + cell_size / 2

        output_list += create_object(item_x, item_y, tid, idx)

        plt.scatter(item_x, item_y, c=col)
        items.append((item_x, item_y, col))

    spawn_i = random.randint(0, size - 1)
    spawn_j = random.randint(0, size - 1)

    while (spawn_i, spawn_j) in locations:
        #print('retry')
        spawn_i = random.randint(0, size - 1)
        spawn_j = random.randint(0, size - 1)

    spawn_x = xmin + spawn_i * cell_size + cell_size / 2
    spawn_y = ymin + spawn_j * cell_size + cell_size / 2

    output_list += create_spawn(spawn_x, spawn_y)
    plt.scatter(spawn_x, spawn_y, c='g', marker='s')

    map_point_idx += 1

    exterior, walls = gen_maze(size,
                               cell_size,
                               xmin=xmin,
                               ymin=ymin,
                               keep_prob=6)

    details['items'] = items
    details['spawn'] = (spawn_x, spawn_y)
    details['exterior'] = exterior[:-1]
    details['walls'] = walls
    with open(base_filepath + filename[:-4] + '.json', 'w') as f:
        json.dump(details, f)

    verticies += exterior[:-1]

    for k in range(4):
        wall_cons.append((wall_idx + k, wall_idx + ((k + 1) % 4)))
    wall_idx += 4

    pad = 8

    for wall in walls:
        x0, y0, x1, y1 = wall

        if x0 == x1:
            verticies += [(x0 - pad, y0), (x1 + pad, y0), (x1 + pad, y1),
                          (x0 - pad, y1)]
        else:
            verticies += [(x0, y0 - pad), (x1, y0 - pad), (x1, y1 + pad),
                          (x0, y1 + pad)]

        for k in range(4):
            wall_cons.append((wall_idx + k, wall_idx + ((k + 1) % 4)))
        wall_idx += 4

    for vx, vy in verticies:
        output_list += create_vertex(vx, vy)

    for id1, id2 in wall_cons:
        output_list += create_line_def(id1, id2)
        output_list += create_side_def()

    output_list += create_sector()

    ## iterate through list to create output text file
    output_string = ''
    for output in output_list:
        output_string += output + '\n'

    wad.data['TEXTMAP'].data = output_string.encode()
    wad.to_file(base_filepath + filename)

    plt.savefig(base_filepath + filename[:-4] + '.jpg')
    plt.close()