extends RefCounted # The editor always works in bottom-face coordinates; imported mesh origins stay intact. var lib func _init(library) -> void:lib=library func bottom_of(record: Dictionary) -> Vector3: return record.position+Vector3.UP*float(lib.catalog[record.kind].boundsBlender.min[2]) func record_at(kind: String,bottom: Vector3,quarter: int,id: String) -> Dictionary: var at:=bottom # Surface joins can be at e.g. Y=1.82; half-metre rounding recreates a lip. if kind!="goal_hole":at=Vector3(round(at.x),at.y,round(at.z)) var local_min: float=lib.catalog[kind].boundsBlender.min[2] return {"id":id,"kind":kind,"position":at-Vector3.UP*local_min,"quarter":quarter} func on_face(support: Dictionary,point: Vector3,normal: Vector3,kind: String,quarter: int,neighbors: Array=[]) -> Vector3: var bounds: AABB=lib.record_bounds(support) var size: Vector2i=lib.footprint(kind,quarter) var height: float=lib.catalog[kind].boundsBlender.size[2] var result:=Vector3(round(point.x),bounds.position.y,round(point.z)) var axis:=normal.abs().max_axis_index() if axis==1: result.y=bounds.end.y if normal.y>=0 else bounds.position.y-height elif axis==0: result.x=bounds.end.x+.5 if normal.x>=0 else bounds.position.x+.5-size.x else: result.z=bounds.end.z+.5 if normal.z>=0 else bounds.position.z+.5-size.y if kind!="goal_hole": if axis==0:result.x=ceilf(result.x-.0001) if normal.x>=0 else floorf(result.x+.0001) elif axis==2:result.z=ceilf(result.z-.0001) if normal.z>=0 else floorf(result.z+.0001) # Top/bottom clicks still mean stacking. Only side joins use rolling surfaces. if axis!=1: var nearby: Array=neighbors if not neighbors.is_empty() else [support] result=bottom_of(align_surfaces(record_at(kind,result,quarter,"preview"),nearby,str(support.id))) return result func ports(record: Dictionary) -> Array: # Catalog ports are authored groove-floor positions in runtime coordinates. # Their travel vector is not an outward normal (both ends may share travel). var result: Array=[] var model: Dictionary=lib.catalog[record.kind] var basis:=Basis(Vector3.UP,-int(record.quarter)*PI/2) var center: Vector3=lib.record_center(record) for port in model.get("ports",[]): var local: Vector3=lib.vector(port.surface) var half_x: float=model.boundsBlender.size[0]*.5 var half_z: float=model.boundsBlender.size[1]*.5 var outward:=Vector3(signf(local.x),0,0) if absf(absf(local.x)-half_x)<.001 else Vector3(0,0,signf(local.z)) if absf(absf(local.x)-half_x)>=.001 and absf(absf(local.z)-half_z)>=.001:continue result.append({"surface":center+basis*local,"normal":basis*outward}) return result func boost_mouth(record: Dictionary) -> Dictionary: var bounds: AABB = lib.record_bounds(record) var basis := Basis(Vector3.UP, -int(record.quarter)*PI/2) var normal: Vector3 = basis * lib.vector(lib.catalog[record.kind].arrowDirectionGodot) var surface := bounds.get_center() + normal*.5 surface.y = record.position.y + lib.catalog[record.kind].baseHeight return {"surface":surface, "normal":normal} func connections(candidate: Dictionary,neighbors: Array) -> Array: var matches: Array=[] var is_boost: bool = candidate.kind == "directional_boost" var own_ports: Array = [boost_mouth(candidate)] if is_boost else ports(candidate) for own in own_ports: for other in neighbors: if other.id==candidate.id:continue var targets: Array=ports(other) if not is_boost and other.kind in ["wall","directional_boost","ice","goal_hole"]: # Flat solid tops and the goal rim have no groove. Use the edge at # this lane, not the block bottom or the decorative arrow mesh. var bounds: AABB=lib.record_bounds(other) var p: Vector3=own.surface var n: Vector3=own.normal var axis:=0 if absf(n.x)>.5 else 2 var lateral:=2 if axis==0 else 0 var edge: float=bounds.position[axis] if n[axis]>0 else bounds.end[axis] if absf(p[axis]-edge)<.001 and p[lateral]>bounds.position[lateral]+.001 and p[lateral].001 or own.normal.dot(target.normal)>-.999:continue # Nearby layers only; a distant floor must not capture a grid pick. var reach: float=maxf(lib.catalog[candidate.kind].boundsBlender.size[2],lib.catalog[other.kind].boundsBlender.size[2]) if absf(delta.y)>reach+.001:continue matches.append({"offset":delta.y,"support":str(other.id)}) return matches func align_surfaces(candidate: Dictionary,neighbors: Array,preferred: String="") -> Dictionary: # Shared by workshop face picking and player grid placement/preview. Keep the # chosen type, X/Z and rotation; solve only the translation along world Y. var matches:=connections(candidate,neighbors) if matches.is_empty():return candidate var best: Dictionary={} for match in matches: var count:=0 for other in matches: if absf(other.offset-match.offset)<.001:count+=1 var priority:=1 if match.support==preferred else 0 var better: bool=best.is_empty() if not best.is_empty(): if priority!=best.priority:better=priority>best.priority elif count!=best.count:better=count>best.count elif not is_equal_approx(absf(match.offset),absf(best.offset)):better=absf(match.offset)