Skip to content

Commit

Permalink
feat: Added Grid generation (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurik244 authored May 18, 2024
1 parent 7313610 commit 2e1369f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
with:
options: "--check --verbose --exclude 'rustplus/api/remote/camera/camera_constants.py'"
src: "./rustplus"
version: "~= 22.0"
version: "~= 24.4.2"
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ requests
numpy
scipy
betterproto==2.0.0b6
shlex
16 changes: 10 additions & 6 deletions rustplus/api/base_rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,23 @@ async def connect(
:return: None
"""
EventLoopManager.set_loop(
self.event_loop
if self.event_loop is not None
else asyncio.get_event_loop(),
(
self.event_loop
if self.event_loop is not None
else asyncio.get_event_loop()
),
self.server_id,
)

if not self.use_test_server:
ServerChecker(self.server_id.ip, self.server_id.port).run()

EventLoopManager.set_loop(
self.event_loop
if self.event_loop is not None
else asyncio.get_event_loop(),
(
self.event_loop
if self.event_loop is not None
else asyncio.get_event_loop()
),
self.server_id,
)

Expand Down
8 changes: 5 additions & 3 deletions rustplus/api/remote/camera/camera_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ async def _create_frame(
last_packet.vertical_fov,
self._cam_info_message.far_plane,
entity_render_distance,
max_entity_amount
if max_entity_amount is not None
else len(self._last_packets.get_last().entities),
(
max_entity_amount
if max_entity_amount is not None
else len(self._last_packets.get_last().entities)
),
)

async def get_frame(
Expand Down
6 changes: 3 additions & 3 deletions rustplus/api/remote/camera/camera_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ def process_rays_batch(self) -> bool:
] = (208, 230, 252)
distance = float("inf")

self.depth_output[
x : x + self.scale_factor, y : y + self.scale_factor
] = distance
self.depth_output[x : x + self.scale_factor, y : y + self.scale_factor] = (
distance
)

return False

Expand Down
6 changes: 3 additions & 3 deletions rustplus/api/remote/events/handler_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def get_handlers(
class EntityHandlerList(HandlerList):
def __init__(self) -> None:
super().__init__()
self._handlers: Dict[
ServerID, Dict[str, Set[RegisteredListener]]
] = defaultdict(dict)
self._handlers: Dict[ServerID, Dict[str, Set[RegisteredListener]]] = (
defaultdict(dict)
)

def unregister(self, listener: RegisteredListener, server_id: ServerID) -> None:
if listener.listener_id in self._handlers.get(server_id):
Expand Down
13 changes: 3 additions & 10 deletions rustplus/api/rust_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
convert_monument,
translate_id_to_stack,
deprecated,
generate_grid,
)


Expand Down Expand Up @@ -205,17 +206,9 @@ async def get_map(
game_map = image.resize((map_size, map_size), Image.LANCZOS).convert("RGBA")

if add_grid:
grid = (
Image.open(
requests.get(
f"https://files.rustmaps.com/grids/{map_size}.png", stream=True
).raw
)
.resize((map_size, map_size), Image.LANCZOS)
.convert("RGBA")
)
grid = generate_grid(map_size)

game_map.paste(grid, (0, 0), grid)
game_map.paste(grid, (5, 5), grid)

if add_icons or add_events or add_vending_machines:
map_markers = (
Expand Down
49 changes: 41 additions & 8 deletions rustplus/utils/rust_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from importlib import resources
from typing import Tuple
from PIL import Image
from PIL import Image, ImageDraw, ImageFont
import logging
import string

from ..api.remote.rustplus_proto import AppMessage
from ..api.structures import RustTime

icons_path = "rustplus.api.icons"
ICONS_PATH = "rustplus.api.icons"
FONT_PATH = "rustplus.utils.fonts"
GRID_DIAMETER = 146.28571428571428


Expand Down Expand Up @@ -60,21 +61,21 @@ def convert_marker(name, angle) -> Image.Image:
"8": "patrol.png",
}

with resources.path(icons_path, name_to_file[name]) as path:
with resources.path(ICONS_PATH, name_to_file[name]) as path:
icon = Image.open(path).convert("RGBA")
if name == "6":
icon = icon.resize((85, 85))
elif name == "2":
icon = icon.resize((96, 96))
elif name == "4":
with resources.path(icons_path, "chinook_blades.png") as path:
with resources.path(ICONS_PATH, "chinook_blades.png") as path:
blades = Image.open(path).convert("RGBA")
blades = blades.resize((100, 100))
icon.paste(blades, (64 - 50, 96 - 50), blades)
icon.paste(blades, (64 - 50, 32 - 50), blades)
elif name == "8":
icon = icon.resize((200, 200))
with resources.path(icons_path, "chinook_blades.png") as path:
with resources.path(ICONS_PATH, "chinook_blades.png") as path:
blades = Image.open(path).convert("RGBA")
blades = blades.resize((200, 200))
icon.paste(blades, (0, 0), blades)
Expand Down Expand Up @@ -129,16 +130,16 @@ def convert_monument(name: str, override_images: dict) -> Image.Image:

if name in name_to_file:
file_name = name_to_file[name]
with resources.path(icons_path, file_name) as path:
with resources.path(ICONS_PATH, file_name) as path:
icon = Image.open(path).convert("RGBA")
elif "swamp" in name:
with resources.path(icons_path, "swamp.png") as path:
with resources.path(ICONS_PATH, "swamp.png") as path:
icon = Image.open(path).convert("RGBA")
else:
logging.getLogger("rustplus.py").info(
f"{name} - Has no icon, report this as an issue"
)
with resources.path(icons_path, "icon.png") as path:
with resources.path(ICONS_PATH, "icon.png") as path:
icon = Image.open(path).convert("RGBA")
return icon

Expand Down Expand Up @@ -228,3 +229,35 @@ def convert_xy_to_grid(
grid_pos_number = str(int(_get_grid_y(coords[1], corrected_map_size)))

return HackyBackwardsCompatCoordClass(grid_pos_letters, grid_pos_number)


def generate_grid(
map_size: int,
text_size: int = 20,
text_padding: int = 5,
color: str = "black",
) -> Image.Image:
img = Image.new("RGBA", (map_size, map_size), (0, 0, 0, 0))
d = ImageDraw.Draw(img)

with resources.path(FONT_PATH, "PermanentMarker.ttf") as path:
font = ImageFont.truetype(str(path), text_size)

letters = list(string.ascii_uppercase)
letters.extend(
a + b for a in string.ascii_uppercase for b in string.ascii_uppercase
)

num_cells = int(map_size / GRID_DIAMETER)

for i in range(num_cells):
for j in range(num_cells):
start = (i * GRID_DIAMETER, j * GRID_DIAMETER)
end = ((i + 1) * GRID_DIAMETER, (j + 1) * GRID_DIAMETER)
d.rectangle((start, end), outline=color)

text = letters[i] + str(j)
text_pos = (start[0] + text_padding, start[1] + text_padding)
d.text(text_pos, text, fill=color, font=font)

return img

0 comments on commit 2e1369f

Please sign in to comment.