Skip to content

Commit

Permalink
Advanced settings with Auto Preset.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwatkot committed Dec 22, 2024
1 parent b8b465e commit 9f6bf62
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 89 deletions.
10 changes: 6 additions & 4 deletions maps4fs/generator/i3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
DEFAULT_MAX_LOD_OCCLUDER_DISTANCE = 10000
NODE_ID_STARTING_VALUE = 2000
TREE_NODE_ID_STARTING_VALUE = 4000
DEFAULT_FOREST_DENSITY = 10


# pylint: disable=R0903
Expand Down Expand Up @@ -51,6 +52,9 @@ def preprocess(self) -> None:
self.logger.info("I3D file processing is not implemented for this game.")
self._map_i3d_path = None

self.forest_density = self.kwargs.get("forest_density", DEFAULT_FOREST_DENSITY)
self.logger.info("Forest density: %s.", self.forest_density)

def process(self) -> None:
"""Updates the map I3D file with the default settings."""
self._update_i3d_file()
Expand Down Expand Up @@ -390,15 +394,13 @@ def _add_forests(self) -> None:
# pylint: disable=no-member
forest_image = cv2.imread(forest_image_path, cv2.IMREAD_UNCHANGED)

forest_density = 10 # TODO: Obtain as a setting from UI.

tree_count = 0
for x, y in self.non_empty_pixels(forest_image, step=forest_density):
for x, y in self.non_empty_pixels(forest_image, step=self.forest_density):
xcs, ycs = self.top_left_coordinates_to_center((x, y))
node_id += 1

rotation = randint(-180, 180)
xcs, ycs = self.randomize_coordinates((xcs, ycs), forest_density) # type: ignore
xcs, ycs = self.randomize_coordinates((xcs, ycs), self.forest_density) # type: ignore

random_tree = choice(tree_schema)
tree_name = random_tree["name"]
Expand Down
186 changes: 102 additions & 84 deletions webui/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,101 +234,117 @@ def add_left_widgets(self) -> None:

st.info(Messages.AUTO_PRESET_DISABLED)

# Add checkbox for advanced settings.
st.write("Advanced settings (do not change if you are not sure):")
# Add checkbox for advanced settings.
st.write("Advanced settings (do not change if you are not sure):")
self.advanced_settings = st.checkbox(
"Show advanced settings",
key="advanced_settings",
)

if self.community:
st.warning(Messages.COMMUNITY_ADVANCED_SETTINGS)
disabled = True
else:
disabled = False
if self.advanced_settings:
self.logger.debug("Advanced settings are enabled.")

self.advanced_settings = st.checkbox(
"Show advanced settings", key="advanced_settings", disabled=disabled
)
st.warning("⚠️ Changing these settings can lead to unexpected results.")

if self.advanced_settings:
self.logger.debug("Advanced settings are enabled.")
with st.expander("DEM Advanced Settings", icon="⛰️"):
st.info(
"ℹ️ Settings related to the Digital Elevation Model (elevation map). "
"This file is used to generate the terrain of the map (hills, valleys, etc.)."
)
# Show multiplier and blur radius inputs.
st.write("Enter the multiplier for the elevation map:")
st.write(Messages.DEM_MULTIPLIER_INFO)

if self.auto_process:
st.info("When the auto preset is enabled, the multiplier is set to 1.")

self.multiplier_input = st.number_input(
"Multiplier",
value=DEFAULT_MULTIPLIER,
min_value=0,
max_value=10000,
step=1,
key="multiplier",
label_visibility="collapsed",
disabled=self.auto_process,
)

st.warning("⚠️ Changing these settings can lead to unexpected results.")
st.write("Enter the blur radius for the elevation map:")
st.write(Messages.DEM_BLUR_RADIUS_INFO)

self.blur_radius_input = st.number_input(
"Blur Radius",
value=DEFAULT_BLUR_RADIUS,
min_value=0,
max_value=300,
key="blur_radius",
label_visibility="collapsed",
step=2,
)

with st.expander("DEM Advanced Settings", icon="⛰️"):
st.info(
"ℹ️ Settings related to the Digital Elevation Model (elevation map). "
"This file is used to generate the terrain of the map (hills, valleys, etc.)."
)
# Show multiplier and blur radius inputs.
st.write("Enter the multiplier for the elevation map:")
st.write(Messages.DEM_MULTIPLIER_INFO)

self.multiplier_input = st.number_input(
"Multiplier",
value=DEFAULT_MULTIPLIER,
min_value=0,
max_value=10000,
step=1,
key="multiplier",
label_visibility="collapsed",
)
st.write("Enter the plateau height (which will be added to the whole map):")
st.write(Messages.DEM_PLATEAU_INFO)
self.plateau_height_input = st.number_input(
"Plateau Height",
value=0,
min_value=0,
max_value=10000,
key="plateau_height",
label_visibility="collapsed",
)

st.write("Enter the blur radius for the elevation map:")
st.write(Messages.DEM_BLUR_RADIUS_INFO)

self.blur_radius_input = st.number_input(
"Blur Radius",
value=DEFAULT_BLUR_RADIUS,
min_value=0,
max_value=300,
key="blur_radius",
label_visibility="collapsed",
step=2,
)
with st.expander("Textures Advanced Settings", icon="🎨"):
st.info(
"ℹ️ Settings related to the textures of the map, which represent different "
"types of terrain, such as grass, dirt, etc."
)

st.write("Enter the plateau height (which will be added to the whole map):")
st.write(Messages.DEM_PLATEAU_INFO)
self.plateau_height_input = st.number_input(
"Plateau Height",
value=0,
min_value=0,
max_value=10000,
key="plateau_height",
label_visibility="collapsed",
)
st.write("Enter the field padding (in meters):")
st.write(Messages.FIELD_PADDING_INFO)
self.fields_padding = st.number_input(
"Field Padding",
value=0,
min_value=0,
max_value=100,
key="field_padding",
label_visibility="collapsed",
)

with st.expander("Textures Advanced Settings", icon="🎨"):
st.info(
"ℹ️ Settings related to the textures of the map, which represent different "
"types of terrain, such as grass, dirt, etc."
)
with st.expander("Farmlands Advanced Settings", icon="🌾"):
st.info(
"ℹ️ Settings related to the farmlands of the map, which represent the lands "
"that can be bought in the game by the player."
)

st.write("Enter the field padding (in meters):")
st.write(Messages.FIELD_PADDING_INFO)
self.fields_padding = st.number_input(
"Field Padding",
value=0,
min_value=0,
max_value=100,
key="field_padding",
label_visibility="collapsed",
)
st.write("Enter the farmland margin (in meters):")
st.write(Messages.FARMLAND_MARGIN_INFO)

with st.expander("Farmlands Advanced Settings", icon="🌾"):
st.info(
"ℹ️ Settings related to the farmlands of the map, which represent the lands "
"that can be bought in the game by the player."
)
self.farmland_margin = st.number_input(
"Farmland Margin",
value=3,
min_value=0,
max_value=100,
key="farmland_margin",
label_visibility="collapsed",
)

st.write("Enter the farmland margin (in meters):")
st.write(Messages.FARMLAND_MARGIN_INFO)
with st.expander("Vegetation Advanced Settings", icon="🌲"):
st.info(
"ℹ️ Settings related to the vegetation of the map, which represent the trees, "
"grass, etc."
)

self.farmland_margin = st.number_input(
"Farmland Margin",
value=3,
min_value=0,
max_value=100,
key="farmland_margin",
label_visibility="collapsed",
)
st.write("Enter the forest density (in meters):")
st.write(Messages.FOREST_DENSITY_INFO)

self.forest_density = st.number_input(
"Forest Density",
value=10,
min_value=2,
max_value=50,
key="forest_density",
label_visibility="collapsed",
)

# Add an empty container for status messages.
self.status_container = st.empty()
Expand Down Expand Up @@ -421,14 +437,16 @@ def generate_map(self) -> None:
os.makedirs(map_directory, exist_ok=True)

# Create an instance of the Map class and generate the map.
multiplier = self.multiplier_input if not self.auto_process else 1

mp = mfs.Map(
game,
coordinates,
height,
self.rotation,
map_directory,
logger=self.logger,
multiplier=self.multiplier_input,
multiplier=multiplier,
blur_radius=self.blur_radius_input,
auto_process=self.auto_process,
plateau=self.plateau_height_input,
Expand Down
8 changes: 7 additions & 1 deletion webui/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Messages:
"🔸 Maximum map size is 2048x2048 meters. \n"
"🔸 Background terrain will not be generated. \n"
"🔸 Map rotation is disabled. \n"
"🔸 Advanced settings are disabled. \n"
"🔸 Texture dissolving is disabled (they will look worse). \n \n"
"If you run the application locally, you won't have any of these limitations "
"and will be able to generate maps of any size with any settings you want and nice looking textures. \n"
Expand Down Expand Up @@ -96,3 +95,10 @@ class Messages:
"It can be useful because without the margin, the farmland will end exact on the same "
"position as the field ends. This can cause some issues with gameplay."
)
FOREST_DENSITY_INFO = (
"This value represents the distance between trees in the forest. "
"The higher the value, the more sparse the forest will be and less trees will be "
"generated. Be careful with low values, because depending on the amount of forest areas "
"and the map size, it may generate dozens of thousands of trees, which can cause "
"performance issues."
)

0 comments on commit 9f6bf62

Please sign in to comment.