Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into dev
  • Loading branch information
Captainbleu committed Dec 13, 2024
2 parents 37e500a + bb5f131 commit 35c3884
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 39 deletions.
2 changes: 1 addition & 1 deletion common/arena/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
ZoneType,
EnemyZone,
StuffZone,
ForbiddenZone,
BlueReservedZone,
YellowReservedZone,
BorderZone,
ZoneNavigability,
)
from arena.arena import Arena
36 changes: 28 additions & 8 deletions common/arena/arena.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
BaseArenaZone,
EnemyZone,
StuffZone,
ForbiddenZone,
BlueReservedZone,
YellowReservedZone,
BorderZone,
ZoneNavigability,
)


Expand Down Expand Up @@ -93,7 +93,7 @@ def __init__(

# Add forbidden and border zones to the grid manager
for zone in zones:
if zone.zone_type in [ZoneType.FORBIDDEN, ZoneType.BORDER_ZONE]:
if zone.navigability == ZoneNavigability.FORBIDDEN:
self.grid_manager.add_forbidden_static_zone(zone.polygon)

# ====== Private Methods ======
Expand Down Expand Up @@ -149,24 +149,44 @@ def visualize(self, show_buffer: bool = True) -> None:
# Draw the arena boundary
arena_polygon = box(0, 0, self.width, self.height)
self.__plot_polygon(ax, arena_polygon, color="lightgrey", label="Arena")

seen_zone_types = set()
first = True
for zone in self.zones:
if show_buffer and zone.zone_type != ZoneType.BORDER_ZONE:
# Plot buffer zone in light red
buffer_polygon = self.__add_buffer_to_zone(
zone.polygon, self.obstacle_buffer
)
self.__plot_polygon(
ax,
buffer_polygon,
color="lightcoral",
label=f"{zone.zone_type.name} Buffer",
color="#E89393",
label=f"zone Buffer" if first else None,
)

# Plot the original zone in dark red
first = False
self.__plot_polygon(
ax, zone.polygon, color="darkred", label=f"{zone.zone_type.name} Zone"
ax,
zone.polygon,
color=zone.zone_color,
label=(
f"{zone.zone_type.name} Zone"
if zone.zone_type.name not in seen_zone_types
else None
),
)
seen_zone_types.add(zone.zone_type.name)

if zone.navigability == ZoneNavigability.FORBIDDEN:
x, y = zone.polygon.exterior.xy
ax.fill(
x, y, alpha=0.3, hatch="x", color=zone.zone_color, edgecolor="black"
)

elif zone.navigability == ZoneNavigability.RESTRICTED:
x, y = zone.polygon.exterior.xy
ax.fill(
x, y, alpha=0.3, hatch="/", color=zone.zone_color, edgecolor="black"
)

# Configure plot appearance
ax.set_xlim(0, self.width)
Expand Down
96 changes: 74 additions & 22 deletions common/arena/arena_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
class ZoneType(Enum):
"""Enumeration for different types of zones in the arena."""

DEFFAULT = auto()
YELLOW_RESERVED = auto()
BLUE_RESERVED = auto()
FORBIDDEN = auto()
Expand All @@ -32,62 +33,113 @@ class ZoneType(Enum):
BORDER_ZONE = auto()


class ZoneNavigability(Enum):
"""Enumeration for different types of zones in the arena."""

FREE = auto()
RESTRICTED = auto()
FORBIDDEN = auto()


# ====== Base Zone Class ======
class BaseArenaZone:
"""
Represents a general zone in the arena.
Represents a zone within an arena.
Attributes:
polygon (Polygon): The geometric representation of the zone.
zone_type (ZoneType): The type of the zone.
polygon (Polygon): The geometric shape of the zone.
zone_type (ZoneType): The type/category of the zone.
zone_color (str): The color representation of the zone. Default is "#742A2A".
navigability (ZoneNavigability): The navigability of the zone. Default is "FREE".
visited_opposant (int): Counter for how many times the zone has been visited by the opponent.
visited_self (int): Counter for how many times the zone has been visited by self.
Args:
polygon (Polygon): The geometric shape of the zone.
zone_type (ZoneType): The type/category of the zone.
navigability (ZoneNavigability): The navigability of the zone.
zone_color (str, optional): The color representation of the zone. Default is "#742A2A".
"""

def __init__(self, polygon: Polygon, zone_type: ZoneType) -> None:
def __init__(
self,
polygon: Polygon,
zone_type: ZoneType = ZoneType.DEFFAULT,
navigability: ZoneNavigability = ZoneNavigability.FREE,
zone_color: str = "#f0aef2",
) -> None:
self.polygon: Polygon = polygon
self.zone_type: ZoneType = zone_type
self.zone_color = zone_color
self.navigability: ZoneNavigability = navigability
self.visited_opposant = 0
self.visited_self = 0


# ====== Specific Zone Classes ======
class EnemyZone(BaseArenaZone):
"""Zone designated for enemies."""

def __init__(self, polygon: Polygon) -> None:
super().__init__(polygon, ZoneType.ENEMY)
def __init__(
self, polygon: Polygon, navigability: ZoneNavigability = ZoneNavigability.FREE
) -> None:
super().__init__(
polygon, ZoneType.ENEMY, navigability=navigability, zone_color="#EE950F"
)


class StuffZone(BaseArenaZone):
"""Zone designated for storage or stuff placement."""

def __init__(self, polygon: Polygon) -> None:
super().__init__(polygon, ZoneType.STUFF_ZONE)


class ForbiddenZone(BaseArenaZone):
"""Zone where access is restricted."""

def __init__(self, polygon: Polygon) -> None:
super().__init__(polygon, ZoneType.FORBIDDEN)
def __init__(
self, polygon: Polygon, navigability: ZoneNavigability = ZoneNavigability.FREE
) -> None:
super().__init__(
polygon,
ZoneType.STUFF_ZONE,
navigability=navigability,
zone_color="#0FEE9C",
)


class BlueReservedZone(BaseArenaZone):
"""Zone reserved for blue team operations."""

def __init__(self, polygon: Polygon) -> None:
super().__init__(polygon, ZoneType.BLUE_RESERVED)
def __init__(
self, polygon: Polygon, navigability: ZoneNavigability = ZoneNavigability.FREE
) -> None:
super().__init__(
polygon,
ZoneType.BLUE_RESERVED,
navigability=navigability,
zone_color="#097D8D",
)


class YellowReservedZone(BaseArenaZone):
"""Zone reserved for yellow team operations."""

def __init__(self, polygon: Polygon) -> None:
super().__init__(polygon, ZoneType.YELLOW_RESERVED)
def __init__(
self, polygon: Polygon, navigability: ZoneNavigability = ZoneNavigability.FREE
) -> None:
super().__init__(
polygon,
ZoneType.YELLOW_RESERVED,
navigability=navigability,
zone_color="#ECC92E",
)


class BorderZone(BaseArenaZone):
"""Zone representing the border of the arena."""

def __init__(self, polygon: Polygon) -> None:
super().__init__(polygon, ZoneType.BORDER_ZONE)
def __init__(
self, polygon: Polygon, navigability: ZoneNavigability = ZoneNavigability.FREE
) -> None:
super().__init__(
polygon,
ZoneType.BORDER_ZONE,
navigability=navigability,
zone_color="#EF0D0D",
)


# ====== Code Summary ======
Expand Down
4 changes: 3 additions & 1 deletion common/arena/grid_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from logger import Logger, LogLevels


# TODO - Handle Restricted Zones. Currently, only forbidden zones are handled. Restricted zones are zones where movement is restricted but not completely forbidden.
# First consider Restricted as obstacles but if no path is found, consider them as free zones.
# ====== GridManager Class ======
class GridManager:
"""
Expand Down Expand Up @@ -201,7 +203,7 @@ def get_static_grid(self) -> Grid:
"""Returns the static grid."""
return self.static_grid

def static_and_dynamic_grid(self) -> Grid:
def get_static_and_dynamic_grid(self) -> Grid:
"""Returns the combined static and dynamic grid."""
return self.static_and_dynamic_grid

Expand Down
31 changes: 24 additions & 7 deletions robot1/rasp/arena_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
ZoneType,
EnemyZone,
StuffZone,
ForbiddenZone,
BlueReservedZone,
YellowReservedZone,
BorderZone,
ZoneNavigability,
)

from pathfinding.core.grid import Grid, GridNode
Expand All @@ -60,12 +60,29 @@
border_buffer=2,
obstacle_buffer=5,
zones=[
ForbiddenZone(create_straight_rectangle(Point(45, 0), Point(0, 45))),
ForbiddenZone(create_straight_rectangle(Point(77.5, 0), Point(122.5, 45))),
ForbiddenZone(create_straight_rectangle(Point(155, 0), Point(200, 45))),
ForbiddenZone(create_straight_rectangle(Point(45, 255), Point(0, 155))),
ForbiddenZone(create_straight_rectangle(Point(77.5, 255), Point(122.5, 155))),
ForbiddenZone(create_straight_rectangle(Point(155, 255), Point(200, 155))),
BaseArenaZone(
create_straight_rectangle(Point(45, 0), Point(0, 45)),
navigability=ZoneNavigability.FORBIDDEN,
),
BaseArenaZone(
create_straight_rectangle(Point(77.5, 0), Point(122.5, 45)),
navigability=ZoneNavigability.FORBIDDEN,
),
BaseArenaZone(create_straight_rectangle(Point(155, 0), Point(200, 45))),
BaseArenaZone(
create_straight_rectangle(Point(45, 255), Point(0, 155)),
navigability=ZoneNavigability.RESTRICTED,
),
BaseArenaZone(create_straight_rectangle(Point(77.5, 255), Point(122.5, 155))),
BaseArenaZone(create_straight_rectangle(Point(155, 255), Point(200, 155))),
StuffZone(
create_straight_rectangle(Point(50, 50), Point(100, 100)),
navigability=ZoneNavigability.FORBIDDEN,
),
YellowReservedZone(create_straight_rectangle(Point(150, 50), Point(200, 100))),
BlueReservedZone(create_straight_rectangle(Point(50, 150), Point(100, 200))),
EnemyZone(create_straight_rectangle(Point(100, 100), Point(150, 150))),
EnemyZone(create_straight_rectangle(Point(10, 10), Point(50, 50))),
],
chunk_size=2,
)
Expand Down

0 comments on commit 35c3884

Please sign in to comment.