<s> __author__ = '<STR_LIT>' <EOL> from session import Session <EOL> from buffer import Buffer <EOL> from cell import Cell <EOL> import random <EOL> import time <EOL> import math <EOL> class Bot ( object ) : <EOL> def __init__ ( self , game ) : <EOL> self . game = game <EOL> self . session = Session ( ) <EOL> self . buffer = Buffer ( ) <EOL> self . name = '<STR_LIT>' <EOL> self . last_x = <NUM_LIT:0> <EOL> self . last_y = <NUM_LIT:0> <EOL> self . view_x = <NUM_LIT:0> <EOL> self . view_y = <NUM_LIT:0> <EOL> self . view_w = <NUM_LIT:0> <EOL> self . view_h = <NUM_LIT:0> <EOL> self . has_sent_init = False <EOL> self . last_sent_spawn = <NUM_LIT:0> <EOL> self . last_update = <NUM_LIT:0> <EOL> self . ids = [ ] <EOL> self . ladder = [ ] <EOL> self . mode = '<STR_LIT>' <EOL> def connect ( self , host , port ) : <EOL> if not self . is_connected ( ) and ( time . time ( ) - self . game . last_connect > <NUM_LIT:15> ) : <EOL> if self . session . connect ( host , port ) : <EOL> print ( '<STR_LIT:[>' + self . name + '<STR_LIT>' ) <EOL> self . last_x = <NUM_LIT:0> <EOL> self . last_y = <NUM_LIT:0> <EOL> self . view_x = <NUM_LIT:0> <EOL> self . view_y = <NUM_LIT:0> <EOL> self . view_w = <NUM_LIT:0> <EOL> self . view_h = <NUM_LIT:0> <EOL> self . game . last_connect = time . time ( ) <EOL> self . has_sent_init = False <EOL> self . last_sent_spawn = <NUM_LIT:0> <EOL> self . ids = [ ] <EOL> self . ladder = { } <EOL> self . send_init ( ) <EOL> self . send_spawn ( ) <EOL> self . send_move_relative ( <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . n_updates = <NUM_LIT:0> <EOL> return True <EOL> print ( '<STR_LIT:[>' + self . name + '<STR_LIT>' ) <EOL> return False <EOL> def disconnect ( self ) : <EOL> if self . is_connected ( ) : <EOL> self . session . disconnect ( ) <EOL> for cell in self . game . cells . values ( ) : <EOL> cell . remove_watcher ( self ) <EOL> for id in self . ids : <EOL> self . game . remove_id ( id ) <EOL> if self . has_id ( id ) : <EOL> self . remove_id ( id ) <EOL> self . game . remove_id ( id ) <EOL> return True <EOL> return False <EOL> def update ( self ) : <EOL> if not self . is_connected ( ) : <EOL> self . connect ( self . game . host , self . game . port ) <EOL> return False <EOL> if not self . is_alive ( ) : <EOL> self . send_spawn ( ) <EOL> all = [ ] <EOL> all . extend ( self . session . inbound ) <EOL> self . session . inbound = self . session . inbound [ len ( all ) : ] <EOL> if ( len ( all ) != <NUM_LIT:0> ) : <EOL> self . n_updates += <NUM_LIT:1> <EOL> for data in all : <EOL> self . buffer . fill ( data ) <EOL> packet = self . buffer . read_byte ( ) <EOL> self . parse_packet ( packet ) <EOL> if not self . last_update == self . game . timestamp : <EOL> return <EOL> return True <EOL> def act ( self ) : <EOL> pass <EOL> def parse_packet ( self , id ) : <EOL> b = self . buffer <EOL> if id == <NUM_LIT:16> : <EOL> self . last_update = self . game . timestamp <EOL> self . parse_mergers ( ) <EOL> self . parse_updates ( ) <EOL> self . parse_deaths ( ) <EOL> elif id == <NUM_LIT> : <EOL> x = b . read_float ( ) <EOL> y = b . read_float ( ) <EOL> ratio = b . read_float ( ) <EOL> print ( '<STR_LIT>' , x , y , ratio ) <EOL> elif id == <NUM_LIT:20> : <EOL> for id in self . ids : <EOL> self . game . remove_id ( id ) <EOL> self . ids = [ ] <EOL> print ( '<STR_LIT>' ) <EOL> elif id == <NUM_LIT:32> : <EOL> id = b . read_int ( ) <EOL> self . add_id ( id ) <EOL> self . game . add_id ( id ) <EOL> print ( '<STR_LIT>' , id ) <EOL> elif id == <NUM_LIT> : <EOL> self . ladder = { } <EOL> self . mode = '<STR_LIT>' <EOL> amount = b . read_int ( ) <EOL> for i in range ( <NUM_LIT:0> , amount ) : <EOL> id = b . read_int ( ) <EOL> self . ladder [ id ] = b . read_string ( ) <EOL> self . game . ladder = self . ladder . copy ( ) <EOL> self . game . mode = '<STR_LIT>' <EOL> elif id == <NUM_LIT:50> : <EOL> self . ladder = [ ] <EOL> count = b . read_int ( ) <EOL> for i in range ( <NUM_LIT:0> , count ) : <EOL> self . ladder . append ( b . read_float ( ) ) <EOL> if len ( self . game . ladder ) == <NUM_LIT:0> : <EOL> self . game . ladder = self . ladder . copy ( ) <EOL> self . game . mode = '<STR_LIT>' <EOL> elif id == <NUM_LIT:64> : <EOL> self . game . view_x = self . view_x = b . read_double ( ) <EOL> self . game . view_y = self . view_y = b . read_double ( ) <EOL> self . game . view_w = self . view_w = b . read_double ( ) <EOL> self . game . view_h = self . view_h = b . read_double ( ) <EOL> print ( '<STR_LIT>' , self . view_x , self . view_y , self . view_w , self . view_h ) <EOL> def parse_mergers ( self ) : <EOL> amount = self . buffer . read_short ( ) <EOL> for i in range ( <NUM_LIT:0> , amount ) : <EOL> hunter , prey = self . buffer . read_int ( ) , self . buffer . read_int ( ) <EOL> if self . game . has_id ( hunter ) and self . game . has_id ( prey ) : <EOL> cell = self . game . get_cell ( prey ) <EOL> self . game . remove_cell ( prey ) <EOL> if self . has_id ( cell . id ) : <EOL> self . remove_id ( cell . id ) <EOL> self . game . remove_id ( cell . id ) <EOL> print ( '<STR_LIT>' % ( hunter , prey ) ) <EOL> def parse_updates ( self ) : <EOL> b = self . buffer <EOL> current_time = time . time ( ) <EOL> while True : <EOL> id = b . read_int ( ) <EOL> if id == <NUM_LIT:0> : <EOL> break <EOL> x = b . read_short ( ) <EOL> y = b . read_short ( ) <EOL> size = b . read_short ( ) <EOL> red = b . read_byte ( ) <EOL> green = b . read_byte ( ) <EOL> blue = b . read_byte ( ) <EOL> color = ( red , green , blue ) <EOL> byte = b . read_byte ( ) <EOL> virus = ( byte & <NUM_LIT:1> ) <EOL> agitated = ( byte & <NUM_LIT:16> ) <EOL> if ( byte & <NUM_LIT:2> ) : <EOL> b . skip ( <NUM_LIT:4> ) <EOL> elif ( byte & <NUM_LIT:4> ) : <EOL> b . skip ( <NUM_LIT:8> ) <EOL> elif ( byte & <NUM_LIT:8> ) : <EOL> b . skip ( <NUM_LIT:16> ) <EOL> name = b . read_string ( ) <EOL> if self . game . has_cell ( id ) : <EOL> cell = self . game . get_cell ( id ) <EOL> t = current_time - cell . last_update <EOL> if ( t > <NUM_LIT:0.0> ) : <EOL> vx = ( float ( x ) - float ( cell . x ) ) / t <EOL> vy = ( float ( y ) - float ( cell . y ) ) / t <EOL> cell . vx = ( vx + cell . vx ) / <NUM_LIT> <EOL> cell . vy = ( vy + cell . vy ) / <NUM_LIT> <EOL> v = math . sqrt ( cell . vx * cell . vx + cell . vy * cell . vy ) <EOL> max_velocity = <NUM_LIT> <EOL> if v > max_velocity : <EOL> cell . vx *= ( max_velocity / v ) <EOL> cell . vy *= ( max_velocity / v ) <EOL> cell . x = x <EOL> cell . y = y <EOL> else : <EOL> cell . x += cell . vx * t <EOL> cell . y += cell . vy * t <EOL> cell . interpolated_x = cell . x <EOL> cell . interpolated_y = cell . y <EOL> cell . last_update = current_time <EOL> cell . size = size <EOL> cell . color = color <EOL> cell . virus = virus <EOL> cell . agitated = agitated <EOL> cell . timestamp = self . game . timestamp <EOL> else : <EOL> cell = Cell ( id , x , y , size , color , virus , agitated , name ) <EOL> cell . watchers . append ( self ) <EOL> cell . timestamp = self . game . timestamp <EOL> if self . has_id ( id ) : <EOL> cell . owner = self <EOL> self . game . add_cell ( cell ) <EOL> def parse_deaths ( self ) : <EOL> amount = self . buffer . read_int ( ) <EOL> for i in range ( <NUM_LIT:0> , amount ) : <EOL> id = self . buffer . read_int ( ) <EOL> if self . has_id ( id ) : <EOL> self . remove_id ( id ) <EOL> self . game . remove_id ( id ) <EOL> if len ( self . ids ) == <NUM_LIT:0> : <EOL> self . send_spawn ( ) <EOL> print ( "<STR_LIT>" ) <EOL> if self . game . has_cell ( id ) : <EOL> cell = self . game . get_cell ( id ) <EOL> cell . remove_watcher ( self ) <EOL> self . game . remove_cell ( id ) <EOL> def send_init ( self ) : <EOL> if self . is_connected ( ) and not self . has_sent_init : <EOL> self . has_sent_init = True <EOL> self . buffer . write_byte ( <NUM_LIT> ) <EOL> self . buffer . write_int ( <NUM_LIT:4> ) <EOL> self . buffer . flush_session ( self . session ) <EOL> self . buffer . write_byte ( <NUM_LIT:255> ) <EOL> self . buffer . write_int ( <NUM_LIT:1> ) <EOL> self . buffer . flush_session ( self . session ) <EOL> return True <EOL> return False <EOL> def send_spawn ( self ) : <EOL> if self . is_connected ( ) and ( time . time ( ) - self . last_sent_spawn > <NUM_LIT:4> ) : <EOL> for cell in self . game . cells . values ( ) : <EOL> cell . remove_watcher ( self ) <EOL> self . last_sent_spawn = time . time ( ) <EOL> self . buffer . write_string ( self . name ) <EOL> self . buffer . flush_session ( self . session ) <EOL> return True <EOL> return False <EOL> def send_move ( self , x , y ) : <EOL> if self . is_connected ( ) and self . is_alive ( ) : <EOL> if not ( self . last_x == x and self . last_y == y ) : <EOL> self . last_x = x <EOL> self . last_y = y <EOL> self . buffer . write_byte ( <NUM_LIT:16> ) <EOL> self . buffer . write_double ( x ) <EOL> self . buffer . write_double ( y ) <EOL> self . buffer . write_int ( <NUM_LIT:0> ) <EOL> self . buffer . flush_session ( self . session ) <EOL> return True <EOL> return False <EOL> def send_move_relative ( self , rel_x , rel_y ) : <EOL> x , y = self . get_center ( ) <EOL> x += rel_x <EOL> y += rel_y <EOL> return self . send_move ( x , y ) <EOL> def send_split ( self , times = <NUM_LIT:1> ) : <EOL> if self . is_connected ( ) and self . is_alive ( ) : <EOL> for i in range ( <NUM_LIT:0> , times ) : <EOL> self . buffer . write_byte ( <NUM_LIT> ) <EOL> self . buffer . flush_session ( self . session ) <EOL> return True <EOL> return False <EOL> def send_throw ( self , times = <NUM_LIT:1> ) : <EOL> if self . is_connected ( ) and self . is_alive ( ) : <EOL> for i in range ( <NUM_LIT:0> , times ) : <EOL> self . buffer . write_byte ( <NUM_LIT> ) <EOL> self . buffer . flush_session ( self . session ) <EOL> return True <EOL> return False <EOL> def send_spectate ( self ) : <EOL> if self . is_connected ( ) : <EOL> self . buffer . write_byte ( <NUM_LIT:1> ) <EOL> self . buffer . flush_session ( self . session ) <EOL> return True <EOL> return False <EOL> def get_center ( self ) : <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> amount = <NUM_LIT:0> <EOL> for id in self . ids : <EOL> cell = self . game . get_cell ( id ) <EOL> if cell : <EOL> x += cell . x <EOL> y += cell . y <EOL> amount += <NUM_LIT:1> <EOL> amount = max ( <NUM_LIT:1> , amount ) <EOL> return x / amount , y / amount <EOL> def get_interpolated_center ( self , current_time ) : <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> amount = <NUM_LIT:0> <EOL> for id in self . ids : <EOL> cell = self . game . get_cell ( id ) <EOL> if cell : <EOL> x += cell . interpolated_x <EOL> y += cell . interpolated_y <EOL> amount += <NUM_LIT:1> <EOL> amount = max ( <NUM_LIT:1> , amount ) <EOL> return x / float ( amount ) , y / float ( amount ) <EOL> def get_mass ( self ) : <EOL> mass = <NUM_LIT:0> <EOL> for id in self . ids : <EOL> cell = self . game . get_cell ( id ) <EOL> if cell : <EOL> mass += cell . size <EOL> return mass <EOL> def is_alive ( self ) : <EOL> return len ( self . ids ) > <NUM_LIT:0> <EOL> def is_connected ( self ) : <EOL> return self . session . is_connected ( ) <EOL> def add_id ( self , id ) : <EOL> if not self . has_id ( id ) : <EOL> self . ids . append ( id ) <EOL> return True <EOL> return False <EOL> def remove_id ( self , id ) : <EOL> if self . has_id ( id ) : <EOL> self . ids . remove ( id ) <EOL> return True <EOL> return False <EOL> def has_id ( self , id ) : <EOL> return id in self . ids </s>
