code wiki / _hdl_build / nx_companion_gate.nx

nx_companion_gate.nx source

↩ module page · 78 lines · 4044 B

1// nx_companion_gate.nx -- GATE: close the research->affordability LOOP. The permaculture SYNTHESIS produced 2// companion-planting facts (garden:companion:*); this proves they feed back into the garden affordability 3// number. T1 companion read (both orders), T2 deterministic wiring (a 3-crop plot's bonus = sum of companion 4// strengths x 5c), T3 loop closed (planting the garden with companions raises total savings above the 5// crop-by-crop base). license_tier: ORIGINAL 6import "nx_garden.nx" 7import "nx_food_science.nx" 8import "nx_seg_store.nx" 9import "nx_syscalls.nx" 10 11const CG_FOOD: *u8 = "knowledge/store/food-" 12 13func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func g_i(v: i64) -> i64 { 15 let bb: *u8 = sys_mmap(28); var m: i64 = v 16 if m < 0 { sys_write(1, "-" as *u8, 1); m = 0 - m } 17 let t: *u8 = sys_mmap(28); var k: i64 = 0 18 if m == 0 { t[0] = 48 as u8; k = 1 } 19 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 20 var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, bb, k); return 0 21} 22 23func main() -> i64 { 24 g_p("=== nx_companion_gate (close the loop: synthesis -> companion planting -> affordability) ===\n" as *u8) 25 fd_seed(CG_FOOD) 26 gd_seed(CG_FOOD) 27 let sc: i64 = gd_seed_companions(CG_FOOD) 28 g_p("companion facts seeded this run=" as *u8); g_i(sc); g_p("\n" as *u8) 29 30 let world: *i64 = fd_world_open(CG_FOOD) 31 let n: i64 = world[0] 32 let widx: *i64 = world[1] as *i64 33 34 var pass: i64 = 0 35 var tot: i64 = 0 36 37 // T1 companion read (both orders) 38 tot = tot + 1 39 var ok1: i64 = 1 40 if gd_companion(CG_FOOD, "onion" as *u8, "carrot" as *u8) != 6 { ok1 = 0 } 41 if gd_companion(CG_FOOD, "carrot" as *u8, "onion" as *u8) != 6 { ok1 = 0 } // reverse order resolves 42 if gd_companion(CG_FOOD, "carrot" as *u8, "garlic" as *u8) != 0 { ok1 = 0 } // not companions 43 if ok1 == 1 { pass = pass + 1; g_p("PASS T1 companion read (onion+carrot=6 both orders; carrot+garlic=0)\n" as *u8) } else { g_p("FAIL T1\n" as *u8) } 44 45 // T2 deterministic wiring: a plot of onion + carrot + garlic 46 let oi: i64 = fd_index_of(world, "onion" as *u8) 47 let cri: i64 = fd_index_of(world, "carrot" as *u8) 48 let gi: i64 = fd_index_of(world, "garlic" as *u8) 49 let three: *i64 = sys_mmap(8 * 4) as *i64 50 three[0] = oi; three[1] = cri; three[2] = gi 51 let bonus3: i64 = gd_companion_bonus(CG_FOOD, world, three, 3) 52 g_p("onion+carrot+garlic plot companion bonus=" as *u8); g_i(bonus3); g_p("c (onion-carrot 6 + onion-garlic 2)x5\n" as *u8) 53 tot = tot + 1 54 if bonus3 == 40 { pass = pass + 1; g_p("PASS T2 wiring exact: companion bonus = (6+2)x5 = 40c\n" as *u8) } else { g_p("FAIL T2 bonus=" as *u8); g_i(bonus3); g_p("\n" as *u8) } 55 56 // T3 loop closed: the whole growable garden gains a companion bonus over the crop-by-crop base 57 let idxs: *i64 = sys_mmap(8 * 64) as *i64 58 var nidx: i64 = 0 59 var base: i64 = 0 60 var i: i64 = 0 61 while i < n { 62 if gd_growable(CG_FOOD, widx[i] as *u8) == 1 { idxs[nidx] = i; nidx = nidx + 1; base = base + gd_savings(CG_FOOD, widx[i] as *u8) } 63 i = i + 1 64 } 65 let bonus: i64 = gd_companion_bonus(CG_FOOD, world, idxs, nidx) 66 let total: i64 = base + bonus 67 g_p("garden of " as *u8); g_i(nidx); g_p(" growable crops: base savings=" as *u8); g_i(base); g_p("c + companion bonus=" as *u8); g_i(bonus); g_p("c = " as *u8); g_i(total); g_p("c\n" as *u8) 68 tot = tot + 1 69 var ok3: i64 = 1 70 if bonus <= 0 { ok3 = 0 } 71 if total <= base { ok3 = 0 } 72 if ok3 == 1 { pass = pass + 1; g_p("PASS T3 loop CLOSED: the synthesis's companion facts raise the garden's real savings\n" as *u8) } else { g_p("FAIL T3\n" as *u8) } 73 74 g_p("nx_companion_gate pass=" as *u8); g_i(pass); g_p("/" as *u8); g_i(tot) 75 if pass == tot { g_p(" verdict=GREEN (research -> synthesis -> companion planting -> affordability, loop closed)\n" as *u8); return 0 } 76 g_p(" verdict=RED\n" as *u8) 77 return 1 78}