refactor(packages): 0.R5 - a question no longer starts its state - 64 lazy starts ('if st.x == null { st.x = ... }' inside a getter) are the state's defaults, gear__ensure is gone, and ludic migrate state --tighten took mut off 208 package parameters: things_all, thing_find, gear_charge, gear_level, jobs_state and the rest read

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-27 00:51:03 +03:00
parent 54f5efb23f
commit 9862ec82e3
102 changed files with 320 additions and 351 deletions

View file

@ -73,7 +73,6 @@ export function nr_text(net_st: mut NetState, cap: int) -> string {
net_st.net__rbad = true
return ""
}
if net_st.net__tb == null { net_st.net__tb = buffer(NET_MTU) }
nb_copy(net_st.net__tb, 0, net_st.net__rb, net_st.net__rp, n)
net_st.net__rp += n
return text_of(net_st.net__tb, n)

View file

@ -61,17 +61,15 @@ export port NetSocket {
export function net_config(net_st: mut NetState, c: NetConfig) -> void { net_st.net__cfg = c }
function net__conf(net_st: mut NetState) -> NetConfig {
if net_st.net__cfg == null { net_st.net__cfg = new NetConfig }
function net__conf(net_st: NetState) -> NetConfig {
return net_st.net__cfg
}
export function net_facts(net_st: mut NetState) -> Queue<NetFact> {
if net_st.net__facts == null { net_st.net__facts = queue_new("net.facts") }
export function net_facts(net_st: NetState) -> Queue<NetFact> {
return net_st.net__facts
}
function net__fact(net_st: mut NetState, what: int, slot: int, pid: int, why: int, text: string) -> void {
function net__fact(net_st: NetState, what: int, slot: int, pid: int, why: int, text: string) -> void {
let f = new NetFact
f.what = what
f.slot = slot

View file

@ -4,7 +4,6 @@
function net__new_guest(net_st: mut NetState, pub: string, local: string) -> void {
let key = `{pub}|{local}`
if net_st.net__seen == null { net_st.net__seen = new []string }
for i in 0 .. len(net_st.net__seen) { if net_st.net__seen[i] == key { return } }
push(net_st.net__seen, key)
net__log(`room {net_st.net__room}: a guest at {pub} / {local}`)

View file

@ -77,7 +77,7 @@ function net__messages(net_st: mut NetState, slot: int, n: int) -> void {
}
# onto the inbox - except a heartbeat, and on a host anything but a hello before its welcome
function net__deliver(net_st: mut NetState, slot: int, from: int, kind: int, o: int, ml: int) -> void {
function net__deliver(net_st: NetState, slot: int, from: int, kind: int, o: int, ml: int) -> void {
if kind == NET_BEAT { return }
if net_st.net__state == NET_HOSTING and net_st.net__peers[slot].pid < 2 and kind != NET_HELLO { return }
let m = new NetMessage

View file

@ -38,7 +38,7 @@ function net__use_relay(net_st: mut NetState) -> void {
export function net_via_relay(net_st: NetState) -> bool { return net_st.net__via_relay }
function net__relayed(net_st: mut NetState, p: NetPeer) -> bool {
function net__relayed(net_st: NetState, p: NetPeer) -> bool {
return net_st.net__relay_ip != 0 and p.ip == net_st.net__relay_ip and (p.port >= net__conf(net_st).relay_port0 or net_st.net__via_relay)
}

View file

@ -10,7 +10,7 @@ const NET_R_DONE: int = 5 # a guest: the hello is on its way
# room codes are on when the game named a matchmaker
export function net_room_on(net_st: mut NetState) -> bool { return len(net__conf(net_st).signal_url) > 0 }
export function net_room_on(net_st: NetState) -> bool { return len(net__conf(net_st).signal_url) > 0 }
export function net_room(net_st: NetState) -> string { return net_st.net__room }
export function net_room_asking(net_st: NetState) -> bool { return net_st.net__r_state == NET_R_STUN or net_st.net__r_state == NET_R_ASK }

View file

@ -48,9 +48,9 @@ export property NetMessage {
}
export state NetState {
net__cfg: NetConfig = null
net__facts: Queue<NetFact> = null
net__seen: []string = null # guests already punched for
net__cfg: NetConfig = new NetConfig
net__facts: Queue<NetFact> = net__facts__new()
net__seen: []string = new []string # guests already punched for
net__punch_ip: words = null
net__punch_port: words = null
net__punch_left: words = null
@ -88,7 +88,7 @@ export state NetState {
net__rl: int = 0
net__rp: int = 0
net__rbad: bool = false
net__tb: []byte = null # text read out of a message, through one scratch buffer
net__tb: []byte = buffer(NET_MTU) # text read out of a message, through one scratch buffer
net__hello_b: []byte = null # the game's hello, sent again on every dial
net__hello_l: int = 0
net__hello_t: float = 0.0 # seconds since it went; -1 not yet
@ -100,15 +100,14 @@ export state NetState {
net__code_port: int = 0
net__bytes_out: int = 0
net__kind_out: words = null
net__inbox: Queue<NetMessage> = null
net__inbox: Queue<NetMessage> = net__inbox__new()
net__stun_ip: int = 0
net__stun_port: int = 0
net__txn: words = null
net__txn: words = words(3)
net__dice: Rng = null
}
export function net_inbox(net_st: mut NetState) -> Queue<NetMessage> {
if net_st.net__inbox == null { net_st.net__inbox = queue_new("net.inbox") }
export function net_inbox(net_st: NetState) -> Queue<NetMessage> {
return net_st.net__inbox
}
@ -126,3 +125,5 @@ function net__init(net_st: mut NetState) -> void {
net_st.net__rb = buffer(NET_MTU)
net_st.net__hello_b = buffer(NET_MTU)
}
function net__facts__new() -> Queue<NetFact> { return queue_new("net.facts") }
function net__inbox__new() -> Queue<NetMessage> { return queue_new("net.inbox") }

View file

@ -2,7 +2,6 @@
# from outside: the router's address and the port it mapped
function net__stun_send(net_st: mut NetState) -> void {
if net_st.net__txn == null { net_st.net__txn = words(3) }
if net_st.net__dice == null { net_st.net__dice = rng_new(net__conf(net_st).seed) }
if net_st.net__stun_ip == 0 {
let c = net__conf(net_st)

View file

@ -97,7 +97,7 @@ program NetTest {
return net_accept(net_st, got[0].slot)
}
function facts_of(net_st: mut NetState, what: int) -> []NetFact {
function facts_of(net_st: NetState, what: int) -> []NetFact {
let all = q_drain(net_facts(net_st))
let out = new []NetFact
for i in 0 .. len(all) { if all[i].what == what { push(out, all[i]) } }