def __init__(self, name, bullet1_cls, bullet2_cls, ship_type=None): self.ship_type = ship_type self.tilt_angle = 0 self.weight = 1 self.health = 100 self.immortality = int(3 / app.constants.FRAME_INTERVAL) self.engine = Engine() self.damage = 0 self.kills = 0 self.name = name[:15] or 'Anonymous' self._acc = Vector2D(x=0, y=0) self.dead_step = -1 # dead_step # -1 : life # 0 : in dive # >0 impolisum and fire # 0..5 implosiom # >80 close socket self.pk = None self.candidat_position = Vector2D(x=0, y=0) self.current_speed = Vector2D(x=0, y=0) self.current_route = _pi_2 self.control = _Control() self.a_sum = Vector2D(x=0, y=0) self.demensions_info = [(p.length, p.angle() - _pi_2) for p in self.demensions] self.demensions_max_radius = max(d.y for d in self.demensions) while True: self.candidat_position = Vector2D(x=random.random() * MAP_SIZE[0], y=random.random() * MAP_SIZE[1]) if not get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y): self.current_position = self.candidat_position self.candidat_position = Vector2D(x=0, y=0) break self.current_polygon = [ Vector2D( length=p_length, angle=p_angle - self.current_route, ) + self.current_position for p_length, p_angle in self.demensions_info ] self.current_polygon_info = get_polygon_info(self.current_polygon) self.gun1 = Gun(**bullet1_cls(self).gun_kwargs) self.bullet1_cls = bullet1_cls self.gun2 = Gun(**bullet2_cls(self).gun_kwargs) self.bullet2_cls = bullet2_cls from app.castomization import SmokeBullet self.smoke_cls = SmokeBullet self.smoke_interval = _smoke_interval self.lifetime = None self.starttime = None self.dead_reason = None
def __init__(self, ws=None, ship=None, is_assigned_to_room=False): self.is_assigned_to_room = is_assigned_to_room self.last_use = now() self.pk = uuid.uuid4().hex[0:8] self.ws = ws self.ship = ship self.centor_vector = Vector2D(x=VIEWPORT_SIZE[0] / 2, y=VIEWPORT_SIZE[1] / 2) self.background_position = Vector2D(x=0, y=0) self.sum_damage = 0 self._observer_position = Vector2D( x=random.randint(int(VIEWPORT_SIZE[0] / 2), int(MAP_SIZE[0] - VIEWPORT_SIZE[0] / 2)), y=random.randint(int(VIEWPORT_SIZE[1] / 2), int(MAP_SIZE[1] - VIEWPORT_SIZE[1] / 2)), ) self._observer_position_delta = [ [1, -1], [0.5, -0.5], ] self.extra_viewport = None
def get_directions_correction(my_obj, obj, prev_point=None): angle_to_obj = Vector2D( x=obj['x'] - my_obj['x'], y=my_obj['y'] - obj['y'], ).angle() if prev_point: if (int(my_obj['x']) == int(prev_point['x']) and int(my_obj['y']) == int(prev_point['y'])): print('Stop %s' % random.random()) # angle_speed = Vector2D( # x=my_obj['x'] - prev_point['x'], # y=prev_point['y'] - my_obj['y'] # ).angle() # if angle_speed > math.pi: # angle_speed -= (math.pi * 2) # if angle_speed < -math.pi: # angle_speed += (math.pi * 2) if my_obj['angle'] > math.pi: my_obj['angle'] -= (math.pi * 2) if my_obj['angle'] < -math.pi: my_obj['angle'] += (math.pi * 2) if angle_to_obj > math.pi: angle_to_obj -= (math.pi * 2) if angle_to_obj < -math.pi: angle_to_obj += (math.pi * 2) angle_diff = angle_to_obj - my_obj['angle'] # if prev_point and my_obj['angle'] and abs(angle_speed-my_obj['angle'])/my_obj['angle'] < math.pi / 2: # angle_diff = angle_to_obj - angle_speed*0.2 - my_obj['angle'] # else: abs_angle_diff = abs(angle_diff) if not (15 <= math.degrees(abs_angle_diff) <= 360 - 15): return '0', abs_angle_diff if angle_diff < math.pi / 180 * 3: if abs_angle_diff <= math.pi: return '1', abs_angle_diff else: return '-1', abs_angle_diff elif angle_diff > math.pi / 180 * 3: if abs_angle_diff <= math.pi: return '-1', abs_angle_diff else: return '1', abs_angle_diff
def calculate_position(self): FI = app.constants.FRAME_INTERVAL self.life_limit -= FI if self.life_limit < 0: self.life_limit = -1000 # selfdestroy return if not self.current_speed: return a_sum = self.current_speed * (-MU) self.current_speed += a_sum * FI sum_vector = self.current_speed * FI + (a_sum / 2) * FI**2 self.candidat_position.reinit(self.current_position.x + sum_vector.x, self.current_position.y - sum_vector.y) if get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y): self.life_limit = -1 return self.current_position.reinit( x=self.candidat_position.x, y=self.candidat_position.y, ) if self.current_speed.length < 2: self.current_speed = None self.current_polygon = [ Vector2D( length=p_length, angle=p_angle, ) + self.current_position for p_length, p_angle in self.demensions_info ]
def __init__(self, name, wearpon1, wearpon2, *args, **kwargs): self.demensions = [ Vector2D(x=-7, y=30), Vector2D(x=7, y=30), Vector2D(x=45, y=5), Vector2D(x=12, y=-30), Vector2D(x=-12, y=-30), Vector2D(x=-45, y=5), ] super(Ship30, self).__init__( name=name, bullet1_cls=map_weapon1[wearpon1], bullet2_cls=map_weapon2[wearpon2], ship_type=30, *args, **kwargs, ) self.health = 170 self.weight = 1.3 self.rotation_speed = 0.9 * 4 self.engine.power = self.engine.power * 1.10
def __init__(self, name, wearpon1, wearpon2, *args, **kwargs): self.demensions = [ Vector2D(x=-7, y=30), Vector2D(x=7, y=30), Vector2D(x=32, y=10), Vector2D(x=12, y=-30), Vector2D(x=-12, y=-30), Vector2D(x=-32, y=10), ] # Bot ship super(Ship10, self).__init__( name=name, bullet1_cls=map_weapon1[wearpon1], bullet2_cls=map_weapon2[wearpon2], ship_type=10, *args, **kwargs, ) self.rotation_speed = 3 self.weight = 0.8 self.health = 80 self.engine.power = 800
def __init__( self, emitter, speed=650, ricochet=False, life_limit=4, health=-35, dispersion=0, able_to_make_tracing=False, # seconds reverse_shot=False, center_shot=False, is_tracer=False, trace_speed=None, trace_dispersion=0, trace_life_limit=0, trace_health=-5, trace_count=0, trace_route=0, trace_ricochet=False, bullet_speed=None, ): self.ship_emitter = emitter.ship_emitter if is_tracer else emitter self.pk = uuid.uuid4().hex[0:4] self.current_polygon = None self.trace_speed = trace_speed self.trace_dispersion = trace_dispersion self.trace_life_limit = trace_life_limit self.trace_health = trace_health self.trace_count = trace_count self.trace_route = trace_route self.trace_ricochet = trace_ricochet self.bullet_speed = bullet_speed if is_tracer: bullet_route = random.random() * 2 * math.pi else: if dispersion: bullet_route = emitter.current_route + random.random( ) * dispersion - dispersion / 2 else: bullet_route = emitter.current_route start_position = emitter.position_abs(candidat=False) max_radius = 2 if is_tracer else emitter.demensions_max_radius self.current_route = 0 if center_shot: self.current_position = Vector2D(x=start_position.x, y=start_position.y) elif reverse_shot: self.current_position = ( Vector2D(x=start_position.x, y=start_position.y) - Vector2D(x=max_radius * math.cos(emitter.current_route), y=-max_radius * math.sin(emitter.current_route))) else: self.current_position = ( Vector2D(x=start_position.x, y=start_position.y) + Vector2D(x=max_radius * math.cos(emitter.current_route), y=-max_radius * math.sin(emitter.current_route))) self.approx_x = floor(self.current_position.x / CELL_STEP) self.approx_y = floor(self.current_position.y / CELL_STEP) if is_tracer: if trace_speed is None: self.current_speed = Vector2D(length=speed, angle=bullet_route) else: if trace_route: self.current_speed = Vector2D( length=trace_speed * random.random(), angle=bullet_route) + self.bullet_speed * trace_route else: self.current_speed = Vector2D(length=trace_speed * random.random(), angle=bullet_route) else: if reverse_shot: self.current_speed = Vector2D(length=speed, angle=bullet_route + math.pi) else: self.current_speed = Vector2D(length=speed, angle=bullet_route) if emitter.current_speed and not center_shot: self.current_speed += emitter.current_speed self.bullet_speed = self.current_speed self.candidat_position = Vector2D(x=0, y=0) self.ricochet = ricochet self.able_to_make_tracing = -able_to_make_tracing if able_to_make_tracing else -100000000 self.life_limit = life_limit self.health = health self.fuel = 0 self.bullet1 = 0 self.bullet2 = 0 if not self.health: self.size = 0 else: self.size = 5 - (self.health / 100) * 25
try: from app.vector import Vector2D from app.constants import MAP_SIZE, TEST_BOT except ImportError: TEST_BOT = False MAP_SIZE = (3371, 1889) class Vector2D: def __init__(self, x=None, y=None): self.x = x self.y = y fake_polygon = ( Vector2D(0, 0), Vector2D(1, 0), Vector2D(1, 1), Vector2D(0, 1), ) polygon2 = ( Vector2D(0, 0), Vector2D(MAP_SIZE[0], 0), Vector2D(MAP_SIZE[0], 50), Vector2D(0, 20), ) polygon3 = ( Vector2D(MAP_SIZE[0] - 50, 0), Vector2D(MAP_SIZE[0], 0), Vector2D(MAP_SIZE[0], MAP_SIZE[1]), Vector2D(MAP_SIZE[0] - 50, MAP_SIZE[1]),
def calculate_position(self): if self.immortality: self.immortality -= 1 if self.health <= 0: if self.dead_step == -1: self.mark_as_dead(to_dive=True) return self._dive() a_friction = self.current_speed * (-MU) tilt_angle_speed = 7 * self.rotation_speed * app.constants.FRAME_INTERVAL if self.control.vector: if self.control.vector > 0 and self.tilt_angle < 9: self.tilt_angle += tilt_angle_speed elif self.control.vector < 0 and self.tilt_angle > -9: self.tilt_angle -= tilt_angle_speed else: if self.tilt_angle > 0: self.tilt_angle -= tilt_angle_speed elif self.tilt_angle < 0: self.tilt_angle += tilt_angle_speed self.current_route -= self.rotation_speed * app.constants.FRAME_INTERVAL * self.control.vector if self.control.accelerator: power = self.engine.power if self.engine.is_ready else 200 self._acc.reinit( angle=self.current_route, length=power / self.weight, ) self.engine.acceleration() self.a_sum.reinit( x=self._acc.x + gravity.x + a_friction.x, y=self._acc.y + gravity.y + a_friction.y, ) else: self.a_sum.reinit( x=gravity.x + a_friction.x, y=gravity.y + a_friction.y, ) self.a_sum *= app.constants.FRAME_INTERVAL self.current_speed += self.a_sum sum_vector = self.current_speed * app.constants.FRAME_INTERVAL + ( self.a_sum / 2) * app.constants.FRAME_INTERVAL**2 self.candidat_position.x = self.current_position.x + sum_vector.x self.candidat_position.y = self.current_position.y - sum_vector.y round_x = floor(self.candidat_position.x / CELL_STEP) round_y = floor(self.candidat_position.y / CELL_STEP) try: has_world_collision = polygon_cell[round_x][round_y] except: has_world_collision = True if has_world_collision: polygon_idx = get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y) if polygon_idx: polygon = all_polygons[polygon_idx] else: polygon = None else: polygon = None if polygon: angle = get_angle_collision(self, polygon) or _pi_2 self.current_speed.reinit(length=self.current_speed.length * 0.3, angle=angle * 2 + self.current_speed.angle()) self.health -= 100 * app.constants.FRAME_INTERVAL # polygon dead if self.health <= 0: self.mark_as_dead(DeadReason.WORLD_COLLISION) self.current_polygon = [ Vector2D( length=p_length, angle=p_angle - self.current_route, ) + self.current_position for p_length, p_angle in self.demensions_info ] self.current_polygon_info = get_polygon_info(self.current_polygon) return self.current_position.reinit( x=self.candidat_position.x, y=self.candidat_position.y, ) self.current_polygon = [ Vector2D( length=p_length, angle=p_angle - self.current_route, ) + self.current_position for p_length, p_angle in self.demensions_info ] self.current_polygon_info = get_polygon_info(self.current_polygon)
from app.engine import Engine from app.gun import Gun from app.helper import in_polygon_info, get_polygon_info, get_angle_collision, polygon_cell from app.polygon_data import all_polygons from app.vector import Vector2D import logging logger = logging.getLogger('xlimb.' + __name__) logger_stat = logging.getLogger('stat.' + __name__) now = datetime.now _pi2 = pi * 2 _pi_2 = pi / 2 gravity = Vector2D(angle=pi * 3 / 2, length=G) gravity_dive = Vector2D(angle=pi * 3 / 2, length=G_DIVE) _smoke_interval = 0.020 class _Control: def __init__(self): self.accelerator = 0 self.vector = 0 self.shot = 0 self.shot2 = 0 class Ship(object): def __init__(self, name, bullet1_cls, bullet2_cls, ship_type=None):
def __init__( self, emitter=None, health=1, fuel=0, bullet1=0, bullet2=0, dispersion=True, start_position=None, life_limit=30, ): self.type = 0 self.damage = 0 # interface consist self.kills = 0 # interface consist self.current_route = 0 # interface consist self.pk = random.randint(1, 30000) self.ship_emitter = emitter if dispersion: bonus_route = random.random() * 2 * math.pi else: bonus_route = 0 self._demensions = [ Vector2D(x=-15, y=15), Vector2D(x=15, y=15), Vector2D(x=15, y=-15), Vector2D(x=-15, y=-15), ] self.demensions_info = [(p.length, p.angle() - math.pi / 2) for p in self._demensions] self.demensions_max_radius = max(d.y for d in self._demensions) if start_position: self.current_position = Vector2D( x=start_position.x, y=start_position.y, ) self.candidat_position = Vector2D(x=0, y=0) else: while True: self.candidat_position = Vector2D( x=random.random() * MAP_SIZE[0], y=random.random() * MAP_SIZE[1], ) if not get_polygon_idx_collision(self.candidat_position.x, self.candidat_position.y): self.current_position = self.candidat_position break self.current_polygon = [ Vector2D( length=p_length, angle=p_angle, ) + self.current_position for p_length, p_angle in self.demensions_info ] if dispersion: self.current_speed = Vector2D(length=60, angle=bonus_route) else: self.current_speed = None self.life_limit = life_limit self.health = health self.fuel = fuel self.bullet1 = bullet1 self.bullet2 = bullet2
def bot_control(ws, viewport_data): last_msg = None bot_path = None prev_point = None while True: sleep(0.1) v_data = copy.deepcopy(viewport_data) try: v_data['my_obj']['x'] v_data['my_obj']['client_pk'] v_data['my_obj']['ship_pk'] except KeyError: bot_path, nearest_point = None, None sleep(1) continue msg = { 'client_pk': v_data['my_obj']['client_pk'], 'ship_pk': v_data['my_obj']['ship_pk'], 'gas': 0, 'direction': 0, 'weapon1': 0, 'weapon2': 0, } if prev_point: speed = Vector2D(x=v_data['my_obj']['x'] - prev_point['x'], y=prev_point['y'] - v_data['my_obj']['y']).length else: speed = 0 if not v_data['ships']: msg['weapon1'] = 0 msg['weapon1'] = 0 if not v_data['bonuses']: msg['gas'] = 0 if _ship_attack(v_data, msg): bot_path, nearest_point = None, None elif _grab_bonus(v_data, speed, msg): bot_path, nearest_point = None, None else: if not bot_path: bot_path, nearest_point, is_main_path = get_bot_path( v_data['my_obj']) if not bot_path: msg['gas'] = 1 continue limit = 100 if is_main_path else 50 if sort_by_distance(v_data['my_obj'], nearest_point) < limit: try: nearest_point = bot_path.__next__() except StopIteration: local_path = copy.deepcopy(path) sort_dst = partial(sort_by_distance, v_data['my_obj']) local_path.sort(key=sort_dst) nearest_point = local_path[0] bot_path = cycle(path) for i in bot_path: if i == nearest_point: break msg['direction'], angle = get_directions_correction( v_data['my_obj'], nearest_point) msg['gas'] = _gas(speed, v_data['my_obj'], nearest_point, is_main_path, angle=angle) if _has_world_collision(v_data['my_obj'], nearest_point): bot_path, nearest_point = None, None pass else: pass candidat_msg = '{client_pk}:cursor_pos:{ship_pk}!{gas}!{direction}!{weapon1}!{weapon2}'.format( **msg) if candidat_msg != last_msg: ws.send(candidat_msg) last_msg = candidat_msg prev_point = copy.deepcopy(v_data['my_obj'])
def __init__(self, x, y): self.candidat_position = Vector2D(x=x, y=y)