def do_action(ship, action, game_map, me): if action == 0: #MOVE NORTH new_position = game_map.normalize(Position(ship.position.x, ship.position.y - 1)) simulation_move(ship, new_position, game_map, me) elif action == 1: #MOVE SOUTH new_position = game_map.normalize(Position(ship.position.x, ship.position.y + 1)) simulation_move(ship, new_position, game_map, me) elif action == 2: #MOVE EAST new_position = game_map.normalize(Position(ship.position.x + 1, ship.position.y)) simulation_move(ship, new_position, game_map, me) elif action == 3: #MOVE WEST new_position = game_map.normalize(Position(ship.position.x - 1, ship.position.y)) simulation_move(ship, new_position, game_map, me) elif action == 4: #STAY STILL cell = game_map[ship] gain = int(ceil(0.25*cell.halite_amount)) mem_halite_amount = ship.halite_amount ship.is_inspired = maj_inspired(ship, game_map, me) if ship.is_inspired == 1: ship.halite_amount = min(1000, ship.halite_amount + 3*gain) cell.halite_amount -= (ship.halite_amount - mem_halite_amount)//3 else: ship.halite_amount = min(1000, ship.halite_amount + gain) cell.halite_amount -= (ship.halite_amount - mem_halite_amount) futur_position.add(ship.position) else: #TURN INTO DROPOFF cell = game_map[ship] new_dropoff = Dropoff(me.id, constants.OWN_ID, Position(ship.position.x, ship.position.y)) cell.structure = new_dropoff cell.ship = None cell.halite_amount = 0 me._dropoffs[constants.OWN_ID] = new_dropoff constants.OWN_ID += 1 me._ships.pop(ship.id)
def parse_replay_file(file_name): print("Load Replay: " + file_name) with open(file_name, 'rb') as f: data = json.loads(zstd.loads(f.read())) print("Load Basic Information") player_id = find_player_data(data) player = [p for p in data['players'] if p['player_id'] == player_id] player = player[0] my_shipyard = Shipyard( player_id, ARBITRARY_ID, hlt.Position(player['factory_location']['x'], player['factory_location']['y'])) other_shipyards = [ Shipyard( p['player_id'], ARBITRARY_ID, hlt.Position(p['factory_location']['x'], p['factory_location']['y'])) for p in data['players'] if int(p['player_id']) != player_id ] width = data['production_map']['width'] height = data['production_map']['height'] print("Load Cell Information") first_cells = [] for x in range(len(data['production_map']['grid'])): row = [] for y in range(len(data['production_map']['grid'][x])): row += [ MapCell(hlt.Position(x, y), data['production_map']['grid'][x][y]['energy']) ] first_cells.append(row) frames = [] for f in data['full_frames']: prev_cells = first_cells if len(frames) == 0 else frames[-1]._cells new_cells = copy.deepcopy(prev_cells) for c in f['cells']: new_cells[c['y']][c['x']].halite_amount = c['production'] frames.append(GameMap(new_cells, width, height)) print("Load Player Ships") moves = [{} if str(player_id) not in f['moves'] else { m['id']: m['direction'] for m in f['moves'][str(player_id)] if m['type'] == "m" } for f in data['full_frames']] ships = [{} if str(player_id) not in f['entities'] else { int(sid): Ship(player_id, int(sid), hlt.Position(ship['x'], ship['y']), ship['energy']) for sid, ship in f['entities'][str(player_id)].items() } for f in data['full_frames']] print("Load Other Player Ships") other_ships = [{ int(sid): Ship(int(pid), int(sid), hlt.Position(ship['x'], ship['y']), ship['energy']) for pid, p in f['entities'].items() if int(pid) != player_id for sid, ship in p.items() } for f in data['full_frames']] print("Load Droppoff Information") first_my_dropoffs = [my_shipyard] first_them_dropoffs = other_shipyards my_dropoffs = [] them_dropoffs = [] for f in data['full_frames']: new_my_dropoffs = copy.deepcopy( first_my_dropoffs if len(my_dropoffs) == 0 else my_dropoffs[-1]) new_them_dropoffs = copy.deepcopy(first_them_dropoffs if len( them_dropoffs) == 0 else them_dropoffs[-1]) for e in f['events']: if e['type'] == 'construct': if int(e['owner_id']) == player_id: new_my_dropoffs.append( Dropoff( player_id, ARBITRARY_ID, hlt.Position(e['location']['x'], e['location']['y']))) else: new_them_dropoffs.append( Dropoff( e['owner_id'], ARBITRARY_ID, hlt.Position(e['location']['x'], e['location']['y']))) my_dropoffs.append(new_my_dropoffs) them_dropoffs.append(new_them_dropoffs) return list( zip(frames, moves, ships, other_ships, my_dropoffs, them_dropoffs))