def display_path(path, points, to, env): for i in range(len(path) - 1): edge = rendering.PolyLine( [points[path[i]].to_list(), points[path[i + 1]].to_list()], False) edge.set_color(0, 128, 0) env.viewer.add_onetime(edge) edge = rendering.PolyLine( [points[path[len(path) - 1]].to_list(), to.to_list()], False) edge.set_color(0, 128, 0) env.viewer.add_onetime(edge)
def display_edges(points, env, onetime=True): graph = env.master_network edges = list(graph.edges) # points = env.network_points edges = [(points[e[0]].to_list(), points[e[1]].to_list()) for e in edges] if env.rendering: for e in edges: edge = rendering.PolyLine([e[0], e[1]], False) if onetime: env.viewer.add_onetime(edge) else: env.viewer.add_geom(edge)
def set_health_bar(self, rec, viewer=None): self.bar = rec if len(self.robots) == 1: self.robots[0].health_bar = rec if viewer: viewer.add_geom( rendering.PolyLine([p.to_list() for p in rec.vertices], True)) else: left_middle = rec.vertices[0].midpoint(rec.vertices[3]) self.robots[0].health_bar = type(rec)(rec.vertices[0], \ rec.width, rec.height/2) self.robots[1].health_bar = type(rec)(left_middle, \ rec.width, rec.height/2) if viewer: viewer.add_geom( rendering.PolyLine([ p.to_list() for p in self.robots[0].health_bar.vertices ], True)) viewer.add_geom( rendering.PolyLine([ p.to_list() for p in self.robots[1].health_bar.vertices ], True))
def init_rendering(self): self.viewer = rendering.Viewer(self.width, self.height) boundary = rendering.PolyLine([(1, 0), (1, 499), (800, 499), (800, 0)], True) self.viewer.add_geom(boundary) health_bar_params = (20, 260) self.my_team.set_health_bar(UprightRectangle(Point(10, self.height / 2 - health_bar_params[1] / 2), \ health_bar_params[0], health_bar_params[1]), self.viewer) self.enemy_team.set_health_bar(UprightRectangle(Point(self.width - health_bar_params[0] - 10, \ self.height / 2 - health_bar_params[1] / 2), health_bar_params[0], health_bar_params[1]), self.viewer) for char in self.inactables(): geoms = char.render() for geom in geoms: self.viewer.add_geom(geom)
def __init__(self): # Record time self.game_time = 0 self.finished = False self.characters = { "obstacles": [], "zones": [], "robots": [], "bullets": [], } # Initialize teams BLUE = Team("BLUE") RED = Team("RED") BLUE.enemy, RED.enemy = RED, BLUE self.my_team, self.enemy_team = BLUE, RED # Initialize robots myRobot = AttackRobot(self, BLUE, Point(170, 295), 0) enemyRobot = ManualControlRobot("OSPWADBR", self, RED, Point(250, 110), 0) myRobot.load(5) enemyRobot.load(40) self.characters['robots'] = [myRobot, enemyRobot] for i in range(len(self.characters['robots'])): self.characters['robots'][i].id = i # Defining course obstacles self.characters['obstacles'] = [ Obstacle(p[0], p[1], p[2]) for p in [(Point(325, 0), 25, 100), ( Point(450, 400), 25, 100), (Point(350, 238), 100, 25), (Point(580, 100), 100, 25), (Point(120, 375), 100, 25), (Point(140, 140), 25, 100), (Point(635, 260), 25, 100)] ] # Team start areas self.startingZones = [ StartingZone(p[0], p[1]) for p in [(Point(0, 0), BLUE), (Point(700, 0), BLUE), (Point(0, 400), RED), (Point(700, 400), RED)] ] self.defenseBuffZones = [ DefenseBuffZone(p[0], p[1]) for p in [(Point(120, 275), BLUE), (Point(580, 125), RED)] ] self.loadingZones = [ LoadingZone(p[0], p[1]) for p in [(Point(350, 0), RED), (Point(350, 400), BLUE)] ] self.characters['zones'] = self.startingZones + self.defenseBuffZones + \ self.loadingZones # self.background = Rectangle(Point(0, 0), self.width, self.height, 0) self.viewer = rendering.Viewer(self.width, self.height) boundary = rendering.PolyLine([(1, 0), (1, 499), (800, 499), (800, 0)], True) boundary.set_color(COLOR_BLACK[0], COLOR_BLACK[1], COLOR_BLACK[2]) self.viewer.add_geom(boundary) for char in self.characters['obstacles'] + self.characters['zones']: geoms = char.render() for geom in geoms: self.viewer.add_geom(geom)
def __init__(self): # initialize robot movement parameters Move.ticks_until_astar_recalc = 25 delta = 40 # offset of graph points from wall corners # delta = ((((Robot.width / 2) ** 2) + ((Robot.height / 2) ** 2))) ** .5 + 1 #radius of robot + 1 (divide by 2 is deliberate) # Record time self.game_time = 0 self.finished = False self.characters = { "obstacles": [], "zones": [], "robots": [], "bullets": [], } # Initialize teams BLUE = Team("BLUE", self.pygame_rendering) RED = Team("RED", self.pygame_rendering) BLUE.enemy, RED.enemy = RED, BLUE self.my_team, self.enemy_team = BLUE, RED # Initialize robots # my_robot = AttackRobot(self, BLUE, Point(780, 100), 135) my_robot = AttackRobot(self, BLUE, Point(780, 100), 135) enemy_robot = AttackRobot(self, RED, Point(50, 450), 0) # enemy_robot = AttackRobot("ASDWOPR", self, RED, Point(50, 450), 0, ignore_angle=True) # bugged spot with closest point unreachable # my_robot = AttackRobot(self, BLUE, Point(365.917389, 355.968720), 312.700132) # enemy_robot = KeyboardRobot("ASDWOPR", self, RED, Point(419.475727, 80.330731), 132.700132, ignore_angle=True) my_robot.load(40) enemy_robot.load(40) self.characters['robots'] = [my_robot, enemy_robot] # my_robot2 = AttackRobot(self, BLUE, Point(20, 100), 0) # enemy_robot2 = AttackRobot(self, RED, Point(780, 450), 180) # self.characters['robots'] += [my_robot2, enemy_robot2] for i in range(len(self.characters['robots'])): self.characters['robots'][i].id = i # Defining course obstacles self.characters['obstacles'] = [ Obstacle(p[0], p[1], p[2]) for p in [(Point(325, 0), 25, 100), ( Point(450, 400), 25, 100), (Point(350, 238), 100, 25), (Point(580, 100), 100, 25), (Point(120, 375), 100, 25), (Point(140, 140), 25, 100), (Point(635, 260), 25, 100)] ] # Team start areas self.starting_zones = [ StartingZone(p[0], p[1]) for p in [(Point(0, 0), BLUE), (Point(700, 0), BLUE), (Point(0, 400), RED), (Point(700, 400), RED)] ] self.defense_buff_zones = [ DefenseBuffZone(p[0], p[1], self) for p in [(Point(120, 275), BLUE), (Point(580, 125), RED)] ] self.loading_zones = [ LoadingZone(p[0], p[1], self) for p in [(Point(350, 400), BLUE), (Point(350, 0), RED)] ] self.characters['zones'] = self.starting_zones + self.defense_buff_zones + \ self.loading_zones if self.rendering: self.init_rendering() elif self.pygame_rendering: self.init_pygame_rendering() # Init movement network G = nx.Graph() # delta declaration at the top of this function self.network_points = [] id = 0 for block in self.characters['obstacles'] + [ self.enemy_team.loading_zone ]: vertices = block.get_vertices() delta_bases = [(-1, -1), (1, -1), (1, 1), (-1, 1)] for i in range(4): delta_x, delta_y = delta_bases[ i] # abs(i - 1.5) // 1.5 * 2 - 1, i // 2 * 2 - 1 delta_x *= delta delta_y *= delta point = vertices[i].move(delta_x, delta_y) if self.is_legal(point): self.network_points.append(point) G.add_node(id) point.id = id id += 1 self.network_edges = [] for i in range(len(self.network_points)): for j in range(i + 1, len(self.network_points)): p_i, p_j = self.network_points[i], self.network_points[j] if p_i.dis(p_j) <= 250 and self.direct_reachable_forward( p_i, p_j, my_robot, ignore_robots=True): self.network_edges.append((p_i, p_j)) G.add_edge(p_i.id, p_j.id, weight=p_i.dis(p_j)) # remove edges that go through enemy loading zone (assign valid edges to appropriate team) # RED.extra_weighted_edge = (0, 8, G[0][8]['weight']) #TODO # BLUE.extra_weighted_edge = (2, 14, G[2][14]['weight']) #TODO # G.remove_edge(0, 8) #TODO # G.remove_edge(2, 14) #TODO self.master_network = G if self.display_visibility_map: for e in self.network_edges: edge = rendering.PolyLine([e[0].to_list(), e[1].to_list()], False) self.viewer.add_geom(edge) for p in self.network_points: geom = rendering.Circle(p, 5) self.viewer.add_geom(geom)
def seed(self, seed=None): self.np_random, seed = seeding.np_random(seed) return [seed] def __init__(self): # Record time self.game_time = 0 self.finished = False """ State: [0]: game_time. Total number of steps elapsed. Equivalent to game_time/50 seconds [1]: number of robots on each team [2-41]: state of 4 robots. non-existant robots are 0 padded. 9 numbers each [2-3]: x, y coord of center of robot [4]: robot angle in degrees [5]: robot gun angle relative to front in degrees [6]: bullet count [7]: remaining number of rounds of shooting cooldown [8]: remaining number of rounds of defense buff [9]: flag=1 if robot is shooting 0 otherwise [10]: remaining health [11]: robot type index [42-51]: state of two defense zones. [42]: flag=1 if can be activated 0 otherwise [43-46]: number of seconds the zone has been touched by each robot [52-57]: state of two loading zones. [52]: number of refills available [53]: number of bullets not yet poured from previous refill [54]: number of bullets loaded into the current robot [58-297]: state of 60 bullets. [58-59]: x, y coord of bullet [60]: direction of bullet in radian [61]: id of bullet's master robot """ self.characters = { "obstacles": [], "zones": [], "robots": [], "bullets": [], } action_space: The Space object corresponding to valid actions observation_space: The Space object corresponding to valid observations reward_range: A tuple corresponding to the min and max possible rewards # Initialize teams BLUE = Team("BLUE", self.pygame_rendering) RED = Team("RED", self.pygame_rendering) BLUE.enemy, RED.enemy = RED, BLUE self.my_team, self.enemy_team = BLUE, RED # Initialize robots my_robot = AttackRobot(self, BLUE, Point(780, 100), 180) # my_robot = AttackRobot(self, BLUE, Point(780, 100), 180) my_robot2 = AttackRobot(self, BLUE, Point(20, 100), 0) enemy_robot = KeyboardRobot("ASDWOPR", self, RED, Point(50, 450), 0) enemy_robot2 = AttackRobot(self, RED, Point(780, 450), 180) # enemy_robot = JoystickRobot(self, RED, Point(50, 450), 0) # my_robot.load(40) enemy_robot.load(40) self.characters['robots'] = [my_robot, enemy_robot] self.characters['robots'] += [my_robot2, enemy_robot2] for i in range(len(self.characters['robots'])): self.characters['robots'][i].id = i # Defining course obstacles self.characters['obstacles'] = [Obstacle(p[0], p[1], p[2]) for p in [(Point(325, 0), 25, 100), (Point(450, 400), 25, 100), (Point(350, 238), 100, 25), (Point(580, 100), 100, 25), (Point(120, 375), 100, 25), (Point(140, 140), 25, 100), (Point(635, 260), 25, 100)]] # Team start areas self.starting_zones = [StartingZone(p[0], p[1]) for p in [(Point(0, 0), BLUE), (Point(700, 0), BLUE), (Point(0, 400), RED), (Point(700, 400), RED)]] self.defense_buff_zones = [DefenseBuffZone(p[0], p[1], self) for p in [ (Point(120, 275), BLUE), (Point(580, 125), RED)]] self.loading_zones = [LoadingZone(p[0], p[1], self) for p in [ (Point(350, 400), BLUE), (Point(350, 0), RED)]] self.characters['zones'] = self.starting_zones + self.defense_buff_zones + \ self.loading_zones if self.rendering: self.init_rendering() elif self.pygame_rendering: self.init_pygame_rendering() # Init movement network G = nx.Graph() delta = 40 self.network_points = [] id = 0 for block in self.characters['obstacles'] + [self.enemy_team.loading_zone]: for i in range(4): delta_x, delta_y = abs(i - 1.5) // 1.5 * 2 - 1, i // 2 * 2 - 1 delta_x *= -delta delta_y *= delta point = block.vertices[i].move(delta_x, delta_y) if self.is_legal(point): self.network_points.append(point) G.add_node(id) point.id = id id += 1 self.network_edges = [] for i in range(len(self.network_points)): for j in range(i + 1, len(self.network_points)): p_i, p_j = self.network_points[i], self.network_points[j] if self.direct_reachable_forward(p_i, p_j, my_robot, True): self.network_edges.append(LineSegment(p_i, p_j)) G.add_edge(p_i.id, p_j.id, weight=p_i.dis(p_j)) self.network = G if self.display_visibility_map: for e in self.network_edges: edge = rendering.PolyLine([e.point_from.to_list(), e.point_to.to_list()], False) self.viewer.add_geom(edge) for p in self.network_points: geom = rendering.Circle(p, 5) self.viewer.add_geom(geom)