This commit is contained in:
Forrest 2026-10-03 12:14:28 -04:00
commit 276d43613d
1687 changed files with 109059 additions and 0 deletions

View file

@ -0,0 +1,95 @@
extends SceneTree
## Builds res://game/resources/player_animations.res: the KayKit Rig_Medium clips the farmer
## uses, copied out of the shared animation files into one AnimationLibrary so the player
## scene's AnimationPlayer can reference (and the editor can preview) them.
## Tracks for bones the mannequin lacks (handslot.l/.r) are dropped; loop flags are set here
## because every source clip is imported as non-looping. Source assets are not modified.
## Re-run after editing CLIPS (editor closed):
## godot --headless --path . --script res://game/tools/build_player_animations.gd
const MANNEQUIN := "res://assets/kaykit_characters/characters/Mannequin_Medium.glb"
const ANIM_DIR := "res://assets/kaykit_characters/animations/Rig_Medium/"
const OUTPUT := "res://game/resources/player_animations.res"
const SKELETON_PATH := "Rig_Medium/Skeleton3D"
## Clip name (kept as in KayKit) -> [source file, loops].
const CLIPS := {
"Idle_A": ["Rig_Medium_General.glb", true],
"Idle_B": ["Rig_Medium_General.glb", true],
"Interact": ["Rig_Medium_General.glb", false],
"PickUp": ["Rig_Medium_General.glb", false],
"Use_Item": ["Rig_Medium_General.glb", false],
"Walking_A": ["Rig_Medium_MovementBasic.glb", true],
"Running_A": ["Rig_Medium_MovementBasic.glb", true],
"Digging": ["Rig_Medium_Tools.glb", true],
"Chopping": ["Rig_Medium_Tools.glb", true],
"Hammering": ["Rig_Medium_Tools.glb", true],
"Waving": ["Rig_Medium_Simulation.glb", false],
}
func _initialize() -> void:
var ok := _build()
quit(0 if ok else 1)
func _build() -> bool:
var bones := _mannequin_bones()
if bones.is_empty():
return false
var library := AnimationLibrary.new()
var sources := {} # file -> instantiated scene root (freed at the end)
var dropped := 0
var ok := true
for clip: String in CLIPS:
var file: String = CLIPS[clip][0]
if not sources.has(file):
var scene := load(ANIM_DIR + file) as PackedScene
if scene == null:
push_error("missing animation file " + file)
ok = false
continue
sources[file] = scene.instantiate()
var player := (sources[file] as Node).get_node_or_null("AnimationPlayer") as AnimationPlayer
if player == null or not player.has_animation(clip):
push_error("clip %s not found in %s" % [clip, file])
ok = false
continue
var anim := player.get_animation(clip).duplicate(true) as Animation
for t in range(anim.get_track_count() - 1, -1, -1):
var path := anim.track_get_path(t)
var keep := String(path.get_concatenated_names()) == SKELETON_PATH \
and path.get_subname_count() == 1 and bones.has(String(path.get_subname(0)))
if not keep:
anim.remove_track(t)
dropped += 1
anim.loop_mode = Animation.LOOP_LINEAR if CLIPS[clip][1] else Animation.LOOP_NONE
library.add_animation(clip, anim)
for file: String in sources:
(sources[file] as Node).free()
if not ok:
return false
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(OUTPUT.get_base_dir()))
var err := ResourceSaver.save(library, OUTPUT)
if err != OK:
push_error("could not save %s (error %d)" % [OUTPUT, err])
return false
print("saved %s: %d clips, %d foreign tracks dropped" % [OUTPUT, library.get_animation_list().size(), dropped])
return true
func _mannequin_bones() -> Dictionary:
var scene := load(MANNEQUIN) as PackedScene
if scene == null:
push_error("missing mannequin " + MANNEQUIN)
return {}
var root := scene.instantiate()
var skel := root.get_node_or_null(SKELETON_PATH) as Skeleton3D
var bones := {}
if skel == null:
push_error("mannequin skeleton not found at " + SKELETON_PATH)
else:
for b in skel.get_bone_count():
bones[skel.get_bone_name(b)] = true
root.free()
return bones

View file

@ -0,0 +1 @@
uid://dxwma3n3eecow

View file

@ -0,0 +1,268 @@
extends SceneTree
## Gives the ithappy farm prefabs that farm.tscn places their collision, so every instance (and
## every later placement of the same prefab) collides. Each processed prefab gets a "Collision"
## StaticBody3D (layer 1, world) holding one CollisionShape3D fitted to its mesh:
## * ground/, road/ (terrain folders): trimesh of the mesh, so the player walks on the visible
## surface instead of the flat floor under it (tile and curb edges are ramps).
## * plant/, animal/: none. Crops are walk-through; animals become moving entities (Step 8).
## * prop/fence_018 (the open gate) carries a hand-made thin box along its panel, so it is
## skipped like any prefab that already has collision.
## * tree/ trees: cylinder around the trunk, widened to half the reach of foliage hanging below
## 1.5 m; tree/ bushes: cylinder at 80 % of the bush's mean half-width; barrels: cylinder.
## * box_*: the pack's unit cube, placed scaled flat as concrete pads, so its top is inset 5 %
## per side; on a pad that turns the 0.2 m edge into a ramp (a plain box edge blocks the
## capsule), on a cube-shaped crate the sides stay near vertical.
## * everything else: box around the geometry in the first 2 m above its base (the footprint,
## ignoring roof overhangs), spanning the full mesh height.
## Prefabs that already contain a CollisionShape3D are left alone, so hand-tuned shapes survive;
## --force regenerates the "Collision" bodies this tool made (other collision is still kept).
## Re-run after placing new prefab types (editor closed):
## godot --headless --path . --script res://game/tools/build_prop_collision.gd [-- --force]
const SCENE := "res://game/scenes/farm.tscn"
const PREFAB_DIR := "res://assets/farm/ithappy/farm/prefabs/assets/"
const BODY_NAME := "Collision"
## Folders whose prefabs are walked on rather than around.
const WALKABLE := ["ground", "road", "field", "landscape", "mountain"]
const NO_COLLISION := ["plant", "animal"]
## Band above the prefab's base that counts as its footprint (about the player's height).
const FOOTPRINT_HEIGHT := 2.0
const TRUNK_HEIGHT := 0.5
const LOW_FOLIAGE_HEIGHT := 1.5
const LOW_FOLIAGE_SHARE := 0.5
const TRUNK_MARGIN := 0.05
const BUSH_SHARE := 0.8
const SLAB_TOP_INSET := 0.05
func _initialize() -> void:
var ok := _build("--force" in OS.get_cmdline_user_args())
quit(0 if ok else 1)
func _build(force: bool) -> bool:
var farm := load(SCENE) as PackedScene
if farm == null:
push_error("missing " + SCENE)
return false
var state := farm.get_state()
var placed := {}
for i in state.get_node_count():
var instance := state.get_node_instance(i)
if instance != null and instance.resource_path.begins_with(PREFAB_DIR):
placed[instance.resource_path] = true
var paths: Array = placed.keys()
paths.sort()
var ok := true
var added := 0
for path: String in paths:
var result := _process_prefab(path, force)
if result.begins_with("error"):
push_error("%s: %s" % [path, result])
ok = false
elif result.begins_with("added"):
added += 1
print("%-44s %s" % [path.trim_prefix(PREFAB_DIR), result])
print("%d placed prefabs, collision added to %d" % [paths.size(), added])
return ok
func _process_prefab(path: String, force: bool) -> String:
var scene := load(path) as PackedScene
if scene == null:
return "error: not a scene"
var root := scene.instantiate(PackedScene.GEN_EDIT_STATE_MAIN) as Node3D
if root == null:
return "error: root is not a Node3D"
var previous := root.get_node_or_null(BODY_NAME)
if force and previous is StaticBody3D:
root.remove_child(previous)
previous.free()
if not root.find_children("*", "CollisionShape3D", true, false).is_empty():
root.free()
return "kept existing collision"
var kind := _kind(path)
if kind == "none":
root.free()
return "no collision (walk-through)"
var faces := _faces(root)
if faces.is_empty():
root.free()
return "error: no mesh faces"
var shape := _fit(kind, faces)
var description := _describe(shape)
var body := StaticBody3D.new()
body.name = BODY_NAME
root.add_child(body)
body.owner = root
body.add_child(shape)
shape.owner = root
root.scene_file_path = ""
var uid := ResourceLoader.get_resource_uid(path)
var packed := PackedScene.new()
var err := packed.pack(root)
root.free()
if err == OK:
err = ResourceSaver.save(packed, path)
if err == OK:
err = _restore_uids(path, uid)
if err != OK:
return "error %d while saving" % err
return "added " + description
func _kind(path: String) -> String:
var folder := path.get_base_dir().get_file()
var file := path.get_file().get_basename()
if folder in NO_COLLISION:
return "none"
if folder in WALKABLE:
return "trimesh"
if file.begins_with("box_"):
return "slab"
if folder == "tree":
return "bush" if file.begins_with("bush") else "trunk"
if file.begins_with("barrel"):
return "cylinder"
return "box"
func _fit(kind: String, faces: PackedVector3Array) -> CollisionShape3D:
var node := CollisionShape3D.new()
node.name = "CollisionShape3D"
var aabb := AABB(faces[0], Vector3.ZERO)
for v in faces:
aabb = aabb.expand(v)
var base := maxf(aabb.position.y, 0.0)
var mid_y := aabb.get_center().y
match kind:
"trimesh":
var mesh_shape := ConcavePolygonShape3D.new()
mesh_shape.set_faces(faces)
node.shape = mesh_shape
"slab":
var hull := ConvexPolygonShape3D.new()
var inset := aabb.size * SLAB_TOP_INSET
var lo := aabb.position
var hi := aabb.end
hull.points = PackedVector3Array([
Vector3(lo.x, lo.y, lo.z), Vector3(hi.x, lo.y, lo.z),
Vector3(hi.x, lo.y, hi.z), Vector3(lo.x, lo.y, hi.z),
Vector3(lo.x + inset.x, hi.y, lo.z + inset.z), Vector3(hi.x - inset.x, hi.y, lo.z + inset.z),
Vector3(hi.x - inset.x, hi.y, hi.z - inset.z), Vector3(lo.x + inset.x, hi.y, hi.z - inset.z),
])
node.shape = hull
"trunk":
var trunk := _band_points(faces, base, base + TRUNK_HEIGHT)
var center := _rect(trunk).get_center()
var foliage := _band_points(faces, base, base + LOW_FOLIAGE_HEIGHT)
var radius := maxf(_reach(trunk, center) + TRUNK_MARGIN, _reach(foliage, center) * LOW_FOLIAGE_SHARE)
node.shape = _cylinder(radius, aabb.size.y)
node.position = Vector3(center.x, mid_y, center.y)
"bush", "cylinder":
var rect := _rect(_band_points(faces, base, base + FOOTPRINT_HEIGHT))
var radius := maxf(rect.size.x, rect.size.y) * 0.5
if kind == "bush":
radius = (rect.size.x + rect.size.y) * 0.25 * BUSH_SHARE
node.shape = _cylinder(radius, aabb.size.y)
node.position = Vector3(rect.get_center().x, mid_y, rect.get_center().y)
_:
var rect := _rect(_band_points(faces, base, base + FOOTPRINT_HEIGHT))
var box := BoxShape3D.new()
box.size = Vector3(rect.size.x, aabb.size.y, rect.size.y)
node.shape = box
node.position = Vector3(rect.get_center().x, mid_y, rect.get_center().y)
return node
## Every mesh triangle of the prefab, in the prefab root's space.
func _faces(root: Node3D) -> PackedVector3Array:
var faces := PackedVector3Array()
for mesh_instance: MeshInstance3D in root.find_children("*", "MeshInstance3D", true, false):
if mesh_instance.mesh == null:
continue
var to_root := mesh_instance.transform
var parent := mesh_instance.get_parent()
while parent != root:
if parent is Node3D:
to_root = (parent as Node3D).transform * to_root
parent = parent.get_parent()
for v in mesh_instance.mesh.get_faces():
faces.append(to_root * v)
return faces
## XZ corners of the faces clipped to heights lo..hi: triangle corners inside the band plus the
## points where triangle edges cross its two planes.
func _band_points(faces: PackedVector3Array, lo: float, hi: float) -> PackedVector2Array:
var points := PackedVector2Array()
for t in range(0, faces.size(), 3):
for i in 3:
var p := faces[t + i]
var q := faces[t + (i + 1) % 3]
if p.y >= lo and p.y <= hi:
points.append(Vector2(p.x, p.z))
for h: float in [lo, hi]:
if (p.y - h) * (q.y - h) < 0.0:
var cross := p.lerp(q, (h - p.y) / (q.y - p.y))
points.append(Vector2(cross.x, cross.z))
return points
func _rect(points: PackedVector2Array) -> Rect2:
var rect := Rect2(points[0], Vector2.ZERO)
for p in points:
rect = rect.expand(p)
return rect
func _reach(points: PackedVector2Array, center: Vector2) -> float:
var reach := 0.0
for p in points:
reach = maxf(reach, p.distance_to(center))
return reach
func _cylinder(radius: float, height: float) -> CylinderShape3D:
var cylinder := CylinderShape3D.new()
cylinder.radius = radius
cylinder.height = height
return cylinder
## ResourceSaver only writes UIDs when the editor supplies them, so this headless save drops the
## scene's own uid and its ext_resource uids. Put them back, or references by uid (farm.tscn's)
## would fall back to paths and the editor would hand the prefab a new uid.
func _restore_uids(path: String, uid: int) -> Error:
var text := FileAccess.get_file_as_string(path)
if text.is_empty():
return FileAccess.get_open_error()
var lines := text.split("\n")
for i in lines.size():
var line := lines[i]
if line.contains(" uid="):
continue
if line.begins_with("[gd_scene") and uid != ResourceUID.INVALID_ID:
lines[i] = line.trim_suffix("]") + ' uid="%s"]' % ResourceUID.id_to_text(uid)
elif line.begins_with("[ext_resource"):
var ext_uid := ResourceLoader.get_resource_uid(line.get_slice(' path="', 1).get_slice('"', 0))
if ext_uid != ResourceUID.INVALID_ID:
lines[i] = line.replace(' path="', ' uid="%s" path="' % ResourceUID.id_to_text(ext_uid))
var file := FileAccess.open(path, FileAccess.WRITE)
if file == null:
return FileAccess.get_open_error()
file.store_string("\n".join(lines))
return OK
func _describe(node: CollisionShape3D) -> String:
var shape := node.shape
if shape is BoxShape3D:
var s := (shape as BoxShape3D).size
return "box %.2f x %.2f x %.2f" % [s.x, s.y, s.z]
if shape is CylinderShape3D:
var c := shape as CylinderShape3D
return "cylinder r %.2f h %.2f" % [c.radius, c.height]
if shape is ConvexPolygonShape3D:
return "ramped slab"
return "trimesh %d triangles" % ((shape as ConcavePolygonShape3D).get_faces().size() / 3)

View file

@ -0,0 +1 @@
uid://7ry6bn00e5pk

View file

@ -0,0 +1,143 @@
extends SceneTree
## Renders a top-down orthographic picture of the farm (needs a window, not --headless):
## godot --path . --script res://game/tools/render_overview.gd -- --out=/tmp/farm.png
## Options: --scene=res://... (default the farm), --center=x,z (default 0,0), --size=meters
## (default 70), --res=pixels (default 1400), --grid=meters (0 = off, default 10),
## --zones (draw the Zones/ areas and markers on top), --pitch=deg / --yaw=deg (angled
## orthographic view instead of straight down; e.g. --pitch=50 --yaw=45 is the game camera).
var _out := "/tmp/farm_overview.png"
var _scene := "res://game/scenes/farm.tscn"
var _center := Vector2.ZERO
var _size := 70.0
var _res := 1400
var _grid := 10.0
var _zones := false
var _pitch := 90.0
var _yaw := 0.0
func _initialize() -> void:
for a in OS.get_cmdline_user_args():
var arg := String(a)
if arg.begins_with("--out="):
_out = arg.trim_prefix("--out=")
elif arg.begins_with("--scene="):
_scene = arg.trim_prefix("--scene=")
elif arg.begins_with("--center="):
var p := arg.trim_prefix("--center=").split(",")
_center = Vector2(float(p[0]), float(p[1]))
elif arg.begins_with("--size="):
_size = float(arg.trim_prefix("--size="))
elif arg.begins_with("--res="):
_res = int(arg.trim_prefix("--res="))
elif arg.begins_with("--grid="):
_grid = float(arg.trim_prefix("--grid="))
elif arg == "--zones":
_zones = true
elif arg.begins_with("--pitch="):
_pitch = float(arg.trim_prefix("--pitch="))
elif arg.begins_with("--yaw="):
_yaw = float(arg.trim_prefix("--yaw="))
_render.call_deferred()
func _render() -> void:
root.size = Vector2i(_res, _res)
var farm := (load(_scene) as PackedScene).instantiate()
root.add_child(farm)
for node in farm.find_children("*", "CanvasLayer", true, false):
(node as CanvasLayer).visible = false
var cam := Camera3D.new()
cam.projection = Camera3D.PROJECTION_ORTHOGONAL
cam.size = _size
cam.far = 400.0
root.add_child(cam)
var basis := Basis.from_euler(Vector3(deg_to_rad(-_pitch), deg_to_rad(_yaw), 0.0))
cam.global_transform = Transform3D(basis, Vector3(_center.x, 0.0, _center.y) + basis.z * 150.0)
cam.make_current()
if _grid > 0.0:
_draw_grid()
if _zones:
_draw_zones(farm)
for i in 8:
await process_frame
await RenderingServer.frame_post_draw
var err := root.get_texture().get_image().save_png(_out)
print("overview %s: %s" % [_out, error_string(err)])
quit()
func _draw_grid() -> void:
var mesh := ImmediateMesh.new()
mesh.surface_begin(Mesh.PRIMITIVE_LINES)
var half := _size * 0.5
var lo := Vector2(_center.x - half, _center.y - half)
var hi := Vector2(_center.x + half, _center.y + half)
var x := ceilf(lo.x / _grid) * _grid
while x <= hi.x:
mesh.surface_add_vertex(Vector3(x, 8.0, lo.y))
mesh.surface_add_vertex(Vector3(x, 8.0, hi.y))
_label(Vector3(x + 0.4, 8.1, lo.y + 1.2), "x%d" % int(x))
x += _grid
var z := ceilf(lo.y / _grid) * _grid
while z <= hi.y:
mesh.surface_add_vertex(Vector3(lo.x, 8.0, z))
mesh.surface_add_vertex(Vector3(hi.x, 8.0, z))
_label(Vector3(lo.x + 0.6, 8.1, z - 0.4), "z%d" % int(z))
z += _grid
mesh.surface_end()
_add_lines(mesh, Color(1, 1, 1, 0.6))
func _draw_zones(farm: Node) -> void:
var zones := farm.get_node_or_null("Zones")
if zones == null:
return
for node in zones.find_children("*", "Node3D", true, false):
var n := node as Node3D
if n is FarmZone:
var zone := n as FarmZone
var rect := zone.rect()
var mesh := ImmediateMesh.new()
mesh.surface_begin(Mesh.PRIMITIVE_LINES)
var c := [Vector2(rect.position.x, rect.position.y), Vector2(rect.end.x, rect.position.y),
Vector2(rect.end.x, rect.end.y), Vector2(rect.position.x, rect.end.y)]
for i in 4:
var a: Vector2 = c[i]
var b: Vector2 = c[(i + 1) % 4]
mesh.surface_add_vertex(Vector3(a.x, 9.0, a.y))
mesh.surface_add_vertex(Vector3(b.x, 9.0, b.y))
mesh.surface_end()
_add_lines(mesh, zone.tint)
_label(Vector3(rect.position.x + 0.5, 9.1, rect.position.y + 1.5), zone.display_name, zone.tint)
elif n is Marker3D:
_label(n.global_position + Vector3(0, 9.0, 0), "◆ " + n.name, Color(1, 0.3, 0.2))
func _add_lines(mesh: ImmediateMesh, color: Color) -> void:
var mat := StandardMaterial3D.new()
mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
mat.albedo_color = color
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
var mi := MeshInstance3D.new()
mi.mesh = mesh
mi.material_override = mat
mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
root.add_child(mi)
func _label(pos: Vector3, text: String, color: Color = Color.WHITE) -> void:
var l := Label3D.new()
l.text = text
l.modulate = color
l.outline_modulate = Color.BLACK
l.outline_size = 8
l.font_size = 48
l.pixel_size = _size / float(_res) * 0.5
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
l.rotation = Vector3(-PI / 2.0, 0.0, 0.0)
l.no_depth_test = true
l.shaded = false
root.add_child(l)
l.global_position = pos

View file

@ -0,0 +1 @@
uid://dkayyvaspu2g