code wiki / _hdl_build / nx_companion_state.nx

nx_companion_state.nx source

↩ module page · 113 lines · 6948 B

1// nx_companion_state.nx -- the CompanionState object: the COGNITIVE CORE / neuro-chemical state (the evolving self). 2// State = 4 ints 0-100 in elara_state.txt [affection,dopamine,cortisol,oxytocin], updated per turn from sentiment + 3// decays to baseline, projected into her VOICE (mood line) + BODY (photo expression tokens) + a real-clock DAY-ARC. 4// Extracted from nx_gen_orchestrator (monolith -> objects); behaviour-preserving (bodies byte-identical except private 5// cs_* helpers; go_clamp100 stays internal). Object model: companion_arch.md. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7 8func cs_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i]; o=o+1; i=i+1} return o } 9func cs_itoa(dst: *u8, off: i64, v: i64) -> i64 { let t: *u8=sys_mmap(28); var m: i64=v; var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var o: i64=off; var q: i64=k-1; while q>=0{dst[o]=t[q];o=o+1;q=q-1} return o } 10func cs_contains_ci(hay: *u8, hlen: i64, needle: *u8) -> i64 { 11 var nl: i64 = 0; while needle[nl]!=(0 as u8) { nl=nl+1 } 12 if nl==0 { return 0 } 13 var i: i64 = 0 14 while i + nl <= hlen { 15 var j: i64 = 0; var ok: i64 = 1 16 while j < nl { 17 var a: i64 = hay[i+j]&0xff; var b2: i64 = needle[j]&0xff 18 if a>=65 { if a<=90 { a=a+32 } } 19 if b2>=65 { if b2<=90 { b2=b2+32 } } 20 if a!=b2 { ok=0; j=nl } else { j=j+1 } 21 } 22 if ok==1 { return 1 } 23 i=i+1 24 } 25 return 0 26} 27 28func go_clamp100(v: i64) -> i64 { if v < 0 { return 0 } if v > 100 { return 100 } return v } 29func go_state_load(st: *i64) -> i64 { 30 st[0]=50; st[1]=50; st[2]=20; st[3]=50 31 let lp: *i64 = sys_mmap(16) as *i64 32 let b: *u8 = sys_read_file("elara_state.txt" as *u8, lp) 33 if (b as i64) == 0 { return 0 } 34 let n: i64 = lp[0] 35 var idx: i64 = 0; var i: i64 = 0; var v: i64 = 0; var any: i64 = 0 36 while i < n { 37 let c: i64 = b[i]&0xff 38 if c>=48 { if c<=57 { v=v*10+(c-48); any=1 } else { if any==1 { if idx<4 { st[idx]=v } idx=idx+1; v=0; any=0 } } } 39 else { if any==1 { if idx<4 { st[idx]=v } idx=idx+1; v=0; any=0 } } 40 i=i+1 41 } 42 if any==1 { if idx<4 { st[idx]=v } } 43 return 0 44} 45func go_state_save(st: *i64) -> i64 { 46 let buf: *u8 = sys_mmap(64); var o: i64 = 0 47 o = cs_itoa(buf, o, st[0]); buf[o]=32 as u8; o=o+1 48 o = cs_itoa(buf, o, st[1]); buf[o]=32 as u8; o=o+1 49 o = cs_itoa(buf, o, st[2]); buf[o]=32 as u8; o=o+1 50 o = cs_itoa(buf, o, st[3]); buf[o]=10 as u8; o=o+1 51 let fd: i64 = sys_openat_wr("elara_state.txt" as *u8, 0x1a4) 52 if fd < 0 { return 0 - 1 } 53 sys_write(fd, buf, o); sys_close(fd); return 0 54} 55func go_state_update(st: *i64, msg: *u8, mlen: i64) -> i64 { 56 st[1] = st[1] + (50 - st[1])/6 57 st[2] = st[2] + (20 - st[2])/6 58 st[3] = st[3] + (50 - st[3])/6 59 var pos: i64 = 0; var neg: i64 = 0 60 if cs_contains_ci(msg, mlen, "love" as *u8)==1 { pos=pos+1 } 61 if cs_contains_ci(msg, mlen, "miss" as *u8)==1 { pos=pos+1 } 62 if cs_contains_ci(msg, mlen, "beautiful" as *u8)==1 { pos=pos+1 } 63 if cs_contains_ci(msg, mlen, "gorgeous" as *u8)==1 { pos=pos+1 } 64 if cs_contains_ci(msg, mlen, "thank" as *u8)==1 { pos=pos+1 } 65 if cs_contains_ci(msg, mlen, "cute" as *u8)==1 { pos=pos+1 } 66 if cs_contains_ci(msg, mlen, "sweet" as *u8)==1 { pos=pos+1 } 67 if cs_contains_ci(msg, mlen, "happy" as *u8)==1 { pos=pos+1 } 68 if cs_contains_ci(msg, mlen, "proud" as *u8)==1 { pos=pos+1 } 69 if cs_contains_ci(msg, mlen, "hate" as *u8)==1 { neg=neg+1 } 70 if cs_contains_ci(msg, mlen, "stupid" as *u8)==1 { neg=neg+1 } 71 if cs_contains_ci(msg, mlen, "shut up" as *u8)==1 { neg=neg+1 } 72 if cs_contains_ci(msg, mlen, "angry" as *u8)==1 { neg=neg+1 } 73 if cs_contains_ci(msg, mlen, "annoying" as *u8)==1 { neg=neg+1 } 74 if cs_contains_ci(msg, mlen, "idiot" as *u8)==1 { neg=neg+1 } 75 if pos > 0 { st[3]=st[3]+8*pos; st[1]=st[1]+6*pos; st[0]=st[0]+2*pos; st[2]=st[2]-5*pos } 76 if neg > 0 { st[2]=st[2]+12*neg; st[3]=st[3]-6*neg; st[1]=st[1]-4*neg; st[0]=st[0]-3*neg } 77 st[0]=go_clamp100(st[0]); st[1]=go_clamp100(st[1]); st[2]=go_clamp100(st[2]); st[3]=go_clamp100(st[3]) 78 return 0 79} 80// mood line appended to her persona (her CURRENT felt state, so her voice reflects the relationship arc) 81func go_mood_line(st: *i64, out: *u8) -> i64 { 82 var o: i64 = 0 83 if st[2] > 60 { o = cs_cat(out, 0, " Right now you feel a little hurt and guarded, quieter and more sensitive than usual." as *u8) } 84 else { if st[3] > 70 { if st[0] > 60 { o = cs_cat(out, 0, " Right now you feel especially close, warm and deeply in love with him." as *u8) } else { o = cs_cat(out, 0, " Right now you feel warm, soft and affectionate." as *u8) } } 85 else { if st[1] > 70 { o = cs_cat(out, 0, " Right now you are in a bright, giddy, playful and excited mood." as *u8) } else { o = 0 } } } 86 out[o]=0 as u8 87 return o 88} 89// expression tokens appended to her photo prompt (her body shows her felt state = emotion->visual) 90func go_expr_tokens(st: *i64, out: *u8) -> i64 { 91 var o: i64 = 0 92 if st[2] > 60 { o = cs_cat(out, 0, ", subtle bittersweet strained expression, slightly sad glistening eyes" as *u8) } 93 else { if st[3] > 70 { o = cs_cat(out, 0, ", warm loving affectionate smile, soft adoring gaze into the camera" as *u8) } 94 else { if st[1] > 70 { o = cs_cat(out, 0, ", bright happy playful grin, sparkling excited eyes" as *u8) } else { o = cs_cat(out, 0, ", relaxed natural expression, soft gentle smile" as *u8) } } } 95 out[o]=0 as u8 96 return o 97} 98// DAY-ARC: she lives a day. Her current activity/location advances with the real clock (Utah MDT = UTC-6). 99func go_day_scene(out: *u8, off: i64) -> i64 { 100 let epoch: i64 = sys_now_realtime_sec() 101 var h: i64 = (epoch / 3600) - 6 102 h = h % 24 103 if h < 0 { h = h + 24 } 104 var o: i64 = off 105 if h < 5 { o = cs_cat(out, o, " It is the middle of the night for you right now; you are sleepy in bed, cozy under the covers." as *u8) } 106 else { if h < 9 { o = cs_cat(out, o, " It is early morning for you; you just woke up and are sipping coffee in the kitchen in a cozy tank top, hair a little messy." as *u8) } 107 else { if h < 12 { o = cs_cat(out, o, " It is mid-morning; you are out at a cafe or running errands, feeling fresh and bright." as *u8) } 108 else { if h < 15 { o = cs_cat(out, o, " It is early afternoon; you are relaxing at home reading, or out at the beach if it is sunny." as *u8) } 109 else { if h < 19 { o = cs_cat(out, o, " It is late afternoon; you are cooking or getting ready for the evening in comfy clothes." as *u8) } 110 else { if h < 23 { o = cs_cat(out, o, " It is evening; you are winding down on the couch, cozy and a little flirty, missing him." as *u8) } 111 else { o = cs_cat(out, o, " It is late night; you are in bed in something soft and comfy, sleepy." as *u8) } } } } } } 112 return o 113}