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.previous_safest = []
        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)

        safest_tile = helpers.get_future_safest_tile(empty_tiles, impacted_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 list_ammo:
            ammo_location.append((ammo[2],ammo[3]))
            
        list_bp=helpers.get_bp(my_location,game_state)
        
        bp_location=[]
        for bp in list_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)
        print("Enemy Location:", (enemy_location[1], enemy_location[0]))
        
        # if I'm on a bomb
        # I should probably move
        if helpers.entity_at(my_location[0],my_location[1],game_state) == 'b':
            if my_location != self.previous_safest:
                safest_tile = helpers.get_future_safest_tile(empty_tiles, impacted_tiles, game_state) 
                previous_safest = 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 = ''
                
        # If I'm in impacted tiles
        elif start in impacted_tiles:
            print("bahaya woi")
            if len(bp_location) != 0 or len(ammo_location) != 0:
                available_ammo_bp = helpers.get_available_ammo_bp(start, game_map, game_state)
                if len(bp_location) != 0 :
                    if available_ammo_bp[0][len(available_ammo_bp[0])-1] not in impacted_tiles:
                        action=helpers.goto_bp_and_ammo(game_map, start,enemy_location, impacted_tiles, game_state)
                    else:
                        if my_location != self.previous_safest:
                            safest_tile = helpers.get_future_safest_tile(empty_tiles, impacted_tiles, game_state) 
                            previous_safest = 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 = ''
                else:
                    if my_location != self.previous_safest:
                        safest_tile = helpers.get_future_safest_tile(empty_tiles, impacted_tiles, game_state) 
                        previous_safest = 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 = ''
            else:
                if my_location != self.previous_safest:
                    safest_tile = helpers.get_future_safest_tile(empty_tiles, impacted_tiles, game_state) 
                    previous_safest = 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 = ''
                        
        # if we're not in bomb range and ammo >= 3, we'll follow enemy movement
        elif my_ammo >= 10:
            # check enemy's surrounding tiles
            enemy_surrounding_tiles = helpers.get_surrounding_tiles(enemy_location)
            empty_tiles_around_enemy = helpers.get_empty_tiles(enemy_surrounding_tiles, game_state)
            minim=10**9+7
            end=(-1,-1)
            for empty in empty_tiles_around_enemy:
                temp=helpers.manhattan_distance(my_location,empty)
                if minim>temp:
                    minim=temp
                    end=(empty[1],empty[0])
            if end !=(-1,1):
                path=helpers.astar(game_map,start, end)
            print(path)
            # action=''
            if path is None:
                action=helpers.goto_bp_and_ammo(game_map, start,enemy_location, impacted_tiles, game_state)
            else:
                if  len(path)<=1:
                    print("len = ",len(empty_tiles_around_enemy))
                    if len(empty_tiles_around_enemy) == 1:
                        print("bomb")
                        action = "bomb"
                    else:
                        action=''
                else:
                    action = helpers.move_to_tile(start, path[1])
        # if there are no bombs in range and there is a power up in map 
        else:
            action=helpers.goto_bp_and_ammo(game_map, start,enemy_location, impacted_tiles, 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: