code wiki / _hdl_build / nx_story_vm.nx
nx_story_vm.nx source
↩ module page · 34 lines · 1715 B
1// nx_story_vm.nx -- S-R1: the sovereign DATA-DRIVEN STORY-GRAPH VM (P4 story pillar foundation).
2// A story is DATA, not control flow: nodes with up to SV_NCH choices; each choice has a target node,
3// an optional REQUIRED flag (gate the branch) and an optional SET flag (effect). State = current node +
4// an i64 flag bitmask. This is the engine for dialogue trees, quests, and branching narrative; later
5// rungs slot LLM-authored text into nodes (P5). Pure integer logic, 100% sovereign. license_tier: ORIGINAL
6import "nx_syscalls.nx"
7
8const SV_NCH: i64 = 4 // max choices per node
9
10// Take `choice` from `cur`. Returns the new node id, or -1 if the choice does not exist OR is BLOCKED
11// (its required flag is not set). Applies the choice's SET-flag effect on success. flags is *i64 (one cell).
12func sv_choose(ch_target: *i64, ch_req: *i64, ch_set: *i64, cur: i64, choice: i64, flags: *i64) -> i64 {
13 if cur < 0 { return -1 }
14 if choice < 0 { return -1 }
15 if choice >= SV_NCH { return -1 }
16 let idx: i64 = cur * SV_NCH + choice
17 let tgt: i64 = ch_target[idx]
18 if tgt < 0 { return -1 } // no such choice
19 let req: i64 = ch_req[idx]
20 if req > 0 { if (flags[0] & req) != req { return -1 } } // BLOCKED: not ALL required flag-bits set (req = bitmask, e.g. KEY|MAP)
21 let setf: i64 = ch_set[idx]
22 if setf > 0 { flags[0] = flags[0] | setf } // apply effect (set = bitmask of bits to raise)
23 return tgt
24}
25
26func sv_is_end(node_is_end: *i64, node: i64) -> i64 {
27 if node < 0 { return 0 }
28 return node_is_end[node]
29}
30
31func sv_has_flag(flags: *i64, bit: i64) -> i64 {
32 if (flags[0] & (1 << bit)) == 0 { return 0 }
33 return 1
34}