Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Python by davon ( 5 years ago )
from game_state import GameState
import asyncio
import random
import os
import helpers   # import our helper functions

# docker-compose down
# docker-compose build
# docker-compose up --abort-on-container-exit

# docker-compose up --abort-on-container-exit

# https://codeshare.io/G66wmo
# https://codeshare.io/5edpML

uri = os.environ.get(
    'GAME_CONNECTION_STRING') or "ws://127.0.0.1:3000/?role=agent&agentId=agentId&name=myAgent"

actions = ["up", "down", "left", "right"]

class Agent():
    def __init__(self):
        self._client = GameState(uri)

        self._client.set_game_tick_callback(self._on_game_tick)
        loop = asyncio.get_event_loop()
        connection = loop.run_until_complete(self._client.connect())
        tasks = [
            asyncio.ensure_future(self._client._handle_messages(connection)),
        ]
        loop.run_until_complete(asyncio.wait(tasks))

    def _get_bomb_to_detonate(self, game_state) -> [int, int] or None:
        agent_number = game_state.get("connection").get("agent_number")
        entities = self._client._state.get("entities")
        bombs = list(filter(lambda entity: entity.get(
            "owner") == agent_number and entity.get("type") == "b", entities))
        bomb = next(iter(bombs or []), None)
        if bomb != None:
            return [bomb.get("x"), bomb.get("y")]
        else:
            return None

    async def _on_game_tick(self, tick_number, game_state):

        ########################
        ###    VARIABLES     ###
        ########################
        width = game_state["world"]["width"]      # returns 9
        height = game_state["world"]["height"]    # returns 9
        my_id = str(game_state["connection"]["agent_number"])

        if my_id == '0':
            enemy_id = '1' 
        else:
            enemy_id = '0'

        my_location = game_state["agent_state"][my_id]["coordinates"] 
        my_ammo = game_state["agent_state"][my_id]["inventory"]["bombs"] 
        my_bomb_range = game_state["agent_state"][my_id]["blast_diameter"]
        my_health = game_state["agent_state"][my_id]["hp"]
        my_invulnerability = game_state["agent_state"][my_id]["invulnerability"]
        
        enemy_location = game_state["agent_state"][enemy_id]["coordinates"] 
        enemy_ammo = game_state["agent_state"][enemy_id]["inventory"]["bombs"] 
        enemy_bomb_range = game_state["agent_state"][enemy_id]["blast_diameter"]
        enemy_health = game_state["agent_state"][enemy_id]["hp"]
        enemy_invulnerability = game_state["agent_state"][enemy_id]["invulnerability"]

        print("My blast: ", my_bomb_range, " Enemy blast: ", enemy_bomb_range)
        ########################
        ###      AGENT       ###
        ########################
        start = (my_location[1], my_location[0])
        # get a list of bombs on the map
        bombs = helpers.get_bombs(game_state, enemy_location, my_location)
        print("Bombs:", bombs[0])
        print("B Id :", bombs[1])
        # check if we're within range of a bomb
        # get list of bombs within range
        bombs_in_range = helpers.get_bombs_in_range(my_location,bombs[0])

        # get impacted tiles
        impacted_tiles = helpers.get_impacted_tiles(bombs, my_bomb_range, enemy_bomb_range, game_state)
        print("Impacted tiles:", impacted_tiles)
        # get our surrounding tiles
        surrounding_tiles = helpers.get_surrounding_tiles(my_location)

        # get list of empty tiles around us
        empty_tiles = helpers.get_empty_tiles(surrounding_tiles,game_state)

        # get game map by list for a star
        game_map = helpers.get_map(width,height,game_state, enemy_location)

        list_ammo=helpers.get_ammo(my_location,game_state)
        
        ammo_location=[]
        for ammo in temp_ammo:
            ammo_location.append((ammo[2],ammo[3]))
            
        list_bp=helpers.get_bp(my_location,game_state)
        
        bp_location=[]
        for bp in temp_bp:
            bp_location.append((bp[2],bp[3]))
            
                list_ammo_bp=list_ammo+list_bp
            list_ammo_bp.sort()
        
        for i in range(height):
            print(game_map[i])
        
        list_ammo_bp =  list_ammo + list_bp
        print("My location:", start)
        
        # if I'm on a bomb
        # I should probably move
        if helpers.entity_at(my_location[0],my_location[1],game_state) == 'b':
            if empty_tiles:
                safest_tile = helpers.get_future_safest_tile(empty_tiles, bombs[0], game_state) 
                action = helpers.move_to_tile(start,(safest_tile[1], safest_tile[0]))
            else:
                # if there isn't a free spot to move to, we're probably stuck here
                action = ''
                
        # If I'm in impacted tiles
        elif my_location in helpers.get_impacted_tiles(bombs, my_bomb_range, enemy_bomb_range, game_state):
            if len(bp_location) is not None or len(ammo_location) is not None:
                action=helpers.goto_pb_and_ammo(game_map, start, list_ammo_bp, empty_tiles, bombs, game_state)
            else:
                safest_tile = helpers.get_future_safest_tile(empty_tiles, bombs[0], game_state) 
                if my_location != safest_tile:
                    action = helpers.move_to_tile(start,(safest_tile[1], safest_tile[0]))
                else:
                    # if we are in the safest tile we stop moving
                    action = ''
                # available_bp = helpers.get_available_bp(start, game_map, game_state)
                # if available_bp is not None:
                #     action = helpers.move_to_tile(my_location, available_ammo[0][1])
                # else:
                #     if available_ammo is not None:
                #         action = helpers.move_to_tile(my_location, available_ammo[0][1]) 
                #     else:
                        
        # if there are no bombs in range and there is a power up or ammo in map 
        else:
            action=helpers.goto_pb_and_ammo(game_map, start, list_ammo_bp, empty_tiles, bombs, game_state)

        # logic to send valid action packet to game server
        if action in ["up", "left", "right", "down"]:
            await self._client.send_move(action)
        elif action == "bomb":
            await self._client.send_bomb()
        elif action == "detonate":
            bomb_coordinates = self._get_bomb_to_detonate(game_state)
            if bomb_coordinates != None:
                x, y = bomb_coordinates
                await self._client.send_detonate(x, y)
        else:
            print(f"Unhandled action: {action}")


def main():
    Agent()


if __name__ == "__main__":
    main()

 

Revise this Paste

Your Name: Code Language: