feat(packages): ludic.crafting - recipes as an open registry, can / take / refund / make through a pack port, stations through a port, the hold-to-make, a Crafted fact

A recipe is `def CraftRecipes key { out, n, ins: [...], ns: [...], station, minutes, hold,
group, learned }`. The pack is the CraftPack port (count, take, add, room, and `leaves`: what
an item leaves once used, so water gives back its bottle); stations are CraftStations (ready,
start), unbound meaning hands only. craft_make hands a click recipe over or starts a timed one
at its station, refunding when the station refuses; the hold runs from the screen that shows it.
Uses ludic.base only; ten tests with a fake pack and fake stations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 06:37:57 +03:00
parent 7f8a7ea7ed
commit cc9cec922a
9 changed files with 475 additions and 0 deletions

View file

@ -0,0 +1,178 @@
# crafting_test.ludic - a recipe made from a fake pack: the makings, the station's say, room for
# the result, what water leaves behind, a station that refuses, the hold, and the facts
import "ludic.crafting"
import "ludic.base"
program CraftingTest {
numbers float
const WOOD: int = 0
const CLOTH: int = 1
const TORCH: int = 2
const ROPE: int = 3
const WATER: int = 4
const BOTTLE: int = 5
const TEA: int = 6
const HERB: int = 7
const BENCH: int = 1
const POT: int = 2
def CraftRecipes torch { out: TORCH, ins: [WOOD, CLOTH], ns: [1, 1] }
def CraftRecipes rope { out: ROPE, n: 2, ins: [HERB], ns: [4], hold: 2.0 }
def CraftRecipes tea { out: TEA, ins: [HERB, WATER], ns: [2, 1], station: POT, minutes: 8 }
def CraftRecipes chair { out: TORCH, ins: [WOOD], ns: [3], station: BENCH }
def CraftRecipes secret { out: TORCH, ins: [WOOD], ns: [1], learned: false }
var pack: []int = null
var bench_near: bool = true
var pot_takes: bool = true
var pot_got: int = -1
function count(it: int) -> int { return pack[it] }
function take(it: int, n: int) -> bool {
if pack[it] < n { return false }
pack[it] = pack[it] - n
return true
}
function add(it: int, n: int) -> int {
pack[it] = pack[it] + n
return n
}
function room(it: int) -> int {
if it == TORCH { return 3 - pack[it] }
return 99
}
function leaves(it: int) -> int {
if it == WATER { return BOTTLE }
return -1
}
function ready(st: int) -> bool {
if st == BENCH { return bench_near }
return true
}
function start(st: int, recipe: int) -> bool {
if not pot_takes { return false }
pot_got = recipe
return true
}
bind CraftPack { count: fn count, take: fn take, add: fn add, room: fn room, leaves: fn leaves }
bind CraftStations { ready: fn ready, start: fn start }
function fresh() -> void {
pack = new []int
for i in 0 .. 8 { push(pack, 0) }
craft_reset()
}
function facts() -> []Crafted { return q_drain(craft_facts()) }
test "the registry is the game's, in the order written" {
expect_eq(CRAFT_COUNT, 5)
expect_eq(CRAFT_TEA, 2)
expect_eq(craft_find(ROPE), CRAFT_ROPE)
expect_eq(craft_by_key("chair"), CRAFT_CHAIR)
expect_eq(craft_inputs(CRAFT_TEA), 2)
expect_eq(craft_find(99), -1)
}
test "made at once from the makings, and said" {
fresh()
expect(not craft_can(CRAFT_TORCH))
pack[WOOD] = 2
pack[CLOTH] = 1
expect(craft_make(CRAFT_TORCH))
expect_eq(pack[WOOD], 1)
expect_eq(pack[CLOTH], 0)
expect_eq(pack[TORCH], 1)
let fs = facts()
expect_eq(len(fs), 1)
expect_eq(fs[0].item, TORCH)
expect(not fs[0].queued)
}
test "nothing is taken when it cannot be made" {
fresh()
pack[WOOD] = 1
expect(not craft_make(CRAFT_TORCH))
expect_eq(pack[WOOD], 1)
expect_eq(len(facts()), 0)
}
test "the station decides, and so does the room for what it makes" {
fresh()
pack[WOOD] = 9
bench_near = false
expect(craft_has(CRAFT_CHAIR))
expect(not craft_can(CRAFT_CHAIR))
bench_near = true
expect(craft_can(CRAFT_CHAIR))
pack[TORCH] = 3
expect(not craft_can(CRAFT_CHAIR))
}
test "an unlearned recipe is never made" {
fresh()
pack[WOOD] = 5
expect(not craft_can(CRAFT_SECRET))
}
test "a timed recipe goes into its station, and water leaves its bottle" {
fresh()
pack[HERB] = 2
pack[WATER] = 1
expect(craft_make(CRAFT_TEA))
expect_eq(pot_got, CRAFT_TEA)
expect_eq(pack[TEA], 0)
expect_eq(pack[WATER], 0)
expect_eq(pack[BOTTLE], 1)
let fs = facts()
expect(fs[0].queued)
expect_eq(fs[0].station, POT)
}
test "a station that says no gives everything back" {
fresh()
pot_takes = false
pack[HERB] = 2
pack[WATER] = 1
expect(not craft_make(CRAFT_TEA))
expect_eq(pack[HERB], 2)
expect_eq(pack[WATER], 1)
expect_eq(pack[BOTTLE], 0)
expect_eq(len(facts()), 0)
}
test "a hold makes it after its seconds, and not before" {
fresh()
pack[HERB] = 4
expect(craft_holds(CRAFT_ROPE))
expect(not craft_holds(CRAFT_TORCH))
expect(craft_hold_start(CRAFT_ROPE))
expect(not craft_hold_run(1.0))
expect(craft_hold_frac(CRAFT_ROPE) == 0.5)
expect(not craft_hold_run(0.9))
expect(craft_hold_run(0.2))
expect_eq(pack[ROPE], 2)
expect_eq(craft_holding(), -1)
}
test "letting go wastes nothing, and losing the makings stops the hold" {
fresh()
pack[HERB] = 4
craft_hold_start(CRAFT_ROPE)
craft_hold_run(1.5)
craft_hold_stop()
expect_eq(pack[HERB], 4)
craft_hold_start(CRAFT_ROPE)
pack[HERB] = 3
expect(not craft_hold_run(5.0))
expect_eq(craft_holding(), -1)
expect_eq(pack[ROPE], 0)
}
test "a click recipe cannot be held" {
fresh()
pack[WOOD] = 1
pack[CLOTH] = 1
expect(not craft_hold_start(CRAFT_TORCH))
}
}