ludic/selfhost/backend/stdlib/emit_ivec.ludic
Orkuncakilkaya ad548840c7
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 32s
ci / build-and-test (push) Successful in 2m49s
commit-lint / conventional-commits (push) Successful in 5s
docs / build-and-deploy (push) Successful in 30s
feat(engine): #90 atlas-aware Sprite component, #91 become from listeners, 0.3.x ergonomics batch
Closes the two open issues and lands the pending unreleased batch:

- #90: `Sprite { atlas: 1 }` routes esys_sprite through atlas_draw_ex
  (scale/flip/tint), so cell / cell_span / strip ids of any size draw
  through the engine sprite-render system. examples/library/sprite_atlas
  is the pixel-readback regression.
- #91: `become` from an @On(Event) listener / global handler / plain
  function no longer segfaults the compiler; it emits @L_scene_leave()
  (a dispatch on the live scene id) so the leaving scene's on-exit runs.
  UI_* handles are readable from any code (widget table built on first
  use). examples/library/scene_menus covers it.
- fix: a windowed `ludicc -o` build that reaches the audio runtime only
  through the atlas/Assets preload import now links audio.ll +
  AVFoundation (the audio backend link was gated on a game-level
  Audio.* call, so any windowed game declaring Sprite failed to link).
- the hand-written "Unreleased" CHANGELOG section is converted to
  changesets under changes/ so `x release` generates it.
- plus the batch: engine-driven retained UI + UiClicked event, Overlay
  phase, TileSkin tilemap-render system, Key.* constants, Font/Ui/File
  namespaces, Sprite.strip, prefabs, managers, countdown fields,
  enum-typed machines, layer @Queries, ludic.prefs / ludic.dungeon
  packages, Ai.seek pathing, Solids.solid2, cursor confine (mode 3)
  fix, shooter centre-aim fix, reserved-word function diagnostic.

Verified: x test (124/124), x test-tools, check-impl, check-vocabulary,
check-docs, docs-gen + docs-check, bootstrap-cfree (seed is a fixpoint).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-04 01:36:08 +03:00

116 lines
6.2 KiB
Text

# emit_ivec.ludic — the IVec2.* namespace: an integer 2D vector value type. An
# IVec2 is a pair of i32 components (x, y) packed into one i64 — x in the high
# 32 bits, y in the low 32 — exactly like Vector (emit_vector.ludic) but with
# whole-integer components and integer arithmetic. It is the natural type for
# tile / grid coordinates, cell offsets, and integer sizes, where a fractional
# part is meaningless and rounding would be a bug. Being one i64, it is a true
# by-value type (assignment copies, no heap) and lives in a single register.
# Reuses vec_pack / vec_x / vec_y from emit_vector.ludic; llty maps `IVec2` to
# i64 (see emit_core.ludic). Every operation is exact integer arithmetic, so it
# is deterministic on every platform.
# |d| for a signed i32 code -> i32 code (branchless: select on d < 0)
function ivec_abs(d: pointer) -> pointer {
let neg = emit_bind(`sub i32 0, {d}`)
let lt = emit_bind(`icmp slt i32 {d}, 0`)
return emit_bind(`select i1 {lt}, i32 {neg}, i32 {d}`)
}
function is_ivec_ns(meth: pointer) -> bool {
if (meth == "make") or (meth == "zero") or (meth == "x") or (meth == "y") { return true }
if (meth == "add") or (meth == "sub") or (meth == "scale") or (meth == "dot") { return true }
if (meth == "equal") or (meth == "manhattan") or (meth == "to_vector") { return true }
if (meth == "distance2") or (meth == "within") or (meth == "heading") or (meth == "along") or (meth == "step") { return true }
return false
}
function emit_ivec_ns(meth: pointer, e: Node) -> Val {
if (meth == "zero") { # the origin, (0, 0)
return val("0", "IVec2")
}
if (meth == "make") { # make(x, y: int) -> IVec2
let x = emit_expr(e.kids[0]); let y = emit_expr(e.kids[1])
return val(vec_pack(x.code, y.code), "IVec2")
}
if (meth == "x") { # the x component -> int
let v = emit_expr(e.kids[0])
return val(vec_x(v.code), "int")
}
if (meth == "y") { # the y component -> int
let v = emit_expr(e.kids[0])
return val(vec_y(v.code), "int")
}
if (meth == "distance2") or (meth == "within") { # squared distance; within(a, b, radius)
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let dx = emit_bind(`sub i32 {vec_x(b.code)}, {vec_x(a.code)}`)
let dy = emit_bind(`sub i32 {vec_y(b.code)}, {vec_y(a.code)}`)
let xx = emit_bind(`mul i32 {dx}, {dx}`)
let yy = emit_bind(`mul i32 {dy}, {dy}`)
let d2 = emit_bind(`add i32 {xx}, {yy}`)
if (meth == "distance2") { return val(d2, "int") }
let r = emit_expr(e.kids[2])
let rr = emit_bind(`mul i32 {r.code}, {r.code}`)
let c = emit_bind(`icmp sle i32 {d2}, {rr}`)
return val(emit_bind(`zext i1 {c} to i32`), "bool")
}
if (meth == "heading") { # degrees from a to b (0 = +x, 90 = +y)
if find_fn("rt_ivec_heading") == null { perr("IVec2.heading needs the engine runtime") }
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
return val(emit_bind(`call i32 @fn_rt_ivec_heading(i64 {a.code}, i64 {b.code})`), "int")
}
if (meth == "along") { # the point `distance` along `degrees` from origin
if find_fn("rt_ivec_along") == null { perr("IVec2.along needs the engine runtime") }
let o = emit_expr(e.kids[0]); let d = emit_expr(e.kids[1]); let n = emit_expr(e.kids[2])
return val(emit_bind(`call i64 @fn_rt_ivec_along(i64 {o.code}, i32 {d.code}, i32 {n.code})`), "IVec2")
}
if (meth == "step") { # a -1/0/1 unit step along `degrees`
if find_fn("rt_ivec_step") == null { perr("IVec2.step needs the engine runtime") }
let d = emit_expr(e.kids[0])
return val(emit_bind(`call i64 @fn_rt_ivec_step(i32 {d.code})`), "IVec2")
}
if (meth == "add") { # component-wise a + b
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let sx = emit_bind(`add i32 {ax}, {bx}`)
let sy = emit_bind(`add i32 {ay}, {by}`)
return val(vec_pack(sx, sy), "IVec2")
}
if (meth == "sub") { # component-wise a - b
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let sx = emit_bind(`sub i32 {ax}, {bx}`)
let sy = emit_bind(`sub i32 {ay}, {by}`)
return val(vec_pack(sx, sy), "IVec2")
}
if (meth == "scale") { # v * s (s: int)
let v = emit_expr(e.kids[0]); let s = emit_expr(e.kids[1])
let vx = vec_x(v.code); let vy = vec_y(v.code)
let sx = emit_bind(`mul i32 {vx}, {s.code}`)
let sy = emit_bind(`mul i32 {vy}, {s.code}`)
return val(vec_pack(sx, sy), "IVec2")
}
if (meth == "dot") { # ax*bx + ay*by -> int
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let px = emit_bind(`mul i32 {ax}, {bx}`); let py = emit_bind(`mul i32 {ay}, {by}`)
return val(emit_bind(`add i32 {px}, {py}`), "int")
}
if (meth == "equal") { # a == b (both components) -> bool
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let c = emit_bind(`icmp eq i64 {a.code}, {b.code}`)
return val(emit_bind(`zext i1 {c} to i32`), "bool")
}
if (meth == "manhattan") { # |dx| + |dy| -> int (grid distance)
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let dx = emit_bind(`sub i32 {ax}, {bx}`); let dy = emit_bind(`sub i32 {ay}, {by}`)
let adx = ivec_abs(dx); let ady = ivec_abs(dy)
return val(emit_bind(`add i32 {adx}, {ady}`), "int")
}
# to_vector(v) -> Vector — widen each integer component to a Q16.16 fixed
let v = emit_expr(e.kids[0])
let vx = vec_x(v.code); let vy = vec_y(v.code)
let fx = emit_bind(`shl i32 {vx}, 16`)
let fy = emit_bind(`shl i32 {vy}, 16`)
return val(vec_pack(fx, fy), "Vector")
}