Skip to content

Commit

Permalink
Get automatic monster sprite handling working
Browse files Browse the repository at this point in the history
  • Loading branch information
ExcaliburZero committed Oct 7, 2023
1 parent 95ed16a commit b6adbe0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
19 changes: 19 additions & 0 deletions cbpickaxe/hoylake.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,25 @@ def load_moves(self, path: str) -> Dict[str, Move]:

return moves

def lookup_filepath(self, path: str) -> pathlib.Path:
"""
Returns a real filesystem path to the file at the given res:// path.
If there is no file at that location in any of the loaded root directories, then a
ValueError will be raised.
"""
self.__check_if_root_loaded()

relative_path = Hoylake.__parse_res_path(path)

for root in self.__roots:
filepath = root / relative_path

if filepath.exists():
return filepath

raise ValueError(f"Could not find file at path: {path}")

def translate(self, string: str, locale: str = "en") -> str:
"""
Translates the given string to the specified locale. Locale defaults to English (en).
Expand Down
56 changes: 54 additions & 2 deletions cbpickaxe_scripts/generate_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys

import jinja2 as j2
import PIL.Image

import cbpickaxe as cbp

Expand All @@ -30,7 +31,11 @@ def main(argv: List[str]) -> int:
parser.add_argument(
"--monster_form_paths",
nargs="+",
default=["res://data/monster_forms/", "res://data/monster_forms_secret/"],
default=[
"res://data/monster_forms/",
"res://data/monster_forms_secret/",
"res://mods/de_example_monster/traffikrabdos.tres",
],
)
parser.add_argument("--output_directory", default="docs")

Expand Down Expand Up @@ -62,13 +67,22 @@ def main(argv: List[str]) -> int:
monster_forms_dir = output_directory / "monsters"
monster_forms_dir.mkdir()

monster_form_images_dir = monster_forms_dir / "sprites"
monster_form_images_dir.mkdir()

monster_path, monster_form = sorted(monster_forms.items())[0]
monster_page_filepath = monster_forms_dir / (
hoylake.translate(monster_form.name) + ".html"
)
with open(monster_page_filepath, "w", encoding="utf-8") as output_stream:
create_monster_form_page(
monster_path, monster_form, hoylake, monster_form_template, output_stream
monster_path,
monster_form,
hoylake,
monster_form_template,
monster_forms_dir,
monster_form_images_dir,
output_stream,
)

return SUCCESS
Expand All @@ -79,14 +93,21 @@ def create_monster_form_page(
monster_form: cbp.MonsterForm,
hoylake: cbp.Hoylake,
template: j2.Template,
dest_dir: pathlib.Path,
images_dir: pathlib.Path,
output_stream: IO[str],
) -> None:
compatible_moves = hoylake.get_moves_by_tags(monster_form.move_tags + ["any"])

monster_sprite_filepath = get_idle_frame(
monster_form, hoylake, images_dir
).relative_to(dest_dir)

output_stream.write(
template.render(
name=hoylake.translate(monster_form.name),
bestiary_index=f"{monster_form.bestiary_index:03}",
monster_sprite_path=monster_sprite_filepath,
description=hoylake.translate(monster_form.description),
elemental_type=monster_form.elemental_types[0].capitalize(),
bestiary_bio_1=hoylake.translate(monster_form.bestiary_bios[0]),
Expand Down Expand Up @@ -129,5 +150,36 @@ def create_monster_form_page(
)


def get_idle_frame(
monster_form: cbp.MonsterForm,
hoylake: cbp.Hoylake,
images_dir: pathlib.Path,
) -> pathlib.Path:
animation = hoylake.load_animation(monster_form.battle_sprite_path)
frame_box = animation.get_frame("idle", 0).box

image_filepath_relative = (
"/".join(monster_form.battle_sprite_path.split("/")[:-1])
+ "/"
+ animation.image
)

image_filepath = hoylake.lookup_filepath(image_filepath_relative)
source_image = PIL.Image.open(image_filepath)

box = (
frame_box.x,
frame_box.y,
frame_box.x + frame_box.width,
frame_box.y + frame_box.height,
)
cropped_image = source_image.crop(box)

output_filepath = images_dir / (hoylake.translate(monster_form.name) + ".png")
cropped_image.save(output_filepath)

return output_filepath


def main_without_args() -> int:
return main(sys.argv[1:])
2 changes: 1 addition & 1 deletion cbpickaxe_scripts/templates/monster_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h1>{{ name }} (#{{ bestiary_index }})</h1>
<p>"{{ description }}"</p>
</div>
<div class="row justify-content-center">
<img src="../../Magikrab.png" alt="Sprite of {{ name }}"
<img src="{{ monster_sprite_path }}" alt="Sprite of {{ name }}"
style="image-rendering: pixelated; max-width: 200px; max-height: 200px; display: block; margin-left: auto; margin-right: auto;">
</div>
<div class="row justify-content-center">
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ install_requires =
godot_parser
smaz-py3
Jinja2
Pillow

[options.package_data]
cbpickaxe =
Expand Down

0 comments on commit b6adbe0

Please sign in to comment.