Skip to content

Commit

Permalink
Merge pull request #3 from holy-the-sea/craftables_fix
Browse files Browse the repository at this point in the history
Fix consecutive sprite replacements for craftables
  • Loading branch information
holy-the-sea authored Jan 22, 2023
2 parents 3768595 + 5d9a5b8 commit d19074d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 50 deletions.
24 changes: 13 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
print(f"Mod folder: {mod_folder_path}")
print(f"Keywords to use: {keywords}\n")

print("Making AT folder...")
if AT_folder_path.exists():
shutil.rmtree(AT_folder_path)
os.makedirs(AT_folder_path)
if not (AT_folder_path).exists():
os.makedirs(AT_folder_path)

print("Cleaning up Textures/ folder...")
# clean up after ourselves
if (mod_folder_path / "Textures").exists():
Expand All @@ -45,6 +38,14 @@
content_json = json5.loads(json_file.read())
print("Done.\n")

print("Making AT folder...")
if AT_folder_path.exists():
shutil.rmtree(AT_folder_path)
os.makedirs(AT_folder_path)
if not (AT_folder_path).exists():
os.makedirs(AT_folder_path)
print("Done.\n")

# check for ConfigSchema and DynamicTokens
print("Checking for ConfigSchema and DynamicTokens...")
config_schema_options = get_config_schema(content_json)
Expand All @@ -62,11 +63,13 @@
continue
except KeyError:
pass
#TODO: fix weather-specific skins?
try:
if any("weather" in key.lower() for key in change["When"]):
continue
except KeyError:
pass
#TODO: paste overlay images to create another texture
try:
if change["PatchMode"] == "Overlay":
continue
Expand Down Expand Up @@ -114,12 +117,11 @@

print("Creating new manifest...")
generate_new_manifest(mod_folder_path)
print("Done.")
print("Done.\n")

print("Moving files...")
shutil.move(mod_folder_path / "Textures", AT_folder_path / "Textures")
print("Done.")
print()
print("Done.\n")

print(f"Converted items: {', '.join(objects_replaced)}\n")
print(f"Finished conversion. Please check files in {AT_folder_path}.\n")
128 changes: 89 additions & 39 deletions src/craftables.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,34 @@

import json5
from PIL import Image, ImageChops
from src.file_names import get_file_path, get_file_variations, expand_target_variations
from src.file_names import expand_target_variations, get_file_path, get_file_variations
from src.generate_jsons import generate_texture_json

# TODO: do springobjects.png


def split_craftables_replacement(
tilesheet_coords, dimension_name, craftable_objects_info
):
tilesheet_X = tilesheet_coords["X"]
tilesheet_Y = tilesheet_coords["Y"]
section_width = tilesheet_coords["Width"]
sprite_X = tilesheet_X
if dimension_name == "Width":
object_list = []
while tilesheet_X <= sprite_X < tilesheet_X + section_width:
if (sprite_X, tilesheet_Y) not in craftable_objects_info.keys():
sprite_X += 16
else:
object_list.append(
craftable_objects_info[(sprite_X, tilesheet_Y)]["Object"]
)
sprite_X += 16
return object_list
if dimension_name == "Height":
raise NotImplementedError("lol i don't even know gl hf")


def convert_craftables(
change,
mod_folder_path,
Expand All @@ -26,7 +49,7 @@ def convert_craftables(
encoding="utf-8",
) as json_file:
craftable_objects_info = json5.loads(json_file.read())
craftable_objects_info = {
craftable_coords_info = {
(value["X"], value["Y"]): {
"Object": key,
"Height": value["Height"],
Expand Down Expand Up @@ -70,10 +93,32 @@ def convert_craftables(
tilesheet_coords = change["ToArea"]
tilesheet_X = tilesheet_coords["X"]
tilesheet_Y = tilesheet_coords["Y"]
object_name = craftable_objects_info[(tilesheet_X, tilesheet_Y)]["Object"]
if object_name == "Campfire_1":
object_name = "Cookout Kit"
print(f"Item replacement known: {object_name}")
try:
replacement_width = craftable_coords_info[(tilesheet_X, tilesheet_Y)][
"Width"
]
replacement_height = craftable_coords_info[(tilesheet_X, tilesheet_Y)][
"Height"
]
object_name = craftable_coords_info[(tilesheet_X, tilesheet_Y)]["Object"]
if object_name == "Campfire_1":
object_name = "Cookout Kit"
print(f"Item replacement known: {object_name}")
object_list = [object_name]
if (
tilesheet_coords["Width"] != replacement_width
or tilesheet_coords["Height"] != replacement_height
):
print("Consecutive objects in X-direction replaced, splitting...")
# TODO: implement here
object_list = split_craftables_replacement(
tilesheet_coords, "Width", craftable_coords_info
)
except KeyError:
print(
f"Couldn't find an object with the coordinates ({tilesheet_X}, {tilesheet_Y})"
)
return objects_replaced
else:
print(
"Item names not found from content.json, must do object identification by comparing sprites instead"
Expand All @@ -94,41 +139,46 @@ def convert_craftables(
else:
file_season = False

if "object_name" in locals():
new_file_path = get_file_path(
file, object_name, mod_folder_path, file_season
)
# * asset is an individual sprite
if "FromArea" not in change:
im.save(new_file_path)
image_variations.append(im)
# * asset was a tilesheet
else:
X = change["FromArea"]["X"]
Y = change["FromArea"]["Y"]
width = change["FromArea"]["Width"]
height = change["FromArea"]["Height"]
X_right = X + width
Y_bottom = Y + height
im = im.crop((X, Y, X_right, Y_bottom))
im.save(new_file_path)
image_variations.append(im)
print(f"Cropped {object_name} from {Path(file)}.\n")
# * update list of which objects we have replaced
objects_replaced[object_name] = image_variations
texture_json_path = Path(new_file_path).parent / "texture.json"
generate_texture_json(
texture_json_path,
object_name,
"Craftable",
16,
32,
keywords,
file_season,
)
if "object_list" in locals():
for object_name in object_list:
new_file_path = get_file_path(
file, object_name, mod_folder_path, file_season
)
# * asset is an individual sprite
if "FromArea" not in change:
im.save(new_file_path)
image_variations.append(im)
# * asset was a tilesheet
else:
# X = change["FromArea"]["X"]
# Y = change["FromArea"]["Y"]
# width = change["FromArea"]["Width"]
# height = change["FromArea"]["Height"]
X = craftable_objects_info[object_name]["X"]
Y = craftable_objects_info[object_name]["Y"]
width = craftable_objects_info[object_name]["Width"]
height = craftable_objects_info[object_name]["Height"]
X_right = X + width
Y_bottom = Y + height
im_cropped = im.crop((X, Y, X_right, Y_bottom))
im_cropped.save(new_file_path)
image_variations.append(im_cropped)
print(f"Cropped {object_name} from {Path(file)}.")
# * update list of which objects we have replaced
objects_replaced[object_name] = image_variations
texture_json_path = Path(new_file_path).parent / "texture.json"
generate_texture_json(
texture_json_path,
object_name,
"Craftable",
16,
32,
keywords,
file_season,
)
else:
print("Trying to identify...")
for coords, values in craftable_objects_info.items():
for coords, values in craftable_coords_info.items():
object_name = values["Object"]
if object_name == "Campfire_1":
object_name = "Cookout Kit"
Expand Down

0 comments on commit d19074d

Please sign in to comment.