<s> """<STR_LIT>""" <EOL> import random <EOL> import math <EOL> from . client import ContextHandler <EOL> from . api import robotapi_pb2 <EOL> from . blocktypes import BlockType <EOL> class Robot ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , owner_name , host , port = <NUM_LIT> , context_handler = None ) : <EOL> self . host = host <EOL> self . owner_name = owner_name <EOL> self . port = port <EOL> self . _context_handler = context_handler <EOL> if not context_handler : <EOL> self . _context_handler = ContextHandler ( host , port ) <EOL> self . _counter = random . randint ( <NUM_LIT:1> , <NUM_LIT:2> ** <NUM_LIT:16> ) <EOL> def _action ( self , request ) : <EOL> """<STR_LIT>""" <EOL> response = self . _context_handler . send_request ( request ) <EOL> return response <EOL> def _new_action ( self ) : <EOL> """<STR_LIT>""" <EOL> request = robotapi_pb2 . RobotRequest ( ) <EOL> request . name = self . owner_name <EOL> self . _counter += <NUM_LIT:1> <EOL> request . key = self . _counter <EOL> return request <EOL> def move ( self , direction ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . action_request . move_direction = direction . value <EOL> return self . _action ( request ) . success <EOL> def turn ( self , direction ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . action_request . turn_direction = direction . value <EOL> return self . _action ( request ) . success <EOL> def mine ( self , direction ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . action_request . mine_direction = direction . value <EOL> return self . _action ( request ) . success <EOL> def place ( self , direction , blocktype ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . action_request . place_direction = direction . value <EOL> request . action_request . place_material . type = blocktype . value <EOL> return self . _action ( request ) . success <EOL> def get_block_type ( self , direction ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . read_request . identify_material . direction = direction . value <EOL> material_id = self . _action ( request ) . material_response . type <EOL> if material_id in BlockType . value_map : <EOL> return BlockType . value_map [ material_id ] <EOL> logging . warn ( "<STR_LIT>" , material_id ) <EOL> return None <EOL> def is_block_solid ( self , direction ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . read_request . is_solid . direction = direction . value <EOL> return self . _action ( request ) . boolean_response <EOL> def _locate ( self , entity ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . read_request . locate_entity = entity <EOL> loc_proto = self . _action ( request ) . location_response . locations [ <NUM_LIT:0> ] <EOL> return Location . from_proto ( loc_proto . absolute_location ) <EOL> def get_location ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _locate ( robotapi_pb2 . RobotReadRequest . SELF ) <EOL> def get_owner_location ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _locate ( robotapi_pb2 . RobotReadRequest . OWNER ) <EOL> def find_type_nearby ( self , blocktype ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . read_request . locate_material_nearby . type = blocktype . value <EOL> loc_proto_list = ( <EOL> self . _action ( request ) . location_response . locations ) <EOL> loc_list = [ <EOL> Location . from_proto ( l . absolute_location ) for l in loc_proto_list ] <EOL> return loc_list <EOL> def find_path ( self , target_location ) : <EOL> """<STR_LIT>""" <EOL> my_loc = self . get_location ( ) <EOL> request = self . _new_action ( ) <EOL> request . read_request . locate_nonsolid_nearby = True <EOL> loc_proto_list = self . _action ( request ) . location_response . locations <EOL> loc_list = [ <EOL> Location . from_proto ( l . absolute_location ) for l in loc_proto_list ] <EOL> best = None <EOL> targetdist = target_location . distance ( loc_list [ <NUM_LIT:0> ] ) + <NUM_LIT:20> <EOL> for loc in loc_list : <EOL> newdist = target_location . distance ( loc ) <EOL> if newdist < targetdist and my_loc . distance ( loc ) == <NUM_LIT:1> : <EOL> best = loc <EOL> targetdist = newdist <EOL> return my_loc . direction ( best ) <EOL> def get_inventory ( self ) : <EOL> """<STR_LIT>""" <EOL> request = self . _new_action ( ) <EOL> request . read_request . get_inventory = True <EOL> inv = self . _action ( request ) . inventory_response <EOL> return [ <EOL> ( self . _material_to_block ( mat ) , count ) <EOL> for mat , count in zip ( inv . materials , inv . counts ) ] <EOL> def _material_to_block ( self , material ) : <EOL> if material . type in BlockType . value_map : <EOL> return BlockType . value_map [ material . type ] <EOL> return None <EOL> def message_owner ( self , msg ) : <EOL> request = self . _new_action ( ) <EOL> request . action_request . chat_message = msg <EOL> request . action_request . is_public_message = False <EOL> return self . _action ( request ) . success <EOL> def message_all ( self , msg ) : <EOL> request = self . _new_action ( ) <EOL> request . action_request . chat_message = msg <EOL> request . action_request . is_public_message = True <EOL> return self . _action ( request ) . success <EOL> class Location ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def from_proto ( cls , location_proto ) : <EOL> """<STR_LIT>""" <EOL> return Location ( location_proto . x , location_proto . y , location_proto . z ) <EOL> def __init__ ( self , x_coord , y_coord , z_coord ) : <EOL> self . x_coord = x_coord <EOL> self . y_coord = y_coord <EOL> self . z_coord = z_coord <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> self . x_coord , self . y_coord , self . z_coord ) <EOL> def __eq__ ( self , other ) : <EOL> if not other : <EOL> return False <EOL> return ( self . x_coord == other . x_coord and <EOL> self . y_coord == other . y_coord and <EOL> self . z_coord == other . z_coord ) <EOL> def distance ( self , other ) : <EOL> """<STR_LIT>""" <EOL> return math . sqrt ( <EOL> ( self . x_coord - other . x_coord ) ** <NUM_LIT:2> + <EOL> ( self . y_coord - other . y_coord ) ** <NUM_LIT:2> + <EOL> ( self . z_coord - other . z_coord ) ** <NUM_LIT:2> ) <EOL> def direction ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if other == None : <EOL> return None <EOL> loc = [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] <EOL> loc [ <NUM_LIT:0> ] = other . x_coord - self . x_coord <EOL> loc [ <NUM_LIT:1> ] = other . y_coord - self . y_coord <EOL> loc [ <NUM_LIT:2> ] = other . z_coord - self . z_coord <EOL> max_value = max ( list ( map ( abs , loc ) ) ) <EOL> max_direction = <NUM_LIT:0> <EOL> if max_value in loc : <EOL> max_direction = loc . index ( max_value ) <EOL> else : <EOL> max_direction = loc . index ( - <NUM_LIT:1> * max_value ) <EOL> if max_direction == <NUM_LIT:1> : <EOL> if loc [ <NUM_LIT:1> ] > <NUM_LIT:0> : <EOL> return Dir . UP <EOL> return Dir . DOWN <EOL> if max_direction == <NUM_LIT:0> : <EOL> if loc [ <NUM_LIT:0> ] > <NUM_LIT:0> : <EOL> return Dir . EAST <EOL> return Dir . WEST <EOL> if loc [ <NUM_LIT:2> ] > <NUM_LIT:0> : <EOL> return Dir . SOUTH <EOL> return Dir . NORTH <EOL> class Dir : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , value ) : <EOL> self . value = value <EOL> self . name = name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . name , self . value ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def __eq__ ( self , other ) : <EOL> if not other : <EOL> return False <EOL> return self . value == other . value <EOL> def setup_dir ( ) : <EOL> """<STR_LIT>""" <EOL> value_map = { } <EOL> for attr , value in robotapi_pb2 . WorldLocation . __dict__ . items ( ) : <EOL> if attr . isupper ( ) and type ( value ) == int : <EOL> dir_obj = Dir ( attr , value ) <EOL> setattr ( Dir , attr , dir_obj ) <EOL> value_map [ value ] = dir_obj <EOL> Dir . value_map = value_map <EOL> setup_dir ( ) </s>
