def calc_aim(self, nearest_enemy, unit, game): aim = model.Vec2Double(0, 0) los = False if nearest_enemy is not None: los = True traj_len_x = nearest_enemy.position.x + \ self.enemy_velocity.x * 2 - unit.position.x traj_len_y = nearest_enemy.position.y + \ self.enemy_velocity.y * 2 - unit.position.y aim = model.Vec2Double(traj_len_x, traj_len_y) sign_x = Calc.sign(traj_len_x) sign_y = Calc.sign(traj_len_y) curr_pos = model.Vec2Double(unit.position.x, unit.position.y) while Calc.distance_sqr(curr_pos, nearest_enemy.position) >= 2: pos_dx = model.Vec2Double(curr_pos.x + sign_x, curr_pos.y) pos_dy = model.Vec2Double(curr_pos.x, curr_pos.y + sign_y) if Calc.distance_sqr( pos_dx, nearest_enemy.position) <= Calc.distance_sqr( pos_dy, nearest_enemy.position): curr_pos.x += sign_x else: curr_pos.y += sign_y if game.level.tiles[int(curr_pos.x)][int( curr_pos.y)] == model.Tile.WALL: los = False break return aim, los
def get_action(self, unit, game, debug): # Replace this code with your own def distance_sqr(a, b): return (a.x - b.x)**2 + (a.y - b.y)**2 nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) nearest_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) print(nearest_enemy.position) target_pos = unit.position if unit.weapon is None and nearest_weapon is not None: target_pos = nearest_weapon.position elif nearest_enemy is not None: target_pos = nearest_enemy.position target_pos.x, target_pos.y = self.findtarget(game, 29) debug.draw(model.CustomData.Log("Target pos: {}".format(target_pos))) aim = model.Vec2Double(0, 0) if nearest_enemy is not None: aim = model.Vec2Double(nearest_enemy.position.x - unit.position.x, nearest_enemy.position.y - unit.position.y) jump = target_pos.y > unit.position.y and self.checkifjumpvalid( game, unit.position, target_pos) if target_pos.x > unit.position.x and game.level.tiles[int( unit.position.x + 1)][int(unit.position.y)] == model.Tile.WALL: jump = True elif target_pos.x < unit.position.x and game.level.tiles[int( unit.position.x - 1)][int(unit.position.y)] == model.Tile.WALL: jump = True if target_pos.x > unit.position.x and game.level.tiles[ int(unit.position.x) + 1][int(unit.position.y - 1)] == model.Tile.EMPTY: jump = True elif target_pos.x < unit.position.x and game.level.tiles[ int(unit.position.x) - 1][int(unit.position.y - 1)] == model.Tile.EMPTY: jump = True #print(unit.position) #self.findnodes(game) #self.findnodes_wall(game) #cspace=np.zeros([40,30],dtype=int) #cspace=self.createcspace(game,cspace,int(unit.position.x),int(unit.position.y)) #for row in cspace: #for val in row: #print(val,end='') #print() return model.UnitAction(velocity=target_pos.x - unit.position.x, jump=jump, jump_down=False, aim=aim, shoot=True, reload=False, swap_weapon=False, plant_mine=False)
def calc_objects(self, unit, game): nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: Calc.distance_sqr(u.position, unit.position), default=None) nearest_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: Calc.distance_sqr(box.position, unit.position), default=None) nearest_hpbox = min( filter(lambda box: isinstance(box.item, model.Item.HealthPack), game.loot_boxes), key=lambda box: Calc.distance_sqr(box.position, unit.position), default=None) expected_enemy_pos = nearest_enemy if (self.prev_enemy_pos is not None) and (nearest_enemy is not None): self.enemy_velocity = model.Vec2Double( nearest_enemy.position.x - self.prev_enemy_pos.x, nearest_enemy.position.y - self.prev_enemy_pos.y) expected_enemy_pos = model.Vec2Double( nearest_enemy.position.x + self.enemy_velocity.x, nearest_enemy.position.y + self.enemy_velocity.y) return nearest_enemy, nearest_weapon, nearest_hpbox, expected_enemy_pos
def calc_objects(self, unit, game): if self.max_hp is None: self.max_hp = unit.health self.curr_hp = unit.health / self.max_hp if unit.weapon is not None: if unit.weapon.typ == 0: self.swap_weapon = True else: self.swap_weapon = False nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: Calc.distance_sqr(u.position, unit.position), default=None) if nearest_enemy is not None: self.rltv_enm_side = Calc.sign(unit.position.x - nearest_enemy.position.x) or -1 dist = Calc.distance_sqr(nearest_enemy.position, unit.position) if dist < 12: self.rltv_enm_dist = 0 elif dist < 60: self.rltv_enm_dist = 1 elif dist < 300: self.rltv_enm_dist = 2 else: self.rltv_enm_dist = 3 nearest_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: Calc.distance_sqr(box.position, unit.position), default=None) nearest_hpbox = min( filter(lambda box: isinstance(box.item, model.Item.HealthPack), game.loot_boxes), key=lambda box: Calc.distance_sqr(box.position, unit.position), default=None) nearest_mine = min( filter(lambda box: isinstance(box.item, model.Item.Mine), game.loot_boxes), key=lambda box: Calc.distance_sqr(box.position, unit.position), default=None) expected_enemy_pos = unit.position if (self.prev_enemy_pos is not None) and (nearest_enemy is not None): self.enemy_velocity = model.Vec2Double( nearest_enemy.position.x - self.prev_enemy_pos.x, nearest_enemy.position.y - self.prev_enemy_pos.y) expected_enemy_pos = model.Vec2Double( nearest_enemy.position.x + self.enemy_velocity.x, nearest_enemy.position.y + self.enemy_velocity.y) return nearest_enemy, nearest_weapon, nearest_hpbox, nearest_mine, expected_enemy_pos
def calc_aim(self, nearest_enemy, unit, game): aim = model.Vec2Double(0, 0) shoot = False if nearest_enemy is not None: shoot = True traj_len_x = nearest_enemy.position.x - unit.position.x traj_len_y = nearest_enemy.position.y - unit.position.y aim = model.Vec2Double(traj_len_x, traj_len_y) return aim, shoot
def get_action(self, unit, game, debug): # Replace this code with your own self.game = game self.unit = unit self.debug = debug def distance_sqr(a, b): return (a.x - b.x) ** 2 + (a.y - b.y) ** 2 nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, self.game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) nearest_weapon = min( filter(lambda box: isinstance( box.item, model.Item.Weapon), self.game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) target_pos = nearest_enemy.position action = self.RRT(unit.position, nearest_weapon.position) aim = model.Vec2Double(0, 0) if nearest_enemy is not None: aim = model.Vec2Double( nearest_enemy.position.x - unit.position.x, nearest_enemy.position.y - unit.position.y) jump = target_pos.y > unit.position.y if target_pos.x > unit.position.x and self.game.level.tiles[int(unit.position.x + 1)][int(unit.position.y)] == model.Tile.WALL: jump = True if target_pos.x < unit.position.x and self.game.level.tiles[int(unit.position.x - 1)][int(unit.position.y)] == model.Tile.WALL: jump = True debug.draw(model.CustomData.Log(f"{action}")) if action != None: return model.UnitAction( velocity=action[0], jump=action[1], jump_down=action[2], aim=aim, shoot=False, reload=False, swap_weapon=False, plant_mine=False) else: return model.UnitAction( velocity=0, jump=False, jump_down=False, aim=aim, shoot=False, reload=False, swap_weapon=False, plant_mine=False)
def point_at_distance(self, p, d): w = model.Vec2Double(0, self.center.y) if p.x < self.center.x: w.x = max(self.center.x - d, 0.2) else: w.x = min(self.center.x + d, 37) return w
def calc_move(self, unit, nearest_weapon, nearest_enemy, nearest_hpbox): target_pos = unit.position if unit.weapon is None and nearest_weapon is not None: target_pos = nearest_weapon.position elif nearest_enemy is not None: target_pos = model.Vec2Double(nearest_enemy.position.x - 10, nearest_enemy.position.y) return target_pos
def find_enemy(self, which, where): e = self.search_enemy(None, None, None)[which].position u = self.unit.position f = (int(e.x), int(e.y)) v = (int(u.x), int(u.y)) path = self.astar(self.map, v, f) l = len(path) if where < l: x = path[l - where] w = model.Vec2Double(x[0], x[1]) return [l, w, e] elif l == 1: x = path[0] w = model.Vec2Double(x[0], x[1]) return [l, w, e] else: return [l, e, e]
def approachingbullet(): nearest=sorted(game.bullets,key=lambda u: distance_sqr(u.position, unit.position)) for bull in nearest: vec2=model.Vec2Double(0, 0) vec2.x=unit.position.x-bull.position.x vec2.y=unit.position.y-bull.position.y if vec2.x*bull.velocity.x+vec2.y*bull.velocity.y>0: return bull return None
def calc_move(self, unit, nearest_weapon, nearest_enemy, nearest_hpbox, nearest_mine): target_pos = unit.position if (unit.weapon is None and nearest_weapon is not None) or (unit.weapon.typ == 0): target_pos = nearest_weapon.position elif nearest_hpbox is not None and self.curr_hp <= 0.9: target_pos = model.Vec2Double(nearest_hpbox.position.x, nearest_hpbox.position.y) elif nearest_mine is not None: target_pos = model.Vec2Double(nearest_mine.position.x, nearest_mine.position.y) elif nearest_enemy is not None: target_pos = model.Vec2Double( nearest_enemy.position.x + 5 * self.rltv_enm_side, nearest_enemy.position.y) velocity = (target_pos.x - unit.position.x) * 5 return target_pos, velocity
def get_action(self, unit, game, debug): # Replace this code with your own def distance_sqr(a, b): return (a.x - b.x)**2 + (a.y - b.y)**2 nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) nearest_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) target_pos = unit.position if unit.weapon is None and nearest_weapon is not None: target_pos = nearest_weapon.position elif nearest_enemy is not None: target_pos = nearest_enemy.position debug.draw(model.CustomData.Log("Target pos: {}".format(target_pos))) aim = model.Vec2Double(0, 0) if nearest_enemy is not None: aim = model.Vec2Double(nearest_enemy.position.x - unit.position.x, nearest_enemy.position.y - unit.position.y) jump = target_pos.y > unit.position.y print("target_pos.y", target_pos.y) print("unit_pos.y", unit.position.y) print("jump", jump) if target_pos.x > unit.position.x and game.level.tiles[int( unit.position.x + 1)][int(unit.position.y)] == model.Tile.WALL: jump = True if target_pos.x < unit.position.x and game.level.tiles[int( unit.position.x - 1)][int(unit.position.y)] == model.Tile.WALL: jump = True jump = True #The unit all need JUMP return model.UnitAction(velocity=target_pos.x - unit.position.x, jump=jump, jump_down=not jump, aim=aim, shoot=True, reload=False, swap_weapon=False, plant_mine=False)
def __init__(self): self.prev_enemy_pos = None self.enemy_velocity = model.Vec2Double(0, 0) self.expected_enemy_pos = None self.max_hp = None self.curr_hp = None self.got_mine = False self.swap_weapon = False self.rltv_enm_side = None self.rltv_enm_dist = 3 self.shoot_counter = 3
def valid(self, i): ii = i - 1 if ii < 0.8: ii = 0.8 p = self.find_tile(ii, 0) if p is None: p = model.Vec2Double(int(self.unit.position.x), int(self.unit.position.y)) ii = i + 1 if ii > 36: ii = 36 q = self.find_tile(ii, 0) if q is None: q = model.Vec2Double(int(self.unit.position.x), int(self.unit.position.y)) if self.game.level.tiles[int(i)][p.y - 1] != model.Tile.JUMP_PAD and ( p.y == q.y): return [True, model.Vec2Double(i, p.y)] else: return [False, None]
def further_from(self, L): j = 0 ma = -1 p = None for i in range(len(L)): pi = model.Vec2Double(L[i][0], L[i][1]) d = self.dist_to(pi) if d > ma: j = i p = L[j] ma = d return (p, j)
def __init__(self): self.shoot = True self.reload = False self.aim = model.Vec2Double(2, 2) self.eye = Eyes() self.actions_stack = None # self.unit = None self.game = None self.debug = None # self.target = None
def get_cords(self, xstate, ystate): time = 1/(self.prop.ticks_per_second * self.prop.updates_per_tick) if ystate == 1: height = self.prop.unit_jump_speed / time if self.game.level.tiles[floor(self.pos.x)][floor(self.pos.y)] == 4: height = self.prop.jump_pad_jump_speed / time if ystate == -1: height = self.prop.unit_fall_speed / time if ystate == 0: height = 0 width = self.prop.unit_max_horizontal_speed / (time) return model.Vec2Double(self.pos.x + width * xstate, self.pos.y + height * ystate)
def brain(self, unit, game, debug, param): #update self.update(unit, game, debug) # if len(self.control.get_active_stack("action")) != 0: return self.control.interpreter() #setup the environment co = Coordination() co.update(unit, game, debug) co.set_2d_field(self.unit.position, 8) #get a gun p = self.get_gun() if p is not None: self.control.append(("XY", p, "N", None)) return self.control.interpreter() #get health pack p = self.condition_interpreter(param["pack"]) if p is not None: self.control.append(("XY", p, "N", None)) return self.control.interpreter() #find enemy u = self.unit.position e = self.eye.find_enemy(0, 2) # co1 = Coordination() co1.update(unit, game, debug) #parameters di = param["distance"] kata = param["kata"] di = self.distance_interpreter(di) # if e[0] < di: co1.center = e[1] self.dist = di e = self.keep_distance(e[1]) e = co1.avoidance_points_proxy(kata, e, self.i_avoid) #detect bullet w = self.should_avoid_bullet() if w is not None: e = w.further_from(co1.avoidance_points) e = e[0] e = model.Vec2Double(e[0], e[1]) self.i_avoid += 1 self.i_avoid %= len(co1.avoidance_points) self.control.append(("DI", e, "T", 3)) return self.control.interpreter() else: e = e[1] self.control.append(("XY", e, "N", None)) return self.control.interpreter()
def on_fire_line(u1, enemy, u2, game): print("on_fire_line") u1 = u1.position enemy = enemy.position u2 = u2.position d = math.sqrt(distance_sqr(u1, enemy)) aim = model.Vec2Double(enemy.x - u1.x, enemy.y - u1.y) for i in range(int(d) - 1): pos1 = model.Vec2Double(u1.x + (i + 1) / d * aim.x, u1.y + (i + 1) / d * aim.y) print("Fireline ", i, int(pos1.x), int(pos1.y)) if math.sqrt(distance_sqr(pos1, u2)) < 1: print("stop fire!!!") return True if i < 6 and game.level.tiles[int( pos1.x)][int(pos1.y + 0.5)] == model.Tile.WALL: print("stop fire wall !!!") return True return False
def find_tile(self, i, typ): j = 0 n = len(self.game.level.tiles[int(i)]) t = None if typ == 0: while j < n: if self.game.level.tiles[int(i)][j] != 0: break j += 1 while j < n: if self.game.level.tiles[int(i)][j] == 0: t = model.Vec2Double(i, j + 2) break j += 1 return t
def should_avoid_bullet(self): u = self.unit L = self.eye.search_bullet(10, u) if len(L) != 0: #build line aL = Line(1, 2) p1 = L[0] p1x = p1.position.x p1y = p1.position.y p1vx = p1.velocity.x p1vy = p1.velocity.y p2 = model.Vec2Double(p1x + p1x * p1vx * 0.1, p1y + p1y * p1vy * 0.1) aL.find_eq(p1.position, p2) return aL else: return None
def set_2d_field(self, p, n): self.field = [] step = 0.5 row = len(self.game.level.tiles) col = len(self.game.level.tiles[0]) for i in range(2 * n): for j in range(2 * n): if i == n and j == n: continue q = model.Vec2Double(p.x + (-n + i) * step, p.y + (-n - j) * step) cond = (0 <= q.x and q.x <= row) and ( 0 <= q.y and q.y <= col) and self.reachable(p, q) if cond: cond = self.game.level.tiles[int(q.x)][int( q.y)] == model.Tile.EMPTY if cond: self.field.append(q)
def closest_valid_neighbor(self, p, step): n = 2 L = [] row = len(self.game.level.tiles) col = len(self.game.level.tiles[0]) for i in range(2 * n): for j in range(2 * n): q = model.Vec2Double(p.x + (-n + i) * step, p.y + (-n - j) * step) cond = (0 <= q.x and q.x <= row) and ( 0 <= q.y and q.y <= col) and self.reachable(p, q) if cond: cond = self.game.level.tiles[int(q.x)][int( q.y)] != model.Tile.WALL cond = cond and self.game.level.tiles[int(q.x)][int( q.y)] != model.Tile.JUMP_PAD if cond: L.append(q) return L[0]
def get_middle_position(from_pos: model.Vec2Double, to_pos: model.Vec2Double, game_params: model.Properties): # Добавить ограничение по времени # Добавить ограничение по геометрии dx = math.fabs(from_pos.x - to_pos.x) dy = math.fabs(from_pos.y - to_pos.y) v_x = game_params.unit_max_horizontal_speed v_up = game_params.unit_jump_speed v_down = game_params.unit_fall_speed time_1 = (dx + v_x * dy / v_down) * v_down /\ (v_x * (v_down + v_up)) if time_1 > game_params.unit_jump_time: return None return model.Vec2Double( from_pos.x + (time_1 * v_x) * get_sign(to_pos.x - from_pos.x), from_pos.y + time_1 * game_params.unit_fall_speed if math.fabs(self.from_position.x - self.to_position.x) > 0.1 else to_pos.y)
def RRT(self, start_pos, end_pos): self.tree = [Node(self.unit, None, self.game)] goal = [model.Vec2Double(floor(end_pos.x), floor(end_pos.y)), model.Vec2Double(floor(end_pos.x + 1), floor( end_pos.y)), model.Vec2Double(floor(end_pos.x), floor(end_pos.y + 1)), model.Vec2Double(floor(end_pos.x + 1), floor(end_pos.y + 1))] while len(self.tree) != 1000: xsamp = model.Vec2Double(randrange(1, 40), randrange(1, 30)) dist = [comp(xsamp, n.pos) for n in self.tree] near = dist.index(min(dist)) nearNode = self.tree[near] nearNode.pick(xsamp, self) leaf = model.Vec2Double(floor(self.tree[-1].pos.x), floor(self.tree[-1].pos.y)) if leaf in goal: return self.tree[-1].set(self.debug)
def get_action(self, unit, game, debug): # Replace this code with your own def distance_sqr(a, b): return (a.x - b.x) ** 2 + (a.y - b.y) ** 2 def help(approach): if(approach is not None and (unit.health>80 or nearest_health is None))and unit.weapon is not None: #debug.draw(model.CustomData.Log("Nearest bullet velocity: {}".format(nearest_bullet.velocity))) #debug.draw(model.CustomData.Log("Nearest bullet position: {}".format(nearest_bullet.position))) #debug.draw(model.CustomData.Log("Nearest bullet distance: {}".format(distance_sqr(nearest_bullet.position,unit.position)))) if(distance_sqr(approach.position,unit.position)<25): ''' ind=0 yind=0 #target_pos.x=unit.position.x+2*nearest_bullet.position.x//abs(nearest_bullet.position.x) for i in range(nearest_bullet.position.x,50*(nearest_bullet.velocity.x),nearest_bullet.velocity.x//abs(nearest_bullet.velocity.x)): ind=i yind=nearest_bullet.y/nearest_bullet.x() if(game.level.tiles[int(i)][int((nearest_enemy.position.y-unit.position.y)*(i-unit.position.x)/(nearest_enemy.position.x-unit.position.x)+unit.position.y)]==model.Tile.WALL): ''' if(approach.velocity.x>0): left=approach.velocity.y/approach.velocity.x*(unit.position.x-.9-approach.position.x)+approach.position.y if(left-(unit.position.y+1.8)>0): jump=False else: #target_pos.y=32 ''' if(game.level.tiles[int(unit.position.x)][int(unit.position.y-3.8)] == model.Tile.EMPTY): jump=False else: ''' jump=True else: right=approach.velocity.y/approach.velocity.x*(unit.position.x+.9-approach.position.x)+approach.position.y if(right-(unit.position.y+1.8)>0): jump=False else: #target_pos.y=32 ''' if(game.level.tiles[int(unit.position.x )][int(unit.position.y-3.8)] == model.Tile.EMPTY): jump=False else: ''' jump=True if(approach.velocity.y<0): up=((unit.position.y+1.8-approach.position.y)/(approach.velocity.y/approach.velocity.x)+approach.position.x) if(abs(up-(unit.position.x-.9))>abs(up-(unit.position.x+.9))): velocity=-50 else: velocity=50 else: down=((unit.position.y-approach.position.y)/(approach.velocity.y/approach.velocity.x)+approach.position.x) if(abs(down-(unit.position.x-.9))>abs(down-(unit.position.x+.9))): velocity=-50 else: velocity=50 return jump,velocity nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units),key=lambda u: distance_sqr(u.position, unit.position),default=None) nearest_weapon = min(filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes),key=lambda box: distance_sqr(box.position, unit.position),default=None) nearest_health = min( filter(lambda box: isinstance(box.item, model.Item.HealthPack), game.loot_boxes),key=lambda box: distance_sqr(box.position, unit.position),default=None) f = sorted(filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes),key=lambda box: distance_sqr(box.position, nearest_enemy.position),reverse=True) farthest_weapon=nearest_enemy for i in f: if(distance_sqr(unit.position,i.position)<=distance_sqr(i.position,nearest_enemy.position)): farthest_weapon=i break nearest_bullet=min( filter(lambda u: u.player_id != unit.player_id, game.bullets),key=lambda u: distance_sqr(u.position, unit.position),default=None) def approachingbullet(): nearest=sorted(game.bullets,key=lambda u: distance_sqr(u.position, unit.position)) for bull in nearest: vec2=model.Vec2Double(0, 0) vec2.x=unit.position.x-bull.position.x vec2.y=unit.position.y-bull.position.y if vec2.x*bull.velocity.x+vec2.y*bull.velocity.y>0: return bull return None target_pos =model.Vec2Double(0, 0) target_pos.x=unit.position.x target_pos.y=unit.position.y if unit.weapon is None and nearest_weapon is not None: target_pos = nearest_weapon.position #debug.draw(model.CustomData.Log("unit pos: {}".format(unit.position))) #debug.draw(model.CustomData.Log("approaching bullet: {}".format(approachingbullet()))) aim = model.Vec2Double(0, 0) reloadweapon=False shoot=True #jump=False swap=False plant=False trick=1 #print('Walked_right=',unit.walked_right) #print('shoot=',unit.shoot) ''' if(len(game.bullets)!=0): print(game.bullets[0].player_id) ''' if unit.weapon is not None and (nearest_health is None ): if(str(unit.weapon.typ)!='WeaponType.ROCKET_LAUNCHER' ): target_pos=farthest_weapon.position jump=True else: target_pos=nearest_enemy.position #print(unit.size) #gap=1 if nearest_health is not None and unit.weapon is not None and nearest_enemy.position.x-nearest_health.position.x!=0 and unit.health>=90: #print('bhai') target_pos=nearest_health.position trick=(nearest_enemy.position.x-nearest_health.position.x)//abs((nearest_enemy.position.x-nearest_health.position.x)) if(unit.position.y!=target_pos.y): trick*=-1 target_pos.x+=trick if(unit.health<90 and nearest_health is not None and unit.weapon is not None): #velocity=(target_pos.x - unit.position.x) target_pos=nearest_health.position #debug.draw(model.CustomData.Log("Health: {}".format(nearest_health.position))) #print(nearest_weapon.item.weapon_type) if nearest_weapon is not None and unit.weapon is not None and (unit.health>80 or nearest_health is None): if (str(nearest_weapon.item.weapon_type)=='WeaponType.ROCKET_LAUNCHER' and str(unit.weapon.typ)!='WeaponType.ROCKET_LAUNCHER' ): target_pos=nearest_weapon.position swap=True elif(str(nearest_weapon.item.weapon_type)!='WeaponType.PISTOL' and str(unit.weapon.typ)=='WeaponType.PISTOL' ): target_pos=nearest_weapon.position swap=True if(unit.weapon is not None): if str(unit.weapon.typ)=='WeaponType.ROCKET_LAUNCHER' and unit.health==100: target_pos=nearest_enemy.position flag=0 mini=min(int(unit.position.x),int(nearest_enemy.position.x)) maxi=max(int(unit.position.x),int(nearest_enemy.position.x)) for i in range(mini+1,maxi): if(game.level.tiles[int(i)][int((nearest_enemy.position.y-unit.position.y)*(i-unit.position.x)/(nearest_enemy.position.x-unit.position.x)+unit.position.y)]==model.Tile.WALL): #print('wall') flag=1 break if(flag==1): shoot=False reloadweapon=True #l=[i.player_id for i in game.units] #print(l) for i in game.units: if(i.player_id==unit.player_id and (unit.position.x!=i.position.x or unit.position.y!=i.position.y) and nearest_enemy.position.x!=unit.position.x): if(((nearest_enemy.position.y-unit.position.y)*(i.position.x-unit.position.x)/(nearest_enemy.position.x-unit.position.x)+unit.position.y)>=i.position.y and ((nearest_enemy.position.y-unit.position.y)*(i.position.x-unit.position.x)/(nearest_enemy.position.x-unit.position.x)+unit.position.y)<=i.position.y+2): if(distance_sqr(unit.position,i.position)<distance_sqr(unit.position,nearest_enemy.position)): shoot=False debug.draw(model.CustomData.Log("Health{}".format(shoot))) reloadweapon=True elif(i.player_id==unit.player_id and (unit.position.y!=i.position.y) and i.position.x==unit.position.x and nearest_enemy.position.x==unit.position.x): if(distance_sqr(unit.position,i.position)<distance_sqr(unit.position,nearest_enemy.position)): shoot=False debug.draw(model.CustomData.Log("Health{}".format(shoot))) reloadweapon=True #print(target_pos) ''' if nearest_enemy.weapon is not None and unit.weapon is not None: if str(nearest_enemy.weapon.typ)=='WeaponType.ROCKET_LAUNCHER' or str(unit.weapon.typ)=='WeaponType.ROCKET_LAUNCHER': if distance_sqr(nearest_enemy,unit)<5: target_pos=(nearest_enemy.position+unit.position)%25 ''' #print(game.level.tiles) jump = (target_pos.y != unit.position.y) velocity=(target_pos.x - unit.position.x) if target_pos.x > unit.position.x and game.level.tiles[int(unit.position.x + 1)][int(unit.position.y)] == model.Tile.WALL: jump = True elif target_pos.x < unit.position.x and game.level.tiles[int(unit.position.x - 1)][int(unit.position.y)] == model.Tile.WALL: jump = True elif(abs(velocity)<5): velocity=(velocity)*5 if(abs(target_pos.x-unit.position.x)<=2 and target_pos.y+1<unit.position.y): jump=False if(velocity>0 ): for i in game.units: if((unit.position.x<i.position.x and unit.position.x+2>i.position.x )and ( abs(unit.position.y-i.position.y)<=3)): if(game.level.tiles[int(i.position.x )][int(i.position.y-3.8)] == model.Tile.EMPTY): debug.draw(model.CustomData.Log("jump")) jump=True else: jump=False else: for i in game.units: if((unit.position.x>i.position.x and unit.position.x-1.5<i.position.x )and abs(unit.position.y-i.position.y)<=3): if(game.level.tiles[int(i.position.x )][int(i.position.y-3.8)] == model.Tile.EMPTY): debug.draw(model.CustomData.Log("jump")) jump=True else: jump=False #print(len(game.units)) approach=approachingbullet() if approach is not None: if(help(approach) is not None): jump,velocity=help(approach) debug.draw(model.CustomData.Log("approach")) elif(nearest_bullet is not None): if(help(nearest_bullet) is not None): jump,velocity=help(nearest_bullet) debug.draw(model.CustomData.Log("NearestBullet")) debug.draw(model.CustomData.Log("Velocity: {}".format(velocity))) #debug.draw(model.CustomData.Log("Target: {}".format(target_pos))) if nearest_enemy is not None: #if(nearest_enemy.walked_right==True): if velocity!=0: aim = model.Vec2Double(nearest_enemy.position.x- unit.position.x,nearest_enemy.position.y - unit.position.y) else: aim = model.Vec2Double(nearest_enemy.position.x- unit.position.x,nearest_enemy.position.y - unit.position.y) return model.UnitAction( velocity=velocity, jump=jump, jump_down=not jump, aim=aim, shoot=shoot, reload=reloadweapon, swap_weapon=swap, plant_mine=plant)
def get_action(self, unit, game, debug): # Replace this code with your own def distance_sqr(a, b): return (a.x - b.x)**2 + (a.y - b.y)**2 nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) nearest_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) nearest_health = min( filter(lambda box: isinstance(box.item, model.Item.HealthPack), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) target_pos = unit.position #Verifica se ja esta com #Alguma arma, se não tiver #Ira em direção a arma #Se ja tiver uma arma, ira em direção #Ao Inimigo procurando_inimigo = False desviar_de_bullet = False if unit.weapon is None and nearest_weapon is not None: target_pos = nearest_weapon.position elif unit.health < 90 and nearest_health is not None: target_pos = nearest_health.position #Se tiver outro pacote mais perto de mim #do que do inimigo, ira atraz dele #Pega o Objeto inimigo for j in game.units: if unit.id != j.id: inimigo = j print("BEGIN FOR") health_mais_perto_de_mim = [] #Itera em todos os pacotes for i in game.loot_boxes: #Se o pacote for de vida if isinstance(i.item, model.Item.HealthPack): #print("loots.item:" , i) #To mais perto que o inimigo #Da vida if distance_sqr(i.position, unit.position) < distance_sqr( i.position, inimigo.position): health_mais_perto_de_mim.append(i) print("TODOS PERTO DE MIM:", health_mais_perto_de_mim) if len(health_mais_perto_de_mim) > 0: target_pos = min(health_mais_perto_de_mim, key=lambda h_m_p_de_m: distance_sqr( h_m_p_de_m.position, unit.position), default=None).position #target_pos = health_mais_perto_de_mim[0].position print("MAIS PERTO DE MIM:", target_pos) #############3#elif unit.weapon is not None and unit.weapon.typ == 2: elif unit.weapon is not None and unit.weapon.typ == 0 or unit.weapon.typ == 1: if nearest_weapon is not None: target_pos = nearest_weapon.position elif len(game.bullets) > 0: #Pega o Objeto inimigo for j in game.units: if unit.id != j.id: inimigo = j lista_bullet_inimigo = list( filter(lambda bull: (bull.unit_id == inimigo.id), game.bullets)) for i in lista_bullet_inimigo: print("INIMIGO BULLET:", i) if len(lista_bullet_inimigo): #nearest_bullet = min( game.bullets, key=lambda bull: distance_sqr(bull.position, unit.position) ) nearest_bullet = min(lista_bullet_inimigo, key=lambda bull: distance_sqr( bull.position, unit.position)) print("ALL BULLETS:", game.bullets) print("MIN BULLETS:", nearest_bullet) target_pos = nearest_bullet.position desviar_de_bullet = True elif nearest_enemy is not None: target_pos = nearest_enemy.position procurando_inimigo = True print("POS:", target_pos) debug.draw(model.CustomData.Log("Txarget pos: {}".format(target_pos))) aim = model.Vec2Double(0, 0) if nearest_enemy is not None: aim = model.Vec2Double(nearest_enemy.position.x - unit.position.x, nearest_enemy.position.y - unit.position.y) #print("AIM:", aim) print("unit.position.x:", unit.position.x) print("unit.position.y:", unit.position.y) jump = target_pos.y > unit.position.y #Caso chegue em uma parede, ele pula if target_pos.x > unit.position.x and game.level.tiles[int( unit.position.x + 1)][int(unit.position.y)] == model.Tile.WALL: jump = True if target_pos.x < unit.position.x and game.level.tiles[int( unit.position.x - 1)][int(unit.position.y)] == model.Tile.WALL: jump = True #posição do alvo maior que a posição minha atual shooting_command = True if nearest_enemy.position.x > unit.position.x: for i in range(int(unit.position.x), int(nearest_enemy.position.x), 1): if game.level.tiles[i][int( unit.position.y)] == model.Tile.WALL: shooting_command = False print("NOOO SHOOTING MAN") break elif nearest_enemy.position.x < unit.position.x: for i in range(int(nearest_enemy.position.x), int(unit.position.x), 1): if game.level.tiles[i][int( unit.position.y)] == model.Tile.WALL: shooting_command = False print("NO NO NO NO SHOOTING MAN") break #Troca de Arma #Se tiver com uma ROCKET_LAUNCHER troca, por qualquer outra troca_arma = MyStrategy.troca_arma(unit) velocidade_deslocamento = MyStrategy.aumentar_velocidade( target_pos.x - unit.position.x) if desviar_de_bullet == True: velocidade_deslocamento *= -1 jump = not jump jump_down = not jump skill_plat_form = MyStrategy.skill_up_platform(unit, target_pos, game, jump) if skill_plat_form == True: jump = False jump_down = False return model.UnitAction( velocity=velocidade_deslocamento, jump=jump, jump_down=jump_down, aim=aim, shoot=shooting_command, reload=False, swap_weapon=troca_arma, #plant_mine=plat_mine_command) plant_mine=False)
def get_action(self, unit, game, debug): def distance_sqr(a, b): return (a.x - b.x)**2 + (a.y - b.y)**2 # Find the nearest enemy unit nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) # Find the best weapon: the nearest one best_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) # Calculate the position in the next tick if unit.weapon is None and best_weapon is not None: target_pos = best_weapon.position elif nearest_enemy is not None: target_pos = nearest_enemy.position else: target_pos = unit.position ''' if unit.weapon is None and best_weapon is not None: target_pos = best_weapon.position elif nearest_enemy is not None: target_pos = nearest_enemy.position ''' debug.draw(model.CustomData.Log("Target pos: {}".format(target_pos))) # Calculate the aiming direction aim = model.Vec2Double(0, 0) if nearest_enemy is not None: aim = model.Vec2Double(nearest_enemy.position.x - unit.position.x, nearest_enemy.position.y - unit.position.y) # Decide whether the unit should jump jump = target_pos.y > unit.position.y if target_pos.x > unit.position.x and game.level.tiles[int( unit.position.x + 1)][int(unit.position.y)] == model.Tile.WALL: jump = True if target_pos.x < unit.position.x and game.level.tiles[int( unit.position.x - 1)][int(unit.position.y)] == model.Tile.WALL: jump = True # Decide whether the unit should shoot vector_len = (aim.x**2 + aim.y**2)**0.5 unit_aim = model.Vec2Double(aim.x / vector_len, aim.y / vector_len) start_x = min(unit.position.x, nearest_enemy.position.x) end_x = max(unit.position.x, nearest_enemy.position.x) start_y = min(unit.position.y, nearest_enemy.position.y) end_y = max(unit.position.y, nearest_enemy.position.y) shoot = True cur_x, cur_y = unit.position.x, unit.position.y while start_x <= cur_x <= end_x and start_y <= cur_y <= end_y: if game.level.tiles[int(cur_x)][int(cur_y)] != model.Tile.EMPTY: shoot = False break else: cur_x += unit_aim.x cur_y += unit_aim.y unit_action = model.UnitAction(velocity=target_pos.x - unit.position.x, jump=jump, jump_down=not jump, aim=aim, shoot=shoot, reload=False, swap_weapon=False, plant_mine=False) return unit_action
def who(self, w): if w == "nearest_enemy": self.target = self.eye.search_enemy(None, None, None)[0].position self.aim = model.Vec2Double(self.target.x - self.unit.position.x, self.target.y - self.unit.position.y)
def get_action(self, unit, game, debug): # Replace this code with your own if len(game.units) > 2: nearest_friend = max( filter(lambda u: u.player_id == unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) else: nearest_friend = None nearest_enemy = min( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) nearest_enemy2 = max( filter(lambda u: u.player_id != unit.player_id, game.units), key=lambda u: distance_sqr(u.position, unit.position), default=None) nearest_weapon = None if nearest_friend: if nearest_friend.id > unit.id: nearest_weapon = min(filter( lambda box: isinstance(box.item, model.Item.Weapon) and box .item.weapon_type != model.WeaponType.ROCKET_LAUNCHER, game.loot_boxes), key=lambda box: distance_sqr( box.position, unit.position), default=None) else: nearest_weapon = min(filter( lambda box: isinstance(box.item, model.Item.Weapon) and box .item.weapon_type == model.WeaponType.ROCKET_LAUNCHER, game.loot_boxes), key=lambda box: distance_sqr( box.position, unit.position), default=None) if not nearest_weapon: nearest_weapon = min( filter(lambda box: isinstance(box.item, model.Item.Weapon), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) nearest_hp = min( filter(lambda box: isinstance(box.item, model.Item.HealthPack), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) nearest_hp2 = max( filter(lambda box: isinstance(box.item, model.Item.HealthPack), game.loot_boxes), key=lambda box: distance_sqr(box.position, unit.position), default=None) target_pos = unit.position if unit.health < 80 and nearest_hp: debug.draw(model.CustomData.Log("target: xp")) target_pos = nearest_hp.position if nearest_enemy: d1 = distance_sqr_u(unit, nearest_hp) d2 = distance_sqr_u(nearest_enemy, nearest_hp) if d2 < d1: target_pos = nearest_hp2.position debug.draw(model.CustomData.Log("target: xp2")) elif unit.weapon is None: debug.draw(model.CustomData.Log("target: initial weapon")) target_pos = nearest_weapon.position elif nearest_weapon and unit.weapon and unit.weapon.typ == model.WeaponType.ROCKET_LAUNCHER: debug.draw(model.CustomData.Log("target: weapon")) target_pos = nearest_weapon.position elif nearest_enemy: debug.draw(model.CustomData.Log("target: enemy")) target_pos = nearest_enemy.position pos = one_step_to(game, unit, target_pos) if pos: target_pos = pos debug.draw(model.CustomData.Log("Target pos: {}".format(target_pos))) debug.draw(model.CustomData.Log("Unit pos: {}".format(unit.position))) jump = target_pos.y > unit.position.y dx = target_pos.x - unit.position.x if dx > 0.5 and game.level.tiles[int(unit.position.x + 1)][int( unit.position.y)] == model.Tile.WALL: jump = True if dx < -0.5 and game.level.tiles[int(unit.position.x - 1)][int( unit.position.y)] == model.Tile.WALL: jump = True if nearest_enemy: dx = (nearest_enemy.position.x - unit.position.x - math.copysign(1, target_pos.x - unit.position.x)) print(dx) if abs(dx) < 1: jump = True if nearest_friend and nearest_friend.id < unit.id: jump = False if nearest_friend and nearest_friend.id > unit.id: dx = (nearest_friend.position.x - unit.position.x - math.copysign(1, target_pos.x - unit.position.x)) dy = (nearest_friend.position.y - unit.position.y) if abs(dx) < 1.5 and abs(dy) < 1.5: jump = True shoot = True if nearest_friend: if nearest_enemy and on_fire_line(unit, nearest_enemy, nearest_friend, game): nearest_enemy = nearest_enemy2 if nearest_enemy and on_fire_line(unit, nearest_enemy, nearest_friend, game): shoot = False else: if on_fire_line(unit, nearest_enemy, unit, game): nearest_enemy = nearest_enemy2 if on_fire_line(unit, nearest_enemy, unit, game): shoot = False aim = model.Vec2Double(0, 0) if nearest_enemy is not None: aim = model.Vec2Double(nearest_enemy.position.x - unit.position.x, nearest_enemy.position.y - unit.position.y) direction = 1 for u in game.units: if u.player_id != unit.player_id or True: if u.weapon and u.weapon.typ == model.WeaponType.ROCKET_LAUNCHER: if distance_sqr(u.position, unit.position) < 150: direction = -1 if nearest_friend and nearest_friend.id < unit.id: direction = 1 break velocity = direction * 20 * (target_pos.x - unit.position.x) print('vel', velocity, random.randint(-10, 10) / 10) return model.UnitAction( velocity=direction * 20 * (target_pos.x - unit.position.x) + random.randint(-10, 10) / 10, jump=jump, jump_down=not jump, aim=aim, shoot=shoot, reload=False, swap_weapon=(unit.weapon and (unit.weapon.typ == model.WeaponType.ROCKET_LAUNCHER)), plant_mine=(nearest_friend and unit.health < 20))