style: apply gdformat formatting across the entire project

Auto-format all GDScript files using gdformat from gdtoolkit.
This is a baseline formatting pass to ensure consistent style:

- Normalizes indentation and spacing
- Wraps long lines to 100 characters
- Removes trailing whitespace
- Standardizes blank lines between functions

68 files reformatted, 8 files left unchanged (3rd-party addon files
with parse errors excluded).
This commit is contained in:
2026-07-05 23:06:23 +02:00
parent 28a2e42c41
commit 4463e524aa
69 changed files with 2253 additions and 1647 deletions
+11 -5
View File
@@ -24,8 +24,10 @@ extends Terrain3D
@export var simple_grass_textured: MultiMeshInstance3D @export var simple_grass_textured: MultiMeshInstance3D
@export var assign_mesh_id: int @export var assign_mesh_id: int
@export var import: bool = false : set = import_sgt @export var import: bool = false:
@export var clear_instances: bool = false : set = clear_multimeshes set = import_sgt
@export var clear_instances: bool = false:
set = clear_multimeshes
func clear_multimeshes(value: bool) -> void: func clear_multimeshes(value: bool) -> void:
@@ -35,8 +37,12 @@ func clear_multimeshes(value: bool) -> void:
func import_sgt(value: bool) -> void: func import_sgt(value: bool) -> void:
var sgt_mm: MultiMesh = simple_grass_textured.multimesh var sgt_mm: MultiMesh = simple_grass_textured.multimesh
var global_xform: Transform3D = simple_grass_textured.global_transform var global_xform: Transform3D = simple_grass_textured.global_transform
print("Starting to import %d instances from SimpleGrassTextured using mesh id %d" % [ sgt_mm.instance_count, assign_mesh_id]) print(
(
"Starting to import %d instances from SimpleGrassTextured using mesh id %d"
% [sgt_mm.instance_count, assign_mesh_id]
)
)
var time: int = Time.get_ticks_msec() var time: int = Time.get_ticks_msec()
get_instancer().add_multimesh(assign_mesh_id, sgt_mm, simple_grass_textured.global_transform) get_instancer().add_multimesh(assign_mesh_id, sgt_mm, simple_grass_textured.global_transform)
print("Import complete in %.2f seconds" % [ float(Time.get_ticks_msec() - time)/1000. ]) print("Import complete in %.2f seconds" % [float(Time.get_ticks_msec() - time) / 1000.])
+95 -95
View File
@@ -26,112 +26,112 @@
# #
# #
#func _init() -> void: #func _init() -> void:
#display_name = "Project On Terrain3D" #display_name = "Project On Terrain3D"
#category = "Edit" #category = "Edit"
#can_restrict_height = false #can_restrict_height = false
#global_reference_frame_available = true #global_reference_frame_available = true
#local_reference_frame_available = true #local_reference_frame_available = true
#individual_instances_reference_frame_available = true #individual_instances_reference_frame_available = true
#use_global_space_by_default() #use_global_space_by_default()
# #
#documentation.add_paragraph( #documentation.add_paragraph(
#"This is a modified version of `Project on Colliders` that queries Terrain3D #"This is a modified version of `Project on Colliders` that queries Terrain3D
#for heights without using collision. It constrains placement by slope or texture. #for heights without using collision. It constrains placement by slope or texture.
# #
#This modifier must have terrain_node set to a Terrain3D node.") #This modifier must have terrain_node set to a Terrain3D node.")
# #
#var p := documentation.add_parameter("Terrain Node") #var p := documentation.add_parameter("Terrain Node")
#p.set_type("NodePath") #p.set_type("NodePath")
#p.set_description("Set your Terrain3D node.") #p.set_description("Set your Terrain3D node.")
# #
#p = documentation.add_parameter("Align with collision normal") #p = documentation.add_parameter("Align with collision normal")
#p.set_type("bool") #p.set_type("bool")
#p.set_description( #p.set_description(
#"Rotate the transform to align it with the collision normal in case #"Rotate the transform to align it with the collision normal in case
#the ray cast hit a collider.") #the ray cast hit a collider.")
# #
#p = documentation.add_parameter("Enable Texture Filtering") #p = documentation.add_parameter("Enable Texture Filtering")
#p.set_type("bool") #p.set_type("bool")
#p.set_description( #p.set_description(
#"If enabled, objects will only be placed based on the ground texture specified.") #"If enabled, objects will only be placed based on the ground texture specified.")
# #
#p = documentation.add_parameter("Target Texture ID") #p = documentation.add_parameter("Target Texture ID")
#p.set_type("int") #p.set_type("int")
#p.set_description( #p.set_description(
#"The ID of the texture to place objects on (0-31). Objects will only be placed on this texture.") #"The ID of the texture to place objects on (0-31). Objects will only be placed on this texture.")
# #
#p = documentation.add_parameter("Not Target Texture") #p = documentation.add_parameter("Not Target Texture")
#p.set_type("bool") #p.set_type("bool")
#p.set_description( #p.set_description(
#"If true, objects will be placed on all textures EXCEPT the target texture.") #"If true, objects will be placed on all textures EXCEPT the target texture.")
# #
#p = documentation.add_parameter("Texture Threshold") #p = documentation.add_parameter("Texture Threshold")
#p.set_type("float") #p.set_type("float")
#p.set_description("The blend value required for placement on the texture.") #p.set_description("The blend value required for placement on the texture.")
# #
# #
#func _process_transforms(transforms, domain, _seed) -> void: #func _process_transforms(transforms, domain, _seed) -> void:
#if transforms.is_empty(): #if transforms.is_empty():
#return #return
# #
#if terrain_node: #if terrain_node:
#_terrain = domain.get_root().get_node_or_null(terrain_node) #_terrain = domain.get_root().get_node_or_null(terrain_node)
# #
#if not _terrain: #if not _terrain:
#warning += """No Terrain3D node found""" #warning += """No Terrain3D node found"""
#return #return
# #
#if not _terrain.data: #if not _terrain.data:
#warning += """Terrain3DData is not initialized""" #warning += """Terrain3DData is not initialized"""
#return #return
# #
## Review transforms ## Review transforms
#var gt: Transform3D = domain.get_global_transform() #var gt: Transform3D = domain.get_global_transform()
#var gt_inverse := gt.affine_inverse() #var gt_inverse := gt.affine_inverse()
#var new_transforms_array: Array[Transform3D] = [] #var new_transforms_array: Array[Transform3D] = []
#var remapped_max_slope: float = remap(max_slope, 0.0, 90.0, 0.0, 1.0) #var remapped_max_slope: float = remap(max_slope, 0.0, 90.0, 0.0, 1.0)
#for i in transforms.list.size(): #for i in transforms.list.size():
#var t: Transform3D = transforms.list[i] #var t: Transform3D = transforms.list[i]
#
#var location: Vector3 = (gt * t).origin
#var height: float = _terrain.data.get_height(location)
#if is_nan(height):
#continue
#
#var normal: Vector3 = _terrain.data.get_normal(location)
#if not abs(Vector3.UP.dot(normal)) >= (1.0 - remapped_max_slope):
#continue
#
#if enable_texture_filtering:
#var texture_info: Vector3 = _terrain.data.get_texture_id(location)
#var base_id: int = int(texture_info.x)
#var overlay_id: int = int(texture_info.y)
#var blend_value: float = texture_info.z
## Skip if overlay or blend != target texture, unless inverted
#if ((overlay_id != target_texture_id or blend_value < texture_threshold) and \
#(base_id != target_texture_id or blend_value >= texture_threshold)) != not_target_texture:
#continue
# #
#if align_with_collision_normal and not is_nan(normal.x): #var location: Vector3 = (gt * t).origin
#t.basis.y = normal #var height: float = _terrain.data.get_height(location)
#t.basis.x = -t.basis.z.cross(normal) #if is_nan(height):
#t.basis = t.basis.orthonormalized() #continue
# #
#t.origin.y = height - gt.origin.y #var normal: Vector3 = _terrain.data.get_normal(location)
#new_transforms_array.push_back(t) #if not abs(Vector3.UP.dot(normal)) >= (1.0 - remapped_max_slope):
#continue
# #
#transforms.list.clear() #if enable_texture_filtering:
#transforms.list.append_array(new_transforms_array) #var texture_info: Vector3 = _terrain.data.get_texture_id(location)
#var base_id: int = int(texture_info.x)
#var overlay_id: int = int(texture_info.y)
#var blend_value: float = texture_info.z
## Skip if overlay or blend != target texture, unless inverted
#if ((overlay_id != target_texture_id or blend_value < texture_threshold) and \
#(base_id != target_texture_id or blend_value >= texture_threshold)) != not_target_texture:
#continue
# #
#if transforms.is_empty(): #if align_with_collision_normal and not is_nan(normal.x):
#warning += """All transforms have been removed. Possible reasons include: \n""" #t.basis.y = normal
#if enable_texture_filtering: #t.basis.x = -t.basis.z.cross(normal)
#warning += """+ No matching texture found at any position. #t.basis = t.basis.orthonormalized()
#+ Texture threshold may be too high. #
#""" #t.origin.y = height - gt.origin.y
#warning += """+ No collider is close enough to the shapes. #new_transforms_array.push_back(t)
#+ Ray length is too short. #
#+ Ray direction is incorrect. #transforms.list.clear()
#+ Collision mask is not set properly. #transforms.list.append_array(new_transforms_array)
#+ Max slope is too low. #
#""" #if transforms.is_empty():
#warning += """All transforms have been removed. Possible reasons include: \n"""
#if enable_texture_filtering:
#warning += """+ No matching texture found at any position.
#+ Texture threshold may be too high.
#"""
#warning += """+ No collider is close enough to the shapes.
#+ Ray length is too short.
#+ Ray direction is incorrect.
#+ Collision mask is not set properly.
#+ Max slope is too low.
#"""
@@ -7,7 +7,6 @@
@tool @tool
extends Node3D extends Node3D
#region settings #region settings
## Auto set if attached as a child of a Terrain3D node ## Auto set if attached as a child of a Terrain3D node
@export var terrain: Terrain3D: @export var terrain: Terrain3D:
@@ -15,7 +14,6 @@ extends Node3D
terrain = value terrain = value
_create_grid() _create_grid()
## Distance between instances ## Distance between instances
@export_range(0.125, 2.0, 0.015625) var instance_spacing: float = 0.5: @export_range(0.125, 2.0, 0.015625) var instance_spacing: float = 0.5:
set(value): set(value):
@@ -24,7 +22,6 @@ extends Node3D
amount = rows * rows amount = rows * rows
_set_offsets() _set_offsets()
## Width of an individual cell of the grid ## Width of an individual cell of the grid
@export_range(8.0, 256.0, 1.0) var cell_width: float = 32.0: @export_range(8.0, 256.0, 1.0) var cell_width: float = 32.0:
set(value): set(value):
@@ -44,7 +41,6 @@ extends Node3D
p.custom_aabb = aabb p.custom_aabb = aabb
_set_offsets() _set_offsets()
## Grid width. Must be odd. ## Grid width. Must be odd.
## Higher values cull slightly better, draw further out. ## Higher values cull slightly better, draw further out.
@export_range(1, 15, 2) var grid_width: int = 9: @export_range(1, 15, 2) var grid_width: int = 9:
@@ -54,7 +50,6 @@ extends Node3D
min_draw_distance = 1.0 min_draw_distance = 1.0
_create_grid() _create_grid()
@export_storage var rows: int = 1 @export_storage var rows: int = 1
@export_storage var amount: int = 1: @export_storage var amount: int = 1:
@@ -65,7 +60,6 @@ extends Node3D
for p in particle_nodes: for p in particle_nodes:
p.amount = amount p.amount = amount
@export_range(1, 256, 1) var process_fixed_fps: int = 30: @export_range(1, 256, 1) var process_fixed_fps: int = 30:
set(value): set(value):
process_fixed_fps = maxi(value, 1) process_fixed_fps = maxi(value, 1)
@@ -73,7 +67,6 @@ extends Node3D
p.fixed_fps = process_fixed_fps p.fixed_fps = process_fixed_fps
p.preprocess = 1.0 / float(process_fixed_fps) p.preprocess = 1.0 / float(process_fixed_fps)
## Access to process material parameters ## Access to process material parameters
@export var process_material: ShaderMaterial @export var process_material: ShaderMaterial
@@ -81,23 +74,21 @@ extends Node3D
@export var mesh: Mesh @export var mesh: Mesh
@export var shadow_mode: GeometryInstance3D.ShadowCastingSetting = ( @export var shadow_mode: GeometryInstance3D.ShadowCastingSetting = (
GeometryInstance3D.ShadowCastingSetting.SHADOW_CASTING_SETTING_ON): GeometryInstance3D.ShadowCastingSetting.SHADOW_CASTING_SETTING_ON
):
set(value): set(value):
shadow_mode = value shadow_mode = value
for p in particle_nodes: for p in particle_nodes:
p.cast_shadow = value p.cast_shadow = value
## Override material for the particle mesh ## Override material for the particle mesh
@export_custom( @export_custom(PROPERTY_HINT_RESOURCE_TYPE, "BaseMaterial3D,ShaderMaterial")
PROPERTY_HINT_RESOURCE_TYPE, var mesh_material_override: Material:
"BaseMaterial3D,ShaderMaterial") var mesh_material_override: Material:
set(value): set(value):
mesh_material_override = value mesh_material_override = value
for p in particle_nodes: for p in particle_nodes:
p.material_override = mesh_material_override p.material_override = mesh_material_override
@export_group("Info") @export_group("Info")
## The minimum distance that particles will be drawn upto ## The minimum distance that particles will be drawn upto
## If using fade out effects like pixel alpha this is the limit to use. ## If using fade out effects like pixel alpha this is the limit to use.
@@ -105,7 +96,6 @@ extends Node3D
set(value): set(value):
min_draw_distance = float(cell_width * grid_width) * 0.5 min_draw_distance = float(cell_width * grid_width) * 0.5
## Displays current total particle count based on Cell Width and Instance Spacing ## Displays current total particle count based on Cell Width and Instance Spacing
@export var particle_count: int = 1: @export var particle_count: int = 1:
set(value): set(value):
@@ -113,7 +103,6 @@ extends Node3D
#endregion #endregion
var offsets: Array[Vector3] var offsets: Array[Vector3]
var last_pos: Vector3 = Vector3.ZERO var last_pos: Vector3 = Vector3.ZERO
var particle_nodes: Array[GPUParticles3D] var particle_nodes: Array[GPUParticles3D]
@@ -139,7 +128,9 @@ func _physics_process(delta: float) -> void:
if last_pos.distance_squared_to(camera.global_position) > 1.0: if last_pos.distance_squared_to(camera.global_position) > 1.0:
var pos: Vector3 = camera.global_position.snapped(Vector3.ONE) var pos: Vector3 = camera.global_position.snapped(Vector3.ONE)
_position_grid(pos) _position_grid(pos)
RenderingServer.material_set_param(process_material.get_rid(), "camera_position", pos ) RenderingServer.material_set_param(
process_material.get_rid(), "camera_position", pos
)
last_pos = camera.global_position last_pos = camera.global_position
_update_process_parameters() _update_process_parameters()
else: else:
@@ -180,7 +171,7 @@ func _create_grid() -> void:
if mesh_material_override: if mesh_material_override:
particle_node.material_override = mesh_material_override particle_node.material_override = mesh_material_override
particle_node.use_fixed_seed = true particle_node.use_fixed_seed = true
if (x > -half_grid and z > -half_grid): # Use the same seed across all nodes if x > -half_grid and z > -half_grid: # Use the same seed across all nodes
particle_node.seed = particle_nodes[0].seed particle_node.seed = particle_nodes[0].seed
self.add_child(particle_node) self.add_child(particle_node)
particle_node.emitting = true particle_node.emitting = true
@@ -194,9 +185,7 @@ func _set_offsets() -> void:
for x in range(-half_grid, half_grid + 1): for x in range(-half_grid, half_grid + 1):
for z in range(-half_grid, half_grid + 1): for z in range(-half_grid, half_grid + 1):
var offset := Vector3( var offset := Vector3(
float(x * rows) * instance_spacing, float(x * rows) * instance_spacing, 0.0, float(z * rows) * instance_spacing
0.0,
float(z * rows) * instance_spacing
) )
offsets.append(offset) offsets.append(offset)
@@ -214,24 +203,42 @@ func _position_grid(pos: Vector3) -> void:
var snap = Vector3(pos.x, 0, pos.z).snapped(Vector3.ONE) + offsets[i] var snap = Vector3(pos.x, 0, pos.z).snapped(Vector3.ONE) + offsets[i]
node.global_position = (snap / instance_spacing).round() * instance_spacing node.global_position = (snap / instance_spacing).round() * instance_spacing
node.reset_physics_interpolation() node.reset_physics_interpolation()
node.restart(true) # keep the same seed. node.restart(true) # keep the same seed.
func _update_process_parameters() -> void: func _update_process_parameters() -> void:
if process_material: if process_material:
var process_rid: RID = process_material.get_rid() var process_rid: RID = process_material.get_rid()
if terrain and process_rid.is_valid(): if terrain and process_rid.is_valid():
RenderingServer.material_set_param(process_rid, "_background_mode", terrain.material.world_background) RenderingServer.material_set_param(
RenderingServer.material_set_param(process_rid, "_vertex_spacing", terrain.vertex_spacing) process_rid, "_background_mode", terrain.material.world_background
RenderingServer.material_set_param(process_rid, "_vertex_density", 1.0 / terrain.vertex_spacing) )
RenderingServer.material_set_param(
process_rid, "_vertex_spacing", terrain.vertex_spacing
)
RenderingServer.material_set_param(
process_rid, "_vertex_density", 1.0 / terrain.vertex_spacing
)
RenderingServer.material_set_param(process_rid, "_region_size", terrain.region_size) RenderingServer.material_set_param(process_rid, "_region_size", terrain.region_size)
RenderingServer.material_set_param(process_rid, "_region_texel_size", 1.0 / terrain.region_size) RenderingServer.material_set_param(
process_rid, "_region_texel_size", 1.0 / terrain.region_size
)
RenderingServer.material_set_param(process_rid, "_region_map_size", 32) RenderingServer.material_set_param(process_rid, "_region_map_size", 32)
RenderingServer.material_set_param(process_rid, "_region_map", terrain.data.get_region_map()) RenderingServer.material_set_param(
RenderingServer.material_set_param(process_rid, "_region_locations", terrain.data.get_region_locations()) process_rid, "_region_map", terrain.data.get_region_map()
RenderingServer.material_set_param(process_rid, "_height_maps", terrain.data.get_height_maps_rid()) )
RenderingServer.material_set_param(process_rid, "_control_maps", terrain.data.get_control_maps_rid()) RenderingServer.material_set_param(
RenderingServer.material_set_param(process_rid, "_color_maps", terrain.data.get_color_maps_rid()) process_rid, "_region_locations", terrain.data.get_region_locations()
)
RenderingServer.material_set_param(
process_rid, "_height_maps", terrain.data.get_height_maps_rid()
)
RenderingServer.material_set_param(
process_rid, "_control_maps", terrain.data.get_control_maps_rid()
)
RenderingServer.material_set_param(
process_rid, "_color_maps", terrain.data.get_color_maps_rid()
)
RenderingServer.material_set_param(process_rid, "instance_spacing", instance_spacing) RenderingServer.material_set_param(process_rid, "instance_spacing", instance_spacing)
RenderingServer.material_set_param(process_rid, "instance_rows", rows) RenderingServer.material_set_param(process_rid, "instance_rows", rows)
RenderingServer.material_set_param(process_rid, "max_dist", min_draw_distance) RenderingServer.material_set_param(process_rid, "max_dist", min_draw_distance)
@@ -5,6 +5,7 @@ extends Button
signal dropped signal dropped
func _can_drop_data(p_position, p_data) -> bool: func _can_drop_data(p_position, p_data) -> bool:
if typeof(p_data) == TYPE_DICTIONARY: if typeof(p_data) == TYPE_DICTIONARY:
if p_data.files.size() == 1: if p_data.files.size() == 1:
@@ -13,5 +14,6 @@ func _can_drop_data(p_position, p_data) -> bool:
return true return true
return false return false
func _drop_data(p_position, p_data) -> void: func _drop_data(p_position, p_data) -> void:
dropped.emit(p_data.files[0]) dropped.emit(p_data.files[0])
+3 -1
View File
@@ -41,7 +41,9 @@ func directory_setup_popup() -> void:
plugin.ui.set_button_editor_icon(select_dir_btn, "Folder") plugin.ui.set_button_editor_icon(select_dir_btn, "Folder")
#Signals #Signals
select_dir_btn.pressed.connect(_on_select_file_pressed.bind(EditorFileDialog.FILE_MODE_OPEN_DIR)) select_dir_btn.pressed.connect(
_on_select_file_pressed.bind(EditorFileDialog.FILE_MODE_OPEN_DIR)
)
dialog.confirmed.connect(_on_close_requested) dialog.confirmed.connect(_on_close_requested)
dialog.canceled.connect(_on_close_requested) dialog.canceled.connect(_on_close_requested)
dialog.get_ok_button().pressed.connect(_on_ok_pressed) dialog.get_ok_button().pressed.connect(_on_ok_pressed)
+1 -2
View File
@@ -2,7 +2,6 @@
# Menu for Terrain3D # Menu for Terrain3D
extends HBoxContainer extends HBoxContainer
const DirectoryWizard: Script = preload("res://addons/terrain_3d/menu/directory_setup.gd") const DirectoryWizard: Script = preload("res://addons/terrain_3d/menu/directory_setup.gd")
const Packer: Script = preload("res://addons/terrain_3d/menu/channel_packer.gd") const Packer: Script = preload("res://addons/terrain_3d/menu/channel_packer.gd")
const Baker: Script = preload("res://addons/terrain_3d/menu/baker.gd") const Baker: Script = preload("res://addons/terrain_3d/menu/baker.gd")
@@ -34,7 +33,7 @@ func _enter_tree() -> void:
add_child(baker) add_child(baker)
menu_button.text = "Terrain3D" menu_button.text = "Terrain3D"
menu_button.get_popup().add_item("Directory Setup...", MENU_DIRECTORY_SETUP) menu_button.get_popup().add_item("Directory Setup...", MENU_DIRECTORY_SETUP)
menu_button.get_popup().add_item("Pack Textures...", MENU_PACK_TEXTURES) menu_button.get_popup().add_item("Pack Textures...", MENU_PACK_TEXTURES)
menu_button.get_popup().add_separator("", MENU_SEPARATOR) menu_button.get_popup().add_separator("", MENU_SEPARATOR)
menu_button.get_popup().add_item("Bake ArrayMesh...", MENU_BAKE_ARRAY_MESH) menu_button.get_popup().add_item("Bake ArrayMesh...", MENU_BAKE_ARRAY_MESH)
+90 -69
View File
@@ -30,11 +30,11 @@ var search_button: Button
#DEPRECATED 4.5 #DEPRECATED 4.5
#class EdDock extends EditorDock: #class EdDock extends EditorDock:
#func _update_layout(layout: int) -> void: #func _update_layout(layout: int) -> void:
#layout 1 vertical, 2 horizontal, 4 window #layout 1 vertical, 2 horizontal, 4 window
#print("Terrain3DAssetDock: _update_layout called with: ", layout) #print("Terrain3DAssetDock: _update_layout called with: ", layout)
var _dock: MarginContainer #DEPRECATED 4.5 - Use EdDock var _dock: MarginContainer #DEPRECATED 4.5 - Use EdDock
var _initialized: bool = false var _initialized: bool = false
var plugin: EditorPlugin var plugin: EditorPlugin
var window: Window var window: Window
@@ -50,12 +50,12 @@ func initialize(p_plugin: EditorPlugin) -> void:
if p_plugin: if p_plugin:
plugin = p_plugin plugin = p_plugin
_dock = ClassDB.instantiate("EditorDock") #DEPRECATED 4.5 - EdDock.new() _dock = ClassDB.instantiate("EditorDock") #DEPRECATED 4.5 - EdDock.new()
_dock.title = "Terrain3D" _dock.title = "Terrain3D"
_dock.dock_icon = preload("../icons/terrain3d.svg") _dock.dock_icon = preload("../icons/terrain3d.svg")
_dock.default_slot = 8 #DEPRECATED 4.5 - EditorDock.DockSlot.DOCK_SLOT_BOTTOM _dock.default_slot = 8 #DEPRECATED 4.5 - EditorDock.DockSlot.DOCK_SLOT_BOTTOM
_dock.closable = false _dock.closable = false
_dock.available_layouts = 0x7 #DEPRECATED 4.5 - EditorDock.DOCK_LAYOUT_ALL _dock.available_layouts = 0x7 #DEPRECATED 4.5 - EditorDock.DOCK_LAYOUT_ALL
_dock.add_child(self) _dock.add_child(self)
plugin.add_dock(_dock) plugin.add_dock(_dock)
_dock.open() _dock.open()
@@ -100,8 +100,12 @@ func initialize(p_plugin: EditorPlugin) -> void:
size_slider.value_changed.connect(_on_slider_changed) size_slider.value_changed.connect(_on_slider_changed)
plugin.ui.toolbar.tool_changed.connect(_on_tool_changed) plugin.ui.toolbar.tool_changed.connect(_on_tool_changed)
meshes_btn.add_theme_font_size_override("font_size", int(16. * EditorInterface.get_editor_scale())) meshes_btn.add_theme_font_size_override(
textures_btn.add_theme_font_size_override("font_size", int(16. * EditorInterface.get_editor_scale())) "font_size", int(16. * EditorInterface.get_editor_scale())
)
textures_btn.add_theme_font_size_override(
"font_size", int(16. * EditorInterface.get_editor_scale())
)
search_box.text_changed.connect(_on_search_text_changed) search_box.text_changed.connect(_on_search_text_changed)
search_button.pressed.connect(_on_search_button_pressed) search_button.pressed.connect(_on_search_button_pressed)
@@ -109,12 +113,18 @@ func initialize(p_plugin: EditorPlugin) -> void:
confirm_dialog = ConfirmationDialog.new() confirm_dialog = ConfirmationDialog.new()
add_child(confirm_dialog, true) add_child(confirm_dialog, true)
confirm_dialog.hide() confirm_dialog.hide()
confirm_dialog.confirmed.connect(func(): _confirmed = true; \ confirm_dialog.confirmed.connect(
confirmation_closed.emit(); \ func():
confirmation_confirmed.emit() ) _confirmed = true
confirm_dialog.canceled.connect(func(): _confirmed = false; \ confirmation_closed.emit()
confirmation_closed.emit(); \ confirmation_confirmed.emit()
confirmation_canceled.emit() ) )
confirm_dialog.canceled.connect(
func():
_confirmed = false
confirmation_closed.emit()
confirmation_canceled.emit()
)
# Setup styles # Setup styles
set("theme_override_styles/panel", get_theme_stylebox("panel", "Panel")) set("theme_override_styles/panel", get_theme_stylebox("panel", "Panel"))
@@ -163,12 +173,12 @@ func update_layout() -> void:
window = get_parent().get_parent().get_parent() window = get_parent().get_parent().get_parent()
_on_pin_changed(pinned_btn.button_pressed) _on_pin_changed(pinned_btn.button_pressed)
plugin.godot_editor_window.mouse_entered.connect(_on_godot_window_entered) plugin.godot_editor_window.mouse_entered.connect(_on_godot_window_entered)
return # Displaying will call this function again return # Displaying will call this function again
# Check if window was just freed. Freed objects have a different hash than null # Check if window was just freed. Freed objects have a different hash than null
elif hash(window) != hash(null): elif hash(window) != hash(null):
window = null window = null
plugin.godot_editor_window.mouse_entered.disconnect(_on_godot_window_entered) plugin.godot_editor_window.mouse_entered.disconnect(_on_godot_window_entered)
return # Will call this function again upon display return # Will call this function again upon display
## Vertical layout: buttons on top ## Vertical layout: buttons on top
if size.x < 700: if size.x < 700:
@@ -180,7 +190,7 @@ func update_layout() -> void:
box.move_child(size_slider, 2) box.move_child(size_slider, 2)
pinned_btn.reparent(buttons) pinned_btn.reparent(buttons)
else: else:
# Wide layout: buttons on left # Wide layout: buttons on left
box.vertical = false box.vertical = false
buttons.vertical = true buttons.vertical = true
search_box.reparent(buttons) search_box.reparent(buttons)
@@ -298,7 +308,7 @@ func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor
print("Terrain3DAssetDock: _on_tool_changed: ", p_tool, ", ", p_operation) print("Terrain3DAssetDock: _on_tool_changed: ", p_tool, ", ", p_operation)
if p_tool == Terrain3DEditor.INSTANCER: if p_tool == Terrain3DEditor.INSTANCER:
_on_meshes_pressed() _on_meshes_pressed()
elif p_tool in [ Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS ]: elif p_tool in [Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS]:
_on_textures_pressed() _on_textures_pressed()
@@ -307,7 +317,9 @@ func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor
func update_assets() -> void: func update_assets() -> void:
if plugin.debug: if plugin.debug:
print("Terrain3DAssetDock: update_assets: ", plugin.terrain.assets if plugin.terrain else "") print(
"Terrain3DAssetDock: update_assets: ", plugin.terrain.assets if plugin.terrain else ""
)
if not _initialized: if not _initialized:
return return
@@ -327,7 +339,7 @@ func update_assets() -> void:
func load_editor_settings() -> void: func load_editor_settings() -> void:
# Remove old editor settings # Remove old editor settings
const ES_DOCK: String = "terrain3d/dock/" const ES_DOCK: String = "terrain3d/dock/"
for setting in [ "slot", "floating", "window_position", "window_size" ]: for setting in ["slot", "floating", "window_position", "window_size"]:
plugin.erase_setting(ES_DOCK + setting) plugin.erase_setting(ES_DOCK + setting)
pinned_btn.button_pressed = plugin.get_setting(ES_DOCK_PINNED, true) pinned_btn.button_pressed = plugin.get_setting(ES_DOCK_PINNED, true)
size_slider.value = plugin.get_setting(ES_DOCK_TILE_SIZE, 90) size_slider.value = plugin.get_setting(ES_DOCK_TILE_SIZE, 90)
@@ -351,7 +363,8 @@ func save_editor_settings() -> void:
############################################################## ##############################################################
class ListContainer extends Container: class ListContainer:
extends Container
var plugin: EditorPlugin var plugin: EditorPlugin
var type := Terrain3DAssets.TYPE_TEXTURE var type := Terrain3DAssets.TYPE_TEXTURE
var entries: Array[ListEntry] var entries: Array[ListEntry]
@@ -362,7 +375,6 @@ class ListContainer extends Container:
var _clearing_resource: bool = false var _clearing_resource: bool = false
var search_text: String = "" var search_text: String = ""
func _ready() -> void: func _ready() -> void:
set_v_size_flags(SIZE_EXPAND_FILL) set_v_size_flags(SIZE_EXPAND_FILL)
set_h_size_flags(SIZE_EXPAND_FILL) set_h_size_flags(SIZE_EXPAND_FILL)
@@ -371,14 +383,12 @@ class ListContainer extends Container:
add_theme_constant_override("shadow_offset_x", 1) add_theme_constant_override("shadow_offset_x", 1)
add_theme_constant_override("shadow_offset_y", 1) add_theme_constant_override("shadow_offset_y", 1)
func clear() -> void: func clear() -> void:
for e in entries: for e in entries:
e.get_parent().remove_child(e) e.get_parent().remove_child(e)
e.queue_free() e.queue_free()
entries.clear() entries.clear()
func update_asset_list() -> void: func update_asset_list() -> void:
if plugin.debug: if plugin.debug:
print("Terrain3DListContainer ", name, ": update_asset_list") print("Terrain3DListContainer ", name, ": update_asset_list")
@@ -407,7 +417,6 @@ class ListContainer extends Container:
add_item() add_item()
set_selected_id(selected_id) set_selected_id(selected_id)
func add_item(p_resource: Resource = null) -> void: func add_item(p_resource: Resource = null) -> void:
var entry: ListEntry = ListEntry.new() var entry: ListEntry = ListEntry.new()
entry.focus_style = focus_style entry.focus_style = focus_style
@@ -428,53 +437,74 @@ class ListContainer extends Container:
if not p_resource.id_changed.is_connected(set_selected_after_swap): if not p_resource.id_changed.is_connected(set_selected_after_swap):
p_resource.id_changed.connect(set_selected_after_swap) p_resource.id_changed.connect(set_selected_after_swap)
func _on_resource_hovered(p_id: int): func _on_resource_hovered(p_id: int):
if type == Terrain3DAssets.TYPE_MESH: if type == Terrain3DAssets.TYPE_MESH:
if plugin.terrain: if plugin.terrain:
plugin.terrain.assets.create_mesh_thumbnails(p_id) plugin.terrain.assets.create_mesh_thumbnails(p_id)
func set_selected_after_swap(
func set_selected_after_swap(p_type: Terrain3DAssets.AssetType, p_old_id: int, p_new_id: int) -> void: p_type: Terrain3DAssets.AssetType, p_old_id: int, p_new_id: int
) -> void:
EditorInterface.mark_scene_as_unsaved() EditorInterface.mark_scene_as_unsaved()
set_selected_id(clamp(p_new_id, 0, entries.size() - 2)) set_selected_id(clamp(p_new_id, 0, entries.size() - 2))
func clicked_id(p_id: int) -> void: func clicked_id(p_id: int) -> void:
# Select Tool if clicking an asset # Select Tool if clicking an asset
plugin.select_terrain() plugin.select_terrain()
if type == Terrain3DAssets.TYPE_TEXTURE and \ if (
not plugin.editor.get_tool() in [ Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS ]: type == Terrain3DAssets.TYPE_TEXTURE
and not (
plugin.editor.get_tool()
in [Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS]
)
):
var paint_btn: Button = plugin.ui.toolbar.get_node_or_null("PaintTexture") var paint_btn: Button = plugin.ui.toolbar.get_node_or_null("PaintTexture")
if paint_btn: if paint_btn:
paint_btn.set_pressed(true) paint_btn.set_pressed(true)
plugin.ui._on_tool_changed(Terrain3DEditor.TEXTURE, Terrain3DEditor.REPLACE) plugin.ui._on_tool_changed(Terrain3DEditor.TEXTURE, Terrain3DEditor.REPLACE)
elif type == Terrain3DAssets.TYPE_MESH and plugin.editor.get_tool() != Terrain3DEditor.INSTANCER: elif (
type == Terrain3DAssets.TYPE_MESH
and plugin.editor.get_tool() != Terrain3DEditor.INSTANCER
):
var instancer_btn: Button = plugin.ui.toolbar.get_node_or_null("InstanceMeshes") var instancer_btn: Button = plugin.ui.toolbar.get_node_or_null("InstanceMeshes")
if instancer_btn: if instancer_btn:
instancer_btn.set_pressed(true) instancer_btn.set_pressed(true)
plugin.ui._on_tool_changed(Terrain3DEditor.INSTANCER, Terrain3DEditor.ADD) plugin.ui._on_tool_changed(Terrain3DEditor.INSTANCER, Terrain3DEditor.ADD)
set_selected_id(p_id) set_selected_id(p_id)
func set_selected_id(p_id: int) -> void: func set_selected_id(p_id: int) -> void:
# "Add new" is the final entry only when search box is blank # "Add new" is the final entry only when search box is blank
var max_id: int = max(0, entries.size() - (1 if search_text else 2)) var max_id: int = max(0, entries.size() - (1 if search_text else 2))
if plugin.debug: if plugin.debug:
print("Terrain3DListContainer ", name, ": set_selected_id: ", selected_id, " to ", clamp(p_id, 0, max_id)) print(
"Terrain3DListContainer ",
name,
": set_selected_id: ",
selected_id,
" to ",
clamp(p_id, 0, max_id)
)
selected_id = clamp(p_id, 0, max_id) selected_id = clamp(p_id, 0, max_id)
for i in entries.size(): for i in entries.size():
var entry: ListEntry = entries[i] var entry: ListEntry = entries[i]
entry.set_selected(i == selected_id) entry.set_selected(i == selected_id)
plugin.ui._on_setting_changed() plugin.ui._on_setting_changed()
func get_selected_asset_id() -> int: func get_selected_asset_id() -> int:
# "Add new" is the final entry only when search box is blank # "Add new" is the final entry only when search box is blank
var max_id: int = max(0, entries.size() - (1 if search_text else 2)) var max_id: int = max(0, entries.size() - (1 if search_text else 2))
var id: int = clamp(selected_id, 0, max_id) var id: int = clamp(selected_id, 0, max_id)
if plugin.debug: if plugin.debug:
print("Terrain3DListContainer ", name, ": get_selected_asset_id: selected_id: ", selected_id, ", clamped: ", id, ", entries: ", entries.size()) print(
"Terrain3DListContainer ",
name,
": get_selected_asset_id: selected_id: ",
selected_id,
", clamped: ",
id,
", entries: ",
entries.size()
)
if id >= entries.size(): if id >= entries.size():
return 0 return 0
var res: Resource = entries[id].resource var res: Resource = entries[id].resource
@@ -485,18 +515,21 @@ class ListContainer extends Container:
else: else:
return (res as Terrain3DTextureAsset).id return (res as Terrain3DTextureAsset).id
func _on_resource_inspected(p_resource: Resource) -> void: func _on_resource_inspected(p_resource: Resource) -> void:
await get_tree().process_frame await get_tree().process_frame
EditorInterface.edit_resource(p_resource) EditorInterface.edit_resource(p_resource)
func _on_resource_changed(p_resource: Resource, p_id: int) -> void: func _on_resource_changed(p_resource: Resource, p_id: int) -> void:
if not p_resource and _clearing_resource: if not p_resource and _clearing_resource:
return return
if not p_resource: if not p_resource:
if plugin.debug: if plugin.debug:
print("Terrain3DListContainer ", name, ": _on_resource_changed: removing asset ID: ", p_id) print(
"Terrain3DListContainer ",
name,
": _on_resource_changed: removing asset ID: ",
p_id
)
_clearing_resource = true _clearing_resource = true
var asset_dock: Control = get_parent().get_parent().get_parent() var asset_dock: Control = get_parent().get_parent().get_parent()
if type == Terrain3DAssets.TYPE_TEXTURE: if type == Terrain3DAssets.TYPE_TEXTURE:
@@ -525,17 +558,14 @@ class ListContainer extends Container:
EditorInterface.inspect_object(null) EditorInterface.inspect_object(null)
_clearing_resource = false _clearing_resource = false
func set_entry_width(value: float) -> void: func set_entry_width(value: float) -> void:
var min_width: float = 90.0 * max(1.0, EditorInterface.get_editor_scale()) var min_width: float = 90.0 * max(1.0, EditorInterface.get_editor_scale())
width = clamp(value, min_width, 512.0) width = clamp(value, min_width, 512.0)
redraw() redraw()
func get_entry_width() -> float: func get_entry_width() -> float:
return width return width
func redraw() -> void: func redraw() -> void:
height = 0 height = 0
var id: int = 0 var id: int = 0
@@ -543,22 +573,24 @@ class ListContainer extends Container:
var columns: int = 3 var columns: int = 3
columns = clamp(size.x / width, 1, 100) columns = clamp(size.x / width, 1, 100)
var tile_size: Vector2 = Vector2(width, width) - Vector2(separation, separation) var tile_size: Vector2 = Vector2(width, width) - Vector2(separation, separation)
var name_font_size := int(clamp(tile_size.x/12., 12., 16.) * EditorInterface.get_editor_scale()) var name_font_size := int(
clamp(tile_size.x / 12., 12., 16.) * EditorInterface.get_editor_scale()
)
for c in get_children(): for c in get_children():
if is_instance_valid(c): if is_instance_valid(c):
c.size = tile_size c.size = tile_size
c.position = Vector2(id % columns, id / columns) * width + \ c.position = (
Vector2(separation / columns, separation / columns) Vector2(id % columns, id / columns) * width
+ Vector2(separation / columns, separation / columns)
)
height = max(height, c.position.y + width) height = max(height, c.position.y + width)
id += 1 id += 1
c.name_label.add_theme_font_size_override("font_size", name_font_size) c.name_label.add_theme_font_size_override("font_size", name_font_size)
# Needed to enable ScrollContainer scroll bar # Needed to enable ScrollContainer scroll bar
func _get_minimum_size() -> Vector2: func _get_minimum_size() -> Vector2:
return Vector2(0, height) return Vector2(0, height)
func _notification(p_what) -> void: func _notification(p_what) -> void:
if p_what == NOTIFICATION_SORT_CHILDREN: if p_what == NOTIFICATION_SORT_CHILDREN:
redraw() redraw()
@@ -569,9 +601,10 @@ class ListContainer extends Container:
############################################################## ##############################################################
class ListEntry extends MarginContainer: class ListEntry:
signal hovered() extends MarginContainer
signal clicked() signal hovered
signal clicked
signal changed(resource: Resource) signal changed(resource: Resource)
signal inspected(resource: Resource) signal inspected(resource: Resource)
@@ -597,7 +630,6 @@ class ListEntry extends MarginContainer:
@onready var disabled_icon: Texture2D = get_theme_icon("GuiVisibilityHidden", "EditorIcons") @onready var disabled_icon: Texture2D = get_theme_icon("GuiVisibilityHidden", "EditorIcons")
@onready var add_icon: Texture2D = get_theme_icon("Add", "EditorIcons") @onready var add_icon: Texture2D = get_theme_icon("Add", "EditorIcons")
func _ready() -> void: func _ready() -> void:
name = "ListEntry" name = "ListEntry"
custom_minimum_size = Vector2i(86., 86.) custom_minimum_size = Vector2i(86., 86.)
@@ -611,7 +643,6 @@ class ListEntry extends MarginContainer:
focus_style.set_border_width_all(2) focus_style.set_border_width_all(2)
focus_style.set_border_color(Color(1, 1, 1, .67)) focus_style.set_border_color(Color(1, 1, 1, .67))
func setup_buttons() -> void: func setup_buttons() -> void:
destroy_buttons() destroy_buttons()
@@ -665,7 +696,6 @@ class ListEntry extends MarginContainer:
button_clear.pressed.connect(_on_clear) button_clear.pressed.connect(_on_clear)
button_row.add_child(button_clear, true) button_row.add_child(button_clear, true)
func destroy_buttons() -> void: func destroy_buttons() -> void:
if button_row: if button_row:
button_row.free() button_row.free()
@@ -683,7 +713,6 @@ class ListEntry extends MarginContainer:
button_clear.free() button_clear.free()
button_clear = null button_clear = null
func get_resource_name() -> StringName: func get_resource_name() -> StringName:
if resource: if resource:
if resource is Terrain3DMeshAsset: if resource is Terrain3DMeshAsset:
@@ -692,7 +721,6 @@ class ListEntry extends MarginContainer:
return (resource as Terrain3DTextureAsset).get_name() return (resource as Terrain3DTextureAsset).get_name()
return "" return ""
func setup_label() -> void: func setup_label() -> void:
name_label = Label.new() name_label = Label.new()
name_label.name = "MeshLabel" name_label.name = "MeshLabel"
@@ -700,7 +728,9 @@ class ListEntry extends MarginContainer:
name_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER name_label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
name_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL name_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
name_label.size_flags_vertical = Control.SIZE_EXPAND_FILL name_label.size_flags_vertical = Control.SIZE_EXPAND_FILL
name_label.add_theme_font_size_override("font_size", int(14. * EditorInterface.get_editor_scale())) name_label.add_theme_font_size_override(
"font_size", int(14. * EditorInterface.get_editor_scale())
)
name_label.add_theme_color_override("font_color", Color.WHITE) name_label.add_theme_color_override("font_color", Color.WHITE)
name_label.add_theme_color_override("font_shadow_color", Color.BLACK) name_label.add_theme_color_override("font_shadow_color", Color.BLACK)
name_label.add_theme_constant_override("shadow_offset_x", 1) name_label.add_theme_constant_override("shadow_offset_x", 1)
@@ -710,7 +740,6 @@ class ListEntry extends MarginContainer:
name_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS name_label.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
add_child(name_label, true) add_child(name_label, true)
func _notification(p_what) -> void: func _notification(p_what) -> void:
match p_what: match p_what:
NOTIFICATION_PREDELETE: NOTIFICATION_PREDELETE:
@@ -758,7 +787,6 @@ class ListEntry extends MarginContainer:
drop_data = false drop_data = false
queue_redraw() queue_redraw()
func _gui_input(p_event: InputEvent) -> void: func _gui_input(p_event: InputEvent) -> void:
if p_event is InputEventMouseButton: if p_event is InputEventMouseButton:
if p_event.is_pressed(): if p_event.is_pressed():
@@ -780,7 +808,6 @@ class ListEntry extends MarginContainer:
if resource: if resource:
_on_clear() _on_clear()
func _can_drop_data(p_at_position: Vector2, p_data: Variant) -> bool: func _can_drop_data(p_at_position: Vector2, p_data: Variant) -> bool:
drop_data = false drop_data = false
if typeof(p_data) == TYPE_DICTIONARY: if typeof(p_data) == TYPE_DICTIONARY:
@@ -789,7 +816,6 @@ class ListEntry extends MarginContainer:
drop_data = true drop_data = true
return drop_data return drop_data
func _drop_data(p_at_position: Vector2, p_data: Variant) -> void: func _drop_data(p_at_position: Vector2, p_data: Variant) -> void:
if typeof(p_data) == TYPE_DICTIONARY: if typeof(p_data) == TYPE_DICTIONARY:
var res: Resource = load(p_data.files[0]) var res: Resource = load(p_data.files[0])
@@ -816,7 +842,6 @@ class ListEntry extends MarginContainer:
emit_signal("clicked") emit_signal("clicked")
emit_signal("inspected", resource) emit_signal("inspected", resource)
func set_edited_resource(p_res: Resource, p_no_signal: bool = true) -> void: func set_edited_resource(p_res: Resource, p_no_signal: bool = true) -> void:
resource = p_res resource = p_res
if resource: if resource:
@@ -836,12 +861,10 @@ class ListEntry extends MarginContainer:
if not p_no_signal: if not p_no_signal:
emit_signal("changed", resource) emit_signal("changed", resource)
func _on_resource_changed(_value: int = 0) -> void: func _on_resource_changed(_value: int = 0) -> void:
queue_redraw() queue_redraw()
emit_signal("changed", resource) emit_signal("changed", resource)
func set_selected(value: bool) -> void: func set_selected(value: bool) -> void:
if not is_inside_tree(): if not is_inside_tree():
#push_error("not in tree") #push_error("not in tree")
@@ -851,26 +874,24 @@ class ListEntry extends MarginContainer:
# Handle scrolling to show the selected item # Handle scrolling to show the selected item
await get_tree().process_frame await get_tree().process_frame
if is_inside_tree(): if is_inside_tree():
get_parent().get_parent().get_v_scroll_bar().ratio = position.y / get_parent().size.y get_parent().get_parent().get_v_scroll_bar().ratio = (
position.y / get_parent().size.y
)
queue_redraw() queue_redraw()
func _on_clear() -> void: func _on_clear() -> void:
if resource: if resource:
name_label.hide() name_label.hide()
set_edited_resource(null, false) set_edited_resource(null, false)
func _on_edit() -> void: func _on_edit() -> void:
emit_signal("clicked") emit_signal("clicked")
emit_signal("inspected", resource) emit_signal("inspected", resource)
func _on_enable() -> void: func _on_enable() -> void:
if resource is Terrain3DMeshAsset: if resource is Terrain3DMeshAsset:
resource.set_enabled(!resource.is_enabled()) resource.set_enabled(!resource.is_enabled())
func _format_number(num: int) -> String: func _format_number(num: int) -> String:
var is_negative: bool = num < 0 var is_negative: bool = num < 0
var str_num: String = str(abs(num)) var str_num: String = str(abs(num))
+65 -60
View File
@@ -98,7 +98,7 @@ func initialize(p_plugin: EditorPlugin) -> void:
placement_opt.item_selected.connect(set_slot) placement_opt.item_selected.connect(set_slot)
floating_btn.pressed.connect(make_dock_float) floating_btn.pressed.connect(make_dock_float)
pinned_btn.toggled.connect(_on_pin_changed) pinned_btn.toggled.connect(_on_pin_changed)
pinned_btn.visible = ( window != null ) pinned_btn.visible = (window != null)
size_slider.value_changed.connect(_on_slider_changed) size_slider.value_changed.connect(_on_slider_changed)
plugin.ui.toolbar.tool_changed.connect(_on_tool_changed) plugin.ui.toolbar.tool_changed.connect(_on_tool_changed)
@@ -127,12 +127,18 @@ func _ready() -> void:
confirm_dialog = ConfirmationDialog.new() confirm_dialog = ConfirmationDialog.new()
add_child(confirm_dialog) add_child(confirm_dialog)
confirm_dialog.hide() confirm_dialog.hide()
confirm_dialog.confirmed.connect(func(): _confirmed = true; \ confirm_dialog.confirmed.connect(
emit_signal("confirmation_closed"); \ func():
emit_signal("confirmation_confirmed") ) _confirmed = true
confirm_dialog.canceled.connect(func(): _confirmed = false; \ emit_signal("confirmation_closed")
emit_signal("confirmation_closed"); \ emit_signal("confirmation_confirmed")
emit_signal("confirmation_canceled") ) )
confirm_dialog.canceled.connect(
func():
_confirmed = false
emit_signal("confirmation_closed")
emit_signal("confirmation_canceled")
)
func get_current_list() -> ListContainer: func get_current_list() -> ListContainer:
@@ -141,8 +147,9 @@ func get_current_list() -> ListContainer:
## Dock placement ## Dock placement
func set_slot(p_slot: int) -> void: func set_slot(p_slot: int) -> void:
p_slot = clamp(p_slot, 0, POS_MAX-1) p_slot = clamp(p_slot, 0, POS_MAX - 1)
if slot != p_slot: if slot != p_slot:
slot = p_slot slot = p_slot
@@ -177,7 +184,7 @@ func remove_dock(p_force: bool = false) -> void:
pinned_btn.visible = false pinned_btn.visible = false
placement_opt.visible = true placement_opt.visible = true
state = HIDDEN state = HIDDEN
update_dock() # return window to side/bottom update_dock() # return window to side/bottom
func update_dock() -> void: func update_dock() -> void:
@@ -207,7 +214,7 @@ func update_layout() -> void:
if not window and get_parent() and get_parent().get_parent() is Window: if not window and get_parent() and get_parent().get_parent() is Window:
window = get_parent().get_parent() window = get_parent().get_parent()
make_dock_float() make_dock_float()
return # Will call this function again upon display return # Will call this function again upon display
var size_parent: Control = size_slider.get_parent() var size_parent: Control = size_slider.get_parent()
# Vertical layout in window / sidebar # Vertical layout in window / sidebar
@@ -238,8 +245,10 @@ func update_layout() -> void:
func update_thumbnails() -> void: func update_thumbnails() -> void:
if not is_instance_valid(plugin.terrain): if not is_instance_valid(plugin.terrain):
return return
if current_list.type == Terrain3DAssets.TYPE_MESH and \ if (
Time.get_ticks_msec() - _last_thumb_update_time > MAX_UPDATE_TIME: current_list.type == Terrain3DAssets.TYPE_MESH
and Time.get_ticks_msec() - _last_thumb_update_time > MAX_UPDATE_TIME
):
plugin.terrain.assets.create_mesh_thumbnails() plugin.terrain.assets.create_mesh_thumbnails()
_last_thumb_update_time = Time.get_ticks_msec() _last_thumb_update_time = Time.get_ticks_msec()
for mesh_asset in mesh_list.entries: for mesh_asset in mesh_list.entries:
@@ -293,7 +302,7 @@ func _on_meshes_pressed() -> void:
func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor.Operation) -> void: func _on_tool_changed(p_tool: Terrain3DEditor.Tool, p_operation: Terrain3DEditor.Operation) -> void:
if p_tool == Terrain3DEditor.INSTANCER: if p_tool == Terrain3DEditor.INSTANCER:
_on_meshes_pressed() _on_meshes_pressed()
elif p_tool in [ Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS ]: elif p_tool in [Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS]:
_on_textures_pressed() _on_textures_pressed()
@@ -324,7 +333,7 @@ func make_dock_float() -> void:
create_window() create_window()
state = WINDOWED state = WINDOWED
visible = true # Asset dock contents are hidden when popping out of the bottom! visible = true # Asset dock contents are hidden when popping out of the bottom!
pinned_btn.visible = true pinned_btn.visible = true
floating_btn.visible = false floating_btn.visible = false
placement_opt.visible = false placement_opt.visible = false
@@ -366,13 +375,18 @@ func clamp_window_position() -> void:
bounds = DisplayServer.screen_get_position(window.current_screen) bounds = DisplayServer.screen_get_position(window.current_screen)
bounds += DisplayServer.screen_get_size(window.current_screen) bounds += DisplayServer.screen_get_size(window.current_screen)
var margin: int = 40 var margin: int = 40
window.position.x = clamp(window.position.x, -window.size.x + 2*margin, bounds.x - margin) window.position.x = clamp(window.position.x, -window.size.x + 2 * margin, bounds.x - margin)
window.position.y = clamp(window.position.y, 25, bounds.y - margin) window.position.y = clamp(window.position.y, 25, bounds.y - margin)
func _on_window_input(event: InputEvent) -> void: func _on_window_input(event: InputEvent) -> void:
# Capture CTRL+S when doc focused to save scene # Capture CTRL+S when doc focused to save scene
if event is InputEventKey and event.keycode == KEY_S and event.pressed and event.is_command_or_control_pressed(): if (
event is InputEventKey
and event.keycode == KEY_S
and event.pressed
and event.is_command_or_control_pressed()
):
save_editor_settings() save_editor_settings()
EditorInterface.save_scene() EditorInterface.save_scene()
@@ -385,7 +399,10 @@ func _on_godot_window_entered() -> void:
func _on_godot_focus_entered() -> void: func _on_godot_focus_entered() -> void:
# If asset dock is windowed, and Godot was minimized, and now is not, restore asset dock window # If asset dock is windowed, and Godot was minimized, and now is not, restore asset dock window
if is_instance_valid(window): if is_instance_valid(window):
if _godot_last_state == Window.MODE_MINIMIZED and plugin.godot_editor_window.mode != Window.MODE_MINIMIZED: if (
_godot_last_state == Window.MODE_MINIMIZED
and plugin.godot_editor_window.mode != Window.MODE_MINIMIZED
):
window.show() window.show()
_godot_last_state = plugin.godot_editor_window.mode _godot_last_state = plugin.godot_editor_window.mode
plugin.godot_editor_window.grab_focus() plugin.godot_editor_window.grab_focus()
@@ -399,6 +416,7 @@ func _on_godot_focus_exited() -> void:
## Manage Editor Settings ## Manage Editor Settings
func load_editor_settings() -> void: func load_editor_settings() -> void:
floating_btn.button_pressed = plugin.get_setting(ES_DOCK_FLOATING, false) floating_btn.button_pressed = plugin.get_setting(ES_DOCK_FLOATING, false)
pinned_btn.button_pressed = plugin.get_setting(ES_DOCK_PINNED, true) pinned_btn.button_pressed = plugin.get_setting(ES_DOCK_PINNED, true)
@@ -432,7 +450,8 @@ func save_editor_settings() -> void:
############################################################## ##############################################################
class ListContainer extends Container: class ListContainer:
extends Container
var plugin: EditorPlugin var plugin: EditorPlugin
var type := Terrain3DAssets.TYPE_TEXTURE var type := Terrain3DAssets.TYPE_TEXTURE
var entries: Array[ListEntry] var entries: Array[ListEntry]
@@ -441,19 +460,16 @@ class ListContainer extends Container:
var width: float = 83 var width: float = 83
var focus_style: StyleBox var focus_style: StyleBox
func _ready() -> void: func _ready() -> void:
set_v_size_flags(SIZE_EXPAND_FILL) set_v_size_flags(SIZE_EXPAND_FILL)
set_h_size_flags(SIZE_EXPAND_FILL) set_h_size_flags(SIZE_EXPAND_FILL)
func clear() -> void: func clear() -> void:
for e in entries: for e in entries:
e.get_parent().remove_child(e) e.get_parent().remove_child(e)
e.queue_free() e.queue_free()
entries.clear() entries.clear()
func update_asset_list() -> void: func update_asset_list() -> void:
clear() clear()
@@ -461,7 +477,10 @@ class ListContainer extends Container:
var t: Terrain3D var t: Terrain3D
if plugin.is_terrain_valid(): if plugin.is_terrain_valid():
t = plugin.terrain t = plugin.terrain
elif is_instance_valid(plugin._last_terrain) and plugin.is_terrain_valid(plugin._last_terrain): elif (
is_instance_valid(plugin._last_terrain)
and plugin.is_terrain_valid(plugin._last_terrain)
):
t = plugin._last_terrain t = plugin._last_terrain
else: else:
return return
@@ -486,7 +505,6 @@ class ListContainer extends Container:
if selected_id >= mesh_count or selected_id < 0: if selected_id >= mesh_count or selected_id < 0:
set_selected_id(0) set_selected_id(0)
func add_item(p_resource: Resource = null, p_assets: Terrain3DAssets = null) -> void: func add_item(p_resource: Resource = null, p_assets: Terrain3DAssets = null) -> void:
var entry: ListEntry = ListEntry.new() var entry: ListEntry = ListEntry.new()
entry.focus_style = focus_style entry.focus_style = focus_style
@@ -507,17 +525,16 @@ class ListContainer extends Container:
if not p_resource.id_changed.is_connected(set_selected_after_swap): if not p_resource.id_changed.is_connected(set_selected_after_swap):
p_resource.id_changed.connect(set_selected_after_swap) p_resource.id_changed.connect(set_selected_after_swap)
func _on_resource_hovered(p_id: int): func _on_resource_hovered(p_id: int):
if type == Terrain3DAssets.TYPE_MESH: if type == Terrain3DAssets.TYPE_MESH:
if plugin.terrain: if plugin.terrain:
plugin.terrain.assets.create_mesh_thumbnails(p_id) plugin.terrain.assets.create_mesh_thumbnails(p_id)
func set_selected_after_swap(
func set_selected_after_swap(p_type: Terrain3DAssets.AssetType, p_old_id: int, p_new_id: int) -> void: p_type: Terrain3DAssets.AssetType, p_old_id: int, p_new_id: int
) -> void:
set_selected_id(clamp(p_new_id, 0, entries.size() - 2)) set_selected_id(clamp(p_new_id, 0, entries.size() - 2))
func set_selected_id(p_id: int) -> void: func set_selected_id(p_id: int) -> void:
selected_id = p_id selected_id = p_id
@@ -528,14 +545,22 @@ class ListContainer extends Container:
plugin.select_terrain() plugin.select_terrain()
# Select Paint tool if clicking a texture # Select Paint tool if clicking a texture
if type == Terrain3DAssets.TYPE_TEXTURE and \ if (
not plugin.editor.get_tool() in [ Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS ]: type == Terrain3DAssets.TYPE_TEXTURE
and not (
plugin.editor.get_tool()
in [Terrain3DEditor.TEXTURE, Terrain3DEditor.COLOR, Terrain3DEditor.ROUGHNESS]
)
):
var paint_btn: Button = plugin.ui.toolbar.get_node_or_null("PaintTexture") var paint_btn: Button = plugin.ui.toolbar.get_node_or_null("PaintTexture")
if paint_btn: if paint_btn:
paint_btn.set_pressed(true) paint_btn.set_pressed(true)
plugin.ui._on_tool_changed(Terrain3DEditor.TEXTURE, Terrain3DEditor.REPLACE) plugin.ui._on_tool_changed(Terrain3DEditor.TEXTURE, Terrain3DEditor.REPLACE)
elif type == Terrain3DAssets.TYPE_MESH and plugin.editor.get_tool() != Terrain3DEditor.INSTANCER: elif (
type == Terrain3DAssets.TYPE_MESH
and plugin.editor.get_tool() != Terrain3DEditor.INSTANCER
):
var instancer_btn: Button = plugin.ui.toolbar.get_node_or_null("InstanceMeshes") var instancer_btn: Button = plugin.ui.toolbar.get_node_or_null("InstanceMeshes")
if instancer_btn: if instancer_btn:
instancer_btn.set_pressed(true) instancer_btn.set_pressed(true)
@@ -544,12 +569,10 @@ class ListContainer extends Container:
# Update editor with selected brush # Update editor with selected brush
plugin.ui._on_setting_changed() plugin.ui._on_setting_changed()
func _on_resource_inspected(p_resource: Resource) -> void: func _on_resource_inspected(p_resource: Resource) -> void:
await get_tree().create_timer(.01).timeout await get_tree().create_timer(.01).timeout
EditorInterface.edit_resource(p_resource) EditorInterface.edit_resource(p_resource)
func _on_resource_changed(p_resource: Resource, p_id: int) -> void: func _on_resource_changed(p_resource: Resource, p_id: int) -> void:
if not p_resource: if not p_resource:
var asset_dock: Control = get_parent().get_parent().get_parent() var asset_dock: Control = get_parent().get_parent().get_parent()
@@ -582,28 +605,23 @@ class ListContainer extends Container:
# If null resource, remove last # If null resource, remove last
if not p_resource: if not p_resource:
var last_offset: int = 2 var last_offset: int = 2
if p_id == entries.size()-2: if p_id == entries.size() - 2:
last_offset = 3 last_offset = 3
set_selected_id(clamp(selected_id, 0, entries.size() - last_offset)) set_selected_id(clamp(selected_id, 0, entries.size() - last_offset))
func get_selected_id() -> int: func get_selected_id() -> int:
return selected_id return selected_id
func get_selected_asset_id() -> int: func get_selected_asset_id() -> int:
return selected_id return selected_id
func set_entry_width(value: float) -> void: func set_entry_width(value: float) -> void:
width = clamp(value, 66, 230) width = clamp(value, 66, 230)
redraw() redraw()
func get_entry_width() -> float: func get_entry_width() -> float:
return width return width
func redraw() -> void: func redraw() -> void:
height = 0 height = 0
var id: int = 0 var id: int = 0
@@ -614,17 +632,17 @@ class ListContainer extends Container:
for c in get_children(): for c in get_children():
if is_instance_valid(c): if is_instance_valid(c):
c.size = Vector2(width, width) - Vector2(separation, separation) c.size = Vector2(width, width) - Vector2(separation, separation)
c.position = Vector2(id % columns, id / columns) * width + \ c.position = (
Vector2(separation / columns, separation / columns) Vector2(id % columns, id / columns) * width
+ Vector2(separation / columns, separation / columns)
)
height = max(height, c.position.y + width) height = max(height, c.position.y + width)
id += 1 id += 1
# Needed to enable ScrollContainer scroll bar # Needed to enable ScrollContainer scroll bar
func _get_minimum_size() -> Vector2: func _get_minimum_size() -> Vector2:
return Vector2(0, height) return Vector2(0, height)
func _notification(p_what) -> void: func _notification(p_what) -> void:
if p_what == NOTIFICATION_SORT_CHILDREN: if p_what == NOTIFICATION_SORT_CHILDREN:
redraw() redraw()
@@ -635,9 +653,10 @@ class ListContainer extends Container:
############################################################## ##############################################################
class ListEntry extends VBoxContainer: class ListEntry:
signal hovered() extends VBoxContainer
signal selected() signal hovered
signal selected
signal changed(resource: Resource) signal changed(resource: Resource)
signal inspected(resource: Resource) signal inspected(resource: Resource)
@@ -664,14 +683,12 @@ class ListEntry extends VBoxContainer:
@onready var background: StyleBox = get_theme_stylebox("pressed", "Button") @onready var background: StyleBox = get_theme_stylebox("pressed", "Button")
@onready var focus_style: StyleBox = get_theme_stylebox("focus", "Button").duplicate() @onready var focus_style: StyleBox = get_theme_stylebox("focus", "Button").duplicate()
func _ready() -> void: func _ready() -> void:
setup_buttons() setup_buttons()
setup_label() setup_label()
focus_style.set_border_width_all(2) focus_style.set_border_width_all(2)
focus_style.set_border_color(Color(1, 1, 1, .67)) focus_style.set_border_color(Color(1, 1, 1, .67))
func setup_buttons() -> void: func setup_buttons() -> void:
var icon_size: Vector2 = Vector2(12, 12) var icon_size: Vector2 = Vector2(12, 12)
var margin_container := MarginContainer.new() var margin_container := MarginContainer.new()
@@ -717,7 +734,6 @@ class ListEntry extends VBoxContainer:
button_clear.pressed.connect(clear) button_clear.pressed.connect(clear)
button_row.add_child(button_clear) button_row.add_child(button_clear)
func setup_label() -> void: func setup_label() -> void:
name_label = Label.new() name_label = Label.new()
add_child(name_label, true) add_child(name_label, true)
@@ -737,7 +753,6 @@ class ListEntry extends VBoxContainer:
else: else:
name_label.text = "Add Mesh" name_label.text = "Add Mesh"
func _notification(p_what) -> void: func _notification(p_what) -> void:
match p_what: match p_what:
NOTIFICATION_DRAW: NOTIFICATION_DRAW:
@@ -766,7 +781,7 @@ class ListEntry extends VBoxContainer:
else: else:
draw_rect(rect, Color(.15, .15, .15, 1.)) draw_rect(rect, Color(.15, .15, .15, 1.))
button_enabled.set_pressed_no_signal(!resource.is_enabled()) button_enabled.set_pressed_no_signal(!resource.is_enabled())
name_label.add_theme_font_size_override("font_size", 4 + rect.size.x/10) name_label.add_theme_font_size_override("font_size", 4 + rect.size.x / 10)
if drop_data: if drop_data:
draw_style_box(focus_style, rect) draw_style_box(focus_style, rect)
if is_hovered: if is_hovered:
@@ -784,7 +799,6 @@ class ListEntry extends VBoxContainer:
drop_data = false drop_data = false
queue_redraw() queue_redraw()
func _gui_input(p_event: InputEvent) -> void: func _gui_input(p_event: InputEvent) -> void:
if p_event is InputEventMouseButton: if p_event is InputEventMouseButton:
if p_event.is_pressed(): if p_event.is_pressed():
@@ -806,7 +820,6 @@ class ListEntry extends VBoxContainer:
if resource: if resource:
clear() clear()
func _can_drop_data(p_at_position: Vector2, p_data: Variant) -> bool: func _can_drop_data(p_at_position: Vector2, p_data: Variant) -> bool:
drop_data = false drop_data = false
if typeof(p_data) == TYPE_DICTIONARY: if typeof(p_data) == TYPE_DICTIONARY:
@@ -815,7 +828,6 @@ class ListEntry extends VBoxContainer:
drop_data = true drop_data = true
return drop_data return drop_data
func _drop_data(p_at_position: Vector2, p_data: Variant) -> void: func _drop_data(p_at_position: Vector2, p_data: Variant) -> void:
if typeof(p_data) == TYPE_DICTIONARY: if typeof(p_data) == TYPE_DICTIONARY:
var res: Resource = load(p_data.files[0]) var res: Resource = load(p_data.files[0])
@@ -844,8 +856,6 @@ class ListEntry extends VBoxContainer:
emit_signal("selected") emit_signal("selected")
emit_signal("inspected", resource) emit_signal("inspected", resource)
func set_edited_resource(p_res: Resource, p_no_signal: bool = true) -> void: func set_edited_resource(p_res: Resource, p_no_signal: bool = true) -> void:
resource = p_res resource = p_res
if resource: if resource:
@@ -861,27 +871,22 @@ class ListEntry extends VBoxContainer:
if !p_no_signal: if !p_no_signal:
emit_signal("changed", resource) emit_signal("changed", resource)
func _on_resource_changed() -> void: func _on_resource_changed() -> void:
queue_redraw() queue_redraw()
emit_signal("changed", resource) emit_signal("changed", resource)
func set_selected(value: bool) -> void: func set_selected(value: bool) -> void:
is_selected = value is_selected = value
queue_redraw() queue_redraw()
func clear() -> void: func clear() -> void:
if resource: if resource:
set_edited_resource(null, false) set_edited_resource(null, false)
func edit() -> void: func edit() -> void:
emit_signal("selected") emit_signal("selected")
emit_signal("inspected", resource) emit_signal("inspected", resource)
func enable() -> void: func enable() -> void:
if resource is Terrain3DMeshAsset: if resource is Terrain3DMeshAsset:
resource.set_enabled(!resource.is_enabled()) resource.set_enabled(!resource.is_enabled())
+8 -6
View File
@@ -8,7 +8,7 @@ extends Control
signal value_changed(Vector2) signal value_changed(Vector2)
var label: Label var label: Label
var suffix: String var suffix: String
var grabbed_handle: int = 0 # -1 left, 0 none, 1 right var grabbed_handle: int = 0 # -1 left, 0 none, 1 right
var min_value: float = 0.0 var min_value: float = 0.0
var max_value: float = 100.0 var max_value: float = 100.0
var step: float = 1.0 var step: float = 1.0
@@ -99,7 +99,7 @@ func _get_handle() -> int:
func _gui_input(p_event: InputEvent) -> void: func _gui_input(p_event: InputEvent) -> void:
if p_event is InputEventMouseButton: if p_event is InputEventMouseButton:
var button: int = p_event.get_button_index() var button: int = p_event.get_button_index()
if button in [ MOUSE_BUTTON_LEFT, MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN ]: if button in [MOUSE_BUTTON_LEFT, MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN]:
if p_event.is_pressed(): if p_event.is_pressed():
var mid_point = (range.x + range.y) / 2.0 var mid_point = (range.x + range.y) / 2.0
var xpos: float = p_event.get_position().x * 2.0 var xpos: float = p_event.get_position().x * 2.0
@@ -125,8 +125,10 @@ func _gui_input(p_event: InputEvent) -> void:
func set_slider(p_xpos: float, p_relative: bool = false) -> void: func set_slider(p_xpos: float, p_relative: bool = false) -> void:
if grabbed_handle == 0: if grabbed_handle == 0:
return return
var xpos_step: float = clamp(snappedf((p_xpos / size.x) * max_value, step), min_value, max_value) var xpos_step: float = clamp(
if(grabbed_handle < 0): snappedf((p_xpos / size.x) * max_value, step), min_value, max_value
)
if grabbed_handle < 0:
if p_relative: if p_relative:
range.x += p_xpos range.x += p_xpos
else: else:
@@ -156,8 +158,8 @@ func _notification(p_what: int) -> void:
# Draw handles, slightly in so they don't get on the outside edges # Draw handles, slightly in so they don't get on the outside edges
var handle_pos: Vector2 var handle_pos: Vector2
handle_pos.x = clamp(startx - handle.get_size().x/2, -10, size.x) handle_pos.x = clamp(startx - handle.get_size().x / 2, -10, size.x)
handle_pos.y = clamp(endx - handle.get_size().x/2, 0, size.x - 10) handle_pos.y = clamp(endx - handle.get_size().x / 2, 0, size.x - 10)
draw_texture(handle, Vector2(handle_pos.x, -mid_y - 10 * (display_scale - 1.))) draw_texture(handle, Vector2(handle_pos.x, -mid_y - 10 * (display_scale - 1.)))
draw_texture(handle, Vector2(handle_pos.y, -mid_y - 10 * (display_scale - 1.))) draw_texture(handle, Vector2(handle_pos.y, -mid_y - 10 * (display_scale - 1.)))
+91 -48
View File
@@ -3,7 +3,6 @@
@tool @tool
extends EditorPlugin extends EditorPlugin
# Includes # Includes
const UI: Script = preload("res://addons/terrain_3d/src/ui.gd") const UI: Script = preload("res://addons/terrain_3d/src/ui.gd")
const RegionGizmo: Script = preload("res://addons/terrain_3d/src/region_gizmo.gd") const RegionGizmo: Script = preload("res://addons/terrain_3d/src/region_gizmo.gd")
@@ -14,7 +13,7 @@ var modifier_ctrl: bool
var modifier_alt: bool var modifier_alt: bool
var modifier_shift: bool var modifier_shift: bool
var _last_modifiers: int = 0 var _last_modifiers: int = 0
var _input_mode: int = 0 # -1: camera move, 0: none, 1: operating var _input_mode: int = 0 # -1: camera move, 0: none, 1: operating
var rmb_release_time: int = 0 var rmb_release_time: int = 0
var _use_meta: bool = false var _use_meta: bool = false
@@ -22,15 +21,15 @@ var terrain: Terrain3D
var _last_terrain: Terrain3D var _last_terrain: Terrain3D
var nav_region: NavigationRegion3D var nav_region: NavigationRegion3D
var debug: int = 0 # Set in _edit() var debug: int = 0 # Set in _edit()
var editor: Terrain3DEditor var editor: Terrain3DEditor
var editor_settings: EditorSettings var editor_settings: EditorSettings
var ui: Node # Terrain3DUI see Godot #75388 var ui: Node # Terrain3DUI see Godot #75388
var asset_dock: PanelContainer var asset_dock: PanelContainer
var region_gizmo: RegionGizmo var region_gizmo: RegionGizmo
var current_region_position: Vector2 var current_region_position: Vector2
var mouse_global_position: Vector3 = Vector3.ZERO var mouse_global_position: Vector3 = Vector3.ZERO
var godot_editor_window: Window # The Godot Editor window var godot_editor_window: Window # The Godot Editor window
func _init() -> void: func _init() -> void:
@@ -193,19 +192,23 @@ func _forward_3d_gui_input(p_viewport_camera: Camera3D, p_event: InputEvent) ->
var t = -Vector3(0, 1, 0).dot(camera_pos) / Vector3(0, 1, 0).dot(camera_dir) var t = -Vector3(0, 1, 0).dot(camera_pos) / Vector3(0, 1, 0).dot(camera_dir)
mouse_global_position = (camera_pos + t * camera_dir) mouse_global_position = (camera_pos + t * camera_dir)
else: else:
#Else look for intersection with terrain #Else look for intersection with terrain
var intersection_point: Vector3 = terrain.get_intersection(camera_pos, camera_dir, true) var intersection_point: Vector3 = terrain.get_intersection(camera_pos, camera_dir, true)
if intersection_point.z > 3.4e38 or is_nan(intersection_point.y): # max double or nan if intersection_point.z > 3.4e38 or is_nan(intersection_point.y): # max double or nan
return AFTER_GUI_INPUT_PASS return AFTER_GUI_INPUT_PASS
mouse_global_position = intersection_point mouse_global_position = intersection_point
## Handle mouse movement ## Handle mouse movement
if p_event is InputEventMouseMotion: if p_event is InputEventMouseMotion:
if _input_mode != -1: # Not cam rotation
if _input_mode != -1: # Not cam rotation
## Update region highlight ## Update region highlight
var region_position: Vector2 = ( Vector2(mouse_global_position.x, mouse_global_position.z) \ var region_position: Vector2 = (
/ (terrain.get_region_size() * terrain.get_vertex_spacing()) ).floor() (
Vector2(mouse_global_position.x, mouse_global_position.z)
/ (terrain.get_region_size() * terrain.get_vertex_spacing())
)
. floor()
)
if current_region_position != region_position: if current_region_position != region_position:
current_region_position = region_position current_region_position = region_position
update_region_grid() update_region_grid()
@@ -237,13 +240,17 @@ func _forward_3d_gui_input(p_viewport_camera: Camera3D, p_event: InputEvent) ->
# Skip regions that already exist or don't # Skip regions that already exist or don't
var has_region: bool = terrain.data.has_regionp(mouse_global_position) var has_region: bool = terrain.data.has_regionp(mouse_global_position)
var op: int = editor.get_operation() var op: int = editor.get_operation()
if ( has_region and op == Terrain3DEditor.ADD) or \ if (
( not has_region and op == Terrain3DEditor.SUBTRACT ): (has_region and op == Terrain3DEditor.ADD)
or (not has_region and op == Terrain3DEditor.SUBTRACT)
):
return AFTER_GUI_INPUT_STOP return AFTER_GUI_INPUT_STOP
# If an automatic operation is ready to go (e.g. gradient) # If an automatic operation is ready to go (e.g. gradient)
if ui.operation_builder and ui.operation_builder.is_ready(): if ui.operation_builder and ui.operation_builder.is_ready():
ui.operation_builder.apply_operation(editor, mouse_global_position, p_viewport_camera.rotation.y) ui.operation_builder.apply_operation(
editor, mouse_global_position, p_viewport_camera.rotation.y
)
return AFTER_GUI_INPUT_STOP return AFTER_GUI_INPUT_STOP
# Mouse clicked, start editing # Mouse clicked, start editing
@@ -261,30 +268,55 @@ func _forward_3d_gui_input(p_viewport_camera: Camera3D, p_event: InputEvent) ->
func _read_input(p_event: InputEvent = null) -> AfterGUIInput: func _read_input(p_event: InputEvent = null) -> AfterGUIInput:
## Determine if user is moving camera or applying ## Determine if user is moving camera or applying
if Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) or \ if (
p_event is InputEventMouseButton and p_event.is_released() and \ Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)
p_event.get_button_index() == MOUSE_BUTTON_LEFT: or (
_input_mode = 1 p_event is InputEventMouseButton
and p_event.is_released()
and p_event.get_button_index() == MOUSE_BUTTON_LEFT
)
):
_input_mode = 1
else: else:
_input_mode = 0 _input_mode = 0
match get_setting("editors/3d/navigation/navigation_scheme", 0): match get_setting("editors/3d/navigation/navigation_scheme", 0):
2, 1: # Modo, Maya 2, 1: # Modo, Maya
if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) or \ if (
( Input.is_key_pressed(KEY_ALT) and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) ): Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)
_input_mode = -1 or (
if p_event is InputEventMouseButton and p_event.is_released() and \ Input.is_key_pressed(KEY_ALT)
( p_event.get_button_index() == MOUSE_BUTTON_RIGHT or \ and Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT)
( Input.is_key_pressed(KEY_ALT) and p_event.get_button_index() == MOUSE_BUTTON_LEFT )): )
rmb_release_time = Time.get_ticks_msec() ):
0, _: # Godot _input_mode = -1
if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT) or \ if (
Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE): p_event is InputEventMouseButton
_input_mode = -1 and p_event.is_released()
if p_event is InputEventMouseButton and p_event.is_released() and \ and (
( p_event.get_button_index() == MOUSE_BUTTON_RIGHT or \ p_event.get_button_index() == MOUSE_BUTTON_RIGHT
p_event.get_button_index() == MOUSE_BUTTON_MIDDLE ): or (
rmb_release_time = Time.get_ticks_msec() Input.is_key_pressed(KEY_ALT)
and p_event.get_button_index() == MOUSE_BUTTON_LEFT
)
)
):
rmb_release_time = Time.get_ticks_msec()
0, _: # Godot
if (
Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT)
or Input.is_mouse_button_pressed(MOUSE_BUTTON_MIDDLE)
):
_input_mode = -1
if (
p_event is InputEventMouseButton
and p_event.is_released()
and (
p_event.get_button_index() == MOUSE_BUTTON_RIGHT
or p_event.get_button_index() == MOUSE_BUTTON_MIDDLE
)
):
rmb_release_time = Time.get_ticks_msec()
if _input_mode < 0: if _input_mode < 0:
# Camera is moving, skip input # Camera is moving, skip input
return AFTER_GUI_INPUT_PASS return AFTER_GUI_INPUT_PASS
@@ -301,26 +333,32 @@ func _read_input(p_event: InputEvent = null) -> AfterGUIInput:
# Keybind enum: Alt,Space,Meta,Capslock # Keybind enum: Alt,Space,Meta,Capslock
var alt_key: int var alt_key: int
match get_setting("terrain3d/config/alt_key_bind", 0): match get_setting("terrain3d/config/alt_key_bind", 0):
3: alt_key = KEY_CAPSLOCK 3:
2: alt_key = KEY_META alt_key = KEY_CAPSLOCK
1: alt_key = KEY_SPACE 2:
0, _: alt_key = KEY_ALT alt_key = KEY_META
1:
alt_key = KEY_SPACE
0, _:
alt_key = KEY_ALT
modifier_alt = Input.is_key_pressed(alt_key) modifier_alt = Input.is_key_pressed(alt_key)
var current_mods: int = int(modifier_shift) | int(modifier_ctrl) << 1 | int(modifier_alt) << 2 var current_mods: int = int(modifier_shift) | int(modifier_ctrl) << 1 | int(modifier_alt) << 2
## Process Hotkeys ## Process Hotkeys
if p_event is InputEventKey and \ if (
current_mods == 0 and \ p_event is InputEventKey
p_event.is_pressed() and \ and current_mods == 0
not p_event.is_echo() and \ and p_event.is_pressed()
consume_hotkey(p_event.keycode): and not p_event.is_echo()
and consume_hotkey(p_event.keycode)
):
# Hotkey found, consume event, and stop input processing # Hotkey found, consume event, and stop input processing
EditorInterface.get_editor_viewport_3d().set_input_as_handled() EditorInterface.get_editor_viewport_3d().set_input_as_handled()
return AFTER_GUI_INPUT_STOP return AFTER_GUI_INPUT_STOP
# Brush data is cleared on set_tool, or clicking textures in the asset dock # Brush data is cleared on set_tool, or clicking textures in the asset dock
# Update modifiers if changed or missing # Update modifiers if changed or missing
if _last_modifiers != current_mods or not ui.brush_data.has("modifier_shift"): if _last_modifiers != current_mods or not ui.brush_data.has("modifier_shift"):
_last_modifiers = current_mods _last_modifiers = current_mods
ui.brush_data["modifier_shift"] = modifier_shift ui.brush_data["modifier_shift"] = modifier_shift
ui.brush_data["modifier_ctrl"] = modifier_ctrl ui.brush_data["modifier_ctrl"] = modifier_ctrl
@@ -426,9 +464,14 @@ func is_terrain_valid(p_terrain: Terrain3D = null) -> bool:
func is_selected() -> bool: func is_selected() -> bool:
var selected: Array[Node] = EditorInterface.get_selection().get_selected_nodes() var selected: Array[Node] = EditorInterface.get_selection().get_selected_nodes()
for node in selected: for node in selected:
if ( is_instance_valid(_last_terrain) and node.get_instance_id() == _last_terrain.get_instance_id() ) or \ if (
node is Terrain3D: (
return true is_instance_valid(_last_terrain)
and node.get_instance_id() == _last_terrain.get_instance_id()
)
or node is Terrain3D
):
return true
return false return false
@@ -2,7 +2,6 @@
# Gradient Operation Builder for Terrain3D # Gradient Operation Builder for Terrain3D
extends "res://addons/terrain_3d/src/operation_builder.gd" extends "res://addons/terrain_3d/src/operation_builder.gd"
const MultiPicker: Script = preload("res://addons/terrain_3d/src/multi_picker.gd") const MultiPicker: Script = preload("res://addons/terrain_3d/src/multi_picker.gd")
@@ -31,7 +30,9 @@ func is_ready() -> bool:
return _get_point_picker().all_points_selected() and not _is_drawable() return _get_point_picker().all_points_selected() and not _is_drawable()
func apply_operation(p_editor: Terrain3DEditor, p_global_position: Vector3, p_camera_direction: float) -> void: func apply_operation(
p_editor: Terrain3DEditor, p_global_position: Vector3, p_camera_direction: float
) -> void:
var points: PackedVector3Array = _get_point_picker().get_points() var points: PackedVector3Array = _get_point_picker().get_points()
assert(points.size() == 2) assert(points.size() == 2)
assert(not _is_drawable()) assert(not _is_drawable())
-3
View File
@@ -2,15 +2,12 @@
# Multipicker for Terrain3D # Multipicker for Terrain3D
extends HBoxContainer extends HBoxContainer
signal pressed signal pressed
signal value_changed signal value_changed
const ICON_PICKER_CHECKED: String = "res://addons/terrain_3d/icons/picker_checked.svg" const ICON_PICKER_CHECKED: String = "res://addons/terrain_3d/icons/picker_checked.svg"
const MAX_POINTS: int = 2 const MAX_POINTS: int = 2
var icon_picker: Texture2D var icon_picker: Texture2D
var icon_picker_checked: Texture2D var icon_picker_checked: Texture2D
var points: PackedVector3Array var points: PackedVector3Array
+3 -3
View File
@@ -2,10 +2,8 @@
# Operation Builder for Terrain3D # Operation Builder for Terrain3D
extends RefCounted extends RefCounted
const ToolSettings: Script = preload("res://addons/terrain_3d/src/tool_settings.gd") const ToolSettings: Script = preload("res://addons/terrain_3d/src/tool_settings.gd")
var tool_settings: ToolSettings var tool_settings: ToolSettings
@@ -21,5 +19,7 @@ func is_ready() -> bool:
return false return false
func apply_operation(editor: Terrain3DEditor, p_global_position: Vector3, p_camera_direction: float) -> void: func apply_operation(
editor: Terrain3DEditor, p_global_position: Vector3, p_camera_direction: float
) -> void:
pass pass
+19 -5
View File
@@ -34,9 +34,17 @@ func _redraw() -> void:
if show_rect: if show_rect:
var modulate: Color = main_color if !use_secondary_color else secondary_color var modulate: Color = main_color if !use_secondary_color else secondary_color
if abs(region_position.x) > Terrain3DData.REGION_MAP_SIZE*.5 or abs(region_position.y) > Terrain3DData.REGION_MAP_SIZE*.5: if (
abs(region_position.x) > Terrain3DData.REGION_MAP_SIZE * .5
or abs(region_position.y) > Terrain3DData.REGION_MAP_SIZE * .5
):
modulate = Color.GRAY modulate = Color.GRAY
draw_rect(Vector2(region_size,region_size)*.5 + rect_position, region_size, selection_material, modulate) draw_rect(
Vector2(region_size, region_size) * .5 + rect_position,
region_size,
selection_material,
modulate
)
for pos in grid: for pos in grid:
var grid_tile_position = Vector2(pos) * region_size var grid_tile_position = Vector2(pos) * region_size
@@ -44,12 +52,19 @@ func _redraw() -> void:
# Skip this one, otherwise focused region borders are not always visible due to draw order # Skip this one, otherwise focused region borders are not always visible due to draw order
continue continue
draw_rect(Vector2(region_size,region_size)*.5 + grid_tile_position, region_size, material, grid_color) draw_rect(
Vector2(region_size, region_size) * .5 + grid_tile_position,
region_size,
material,
grid_color
)
draw_rect(Vector2.ZERO, region_size * Terrain3DData.REGION_MAP_SIZE, material, border_color) draw_rect(Vector2.ZERO, region_size * Terrain3DData.REGION_MAP_SIZE, material, border_color)
func draw_rect(p_pos: Vector2, p_size: float, p_material: StandardMaterial3D, p_modulate: Color) -> void: func draw_rect(
p_pos: Vector2, p_size: float, p_material: StandardMaterial3D, p_modulate: Color
) -> void:
var lines: PackedVector3Array = [ var lines: PackedVector3Array = [
Vector3(-1, 0, -1), Vector3(-1, 0, -1),
Vector3(-1, 0, 1), Vector3(-1, 0, 1),
@@ -65,4 +80,3 @@ func draw_rect(p_pos: Vector2, p_size: float, p_material: StandardMaterial3D, p_
lines[i] = ((lines[i] / 2.0) * p_size) + Vector3(p_pos.x, 0, p_pos.y) lines[i] = ((lines[i] / 2.0) * p_size) + Vector3(p_pos.x, 0, p_pos.y)
add_lines(lines, p_material, false, p_modulate) add_lines(lines, p_material, false, p_modulate)
+491 -135
View File
@@ -32,13 +32,13 @@ const ES_TOOL_SETTINGS: String = "terrain3d/tool_settings/"
const NONE: int = 0x0 const NONE: int = 0x0
const ALLOW_LARGER: int = 0x1 const ALLOW_LARGER: int = 0x1
const ALLOW_SMALLER: int = 0x2 const ALLOW_SMALLER: int = 0x2
const ALLOW_OUT_OF_BOUNDS: int = 0x3 # LARGER|SMALLER const ALLOW_OUT_OF_BOUNDS: int = 0x3 # LARGER|SMALLER
const NO_LABEL: int = 0x4 const NO_LABEL: int = 0x4
const ADD_SEPARATOR: int = 0x8 # Add a vertical line before this entry const ADD_SEPARATOR: int = 0x8 # Add a vertical line before this entry
const ADD_SPACER: int = 0x10 # Add a space before this entry const ADD_SPACER: int = 0x10 # Add a space before this entry
const NO_SAVE: int = 0x20 # Don't save this in EditorSettings const NO_SAVE: int = 0x20 # Don't save this in EditorSettings
var plugin: EditorPlugin # Actually Terrain3DEditorPlugin, but Godot still has CRC errors var plugin: EditorPlugin # Actually Terrain3DEditorPlugin, but Godot still has CRC errors
var brush_preview_material: ShaderMaterial var brush_preview_material: ShaderMaterial
var select_brush_button: Button var select_brush_button: Button
var selected_brush_imgs: Array var selected_brush_imgs: Array
@@ -62,103 +62,394 @@ func _ready() -> void:
add_brushes(main_list) add_brushes(main_list)
add_setting({ "name":"instructions", "label":"Click the terrain to add a region. CTRL+Click to remove. Or select another tool on the left.", add_setting(
"type":SettingType.LABEL, "list":main_list, "flags":NO_LABEL|NO_SAVE }) {
"name": "instructions",
"label":
"Click the terrain to add a region. CTRL+Click to remove. Or select another tool on the left.",
"type": SettingType.LABEL,
"list": main_list,
"flags": NO_LABEL | NO_SAVE
}
)
add_setting({ "name":"size", "type":SettingType.SLIDER, "list":main_list, "default":20, "unit":"m", add_setting(
"range":Vector3(0.1, 200, 1), "flags":ALLOW_LARGER|ADD_SPACER }) {
"name": "size",
"type": SettingType.SLIDER,
"list": main_list,
"default": 20,
"unit": "m",
"range": Vector3(0.1, 200, 1),
"flags": ALLOW_LARGER | ADD_SPACER
}
)
add_setting({ "name":"strength", "type":SettingType.SLIDER, "list":main_list, "default":33, add_setting(
"unit":"%", "range":Vector3(1, 100, 1), "flags":ALLOW_LARGER }) {
"name": "strength",
"type": SettingType.SLIDER,
"list": main_list,
"default": 33,
"unit": "%",
"range": Vector3(1, 100, 1),
"flags": ALLOW_LARGER
}
)
add_setting({ "name":"height", "type":SettingType.SLIDER, "list":main_list, "default":20, add_setting(
"unit":"m", "range":Vector3(-500, 500, 0.1), "flags":ALLOW_OUT_OF_BOUNDS }) {
add_setting({ "name":"height_picker", "type":SettingType.PICKER, "list":main_list, "name": "height",
"default":Terrain3DEditor.HEIGHT, "flags":NO_LABEL }) "type": SettingType.SLIDER,
"list": main_list,
"default": 20,
"unit": "m",
"range": Vector3(-500, 500, 0.1),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
add_setting(
{
"name": "height_picker",
"type": SettingType.PICKER,
"list": main_list,
"default": Terrain3DEditor.HEIGHT,
"flags": NO_LABEL
}
)
add_setting({ "name":"color", "type":SettingType.COLOR_SELECT, "list":main_list, add_setting(
"default":Color.WHITE, "flags":ADD_SEPARATOR }) {
add_setting({ "name":"color_picker", "type":SettingType.PICKER, "list":main_list, "name": "color",
"default":Terrain3DEditor.COLOR, "flags":NO_LABEL }) "type": SettingType.COLOR_SELECT,
"list": main_list,
"default": Color.WHITE,
"flags": ADD_SEPARATOR
}
)
add_setting(
{
"name": "color_picker",
"type": SettingType.PICKER,
"list": main_list,
"default": Terrain3DEditor.COLOR,
"flags": NO_LABEL
}
)
add_setting({ "name":"roughness", "type":SettingType.SLIDER, "list":main_list, "default":-65, add_setting(
"unit":"%", "range":Vector3(-100, 100, 1), "flags":ADD_SEPARATOR }) {
add_setting({ "name":"roughness_picker", "type":SettingType.PICKER, "list":main_list, "name": "roughness",
"default":Terrain3DEditor.ROUGHNESS, "flags":NO_LABEL }) "type": SettingType.SLIDER,
"list": main_list,
"default": -65,
"unit": "%",
"range": Vector3(-100, 100, 1),
"flags": ADD_SEPARATOR
}
)
add_setting(
{
"name": "roughness_picker",
"type": SettingType.PICKER,
"list": main_list,
"default": Terrain3DEditor.ROUGHNESS,
"flags": NO_LABEL
}
)
add_setting({ "name":"enable_texture", "label":"Texture", "type":SettingType.CHECKBOX, add_setting(
"list":main_list, "default":true, "flags":ADD_SEPARATOR }) {
"name": "enable_texture",
"label": "Texture",
"type": SettingType.CHECKBOX,
"list": main_list,
"default": true,
"flags": ADD_SEPARATOR
}
)
add_setting({ "name":"texture_filter", "label":"Texture Filter", "type":SettingType.CHECKBOX, add_setting(
"list":main_list, "default":false, "flags":ADD_SEPARATOR }) {
"name": "texture_filter",
"label": "Texture Filter",
"type": SettingType.CHECKBOX,
"list": main_list,
"default": false,
"flags": ADD_SEPARATOR
}
)
add_setting({ "name":"margin", "type":SettingType.SLIDER, "list":main_list, "default":0, add_setting(
"unit":"", "range":Vector3(-50, 50, 1), "flags":ALLOW_OUT_OF_BOUNDS }) {
"name": "margin",
"type": SettingType.SLIDER,
"list": main_list,
"default": 0,
"unit": "",
"range": Vector3(-50, 50, 1),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
# Slope painting filter # Slope painting filter
add_setting({ "name":"slope", "type":SettingType.DOUBLE_SLIDER, "list":main_list, "default":Vector2(0, 90), add_setting(
"unit":"°", "range":Vector3(0, 90, 1), "flags":ADD_SEPARATOR }) {
"name": "slope",
"type": SettingType.DOUBLE_SLIDER,
"list": main_list,
"default": Vector2(0, 90),
"unit": "°",
"range": Vector3(0, 90, 1),
"flags": ADD_SEPARATOR
}
)
add_setting({ "name":"enable_angle", "label":"Angle", "type":SettingType.CHECKBOX, add_setting(
"list":main_list, "default":true, "flags":ADD_SEPARATOR }) {
add_setting({ "name":"angle", "type":SettingType.SLIDER, "list":main_list, "default":0, "name": "enable_angle",
"unit":"%", "range":Vector3(0, 337.5, 22.5), "flags":NO_LABEL }) "label": "Angle",
add_setting({ "name":"angle_picker", "type":SettingType.PICKER, "list":main_list, "type": SettingType.CHECKBOX,
"default":Terrain3DEditor.ANGLE, "flags":NO_LABEL }) "list": main_list,
add_setting({ "name":"dynamic_angle", "label":"Dynamic", "type":SettingType.CHECKBOX, "default": true,
"list":main_list, "default":false, "flags":ADD_SPACER }) "flags": ADD_SEPARATOR
}
)
add_setting(
{
"name": "angle",
"type": SettingType.SLIDER,
"list": main_list,
"default": 0,
"unit": "%",
"range": Vector3(0, 337.5, 22.5),
"flags": NO_LABEL
}
)
add_setting(
{
"name": "angle_picker",
"type": SettingType.PICKER,
"list": main_list,
"default": Terrain3DEditor.ANGLE,
"flags": NO_LABEL
}
)
add_setting(
{
"name": "dynamic_angle",
"label": "Dynamic",
"type": SettingType.CHECKBOX,
"list": main_list,
"default": false,
"flags": ADD_SPACER
}
)
add_setting({ "name":"enable_scale", "label":"Scale", "type":SettingType.CHECKBOX, add_setting(
"list":main_list, "default":true, "flags":ADD_SEPARATOR }) {
add_setting({ "name":"scale", "label":"±", "type":SettingType.SLIDER, "list":main_list, "default":0, "name": "enable_scale",
"unit":"%", "range":Vector3(-60, 80, 20), "flags":NO_LABEL }) "label": "Scale",
add_setting({ "name":"scale_picker", "type":SettingType.PICKER, "list":main_list, "type": SettingType.CHECKBOX,
"default":Terrain3DEditor.SCALE, "flags":NO_LABEL }) "list": main_list,
"default": true,
"flags": ADD_SEPARATOR
}
)
add_setting(
{
"name": "scale",
"label": "±",
"type": SettingType.SLIDER,
"list": main_list,
"default": 0,
"unit": "%",
"range": Vector3(-60, 80, 20),
"flags": NO_LABEL
}
)
add_setting(
{
"name": "scale_picker",
"type": SettingType.PICKER,
"list": main_list,
"default": Terrain3DEditor.SCALE,
"flags": NO_LABEL
}
)
## Slope sculpting brush ## Slope sculpting brush
add_setting({ "name":"gradient_points", "type":SettingType.MULTI_PICKER, "label":"Points", add_setting(
"list":main_list, "default":Terrain3DEditor.SCULPT, "flags":ADD_SEPARATOR }) {
add_setting({ "name":"drawable", "type":SettingType.CHECKBOX, "list":main_list, "default":false, "name": "gradient_points",
"flags":ADD_SEPARATOR }) "type": SettingType.MULTI_PICKER,
"label": "Points",
"list": main_list,
"default": Terrain3DEditor.SCULPT,
"flags": ADD_SEPARATOR
}
)
add_setting(
{
"name": "drawable",
"type": SettingType.CHECKBOX,
"list": main_list,
"default": false,
"flags": ADD_SEPARATOR
}
)
settings["drawable"].toggled.connect(_on_drawable_toggled) settings["drawable"].toggled.connect(_on_drawable_toggled)
## Instancer ## Instancer
height_list = create_submenu(main_list, "Height", Layout.VERTICAL) height_list = create_submenu(main_list, "Height", Layout.VERTICAL)
add_setting({ "name":"height_offset", "type":SettingType.SLIDER, "list":height_list, "default":0, add_setting(
"unit":"m", "range":Vector3(-10, 10, 0.05), "flags":ALLOW_OUT_OF_BOUNDS }) {
add_setting({ "name":"random_height", "label":"Random Height ±", "type":SettingType.SLIDER, "name": "height_offset",
"list":height_list, "default":0, "unit":"m", "range":Vector3(0, 10, 0.05), "type": SettingType.SLIDER,
"flags":ALLOW_OUT_OF_BOUNDS }) "list": height_list,
"default": 0,
"unit": "m",
"range": Vector3(-10, 10, 0.05),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
add_setting(
{
"name": "random_height",
"label": "Random Height ±",
"type": SettingType.SLIDER,
"list": height_list,
"default": 0,
"unit": "m",
"range": Vector3(0, 10, 0.05),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
scale_list = create_submenu(main_list, "Scale", Layout.VERTICAL) scale_list = create_submenu(main_list, "Scale", Layout.VERTICAL)
add_setting({ "name":"fixed_scale", "type":SettingType.SLIDER, "list":scale_list, "default":100, add_setting(
"unit":"%", "range":Vector3(1, 1000, 1), "flags":ALLOW_OUT_OF_BOUNDS }) {
add_setting({ "name":"random_scale", "label":"Random Scale ±", "type":SettingType.SLIDER, "list":scale_list, "name": "fixed_scale",
"default":20, "unit":"%", "range":Vector3(0, 99, 1), "flags":ALLOW_OUT_OF_BOUNDS }) "type": SettingType.SLIDER,
"list": scale_list,
"default": 100,
"unit": "%",
"range": Vector3(1, 1000, 1),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
add_setting(
{
"name": "random_scale",
"label": "Random Scale ±",
"type": SettingType.SLIDER,
"list": scale_list,
"default": 20,
"unit": "%",
"range": Vector3(0, 99, 1),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
rotation_list = create_submenu(main_list, "Rotation", Layout.VERTICAL) rotation_list = create_submenu(main_list, "Rotation", Layout.VERTICAL)
add_setting({ "name":"fixed_spin", "label":"Fixed Spin (Around Y)", "type":SettingType.SLIDER, "list":rotation_list, add_setting(
"default":0, "unit":"°", "range":Vector3(0, 360, 1) }) {
add_setting({ "name":"random_spin", "type":SettingType.SLIDER, "list":rotation_list, "default":360, "name": "fixed_spin",
"unit":"°", "range":Vector3(0, 360, 1) }) "label": "Fixed Spin (Around Y)",
add_setting({ "name":"fixed_tilt", "label":"Fixed Tilt", "type":SettingType.SLIDER, "list":rotation_list, "type": SettingType.SLIDER,
"default":0, "unit":"°", "range":Vector3(-85, 85, 1), "flags":ALLOW_OUT_OF_BOUNDS }) "list": rotation_list,
add_setting({ "name":"random_tilt", "label":"Random Tilt ±", "type":SettingType.SLIDER, "list":rotation_list, "default": 0,
"default":10, "unit":"°", "range":Vector3(0, 85, 1), "flags":ALLOW_OUT_OF_BOUNDS }) "unit": "°",
add_setting({ "name":"align_to_normal", "type":SettingType.CHECKBOX, "list":rotation_list, "default":false }) "range": Vector3(0, 360, 1)
}
)
add_setting(
{
"name": "random_spin",
"type": SettingType.SLIDER,
"list": rotation_list,
"default": 360,
"unit": "°",
"range": Vector3(0, 360, 1)
}
)
add_setting(
{
"name": "fixed_tilt",
"label": "Fixed Tilt",
"type": SettingType.SLIDER,
"list": rotation_list,
"default": 0,
"unit": "°",
"range": Vector3(-85, 85, 1),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
add_setting(
{
"name": "random_tilt",
"label": "Random Tilt ±",
"type": SettingType.SLIDER,
"list": rotation_list,
"default": 10,
"unit": "°",
"range": Vector3(0, 85, 1),
"flags": ALLOW_OUT_OF_BOUNDS
}
)
add_setting(
{
"name": "align_to_normal",
"type": SettingType.CHECKBOX,
"list": rotation_list,
"default": false
}
)
color_list = create_submenu(main_list, "Color", Layout.VERTICAL) color_list = create_submenu(main_list, "Color", Layout.VERTICAL)
add_setting({ "name":"vertex_color", "type":SettingType.COLOR_SELECT, "list":color_list, add_setting(
"default":Color.WHITE }) {
add_setting({ "name":"random_hue", "label":"Random Hue Shift ±", "type":SettingType.SLIDER, "name": "vertex_color",
"list":color_list, "default":0, "unit":"°", "range":Vector3(0, 360, 1) }) "type": SettingType.COLOR_SELECT,
add_setting({ "name":"random_darken", "type":SettingType.SLIDER, "list":color_list, "default":50, "list": color_list,
"unit":"%", "range":Vector3(0, 100, 1) }) "default": Color.WHITE
}
)
add_setting(
{
"name": "random_hue",
"label": "Random Hue Shift ±",
"type": SettingType.SLIDER,
"list": color_list,
"default": 0,
"unit": "°",
"range": Vector3(0, 360, 1)
}
)
add_setting(
{
"name": "random_darken",
"type": SettingType.SLIDER,
"list": color_list,
"default": 50,
"unit": "%",
"range": Vector3(0, 100, 1)
}
)
#add_setting({ "name":"blend_mode", "type":SettingType.OPTION, "list":color_list, "default":0, #add_setting({ "name":"blend_mode", "type":SettingType.OPTION, "list":color_list, "default":0,
#"range":Vector3(0, 3, 1) }) #"range":Vector3(0, 3, 1) })
if DisplayServer.is_touchscreen_available(): if DisplayServer.is_touchscreen_available():
add_setting({ "name":"invert", "label":"Invert", "type":SettingType.CHECKBOX, "list":main_list, "default":false, "flags":ADD_SEPARATOR }) add_setting(
{
"name": "invert",
"label": "Invert",
"type": SettingType.CHECKBOX,
"list": main_list,
"default": false,
"flags": ADD_SEPARATOR
}
)
var spacer: Control = Control.new() var spacer: Control = Control.new()
spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL spacer.size_flags_horizontal = Control.SIZE_EXPAND_FILL
@@ -166,22 +457,67 @@ func _ready() -> void:
## Advanced Settings Menu ## Advanced Settings Menu
advanced_list = create_submenu(main_list, "", Layout.VERTICAL, false) advanced_list = create_submenu(main_list, "", Layout.VERTICAL, false)
add_setting({ "name":"auto_regions", "label":"Add regions while sculpting", "type":SettingType.CHECKBOX, add_setting(
"list":advanced_list, "default":true }) {
add_setting({ "name":"align_to_view", "type":SettingType.CHECKBOX, "list":advanced_list, "name": "auto_regions",
"default":true }) "label": "Add regions while sculpting",
add_setting({ "name":"show_cursor_while_painting", "type":SettingType.CHECKBOX, "list":advanced_list, "type": SettingType.CHECKBOX,
"default":true }) "list": advanced_list,
"default": true
}
)
add_setting(
{
"name": "align_to_view",
"type": SettingType.CHECKBOX,
"list": advanced_list,
"default": true
}
)
add_setting(
{
"name": "show_cursor_while_painting",
"type": SettingType.CHECKBOX,
"list": advanced_list,
"default": true
}
)
advanced_list.add_child(HSeparator.new(), true) advanced_list.add_child(HSeparator.new(), true)
add_setting({ "name":"gamma", "type":SettingType.SLIDER, "list":advanced_list, "default":1.0, add_setting(
"unit":"γ", "range":Vector3(0.1, 2.0, 0.01) }) {
add_setting({ "name":"jitter", "type":SettingType.SLIDER, "list":advanced_list, "default":50, "name": "gamma",
"unit":"%", "range":Vector3(0, 100, 1) }) "type": SettingType.SLIDER,
add_setting({ "name":"crosshair_threshold", "type":SettingType.SLIDER, "list":advanced_list, "default":16., "list": advanced_list,
"unit":"m", "range":Vector3(0, 200, 1) }) "default": 1.0,
"unit": "γ",
"range": Vector3(0.1, 2.0, 0.01)
}
)
add_setting(
{
"name": "jitter",
"type": SettingType.SLIDER,
"list": advanced_list,
"default": 50,
"unit": "%",
"range": Vector3(0, 100, 1)
}
)
add_setting(
{
"name": "crosshair_threshold",
"type": SettingType.SLIDER,
"list": advanced_list,
"default": 16.,
"unit": "m",
"range": Vector3(0, 200, 1)
}
)
func create_submenu(p_parent: Control, p_button_name: String, p_layout: Layout, p_hover_pop: bool = true) -> Container: func create_submenu(
p_parent: Control, p_button_name: String, p_layout: Layout, p_hover_pop: bool = true
) -> Container:
var menu_button: Button = Button.new() var menu_button: Button = Button.new()
if p_button_name.is_empty(): if p_button_name.is_empty():
menu_button.icon = get_theme_icon("GuiTabMenuHl", "EditorIcons") menu_button.icon = get_theme_icon("GuiTabMenuHl", "EditorIcons")
@@ -204,24 +540,26 @@ func create_submenu(p_parent: Control, p_button_name: String, p_layout: Layout,
submenu.mouse_entered.connect(func(): submenu.set_meta("mouse_entered", true)) submenu.mouse_entered.connect(func(): submenu.set_meta("mouse_entered", true))
submenu.mouse_exited.connect(func(): submenu.mouse_exited.connect(
# On mouse_exit, hide popup unless LineEdit focused func():
var focused_element: Control = submenu.gui_get_focus_owner() # On mouse_exit, hide popup unless LineEdit focused
if not focused_element is LineEdit: var focused_element: Control = submenu.gui_get_focus_owner()
_on_show_submenu(false, menu_button) if not focused_element is LineEdit:
submenu.set_meta("mouse_entered", false)
return
focused_element.focus_exited.connect(func():
# Close submenu once lineedit loses focus
if not submenu.get_meta("mouse_entered"):
_on_show_submenu(false, menu_button) _on_show_submenu(false, menu_button)
submenu.set_meta("mouse_entered", false) submenu.set_meta("mouse_entered", false)
) return
focused_element.focus_exited.connect(
func():
# Close submenu once lineedit loses focus
if not submenu.get_meta("mouse_entered"):
_on_show_submenu(false, menu_button)
submenu.set_meta("mouse_entered", false)
)
) )
var sublist: Container var sublist: Container
match(p_layout): match p_layout:
Layout.GRID: Layout.GRID:
sublist = GridContainer.new() sublist = GridContainer.new()
Layout.VERTICAL: Layout.VERTICAL:
@@ -242,12 +580,14 @@ func _on_show_submenu(p_toggled: bool, p_button: Button) -> void:
return return
# Hide menu if mouse is not in button or panel # Hide menu if mouse is not in button or panel
var button_rect: Rect2 = Rect2(p_button.get_screen_transform().origin, p_button.get_global_rect().size) var button_rect: Rect2 = Rect2(
p_button.get_screen_transform().origin, p_button.get_global_rect().size
)
var in_button: bool = button_rect.has_point(DisplayServer.mouse_get_position()) var in_button: bool = button_rect.has_point(DisplayServer.mouse_get_position())
var popup: PopupPanel = p_button.get_child(0) var popup: PopupPanel = p_button.get_child(0)
var popup_rect: Rect2 = Rect2(popup.position, popup.size) var popup_rect: Rect2 = Rect2(popup.position, popup.size)
var in_popup: bool = popup_rect.has_point(DisplayServer.mouse_get_position()) var in_popup: bool = popup_rect.has_point(DisplayServer.mouse_get_position())
if not p_toggled and ( in_button or in_popup ): if not p_toggled and (in_button or in_popup):
return return
# Hide all submenus before possibly enabling the current one # Hide all submenus before possibly enabling the current one
@@ -255,7 +595,7 @@ func _on_show_submenu(p_toggled: bool, p_button: Button) -> void:
popup.set_visible(p_toggled) popup.set_visible(p_toggled)
var popup_pos: Vector2 = p_button.get_screen_transform().origin var popup_pos: Vector2 = p_button.get_screen_transform().origin
popup_pos.y -= popup.size.y popup_pos.y -= popup.size.y
if popup.get_child_count()>0 and popup.get_child(0) == advanced_list: if popup.get_child_count() > 0 and popup.get_child(0) == advanced_list:
popup_pos.x -= popup.size.x - p_button.size.x popup_pos.x -= popup.size.x - p_button.size.x
popup.set_position(popup_pos) popup.set_position(popup_pos)
@@ -352,7 +692,11 @@ func _on_picked(p_type: Terrain3DEditor.Tool, p_color: Color, p_global_position:
Terrain3DEditor.ROUGHNESS: Terrain3DEditor.ROUGHNESS:
# This converts 0,1 to -100,100 # This converts 0,1 to -100,100
# It also quantizes explicitly so picked values matches painted values # It also quantizes explicitly so picked values matches painted values
settings["roughness"].value = round(200. * float(int(p_color.a * 255.) / 255. - .5)) if not is_nan(p_color.r) else 0. settings["roughness"].value = (
round(200. * float(int(p_color.a * 255.) / 255. - .5))
if not is_nan(p_color.r)
else 0.
)
Terrain3DEditor.ANGLE: Terrain3DEditor.ANGLE:
settings["angle"].value = p_color.r settings["angle"].value = p_color.r
Terrain3DEditor.SCALE: Terrain3DEditor.SCALE:
@@ -365,7 +709,9 @@ func _on_point_pick(p_type: Terrain3DEditor.Tool, p_name: String) -> void:
emit_signal("picking", p_type, _on_point_picked.bind(p_name)) emit_signal("picking", p_type, _on_point_picked.bind(p_name))
func _on_point_picked(p_type: Terrain3DEditor.Tool, p_color: Color, p_global_position: Vector3, p_name: String) -> void: func _on_point_picked(
p_type: Terrain3DEditor.Tool, p_color: Color, p_global_position: Vector3, p_name: String
) -> void:
assert(p_type == Terrain3DEditor.SCULPT) assert(p_type == Terrain3DEditor.SCULPT)
var point: Vector3 = p_global_position var point: Vector3 = p_global_position
point.y = p_color.r point.y = p_color.r
@@ -375,7 +721,7 @@ func _on_point_picked(p_type: Terrain3DEditor.Tool, p_color: Color, p_global_pos
func add_setting(p_args: Dictionary) -> void: func add_setting(p_args: Dictionary) -> void:
var p_name: StringName = p_args.get("name", "") var p_name: StringName = p_args.get("name", "")
var p_label: String = p_args.get("label", "") # Optional replacement for name var p_label: String = p_args.get("label", "") # Optional replacement for name
var p_type: SettingType = p_args.get("type", SettingType.TYPE_MAX) var p_type: SettingType = p_args.get("type", SettingType.TYPE_MAX)
var p_list: Control = p_args.get("list") var p_list: Control = p_args.get("list")
var p_default: Variant = p_args.get("default") var p_default: Variant = p_args.get("default")
@@ -391,7 +737,7 @@ func add_setting(p_args: Dictionary) -> void:
var container: HBoxContainer = HBoxContainer.new() var container: HBoxContainer = HBoxContainer.new()
container.set_v_size_flags(SIZE_EXPAND_FILL) container.set_v_size_flags(SIZE_EXPAND_FILL)
var control: Control # Houses the setting to be saved var control: Control # Houses the setting to be saved
var pending_children: Array[Control] var pending_children: Array[Control]
match p_type: match p_type:
@@ -404,11 +750,15 @@ func add_setting(p_args: Dictionary) -> void:
SettingType.CHECKBOX: SettingType.CHECKBOX:
var checkbox := CheckBox.new() var checkbox := CheckBox.new()
if !(p_flags & NO_SAVE): if !(p_flags & NO_SAVE):
checkbox.set_pressed_no_signal(plugin.get_setting(ES_TOOL_SETTINGS + p_name, p_default)) checkbox.set_pressed_no_signal(
checkbox.toggled.connect( ( plugin.get_setting(ES_TOOL_SETTINGS + p_name, p_default)
func(value, path): )
plugin.set_setting(path, value) checkbox.toggled.connect(
).bind(ES_TOOL_SETTINGS + p_name) ) (
(func(value, path): plugin.set_setting(path, value))
. bind(ES_TOOL_SETTINGS + p_name)
)
)
else: else:
checkbox.set_pressed_no_signal(p_default) checkbox.set_pressed_no_signal(p_default)
checkbox.pressed.connect(_on_setting_changed) checkbox.pressed.connect(_on_setting_changed)
@@ -422,10 +772,12 @@ func add_setting(p_args: Dictionary) -> void:
picker.get_picker().set_color_mode(ColorPicker.MODE_HSV) picker.get_picker().set_color_mode(ColorPicker.MODE_HSV)
if !(p_flags & NO_SAVE): if !(p_flags & NO_SAVE):
picker.set_pick_color(plugin.get_setting(ES_TOOL_SETTINGS + p_name, p_default)) picker.set_pick_color(plugin.get_setting(ES_TOOL_SETTINGS + p_name, p_default))
picker.color_changed.connect( ( picker.color_changed.connect(
func(value, path): (
plugin.set_setting(path, value) (func(value, path): plugin.set_setting(path, value))
).bind(ES_TOOL_SETTINGS + p_name) ) . bind(ES_TOOL_SETTINGS + p_name)
)
)
else: else:
picker.set_pick_color(p_default) picker.set_pick_color(p_default)
picker.color_changed.connect(_on_setting_changed) picker.color_changed.connect(_on_setting_changed)
@@ -484,7 +836,7 @@ func add_setting(p_args: Dictionary) -> void:
pending_children.push_back(spin_slider) pending_children.push_back(spin_slider)
control = spin_slider control = spin_slider
else: # DOUBLE_SLIDER else: # DOUBLE_SLIDER
var label := Label.new() var label := Label.new()
label.set_custom_minimum_size(Vector2(60, 0)) label.set_custom_minimum_size(Vector2(60, 0))
label.set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER) label.set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER)
@@ -505,10 +857,12 @@ func add_setting(p_args: Dictionary) -> void:
if !(p_flags & NO_SAVE): if !(p_flags & NO_SAVE):
slider.set_value(plugin.get_setting(ES_TOOL_SETTINGS + p_name, p_default)) slider.set_value(plugin.get_setting(ES_TOOL_SETTINGS + p_name, p_default))
slider.value_changed.connect( ( slider.value_changed.connect(
func(value, path): (
plugin.set_setting(path, value) (func(value, path): plugin.set_setting(path, value))
).bind(ES_TOOL_SETTINGS + p_name) ) . bind(ES_TOOL_SETTINGS + p_name)
)
)
else: else:
slider.set_value(p_default) slider.set_value(p_default)
@@ -569,11 +923,14 @@ func get_setting(p_setting: String) -> Variant:
value = object.get_value() value = object.get_value()
# Adjust widths of all sliders on update of values # Adjust widths of all sliders on update of values
var digits: float = count_digits(value) var digits: float = count_digits(value)
var width: float = clamp( (1 + count_digits(value)) * 19., 50, 80) * clamp(EditorInterface.get_editor_scale(), .9, 2) var width: float = (
clamp((1 + count_digits(value)) * 19., 50, 80)
* clamp(EditorInterface.get_editor_scale(), .9, 2)
)
object.set_custom_minimum_size(Vector2(width, 0)) object.set_custom_minimum_size(Vector2(width, 0))
elif object is DoubleSlider: elif object is DoubleSlider:
value = object.get_value() value = object.get_value()
elif object is ButtonGroup: # "brush" elif object is ButtonGroup: # "brush"
value = selected_brush_imgs value = selected_brush_imgs
elif object is CheckBox: elif object is CheckBox:
value = object.is_pressed() value = object.is_pressed()
@@ -588,11 +945,11 @@ func get_setting(p_setting: String) -> Variant:
func set_setting(p_setting: String, p_value: Variant) -> void: func set_setting(p_setting: String, p_value: Variant) -> void:
var object: Object = settings.get(p_setting) var object: Object = settings.get(p_setting)
if object is DoubleSlider: # Expects p_value is Vector2 if object is DoubleSlider: # Expects p_value is Vector2
object.set_value(p_value) object.set_value(p_value)
elif object is Range: elif object is Range:
object.set_value(p_value) object.set_value(p_value)
elif object is ButtonGroup: # Expects p_value is Array [ "button name", boolean ] elif object is ButtonGroup: # Expects p_value is Array [ "button name", boolean ]
if p_value is Array and p_value.size() == 2: if p_value is Array and p_value.size() == 2:
for button in object.get_buttons(): for button in object.get_buttons():
if button.name == p_value[0]: if button.name == p_value[0]:
@@ -601,8 +958,8 @@ func set_setting(p_setting: String, p_value: Variant) -> void:
object.button_pressed = p_value object.button_pressed = p_value
elif object is ColorPickerButton: elif object is ColorPickerButton:
object.color = p_value object.color = p_value
plugin.set_setting(ES_TOOL_SETTINGS + p_setting, p_value) # Signal doesn't fire on CPB plugin.set_setting(ES_TOOL_SETTINGS + p_setting, p_value) # Signal doesn't fire on CPB
elif object is MultiPicker: # Expects p_value is PackedVector3Array elif object is MultiPicker: # Expects p_value is PackedVector3Array
object.points = p_value object.points = p_value
_on_setting_changed(object) _on_setting_changed(object)
@@ -644,7 +1001,7 @@ func _generate_brush_texture(p_btn: Button) -> void:
img = img.duplicate() img = img.duplicate()
img.resize(1024, 1024, Image.INTERPOLATE_CUBIC) img.resize(1024, 1024, Image.INTERPOLATE_CUBIC)
var tex: ImageTexture = ImageTexture.create_from_image(img) var tex: ImageTexture = ImageTexture.create_from_image(img)
selected_brush_imgs = [ img, tex ] selected_brush_imgs = [img, tex]
func _on_drawable_toggled(p_button_pressed: bool) -> void: func _on_drawable_toggled(p_button_pressed: bool) -> void:
@@ -676,18 +1033,17 @@ func count_digits(p_value: float) -> int:
var count: int = 1 var count: int = 1
for i in range(5, 0, -1): for i in range(5, 0, -1):
if abs(p_value) >= pow(10, i): if abs(p_value) >= pow(10, i):
count = i+1 count = i + 1
break break
if p_value - floor(p_value) >= .1: if p_value - floor(p_value) >= .1:
count += 1 # For the decimal count += 1 # For the decimal
if p_value*10 - floor(p_value*10.) >= .1: if p_value * 10 - floor(p_value * 10.) >= .1:
count += 1 count += 1
if p_value*100 - floor(p_value*100.) >= .1: if p_value * 100 - floor(p_value * 100.) >= .1:
count += 1 count += 1
if p_value*1000 - floor(p_value*1000.) >= .1: if p_value * 1000 - floor(p_value * 1000.) >= .1:
count += 1 count += 1
# Negative sign # Negative sign
if p_value < 0: if p_value < 0:
count += 1 count += 1
return count return count
+132 -38
View File
@@ -33,61 +33,151 @@ func _ready() -> void:
add_tool_group.pressed.connect(_on_tool_selected) add_tool_group.pressed.connect(_on_tool_selected)
sub_tool_group.pressed.connect(_on_tool_selected) sub_tool_group.pressed.connect(_on_tool_selected)
add_tool_button({ "tool":Terrain3DEditor.REGION, add_tool_button(
"add_text":"Add Region (E)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_REGION_ADD, {
"sub_text":"Remove Region", "sub_op":Terrain3DEditor.SUBTRACT, "sub_icon":ICON_REGION_REMOVE }) "tool": Terrain3DEditor.REGION,
"add_text": "Add Region (E)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_REGION_ADD,
"sub_text": "Remove Region",
"sub_op": Terrain3DEditor.SUBTRACT,
"sub_icon": ICON_REGION_REMOVE
}
)
add_child(HSeparator.new()) add_child(HSeparator.new())
add_tool_button({ "tool":Terrain3DEditor.SCULPT, add_tool_button(
"add_text":"Raise (R)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_HEIGHT_ADD, {
"sub_text":"Lower (R)", "sub_op":Terrain3DEditor.SUBTRACT, "sub_icon":ICON_HEIGHT_SUB }) "tool": Terrain3DEditor.SCULPT,
"add_text": "Raise (R)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_HEIGHT_ADD,
"sub_text": "Lower (R)",
"sub_op": Terrain3DEditor.SUBTRACT,
"sub_icon": ICON_HEIGHT_SUB
}
)
add_tool_button({ "tool":Terrain3DEditor.SCULPT, add_tool_button(
"add_text":"Smooth (Shift)", "add_op":Terrain3DEditor.AVERAGE, "add_icon":ICON_HEIGHT_SMOOTH }) {
"tool": Terrain3DEditor.SCULPT,
"add_text": "Smooth (Shift)",
"add_op": Terrain3DEditor.AVERAGE,
"add_icon": ICON_HEIGHT_SMOOTH
}
)
add_tool_button({ "tool":Terrain3DEditor.HEIGHT, add_tool_button(
"add_text":"Height (H)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_HEIGHT_FLAT, {
"sub_text":"Height (H)", "sub_op":Terrain3DEditor.SUBTRACT, "sub_icon":ICON_HEIGHT_FLAT }) "tool": Terrain3DEditor.HEIGHT,
"add_text": "Height (H)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_HEIGHT_FLAT,
"sub_text": "Height (H)",
"sub_op": Terrain3DEditor.SUBTRACT,
"sub_icon": ICON_HEIGHT_FLAT
}
)
add_tool_button({ "tool":Terrain3DEditor.SCULPT, add_tool_button(
"add_text":"Slope (S)", "add_op":Terrain3DEditor.GRADIENT, "add_icon":ICON_HEIGHT_SLOPE }) {
"tool": Terrain3DEditor.SCULPT,
"add_text": "Slope (S)",
"add_op": Terrain3DEditor.GRADIENT,
"add_icon": ICON_HEIGHT_SLOPE
}
)
add_child(HSeparator.new()) add_child(HSeparator.new())
add_tool_button({ "tool":Terrain3DEditor.TEXTURE, add_tool_button(
"add_text":"Paint Texture (B)", "add_op":Terrain3DEditor.REPLACE, "add_icon":ICON_PAINT_TEXTURE }) {
"tool": Terrain3DEditor.TEXTURE,
"add_text": "Paint Texture (B)",
"add_op": Terrain3DEditor.REPLACE,
"add_icon": ICON_PAINT_TEXTURE
}
)
add_tool_button({ "tool":Terrain3DEditor.TEXTURE, add_tool_button(
"add_text":"Spray Texture (V)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_SPRAY_TEXTURE }) {
"tool": Terrain3DEditor.TEXTURE,
"add_text": "Spray Texture (V)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_SPRAY_TEXTURE
}
)
add_tool_button({ "tool":Terrain3DEditor.AUTOSHADER, add_tool_button(
"add_text":"Paint Autoshader (A)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_AUTOSHADER, {
"sub_text":"Disable Autoshader (A)", "sub_op":Terrain3DEditor.SUBTRACT }) "tool": Terrain3DEditor.AUTOSHADER,
"add_text": "Paint Autoshader (A)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_AUTOSHADER,
"sub_text": "Disable Autoshader (A)",
"sub_op": Terrain3DEditor.SUBTRACT
}
)
add_child(HSeparator.new()) add_child(HSeparator.new())
add_tool_button({ "tool":Terrain3DEditor.COLOR, add_tool_button(
"add_text":"Paint Color (C)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_COLOR, {
"sub_text":"Remove Color (C)", "sub_op":Terrain3DEditor.SUBTRACT }) "tool": Terrain3DEditor.COLOR,
"add_text": "Paint Color (C)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_COLOR,
"sub_text": "Remove Color (C)",
"sub_op": Terrain3DEditor.SUBTRACT
}
)
add_tool_button({ "tool":Terrain3DEditor.ROUGHNESS, add_tool_button(
"add_text":"Paint Wetness (W)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_WETNESS, {
"sub_text":"Remove Wetness (W)", "sub_op":Terrain3DEditor.SUBTRACT }) "tool": Terrain3DEditor.ROUGHNESS,
"add_text": "Paint Wetness (W)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_WETNESS,
"sub_text": "Remove Wetness (W)",
"sub_op": Terrain3DEditor.SUBTRACT
}
)
add_child(HSeparator.new()) add_child(HSeparator.new())
add_tool_button({ "tool":Terrain3DEditor.HOLES, add_tool_button(
"add_text":"Add Holes (X)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_HOLES, {
"sub_text":"Remove Holes (X)", "sub_op":Terrain3DEditor.SUBTRACT }) "tool": Terrain3DEditor.HOLES,
"add_text": "Add Holes (X)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_HOLES,
"sub_text": "Remove Holes (X)",
"sub_op": Terrain3DEditor.SUBTRACT
}
)
add_tool_button({ "tool":Terrain3DEditor.NAVIGATION, add_tool_button(
"add_text":"Paint Navigable Area (N)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_NAVIGATION, {
"sub_text":"Remove Navigable Area (N)", "sub_op":Terrain3DEditor.SUBTRACT }) "tool": Terrain3DEditor.NAVIGATION,
"add_text": "Paint Navigable Area (N)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_NAVIGATION,
"sub_text": "Remove Navigable Area (N)",
"sub_op": Terrain3DEditor.SUBTRACT
}
)
add_tool_button({ "tool":Terrain3DEditor.INSTANCER, add_tool_button(
"add_text":"Instance Meshes (I)", "add_op":Terrain3DEditor.ADD, "add_icon":ICON_INSTANCER, {
"sub_text":"Remove Meshes (I)", "sub_op":Terrain3DEditor.SUBTRACT }) "tool": Terrain3DEditor.INSTANCER,
"add_text": "Instance Meshes (I)",
"add_op": Terrain3DEditor.ADD,
"add_icon": ICON_INSTANCER,
"sub_text": "Remove Meshes (I)",
"sub_op": Terrain3DEditor.SUBTRACT
}
)
# Select first button # Select first button
var buttons: Array[BaseButton] = add_tool_group.get_buttons() var buttons: Array[BaseButton] = add_tool_group.get_buttons()
@@ -98,7 +188,7 @@ func _ready() -> void:
func add_tool_button(p_params: Dictionary) -> void: func add_tool_button(p_params: Dictionary) -> void:
# Additive button # Additive button
var button := Button.new() var button := Button.new()
var name_str: String = p_params.get("add_text", "blank").get_slice('(', 0).to_pascal_case() var name_str: String = p_params.get("add_text", "blank").get_slice("(", 0).to_pascal_case()
button.set_name(name_str) button.set_name(name_str)
button.set_meta("Tool", p_params.get("tool", 0)) button.set_meta("Tool", p_params.get("tool", 0))
button.set_meta("Operation", p_params.get("add_op", 0)) button.set_meta("Operation", p_params.get("add_op", 0))
@@ -116,7 +206,7 @@ func add_tool_button(p_params: Dictionary) -> void:
var button2: Button var button2: Button
if p_params.has("sub_text"): if p_params.has("sub_text"):
button2 = Button.new() button2 = Button.new()
name_str = p_params.get("sub_text", "blank").get_slice('(', 0).to_pascal_case() name_str = p_params.get("sub_text", "blank").get_slice("(", 0).to_pascal_case()
button2.set_name(name_str) button2.set_name(name_str)
button2.set_meta("Tool", p_params.get("tool", 0)) button2.set_meta("Tool", p_params.get("tool", 0))
button2.set_meta("Operation", p_params.get("sub_op", 0)) button2.set_meta("Operation", p_params.get("sub_op", 0))
@@ -151,4 +241,8 @@ func _on_tool_selected(p_button: BaseButton) -> void:
var id: int = p_button.get_meta("ID", -2) var id: int = p_button.get_meta("ID", -2)
for button in change_group.get_buttons(): for button in change_group.get_buttons():
button.set_pressed_no_signal(button.get_meta("ID", -1) == id) button.set_pressed_no_signal(button.get_meta("ID", -1) == id)
emit_signal("tool_changed", p_button.get_meta("Tool", Terrain3DEditor.TOOL_MAX), p_button.get_meta("Operation", Terrain3DEditor.OP_MAX)) emit_signal(
"tool_changed",
p_button.get_meta("Tool", Terrain3DEditor.TOOL_MAX),
p_button.get_meta("Operation", Terrain3DEditor.OP_MAX)
)
+27 -13
View File
@@ -3,10 +3,12 @@
@tool @tool
extends Terrain3D extends Terrain3D
@export var clear_all: bool = false:
@export var clear_all: bool = false : set = reset_settings set = reset_settings
@export var clear_terrain: bool = false : set = reset_terrain @export var clear_terrain: bool = false:
@export var update_height_range: bool = false : set = update_heights set = reset_terrain
@export var update_height_range: bool = false:
set = update_heights
func reset_settings(p_value) -> void: func reset_settings(p_value) -> void:
@@ -27,7 +29,7 @@ func reset_settings(p_value) -> void:
func reset_terrain(p_value) -> void: func reset_terrain(p_value) -> void:
data_directory = "" data_directory = ""
for region:Terrain3DRegion in data.get_regions_active(): for region: Terrain3DRegion in data.get_regions_active():
data.remove_region(region, false) data.remove_region(region, false)
data.update_maps(Terrain3DRegion.TYPE_MAX, true, false) data.update_maps(Terrain3DRegion.TYPE_MAX, true, false)
@@ -42,15 +44,19 @@ func update_heights(p_value) -> void:
@export_global_file var height_file_name: String = "" @export_global_file var height_file_name: String = ""
@export_global_file var control_file_name: String = "" @export_global_file var control_file_name: String = ""
@export_global_file var color_file_name: String = "" @export_global_file var color_file_name: String = ""
@export var import_position: Vector2i = Vector2i(0, 0) : set = set_import_position @export var import_position: Vector2i = Vector2i(0, 0):
set = set_import_position
@export var import_scale: float = 1.0 @export var import_scale: float = 1.0
@export var height_offset: float = 0.0 @export var height_offset: float = 0.0
@export var r16_range: Vector2 = Vector2(0, 1) @export var r16_range: Vector2 = Vector2(0, 1)
@export var r16_size: Vector2i = Vector2i(1024, 1024) : set = set_r16_size @export var r16_size: Vector2i = Vector2i(1024, 1024):
@export var run_import: bool = false : set = start_import set = set_r16_size
@export var run_import: bool = false:
set = start_import
@export_dir var destination_directory: String = "" @export_dir var destination_directory: String = ""
@export var save_to_disk: bool = false : set = save_data @export var save_to_disk: bool = false:
set = save_data
func set_import_position(p_value: Vector2i) -> void: func set_import_position(p_value: Vector2i) -> void:
@@ -65,14 +71,21 @@ func set_r16_size(p_value: Vector2i) -> void:
func start_import(p_value: bool) -> void: func start_import(p_value: bool) -> void:
if p_value: if p_value:
print("Terrain3DImporter: Importing files:\n\t%s\n\t%s\n\t%s" % [ height_file_name, control_file_name, color_file_name]) print(
(
"Terrain3DImporter: Importing files:\n\t%s\n\t%s\n\t%s"
% [height_file_name, control_file_name, color_file_name]
)
)
var imported_images: Array[Image] var imported_images: Array[Image]
imported_images.resize(Terrain3DRegion.TYPE_MAX) imported_images.resize(Terrain3DRegion.TYPE_MAX)
var min_max := Vector2(0, 1) var min_max := Vector2(0, 1)
var img: Image var img: Image
if height_file_name: if height_file_name:
img = Terrain3DUtil.load_image(height_file_name, ResourceLoader.CACHE_MODE_IGNORE, r16_range, r16_size) img = Terrain3DUtil.load_image(
height_file_name, ResourceLoader.CACHE_MODE_IGNORE, r16_range, r16_size
)
min_max = Terrain3DUtil.get_min_max(img) min_max = Terrain3DUtil.get_min_max(img)
imported_images[Terrain3DRegion.TYPE_HEIGHT] = img imported_images[Terrain3DRegion.TYPE_HEIGHT] = img
if control_file_name: if control_file_name:
@@ -100,9 +113,10 @@ func save_data(p_value: bool) -> void:
enum { TYPE_HEIGHT, TYPE_CONTROL, TYPE_COLOR } enum { TYPE_HEIGHT, TYPE_CONTROL, TYPE_COLOR }
@export_enum("Height:0", "Control:1", "Color:2") var map_type: int = TYPE_HEIGHT @export_enum("Height:0", "Control:1", "Color:2") var map_type: int = TYPE_HEIGHT
@export var file_name_out: String = "" @export var file_name_out: String = ""
@export var run_export: bool = false : set = start_export @export var run_export: bool = false:
set = start_export
func start_export(p_value: bool) -> void: func start_export(p_value: bool) -> void:
var err: int = data.export_image(file_name_out, map_type) var err: int = data.export_image(file_name_out, map_type)
print("Terrain3DImporter: Export error status: ", err, " ", error_string(err)) print("Terrain3DImporter: Export error status: ", err, " ", error_string(err))
+10 -6
View File
@@ -9,13 +9,12 @@
# It should unload the regions, rename files, and reload them # It should unload the regions, rename files, and reload them
# Clear the script and resave your scene # Clear the script and resave your scene
@tool @tool
extends Terrain3D extends Terrain3D
@export var offset: Vector2i @export var offset: Vector2i
@export var run: bool = false : set = start_rename @export var run: bool = false:
set = start_rename
func start_rename(val: bool = false) -> void: func start_rename(val: bool = false) -> void:
@@ -36,17 +35,22 @@ func start_rename(val: bool = false) -> void:
var region_loc: Vector2i = Terrain3DUtil.filename_to_location(file_name) var region_loc: Vector2i = Terrain3DUtil.filename_to_location(file_name)
var new_loc: Vector2i = region_loc + offset var new_loc: Vector2i = region_loc + offset
if new_loc.x < -16 or new_loc.x > 15 or new_loc.y < -16 or new_loc.y > 15: if new_loc.x < -16 or new_loc.x > 15 or new_loc.y < -16 or new_loc.y > 15:
push_error("New location %.0v out of bounds for region %.0v. Aborting" % [ new_loc, region_loc ]) push_error(
(
"New location %.0v out of bounds for region %.0v. Aborting"
% [new_loc, region_loc]
)
)
return return
var new_name: String = "tmp_" + Terrain3DUtil.location_to_filename(new_loc) var new_name: String = "tmp_" + Terrain3DUtil.location_to_filename(new_loc)
dir.rename(file_name, new_name) dir.rename(file_name, new_name)
affected_files.push_back(new_name) affected_files.push_back(new_name)
print("File: %s renamed to: %s" % [ file_name, new_name ]) print("File: %s renamed to: %s" % [file_name, new_name])
for file_name in affected_files: for file_name in affected_files:
var new_name: String = file_name.trim_prefix("tmp_") var new_name: String = file_name.trim_prefix("tmp_")
dir.rename(file_name, new_name) dir.rename(file_name, new_name)
print("File: %s renamed to: %s" % [ file_name, new_name ]) print("File: %s renamed to: %s" % [file_name, new_name])
data_directory = dir_name data_directory = dir_name
EditorInterface.get_resource_filesystem().scan() EditorInterface.get_resource_filesystem().scan()
+16 -5
View File
@@ -5,14 +5,16 @@
extends Node3D extends Node3D
class_name Terrain3DObjects class_name Terrain3DObjects
const TransformChangedNotifier: Script = preload("res://addons/terrain_3d/utils/transform_changed_notifier.gd") const TransformChangedNotifier: Script = preload(
"res://addons/terrain_3d/utils/transform_changed_notifier.gd"
)
const CHILD_HELPER_NAME: StringName = &"TransformChangedSignaller" const CHILD_HELPER_NAME: StringName = &"TransformChangedSignaller"
const CHILD_HELPER_PATH: NodePath = ^"TransformChangedSignaller" const CHILD_HELPER_PATH: NodePath = ^"TransformChangedSignaller"
var _undo_redo = null var _undo_redo = null
var _terrain_id: int var _terrain_id: int
var _offsets: Dictionary # Object ID -> Vector3(X, Y offset relative to terrain height, Z) var _offsets: Dictionary # Object ID -> Vector3(X, Y offset relative to terrain height, Z)
var _ignore_transform_change: bool = false var _ignore_transform_change: bool = false
@@ -45,7 +47,12 @@ func editor_setup(p_plugin) -> void:
func get_terrain() -> Terrain3D: func get_terrain() -> Terrain3D:
var terrain := instance_from_id(_terrain_id) as Terrain3D var terrain := instance_from_id(_terrain_id) as Terrain3D
if not terrain or terrain.is_queued_for_deletion() or not terrain.is_inside_tree(): if not terrain or terrain.is_queued_for_deletion() or not terrain.is_inside_tree():
var terrains: Array[Node] = Engine.get_singleton(&"EditorInterface").get_edited_scene_root().find_children("", "Terrain3D") var terrains: Array[Node] = (
Engine
. get_singleton(&"EditorInterface")
. get_edited_scene_root()
. find_children("", "Terrain3D")
)
if terrains.size() > 0: if terrains.size() > 0:
terrain = terrains[0] terrain = terrains[0]
_terrain_id = terrain.get_instance_id() if terrain else 0 _terrain_id = terrain.get_instance_id() if terrain else 0
@@ -143,8 +150,12 @@ func _on_child_transform_changed(p_node: Node3D) -> void:
# Make sure that when the user undo's the translation, the offset change gets undone too! # Make sure that when the user undo's the translation, the offset change gets undone too!
_undo_redo.create_action("Translate", UndoRedo.MERGE_ALL) _undo_redo.create_action("Translate", UndoRedo.MERGE_ALL)
_undo_redo.add_do_method(self, &"_set_offset_and_position", p_node.get_instance_id(), new_offset, new_position) _undo_redo.add_do_method(
_undo_redo.add_undo_method(self, &"_set_offset_and_position", p_node.get_instance_id(), old_offset, old_position) self, &"_set_offset_and_position", p_node.get_instance_id(), new_offset, new_position
)
_undo_redo.add_undo_method(
self, &"_set_offset_and_position", p_node.get_instance_id(), old_offset, old_position
)
_undo_redo.commit_action() _undo_redo.commit_action()
+1 -1
View File
@@ -22,7 +22,7 @@ func _input(p_event: InputEvent) -> void:
return return
func rotate_camera(p_relative:Vector2) -> void: func rotate_camera(p_relative: Vector2) -> void:
_camera_yaw.rotation.y -= p_relative.x * mouse_sensitivity _camera_yaw.rotation.y -= p_relative.x * mouse_sensitivity
_camera_yaw.orthonormalize() _camera_yaw.orthonormalize()
_camera_pitch.rotation.x += p_relative.y * mouse_sensitivity * CAMERA_RATIO * mouse_y_inversion _camera_pitch.rotation.x += p_relative.y * mouse_sensitivity * CAMERA_RATIO * mouse_y_inversion
+12 -8
View File
@@ -20,20 +20,22 @@ func _ready() -> void:
func create_terrain() -> Terrain3D: func create_terrain() -> Terrain3D:
# Create textures # Create textures
var green_gr := Gradient.new() var green_gr := Gradient.new()
green_gr.set_color(0, Color.from_hsv(100./360., .35, .3)) green_gr.set_color(0, Color.from_hsv(100. / 360., .35, .3))
green_gr.set_color(1, Color.from_hsv(120./360., .4, .37)) green_gr.set_color(1, Color.from_hsv(120. / 360., .4, .37))
var green_ta: Terrain3DTextureAsset = await create_texture_asset("Grass", green_gr, 1024) var green_ta: Terrain3DTextureAsset = await create_texture_asset("Grass", green_gr, 1024)
green_ta.uv_scale = 0.1 green_ta.uv_scale = 0.1
green_ta.detiling_rotation = 0.1 green_ta.detiling_rotation = 0.1
var brown_gr := Gradient.new() var brown_gr := Gradient.new()
brown_gr.set_color(0, Color.from_hsv(30./360., .4, .3)) brown_gr.set_color(0, Color.from_hsv(30. / 360., .4, .3))
brown_gr.set_color(1, Color.from_hsv(30./360., .4, .4)) brown_gr.set_color(1, Color.from_hsv(30. / 360., .4, .4))
var brown_ta: Terrain3DTextureAsset = await create_texture_asset("Dirt", brown_gr, 1024) var brown_ta: Terrain3DTextureAsset = await create_texture_asset("Dirt", brown_gr, 1024)
brown_ta.uv_scale = 0.03 brown_ta.uv_scale = 0.03
green_ta.detiling_rotation = 0.1 green_ta.detiling_rotation = 0.1
var grass_ma: Terrain3DMeshAsset = create_mesh_asset("Grass", Color.from_hsv(120./360., .4, .37)) var grass_ma: Terrain3DMeshAsset = create_mesh_asset(
"Grass", Color.from_hsv(120. / 360., .4, .37)
)
# Create a terrain # Create a terrain
var terrain := Terrain3D.new() var terrain := Terrain3D.new()
@@ -77,7 +79,9 @@ func create_terrain() -> Terrain3D:
return terrain return terrain
func create_texture_asset(asset_name: String, gradient: Gradient, texture_size: int = 512) -> Terrain3DTextureAsset: func create_texture_asset(
asset_name: String, gradient: Gradient, texture_size: int = 512
) -> Terrain3DTextureAsset:
# Create noise map # Create noise map
var fnl := FastNoiseLite.new() var fnl := FastNoiseLite.new()
fnl.frequency = 0.004 fnl.frequency = 0.004
@@ -96,7 +100,7 @@ func create_texture_asset(asset_name: String, gradient: Gradient, texture_size:
for x in alb_noise_img.get_width(): for x in alb_noise_img.get_width():
for y in alb_noise_img.get_height(): for y in alb_noise_img.get_height():
var clr: Color = alb_noise_img.get_pixel(x, y) var clr: Color = alb_noise_img.get_pixel(x, y)
clr.a = clr.v # Noise as height clr.a = clr.v # Noise as height
alb_noise_img.set_pixel(x, y, clr) alb_noise_img.set_pixel(x, y, clr)
alb_noise_img.generate_mipmaps() alb_noise_img.generate_mipmaps()
var albedo := ImageTexture.create_from_image(alb_noise_img) var albedo := ImageTexture.create_from_image(alb_noise_img)
@@ -113,7 +117,7 @@ func create_texture_asset(asset_name: String, gradient: Gradient, texture_size:
for x in nrm_noise_img.get_width(): for x in nrm_noise_img.get_width():
for y in nrm_noise_img.get_height(): for y in nrm_noise_img.get_height():
var normal_rgh: Color = nrm_noise_img.get_pixel(x, y) var normal_rgh: Color = nrm_noise_img.get_pixel(x, y)
normal_rgh.a = 0.8 # Roughness normal_rgh.a = 0.8 # Roughness
nrm_noise_img.set_pixel(x, y, normal_rgh) nrm_noise_img.set_pixel(x, y, normal_rgh)
nrm_noise_img.generate_mipmaps() nrm_noise_img.generate_mipmaps()
var normal := ImageTexture.create_from_image(nrm_noise_img) var normal := ImageTexture.create_from_image(nrm_noise_img)
+13 -12
View File
@@ -9,15 +9,16 @@ func _ready():
$UI.player = $Player $UI.player = $Player
# Load Sky3D into the demo environment if enabled # Load Sky3D into the demo environment if enabled
if Engine.is_editor_hint() and has_node("Environment") and \ if (
Engine.get_singleton(&"EditorInterface").is_plugin_enabled("sky_3d"): Engine.is_editor_hint()
$Environment.queue_free() and has_node("Environment")
var sky3d = load("res://addons/sky_3d/src/Sky3D.gd").new() and Engine.get_singleton(&"EditorInterface").is_plugin_enabled("sky_3d")
sky3d.name = "Sky3D" ):
add_child(sky3d, true) $Environment.queue_free()
move_child(sky3d, 1) var sky3d = load("res://addons/sky_3d/src/Sky3D.gd").new()
sky3d.owner = self sky3d.name = "Sky3D"
sky3d.current_time = 10 add_child(sky3d, true)
sky3d.enable_editor_time = false move_child(sky3d, 1)
sky3d.owner = self
sky3d.current_time = 10
sky3d.enable_editor_time = false
+3 -1
View File
@@ -23,7 +23,9 @@ func _process(p_delta: float) -> void:
func is_on_nav_mesh() -> bool: func is_on_nav_mesh() -> bool:
var closest_point := NavigationServer3D.map_get_closest_point(nav_agent.get_navigation_map(), global_position) var closest_point := NavigationServer3D.map_get_closest_point(
nav_agent.get_navigation_map(), global_position
)
return global_position.distance_squared_to(closest_point) < nav_agent.path_max_distance ** 2 return global_position.distance_squared_to(closest_point) < nav_agent.path_max_distance ** 2
+17 -17
View File
@@ -2,7 +2,7 @@ extends CharacterBody3D
@export var MOVE_SPEED: float = 50.0 @export var MOVE_SPEED: float = 50.0
@export var JUMP_SPEED: float = 2.0 @export var JUMP_SPEED: float = 2.0
@export var first_person: bool = false : @export var first_person: bool = false:
set(p_value): set(p_value):
first_person = p_value first_person = p_value
if first_person: if first_person:
@@ -13,17 +13,17 @@ extends CharacterBody3D
$Body.visible = true $Body.visible = true
create_tween().tween_property($CameraManager/Arm, "spring_length", 6.0, .33) create_tween().tween_property($CameraManager/Arm, "spring_length", 6.0, .33)
@export var gravity_enabled: bool = true : @export var gravity_enabled: bool = true:
set(p_value): set(p_value):
gravity_enabled = p_value gravity_enabled = p_value
if not gravity_enabled: if not gravity_enabled:
velocity.y = 0 velocity.y = 0
@export var collision_enabled: bool = true : @export var collision_enabled: bool = true:
set(p_value): set(p_value):
collision_enabled = p_value collision_enabled = p_value
$CollisionShapeBody.disabled = ! collision_enabled $CollisionShapeBody.disabled = !collision_enabled
$CollisionShapeRay.disabled = ! collision_enabled $CollisionShapeRay.disabled = !collision_enabled
func _physics_process(p_delta) -> void: func _physics_process(p_delta) -> void:
@@ -41,18 +41,18 @@ func _physics_process(p_delta) -> void:
# Returns the input vector relative to the camera. Forward is always the direction the camera is facing # Returns the input vector relative to the camera. Forward is always the direction the camera is facing
func get_camera_relative_input() -> Vector3: func get_camera_relative_input() -> Vector3:
var input_dir: Vector3 = Vector3.ZERO var input_dir: Vector3 = Vector3.ZERO
if Input.is_key_pressed(KEY_A): # Left if Input.is_key_pressed(KEY_A): # Left
input_dir -= %Camera3D.global_transform.basis.x input_dir -= %Camera3D.global_transform.basis.x
if Input.is_key_pressed(KEY_D): # Right if Input.is_key_pressed(KEY_D): # Right
input_dir += %Camera3D.global_transform.basis.x input_dir += %Camera3D.global_transform.basis.x
if Input.is_key_pressed(KEY_W): # Forward if Input.is_key_pressed(KEY_W): # Forward
input_dir -= %Camera3D.global_transform.basis.z input_dir -= %Camera3D.global_transform.basis.z
if Input.is_key_pressed(KEY_S): # Backward if Input.is_key_pressed(KEY_S): # Backward
input_dir += %Camera3D.global_transform.basis.z input_dir += %Camera3D.global_transform.basis.z
if Input.is_key_pressed(KEY_E) or Input.is_key_pressed(KEY_SPACE): # Up if Input.is_key_pressed(KEY_E) or Input.is_key_pressed(KEY_SPACE): # Up
velocity.y += JUMP_SPEED + MOVE_SPEED*.016 velocity.y += JUMP_SPEED + MOVE_SPEED * .016
if Input.is_key_pressed(KEY_Q): # Down if Input.is_key_pressed(KEY_Q): # Down
velocity.y -= JUMP_SPEED + MOVE_SPEED*.016 velocity.y -= JUMP_SPEED + MOVE_SPEED * .016
if Input.is_key_pressed(KEY_KP_ADD) or Input.is_key_pressed(KEY_EQUAL): if Input.is_key_pressed(KEY_KP_ADD) or Input.is_key_pressed(KEY_EQUAL):
MOVE_SPEED = clamp(MOVE_SPEED + .5, 5, 9999) MOVE_SPEED = clamp(MOVE_SPEED + .5, 5, 9999)
if Input.is_key_pressed(KEY_KP_SUBTRACT) or Input.is_key_pressed(KEY_MINUS): if Input.is_key_pressed(KEY_KP_SUBTRACT) or Input.is_key_pressed(KEY_MINUS):
@@ -70,12 +70,12 @@ func _input(p_event: InputEvent) -> void:
elif p_event is InputEventKey: elif p_event is InputEventKey:
if p_event.pressed: if p_event.pressed:
if p_event.keycode == KEY_V: if p_event.keycode == KEY_V:
first_person = ! first_person first_person = !first_person
elif p_event.keycode == KEY_G: elif p_event.keycode == KEY_G:
gravity_enabled = ! gravity_enabled gravity_enabled = !gravity_enabled
elif p_event.keycode == KEY_C: elif p_event.keycode == KEY_C:
collision_enabled = ! collision_enabled collision_enabled = !collision_enabled
# Else if up/down released # Else if up/down released
elif p_event.keycode in [ KEY_Q, KEY_E, KEY_SPACE ]: elif p_event.keycode in [KEY_Q, KEY_E, KEY_SPACE]:
velocity.y = 0 velocity.y = 0
+14 -7
View File
@@ -2,11 +2,16 @@ extends Node
signal bake_finished signal bake_finished
@export var enabled: bool = true : set = set_enabled @export var enabled: bool = true:
@export var enter_cost: float = 0.0 : set = set_enter_cost set = set_enabled
@export var travel_cost: float = 1.0 : set = set_travel_cost @export var enter_cost: float = 0.0:
@export_flags_3d_navigation var navigation_layers: int = 1 : set = set_navigation_layers set = set_enter_cost
@export var template: NavigationMesh : set = set_template @export var travel_cost: float = 1.0:
set = set_travel_cost
@export_flags_3d_navigation var navigation_layers: int = 1:
set = set_navigation_layers
@export var template: NavigationMesh:
set = set_template
@export var terrain: Terrain3D @export var terrain: Terrain3D
@export var player: Node3D @export var player: Node3D
@export var mesh_size := Vector3(256, 512, 256) @export var mesh_size := Vector3(256, 512, 256)
@@ -16,7 +21,7 @@ signal bake_finished
@export var log_timing: bool = false @export var log_timing: bool = false
var _scene_geometry: NavigationMeshSourceGeometryData3D var _scene_geometry: NavigationMeshSourceGeometryData3D
var _current_center := Vector3(INF,INF,INF) var _current_center := Vector3(INF, INF, INF)
var _bake_task_id: int = -1 var _bake_task_id: int = -1
var _bake_task_timer: float = 0.0 var _bake_task_timer: float = 0.0
@@ -112,7 +117,9 @@ func _process(p_delta: float) -> void:
func _rebake(p_center: Vector3) -> void: func _rebake(p_center: Vector3) -> void:
assert(template != null) assert(template != null)
_bake_task_id = WorkerThreadPool.add_task(_task_bake.bind(p_center), false, "RuntimeNavigationBaker") _bake_task_id = WorkerThreadPool.add_task(
_task_bake.bind(p_center), false, "RuntimeNavigationBaker"
)
_bake_task_timer = 0.0 _bake_task_timer = 0.0
_bake_cooldown_timer = bake_cooldown _bake_cooldown_timer = bake_cooldown
+7 -6
View File
@@ -1,6 +1,5 @@
extends Control extends Control
var player: Node var player: Node
var visible_mode: int = 1 var visible_mode: int = 1
@@ -11,7 +10,7 @@ func _init() -> void:
func _process(p_delta) -> void: func _process(p_delta) -> void:
$Label.text = "FPS: %d\n" % Engine.get_frames_per_second() $Label.text = "FPS: %d\n" % Engine.get_frames_per_second()
if(visible_mode == 1): if visible_mode == 1:
$Label.text += "Move Speed: %.1f\n" % player.MOVE_SPEED if player else "" $Label.text += "Move Speed: %.1f\n" % player.MOVE_SPEED if player else ""
$Label.text += "Position: %.1v\n" % player.global_position if player else "" $Label.text += "Position: %.1v\n" % player.global_position if player else ""
$Label.text += """ $Label.text += """
@@ -37,12 +36,12 @@ func _unhandled_key_input(p_event: InputEvent) -> void:
KEY_F8: KEY_F8:
get_tree().quit() get_tree().quit()
KEY_F9: KEY_F9:
visible_mode = (visible_mode + 1 ) % 3 visible_mode = (visible_mode + 1) % 3
$Label/Panel.visible = (visible_mode == 1) $Label/Panel.visible = (visible_mode == 1)
visible = visible_mode > 0 visible = visible_mode > 0
KEY_F10: KEY_F10:
var vp = get_viewport() var vp = get_viewport()
vp.debug_draw = (vp.debug_draw + 1 ) % 6 vp.debug_draw = (vp.debug_draw + 1) % 6
get_viewport().set_input_as_handled() get_viewport().set_input_as_handled()
KEY_F11: KEY_F11:
toggle_fullscreen() toggle_fullscreen()
@@ -56,8 +55,10 @@ func _unhandled_key_input(p_event: InputEvent) -> void:
func toggle_fullscreen() -> void: func toggle_fullscreen() -> void:
if DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN or \ if (
DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_FULLSCREEN: DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_EXCLUSIVE_FULLSCREEN
or DisplayServer.window_get_mode() == DisplayServer.WINDOW_MODE_FULLSCREEN
):
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED) DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
DisplayServer.window_set_size(Vector2(1280, 720)) DisplayServer.window_set_size(Vector2(1280, 720))
else: else:
+3 -1
View File
@@ -61,7 +61,9 @@ func _physics_process(delta: float) -> void:
desired_focus_position += cam_right * (diff_right - sign(diff_right) * deadzone_right) desired_focus_position += cam_right * (diff_right - sign(diff_right) * deadzone_right)
if abs(diff_forward) > deadzone_forward: if abs(diff_forward) > deadzone_forward:
desired_focus_position += cam_forward * (diff_forward - sign(diff_forward) * deadzone_forward) desired_focus_position += (
cam_forward * (diff_forward - sign(diff_forward) * deadzone_forward)
)
focus_position = focus_position.lerp(desired_focus_position, focus_t) focus_position = focus_position.lerp(desired_focus_position, focus_t)
+11 -7
View File
@@ -30,25 +30,28 @@ var path_pending := false
var is_dead_visual := false var is_dead_visual := false
var dead_material := StandardMaterial3D.new() var dead_material := StandardMaterial3D.new()
func debug_log(message: String) -> void: func debug_log(message: String) -> void:
if DEBUG_LOGS: if DEBUG_LOGS:
print("[NPCVisual] ", name, " | ", message) print("[NPCVisual] ", name, " | ", message)
func _ready() -> void: func _ready() -> void:
dead_material.albedo_color = Color(0.25, 0.25, 0.25, 1.0) dead_material.albedo_color = Color(0.25, 0.25, 0.25, 1.0)
func setup_from_sim(npc: SimNPC) -> void: func setup_from_sim(npc: SimNPC) -> void:
sim_id = npc.id sim_id = npc.id
npc_name = npc.npc_name npc_name = npc.npc_name
profession = npc.profession profession = npc.profession
name = "NPC_%s_%s" % [sim_id, npc_name] name = "NPC_%s_%s" % [sim_id, npc_name]
set_carried_food_visible( set_carried_food_visible(npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD) > 0.0)
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD) > 0.0
)
func set_carried_food_visible(is_visible: bool) -> void: func set_carried_food_visible(is_visible: bool) -> void:
carried_food_visual.visible = is_visible and not is_dead_visual carried_food_visual.visible = is_visible and not is_dead_visual
func set_target_position(pos: Vector3) -> void: func set_target_position(pos: Vector3) -> void:
path_request_id += 1 path_request_id += 1
var request_id := path_request_id var request_id := path_request_id
@@ -72,10 +75,7 @@ func set_target_position(pos: Vector3) -> void:
return return
current_path = NavigationServer3D.map_get_path( current_path = NavigationServer3D.map_get_path(
get_world_3d().navigation_map, get_world_3d().navigation_map, global_position, pos, true
global_position,
pos,
true
) )
path_pending = false path_pending = false
@@ -87,6 +87,7 @@ func set_target_position(pos: Vector3) -> void:
else: else:
debug_log("New target: %s Path points: %s" % [pos, current_path.size()]) debug_log("New target: %s Path points: %s" % [pos, current_path.size()])
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
if is_dead_visual: if is_dead_visual:
velocity = Vector3.ZERO velocity = Vector3.ZERO
@@ -147,6 +148,7 @@ func _physics_process(delta: float) -> void:
var target_angle := atan2(direction.x, direction.z) var target_angle := atan2(direction.x, direction.z)
rotation.y = lerp_angle(rotation.y, target_angle, rotation_speed * delta) rotation.y = lerp_angle(rotation.y, target_angle, rotation_speed * delta)
func _report_arrival_once() -> void: func _report_arrival_once() -> void:
if has_reported_arrival: if has_reported_arrival:
return return
@@ -158,6 +160,7 @@ func _report_arrival_once() -> void:
debug_log("Arrived at target") debug_log("Arrived at target")
arrived_at_target.emit(sim_id) arrived_at_target.emit(sim_id)
func _report_navigation_failure_once() -> void: func _report_navigation_failure_once() -> void:
if has_reported_arrival: if has_reported_arrival:
return return
@@ -172,6 +175,7 @@ func _report_navigation_failure_once() -> void:
debug_log("Navigation failed") debug_log("Navigation failed")
navigation_failed.emit(sim_id) navigation_failed.emit(sim_id)
func apply_dead_visual_state() -> void: func apply_dead_visual_state() -> void:
is_dead_visual = true is_dead_visual = true
carried_food_visual.visible = false carried_food_visual.visible = false
+5 -8
View File
@@ -14,13 +14,9 @@ extends CharacterBody3D
@export var stomach_capacity_for_food := 5 @export var stomach_capacity_for_food := 5
@export var interaction_range := 3.0 @export var interaction_range := 3.0
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
var input := Input.get_vector( var input := Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
"move_left",
"move_right",
"move_forward",
"move_backward"
)
var forward := -camera_rig.global_transform.basis.z var forward := -camera_rig.global_transform.basis.z
var right := camera_rig.global_transform.basis.x var right := camera_rig.global_transform.basis.x
@@ -73,13 +69,13 @@ func try_interact() -> void:
simulation_manager.eat_food(stomach_capacity_for_food) simulation_manager.eat_food(stomach_capacity_for_food)
print("Player ate food from the village supply.") print("Player ate food from the village supply.")
func try_harvest_resource_node() -> bool: func try_harvest_resource_node() -> bool:
if not simulation_manager.has_method("find_resource_node_for_player"): if not simulation_manager.has_method("find_resource_node_for_player"):
push_error("Player: SimulationManager cannot find ResourceNodes") push_error("Player: SimulationManager cannot find ResourceNodes")
return false return false
var node: ResourceNode = simulation_manager.find_resource_node_for_player( var node: ResourceNode = simulation_manager.find_resource_node_for_player(
global_position, global_position, interaction_range
interaction_range
) )
if node == null: if node == null:
return false return false
@@ -103,6 +99,7 @@ func try_harvest_resource_node() -> bool:
print("%s could not be harvested." % node.node_id) print("%s could not be harvested." % node.node_id)
return true return true
func is_near(zone: Marker3D) -> bool: func is_near(zone: Marker3D) -> bool:
if zone == null: if zone == null:
return false return false
+10 -10
View File
@@ -33,6 +33,7 @@ var last_task: StringName
var random_source: RandomNumberGenerator var random_source: RandomNumberGenerator
var debug_logs := true var debug_logs := true
func _init( func _init(
_id: int, _id: int,
_npc_name: String, _npc_name: String,
@@ -53,11 +54,10 @@ func _init(
hunger = random_source.randf_range(20.0, 80.0) hunger = random_source.randf_range(20.0, 80.0)
energy = random_source.randf_range(40.0, 100.0) energy = random_source.randf_range(40.0, 100.0)
position = Vector3( position = Vector3(
random_source.randf_range(-8.0, 8.0), random_source.randf_range(-8.0, 8.0), 0.0, random_source.randf_range(-8.0, 8.0)
0.0,
random_source.randf_range(-8.0, 8.0)
) )
func die_from_starvation() -> void: func die_from_starvation() -> void:
is_dead = true is_dead = true
current_task = SimulationIds.ACTION_DEAD current_task = SimulationIds.ACTION_DEAD
@@ -70,34 +70,34 @@ func die_from_starvation() -> void:
if debug_logs: if debug_logs:
print("[SimNPC] ", npc_name, " died from starvation.") print("[SimNPC] ", npc_name, " died from starvation.")
func get_inventory_amount(item_id: StringName) -> float: func get_inventory_amount(item_id: StringName) -> float:
return float(inventory.get(String(item_id), 0.0)) return float(inventory.get(String(item_id), 0.0))
func add_inventory(item_id: StringName, amount: float) -> void: func add_inventory(item_id: StringName, amount: float) -> void:
inventory[String(item_id)] = get_inventory_amount(item_id) + maxf(amount, 0.0) inventory[String(item_id)] = get_inventory_amount(item_id) + maxf(amount, 0.0)
func remove_inventory(item_id: StringName, requested_amount: float) -> float: func remove_inventory(item_id: StringName, requested_amount: float) -> float:
var removed := minf( var removed := minf(maxf(requested_amount, 0.0), get_inventory_amount(item_id))
maxf(requested_amount, 0.0),
get_inventory_amount(item_id)
)
inventory[String(item_id)] = get_inventory_amount(item_id) - removed inventory[String(item_id)] = get_inventory_amount(item_id) - removed
return removed return removed
func set_task(action_id: StringName, duration: float = -1.0) -> void: func set_task(action_id: StringName, duration: float = -1.0) -> void:
var definition := SimulationDefinitions.get_action(action_id) var definition := SimulationDefinitions.get_action(action_id)
if definition == null: if definition == null:
push_error("SimNPC: unknown action_id '%s'" % action_id) push_error("SimNPC: unknown action_id '%s'" % action_id)
return return
current_task = action_id current_task = action_id
task_duration = ( task_duration = (duration if duration >= 0.0 else definition.default_duration)
duration if duration >= 0.0 else definition.default_duration
)
task_progress = 0.0 task_progress = 0.0
task_complete = false task_complete = false
task_state = TASK_STATE_TRAVELING task_state = TASK_STATE_TRAVELING
has_travel_target = false has_travel_target = false
func start_working() -> void: func start_working() -> void:
if task_state != TASK_STATE_TRAVELING: if task_state != TASK_STATE_TRAVELING:
return return
+28 -23
View File
@@ -17,6 +17,7 @@ var wood_priority := 1.0
var safety_priority := 1.0 var safety_priority := 1.0
var knowledge_priority := 1.0 var knowledge_priority := 1.0
func update_modifiers() -> void: func update_modifiers() -> void:
food_modifier = 1.0 food_modifier = 1.0
wood_modifier = 1.0 wood_modifier = 1.0
@@ -31,14 +32,14 @@ func update_modifiers() -> void:
if knowledge > 25: if knowledge > 25:
knowledge_modifier = 0.75 knowledge_modifier = 0.75
debug_log("FoodMod=%s SafetyMod=%s KnowledgeMod=%s" % debug_log(
[ (
food_modifier, "FoodMod=%s SafetyMod=%s KnowledgeMod=%s"
safety_modifier, % [food_modifier, safety_modifier, knowledge_modifier]
knowledge_modifier )
]
) )
func update_priorities() -> void: func update_priorities() -> void:
food_priority = 1.0 food_priority = 1.0
wood_priority = 1.0 wood_priority = 1.0
@@ -68,6 +69,7 @@ func update_priorities() -> void:
if debug_logs: if debug_logs:
print(get_priority_summary()) print(get_priority_summary())
func apply_resource_delta(resource_id: StringName, amount: float) -> void: func apply_resource_delta(resource_id: StringName, amount: float) -> void:
match resource_id: match resource_id:
&"food": &"food":
@@ -87,6 +89,7 @@ func apply_resource_delta(resource_id: StringName, amount: float) -> void:
update_modifiers() update_modifiers()
update_priorities() update_priorities()
func apply_npc_task(npc: SimNPC) -> void: func apply_npc_task(npc: SimNPC) -> void:
if npc.is_dead: if npc.is_dead:
return return
@@ -123,27 +126,29 @@ func apply_npc_task(npc: SimNPC) -> void:
update_modifiers() update_modifiers()
update_priorities() update_priorities()
func get_summary() -> String:
return "Village | food: %s | wood: %s | safety: %s | knowledge: %s | priorities F/W/S/K: %s/%s/%s/%s" % [ func get_summary() -> String:
round(food), return (
round(wood), "Village | food: %s | wood: %s | safety: %s | knowledge: %s | priorities F/W/S/K: %s/%s/%s/%s"
round(safety), % [
round(knowledge), round(food),
food_priority, round(wood),
wood_priority, round(safety),
safety_priority, round(knowledge),
knowledge_priority food_priority,
] wood_priority,
safety_priority,
knowledge_priority
]
)
func get_priority_summary() -> String: func get_priority_summary() -> String:
return "Priorities | food: %s | wood: %s | safety: %s | knowledge: %s" % [ return (
food_priority, "Priorities | food: %s | wood: %s | safety: %s | knowledge: %s"
wood_priority, % [food_priority, wood_priority, safety_priority, knowledge_priority]
safety_priority, )
knowledge_priority
]
func debug_log(message: String) -> void: func debug_log(message: String) -> void:
if debug_logs: if debug_logs:
+3
View File
@@ -5,9 +5,11 @@ var tick_interval: float
var accumulator := 0.0 var accumulator := 0.0
var elapsed_ticks := 0 var elapsed_ticks := 0
func _init(interval: float = 1.0) -> void: func _init(interval: float = 1.0) -> void:
tick_interval = maxf(interval, 0.0001) tick_interval = maxf(interval, 0.0001)
func advance(delta: float) -> int: func advance(delta: float) -> int:
accumulator += maxf(delta, 0.0) accumulator += maxf(delta, 0.0)
var ticks_due := 0 var ticks_due := 0
@@ -17,6 +19,7 @@ func advance(delta: float) -> int:
ticks_due += 1 ticks_due += 1
return ticks_due return ticks_due
func reset() -> void: func reset() -> void:
accumulator = 0.0 accumulator = 0.0
elapsed_ticks = 0 elapsed_ticks = 0
+157 -141
View File
@@ -1,10 +1,6 @@
extends Node extends Node
signal npc_task_changed( signal npc_task_changed(npc: SimNPC, old_task: StringName, new_task: StringName)
npc: SimNPC,
old_task: StringName,
new_task: StringName
)
signal village_changed(village: SimVillage) signal village_changed(village: SimVillage)
signal npc_died(npc: SimNPC) signal npc_died(npc: SimNPC)
signal npc_target_requested(npc: SimNPC) signal npc_target_requested(npc: SimNPC)
@@ -32,9 +28,8 @@ var action_selector := ActionSelectionSystem.new()
var action_executor := ActionExecutionSystem.new() var action_executor := ActionExecutionSystem.new()
var target_resolver := ActionTargetResolver.new() var target_resolver := ActionTargetResolver.new()
var names := [ var names := ["Amina", "Tarik", "Jasmin"]
"Amina", "Tarik", "Jasmin"
]
func _ready() -> void: func _ready() -> void:
add_to_group("simulation_manager") add_to_group("simulation_manager")
@@ -54,19 +49,18 @@ func _ready() -> void:
if debug_logs: if debug_logs:
print("--- Simulation started ---") print("--- Simulation started ---")
func _process(delta: float) -> void: func _process(delta: float) -> void:
var ticks_due := clock.advance(delta) var ticks_due := clock.advance(delta)
for tick in ticks_due: for tick in ticks_due:
simulate_tick() simulate_tick()
func generate_npcs() -> void: func generate_npcs() -> void:
var profession_ids := SimulationDefinitions.get_profession_ids() var profession_ids := SimulationDefinitions.get_profession_ids()
for i in range(names.size()): for i in range(names.size()):
var npc_random := _create_random_source(i, 0) var npc_random := _create_random_source(i, 0)
var profession_index := npc_random.randi_range( var profession_index := npc_random.randi_range(0, profession_ids.size() - 1)
0,
profession_ids.size() - 1
)
var npc := SimNPC.new( var npc := SimNPC.new(
i, i,
names[i], names[i],
@@ -80,21 +74,20 @@ func generate_npcs() -> void:
npcs.append(npc) npcs.append(npc)
wander_random_sources[i] = _create_random_source(i, 1) wander_random_sources[i] = _create_random_source(i, 1)
func _create_random_source(npc_id: int, stream_id: int) -> RandomNumberGenerator: func _create_random_source(npc_id: int, stream_id: int) -> RandomNumberGenerator:
var source := RandomNumberGenerator.new() var source := RandomNumberGenerator.new()
source.seed = simulation_seed + (npc_id + 1) * 1000003 + stream_id * 7919 source.seed = simulation_seed + (npc_id + 1) * 1000003 + stream_id * 7919
return source return source
func get_wander_offset(npc_id: int) -> Vector3: func get_wander_offset(npc_id: int) -> Vector3:
var source: RandomNumberGenerator = wander_random_sources.get(npc_id) var source: RandomNumberGenerator = wander_random_sources.get(npc_id)
if source == null: if source == null:
source = _create_random_source(npc_id, 1) source = _create_random_source(npc_id, 1)
wander_random_sources[npc_id] = source wander_random_sources[npc_id] = source
return Vector3( return Vector3(source.randf_range(-6.0, 6.0), 0.0, source.randf_range(-6.0, 6.0))
source.randf_range(-6.0, 6.0),
0.0,
source.randf_range(-6.0, 6.0)
)
func simulate_tick() -> void: func simulate_tick() -> void:
tick_count += 1 tick_count += 1
@@ -113,21 +106,12 @@ func simulate_tick() -> void:
action_executor.advance_npc(npc, village) action_executor.advance_npc(npc, village)
if ( if (
not npc.is_dead not npc.is_dead
and old_state in [ and old_state in [SimNPC.TASK_STATE_IDLE, SimNPC.TASK_STATE_COMPLETE]
SimNPC.TASK_STATE_IDLE, and npc.task_state in [SimNPC.TASK_STATE_IDLE, SimNPC.TASK_STATE_COMPLETE]
SimNPC.TASK_STATE_COMPLETE
]
and npc.task_state in [
SimNPC.TASK_STATE_IDLE,
SimNPC.TASK_STATE_COMPLETE
]
): ):
var selection := action_selector.select_action(npc, village) var selection := action_selector.select_action(npc, village)
if selection != null: if selection != null:
npc.set_task( npc.set_task(selection.action_id, selection.duration_override)
selection.action_id,
selection.duration_override
)
if old_task != npc.current_task and old_target != &"": if old_task != npc.current_task and old_target != &"":
release_npc_reservation(npc.id) release_npc_reservation(npc.id)
@@ -147,45 +131,36 @@ func simulate_tick() -> void:
if not was_complete and npc.task_complete and not npc.is_dead: if not was_complete and npc.task_complete and not npc.is_dead:
var completed_task := npc.current_task var completed_task := npc.current_task
var completed_definition := SimulationDefinitions.get_action( var completed_definition := SimulationDefinitions.get_action(completed_task)
completed_task
)
if ( if (
npc.target_id != &"" npc.target_id != &""
and completed_definition != null and completed_definition != null
and completed_definition.target_type == SimulationIds.TARGET_RESOURCE and completed_definition.target_type == SimulationIds.TARGET_RESOURCE
): ):
var resource_state := get_resource_state(npc.target_id) var resource_state := get_resource_state(npc.target_id)
if ( if resource_state != null and resource_state.get_reserved_by() == npc.id:
resource_state != null
and resource_state.get_reserved_by() == npc.id
):
var extracted := resource_state.extract() var extracted := resource_state.extract()
if extracted > 0: if extracted > 0:
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD: if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD:
npc.add_inventory( npc.add_inventory(SimulationIds.RESOURCE_FOOD, extracted)
SimulationIds.RESOURCE_FOOD,
extracted
)
npc_inventory_changed.emit( npc_inventory_changed.emit(
npc, npc,
SimulationIds.RESOURCE_FOOD, SimulationIds.RESOURCE_FOOD,
npc.get_inventory_amount( npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD)
SimulationIds.RESOURCE_FOOD
)
) )
else: else:
village.apply_resource_delta( village.apply_resource_delta(
resource_state.get_resource_id(), resource_state.get_resource_id(), extracted
extracted
) )
_record_economic_event( _record_economic_event(
SimulationIds.EVENT_RESOURCE_EXTRACTED, SimulationIds.EVENT_RESOURCE_EXTRACTED,
npc.id, npc.id,
resource_state.get_node_id(), resource_state.get_node_id(),
_npc_inventory_id(npc.id) (
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD _npc_inventory_id(npc.id)
else &"village", if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
else &"village"
),
resource_state.get_resource_id(), resource_state.get_resource_id(),
extracted extracted
) )
@@ -201,7 +176,13 @@ func simulate_tick() -> void:
resource_state.get_node_id() resource_state.get_node_id()
) )
elif debug_logs: elif debug_logs:
print("[SimulationManager] ", npc.npc_name, " could not complete ", completed_task, ": target reservation was invalid") print(
"[SimulationManager] ",
npc.npc_name,
" could not complete ",
completed_task,
": target reservation was invalid"
)
release_npc_reservation(npc.id) release_npc_reservation(npc.id)
elif completed_task == SimulationIds.ACTION_DEPOSIT_FOOD: elif completed_task == SimulationIds.ACTION_DEPOSIT_FOOD:
deposit_npc_inventory(npc, SimulationIds.RESOURCE_FOOD) deposit_npc_inventory(npc, SimulationIds.RESOURCE_FOOD)
@@ -209,13 +190,19 @@ func simulate_tick() -> void:
withdraw_to_npc(npc, SimulationIds.RESOURCE_FOOD, 1.0) withdraw_to_npc(npc, SimulationIds.RESOURCE_FOOD, 1.0)
elif completed_task == SimulationIds.ACTION_EAT: elif completed_task == SimulationIds.ACTION_EAT:
consume_npc_food(npc) consume_npc_food(npc)
elif completed_task not in [ elif (
SimulationIds.ACTION_GATHER_FOOD, completed_task
SimulationIds.ACTION_GATHER_WOOD not in [SimulationIds.ACTION_GATHER_FOOD, SimulationIds.ACTION_GATHER_WOOD]
]: ):
village.apply_npc_task(npc) village.apply_npc_task(npc)
elif debug_logs: elif debug_logs:
print("[SimulationManager] ", npc.npc_name, " could not complete ", completed_task, ": no resource target") print(
"[SimulationManager] ",
npc.npc_name,
" could not complete ",
completed_task,
": no resource target"
)
village_was_changed = true village_was_changed = true
@@ -254,7 +241,14 @@ func simulate_tick() -> void:
) )
if old_state != npc.task_state: if old_state != npc.task_state:
print("[SimulationManager] State changed: ", npc.npc_name, " ", old_state, " -> ", npc.task_state) print(
"[SimulationManager] State changed: ",
npc.npc_name,
" ",
old_state,
" -> ",
npc.task_state
)
if village_was_changed: if village_was_changed:
village_changed.emit(village) village_changed.emit(village)
@@ -262,12 +256,14 @@ func simulate_tick() -> void:
if debug_logs: if debug_logs:
print(village.get_summary()) print(village.get_summary())
func set_npc_target_id(npc_id: int, target_id: StringName) -> void: func set_npc_target_id(npc_id: int, target_id: StringName) -> void:
for npc in npcs: for npc in npcs:
if npc.id == npc_id: if npc.id == npc_id:
npc.target_id = target_id npc.target_id = target_id
return return
func release_npc_reservation(npc_id: int) -> void: func release_npc_reservation(npc_id: int) -> void:
for npc in npcs: for npc in npcs:
if npc.id == npc_id: if npc.id == npc_id:
@@ -278,6 +274,7 @@ func release_npc_reservation(npc_id: int) -> void:
npc.target_id = &"" npc.target_id = &""
return return
func notify_npc_arrived(npc_id: int) -> void: func notify_npc_arrived(npc_id: int) -> void:
for npc in npcs: for npc in npcs:
if npc.id == npc_id: if npc.id == npc_id:
@@ -285,9 +282,7 @@ func notify_npc_arrived(npc_id: int) -> void:
return return
if npc.task_state == SimNPC.TASK_STATE_TRAVELING: if npc.task_state == SimNPC.TASK_STATE_TRAVELING:
var definition := SimulationDefinitions.get_action( var definition := SimulationDefinitions.get_action(npc.current_task)
npc.current_task
)
if ( if (
npc.target_id != &"" npc.target_id != &""
and definition != null and definition != null
@@ -300,7 +295,13 @@ func notify_npc_arrived(npc_id: int) -> void:
or resource_state.get_reserved_by() != npc.id or resource_state.get_reserved_by() != npc.id
): ):
if debug_logs: if debug_logs:
print("[SimulationManager] ", npc.npc_name, " arrived but target ", npc.target_id, " is unavailable, replanning") print(
"[SimulationManager] ",
npc.npc_name,
" arrived but target ",
npc.target_id,
" is unavailable, replanning"
)
notify_npc_navigation_failed(npc.id) notify_npc_navigation_failed(npc.id)
return return
@@ -308,9 +309,15 @@ func notify_npc_arrived(npc_id: int) -> void:
npc.start_working() npc.start_working()
if debug_logs: if debug_logs:
print("[SimulationManager] ", npc.npc_name, " arrived and started working on: ", npc.current_task) print(
"[SimulationManager] ",
npc.npc_name,
" arrived and started working on: ",
npc.current_task
)
return return
func notify_npc_navigation_failed(npc_id: int) -> void: func notify_npc_navigation_failed(npc_id: int) -> void:
for npc in npcs: for npc in npcs:
if npc.id != npc_id: if npc.id != npc_id:
@@ -326,12 +333,20 @@ func notify_npc_navigation_failed(npc_id: int) -> void:
npc_target_requested.emit(npc) npc_target_requested.emit(npc)
if debug_logs: if debug_logs:
print("[SimulationManager] ", npc.npc_name, " could not navigate for ", failed_task, " and is trying a wander target") print(
"[SimulationManager] ",
npc.npc_name,
" could not navigate for ",
failed_task,
" and is trying a wander target"
)
return return
func notify_npc_target_unavailable(npc_id: int) -> void: func notify_npc_target_unavailable(npc_id: int) -> void:
notify_npc_navigation_failed(npc_id) notify_npc_navigation_failed(npc_id)
func resolve_npc_target(npc_id: int, origin: Vector3) -> bool: func resolve_npc_target(npc_id: int, origin: Vector3) -> bool:
if active_world_adapter == null: if active_world_adapter == null:
push_error("SimulationManager: active_world_adapter is missing") push_error("SimulationManager: active_world_adapter is missing")
@@ -341,12 +356,7 @@ func resolve_npc_target(npc_id: int, origin: Vector3) -> bool:
continue continue
if npc.is_dead or npc.task_state != SimNPC.TASK_STATE_TRAVELING: if npc.is_dead or npc.task_state != SimNPC.TASK_STATE_TRAVELING:
return false return false
var result := target_resolver.resolve( var result := target_resolver.resolve(npc, origin, self, active_world_adapter)
npc,
origin,
self,
active_world_adapter
)
if result.is_empty(): if result.is_empty():
notify_npc_target_unavailable(npc.id) notify_npc_target_unavailable(npc.id)
return false return false
@@ -357,6 +367,7 @@ func resolve_npc_target(npc_id: int, origin: Vector3) -> bool:
return true return true
return false return false
func request_current_travel(npc_id: int) -> bool: func request_current_travel(npc_id: int) -> bool:
for npc in npcs: for npc in npcs:
if npc.id != npc_id: if npc.id != npc_id:
@@ -370,6 +381,7 @@ func request_current_travel(npc_id: int) -> bool:
return true return true
return false return false
func synchronize_npc_position(npc_id: int, active_position: Vector3) -> bool: func synchronize_npc_position(npc_id: int, active_position: Vector3) -> bool:
for npc in npcs: for npc in npcs:
if npc.id == npc_id: if npc.id == npc_id:
@@ -377,9 +389,11 @@ func synchronize_npc_position(npc_id: int, active_position: Vector3) -> bool:
return true return true
return false return false
func _npc_inventory_id(npc_id: int) -> StringName: func _npc_inventory_id(npc_id: int) -> StringName:
return StringName("npc_%d_inventory" % npc_id) return StringName("npc_%d_inventory" % npc_id)
func _record_economic_event( func _record_economic_event(
event_type: StringName, event_type: StringName,
actor_id: int, actor_id: int,
@@ -391,35 +405,26 @@ func _record_economic_event(
if amount <= 0.0: if amount <= 0.0:
return return
var event := EconomicEventRecord.create( var event := EconomicEventRecord.create(
next_event_id, next_event_id, event_type, tick_count, actor_id, source_id, destination_id, item_id, amount
event_type,
tick_count,
actor_id,
source_id,
destination_id,
item_id,
amount
) )
next_event_id += 1 next_event_id += 1
economic_events.append(event) economic_events.append(event)
economic_event_recorded.emit(event) economic_event_recorded.emit(event)
func _initialize_storage() -> void: func _initialize_storage() -> void:
if storage_states.has(SimulationIds.STORAGE_VILLAGE_PANTRY): if storage_states.has(SimulationIds.STORAGE_VILLAGE_PANTRY):
_sync_village_food() _sync_village_food()
return return
storage_states[SimulationIds.STORAGE_VILLAGE_PANTRY] = ( storage_states[SimulationIds.STORAGE_VILLAGE_PANTRY] = (StorageStateRecord.create(
StorageStateRecord.create( SimulationIds.STORAGE_VILLAGE_PANTRY, {String(SimulationIds.RESOURCE_FOOD): village.food}
SimulationIds.STORAGE_VILLAGE_PANTRY, ))
{String(SimulationIds.RESOURCE_FOOD): village.food}
)
)
_sync_village_food() _sync_village_food()
func get_pantry() -> StorageStateRecord: func get_pantry() -> StorageStateRecord:
return storage_states.get( return storage_states.get(SimulationIds.STORAGE_VILLAGE_PANTRY) as StorageStateRecord
SimulationIds.STORAGE_VILLAGE_PANTRY
) as StorageStateRecord
func _sync_village_food() -> void: func _sync_village_food() -> void:
var pantry := get_pantry() var pantry := get_pantry()
@@ -428,6 +433,7 @@ func _sync_village_food() -> void:
village.update_modifiers() village.update_modifiers()
village.update_priorities() village.update_priorities()
func deposit_npc_inventory(npc: SimNPC, item_id: StringName) -> float: func deposit_npc_inventory(npc: SimNPC, item_id: StringName) -> float:
var available := npc.get_inventory_amount(item_id) var available := npc.get_inventory_amount(item_id)
var deposited := get_pantry().deposit(item_id, available) var deposited := get_pantry().deposit(item_id, available)
@@ -445,11 +451,8 @@ func deposit_npc_inventory(npc: SimNPC, item_id: StringName) -> float:
) )
return deposited return deposited
func withdraw_to_npc(
npc: SimNPC, func withdraw_to_npc(npc: SimNPC, item_id: StringName, amount: float) -> float:
item_id: StringName,
amount: float
) -> float:
var withdrawn := get_pantry().withdraw(item_id, amount) var withdrawn := get_pantry().withdraw(item_id, amount)
npc.add_inventory(item_id, withdrawn) npc.add_inventory(item_id, withdrawn)
_sync_village_food() _sync_village_food()
@@ -465,6 +468,7 @@ func withdraw_to_npc(
) )
return withdrawn return withdrawn
func consume_npc_food(npc: SimNPC) -> bool: func consume_npc_food(npc: SimNPC) -> bool:
if npc.remove_inventory(SimulationIds.RESOURCE_FOOD, 1.0) < 1.0: if npc.remove_inventory(SimulationIds.RESOURCE_FOOD, 1.0) < 1.0:
npc.hunger = minf(npc.hunger + 5.0, 100.0) npc.hunger = minf(npc.hunger + 5.0, 100.0)
@@ -474,9 +478,7 @@ func consume_npc_food(npc: SimNPC) -> bool:
npc.starvation_ticks = 0 npc.starvation_ticks = 0
npc.is_starving = npc.hunger >= 90.0 npc.is_starving = npc.hunger >= 90.0
npc_inventory_changed.emit( npc_inventory_changed.emit(
npc, npc, SimulationIds.RESOURCE_FOOD, npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD)
SimulationIds.RESOURCE_FOOD,
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD)
) )
_record_economic_event( _record_economic_event(
SimulationIds.EVENT_ITEM_CONSUMED, SimulationIds.EVENT_ITEM_CONSUMED,
@@ -488,10 +490,12 @@ func consume_npc_food(npc: SimNPC) -> bool:
) )
return true return true
func register_loaded_resource_nodes() -> void: func register_loaded_resource_nodes() -> void:
for node in ResourceNode.get_all(): for node in ResourceNode.get_all():
register_resource_node(node) register_resource_node(node)
func register_resource_node(node: ResourceNode) -> bool: func register_resource_node(node: ResourceNode) -> bool:
if node == null or node.node_id.is_empty(): if node == null or node.node_id.is_empty():
return false return false
@@ -502,8 +506,10 @@ func register_resource_node(node: ResourceNode) -> bool:
or action_definition.resource_action_id != node.action_id or action_definition.resource_action_id != node.action_id
): ):
push_error( push_error(
"SimulationManager: ResourceNode '%s' has invalid action_id '%s'" (
% [node.node_id, node.action_id] "SimulationManager: ResourceNode '%s' has invalid action_id '%s'"
% [node.node_id, node.action_id]
)
) )
return false return false
var resource_state := get_resource_state(node.node_id) var resource_state := get_resource_state(node.node_id)
@@ -511,46 +517,40 @@ func register_resource_node(node: ResourceNode) -> bool:
resource_state = ResourceStateRecord.create_from_node(node) resource_state = ResourceStateRecord.create_from_node(node)
resource_states[node.node_id] = resource_state resource_states[node.node_id] = resource_state
elif not resource_state.apply_definition(node): elif not resource_state.apply_definition(node):
push_error( push_error("SimulationManager: ResourceNode definition mismatch for '%s'" % node.node_id)
"SimulationManager: ResourceNode definition mismatch for '%s'"
% node.node_id
)
return false return false
return node.bind_state(resource_state) return node.bind_state(resource_state)
func get_resource_state(node_id: StringName) -> ResourceStateRecord: func get_resource_state(node_id: StringName) -> ResourceStateRecord:
return resource_states.get(node_id) as ResourceStateRecord return resource_states.get(node_id) as ResourceStateRecord
func reserve_resource(node_id: StringName, agent_id: int) -> bool: func reserve_resource(node_id: StringName, agent_id: int) -> bool:
var resource_state := get_resource_state(node_id) var resource_state := get_resource_state(node_id)
return resource_state != null and resource_state.reserve(agent_id) return resource_state != null and resource_state.reserve(agent_id)
func release_resource(node_id: StringName, agent_id: int) -> void: func release_resource(node_id: StringName, agent_id: int) -> void:
var resource_state := get_resource_state(node_id) var resource_state := get_resource_state(node_id)
if resource_state != null: if resource_state != null:
resource_state.release(agent_id) resource_state.release(agent_id)
func find_resource_node_for_player(
from_position: Vector3, func find_resource_node_for_player(from_position: Vector3, max_distance: float) -> ResourceNode:
max_distance: float
) -> ResourceNode:
var best: ResourceNode var best: ResourceNode
var best_distance := max_distance * max_distance var best_distance := max_distance * max_distance
for node in ResourceNode.get_all(): for node in ResourceNode.get_all():
var resource_state := get_resource_state(node.node_id) var resource_state := get_resource_state(node.node_id)
if ( if resource_state == null or not resource_state.can_player_use_resource():
resource_state == null
or not resource_state.can_player_use_resource()
):
continue continue
var distance := from_position.distance_squared_to( var distance := from_position.distance_squared_to(node.interaction_point.global_position)
node.interaction_point.global_position
)
if distance <= best_distance: if distance <= best_distance:
best_distance = distance best_distance = distance
best = node best = node
return best return best
func harvest_resource_node(node: ResourceNode) -> float: func harvest_resource_node(node: ResourceNode) -> float:
if node == null: if node == null:
return 0.0 return 0.0
@@ -570,9 +570,11 @@ func harvest_resource_node(node: ResourceNode) -> float:
SimulationIds.EVENT_RESOURCE_EXTRACTED, SimulationIds.EVENT_RESOURCE_EXTRACTED,
-1, -1,
resource_state.get_node_id(), resource_state.get_node_id(),
SimulationIds.STORAGE_VILLAGE_PANTRY (
if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD SimulationIds.STORAGE_VILLAGE_PANTRY
else &"village", if resource_state.get_resource_id() == SimulationIds.RESOURCE_FOOD
else &"village"
),
resource_state.get_resource_id(), resource_state.get_resource_id(),
extracted extracted
) )
@@ -590,11 +592,13 @@ func harvest_resource_node(node: ResourceNode) -> float:
return extracted return extracted
func eat_food(amount: float) -> void: func eat_food(amount: float) -> void:
get_pantry().withdraw(SimulationIds.RESOURCE_FOOD, amount) get_pantry().withdraw(SimulationIds.RESOURCE_FOOD, amount)
_sync_village_food() _sync_village_food()
village_changed.emit(village) village_changed.emit(village)
func add_food(amount: float) -> void: func add_food(amount: float) -> void:
if amount >= 0.0: if amount >= 0.0:
get_pantry().deposit(SimulationIds.RESOURCE_FOOD, amount) get_pantry().deposit(SimulationIds.RESOURCE_FOOD, amount)
@@ -603,18 +607,22 @@ func add_food(amount: float) -> void:
_sync_village_food() _sync_village_food()
village_changed.emit(village) village_changed.emit(village)
func add_wood(amount: float) -> void: func add_wood(amount: float) -> void:
village.apply_resource_delta(&"wood", amount) village.apply_resource_delta(&"wood", amount)
village_changed.emit(village) village_changed.emit(village)
func add_safety(amount: float) -> void: func add_safety(amount: float) -> void:
village.apply_resource_delta(&"safety", amount) village.apply_resource_delta(&"safety", amount)
village_changed.emit(village) village_changed.emit(village)
func add_knowledge(amount: float) -> void: func add_knowledge(amount: float) -> void:
village.apply_resource_delta(&"knowledge", amount) village.apply_resource_delta(&"knowledge", amount)
village_changed.emit(village) village_changed.emit(village)
func get_starving_count() -> int: func get_starving_count() -> int:
var count := 0 var count := 0
for npc in npcs: for npc in npcs:
@@ -622,37 +630,42 @@ func get_starving_count() -> int:
count += 1 count += 1
return count return count
func get_state_snapshot() -> Dictionary: func get_state_snapshot() -> Dictionary:
var npc_snapshots: Array[Dictionary] = [] var npc_snapshots: Array[Dictionary] = []
for npc in npcs: for npc in npcs:
npc_snapshots.append({ npc_snapshots.append(
"id": npc.id, {
"name": npc.npc_name, "id": npc.id,
"profession": npc.profession, "name": npc.npc_name,
"hunger": npc.hunger, "profession": npc.profession,
"energy": npc.energy, "hunger": npc.hunger,
"strength": npc.strength, "energy": npc.energy,
"intelligence": npc.intelligence, "strength": npc.strength,
"task": npc.current_task, "intelligence": npc.intelligence,
"task_state": npc.task_state, "task": npc.current_task,
"task_duration": npc.task_duration, "task_state": npc.task_state,
"task_progress": npc.task_progress, "task_duration": npc.task_duration,
"target_id": String(npc.target_id), "task_progress": npc.task_progress,
"position": [npc.position.x, npc.position.y, npc.position.z], "target_id": String(npc.target_id),
"travel_target_position": [ "position": [npc.position.x, npc.position.y, npc.position.z],
npc.travel_target_position.x, "travel_target_position":
npc.travel_target_position.y, [
npc.travel_target_position.z npc.travel_target_position.x,
], npc.travel_target_position.y,
"has_travel_target": npc.has_travel_target, npc.travel_target_position.z
"starvation_ticks": npc.starvation_ticks, ],
"is_dead": npc.is_dead "has_travel_target": npc.has_travel_target,
}) "starvation_ticks": npc.starvation_ticks,
"is_dead": npc.is_dead
}
)
return { return {
"seed": simulation_seed, "seed": simulation_seed,
"tick_count": tick_count, "tick_count": tick_count,
"village": { "village":
{
"food": village.food, "food": village.food,
"wood": village.wood, "wood": village.wood,
"safety": village.safety, "safety": village.safety,
@@ -661,9 +674,11 @@ func get_state_snapshot() -> Dictionary:
"npcs": npc_snapshots "npcs": npc_snapshots
} }
func get_state_checksum() -> String: func get_state_checksum() -> String:
return create_state_record().to_json().sha256_text() return create_state_record().to_json().sha256_text()
func create_state_record() -> SimulationStateRecord: func create_state_record() -> SimulationStateRecord:
var record := SimulationStateRecord.new() var record := SimulationStateRecord.new()
var wander_streams: Array[Dictionary] = [] var wander_streams: Array[Dictionary] = []
@@ -671,11 +686,9 @@ func create_state_record() -> SimulationStateRecord:
sorted_npc_ids.sort() sorted_npc_ids.sort()
for npc_id in sorted_npc_ids: for npc_id in sorted_npc_ids:
var source: RandomNumberGenerator = wander_random_sources[npc_id] var source: RandomNumberGenerator = wander_random_sources[npc_id]
wander_streams.append({ wander_streams.append(
"npc_id": int(npc_id), {"npc_id": int(npc_id), "seed": str(source.seed), "state": str(source.state)}
"seed": str(source.seed), )
"state": str(source.state)
})
record.simulation = { record.simulation = {
"seed": simulation_seed, "seed": simulation_seed,
@@ -704,9 +717,11 @@ func create_state_record() -> SimulationStateRecord:
record.economic_events.append(event) record.economic_events.append(event)
return record return record
func serialize_state() -> String: func serialize_state() -> String:
return create_state_record().to_json() return create_state_record().to_json()
func restore_state_from_json(json_text: String) -> bool: func restore_state_from_json(json_text: String) -> bool:
var record := SimulationStateRecord.from_json(json_text) var record := SimulationStateRecord.from_json(json_text)
if record == null: if record == null:
@@ -714,6 +729,7 @@ func restore_state_from_json(json_text: String) -> bool:
return false return false
return restore_state(record) return restore_state(record)
func restore_state(record: SimulationStateRecord) -> bool: func restore_state(record: SimulationStateRecord) -> bool:
if record == null: if record == null:
return false return false
@@ -1,6 +1,7 @@
class_name ActionExecutionSystem class_name ActionExecutionSystem
extends RefCounted extends RefCounted
func advance_npc(npc: SimNPC, village: SimVillage) -> void: func advance_npc(npc: SimNPC, village: SimVillage) -> void:
if npc.is_dead: if npc.is_dead:
return return
@@ -12,6 +13,7 @@ func advance_npc(npc: SimNPC, village: SimVillage) -> void:
npc.task_complete = true npc.task_complete = true
npc.task_state = SimNPC.TASK_STATE_COMPLETE npc.task_state = SimNPC.TASK_STATE_COMPLETE
func _update_needs(npc: SimNPC, village: SimVillage) -> void: func _update_needs(npc: SimNPC, village: SimVillage) -> void:
npc.hunger += 3.0 * village.food_modifier npc.hunger += 3.0 * village.food_modifier
match npc.task_state: match npc.task_state:
+2 -4
View File
@@ -4,9 +4,7 @@ extends RefCounted
var action_id: StringName var action_id: StringName
var duration_override := -1.0 var duration_override := -1.0
func _init(
selected_action_id: StringName, func _init(selected_action_id: StringName, selected_duration_override: float = -1.0) -> void:
selected_duration_override: float = -1.0
) -> void:
action_id = selected_action_id action_id = selected_action_id
duration_override = selected_duration_override duration_override = selected_duration_override
+46 -23
View File
@@ -1,6 +1,7 @@
class_name ActionSelectionSystem class_name ActionSelectionSystem
extends RefCounted extends RefCounted
func select_action(npc: SimNPC, village: SimVillage) -> ActionSelectionResult: func select_action(npc: SimNPC, village: SimVillage) -> ActionSelectionResult:
if npc.is_dead: if npc.is_dead:
return null return null
@@ -8,10 +9,7 @@ func select_action(npc: SimNPC, village: SimVillage) -> ActionSelectionResult:
if npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD) >= 1.0: if npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD) >= 1.0:
return ActionSelectionResult.new(SimulationIds.ACTION_EAT, 1.0) return ActionSelectionResult.new(SimulationIds.ACTION_EAT, 1.0)
if village.food > 0: if village.food > 0:
return ActionSelectionResult.new( return ActionSelectionResult.new(SimulationIds.ACTION_WITHDRAW_FOOD, 1.0)
SimulationIds.ACTION_WITHDRAW_FOOD,
1.0
)
return ActionSelectionResult.new(SimulationIds.ACTION_GATHER_FOOD, 4.0) return ActionSelectionResult.new(SimulationIds.ACTION_GATHER_FOOD, 4.0)
if npc.hunger > 75.0: if npc.hunger > 75.0:
if npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD) >= 1.0: if npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD) >= 1.0:
@@ -25,34 +23,58 @@ func select_action(npc: SimNPC, village: SimVillage) -> ActionSelectionResult:
return ActionSelectionResult.new(SimulationIds.ACTION_REST) return ActionSelectionResult.new(SimulationIds.ACTION_REST)
return ActionSelectionResult.new(_choose_best_work_action(npc, village)) return ActionSelectionResult.new(_choose_best_work_action(npc, village))
func _choose_best_work_action(
npc: SimNPC, func _choose_best_work_action(npc: SimNPC, village: SimVillage) -> StringName:
village: SimVillage
) -> StringName:
var scores := { var scores := {
SimulationIds.ACTION_GATHER_FOOD: _calculate_score( SimulationIds.ACTION_GATHER_FOOD:
npc, SimulationIds.ACTION_GATHER_FOOD, village.food_priority, _calculate_score(
village.food, 15.0, 30.0, 3.0, 1.5 npc,
SimulationIds.ACTION_GATHER_FOOD,
village.food_priority,
village.food,
15.0,
30.0,
3.0,
1.5
), ),
SimulationIds.ACTION_GATHER_WOOD: _calculate_score( SimulationIds.ACTION_GATHER_WOOD:
npc, SimulationIds.ACTION_GATHER_WOOD, village.wood_priority, _calculate_score(
village.wood, 10.0, 20.0, 2.5, 1.0 npc,
SimulationIds.ACTION_GATHER_WOOD,
village.wood_priority,
village.wood,
10.0,
20.0,
2.5,
1.0
), ),
SimulationIds.ACTION_PATROL: _calculate_score( SimulationIds.ACTION_PATROL:
npc, SimulationIds.ACTION_PATROL, village.safety_priority, _calculate_score(
village.safety, 30.0, 50.0, 3.0, 1.5 npc,
SimulationIds.ACTION_PATROL,
village.safety_priority,
village.safety,
30.0,
50.0,
3.0,
1.5
), ),
SimulationIds.ACTION_STUDY: _calculate_score( SimulationIds.ACTION_STUDY:
npc, SimulationIds.ACTION_STUDY, village.knowledge_priority, _calculate_score(
village.knowledge, 10.0, 25.0, 1.5, 0.75 npc,
SimulationIds.ACTION_STUDY,
village.knowledge_priority,
village.knowledge,
10.0,
25.0,
1.5,
0.75
) )
} }
var best_action := SimulationIds.ACTION_GATHER_FOOD var best_action := SimulationIds.ACTION_GATHER_FOOD
var best_score: float = scores[best_action] var best_score: float = scores[best_action]
for action_id in [ for action_id in [
SimulationIds.ACTION_GATHER_WOOD, SimulationIds.ACTION_GATHER_WOOD, SimulationIds.ACTION_PATROL, SimulationIds.ACTION_STUDY
SimulationIds.ACTION_PATROL,
SimulationIds.ACTION_STUDY
]: ]:
var score: float = scores[action_id] var score: float = scores[action_id]
if score > best_score: if score > best_score:
@@ -60,6 +82,7 @@ func _choose_best_work_action(
best_score = score best_score = score
return best_action return best_action
func _calculate_score( func _calculate_score(
npc: SimNPC, npc: SimNPC,
action_id: StringName, action_id: StringName,
+8 -19
View File
@@ -1,11 +1,9 @@
class_name ActionTargetResolver class_name ActionTargetResolver
extends RefCounted extends RefCounted
func resolve( func resolve(
npc: SimNPC, npc: SimNPC, origin: Vector3, simulation_manager: Node, active_world_adapter: Node
origin: Vector3,
simulation_manager: Node,
active_world_adapter: Node
) -> Dictionary: ) -> Dictionary:
var definition := SimulationDefinitions.get_action(npc.current_task) var definition := SimulationDefinitions.get_action(npc.current_task)
if definition == null: if definition == null:
@@ -13,18 +11,17 @@ func resolve(
match definition.target_type: match definition.target_type:
SimulationIds.TARGET_RESOURCE: SimulationIds.TARGET_RESOURCE:
return _resolve_resource( return _resolve_resource(
npc, origin, definition, simulation_manager, npc, origin, definition, simulation_manager, active_world_adapter
active_world_adapter
) )
SimulationIds.TARGET_ACTIVITY: SimulationIds.TARGET_ACTIVITY:
return active_world_adapter.get_activity_target(npc.current_task) return active_world_adapter.get_activity_target(npc.current_task)
SimulationIds.TARGET_FREE: SimulationIds.TARGET_FREE:
return { return {
"target_id": "", "target_id": "", "position": origin + simulation_manager.get_wander_offset(npc.id)
"position": origin + simulation_manager.get_wander_offset(npc.id)
} }
return {} return {}
func _resolve_resource( func _resolve_resource(
npc: SimNPC, npc: SimNPC,
origin: Vector3, origin: Vector3,
@@ -34,18 +31,10 @@ func _resolve_resource(
) -> Dictionary: ) -> Dictionary:
var best: Dictionary = {} var best: Dictionary = {}
var best_distance := INF var best_distance := INF
for candidate in active_world_adapter.get_resource_candidates( for candidate in active_world_adapter.get_resource_candidates(definition.resource_action_id):
definition.resource_action_id
):
var node_id := StringName(candidate["target_id"]) var node_id := StringName(candidate["target_id"])
var state: ResourceStateRecord = simulation_manager.get_resource_state( var state: ResourceStateRecord = simulation_manager.get_resource_state(node_id)
node_id if state == null or not state.can_npc_use() or not state.is_available_for(npc.id):
)
if (
state == null
or not state.can_npc_use()
or not state.is_available_for(npc.id)
):
continue continue
var position: Vector3 = candidate["position"] var position: Vector3 = candidate["position"]
var distance := origin.distance_squared_to(position) var distance := origin.distance_squared_to(position)
+7 -8
View File
@@ -8,6 +8,7 @@ extends Resource
@export var target_type: StringName = SimulationIds.TARGET_ACTIVITY @export var target_type: StringName = SimulationIds.TARGET_ACTIVITY
@export var resource_action_id: StringName @export var resource_action_id: StringName
func validate() -> Array[String]: func validate() -> Array[String]:
var errors: Array[String] = [] var errors: Array[String] = []
if action_id.is_empty(): if action_id.is_empty():
@@ -16,15 +17,13 @@ func validate() -> Array[String]:
errors.append("display_name is empty for '%s'" % action_id) errors.append("display_name is empty for '%s'" % action_id)
if default_duration <= 0.0: if default_duration <= 0.0:
errors.append("default_duration must be positive for '%s'" % action_id) errors.append("default_duration must be positive for '%s'" % action_id)
if target_type not in [
SimulationIds.TARGET_RESOURCE,
SimulationIds.TARGET_ACTIVITY,
SimulationIds.TARGET_FREE
]:
errors.append("target_type '%s' is invalid for '%s'" % [target_type, action_id])
if ( if (
target_type == SimulationIds.TARGET_RESOURCE target_type
and resource_action_id.is_empty() not in [
SimulationIds.TARGET_RESOURCE, SimulationIds.TARGET_ACTIVITY, SimulationIds.TARGET_FREE
]
): ):
errors.append("target_type '%s' is invalid for '%s'" % [target_type, action_id])
if target_type == SimulationIds.TARGET_RESOURCE and resource_action_id.is_empty():
errors.append("resource_action_id is required for '%s'" % action_id) errors.append("resource_action_id is required for '%s'" % action_id)
return errors return errors
@@ -4,6 +4,7 @@ extends Resource
@export var profession_id: StringName @export var profession_id: StringName
@export var display_name: String @export var display_name: String
func validate() -> Array[String]: func validate() -> Array[String]:
var errors: Array[String] = [] var errors: Array[String] = []
if profession_id.is_empty(): if profession_id.is_empty():
@@ -21,6 +21,7 @@ const PROFESSION_PATHS := [
"res://simulation/definitions/professions/wanderer.tres" "res://simulation/definitions/professions/wanderer.tres"
] ]
static func get_actions() -> Array[ActionDefinition]: static func get_actions() -> Array[ActionDefinition]:
var definitions: Array[ActionDefinition] = [] var definitions: Array[ActionDefinition] = []
for path in ACTION_PATHS: for path in ACTION_PATHS:
@@ -29,6 +30,7 @@ static func get_actions() -> Array[ActionDefinition]:
definitions.append(definition) definitions.append(definition)
return definitions return definitions
static func get_professions() -> Array[ProfessionDefinition]: static func get_professions() -> Array[ProfessionDefinition]:
var definitions: Array[ProfessionDefinition] = [] var definitions: Array[ProfessionDefinition] = []
for path in PROFESSION_PATHS: for path in PROFESSION_PATHS:
@@ -37,26 +39,28 @@ static func get_professions() -> Array[ProfessionDefinition]:
definitions.append(definition) definitions.append(definition)
return definitions return definitions
static func get_action(action_id: StringName) -> ActionDefinition: static func get_action(action_id: StringName) -> ActionDefinition:
for definition in get_actions(): for definition in get_actions():
if definition.action_id == action_id: if definition.action_id == action_id:
return definition return definition
return null return null
static func get_profession(
profession_id: StringName static func get_profession(profession_id: StringName) -> ProfessionDefinition:
) -> ProfessionDefinition:
for definition in get_professions(): for definition in get_professions():
if definition.profession_id == profession_id: if definition.profession_id == profession_id:
return definition return definition
return null return null
static func get_profession_ids() -> Array[StringName]: static func get_profession_ids() -> Array[StringName]:
var ids: Array[StringName] = [] var ids: Array[StringName] = []
for definition in get_professions(): for definition in get_professions():
ids.append(definition.profession_id) ids.append(definition.profession_id)
return ids return ids
static func validate() -> Array[String]: static func validate() -> Array[String]:
var errors: Array[String] = [] var errors: Array[String] = []
if get_actions().size() != ACTION_PATHS.size(): if get_actions().size() != ACTION_PATHS.size():
@@ -85,15 +89,19 @@ static func validate() -> Array[String]:
and not profession_ids.has(definition.preferred_profession_id) and not profession_ids.has(definition.preferred_profession_id)
): ):
errors.append( errors.append(
"Action '%s' references unknown profession '%s'" (
% [definition.action_id, definition.preferred_profession_id] "Action '%s' references unknown profession '%s'"
% [definition.action_id, definition.preferred_profession_id]
)
) )
if ( if (
definition.target_type == SimulationIds.TARGET_RESOURCE definition.target_type == SimulationIds.TARGET_RESOURCE
and not action_ids.has(definition.resource_action_id) and not action_ids.has(definition.resource_action_id)
): ):
errors.append( errors.append(
"Action '%s' references unknown resource action '%s'" (
% [definition.action_id, definition.resource_action_id] "Action '%s' references unknown resource action '%s'"
% [definition.action_id, definition.resource_action_id]
)
) )
return errors return errors
@@ -4,6 +4,7 @@ extends Node
var store := SaveSlotStore.new() var store := SaveSlotStore.new()
func _unhandled_key_input(event: InputEvent) -> void: func _unhandled_key_input(event: InputEvent) -> void:
if not event.pressed or event.echo: if not event.pressed or event.echo:
return return
+12 -4
View File
@@ -8,9 +8,11 @@ const MAX_SAVE_BYTES := 16 * 1024 * 1024
var save_directory: String var save_directory: String
var last_error := "" var last_error := ""
func _init(directory: String = DEFAULT_DIRECTORY) -> void: func _init(directory: String = DEFAULT_DIRECTORY) -> void:
save_directory = directory save_directory = directory
func save(manager: Node, slot_name: String = DEFAULT_SLOT) -> bool: func save(manager: Node, slot_name: String = DEFAULT_SLOT) -> bool:
last_error = "" last_error = ""
if manager == null or not manager.has_method("serialize_state"): if manager == null or not manager.has_method("serialize_state"):
@@ -55,6 +57,7 @@ func save(manager: Node, slot_name: String = DEFAULT_SLOT) -> bool:
_remove_if_present(backup_path) _remove_if_present(backup_path)
return true return true
func load_into(manager: Node, slot_name: String = DEFAULT_SLOT) -> bool: func load_into(manager: Node, slot_name: String = DEFAULT_SLOT) -> bool:
last_error = "" last_error = ""
if manager == null or not manager.has_method("restore_state"): if manager == null or not manager.has_method("restore_state"):
@@ -72,14 +75,13 @@ func load_into(manager: Node, slot_name: String = DEFAULT_SLOT) -> bool:
return _fail("Simulation refused the validated save record") return _fail("Simulation refused the validated save record")
return true return true
func has_slot(slot_name: String = DEFAULT_SLOT) -> bool: func has_slot(slot_name: String = DEFAULT_SLOT) -> bool:
if not _is_valid_slot_name(slot_name): if not _is_valid_slot_name(slot_name):
return false return false
var final_path := get_slot_path(slot_name) var final_path := get_slot_path(slot_name)
return ( return _read_record(final_path) != null or _read_record(final_path + ".bak") != null
_read_record(final_path) != null
or _read_record(final_path + ".bak") != null
)
func delete_slot(slot_name: String = DEFAULT_SLOT) -> bool: func delete_slot(slot_name: String = DEFAULT_SLOT) -> bool:
if not _is_valid_slot_name(slot_name): if not _is_valid_slot_name(slot_name):
@@ -90,9 +92,11 @@ func delete_slot(slot_name: String = DEFAULT_SLOT) -> bool:
_remove_if_present(final_path + ".bak") _remove_if_present(final_path + ".bak")
return true return true
func get_slot_path(slot_name: String = DEFAULT_SLOT) -> String: func get_slot_path(slot_name: String = DEFAULT_SLOT) -> String:
return save_directory.path_join(slot_name + ".json") return save_directory.path_join(slot_name + ".json")
func _read_record(path: String) -> SimulationStateRecord: func _read_record(path: String) -> SimulationStateRecord:
if not FileAccess.file_exists(path): if not FileAccess.file_exists(path):
return null return null
@@ -106,6 +110,7 @@ func _read_record(path: String) -> SimulationStateRecord:
file.close() file.close()
return SimulationStateRecord.from_json(json_text) return SimulationStateRecord.from_json(json_text)
func _ensure_directory() -> bool: func _ensure_directory() -> bool:
var absolute_directory := ProjectSettings.globalize_path(save_directory) var absolute_directory := ProjectSettings.globalize_path(save_directory)
var error := DirAccess.make_dir_recursive_absolute(absolute_directory) var error := DirAccess.make_dir_recursive_absolute(absolute_directory)
@@ -113,6 +118,7 @@ func _ensure_directory() -> bool:
return _fail("Could not create the save directory") return _fail("Could not create the save directory")
return true return true
func _is_valid_slot_name(slot_name: String) -> bool: func _is_valid_slot_name(slot_name: String) -> bool:
if slot_name.is_empty() or slot_name.length() > 32: if slot_name.is_empty() or slot_name.length() > 32:
return false return false
@@ -122,10 +128,12 @@ func _is_valid_slot_name(slot_name: String) -> bool:
return false return false
return true return true
func _remove_if_present(path: String) -> void: func _remove_if_present(path: String) -> void:
if FileAccess.file_exists(path): if FileAccess.file_exists(path):
DirAccess.remove_absolute(path) DirAccess.remove_absolute(path)
func _fail(message: String) -> bool: func _fail(message: String) -> bool:
last_error = message last_error = message
return false return false
+29 -15
View File
@@ -5,9 +5,11 @@ const SCHEMA_VERSION := 1
var data: Dictionary var data: Dictionary
func _init(record_data: Dictionary = {}) -> void: func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true) data = record_data.duplicate(true)
static func create( static func create(
event_id: int, event_id: int,
event_type: StringName, event_type: StringName,
@@ -18,25 +20,36 @@ static func create(
item_id: StringName, item_id: StringName,
amount: float amount: float
) -> EconomicEventRecord: ) -> EconomicEventRecord:
return EconomicEventRecord.new({ return EconomicEventRecord.new(
"schema_version": SCHEMA_VERSION, {
"event_id": event_id, "schema_version": SCHEMA_VERSION,
"event_type": String(event_type), "event_id": event_id,
"tick": tick, "event_type": String(event_type),
"actor_id": actor_id, "tick": tick,
"source_id": String(source_id), "actor_id": actor_id,
"destination_id": String(destination_id), "source_id": String(source_id),
"item_id": String(item_id), "destination_id": String(destination_id),
"amount": amount "item_id": String(item_id),
}) "amount": amount
}
)
static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord: static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION: if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
return null return null
if not record_data.has_all([ if not record_data.has_all(
"event_id", "event_type", "tick", "actor_id", "source_id", [
"destination_id", "item_id", "amount" "event_id",
]): "event_type",
"tick",
"actor_id",
"source_id",
"destination_id",
"item_id",
"amount"
]
):
return null return null
var normalized := record_data.duplicate(true) var normalized := record_data.duplicate(true)
normalized["schema_version"] = SCHEMA_VERSION normalized["schema_version"] = SCHEMA_VERSION
@@ -60,5 +73,6 @@ static func from_dictionary(record_data: Dictionary) -> EconomicEventRecord:
return null return null
return EconomicEventRecord.new(normalized) return EconomicEventRecord.new(normalized)
func to_dictionary() -> Dictionary: func to_dictionary() -> Dictionary:
return data.duplicate(true) return data.duplicate(true)
+74 -62
View File
@@ -7,41 +7,47 @@ const PREVIOUS_SCHEMA_VERSION := 2
var data: Dictionary var data: Dictionary
func _init(record_data: Dictionary = {}) -> void: func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true) data = record_data.duplicate(true)
static func capture(npc: SimNPC) -> NPCStateRecord: static func capture(npc: SimNPC) -> NPCStateRecord:
return NPCStateRecord.new({ return NPCStateRecord.new(
"schema_version": SCHEMA_VERSION, {
"id": npc.id, "schema_version": SCHEMA_VERSION,
"name": npc.npc_name, "id": npc.id,
"profession": String(npc.profession), "name": npc.npc_name,
"hunger": npc.hunger, "profession": String(npc.profession),
"energy": npc.energy, "hunger": npc.hunger,
"strength": npc.strength, "energy": npc.energy,
"intelligence": npc.intelligence, "strength": npc.strength,
"current_task": String(npc.current_task), "intelligence": npc.intelligence,
"task_state": String(npc.task_state), "current_task": String(npc.current_task),
"position": [npc.position.x, npc.position.y, npc.position.z], "task_state": String(npc.task_state),
"is_starving": npc.is_starving, "position": [npc.position.x, npc.position.y, npc.position.z],
"starvation_ticks": npc.starvation_ticks, "is_starving": npc.is_starving,
"starvation_death_threshold": npc.starvation_death_threshold, "starvation_ticks": npc.starvation_ticks,
"is_dead": npc.is_dead, "starvation_death_threshold": npc.starvation_death_threshold,
"task_duration": npc.task_duration, "is_dead": npc.is_dead,
"task_progress": npc.task_progress, "task_duration": npc.task_duration,
"task_complete": npc.task_complete, "task_progress": npc.task_progress,
"target_id": String(npc.target_id), "task_complete": npc.task_complete,
"travel_target_position": [ "target_id": String(npc.target_id),
npc.travel_target_position.x, "travel_target_position":
npc.travel_target_position.y, [
npc.travel_target_position.z npc.travel_target_position.x,
], npc.travel_target_position.y,
"has_travel_target": npc.has_travel_target, npc.travel_target_position.z
"inventory": npc.inventory.duplicate(true), ],
"last_task": String(npc.last_task), "has_travel_target": npc.has_travel_target,
"random_seed": str(npc.random_source.seed), "inventory": npc.inventory.duplicate(true),
"random_state": str(npc.random_source.state) "last_task": String(npc.last_task),
}) "random_seed": str(npc.random_source.seed),
"random_state": str(npc.random_source.state)
}
)
static func from_dictionary(record_data: Dictionary) -> NPCStateRecord: static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
var version := int(record_data.get("schema_version", -1)) var version := int(record_data.get("schema_version", -1))
@@ -49,23 +55,40 @@ static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
record_data = _migrate_legacy(record_data, version) record_data = _migrate_legacy(record_data, version)
elif version != SCHEMA_VERSION: elif version != SCHEMA_VERSION:
return null return null
if not record_data.has_all([ if not record_data.has_all(
"id", "name", "profession", "hunger", "energy", "strength", [
"intelligence", "current_task", "task_state", "position", "id",
"is_starving", "starvation_ticks", "starvation_death_threshold", "name",
"is_dead", "task_duration", "task_progress", "task_complete", "profession",
"target_id", "travel_target_position", "has_travel_target", "inventory", "hunger",
"last_task", "random_seed", "random_state" "energy",
]): "strength",
"intelligence",
"current_task",
"task_state",
"position",
"is_starving",
"starvation_ticks",
"starvation_death_threshold",
"is_dead",
"task_duration",
"task_progress",
"task_complete",
"target_id",
"travel_target_position",
"has_travel_target",
"inventory",
"last_task",
"random_seed",
"random_state"
]
):
return null return null
var saved_position = record_data["position"] var saved_position = record_data["position"]
if not saved_position is Array or saved_position.size() != 3: if not saved_position is Array or saved_position.size() != 3:
return null return null
var saved_target_position = record_data["travel_target_position"] var saved_target_position = record_data["travel_target_position"]
if ( if not saved_target_position is Array or saved_target_position.size() != 3:
not saved_target_position is Array
or saved_target_position.size() != 3
):
return null return null
if not record_data["inventory"] is Dictionary: if not record_data["inventory"] is Dictionary:
return null return null
@@ -74,36 +97,26 @@ static func from_dictionary(record_data: Dictionary) -> NPCStateRecord:
return null return null
var action_id := StringName(record_data["current_task"]) var action_id := StringName(record_data["current_task"])
if ( if (
action_id not in [ action_id not in [SimulationIds.ACTION_IDLE, SimulationIds.ACTION_DEAD]
SimulationIds.ACTION_IDLE,
SimulationIds.ACTION_DEAD
]
and SimulationDefinitions.get_action(action_id) == null and SimulationDefinitions.get_action(action_id) == null
): ):
return null return null
var last_action_id := StringName(record_data["last_task"]) var last_action_id := StringName(record_data["last_task"])
if ( if not last_action_id.is_empty() and SimulationDefinitions.get_action(last_action_id) == null:
not last_action_id.is_empty()
and SimulationDefinitions.get_action(last_action_id) == null
):
return null return null
return NPCStateRecord.new(record_data) return NPCStateRecord.new(record_data)
static func _migrate_legacy(
legacy_data: Dictionary, static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary:
version: int
) -> Dictionary:
var migrated := legacy_data.duplicate(true) var migrated := legacy_data.duplicate(true)
migrated["schema_version"] = SCHEMA_VERSION migrated["schema_version"] = SCHEMA_VERSION
if version == LEGACY_SCHEMA_VERSION: if version == LEGACY_SCHEMA_VERSION:
migrated["travel_target_position"] = legacy_data.get( migrated["travel_target_position"] = legacy_data.get("position", [0.0, 0.0, 0.0])
"position",
[0.0, 0.0, 0.0]
)
migrated["has_travel_target"] = false migrated["has_travel_target"] = false
migrated["inventory"] = {} migrated["inventory"] = {}
return migrated return migrated
func restore(debug_logs: bool) -> SimNPC: func restore(debug_logs: bool) -> SimNPC:
var random_source := RandomNumberGenerator.new() var random_source := RandomNumberGenerator.new()
random_source.seed = String(data["random_seed"]).to_int() random_source.seed = String(data["random_seed"]).to_int()
@@ -122,9 +135,7 @@ func restore(debug_logs: bool) -> SimNPC:
npc.current_task = StringName(data["current_task"]) npc.current_task = StringName(data["current_task"])
npc.task_state = StringName(data["task_state"]) npc.task_state = StringName(data["task_state"])
npc.position = Vector3( npc.position = Vector3(
float(saved_position[0]), float(saved_position[0]), float(saved_position[1]), float(saved_position[2])
float(saved_position[1]),
float(saved_position[2])
) )
npc.is_starving = bool(data["is_starving"]) npc.is_starving = bool(data["is_starving"])
npc.starvation_ticks = int(data["starvation_ticks"]) npc.starvation_ticks = int(data["starvation_ticks"])
@@ -146,5 +157,6 @@ func restore(debug_logs: bool) -> SimNPC:
npc.debug_logs = debug_logs npc.debug_logs = debug_logs
return npc return npc
func to_dictionary() -> Dictionary: func to_dictionary() -> Dictionary:
return data.duplicate(true) return data.duplicate(true)
+53 -28
View File
@@ -9,22 +9,27 @@ const LEGACY_SCHEMA_VERSION := 1
var data: Dictionary var data: Dictionary
func _init(record_data: Dictionary = {}) -> void: func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true) data = record_data.duplicate(true)
static func create_from_node(node: ResourceNode) -> ResourceStateRecord: static func create_from_node(node: ResourceNode) -> ResourceStateRecord:
return ResourceStateRecord.new({ return ResourceStateRecord.new(
"schema_version": SCHEMA_VERSION, {
"node_id": String(node.node_id), "schema_version": SCHEMA_VERSION,
"action_id": String(node.action_id), "node_id": String(node.node_id),
"resource_id": String(node.resource_id), "action_id": String(node.action_id),
"amount_remaining": node.initial_amount, "resource_id": String(node.resource_id),
"yield_per_action": node.yield_per_action, "amount_remaining": node.initial_amount,
"reserved_by": -1, "yield_per_action": node.yield_per_action,
"enabled": node.initial_enabled, "reserved_by": -1,
"can_npcs_use": node.can_npcs_use, "enabled": node.initial_enabled,
"can_player_use": node.can_player_use "can_npcs_use": node.can_npcs_use,
}) "can_player_use": node.can_player_use
}
)
static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord: static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
var version := int(record_data.get("schema_version", -1)) var version := int(record_data.get("schema_version", -1))
@@ -33,26 +38,30 @@ static func from_dictionary(record_data: Dictionary) -> ResourceStateRecord:
elif version != SCHEMA_VERSION: elif version != SCHEMA_VERSION:
return null return null
if not record_data.has_all([ if not record_data.has_all(
"node_id", "action_id", "resource_id", "amount_remaining", [
"yield_per_action", "reserved_by", "enabled", "can_npcs_use", "node_id",
"can_player_use" "action_id",
]): "resource_id",
"amount_remaining",
"yield_per_action",
"reserved_by",
"enabled",
"can_npcs_use",
"can_player_use"
]
):
return null return null
if String(record_data["node_id"]).is_empty(): if String(record_data["node_id"]).is_empty():
return null return null
var action_id := StringName(record_data["action_id"]) var action_id := StringName(record_data["action_id"])
if ( if not action_id.is_empty() and SimulationDefinitions.get_action(action_id) == null:
not action_id.is_empty()
and SimulationDefinitions.get_action(action_id) == null
):
return null return null
return ResourceStateRecord.new(record_data) return ResourceStateRecord.new(record_data)
static func _migrate_v1(legacy_data: Dictionary) -> Dictionary: static func _migrate_v1(legacy_data: Dictionary) -> Dictionary:
if not legacy_data.has_all([ if not legacy_data.has_all(["node_id", "amount_remaining", "reserved_by", "enabled"]):
"node_id", "amount_remaining", "reserved_by", "enabled"
]):
return {} return {}
var migrated := legacy_data.duplicate(true) var migrated := legacy_data.duplicate(true)
migrated["schema_version"] = SCHEMA_VERSION migrated["schema_version"] = SCHEMA_VERSION
@@ -63,6 +72,7 @@ static func _migrate_v1(legacy_data: Dictionary) -> Dictionary:
migrated["can_player_use"] = false migrated["can_player_use"] = false
return migrated return migrated
func apply_definition(node: ResourceNode) -> bool: func apply_definition(node: ResourceNode) -> bool:
if node == null or node.node_id != get_node_id(): if node == null or node.node_id != get_node_id():
return false return false
@@ -85,44 +95,54 @@ func apply_definition(node: ResourceNode) -> bool:
and can_player_use_resource() == node.can_player_use and can_player_use_resource() == node.can_player_use
) )
func get_node_id() -> StringName: func get_node_id() -> StringName:
return StringName(data["node_id"]) return StringName(data["node_id"])
func get_action_id() -> StringName: func get_action_id() -> StringName:
return StringName(data["action_id"]) return StringName(data["action_id"])
func get_resource_id() -> StringName: func get_resource_id() -> StringName:
return StringName(data["resource_id"]) return StringName(data["resource_id"])
func get_amount_remaining() -> float: func get_amount_remaining() -> float:
return float(data["amount_remaining"]) return float(data["amount_remaining"])
func get_yield_per_action() -> float: func get_yield_per_action() -> float:
return float(data["yield_per_action"]) return float(data["yield_per_action"])
func get_reserved_by() -> int: func get_reserved_by() -> int:
return int(data["reserved_by"]) return int(data["reserved_by"])
func is_enabled() -> bool: func is_enabled() -> bool:
return bool(data["enabled"]) return bool(data["enabled"])
func can_npc_use() -> bool: func can_npc_use() -> bool:
return bool(data["can_npcs_use"]) return bool(data["can_npcs_use"])
func can_player_use_resource() -> bool: func can_player_use_resource() -> bool:
return bool(data["can_player_use"]) return bool(data["can_player_use"])
func is_depleted() -> bool: func is_depleted() -> bool:
return get_amount_remaining() <= 0.0 return get_amount_remaining() <= 0.0
func can_extract() -> bool: func can_extract() -> bool:
return is_enabled() and not is_depleted() return is_enabled() and not is_depleted()
func is_available_for(agent_id: int) -> bool: func is_available_for(agent_id: int) -> bool:
return ( return can_extract() and (get_reserved_by() == -1 or get_reserved_by() == agent_id)
can_extract()
and (get_reserved_by() == -1 or get_reserved_by() == agent_id)
)
func reserve(agent_id: int) -> bool: func reserve(agent_id: int) -> bool:
if not is_available_for(agent_id): if not is_available_for(agent_id):
@@ -131,12 +151,14 @@ func reserve(agent_id: int) -> bool:
changed.emit(self) changed.emit(self)
return true return true
func release(agent_id: int) -> void: func release(agent_id: int) -> void:
if get_reserved_by() != agent_id: if get_reserved_by() != agent_id:
return return
data["reserved_by"] = -1 data["reserved_by"] = -1
changed.emit(self) changed.emit(self)
func extract(requested_amount: float = -1.0) -> float: func extract(requested_amount: float = -1.0) -> float:
if not can_extract(): if not can_extract():
return 0.0 return 0.0
@@ -152,6 +174,7 @@ func extract(requested_amount: float = -1.0) -> float:
depleted.emit(self) depleted.emit(self)
return extracted return extracted
func set_amount_remaining(amount: float) -> void: func set_amount_remaining(amount: float) -> void:
var was_depleted := is_depleted() var was_depleted := is_depleted()
data["amount_remaining"] = maxf(amount, 0.0) data["amount_remaining"] = maxf(amount, 0.0)
@@ -161,9 +184,11 @@ func set_amount_remaining(amount: float) -> void:
if not was_depleted and is_depleted(): if not was_depleted and is_depleted():
depleted.emit(self) depleted.emit(self)
func set_enabled(value: bool) -> void: func set_enabled(value: bool) -> void:
data["enabled"] = value data["enabled"] = value
changed.emit(self) changed.emit(self)
func to_dictionary() -> Dictionary: func to_dictionary() -> Dictionary:
return data.duplicate(true) return data.duplicate(true)
+28 -16
View File
@@ -13,6 +13,7 @@ var resources: Array[ResourceStateRecord] = []
var storages: Array[StorageStateRecord] = [] var storages: Array[StorageStateRecord] = []
var economic_events: Array[EconomicEventRecord] = [] var economic_events: Array[EconomicEventRecord] = []
func to_dictionary() -> Dictionary: func to_dictionary() -> Dictionary:
var npc_data: Array[Dictionary] = [] var npc_data: Array[Dictionary] = []
for npc_record in npcs: for npc_record in npcs:
@@ -39,15 +40,18 @@ func to_dictionary() -> Dictionary:
"economic_events": event_data "economic_events": event_data
} }
func to_json() -> String: func to_json() -> String:
return JSON.stringify(to_dictionary()) return JSON.stringify(to_dictionary())
static func from_json(json_text: String) -> SimulationStateRecord: static func from_json(json_text: String) -> SimulationStateRecord:
var parsed = JSON.parse_string(json_text) var parsed = JSON.parse_string(json_text)
if not parsed is Dictionary: if not parsed is Dictionary:
return null return null
return from_dictionary(parsed) return from_dictionary(parsed)
static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord: static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
if record_data.get("schema", "") != SCHEMA_NAME: if record_data.get("schema", "") != SCHEMA_NAME:
return null return null
@@ -56,19 +60,25 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
record_data = _migrate_legacy(record_data, version) record_data = _migrate_legacy(record_data, version)
elif version != SCHEMA_VERSION: elif version != SCHEMA_VERSION:
return null return null
if not record_data.has_all([ if not record_data.has_all(
"simulation", "village", "npcs", "resources", "storages", ["simulation", "village", "npcs", "resources", "storages", "economic_events"]
"economic_events" ):
]):
return null return null
var simulation_data = record_data["simulation"] var simulation_data = record_data["simulation"]
if not simulation_data is Dictionary: if not simulation_data is Dictionary:
return null return null
if not simulation_data.has_all([ if not simulation_data.has_all(
"seed", "tick_interval", "tick_count", "clock_accumulator", [
"clock_elapsed_ticks", "wander_random_streams", "next_event_id" "seed",
]): "tick_interval",
"tick_count",
"clock_accumulator",
"clock_elapsed_ticks",
"wander_random_streams",
"next_event_id"
]
):
return null return null
var wander_streams = simulation_data["wander_random_streams"] var wander_streams = simulation_data["wander_random_streams"]
if not wander_streams is Array: if not wander_streams is Array:
@@ -160,20 +170,22 @@ static func from_dictionary(record_data: Dictionary) -> SimulationStateRecord:
return record return record
static func _migrate_legacy(
legacy_data: Dictionary, static func _migrate_legacy(legacy_data: Dictionary, version: int) -> Dictionary:
version: int
) -> Dictionary:
var migrated := legacy_data.duplicate(true) var migrated := legacy_data.duplicate(true)
migrated["schema_version"] = SCHEMA_VERSION migrated["schema_version"] = SCHEMA_VERSION
if version == LEGACY_SCHEMA_VERSION: if version == LEGACY_SCHEMA_VERSION:
var village_data: Dictionary = legacy_data.get("village", {}) var village_data: Dictionary = legacy_data.get("village", {})
var initial_food := float(village_data.get("food", 0.0)) var initial_food := float(village_data.get("food", 0.0))
migrated["storages"] = [ migrated["storages"] = [
StorageStateRecord.create( (
SimulationIds.STORAGE_VILLAGE_PANTRY, StorageStateRecord
{String(SimulationIds.RESOURCE_FOOD): initial_food} . create(
).to_dictionary() SimulationIds.STORAGE_VILLAGE_PANTRY,
{String(SimulationIds.RESOURCE_FOOD): initial_food}
)
. to_dictionary()
)
] ]
migrated["economic_events"] = [] migrated["economic_events"] = []
var simulation_data: Dictionary = migrated.get("simulation", {}) var simulation_data: Dictionary = migrated.get("simulation", {})
+20 -14
View File
@@ -5,20 +5,23 @@ const SCHEMA_VERSION := 1
var data: Dictionary var data: Dictionary
func _init(record_data: Dictionary = {}) -> void: func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true) data = record_data.duplicate(true)
static func create( static func create(
storage_id: StringName, storage_id: StringName, initial_amounts: Dictionary, capacity: float = 100.0
initial_amounts: Dictionary,
capacity: float = 100.0
) -> StorageStateRecord: ) -> StorageStateRecord:
return StorageStateRecord.new({ return StorageStateRecord.new(
"schema_version": SCHEMA_VERSION, {
"storage_id": String(storage_id), "schema_version": SCHEMA_VERSION,
"amounts": initial_amounts.duplicate(true), "storage_id": String(storage_id),
"capacity": capacity "amounts": initial_amounts.duplicate(true),
}) "capacity": capacity
}
)
static func from_dictionary(record_data: Dictionary) -> StorageStateRecord: static func from_dictionary(record_data: Dictionary) -> StorageStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION: if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
@@ -34,34 +37,36 @@ static func from_dictionary(record_data: Dictionary) -> StorageStateRecord:
normalized["capacity"] = float(record_data["capacity"]) normalized["capacity"] = float(record_data["capacity"])
var normalized_amounts := {} var normalized_amounts := {}
for item_id in record_data["amounts"]: for item_id in record_data["amounts"]:
normalized_amounts[String(item_id)] = float( normalized_amounts[String(item_id)] = float(record_data["amounts"][item_id])
record_data["amounts"][item_id]
)
normalized["amounts"] = normalized_amounts normalized["amounts"] = normalized_amounts
return StorageStateRecord.new(normalized) return StorageStateRecord.new(normalized)
func get_storage_id() -> StringName: func get_storage_id() -> StringName:
return StringName(data["storage_id"]) return StringName(data["storage_id"])
func get_amount(item_id: StringName) -> float: func get_amount(item_id: StringName) -> float:
return float(data["amounts"].get(String(item_id), 0.0)) return float(data["amounts"].get(String(item_id), 0.0))
func get_total_amount() -> float: func get_total_amount() -> float:
var total := 0.0 var total := 0.0
for amount in data["amounts"].values(): for amount in data["amounts"].values():
total += float(amount) total += float(amount)
return total return total
func deposit(item_id: StringName, requested_amount: float) -> float: func deposit(item_id: StringName, requested_amount: float) -> float:
var accepted := minf( var accepted := minf(
maxf(requested_amount, 0.0), maxf(requested_amount, 0.0), maxf(float(data["capacity"]) - get_total_amount(), 0.0)
maxf(float(data["capacity"]) - get_total_amount(), 0.0)
) )
if accepted <= 0.0: if accepted <= 0.0:
return 0.0 return 0.0
data["amounts"][String(item_id)] = get_amount(item_id) + accepted data["amounts"][String(item_id)] = get_amount(item_id) + accepted
return accepted return accepted
func withdraw(item_id: StringName, requested_amount: float) -> float: func withdraw(item_id: StringName, requested_amount: float) -> float:
var removed := minf(maxf(requested_amount, 0.0), get_amount(item_id)) var removed := minf(maxf(requested_amount, 0.0), get_amount(item_id))
if removed <= 0.0: if removed <= 0.0:
@@ -69,5 +74,6 @@ func withdraw(item_id: StringName, requested_amount: float) -> float:
data["amounts"][String(item_id)] = get_amount(item_id) - removed data["amounts"][String(item_id)] = get_amount(item_id) - removed
return removed return removed
func to_dictionary() -> Dictionary: func to_dictionary() -> Dictionary:
return data.duplicate(true) return data.duplicate(true)
+14 -7
View File
@@ -5,17 +5,22 @@ const SCHEMA_VERSION := 1
var data: Dictionary var data: Dictionary
func _init(record_data: Dictionary = {}) -> void: func _init(record_data: Dictionary = {}) -> void:
data = record_data.duplicate(true) data = record_data.duplicate(true)
static func capture(village: SimVillage) -> VillageStateRecord: static func capture(village: SimVillage) -> VillageStateRecord:
return VillageStateRecord.new({ return VillageStateRecord.new(
"schema_version": SCHEMA_VERSION, {
"food": village.food, "schema_version": SCHEMA_VERSION,
"wood": village.wood, "food": village.food,
"safety": village.safety, "wood": village.wood,
"knowledge": village.knowledge "safety": village.safety,
}) "knowledge": village.knowledge
}
)
static func from_dictionary(record_data: Dictionary) -> VillageStateRecord: static func from_dictionary(record_data: Dictionary) -> VillageStateRecord:
if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION: if int(record_data.get("schema_version", -1)) != SCHEMA_VERSION:
@@ -24,6 +29,7 @@ static func from_dictionary(record_data: Dictionary) -> VillageStateRecord:
return null return null
return VillageStateRecord.new(record_data) return VillageStateRecord.new(record_data)
func restore(debug_logs: bool) -> SimVillage: func restore(debug_logs: bool) -> SimVillage:
var village := SimVillage.new() var village := SimVillage.new()
village.debug_logs = debug_logs village.debug_logs = debug_logs
@@ -35,5 +41,6 @@ func restore(debug_logs: bool) -> SimVillage:
village.update_priorities() village.update_priorities()
return village return village
func to_dictionary() -> Dictionary: func to_dictionary() -> Dictionary:
return data.duplicate(true) return data.duplicate(true)
+28 -29
View File
@@ -1,5 +1,6 @@
extends SceneTree extends SceneTree
class FakeActiveWorld: class FakeActiveWorld:
extends Node extends Node
@@ -12,12 +13,15 @@ class FakeActiveWorld:
func get_activity_target(_action_id: StringName) -> Dictionary: func get_activity_target(_action_id: StringName) -> Dictionary:
return {"target_id": "", "position": activity_position} return {"target_id": "", "position": activity_position}
var failures: Array[String] = [] var failures: Array[String] = []
var travel_requests: Array[Vector3] = [] var travel_requests: Array[Vector3] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
_test_selection_and_execution_are_separate() _test_selection_and_execution_are_separate()
_test_target_resolution_and_travel_are_separate() _test_target_resolution_and_travel_are_separate()
@@ -31,16 +35,11 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _test_selection_and_execution_are_separate() -> void: func _test_selection_and_execution_are_separate() -> void:
var village := SimVillage.new() var village := SimVillage.new()
village.debug_logs = false village.debug_logs = false
var npc := SimNPC.new( var npc := SimNPC.new(700, "BoundaryWorker", SimulationIds.PROFESSION_FARMER, 5.0, 5.0)
700,
"BoundaryWorker",
SimulationIds.PROFESSION_FARMER,
5.0,
5.0
)
npc.hunger = 80.0 npc.hunger = 80.0
var executor := ActionExecutionSystem.new() var executor := ActionExecutionSystem.new()
executor.advance_npc(npc, village) executor.advance_npc(npc, village)
@@ -55,12 +54,12 @@ func _test_selection_and_execution_are_separate() -> void:
"Selection should independently choose food retrieval" "Selection should independently choose food retrieval"
) )
func _test_target_resolution_and_travel_are_separate() -> void: func _test_target_resolution_and_travel_are_separate() -> void:
var adapter := FakeActiveWorld.new() var adapter := FakeActiveWorld.new()
adapter.resource_candidates = [{ adapter.resource_candidates = [
"target_id": "boundary_bush", {"target_id": "boundary_bush", "position": Vector3(3.0, 0.0, 2.0)}
"position": Vector3(3.0, 0.0, 2.0) ]
}]
root.add_child(adapter) root.add_child(adapter)
var manager: Node = load("res://simulation/SimulationManager.gd").new() var manager: Node = load("res://simulation/SimulationManager.gd").new()
@@ -70,18 +69,20 @@ func _test_target_resolution_and_travel_are_separate() -> void:
manager.set_process(false) manager.set_process(false)
manager.npc_travel_requested.connect(_on_travel_requested) manager.npc_travel_requested.connect(_on_travel_requested)
var resource_state := ResourceStateRecord.from_dictionary({ var resource_state := ResourceStateRecord.from_dictionary(
"schema_version": ResourceStateRecord.SCHEMA_VERSION, {
"node_id": "boundary_bush", "schema_version": ResourceStateRecord.SCHEMA_VERSION,
"action_id": String(SimulationIds.ACTION_GATHER_FOOD), "node_id": "boundary_bush",
"resource_id": String(SimulationIds.RESOURCE_FOOD), "action_id": String(SimulationIds.ACTION_GATHER_FOOD),
"amount_remaining": 5.0, "resource_id": String(SimulationIds.RESOURCE_FOOD),
"yield_per_action": 1.0, "amount_remaining": 5.0,
"reserved_by": -1, "yield_per_action": 1.0,
"enabled": true, "reserved_by": -1,
"can_npcs_use": true, "enabled": true,
"can_player_use": true "can_npcs_use": true,
}) "can_player_use": true
}
)
manager.resource_states[resource_state.get_node_id()] = resource_state manager.resource_states[resource_state.get_node_id()] = resource_state
var npc: SimNPC = manager.npcs[0] var npc: SimNPC = manager.npcs[0]
@@ -91,8 +92,7 @@ func _test_target_resolution_and_travel_are_separate() -> void:
"Target resolver should resolve a resource from adapter facts" "Target resolver should resolve a resource from adapter facts"
) )
_check( _check(
npc.target_id == &"boundary_bush" npc.target_id == &"boundary_bush" and resource_state.get_reserved_by() == npc.id,
and resource_state.get_reserved_by() == npc.id,
"Target resolver should authoritatively assign and reserve the target" "Target resolver should authoritatively assign and reserve the target"
) )
_check( _check(
@@ -113,12 +113,11 @@ func _test_target_resolution_and_travel_are_separate() -> void:
manager.free() manager.free()
adapter.free() adapter.free()
func _on_travel_requested(
_npc: SimNPC, func _on_travel_requested(_npc: SimNPC, target_position: Vector3) -> void:
target_position: Vector3
) -> void:
travel_requests.append(target_position) travel_requests.append(target_position)
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+4
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var clock := SimulationClock.new(1.0) var clock := SimulationClock.new(1.0)
_check(clock.advance(0.4) == 0, "Clock should not tick before its interval") _check(clock.advance(0.4) == 0, "Clock should not tick before its interval")
@@ -28,6 +30,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _run_scenario(seed_value: int) -> String: func _run_scenario(seed_value: int) -> String:
var manager: Node = load("res://simulation/SimulationManager.gd").new() var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.simulation_seed = seed_value manager.simulation_seed = seed_value
@@ -47,6 +50,7 @@ func _run_scenario(seed_value: int) -> String:
manager.free() manager.free()
return checksum return checksum
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+45 -53
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var manager: Node = load("res://simulation/SimulationManager.gd").new() var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.debug_logs = false manager.debug_logs = false
@@ -14,18 +16,20 @@ func _run() -> void:
var pantry: StorageStateRecord = manager.get_pantry() var pantry: StorageStateRecord = manager.get_pantry()
pantry.withdraw(SimulationIds.RESOURCE_FOOD, 1000.0) pantry.withdraw(SimulationIds.RESOURCE_FOOD, 1000.0)
manager._sync_village_food() manager._sync_village_food()
var resource := ResourceStateRecord.from_dictionary({ var resource := ResourceStateRecord.from_dictionary(
"schema_version": ResourceStateRecord.SCHEMA_VERSION, {
"node_id": "food_loop_bush", "schema_version": ResourceStateRecord.SCHEMA_VERSION,
"action_id": String(SimulationIds.ACTION_GATHER_FOOD), "node_id": "food_loop_bush",
"resource_id": String(SimulationIds.RESOURCE_FOOD), "action_id": String(SimulationIds.ACTION_GATHER_FOOD),
"amount_remaining": 10.0, "resource_id": String(SimulationIds.RESOURCE_FOOD),
"yield_per_action": 2.0, "amount_remaining": 10.0,
"reserved_by": -1, "yield_per_action": 2.0,
"enabled": true, "reserved_by": -1,
"can_npcs_use": true, "enabled": true,
"can_player_use": true "can_npcs_use": true,
}) "can_player_use": true
}
)
manager.resource_states[resource.get_node_id()] = resource manager.resource_states[resource.get_node_id()] = resource
var npc: SimNPC = manager.npcs[0] var npc: SimNPC = manager.npcs[0]
@@ -38,10 +42,9 @@ func _run() -> void:
manager.simulate_tick() manager.simulate_tick()
_check( _check(
is_equal_approx(resource.get_amount_remaining(), 8.0) (
and is_equal_approx( is_equal_approx(resource.get_amount_remaining(), 8.0)
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), and is_equal_approx(npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 2.0)
2.0
), ),
"Gathering should move food from the source into NPC inventory" "Gathering should move food from the source into NPC inventory"
) )
@@ -58,10 +61,9 @@ func _run() -> void:
manager.notify_npc_arrived(npc.id) manager.notify_npc_arrived(npc.id)
manager.simulate_tick() manager.simulate_tick()
_check( _check(
is_equal_approx(pantry.get_amount(SimulationIds.RESOURCE_FOOD), 2.0) (
and is_equal_approx( is_equal_approx(pantry.get_amount(SimulationIds.RESOURCE_FOOD), 2.0)
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), and is_equal_approx(npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 0.0)
0.0
), ),
"Deposit should transfer carried food into the pantry" "Deposit should transfer carried food into the pantry"
) )
@@ -75,28 +77,23 @@ func _run() -> void:
manager.notify_npc_arrived(npc.id) manager.notify_npc_arrived(npc.id)
manager.simulate_tick() manager.simulate_tick()
_check( _check(
is_equal_approx(pantry.get_amount(SimulationIds.RESOURCE_FOOD), 1.0) (
and is_equal_approx( is_equal_approx(pantry.get_amount(SimulationIds.RESOURCE_FOOD), 1.0)
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), and is_equal_approx(npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 1.0)
1.0
), ),
"Withdraw should transfer pantry food into NPC inventory" "Withdraw should transfer pantry food into NPC inventory"
) )
manager.simulate_tick() manager.simulate_tick()
_check( _check(npc.current_task == SimulationIds.ACTION_EAT, "NPC carrying food should select eat")
npc.current_task == SimulationIds.ACTION_EAT,
"NPC carrying food should select eat"
)
manager.notify_npc_arrived(npc.id) manager.notify_npc_arrived(npc.id)
var hunger_before_eating := npc.hunger var hunger_before_eating := npc.hunger
manager.simulate_tick() manager.simulate_tick()
manager.simulate_tick() manager.simulate_tick()
_check( _check(
npc.hunger < hunger_before_eating (
and is_equal_approx( npc.hunger < hunger_before_eating
npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), and is_equal_approx(npc.get_inventory_amount(SimulationIds.RESOURCE_FOOD), 0.0)
0.0
), ),
"Eating should consume carried food and reduce hunger" "Eating should consume carried food and reduce hunger"
) )
@@ -121,8 +118,10 @@ func _run() -> void:
"Economic events should restore with the simulation" "Economic events should restore with the simulation"
) )
_check( _check(
restored.economic_events.size() == manager.economic_events.size() (
and restored.next_event_id == manager.next_event_id, restored.economic_events.size() == manager.economic_events.size()
and restored.next_event_id == manager.next_event_id
),
"Economic event history and its next ID should round-trip" "Economic event history and its next ID should round-trip"
) )
@@ -134,10 +133,12 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
func _check_economic_event_chain(manager: Node) -> void: func _check_economic_event_chain(manager: Node) -> void:
var expected_types: Array[StringName] = [ var expected_types: Array[StringName] = [
SimulationIds.EVENT_RESOURCE_EXTRACTED, SimulationIds.EVENT_RESOURCE_EXTRACTED,
@@ -154,28 +155,19 @@ func _check_economic_event_chain(manager: Node) -> void:
for index in expected_types.size(): for index in expected_types.size():
var event: EconomicEventRecord = manager.economic_events[index] var event: EconomicEventRecord = manager.economic_events[index]
_check( _check(
int(event.data["event_id"]) == index (
and StringName(event.data["event_type"]) == expected_types[index] int(event.data["event_id"]) == index
and StringName(event.data["item_id"]) and StringName(event.data["event_type"]) == expected_types[index]
== SimulationIds.RESOURCE_FOOD, and StringName(event.data["item_id"]) == SimulationIds.RESOURCE_FOOD
),
"Economic event order, identity, and item should be deterministic" "Economic event order, identity, and item should be deterministic"
) )
_check( _check(
is_equal_approx( (
float(manager.economic_events[0].data["amount"]), is_equal_approx(float(manager.economic_events[0].data["amount"]), 2.0)
2.0 and is_equal_approx(float(manager.economic_events[1].data["amount"]), 2.0)
) and is_equal_approx(float(manager.economic_events[2].data["amount"]), 1.0)
and is_equal_approx( and is_equal_approx(float(manager.economic_events[3].data["amount"]), 1.0)
float(manager.economic_events[1].data["amount"]),
2.0
)
and is_equal_approx(
float(manager.economic_events[2].data["amount"]),
1.0
)
and is_equal_approx(
float(manager.economic_events[3].data["amount"]),
1.0
), ),
"Economic events should record exact transferred quantities" "Economic events should record exact transferred quantities"
) )
+16 -38
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var main_scene: Node = load("res://main.tscn").instantiate() var main_scene: Node = load("res://main.tscn").instantiate()
root.add_child(main_scene) root.add_child(main_scene)
@@ -19,26 +21,21 @@ func _run() -> void:
main_scene.has_node("JajceWorld/TerrainRoot/Terrain3D"), main_scene.has_node("JajceWorld/TerrainRoot/Terrain3D"),
"Playable runtime should instance the Jajce Terrain3D world" "Playable runtime should instance the Jajce Terrain3D world"
) )
var terrain: Terrain3D = main_scene.get_node( var terrain: Terrain3D = main_scene.get_node("JajceWorld/TerrainRoot/Terrain3D")
"JajceWorld/TerrainRoot/Terrain3D"
)
_check( _check(
terrain.get_camera() == main_scene.get_node("CameraRig/Camera3D"), terrain.get_camera() == main_scene.get_node("CameraRig/Camera3D"),
"Runtime Terrain3D should bind to the gameplay camera" "Runtime Terrain3D should bind to the gameplay camera"
) )
_check( _check(
not main_scene.has_node("World") (
and not main_scene.has_node("ResourceNodes") not main_scene.has_node("World")
and not main_scene.has_node("ActivityMarkers"), and not main_scene.has_node("ResourceNodes")
and not main_scene.has_node("ActivityMarkers")
),
"Runtime should not retain duplicate flat-world objects" "Runtime should not retain duplicate flat-world objects"
) )
var resource_root := main_scene.get_node( var resource_root := main_scene.get_node("JajceWorld/WorldObjects/ResourceNodes")
"JajceWorld/WorldObjects/ResourceNodes" _check(resource_root.get_child_count() == 4, "Runtime should contain four Jajce ResourceNodes")
)
_check(
resource_root.get_child_count() == 4,
"Runtime should contain four Jajce ResourceNodes"
)
_check( _check(
simulation_manager.resource_states.size() == 4, simulation_manager.resource_states.size() == 4,
"Simulation authority should bind all four Jajce resources" "Simulation authority should bind all four Jajce resources"
@@ -48,11 +45,7 @@ func _run() -> void:
await _wait_for_navigation_map(navigation_map) await _wait_for_navigation_map(navigation_map)
NavigationServer3D.map_force_update(navigation_map) NavigationServer3D.map_force_update(navigation_map)
var fixed_origins := [ var fixed_origins := [Vector3(-6.0, 0.0, -4.0), Vector3(0.0, 0.0, 0.0), Vector3(6.0, 0.0, 4.0)]
Vector3(-6.0, 0.0, -4.0),
Vector3(0.0, 0.0, 0.0),
Vector3(6.0, 0.0, 4.0)
]
var destinations: Array[Vector3] = [] var destinations: Array[Vector3] = []
for marker_path in [ for marker_path in [
"JajceWorld/LegacyActivityMarkers/PantryMarker", "JajceWorld/LegacyActivityMarkers/PantryMarker",
@@ -67,25 +60,14 @@ func _run() -> void:
for origin in fixed_origins: for origin in fixed_origins:
for destination in destinations: for destination in destinations:
var path := NavigationServer3D.map_get_path( var path := NavigationServer3D.map_get_path(navigation_map, origin, destination, true)
navigation_map,
origin,
destination,
true
)
_check( _check(
not path.is_empty(), not path.is_empty(),
"Navigation path should exist from %s to %s" % [origin, destination] "Navigation path should exist from %s to %s" % [origin, destination]
) )
var village := SimVillage.new() var village := SimVillage.new()
var worker := SimNPC.new( var worker := SimNPC.new(100, "BaselineWorker", SimulationIds.PROFESSION_SCHOLAR, 5.0, 5.0)
100,
"BaselineWorker",
SimulationIds.PROFESSION_SCHOLAR,
5.0,
5.0
)
worker.set_task(SimulationIds.ACTION_STUDY, 2.0) worker.set_task(SimulationIds.ACTION_STUDY, 2.0)
worker.start_working() worker.start_working()
var executor := ActionExecutionSystem.new() var executor := ActionExecutionSystem.new()
@@ -93,13 +75,7 @@ func _run() -> void:
executor.advance_npc(worker, village) executor.advance_npc(worker, village)
_check(worker.task_complete, "A two-tick working task should complete") _check(worker.task_complete, "A two-tick working task should complete")
var starving := SimNPC.new( var starving := SimNPC.new(101, "BaselineStarving", SimulationIds.PROFESSION_WANDERER, 5.0, 5.0)
101,
"BaselineStarving",
SimulationIds.PROFESSION_WANDERER,
5.0,
5.0
)
starving.hunger = 100.0 starving.hunger = 100.0
starving.starvation_death_threshold = 1 starving.starvation_death_threshold = 1
executor.advance_npc(starving, village) executor.advance_npc(starving, village)
@@ -114,6 +90,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _wait_for_navigation_map(navigation_map: RID) -> void: func _wait_for_navigation_map(navigation_map: RID) -> void:
for attempt in 30: for attempt in 30:
if ( if (
@@ -125,6 +102,7 @@ func _wait_for_navigation_map(navigation_map: RID) -> void:
await physics_frame await physics_frame
_check(false, "Navigation map did not synchronize within 30 physics frames") _check(false, "Navigation map did not synchronize within 30 physics frames")
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+20 -33
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var camera := Camera3D.new() var camera := Camera3D.new()
camera.current = true camera.current = true
@@ -24,8 +26,10 @@ func _run() -> void:
) )
_check( _check(
terrain.assets.get_texture_count() >= 3, terrain.assets.get_texture_count() >= 3,
"Jajce terrain should expose at least three materials (found %s)" (
% terrain.assets.get_texture_count() "Jajce terrain should expose at least three materials (found %s)"
% terrain.assets.get_texture_count()
)
) )
_check( _check(
absf(terrain.data.get_height(Vector3.ZERO)) < 0.1, absf(terrain.data.get_height(Vector3.ZERO)) < 0.1,
@@ -42,32 +46,22 @@ func _run() -> void:
world.get_node("FoliageRoot").get_child_count() >= 10, world.get_node("FoliageRoot").get_child_count() >= 10,
"JajceWorld should include restrained authored foliage clusters" "JajceWorld should include restrained authored foliage clusters"
) )
_check( _check(world.has_node("FortressBlockout"), "JajceWorld should include the fortress landmark")
world.has_node("FortressBlockout"), _check(world.has_node("WaterRoot/WaterfallBody"), "WaterRoot should include a waterfall drop")
"JajceWorld should include the fortress landmark"
)
_check(
world.has_node("WaterRoot/WaterfallBody"),
"WaterRoot should include a waterfall drop"
)
_check( _check(
world.has_node("WaterRoot/WaterfallMist"), world.has_node("WaterRoot/WaterfallMist"),
"WaterRoot should include waterfall mist particles" "WaterRoot should include waterfall mist particles"
) )
_check( _check(world.has_node("WaterRoot/RiverSurface"), "WaterRoot should include a river surface")
world.has_node("WaterRoot/RiverSurface"),
"WaterRoot should include a river surface"
)
var environment: Environment = ( var environment: Environment = (
world.get_node("WorldEnvironment") as WorldEnvironment (world.get_node("WorldEnvironment") as WorldEnvironment).environment
).environment )
_check( _check(
environment.fog_enabled and environment.sky != null, environment.fog_enabled and environment.sky != null,
"Jajce environment should provide sky and restrained valley fog" "Jajce environment should provide sky and restrained valley fog"
) )
_check( _check(
world.has_node("VillageRoot/Mill") world.has_node("VillageRoot/Mill") and world.has_node("VillageRoot/Bridge"),
and world.has_node("VillageRoot/Bridge"),
"VillageRoot should include the mill and bridge blockouts" "VillageRoot should include the mill and bridge blockouts"
) )
var smoke_count := 0 var smoke_count := 0
@@ -103,11 +97,7 @@ func _run() -> void:
await _wait_for_navigation_map(navigation_map) await _wait_for_navigation_map(navigation_map)
NavigationServer3D.map_force_update(navigation_map) NavigationServer3D.map_force_update(navigation_map)
var origins := [ var origins := [Vector3(-6, 0, -4), Vector3(0, 0, 0), Vector3(6, 0, 4)]
Vector3(-6, 0, -4),
Vector3(0, 0, 0),
Vector3(6, 0, 4)
]
var destinations: Array[Vector3] = [] var destinations: Array[Vector3] = []
for resource in resources: for resource in resources:
destinations.append((resource as ResourceNode).interaction_point.global_position) destinations.append((resource as ResourceNode).interaction_point.global_position)
@@ -116,12 +106,7 @@ func _run() -> void:
for origin in origins: for origin in origins:
for destination in destinations: for destination in destinations:
var path := NavigationServer3D.map_get_path( var path := NavigationServer3D.map_get_path(navigation_map, origin, destination, true)
navigation_map,
origin,
destination,
true
)
_check( _check(
not path.is_empty(), not path.is_empty(),
"Jajce navigation path should exist from %s to %s" % [origin, destination] "Jajce navigation path should exist from %s to %s" % [origin, destination]
@@ -143,9 +128,7 @@ func _run() -> void:
) )
var selected := ResourceNode.get_by_id(npc.target_id) var selected := ResourceNode.get_by_id(npc.target_id)
if selected != null: if selected != null:
var selected_state: ResourceStateRecord = manager.get_resource_state( var selected_state: ResourceStateRecord = manager.get_resource_state(selected.node_id)
selected.node_id
)
_check( _check(
selected_state.get_reserved_by() == npc.id, selected_state.get_reserved_by() == npc.id,
"Selected Jajce resource should be authoritatively reserved" "Selected Jajce resource should be authoritatively reserved"
@@ -153,7 +136,9 @@ func _run() -> void:
manager.release_resource(selected.node_id, npc.id) manager.release_resource(selected.node_id, npc.id)
if failures.is_empty(): if failures.is_empty():
print("[TEST] Jajce scaffold passed: Terrain3D, 6 textures, shader water, building blockouts, chimney smoke") print(
"[TEST] Jajce scaffold passed: Terrain3D, 6 textures, shader water, building blockouts, chimney smoke"
)
quit(0) quit(0)
return return
@@ -161,6 +146,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _wait_for_navigation_map(navigation_map: RID) -> void: func _wait_for_navigation_map(navigation_map: RID) -> void:
for attempt in 30: for attempt in 30:
if ( if (
@@ -172,6 +158,7 @@ func _wait_for_navigation_map(navigation_map: RID) -> void:
await physics_frame await physics_frame
_check(false, "Jajce navigation map did not synchronize within 30 physics frames") _check(false, "Jajce navigation map did not synchronize within 30 physics frames")
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+12 -22
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var main_scene: Node = load("res://main.tscn").instantiate() var main_scene: Node = load("res://main.tscn").instantiate()
root.add_child(main_scene) root.add_child(main_scene)
@@ -21,25 +23,13 @@ func _run() -> void:
var carried_food: MeshInstance3D = visual.get_node("CarriedFood") var carried_food: MeshInstance3D = visual.get_node("CarriedFood")
_check(not carried_food.visible, "NPC should begin without a carried-food prop") _check(not carried_food.visible, "NPC should begin without a carried-food prop")
npc.add_inventory(SimulationIds.RESOURCE_FOOD, 1.0) npc.add_inventory(SimulationIds.RESOURCE_FOOD, 1.0)
manager.emit_signal( manager.emit_signal("npc_inventory_changed", npc, SimulationIds.RESOURCE_FOOD, 1.0)
"npc_inventory_changed",
npc,
SimulationIds.RESOURCE_FOOD,
1.0
)
_check(carried_food.visible, "Carried food should be visible on the NPC") _check(carried_food.visible, "Carried food should be visible on the NPC")
npc.remove_inventory(SimulationIds.RESOURCE_FOOD, 1.0) npc.remove_inventory(SimulationIds.RESOURCE_FOOD, 1.0)
manager.emit_signal( manager.emit_signal("npc_inventory_changed", npc, SimulationIds.RESOURCE_FOOD, 0.0)
"npc_inventory_changed",
npc,
SimulationIds.RESOURCE_FOOD,
0.0
)
_check(not carried_food.visible, "Deposited or eaten food should be hidden") _check(not carried_food.visible, "Deposited or eaten food should be hidden")
var resource_state: ResourceStateRecord = manager.get_resource_state( var resource_state: ResourceStateRecord = manager.get_resource_state(&"berry_bush_01")
&"berry_bush_01"
)
npc.set_task(SimulationIds.ACTION_GATHER_FOOD) npc.set_task(SimulationIds.ACTION_GATHER_FOOD)
npc.target_id = resource_state.get_node_id() npc.target_id = resource_state.get_node_id()
npc.travel_target_position = Vector3(-6.0, 0.0, -8.0) npc.travel_target_position = Vector3(-6.0, 0.0, -8.0)
@@ -60,9 +50,11 @@ func _run() -> void:
"Unload should synchronize the last active position" "Unload should synchronize the last active position"
) )
_check( _check(
npc.current_task == saved_action (
and npc.target_id == saved_target_id npc.current_task == saved_action
and npc.travel_target_position.is_equal_approx(saved_target_position), and npc.target_id == saved_target_id
and npc.travel_target_position.is_equal_approx(saved_target_position)
),
"Unload should preserve action and target state" "Unload should preserve action and target state"
) )
_check( _check(
@@ -87,10 +79,7 @@ func _run() -> void:
manager.get_state_checksum() == unloaded_checksum, manager.get_state_checksum() == unloaded_checksum,
"Loading a visual must not mutate simulation state" "Loading a visual must not mutate simulation state"
) )
_check( _check(resource_state.get_reserved_by() == npc.id, "Reload should preserve the reservation")
resource_state.get_reserved_by() == npc.id,
"Reload should preserve the reservation"
)
var moved_position := authoritative_position + Vector3(0.5, 0.0, 0.25) var moved_position := authoritative_position + Vector3(0.5, 0.0, 0.25)
reloaded.emit_signal("position_changed", npc.id, moved_position) reloaded.emit_signal("position_changed", npc.id, moved_position)
@@ -124,6 +113,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+17 -20
View File
@@ -3,9 +3,11 @@ extends SceneTree
var main_scene: Node var main_scene: Node
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
main_scene = load("res://main.tscn").instantiate() main_scene = load("res://main.tscn").instantiate()
root.add_child(main_scene) root.add_child(main_scene)
@@ -14,20 +16,14 @@ func _run() -> void:
var simulation_manager := main_scene.get_node("SimulationManager") var simulation_manager := main_scene.get_node("SimulationManager")
var player := main_scene.get_node("Player") var player := main_scene.get_node("Player")
var bush := main_scene.get_node( var bush := (
"JajceWorld/WorldObjects/ResourceNodes/BerryBush_01" main_scene.get_node("JajceWorld/WorldObjects/ResourceNodes/BerryBush_01") as ResourceNode
) as ResourceNode )
var tree := main_scene.get_node( var tree := main_scene.get_node("JajceWorld/WorldObjects/ResourceNodes/Tree_01") as ResourceNode
"JajceWorld/WorldObjects/ResourceNodes/Tree_01"
) as ResourceNode
simulation_manager.set_process(false) simulation_manager.set_process(false)
var bush_state: ResourceStateRecord = simulation_manager.get_resource_state( var bush_state: ResourceStateRecord = simulation_manager.get_resource_state(bush.node_id)
bush.node_id var tree_state: ResourceStateRecord = simulation_manager.get_resource_state(tree.node_id)
)
var tree_state: ResourceStateRecord = simulation_manager.get_resource_state(
tree.node_id
)
bush_state.set_amount_remaining(1.0) bush_state.set_amount_remaining(1.0)
_check( _check(
simulation_manager.reserve_resource(bush.node_id, 999), simulation_manager.reserve_resource(bush.node_id, 999),
@@ -41,11 +37,11 @@ func _run() -> void:
is_equal_approx(bush_state.get_amount_remaining(), 0.0), is_equal_approx(bush_state.get_amount_remaining(), 0.0),
"Player should deplete the nearby bush" "Player should deplete the nearby bush"
) )
_check(is_equal_approx(simulation_manager.village.food, food_before + 1.0), "Village should receive exactly the bush's remaining food")
_check( _check(
bush_state.get_reserved_by() == -1, is_equal_approx(simulation_manager.village.food, food_before + 1.0),
"Depletion should release the NPC reservation" "Village should receive exactly the bush's remaining food"
) )
_check(bush_state.get_reserved_by() == -1, "Depletion should release the NPC reservation")
player.global_position = tree.interaction_point.global_position player.global_position = tree.interaction_point.global_position
var wood_before: float = simulation_manager.village.wood var wood_before: float = simulation_manager.village.wood
@@ -53,13 +49,13 @@ func _run() -> void:
player.try_interact() player.try_interact()
_check( _check(
is_equal_approx( is_equal_approx(tree_state.get_amount_remaining(), tree_before - tree.yield_per_action),
tree_state.get_amount_remaining(),
tree_before - tree.yield_per_action
),
"Player should extract the tree's configured yield" "Player should extract the tree's configured yield"
) )
_check(is_equal_approx(simulation_manager.village.wood, wood_before + tree.yield_per_action), "Village should receive exactly the extracted wood") _check(
is_equal_approx(simulation_manager.village.wood, wood_before + tree.yield_per_action),
"Village should receive exactly the extracted wood"
)
if failures.is_empty(): if failures.is_empty():
print("[TEST] ResourceNode player parity passed") print("[TEST] ResourceNode player parity passed")
@@ -70,6 +66,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+5 -5
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var test_directory := "user://save_slot_test_%s" % Time.get_ticks_usec() var test_directory := "user://save_slot_test_%s" % Time.get_ticks_usec()
var store := SaveSlotStore.new(test_directory) var store := SaveSlotStore.new(test_directory)
@@ -48,17 +50,14 @@ func _run() -> void:
var unchanged_checksum: String = manager.get_state_checksum() var unchanged_checksum: String = manager.get_state_checksum()
var file := FileAccess.open(final_path, FileAccess.WRITE) var file := FileAccess.open(final_path, FileAccess.WRITE)
file.store_string("{\"schema\":\"broken\"}") file.store_string('{"schema":"broken"}')
file.close() file.close()
_check(not store.load_into(manager), "Invalid save data should be rejected") _check(not store.load_into(manager), "Invalid save data should be rejected")
_check( _check(
manager.get_state_checksum() == unchanged_checksum, manager.get_state_checksum() == unchanged_checksum,
"A rejected save must not partially mutate the simulation" "A rejected save must not partially mutate the simulation"
) )
_check( _check(not store.save(manager, "../escape"), "Slot names must not escape the save directory")
not store.save(manager, "../escape"),
"Slot names must not escape the save directory"
)
store.delete_slot() store.delete_slot()
DirAccess.remove_absolute(ProjectSettings.globalize_path(test_directory)) DirAccess.remove_absolute(ProjectSettings.globalize_path(test_directory))
@@ -70,6 +69,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+18 -33
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
_test_registry_integrity() _test_registry_integrity()
_test_definition_backed_behavior() _test_definition_backed_behavior()
@@ -20,12 +22,10 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _test_registry_integrity() -> void: func _test_registry_integrity() -> void:
var errors := SimulationDefinitions.validate() var errors := SimulationDefinitions.validate()
_check( _check(errors.is_empty(), "Definition registry should validate: %s" % [errors])
errors.is_empty(),
"Definition registry should validate: %s" % [errors]
)
var action_ids: Array[StringName] = [] var action_ids: Array[StringName] = []
for definition in SimulationDefinitions.get_actions(): for definition in SimulationDefinitions.get_actions():
@@ -34,30 +34,19 @@ func _test_registry_integrity() -> void:
SimulationDefinitions.get_action(definition.action_id) == definition, SimulationDefinitions.get_action(definition.action_id) == definition,
"Action lookup should return '%s'" % definition.action_id "Action lookup should return '%s'" % definition.action_id
) )
_check( _check(action_ids.size() == 9, "Registry should contain all nine executable prototype actions")
action_ids.size() == 9,
"Registry should contain all nine executable prototype actions"
)
var profession_ids := SimulationDefinitions.get_profession_ids() var profession_ids := SimulationDefinitions.get_profession_ids()
_check( _check(profession_ids.size() == 5, "Registry should contain all five prototype professions")
profession_ids.size() == 5,
"Registry should contain all five prototype professions"
)
for profession_id in profession_ids: for profession_id in profession_ids:
_check( _check(
SimulationDefinitions.get_profession(profession_id) != null, SimulationDefinitions.get_profession(profession_id) != null,
"Profession lookup should return '%s'" % profession_id "Profession lookup should return '%s'" % profession_id
) )
func _test_definition_backed_behavior() -> void: func _test_definition_backed_behavior() -> void:
var npc := SimNPC.new( var npc := SimNPC.new(500, "DefinitionWorker", SimulationIds.PROFESSION_SCHOLAR, 5.0, 5.0)
500,
"DefinitionWorker",
SimulationIds.PROFESSION_SCHOLAR,
5.0,
5.0
)
npc.set_task(SimulationIds.ACTION_STUDY) npc.set_task(SimulationIds.ACTION_STUDY)
var study := SimulationDefinitions.get_action(SimulationIds.ACTION_STUDY) var study := SimulationDefinitions.get_action(SimulationIds.ACTION_STUDY)
_check( _check(
@@ -68,23 +57,18 @@ func _test_definition_backed_behavior() -> void:
study.preferred_profession_id == SimulationIds.PROFESSION_SCHOLAR, study.preferred_profession_id == SimulationIds.PROFESSION_SCHOLAR,
"Study should reference the stable scholar profession ID" "Study should reference the stable scholar profession ID"
) )
var gather := SimulationDefinitions.get_action( var gather := SimulationDefinitions.get_action(SimulationIds.ACTION_GATHER_FOOD)
SimulationIds.ACTION_GATHER_FOOD
)
_check( _check(
gather.target_type == SimulationIds.TARGET_RESOURCE (
and gather.resource_action_id == SimulationIds.ACTION_GATHER_FOOD, gather.target_type == SimulationIds.TARGET_RESOURCE
and gather.resource_action_id == SimulationIds.ACTION_GATHER_FOOD
),
"Gather-food target metadata should come from its definition" "Gather-food target metadata should come from its definition"
) )
func _test_state_reference_validation() -> void: func _test_state_reference_validation() -> void:
var npc := SimNPC.new( var npc := SimNPC.new(501, "ValidationWorker", SimulationIds.PROFESSION_FARMER, 5.0, 5.0)
501,
"ValidationWorker",
SimulationIds.PROFESSION_FARMER,
5.0,
5.0
)
var valid_data := NPCStateRecord.capture(npc).to_dictionary() var valid_data := NPCStateRecord.capture(npc).to_dictionary()
var invalid_profession := valid_data.duplicate(true) var invalid_profession := valid_data.duplicate(true)
invalid_profession["profession"] = "missing_profession" invalid_profession["profession"] = "missing_profession"
@@ -99,6 +83,7 @@ func _test_state_reference_validation() -> void:
"NPC state should reject an unknown action ID" "NPC state should reject an unknown action ID"
) )
func _test_id_round_trip() -> void: func _test_id_round_trip() -> void:
var manager: Node = load("res://simulation/SimulationManager.gd").new() var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.debug_logs = false manager.debug_logs = false
@@ -116,8 +101,7 @@ func _test_id_round_trip() -> void:
) )
for npc in restored.npcs: for npc in restored.npcs:
_check( _check(
typeof(npc.profession) == TYPE_STRING_NAME, typeof(npc.profession) == TYPE_STRING_NAME, "Profession should restore as StringName"
"Profession should restore as StringName"
) )
_check( _check(
typeof(npc.current_task) == TYPE_STRING_NAME, typeof(npc.current_task) == TYPE_STRING_NAME,
@@ -131,6 +115,7 @@ func _test_id_round_trip() -> void:
manager.free() manager.free()
restored.free() restored.free()
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+39 -48
View File
@@ -2,9 +2,11 @@ extends SceneTree
var failures: Array[String] = [] var failures: Array[String] = []
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
await _test_deterministic_continuation() await _test_deterministic_continuation()
await _test_resource_state_round_trip() await _test_resource_state_round_trip()
@@ -23,6 +25,7 @@ func _run() -> void:
push_error("[TEST] " + failure) push_error("[TEST] " + failure)
quit(1) quit(1)
func _test_deterministic_continuation() -> void: func _test_deterministic_continuation() -> void:
var uninterrupted := _create_manager(91234) var uninterrupted := _create_manager(91234)
_advance_scenario(uninterrupted, 24) _advance_scenario(uninterrupted, 24)
@@ -63,10 +66,9 @@ func _test_deterministic_continuation() -> void:
before_save.free() before_save.free()
restored.free() restored.free()
func _test_resource_state_round_trip() -> void: func _test_resource_state_round_trip() -> void:
var resource: ResourceNode = load( var resource: ResourceNode = load("res://world/resource_nodes/ResourceNode.tscn").instantiate()
"res://world/resource_nodes/ResourceNode.tscn"
).instantiate()
resource.node_id = &"SerializationTestBerry" resource.node_id = &"SerializationTestBerry"
resource.initial_amount = 9.0 resource.initial_amount = 9.0
resource.yield_per_action = 2.0 resource.yield_per_action = 2.0
@@ -78,9 +80,7 @@ func _test_resource_state_round_trip() -> void:
manager.register_resource_node(resource), manager.register_resource_node(resource),
"Resource presentation should bind to simulation authority" "Resource presentation should bind to simulation authority"
) )
var resource_state: ResourceStateRecord = manager.get_resource_state( var resource_state: ResourceStateRecord = manager.get_resource_state(resource.node_id)
resource.node_id
)
_check(resource_state.reserve(42), "Resource should accept the test reservation") _check(resource_state.reserve(42), "Resource should accept the test reservation")
resource_state.extract() resource_state.extract()
var saved_json: String = manager.serialize_state() var saved_json: String = manager.serialize_state()
@@ -103,15 +103,10 @@ func _test_resource_state_round_trip() -> void:
is_equal_approx(resource_state.get_amount_remaining(), 7.0), is_equal_approx(resource_state.get_amount_remaining(), 7.0),
"Resource amount should round-trip" "Resource amount should round-trip"
) )
_check( _check(resource_state.get_reserved_by() == 42, "Resource reservation should round-trip")
resource_state.get_reserved_by() == 42,
"Resource reservation should round-trip"
)
_check(resource_state.is_enabled(), "Resource enabled state should round-trip") _check(resource_state.is_enabled(), "Resource enabled state should round-trip")
var rebound: ResourceNode = load( var rebound: ResourceNode = load("res://world/resource_nodes/ResourceNode.tscn").instantiate()
"res://world/resource_nodes/ResourceNode.tscn"
).instantiate()
rebound.node_id = &"SerializationTestBerry" rebound.node_id = &"SerializationTestBerry"
rebound.initial_amount = 999.0 rebound.initial_amount = 999.0
root.add_child(rebound) root.add_child(rebound)
@@ -128,28 +123,28 @@ func _test_resource_state_round_trip() -> void:
manager.free() manager.free()
rebound.free() rebound.free()
func _test_schema_rejection() -> void: func _test_schema_rejection() -> void:
var unsupported := JSON.stringify({ var unsupported := JSON.stringify(
"schema": SimulationStateRecord.SCHEMA_NAME, {"schema": SimulationStateRecord.SCHEMA_NAME, "schema_version": 999}
"schema_version": 999 )
})
_check( _check(
SimulationStateRecord.from_json(unsupported) == null, SimulationStateRecord.from_json(unsupported) == null,
"Unsupported schema versions should be rejected" "Unsupported schema versions should be rejected"
) )
_check( _check(SimulationStateRecord.from_json("[]") == null, "Non-record JSON should be rejected")
SimulationStateRecord.from_json("[]") == null,
"Non-record JSON should be rejected"
)
func _test_legacy_resource_migration() -> void: func _test_legacy_resource_migration() -> void:
var migrated := ResourceStateRecord.from_dictionary({ var migrated := ResourceStateRecord.from_dictionary(
"schema_version": 1, {
"node_id": "legacy_tree", "schema_version": 1,
"amount_remaining": 6.0, "node_id": "legacy_tree",
"reserved_by": 12, "amount_remaining": 6.0,
"enabled": true "reserved_by": 12,
}) "enabled": true
}
)
_check(migrated != null, "ResourceStateRecord v1 should migrate explicitly") _check(migrated != null, "ResourceStateRecord v1 should migrate explicitly")
if migrated == null: if migrated == null:
return return
@@ -158,19 +153,13 @@ func _test_legacy_resource_migration() -> void:
"Migrated resource state should use the current nested schema" "Migrated resource state should use the current nested schema"
) )
_check( _check(
is_equal_approx(migrated.get_amount_remaining(), 6.0) is_equal_approx(migrated.get_amount_remaining(), 6.0) and migrated.get_reserved_by() == 12,
and migrated.get_reserved_by() == 12,
"Resource migration should preserve mutable authority" "Resource migration should preserve mutable authority"
) )
func _test_legacy_npc_migration() -> void: func _test_legacy_npc_migration() -> void:
var npc := SimNPC.new( var npc := SimNPC.new(88, "LegacyNPC", SimulationIds.PROFESSION_FARMER, 5.0, 5.0)
88,
"LegacyNPC",
SimulationIds.PROFESSION_FARMER,
5.0,
5.0
)
var legacy_data := NPCStateRecord.capture(npc).to_dictionary() var legacy_data := NPCStateRecord.capture(npc).to_dictionary()
legacy_data["schema_version"] = NPCStateRecord.LEGACY_SCHEMA_VERSION legacy_data["schema_version"] = NPCStateRecord.LEGACY_SCHEMA_VERSION
legacy_data.erase("travel_target_position") legacy_data.erase("travel_target_position")
@@ -184,6 +173,7 @@ func _test_legacy_npc_migration() -> void:
"Legacy NPC migration should not invent an active travel target" "Legacy NPC migration should not invent an active travel target"
) )
func _test_legacy_world_storage_migration() -> void: func _test_legacy_world_storage_migration() -> void:
var manager := _create_manager(55) var manager := _create_manager(55)
var legacy_data: Dictionary = manager.create_state_record().to_dictionary() var legacy_data: Dictionary = manager.create_state_record().to_dictionary()
@@ -193,35 +183,34 @@ func _test_legacy_world_storage_migration() -> void:
_check(migrated != null, "World schema v1 should migrate pantry storage") _check(migrated != null, "World schema v1 should migrate pantry storage")
if migrated != null: if migrated != null:
_check( _check(
migrated.storages.size() == 1 (
and is_equal_approx( migrated.storages.size() == 1
migrated.storages[0].get_amount( and is_equal_approx(
SimulationIds.RESOURCE_FOOD migrated.storages[0].get_amount(SimulationIds.RESOURCE_FOOD),
), manager.village.food
manager.village.food )
), ),
"World migration should preserve legacy village food in pantry" "World migration should preserve legacy village food in pantry"
) )
manager.free() manager.free()
func _test_previous_world_event_migration() -> void: func _test_previous_world_event_migration() -> void:
var manager := _create_manager(56) var manager := _create_manager(56)
var previous_data: Dictionary = manager.create_state_record().to_dictionary() var previous_data: Dictionary = manager.create_state_record().to_dictionary()
previous_data["schema_version"] = ( previous_data["schema_version"] = (SimulationStateRecord.PREVIOUS_SCHEMA_VERSION)
SimulationStateRecord.PREVIOUS_SCHEMA_VERSION
)
previous_data.erase("economic_events") previous_data.erase("economic_events")
previous_data["simulation"].erase("next_event_id") previous_data["simulation"].erase("next_event_id")
var migrated := SimulationStateRecord.from_dictionary(previous_data) var migrated := SimulationStateRecord.from_dictionary(previous_data)
_check(migrated != null, "World schema v2 should add an empty event stream") _check(migrated != null, "World schema v2 should add an empty event stream")
if migrated != null: if migrated != null:
_check( _check(
migrated.economic_events.is_empty() migrated.economic_events.is_empty() and int(migrated.simulation["next_event_id"]) == 0,
and int(migrated.simulation["next_event_id"]) == 0,
"World v2 migration should initialize deterministic event identity" "World v2 migration should initialize deterministic event identity"
) )
manager.free() manager.free()
func _create_manager(seed_value: int) -> Node: func _create_manager(seed_value: int) -> Node:
var manager: Node = load("res://simulation/SimulationManager.gd").new() var manager: Node = load("res://simulation/SimulationManager.gd").new()
manager.simulation_seed = seed_value manager.simulation_seed = seed_value
@@ -230,6 +219,7 @@ func _create_manager(seed_value: int) -> Node:
manager.set_process(false) manager.set_process(false)
return manager return manager
func _advance_scenario(manager: Node, steps: int) -> void: func _advance_scenario(manager: Node, steps: int) -> void:
for step in steps: for step in steps:
manager.simulate_tick() manager.simulate_tick()
@@ -238,6 +228,7 @@ func _advance_scenario(manager: Node, steps: int) -> void:
if npc.task_state == SimNPC.TASK_STATE_TRAVELING: if npc.task_state == SimNPC.TASK_STATE_TRAVELING:
manager.notify_npc_arrived(npc.id) manager.notify_npc_arrived(npc.id)
func _check(condition: bool, message: String) -> void: func _check(condition: bool, message: String) -> void:
if not condition: if not condition:
failures.append(message) failures.append(message)
+18 -66
View File
@@ -6,13 +6,13 @@ const HEIGHT_SCALE := 40.0
const DATA_DIRECTORY := "res://terrain/jajce/data" const DATA_DIRECTORY := "res://terrain/jajce/data"
const TEXTURE_DIRECTORY := "res://terrain/jajce/textures" const TEXTURE_DIRECTORY := "res://terrain/jajce/textures"
func _initialize() -> void: func _initialize() -> void:
call_deferred("_generate") call_deferred("_generate")
func _generate() -> void: func _generate() -> void:
DirAccess.make_dir_recursive_absolute( DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(TEXTURE_DIRECTORY))
ProjectSettings.globalize_path(TEXTURE_DIRECTORY)
)
_generate_texture("grass_albedo.png", Color("#516f36"), Color("#79924c"), 17) _generate_texture("grass_albedo.png", Color("#516f36"), Color("#79924c"), 17)
_generate_texture("soil_albedo.png", Color("#65513b"), Color("#92734d"), 29) _generate_texture("soil_albedo.png", Color("#65513b"), Color("#92734d"), 29)
_generate_texture("rock_albedo.png", Color("#55534f"), Color("#89847b"), 43) _generate_texture("rock_albedo.png", Color("#55534f"), Color("#89847b"), 43)
@@ -22,12 +22,7 @@ func _generate() -> void:
root.add_child(terrain) root.add_child(terrain)
await process_frame await process_frame
var height_map := Image.create_empty( var height_map := Image.create_empty(MAP_SIZE, MAP_SIZE, false, Image.FORMAT_RF)
MAP_SIZE,
MAP_SIZE,
false,
Image.FORMAT_RF
)
var noise := FastNoiseLite.new() var noise := FastNoiseLite.new()
noise.seed = 1943 noise.seed = 1943
noise.frequency = 0.018 noise.frequency = 0.018
@@ -40,79 +35,36 @@ func _generate() -> void:
var world_z := float(image_z - HALF_SIZE) var world_z := float(image_z - HALF_SIZE)
var distance := Vector2(world_x, world_z).length() var distance := Vector2(world_x, world_z).length()
var outside_play_area := smoothstep(20.0, 52.0, distance) var outside_play_area := smoothstep(20.0, 52.0, distance)
var ridge := 28.0 * _elliptical_peak( var ridge := 28.0 * _elliptical_peak(world_x, world_z, -72.0, 18.0, 72.0, 48.0)
world_x, var eastern_hill := 15.0 * _elliptical_peak(world_x, world_z, 105.0, 65.0, 110.0, 90.0)
world_z, var southern_hill := (
-72.0, 11.0 * _elliptical_peak(world_x, world_z, -35.0, -125.0, 130.0, 85.0)
18.0,
72.0,
48.0
)
var eastern_hill := 15.0 * _elliptical_peak(
world_x,
world_z,
105.0,
65.0,
110.0,
90.0
)
var southern_hill := 11.0 * _elliptical_peak(
world_x,
world_z,
-35.0,
-125.0,
130.0,
85.0
)
var river_trench := -5.0 * exp(
-pow((world_x - 25.0) / 18.0, 2.0)
) )
var river_trench := -5.0 * exp(-pow((world_x - 25.0) / 18.0, 2.0))
var broad_slope := maxf(0.0, (-world_x - 30.0) * 0.035) var broad_slope := maxf(0.0, (-world_x - 30.0) * 0.035)
var detail := noise.get_noise_2d(world_x, world_z) * 2.4 var detail := noise.get_noise_2d(world_x, world_z) * 2.4
var height := ( var height := (
ridge (ridge + eastern_hill + southern_hill + river_trench + broad_slope + detail)
+ eastern_hill * outside_play_area
+ southern_hill
+ river_trench
+ broad_slope
+ detail
) * outside_play_area
height_map.set_pixel(
image_x,
image_z,
Color(height / HEIGHT_SCALE, 0.0, 0.0, 1.0)
) )
height_map.set_pixel(image_x, image_z, Color(height / HEIGHT_SCALE, 0.0, 0.0, 1.0))
terrain.data.import_images( terrain.data.import_images(
[height_map, null, null], [height_map, null, null], Vector3(-HALF_SIZE, 0.0, -HALF_SIZE), 0.0, HEIGHT_SCALE
Vector3(-HALF_SIZE, 0.0, -HALF_SIZE),
0.0,
HEIGHT_SCALE
) )
terrain.data.save_directory(DATA_DIRECTORY) terrain.data.save_directory(DATA_DIRECTORY)
print("[TerrainGenerator] Generated bounded Jajce terrain and textures") print("[TerrainGenerator] Generated bounded Jajce terrain and textures")
quit(0) quit(0)
func _elliptical_peak( func _elliptical_peak(
x: float, x: float, z: float, center_x: float, center_z: float, width: float, depth: float
z: float,
center_x: float,
center_z: float,
width: float,
depth: float
) -> float: ) -> float:
return exp( return exp(-(pow((x - center_x) / width, 2.0) + pow((z - center_z) / depth, 2.0)))
-(
pow((x - center_x) / width, 2.0)
+ pow((z - center_z) / depth, 2.0)
)
)
func _generate_texture( func _generate_texture(
file_name: String, file_name: String, dark_color: Color, light_color: Color, seed_value: int
dark_color: Color,
light_color: Color,
seed_value: int
) -> void: ) -> void:
var noise := FastNoiseLite.new() var noise := FastNoiseLite.new()
noise.seed = seed_value noise.seed = seed_value
+4 -10
View File
@@ -4,9 +4,11 @@ const REGION_SIZE := 256
const TERRAIN_SIZE := 512 const TERRAIN_SIZE := 512
const DATA_DIRECTORY := "res://terrain/jajce/data" const DATA_DIRECTORY := "res://terrain/jajce/data"
func _initialize() -> void: func _initialize() -> void:
call_deferred("_run") call_deferred("_run")
func _run() -> void: func _run() -> void:
var camera := Camera3D.new() var camera := Camera3D.new()
camera.current = true camera.current = true
@@ -18,12 +20,7 @@ func _run() -> void:
terrain.set_camera(camera) terrain.set_camera(camera)
await process_frame await process_frame
var height_map := Image.create_empty( var height_map := Image.create_empty(TERRAIN_SIZE, TERRAIN_SIZE, false, Image.FORMAT_RF)
TERRAIN_SIZE,
TERRAIN_SIZE,
false,
Image.FORMAT_RF
)
height_map.fill(Color(0.0, 0.0, 0.0, 1.0)) height_map.fill(Color(0.0, 0.0, 0.0, 1.0))
var images: Array[Image] var images: Array[Image]
@@ -31,10 +28,7 @@ func _run() -> void:
images[Terrain3DRegion.TYPE_HEIGHT] = height_map images[Terrain3DRegion.TYPE_HEIGHT] = height_map
terrain.data.import_images( terrain.data.import_images(
images, images, Vector3(-TERRAIN_SIZE / 2.0, 0.0, -TERRAIN_SIZE / 2.0), 0.0, 1.0
Vector3(-TERRAIN_SIZE / 2.0, 0.0, -TERRAIN_SIZE / 2.0),
0.0,
1.0
) )
terrain.data.save_directory(DATA_DIRECTORY) terrain.data.save_directory(DATA_DIRECTORY)
print("[TOOL] Generated flat Jajce Terrain3D seed data in ", DATA_DIRECTORY) print("[TOOL] Generated flat Jajce Terrain3D seed data in ", DATA_DIRECTORY)
+3
View File
@@ -3,11 +3,13 @@ extends Node
const TEXTURE_DIR := "res://terrain/jajce/textures" const TEXTURE_DIR := "res://terrain/jajce/textures"
func _ready() -> void: func _ready() -> void:
if not Engine.is_editor_hint(): if not Engine.is_editor_hint():
return return
call_deferred("generate") call_deferred("generate")
func generate() -> void: func generate() -> void:
var dir := DirAccess.open(TEXTURE_DIR) var dir := DirAccess.open(TEXTURE_DIR)
if not dir: if not dir:
@@ -19,6 +21,7 @@ func generate() -> void:
print("[TerrainTextureGenerator] Generated 3 terrain textures") print("[TerrainTextureGenerator] Generated 3 terrain textures")
func _generate_texture(file_name: String, dark: Color, light: Color, seed_val: int) -> void: func _generate_texture(file_name: String, dark: Color, light: Color, seed_val: int) -> void:
seed(seed_val) seed(seed_val)
var grid_size := 16 var grid_size := 16
+13 -9
View File
@@ -6,17 +6,18 @@ extends Node
@export var rest_marker: Marker3D @export var rest_marker: Marker3D
@export var pantry_marker: Marker3D @export var pantry_marker: Marker3D
func get_resource_candidates(action_id: StringName) -> Array[Dictionary]: func get_resource_candidates(action_id: StringName) -> Array[Dictionary]:
var candidates: Array[Dictionary] = [] var candidates: Array[Dictionary] = []
for node in ResourceNode.get_all(): for node in ResourceNode.get_all():
if node.action_id != action_id or node.interaction_point == null: if node.action_id != action_id or node.interaction_point == null:
continue continue
candidates.append({ candidates.append(
"target_id": String(node.node_id), {"target_id": String(node.node_id), "position": node.interaction_point.global_position}
"position": node.interaction_point.global_position )
})
return candidates return candidates
func get_activity_target(action_id: StringName) -> Dictionary: func get_activity_target(action_id: StringName) -> Dictionary:
var marker: Marker3D var marker: Marker3D
match action_id: match action_id:
@@ -33,10 +34,13 @@ func get_activity_target(action_id: StringName) -> Dictionary:
if marker == null: if marker == null:
return {} return {}
var target_id := "" var target_id := ""
if action_id in [ if (
SimulationIds.ACTION_EAT, action_id
SimulationIds.ACTION_DEPOSIT_FOOD, in [
SimulationIds.ACTION_WITHDRAW_FOOD SimulationIds.ACTION_EAT,
]: SimulationIds.ACTION_DEPOSIT_FOOD,
SimulationIds.ACTION_WITHDRAW_FOOD
]
):
target_id = String(SimulationIds.STORAGE_VILLAGE_PANTRY) target_id = String(SimulationIds.STORAGE_VILLAGE_PANTRY)
return {"target_id": target_id, "position": marker.global_position} return {"target_id": target_id, "position": marker.global_position}
+1
View File
@@ -17,6 +17,7 @@ const COLORS := {
const WIND_SHADER := preload("res://world/jajce/materials/wind.gdshader") const WIND_SHADER := preload("res://world/jajce/materials/wind.gdshader")
func _ready() -> void: func _ready() -> void:
for child in get_children(): for child in get_children():
child.queue_free() child.queue_free()
+2
View File
@@ -4,9 +4,11 @@ extends Camera3D
@export var orbit_speed := 0.025 @export var orbit_speed := 0.025
@export var animate_orbit := true @export var animate_orbit := true
func _ready() -> void: func _ready() -> void:
look_at(focus_point, Vector3.UP) look_at(focus_point, Vector3.UP)
func _process(delta: float) -> void: func _process(delta: float) -> void:
if not animate_orbit: if not animate_orbit:
return return
+3 -1
View File
@@ -2,9 +2,11 @@ extends Node3D
@onready var terrain: Terrain3D = $TerrainRoot/Terrain3D @onready var terrain: Terrain3D = $TerrainRoot/Terrain3D
func _ready() -> void: func _ready() -> void:
call_deferred("_initialize_world_presentation") call_deferred("_initialize_world_presentation")
func _initialize_world_presentation() -> void: func _initialize_world_presentation() -> void:
var active_camera := get_viewport().get_camera_3d() var active_camera := get_viewport().get_camera_3d()
if active_camera != null: if active_camera != null:
@@ -16,7 +18,7 @@ func _initialize_world_presentation() -> void:
for child in $VillageRoot.get_children(): for child in $VillageRoot.get_children():
groups_to_snap.append(child) groups_to_snap.append(child)
for item in groups_to_snap: for item in groups_to_snap:
var pos := item.global_position var pos: Vector3 = item.global_position
var h: float = terrain.data.get_height(pos) var h: float = terrain.data.get_height(pos)
if not is_nan(h): if not is_nan(h):
item.global_position.y = h item.global_position.y = h
+19 -7
View File
@@ -21,6 +21,7 @@ static var _all: Array[ResourceNode] = []
var state: ResourceStateRecord var state: ResourceStateRecord
func _ready() -> void: func _ready() -> void:
if node_id.is_empty(): if node_id.is_empty():
push_error("ResourceNode at %s has empty node_id" % get_path()) push_error("ResourceNode at %s has empty node_id" % get_path())
@@ -30,8 +31,10 @@ func _ready() -> void:
var existing := get_by_id(node_id) var existing := get_by_id(node_id)
if existing != null: if existing != null:
push_error( push_error(
"Duplicate ResourceNode node_id '%s' at %s; first registered at %s" (
% [node_id, get_path(), existing.get_path()] "Duplicate ResourceNode node_id '%s' at %s; first registered at %s"
% [node_id, get_path(), existing.get_path()]
)
) )
initial_enabled = false initial_enabled = false
_update_presentation() _update_presentation()
@@ -41,6 +44,7 @@ func _ready() -> void:
_try_register_with_simulation() _try_register_with_simulation()
_update_presentation() _update_presentation()
func _exit_tree() -> void: func _exit_tree() -> void:
_all.erase(self) _all.erase(self)
if state != null and state.changed.is_connected(_on_state_changed): if state != null and state.changed.is_connected(_on_state_changed):
@@ -49,6 +53,7 @@ func _exit_tree() -> void:
state.depleted.disconnect(_on_state_depleted) state.depleted.disconnect(_on_state_depleted)
state = null state = null
func bind_state(resource_state: ResourceStateRecord) -> bool: func bind_state(resource_state: ResourceStateRecord) -> bool:
if resource_state == null or resource_state.get_node_id() != node_id: if resource_state == null or resource_state.get_node_id() != node_id:
return false return false
@@ -64,6 +69,7 @@ func bind_state(resource_state: ResourceStateRecord) -> bool:
_update_presentation() _update_presentation()
return true return true
func _try_register_with_simulation() -> void: func _try_register_with_simulation() -> void:
var managers := get_tree().get_nodes_in_group("simulation_manager") var managers := get_tree().get_nodes_in_group("simulation_manager")
if managers.size() != 1: if managers.size() != 1:
@@ -72,21 +78,27 @@ func _try_register_with_simulation() -> void:
if manager.has_method("register_resource_node"): if manager.has_method("register_resource_node"):
manager.register_resource_node(self) manager.register_resource_node(self)
func get_amount_remaining() -> float: func get_amount_remaining() -> float:
return state.get_amount_remaining() if state != null else initial_amount return state.get_amount_remaining() if state != null else initial_amount
func get_reserved_by() -> int: func get_reserved_by() -> int:
return state.get_reserved_by() if state != null else -1 return state.get_reserved_by() if state != null else -1
func is_enabled() -> bool: func is_enabled() -> bool:
return state.is_enabled() if state != null else initial_enabled return state.is_enabled() if state != null else initial_enabled
func is_depleted() -> bool: func is_depleted() -> bool:
return state.is_depleted() if state != null else initial_amount <= 0.0 return state.is_depleted() if state != null else initial_amount <= 0.0
func can_extract() -> bool: func can_extract() -> bool:
return state.can_extract() if state != null else initial_enabled and not is_depleted() return state.can_extract() if state != null else initial_enabled and not is_depleted()
func _on_state_changed(changed_state: ResourceStateRecord) -> void: func _on_state_changed(changed_state: ResourceStateRecord) -> void:
if changed_state != state: if changed_state != state:
return return
@@ -94,21 +106,19 @@ func _on_state_changed(changed_state: ResourceStateRecord) -> void:
reservation_changed.emit(node_id, state.get_reserved_by()) reservation_changed.emit(node_id, state.get_reserved_by())
_update_presentation() _update_presentation()
func _on_state_depleted(depleted_state: ResourceStateRecord) -> void: func _on_state_depleted(depleted_state: ResourceStateRecord) -> void:
if depleted_state == state: if depleted_state == state:
depleted.emit(node_id) depleted.emit(node_id)
func _update_presentation() -> void: func _update_presentation() -> void:
if has_node("Visual"): if has_node("Visual"):
$Visual.visible = not (hide_visual_when_depleted and is_depleted()) $Visual.visible = not (hide_visual_when_depleted and is_depleted())
if not has_node("DebugLabel"): if not has_node("DebugLabel"):
return return
var label := $DebugLabel as Label3D var label := $DebugLabel as Label3D
var text := "%s\n%s %.1f" % [ var text := "%s\n%s %.1f" % [node_id, resource_id, get_amount_remaining()]
node_id,
resource_id,
get_amount_remaining()
]
if get_reserved_by() >= 0: if get_reserved_by() >= 0:
text += "\nheld:%d" % get_reserved_by() text += "\nheld:%d" % get_reserved_by()
if is_depleted(): if is_depleted():
@@ -117,11 +127,13 @@ func _update_presentation() -> void:
text += "\nUNBOUND" text += "\nUNBOUND"
label.text = text label.text = text
static func get_by_id(search_id: StringName) -> ResourceNode: static func get_by_id(search_id: StringName) -> ResourceNode:
for node in _all: for node in _all:
if node.node_id == search_id: if node.node_id == search_id:
return node return node
return null return null
static func get_all() -> Array[ResourceNode]: static func get_all() -> Array[ResourceNode]:
return _all.duplicate() return _all.duplicate()
+16 -11
View File
@@ -6,6 +6,7 @@ const DEBUG_LOGS := false
@onready var village_stats_label: Label = $VillagePanel/MarginContainer/VillageStatsLabel @onready var village_stats_label: Label = $VillagePanel/MarginContainer/VillageStatsLabel
func _ready() -> void: func _ready() -> void:
if simulation_manager == null: if simulation_manager == null:
push_error("VillageUI: simulation_manager missing") push_error("VillageUI: simulation_manager missing")
@@ -19,8 +20,10 @@ func _ready() -> void:
if "village" in simulation_manager: if "village" in simulation_manager:
_on_village_changed(simulation_manager.village) _on_village_changed(simulation_manager.village)
func _on_village_changed(village: SimVillage) -> void: func _on_village_changed(village: SimVillage) -> void:
village_stats_label.text = """ village_stats_label.text = (
"""
Village Village
Food: %s Food: %s
@@ -32,13 +35,15 @@ func _on_village_changed(village: SimVillage) -> void:
Food Mod: %.2f Food Mod: %.2f
Safety Mod: %.2f Safety Mod: %.2f
Knowledge Mod: %.2f Knowledge Mod: %.2f
""" % [ """
round(village.food), % [
round(village.wood), round(village.food),
round(village.safety), round(village.wood),
round(village.knowledge), round(village.safety),
simulation_manager.get_starving_count(), round(village.knowledge),
village.food_modifier, simulation_manager.get_starving_count(),
village.safety_modifier, village.food_modifier,
village.knowledge_modifier village.safety_modifier,
] village.knowledge_modifier
]
)
+27 -44
View File
@@ -8,25 +8,24 @@ const DEBUG_LOGS := true
var active_npc_visuals := {} var active_npc_visuals := {}
func debug_log(message: String) -> void: func debug_log(message: String) -> void:
if DEBUG_LOGS: if DEBUG_LOGS:
print("[WorldViewManager] ", message) print("[WorldViewManager] ", message)
func _ready() -> void: func _ready() -> void:
call_deferred("initialize_world_view") call_deferred("initialize_world_view")
func initialize_world_view() -> void: func initialize_world_view() -> void:
await get_tree().physics_frame await get_tree().physics_frame
if simulation_manager.has_signal("npc_target_requested"): if simulation_manager.has_signal("npc_target_requested"):
simulation_manager.npc_target_requested.connect( simulation_manager.npc_target_requested.connect(_on_npc_target_requested)
_on_npc_target_requested
)
else: else:
push_error("WorldViewManager: missing npc_target_requested signal") push_error("WorldViewManager: missing npc_target_requested signal")
if simulation_manager.has_signal("npc_travel_requested"): if simulation_manager.has_signal("npc_travel_requested"):
simulation_manager.npc_travel_requested.connect( simulation_manager.npc_travel_requested.connect(_on_npc_travel_requested)
_on_npc_travel_requested
)
else: else:
push_error("WorldViewManager: missing npc_travel_requested signal") push_error("WorldViewManager: missing npc_travel_requested signal")
if simulation_manager.has_signal("npc_died"): if simulation_manager.has_signal("npc_died"):
@@ -34,19 +33,16 @@ func initialize_world_view() -> void:
else: else:
push_error("WorldViewManager: SimulationManager has no npc_died signal") push_error("WorldViewManager: SimulationManager has no npc_died signal")
if simulation_manager.has_signal("npc_inventory_changed"): if simulation_manager.has_signal("npc_inventory_changed"):
simulation_manager.npc_inventory_changed.connect( simulation_manager.npc_inventory_changed.connect(_on_npc_inventory_changed)
_on_npc_inventory_changed
)
else: else:
push_error( push_error("WorldViewManager: SimulationManager has no npc_inventory_changed signal")
"WorldViewManager: SimulationManager has no npc_inventory_changed signal"
)
if simulation_manager.has_signal("state_restored"): if simulation_manager.has_signal("state_restored"):
simulation_manager.state_restored.connect(_on_simulation_state_restored) simulation_manager.state_restored.connect(_on_simulation_state_restored)
else: else:
push_error("WorldViewManager: SimulationManager has no state_restored signal") push_error("WorldViewManager: SimulationManager has no state_restored signal")
spawn_initial_npcs() spawn_initial_npcs()
func spawn_initial_npcs() -> void: func spawn_initial_npcs() -> void:
if simulation_manager == null: if simulation_manager == null:
push_error("WorldViewManager: simulation_manager is missing") push_error("WorldViewManager: simulation_manager is missing")
@@ -61,6 +57,7 @@ func spawn_initial_npcs() -> void:
for npc in simulation_manager.npcs: for npc in simulation_manager.npcs:
spawn_npc_visual(npc) spawn_npc_visual(npc)
func spawn_npc_visual(npc: SimNPC) -> void: func spawn_npc_visual(npc: SimNPC) -> void:
if active_npc_visuals.has(npc.id): if active_npc_visuals.has(npc.id):
return return
@@ -87,18 +84,17 @@ func spawn_npc_visual(npc: SimNPC) -> void:
elif npc.task_state == SimNPC.TASK_STATE_TRAVELING: elif npc.task_state == SimNPC.TASK_STATE_TRAVELING:
simulation_manager.request_current_travel(npc.id) simulation_manager.request_current_travel(npc.id)
func despawn_npc_visual(npc_id: int) -> bool: func despawn_npc_visual(npc_id: int) -> bool:
var visual = active_npc_visuals.get(npc_id) as Node3D var visual = active_npc_visuals.get(npc_id) as Node3D
if visual == null: if visual == null:
return false return false
simulation_manager.synchronize_npc_position( simulation_manager.synchronize_npc_position(npc_id, visual.global_position)
npc_id,
visual.global_position
)
active_npc_visuals.erase(npc_id) active_npc_visuals.erase(npc_id)
visual.queue_free() visual.queue_free()
return true return true
func reload_npc_visual(npc_id: int) -> bool: func reload_npc_visual(npc_id: int) -> bool:
if active_npc_visuals.has(npc_id): if active_npc_visuals.has(npc_id):
return false return false
@@ -108,25 +104,22 @@ func reload_npc_visual(npc_id: int) -> bool:
return true return true
return false return false
func _on_npc_target_requested(npc: SimNPC) -> void: func _on_npc_target_requested(npc: SimNPC) -> void:
var visual = active_npc_visuals.get(npc.id) as Node3D var visual = active_npc_visuals.get(npc.id) as Node3D
if visual == null: if visual == null:
return return
if not simulation_manager.resolve_npc_target( if not simulation_manager.resolve_npc_target(npc.id, visual.global_position):
npc.id,
visual.global_position
):
debug_log("Target resolution failed for %s" % npc.npc_name) debug_log("Target resolution failed for %s" % npc.npc_name)
func _on_npc_travel_requested(
npc: SimNPC, func _on_npc_travel_requested(npc: SimNPC, target_position: Vector3) -> void:
target_position: Vector3
) -> void:
var visual = active_npc_visuals.get(npc.id) var visual = active_npc_visuals.get(npc.id)
if visual == null: if visual == null:
return return
visual.set_target_position(target_position) visual.set_target_position(target_position)
func _on_npc_visual_arrived(sim_id: int) -> void: func _on_npc_visual_arrived(sim_id: int) -> void:
debug_log("NPC visual arrived. sim_id=%s" % sim_id) debug_log("NPC visual arrived. sim_id=%s" % sim_id)
if simulation_manager == null: if simulation_manager == null:
@@ -134,12 +127,10 @@ func _on_npc_visual_arrived(sim_id: int) -> void:
return return
var visual = active_npc_visuals.get(sim_id) as Node3D var visual = active_npc_visuals.get(sim_id) as Node3D
if visual != null: if visual != null:
simulation_manager.synchronize_npc_position( simulation_manager.synchronize_npc_position(sim_id, visual.global_position)
sim_id,
visual.global_position
)
simulation_manager.notify_npc_arrived(sim_id) simulation_manager.notify_npc_arrived(sim_id)
func _on_npc_visual_navigation_failed(sim_id: int) -> void: func _on_npc_visual_navigation_failed(sim_id: int) -> void:
debug_log("NPC visual navigation failed. sim_id=%s" % sim_id) debug_log("NPC visual navigation failed. sim_id=%s" % sim_id)
if simulation_manager == null: if simulation_manager == null:
@@ -147,18 +138,14 @@ func _on_npc_visual_navigation_failed(sim_id: int) -> void:
return return
var visual = active_npc_visuals.get(sim_id) as Node3D var visual = active_npc_visuals.get(sim_id) as Node3D
if visual != null: if visual != null:
simulation_manager.synchronize_npc_position( simulation_manager.synchronize_npc_position(sim_id, visual.global_position)
sim_id,
visual.global_position
)
simulation_manager.notify_npc_navigation_failed(sim_id) simulation_manager.notify_npc_navigation_failed(sim_id)
func _on_npc_visual_position_changed(
sim_id: int, func _on_npc_visual_position_changed(sim_id: int, active_position: Vector3) -> void:
active_position: Vector3
) -> void:
simulation_manager.synchronize_npc_position(sim_id, active_position) simulation_manager.synchronize_npc_position(sim_id, active_position)
func _on_npc_died(npc: SimNPC) -> void: func _on_npc_died(npc: SimNPC) -> void:
if not active_npc_visuals.has(npc.id): if not active_npc_visuals.has(npc.id):
return return
@@ -168,11 +155,8 @@ func _on_npc_died(npc: SimNPC) -> void:
else: else:
push_error("WorldViewManager: NPCVisual has no apply_dead_visual_state") push_error("WorldViewManager: NPCVisual has no apply_dead_visual_state")
func _on_npc_inventory_changed(
npc: SimNPC, func _on_npc_inventory_changed(npc: SimNPC, item_id: StringName, amount: float) -> void:
item_id: StringName,
amount: float
) -> void:
if item_id != SimulationIds.RESOURCE_FOOD: if item_id != SimulationIds.RESOURCE_FOOD:
return return
var visual = active_npc_visuals.get(npc.id) var visual = active_npc_visuals.get(npc.id)
@@ -181,9 +165,8 @@ func _on_npc_inventory_changed(
if visual.has_method("set_carried_food_visible"): if visual.has_method("set_carried_food_visible"):
visual.set_carried_food_visible(amount > 0.0) visual.set_carried_food_visible(amount > 0.0)
else: else:
push_error( push_error("WorldViewManager: NPCVisual has no set_carried_food_visible method")
"WorldViewManager: NPCVisual has no set_carried_food_visible method"
)
func _on_simulation_state_restored() -> void: func _on_simulation_state_restored() -> void:
for visual in active_npc_visuals.values(): for visual in active_npc_visuals.values():