ludic/packages/ludic.crafting/tests/crafting_test.ludic
2026-09-25 14:02:21 +03:00

180 lines
7.5 KiB
Text

# 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 }
state CraftingtestState {
pack: []int = null
bench_near: bool = true
pot_takes: bool = true
pot_got: int = -1
}
function count(craftingtest_st: CraftingtestState, it: int) -> int { return craftingtest_st.pack[it] }
function take(craftingtest_st: mut CraftingtestState, it: int, n: int) -> bool {
if craftingtest_st.pack[it] < n { return false }
craftingtest_st.pack[it] = craftingtest_st.pack[it] - n
return true
}
function add(craftingtest_st: mut CraftingtestState, it: int, n: int) -> int {
craftingtest_st.pack[it] = craftingtest_st.pack[it] + n
return n
}
function room(craftingtest_st: CraftingtestState, it: int) -> int {
if it == TORCH { return 3 - craftingtest_st.pack[it] }
return 99
}
function leaves(it: int) -> int {
if it == WATER { return BOTTLE }
return -1
}
function ready(craftingtest_st: CraftingtestState, st: int) -> bool {
if st == BENCH { return craftingtest_st.bench_near }
return true
}
function start(craftingtest_st: mut CraftingtestState, st: int, recipe: int) -> bool {
if not craftingtest_st.pot_takes { return false }
craftingtest_st.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(base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) -> void {
craftingtest_st.pack = new []int
for i in 0 .. 8 { push(craftingtest_st.pack, 0) }
craft_reset(base_st, crafting_st)
}
function facts(base_st: mut BaseState, crafting_st: mut CraftingState) -> []Crafted { return q_drain(base_st, craft_facts(base_st, crafting_st)) }
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" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
expect(not craft_can(CRAFT_TORCH))
craftingtest_st.pack[WOOD] = 2
craftingtest_st.pack[CLOTH] = 1
expect(craft_make(base_st, crafting_st, CRAFT_TORCH))
expect_eq(craftingtest_st.pack[WOOD], 1)
expect_eq(craftingtest_st.pack[CLOTH], 0)
expect_eq(craftingtest_st.pack[TORCH], 1)
let fs = facts(base_st, crafting_st)
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" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[WOOD] = 1
expect(not craft_make(base_st, crafting_st, CRAFT_TORCH))
expect_eq(craftingtest_st.pack[WOOD], 1)
expect_eq(len(facts(base_st, crafting_st)), 0)
}
test "the station decides, and so does the room for what it makes" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[WOOD] = 9
craftingtest_st.bench_near = false
expect(craft_has(CRAFT_CHAIR))
expect(not craft_can(CRAFT_CHAIR))
craftingtest_st.bench_near = true
expect(craft_can(CRAFT_CHAIR))
craftingtest_st.pack[TORCH] = 3
expect(not craft_can(CRAFT_CHAIR))
}
test "an unlearned recipe is never made" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[WOOD] = 5
expect(not craft_can(CRAFT_SECRET))
}
test "a timed recipe goes into its station, and water leaves its bottle" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[HERB] = 2
craftingtest_st.pack[WATER] = 1
expect(craft_make(base_st, crafting_st, CRAFT_TEA))
expect_eq(craftingtest_st.pot_got, CRAFT_TEA)
expect_eq(craftingtest_st.pack[TEA], 0)
expect_eq(craftingtest_st.pack[WATER], 0)
expect_eq(craftingtest_st.pack[BOTTLE], 1)
let fs = facts(base_st, crafting_st)
expect(fs[0].queued)
expect_eq(fs[0].station, POT)
}
test "a station that says no gives everything back" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pot_takes = false
craftingtest_st.pack[HERB] = 2
craftingtest_st.pack[WATER] = 1
expect(not craft_make(base_st, crafting_st, CRAFT_TEA))
expect_eq(craftingtest_st.pack[HERB], 2)
expect_eq(craftingtest_st.pack[WATER], 1)
expect_eq(craftingtest_st.pack[BOTTLE], 0)
expect_eq(len(facts(base_st, crafting_st)), 0)
}
test "a hold makes it after its seconds, and not before" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[HERB] = 4
expect(craft_holds(CRAFT_ROPE))
expect(not craft_holds(CRAFT_TORCH))
expect(craft_hold_start(crafting_st, CRAFT_ROPE))
expect(not craft_hold_run(base_st, crafting_st, 1.0))
expect(craft_hold_frac(crafting_st, CRAFT_ROPE) == 0.5)
expect(not craft_hold_run(base_st, crafting_st, 0.9))
expect(craft_hold_run(base_st, crafting_st, 0.2))
expect_eq(craftingtest_st.pack[ROPE], 2)
expect_eq(craft_holding(crafting_st), -1)
}
test "letting go wastes nothing, and losing the makings stops the hold" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[HERB] = 4
craft_hold_start(crafting_st, CRAFT_ROPE)
craft_hold_run(base_st, crafting_st, 1.5)
craft_hold_stop(crafting_st)
expect_eq(craftingtest_st.pack[HERB], 4)
craft_hold_start(crafting_st, CRAFT_ROPE)
craftingtest_st.pack[HERB] = 3
expect(not craft_hold_run(base_st, crafting_st, 5.0))
expect_eq(craft_holding(crafting_st), -1)
expect_eq(craftingtest_st.pack[ROPE], 0)
}
test "a click recipe cannot be held" (base_st: mut BaseState, crafting_st: mut CraftingState, craftingtest_st: mut CraftingtestState) {
fresh(base_st, crafting_st, craftingtest_st)
craftingtest_st.pack[WOOD] = 1
craftingtest_st.pack[CLOTH] = 1
expect(not craft_hold_start(crafting_st, CRAFT_TORCH))
}
}